summaryrefslogtreecommitdiff
path: root/gst/autoplug/gstspideridentity.c
blob: 0718df5bb6b9d5d0ac3df4479efeef11d8d1d358 (plain)
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
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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/* GStreamer
 * Copyright (C) 2002 Erik Walthinsen <omega@cse.ogi.edu>
 *               2002 Wim Taymans <wtay@chello.be>
 *
 * gstspideridentity.c: identity element for the spider autoplugger
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "gstspideridentity.h"
#include "gstspider.h"

GST_DEBUG_CATEGORY_STATIC (gst_spider_identity_debug);
#define GST_CAT_DEFAULT gst_spider_identity_debug

static GstElementDetails gst_spider_identity_details = GST_ELEMENT_DETAILS (
  "SpiderIdentity",
  "Generic",
  "Link between spider and outside elements",
  "Benjamin Otte <in7y118@public.uni-hamburg.de>"
);


/* generic templates 
 * delete me when meging with spider.c
 */
static GstStaticPadTemplate spider_src_factory =
GST_STATIC_PAD_TEMPLATE (
  "src",
  GST_PAD_SRC,
  GST_PAD_ALWAYS,
  GST_STATIC_CAPS2_ANY
);

static GstStaticPadTemplate spider_sink_factory =
GST_STATIC_PAD_TEMPLATE (
  "sink",
  GST_PAD_SINK,
  GST_PAD_ALWAYS,
  GST_STATIC_CAPS2_ANY
);

/* SpiderIdentity signals and args */
enum {
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  ARG_0,
  /* FILL ME */
};

/* GObject stuff */
static void			gst_spider_identity_class_init		(GstSpiderIdentityClass *klass);
static void			gst_spider_identity_init		(GstSpiderIdentity *spider_identity);

/* functions set in pads, elements and stuff */
static void			gst_spider_identity_chain		(GstPad *pad, GstBuffer *buf);
static GstElementStateReturn	gst_spider_identity_change_state	(GstElement *element);
static GstPadLinkReturn		gst_spider_identity_link		(GstPad *pad, const GstCaps2 *caps);
static GstCaps2 *		gst_spider_identity_getcaps		(GstPad *pad);
/* loop functions */
static void			gst_spider_identity_dumb_loop		(GstSpiderIdentity *ident);
static void                     gst_spider_identity_src_loop		(GstSpiderIdentity *ident);
static void                     gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident);

static gboolean 		gst_spider_identity_handle_src_event 	(GstPad *pad, GstEvent *event);

/* other functions */
static void			gst_spider_identity_start_type_finding	(GstSpiderIdentity *ident);

static GstElementClass *	parent_class				= NULL;
/* no signals
static guint gst_spider_identity_signals[LAST_SIGNAL] = { 0 }; */

GType
gst_spider_identity_get_type (void) 
{
  static GType spider_identity_type = 0;

  if (!spider_identity_type) {
    static const GTypeInfo spider_identity_info = {
      sizeof(GstSpiderIdentityClass),      NULL,
      NULL,
      (GClassInitFunc)gst_spider_identity_class_init,
      NULL,
      NULL,
      sizeof(GstSpiderIdentity),
      0,
      (GInstanceInitFunc)gst_spider_identity_init,
    };
    spider_identity_type = g_type_register_static (GST_TYPE_ELEMENT, "GstSpiderIdentity", 
						   &spider_identity_info, 0);
    GST_DEBUG_CATEGORY_INIT (gst_spider_identity_debug, "spideridentity", 
			     0, "spider autoplugging proxy element");
  }
  return spider_identity_type;
}

static void 
gst_spider_identity_class_init (GstSpiderIdentityClass *klass) 
{
  GstElementClass *gstelement_class = (GstElementClass *) klass;
  
  parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
  
  /* add our two pad templates */
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&spider_src_factory));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&spider_sink_factory));
  gst_element_class_set_details (gstelement_class, &gst_spider_identity_details);
  
  gstelement_class->change_state = GST_DEBUG_FUNCPTR(gst_spider_identity_change_state);
  gstelement_class->request_new_pad = GST_DEBUG_FUNCPTR(gst_spider_identity_request_new_pad);
}

static void 
gst_spider_identity_init (GstSpiderIdentity *ident) 
{
  /* sink */
  ident->sink = gst_pad_new_from_template (
      gst_static_pad_template_get (&spider_sink_factory), "sink");
  gst_element_add_pad (GST_ELEMENT (ident), ident->sink);
  gst_pad_set_link_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
  gst_pad_set_getcaps_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
  /* src */
  ident->src = gst_pad_new_from_template (
      gst_static_pad_template_get (&spider_src_factory), "src");
  gst_element_add_pad (GST_ELEMENT (ident), ident->src);
  gst_pad_set_link_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
  gst_pad_set_getcaps_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
  gst_pad_set_event_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_handle_src_event));

  /* variables */
  ident->plugged = FALSE;
}

