snowbin.c 11.7 KB
Newer Older
hark's avatar
hark committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* [name], A player for snowmix
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#define SNOWBIN

#ifdef CLI
#include "s2s-cli.h"
#else
#include "common.h"
#endif

hark's avatar
hark committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

  int
get_vars (snowMixer *mixer, snowFeed *feed)
{
  FILE *fp;
  char line[1035];
  regex_t regex;
  int retsnow;
  char msgbuf[100];
  size_t     nmatch = 3;
  regmatch_t pmatch[3];
  char key[100], value[100];

  /* Compile regular expression */
  //  retsnow = regcomp(&regex, "^Snowmix[[:print:]]*", 0);
  //  retsnow = regcomp(&regex, "^[_[:alnum:]]*[[:punct:]][[:blank:]][[:alnum:]]*", REG_EXTENDED);
  retsnow = regcomp(&regex, "^\([_[:alnum:]]*\)[[:punct:]][[:blank:]]\([[:print:]]*\)", REG_EXTENDED);


  if (retsnow) {
    fprintf(stderr, "Could not compile regex\n");
    exit(1);
  }

  /* Open the command for reading. */
  fp = popen("./getvars.sh 2", "r");
  if (fp == NULL) {
    printf("Failed to run command\n" );
    exit(1);
  }

  while (fgets(line, sizeof(line)-1, fp) != NULL) {

    retsnow = regexec(&regex, line, nmatch, pmatch, 0);


    if (!retsnow) {
      //puts("Match");
      //      printf("%s m1: %s m2: %s m3: %s", line, pmatch[0], pmatch[1], pmatch[2]);
      int n = 0;

     /* 
         printf("With the whole expression, "
         "a matched substring \"%.*s\" is found at position %d to %d.\n",
         pmatch[n].rm_eo - pmatch[n].rm_so, &line[pmatch[n].rm_so],
         pmatch[n].rm_so, pmatch[n].rm_eo - 1);
       */  
      n = 1;
//      snprintf(key, sizeof(&key), "%.*s",pmatch[n].rm_eo - pmatch[n].rm_so, &line[pmatch[n].rm_so],pmatch[n].rm_so, pmatch[n].rm_eo - 1);
//                                      |             lenght               |    start?             |
      snprintf(key, sizeof(key), "%.*s",pmatch[n].rm_eo - pmatch[n].rm_so, &line[pmatch[n].rm_so]);


      n = 2;
//      snprintf(value, sizeof(&value), "%.*s",pmatch[n].rm_eo - pmatch[n].rm_so, &line[pmatch[n].rm_so],pmatch[n].rm_so, pmatch[n].rm_eo - 1);
 
      snprintf(value, sizeof(value), "%.*s",pmatch[n].rm_eo - pmatch[n].rm_so, &line[pmatch[n].rm_so]);
//      printf("keyvalue:  %s %s  \n ", key, value);

      switchs(key){
        cases("host")
          mixer->host = strdup(value);
        break;
        cases("port")
          mixer->port = atoi(value);
        break;
        cases("feed_id")
          feed->id = atoi(value);
        break;
        cases("feed_rate")
          feed->audio_rate = atoi(value);
        break;
        cases("feed_width")
          feed->width = atoi(value);
         break;
        cases("feed_height")
          feed->height = atoi(value);
        break;
        cases("feed_control_pipe")
          feed->control_pipe = strdup(value);
        break;
        cases("ctrsocket")
        //  strncpy(mixer->ctrlsocket, value, 100);
        mixer->control_socket = strdup(value);
        break;
        cases("system_width")
          mixer->width = atoi(value);
        break;
        cases("system_height")
          mixer->height = atoi(value);
        break;
        cases("ratefraction")
//          strncpy(mixer->ratefraction, value, 100);
        mixer->ratefraction = strdup(value);
          break;
        cases("snowmix")
//          strncpy(mixer->state, value, 100);
        mixer->state = strdup(value);
          break;
        cases("channels")
          mixer->audio_channels = atoi(value);
        break;
        cases("feed_channels")
          feed->audio_channels = atoi(value);
        break;
        cases("rate")
          mixer->audio_rate = atoi(value);
        break;
        defaults
            printf("FIX ME: no match for key of %s=%s \n", key, value);
        break;

      } switchs_end;
//      printf("%u", __done); 


    }
    else if (retsnow == REG_NOMATCH) {
      //puts("No match");
      printf("FIX ME: no match for line: %s", line);
    }
    else {
      regerror(retsnow, &regex, msgbuf, sizeof(msgbuf));
      fprintf(stderr, "Regex match failed: %s\n", msgbuf);
      exit(1);
    }

  }

  /* Free compiled regular expression if you want to use the regex_t again */
  regfree(&regex);
  /* close */
  pclose(fp);

  return 0;
}




hark's avatar
hark committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
extern void
print_snowMixer (snowMixer *m)
{
  printf("\n \n snowMixer: \n host %s \n port %u \n control_socket %s \n state %s \n width %u \n height %u \n ratefraction %s \n audio_rate %u \n audio_channels %u \n",
          m->host,
          m->port,
          m->control_socket,
          m->state,
          m->width,
          m->height,
          m->ratefraction,
          m->audio_rate,
          m->audio_channels);
}
extern void
print_snowFeed (snowFeed *f)
{
  printf("\n \n snowFeed \nid %u \n control_pipe %s \n width %u \n height %u \n audio_channels %u \n audio_rate %u \n ",
      f->id,
      f->control_pipe,
      f->width,
      f->height,
      f->audio_channels,
      f->audio_rate);
}



  extern GstElement *
get_snowvideobin (snowData *sd)
hark's avatar
hark committed
198
199
{
  GstElement *videorate, *videoconvert, *capsfilter,
hark's avatar
hark committed
200
             *videoscale, *shmsink, *queue;
hark's avatar
hark committed
201
202
  GstPad *vpad, *vghostpad;
  GstElement *snowvideobin;
hark's avatar
hark committed
203
204
205
206
207
208
209
210
  char *feedname = "feed1";
  snowFeed *feed;
  snowMixer *mixer;
  feed = sd->currentFeed;
  mixer = sd->mixer;
  printf("======= get snowvideobin ===============");
  print_snowFeed(feed);
  snowvideobin = gst_bin_new ("Snowvideobin");
hark's avatar
hark committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231


  videorate = gst_element_factory_make ("videorate", "videorate");
#ifdef GST1
  videoconvert = gst_element_factory_make ("videoconvert", "videoconvert");

#else
  videoconvert =
    gst_element_factory_make ("ffmpegcolorspace", "videoconvert");

#endif

  videoscale = gst_element_factory_make ("videoscale", "videoscale");
  queue = gst_element_factory_make ("queue", "queue");


  /* caps for snowmix */
  /* video/x-raw-rgb, bpp=(int)32, depth=(int)32, endianness=(int)4321, red_mask=(int)65280, green_mask=(int)16711680, blue_mask=(int)-16777216, width=(int)$WIDTH, height=(int)$HEIGHT, framerate=(fraction)$FRAMERATE/1, pixel-aspect-ratio=(fraction)1/1, interlaced=(boolean)false" */

#ifdef GST1
  GstCaps *caps = gst_caps_new_simple ("video/x-raw",
hark's avatar
hark committed
232
233
      "format", G_TYPE_STRING, "BGRA",
      //"interlace-mode", G_TYPE_STRING, "progressive",
hark's avatar
hark committed
234
235
      "width", G_TYPE_INT, feed->width,
      "height", G_TYPE_INT, feed->height,
hark's avatar
hark committed
236
237
238
      //"pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
      "framerate", GST_TYPE_FRACTION, 25, 1,
      NULL);
hark's avatar
hark committed
239
240
241
#else

  GstCaps *caps = gst_caps_new_simple ("video/x-raw-rgb",
hark's avatar
hark committed
242
243
244
245
246
247
248
      "bpp", G_TYPE_INT, 32,
      "depth", G_TYPE_INT, 32,
      "endianness", G_TYPE_INT, 4321,
      "red_mask", G_TYPE_INT, 65280,
      "green_mask", G_TYPE_INT, 16711680,
      "blue_mask", G_TYPE_INT, -16777216,
      "interlaced", G_TYPE_BOOLEAN, FALSE,
hark's avatar
hark committed
249
250
      "width", G_TYPE_INT, feed->width,
      "height", G_TYPE_INT, feed->height,
hark's avatar
hark committed
251
252
253
254
      "pixel-aspect-ratio",
      GST_TYPE_FRACTION, 4, 3,
      "framerate", GST_TYPE_FRACTION, 25, 1,
      NULL);
hark's avatar
hark committed
255
256
257
258
259
260
261
262
263
#endif

  capsfilter = gst_element_factory_make ("capsfilter", "capsfilter");

  g_object_set (videoscale, "add-borders", TRUE, NULL);
  g_object_set (G_OBJECT (capsfilter), "caps", caps, NULL);

  shmsink = gst_element_factory_make ("shmsink", "shmsink");

hark's avatar
hark committed
264
  remove (feed->control_pipe);
hark's avatar
hark committed
265

hark's avatar
hark committed
266
  g_object_set (shmsink, "socket-path", feed->control_pipe, NULL);
hark's avatar
hark committed
267
#ifndef GST1 //gst0.10
hark's avatar
hark committed
268
269
270
271
272
273
274
  g_object_set (shmsink, "shm-size", "23224320", NULL);

#endif
  g_object_set (shmsink, "wait-for-connection", FALSE, NULL);
#ifdef GST1
  /* add and link elements in the bin */
  gst_bin_add_many (GST_BIN (snowvideobin), queue, videorate, videoscale,
hark's avatar
hark committed
275
      videoconvert, capsfilter, shmsink, NULL);
hark's avatar
hark committed
276
277
278

  if( !gst_element_link_many (queue, videorate, videoscale, videoconvert,capsfilter, shmsink, NULL)) {printf("linking snowbin failed");}

hark's avatar
hark committed
279
#else //gst0.10
hark's avatar
hark committed
280
  gst_bin_add_many (GST_BIN (snowvideobin), queue, videorate, videoscale,
hark's avatar
hark committed
281
      videoconvert, capsfilter, shmsink, NULL);
hark's avatar
hark committed
282
283

  gst_element_link_many (queue, videorate, videoscale, videoconvert,
hark's avatar
hark committed
284
      capsfilter, shmsink, NULL);
hark's avatar
hark committed
285
286
287
288
289
290
291
292
293
294
295
296
297
#endif

  vpad = gst_element_get_static_pad (queue, "sink");
  vghostpad = gst_ghost_pad_new ("sink", vpad);

  gst_pad_set_active (vghostpad, TRUE);
  gst_element_add_pad (snowvideobin, vghostpad);

  gst_object_unref (vpad);

  return snowvideobin;
}