static void 
gst_spider_identity_chain (GstPad *pad, GstBuffer *buf) 
{
  GstSpiderIdentity *ident;
  
  /*g_print ("chaining on pad %s:%s with buffer %p\n", GST_DEBUG_PAD_NAME (pad), buf);*/

  g_return_if_fail (pad != NULL);
  g_return_if_fail (GST_IS_PAD (pad));
  if (buf == NULL) return;

  ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));

  if (GST_IS_EVENT (buf)) {
    /* start hack for current event stuff here */
    /* check for unlinked elements and send them the EOS event, too */
    if (GST_EVENT_TYPE (GST_EVENT (buf)) == GST_EVENT_EOS)
    {
      GstSpider *spider = (GstSpider *) GST_OBJECT_PARENT (ident);
      GList *list = spider->links;
      while (list)
      {
	GstSpiderConnection *conn = (GstSpiderConnection *) list->data;
	list = g_list_next (list);
        if (conn->current != (GstElement *) conn->src) {
          GST_DEBUG ("sending EOS to unconnected element %s from %s", 
	       GST_ELEMENT_NAME (conn->src), GST_ELEMENT_NAME (ident));
          gst_pad_push (conn->src->src, GST_DATA (GST_BUFFER (gst_event_new (GST_EVENT_EOS))));  
          gst_element_set_eos (GST_ELEMENT (conn->src));
	}
      }
    }
    /* end hack for current event stuff here */

    gst_pad_event_default (pad, GST_EVENT (buf));
    return;
  }

  if ((ident->src != NULL) && (GST_PAD_PEER (ident->src) != NULL)) {
    /* g_print("pushing buffer %p (refcount %d - buffersize %d) to pad %s:%s\n", buf, GST_BUFFER_REFCOUNT (buf), GST_BUFFER_SIZE (buf), GST_DEBUG_PAD_NAME (ident->src)); */
    GST_LOG ( "push %p %" G_GINT64_FORMAT, buf, GST_BUFFER_OFFSET (buf));
    gst_pad_push (ident->src, GST_DATA (buf));
  } else if (GST_IS_BUFFER (buf)) {
    gst_buffer_unref (buf);
  }
}
GstSpiderIdentity*           
gst_spider_identity_new_src (gchar *name)
{
  GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);
  /* set the right functions */
  gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_src_loop));
  
  return ret;
}
GstSpiderIdentity*           
gst_spider_identity_new_sink (gchar *name)
{
  GstSpiderIdentity *ret = (GstSpiderIdentity *) gst_element_factory_make ("spideridentity", name);

  /* set the right functions */
  gst_element_set_loop_function (GST_ELEMENT (ret), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));

  return ret;
}

/* shamelessly stolen from gstqueue.c to get proxy links */
static GstPadLinkReturn
gst_spider_identity_link (GstPad *pad, const GstCaps2 *caps)
{
  GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
  GstPad *otherpad;

  if (pad == spider_identity->src) 
    otherpad = spider_identity->sink;
  else
    otherpad = spider_identity->src;

  if (otherpad != NULL)
    return gst_pad_proxy_link (otherpad, caps);
  
  return GST_PAD_LINK_OK;
}

static GstCaps2*
gst_spider_identity_getcaps (GstPad *pad)
{
  GstSpiderIdentity *spider_identity = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));
  GstPad *otherpad;
  GstPad *peer;

  if (pad == spider_identity->src) 
    otherpad = spider_identity->sink;
  else
    otherpad = spider_identity->src;

  if (otherpad != NULL) {
    peer = GST_PAD_PEER (otherpad);

    if (peer)
      return gst_pad_get_caps (peer);
  }
  return gst_caps2_new_any ();
}

GstPad*
gst_spider_identity_request_new_pad  (GstElement *element, GstPadTemplate *templ, const gchar *name)
{
  GstSpiderIdentity *ident;
  
  /*checks */
  g_return_val_if_fail (templ != NULL, NULL);
  g_return_val_if_fail (GST_IS_PAD_TEMPLATE (templ), NULL);
  ident = GST_SPIDER_IDENTITY (element);
  g_return_val_if_fail (ident != NULL, NULL);
  g_return_val_if_fail (GST_IS_SPIDER_IDENTITY (ident), NULL);
  
  switch (GST_PAD_TEMPLATE_DIRECTION (templ))
  {
    case GST_PAD_SINK:
      if (ident->sink != NULL) break;
      /* sink */
      GST_DEBUG ( "element %s requests new sink pad", GST_ELEMENT_NAME(ident));
      ident->sink = gst_pad_new ("sink", GST_PAD_SINK);
      gst_element_add_pad (GST_ELEMENT (ident), ident->sink);
      gst_pad_set_link_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
      gst_pad_set_getcaps_function (ident->sink, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
      return ident->sink;
    case GST_PAD_SRC:
      /* src */
      if (ident->src != NULL) break;
      GST_DEBUG ( "element %s requests new src pad", GST_ELEMENT_NAME(ident));
      ident->src = gst_pad_new ("src", GST_PAD_SRC);
      gst_element_add_pad (GST_ELEMENT (ident), ident->src);
      gst_pad_set_link_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_link));
      gst_pad_set_getcaps_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_getcaps));
      gst_pad_set_event_function (ident->src, GST_DEBUG_FUNCPTR (gst_spider_identity_handle_src_event));
      return ident->src;
    default:
      break;
  }
  
  GST_DEBUG ( "element %s requested a new pad but none could be created", GST_ELEMENT_NAME(ident));
  return NULL;
}

/* this function has to
 * - start the autoplugger
 * - start type finding
 * ...
 */
static GstElementStateReturn
gst_spider_identity_change_state (GstElement *element)
{
  GstSpiderIdentity *ident;
  GstSpider *spider;
  GstElementStateReturn ret = GST_STATE_SUCCESS;
  
  /* element check */
  ident = GST_SPIDER_IDENTITY (element);
  g_return_val_if_fail (ident != NULL, GST_STATE_FAILURE);
  g_return_val_if_fail (GST_IS_SPIDER_IDENTITY (ident), GST_STATE_FAILURE);
  
  switch (GST_STATE_TRANSITION (element)) {
    case GST_STATE_PAUSED_TO_PLAYING:
      /* autoplugger check */
      spider = GST_SPIDER (GST_ELEMENT_PARENT (ident));
      g_return_val_if_fail (spider != NULL, GST_STATE_FAILURE);
      g_return_val_if_fail (GST_IS_SPIDER (spider), GST_STATE_FAILURE);
  
      /* start typefinding or plugging */
      if ((GST_RPAD_PEER (ident->sink) != NULL) && (GST_RPAD_PEER (ident->src) == NULL))
      {
        if (gst_pad_get_caps ((GstPad *) GST_PAD_PEER (ident->sink)) == NULL)
        {
          gst_spider_identity_start_type_finding (ident);
          break;
        } else {
          gst_spider_identity_plug (ident);
        }
      }
      /* autoplug on src */
      if ((GST_RPAD_PEER (ident->src) != NULL) && (GST_RPAD_PEER (ident->sink) == NULL))
      {
        gst_spider_identity_plug (ident);
      }
    default:
      break;
  }
  
  if ((ret != GST_STATE_FAILURE) && (GST_ELEMENT_CLASS (parent_class)->change_state))
    ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
  
  return ret;
}

static void
gst_spider_identity_start_type_finding (GstSpiderIdentity *ident)
{
/*  GstElement* typefind;
  gchar *name;*/
  gboolean restart = FALSE;
  
  GST_DEBUG ("element %s starts typefinding", GST_ELEMENT_NAME(ident));
  if (GST_STATE (GST_ELEMENT_PARENT (ident)) == GST_STATE_PLAYING)
  {
    gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PAUSED);
    restart = TRUE;
  }

  gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_sink_loop_type_finding));

  if (restart)
  {
    gst_element_set_state (GST_ELEMENT (GST_ELEMENT_PARENT (ident)), GST_STATE_PLAYING);
  }
}

/* since we can't set the loop function to NULL if there's a cothread for us,
 * we have to use a dumb one
 */
static void
gst_spider_identity_dumb_loop  (GstSpiderIdentity *ident)
{
  GstBuffer *buf;

  g_return_if_fail (ident != NULL);
  g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
  g_assert (ident->sink != NULL);

  buf = GST_BUFFER (gst_pad_pull (ident->sink));

  gst_spider_identity_chain (ident->sink, buf);
}
/* do nothing until we're linked - then disable yourself
 */