hark's avatar
hark committed
298
  extern GstElement *
hark's avatar
hark committed
299
get_jacksink (int feedid)
hark's avatar
hark committed
300
301
{
  GstElement *js;
hark's avatar
hark committed
302
  char *feedname = "feedfuck";
hark's avatar
hark committed
303
304
305
306
307
308
  js = gst_element_factory_make ("jackaudiosink", "jackaudiosink");


  /* check for jackportpattern */
  if (g_object_class_find_property
      (G_OBJECT_GET_CLASS (js), "port-pattern"))
hark's avatar
hark committed
309
  {
hark's avatar
hark committed
310

hark's avatar
hark committed
311
    char *portpattern = "feedfuck/audio_in";
hark's avatar
hark committed
312
313
314
    asprintf (&portpattern, "feed%u/audio_in", feedid);
    asprintf (&feedname, "feed%u", feedid);
    g_print ("snowaudiobin: feed: %u portpattern is: : %s : \n", feedid, portpattern);
hark's avatar
hark committed
315

hark's avatar
hark committed
316
317
    g_object_set (G_OBJECT (js), "port-pattern", portpattern,
        NULL);
hark's avatar
hark committed
318
  }
hark's avatar
hark committed
319
320
    g_object_set (G_OBJECT (js), "client-name", feedname,
        NULL);
hark's avatar
hark committed
321
322
323
  return js;
}