static void
gst_spider_identity_src_loop (GstSpiderIdentity *ident)
{
  /* checks - disable for speed */
  g_return_if_fail (ident != NULL);
  g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));
  
  /* we don't want a loop function if we're plugged */
  if (ident->sink && GST_PAD_PEER (ident->sink))
  {
    gst_element_set_loop_function (GST_ELEMENT (ident), (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
    gst_spider_identity_dumb_loop (ident);
    return;
  }
  gst_element_interrupt (GST_ELEMENT (ident));
}
/* This loop function is only needed when typefinding.
 */
typedef struct {
  GstBuffer *buffer;
  guint best_probability;
  GstCaps2 *caps;
} SpiderTypeFind;
guint8 *
spider_find_peek (gpointer data, gint64 offset, guint size)
{
  SpiderTypeFind *find = (SpiderTypeFind *) data;
  gint64 buffer_offset = GST_BUFFER_OFFSET_IS_VALID (find->buffer) ? 
			 GST_BUFFER_OFFSET (find->buffer) : 0;
  
  if (offset >= buffer_offset && offset + size <= buffer_offset + GST_BUFFER_SIZE (find->buffer)) {
    GST_LOG ("peek %"G_GINT64_FORMAT", %u successful", offset, size);
    return GST_BUFFER_DATA (find->buffer) + offset - buffer_offset;
  } else {
    GST_LOG ("peek %"G_GINT64_FORMAT", %u failed", offset, size);
    return NULL;
  }
}
static void
spider_find_suggest (gpointer data, guint probability, const GstCaps2 *caps)
{
  SpiderTypeFind *find = (SpiderTypeFind *) data;
  G_GNUC_UNUSED gchar *caps_str;

  caps_str = gst_caps2_to_string (caps);
  GST_INFO ("suggest %u, %s", probability, caps_str);
  g_free (caps_str);
  if (probability > find->best_probability) {
    gst_caps2_replace (&find->caps, gst_caps2_copy (caps));
    find->best_probability = probability;
  }
}
static void
gst_spider_identity_sink_loop_type_finding (GstSpiderIdentity *ident)
{
  GstData *data;
  GstTypeFind gst_find;
  SpiderTypeFind find;
  GList *walk, *type_list = NULL;

  g_return_if_fail (GST_IS_SPIDER_IDENTITY (ident));

  data = gst_pad_pull (ident->sink);
  while (!GST_IS_BUFFER (data)) {
    gst_spider_identity_chain (ident->sink, GST_BUFFER (data));
    data = gst_pad_pull (ident->sink);
  }
  
  find.buffer = GST_BUFFER (data);
  /* maybe there are already valid caps now? */
  find.caps = gst_pad_get_caps (ident->sink);
  if (find.caps != NULL) {
    goto plug;
  }
  
  /* now do the actual typefinding with the supplied buffer */
  walk = type_list = gst_type_find_factory_get_list ();
    
  find.best_probability = 0;
  find.caps = NULL;
  gst_find.data = &find;
  gst_find.peek = spider_find_peek;
  gst_find.suggest = spider_find_suggest;
  while (walk) {
    GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (walk->data);

    GST_DEBUG ("trying typefind function %s", GST_PLUGIN_FEATURE_NAME (factory));
    gst_type_find_factory_call_function (factory, &gst_find);
    if (find.best_probability >= GST_TYPE_FIND_MAXIMUM)
      goto plug;
    walk = g_list_next (walk);
  }
  if (find.best_probability > 0)
    goto plug;
  gst_element_error(GST_ELEMENT(ident), "Could not find media type", NULL);
  find.buffer = GST_BUFFER (gst_event_new (GST_EVENT_EOS));

end:
  /* remove loop function */
  gst_element_set_loop_function (GST_ELEMENT (ident), 
                                (GstElementLoopFunction) GST_DEBUG_FUNCPTR (gst_spider_identity_dumb_loop));
  
  /* push the buffer */
  gst_spider_identity_chain (ident->sink, find.buffer);
  
  return;

plug:
  GST_INFO ("typefind function found caps"); 
  g_assert (gst_pad_try_set_caps (ident->src, find.caps) > 0);
  gst_caps2_debug (find.caps, "spider starting caps");
  gst_caps2_free (find.caps);
  if (type_list)
    g_list_free (type_list);

  gst_spider_identity_plug (ident);

  goto end;
}

static gboolean
gst_spider_identity_handle_src_event (GstPad *pad, GstEvent *event)
{
  gboolean res = TRUE;
  GstSpiderIdentity *ident;

  GST_DEBUG ( "spider_identity src_event");

  ident = GST_SPIDER_IDENTITY (gst_pad_get_parent (pad));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_FLUSH:
    case GST_EVENT_SEEK:
    default:
      res = gst_pad_event_default (pad, event);
      break;
  }

  return res;
}