hark's avatar
hark committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  extern GstElement *
get_snowsink (int feedid)
{
  GstElement *fdsink, *abin;
  char *feedname = "feedfuck";
  asprintf (&feedname, "feed%u", feedid);
  abin = gst_bin_new("snowmix socket");
 
  // file descriptor sink
  fdsink     = gst_element_factory_make("fdsink","fdsink");
 
// start nc
  int filedes[2];
  if (pipe(filedes) == -1) 
  {
    perror("pipe");
    exit(1);
  }

  pid_t pid = fork();
  if (pid == -1) 
  {
    perror("fork");
    //exit(1);
  } 
  else if (pid == 0) 
  {
    while ((dup2(filedes[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {}
    close(filedes[1]);
    close(filedes[0]);
    printf("executing nc\n");
    execl("/bin/nc", "nc", "--stdout", "127.0.0.1", "9999");
    perror("execl");
    //_exit(1);
  }
  
  close(filedes[1]);

  printf("fd: %i\n", filedes[0]);
// set filedescriptor
  g_object_set( G_OBJECT (fdsink), "fd", filedes[0], NULL);


  return abin;
}


hark's avatar
hark committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
extern int
snowbin_init_snowData (PlayerData *s) 
{

  struct snowData *sd;
  struct snowMixer *mixer;
  struct snowFeed *feed;
  sd = (snowData*)malloc(sizeof(struct snowData));
  mixer = (snowMixer*)malloc(sizeof(struct snowMixer));
  mixer->host = "bla"; //host
  mixer->control_socket = "bla"; //ctrsocket
  mixer->state = "bla"; //snowmix
  mixer->ratefraction = "bla"; //ratefraction

  feed = (snowFeed*)malloc(sizeof(struct snowFeed));
  feed->control_pipe = "bla"; //feed_control_pipe

  get_vars(mixer, feed);

//  print_snowMixer(snow->mixer);
//  print_snowFeed(snow->currentFeed);

  sd->currentFeed = feed;
  sd->mixer = mixer;
  s->snow = sd;
  return 0;
}


hark's avatar
hark committed
400
401
  extern GstElement *
get_snowaudiobin (snowData *sd)
hark's avatar
hark committed
402
403
404
405
{
  GstElement *jackaudiosink, *audioconvert;
  GstPad *apad, *aghostpad;
  GstElement *snowaudiobin;
hark's avatar
hark committed
406
407
408
  char *feedname = "feedfuck";
  asprintf (&feedname, "feed%u", sd->currentFeed->id);
 
hark's avatar
hark committed
409
410
  snowaudiobin = gst_bin_new ("snowaudiobin");

hark's avatar
hark committed
411
  jackaudiosink = get_jacksink(sd->currentFeed->id);
hark's avatar
hark committed
412
 
hark's avatar
hark committed
413
414
415
  audioconvert = gst_element_factory_make ("audioconvert", "audioconvert");

  gst_bin_add_many (GST_BIN (snowaudiobin), audioconvert, jackaudiosink,
hark's avatar
hark committed
416
      NULL);
hark's avatar
hark committed
417
418
419
420
421
422
423
424
425
426
427
428
429
  gst_element_link_many (audioconvert, jackaudiosink, NULL);

  apad = gst_element_get_static_pad (audioconvert, "sink");
  aghostpad = gst_ghost_pad_new ("sink", apad);

  gst_pad_set_active (aghostpad, TRUE);
  gst_element_add_pad (snowaudiobin, aghostpad);

  gst_object_unref (apad);

  return snowaudiobin;

}