~ubuntu-branches/ubuntu/precise/gst-plugins-bad0.10/precise-proposed

« back to all changes in this revision

Viewing changes to gst/switch/gstswitch.c

Tags: upstream-0.10.5.3
Import upstream version 0.10.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* GStreamer
2
 
 * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
3
 
 *
4
 
 * This library is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU Library General Public
6
 
 * License as published by the Free Software Foundation; either
7
 
 * version 2 of the License, or (at your option) any later version.
8
 
 *
9
 
 * This library is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
 * Library General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU Library General Public
15
 
 * License along with this library; if not, write to the
16
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
 
 * Boston, MA 02111-1307, USA.
18
 
 */
19
 
 
20
 
#ifdef HAVE_CONFIG_H
21
 
#include "config.h"
22
 
#endif
23
 
 
24
 
/* Object header */
25
 
#include "gstswitch.h"
26
 
#include <gst/gst.h>
27
 
 
28
 
#include <string.h>
29
 
 
30
 
/* This element allows runtime switching between many sources. It outputs a
31
 
 * new segment every time it switches. The input sources are expected to be
32
 
 * rate controlled/live or synced to the clock using identity sync=true upstream
33
 
 * of this element. If they are not, your cpu usage will hike up.
34
 
 *
35
 
 * Example pipelines:
36
 
 * videotestsrc pattern=0 ! identity sync=true \
37
 
 *                                              switch ! ximagesink
38
 
 * videotestsrc pattern=1 ! identity sync=true /
39
 
 *
40
 
 * videotestsrc pattern=0 ! identity sync=true \
41
 
 *                                              switch ! 
42
 
 *                                               identity single-segment=true !
43
 
 *                                               theoraenc ! oggmux ! filesink
44
 
 * videotestsrc pattern=1 ! identity sync=true /
45
 
 *
46
 
 * To switch both an audio and a video stream, you need 2 switch elements
47
 
 * one for audio and one for video. To make sure they are in sync, the app
48
 
 * needs to do the following when switching:
49
 
 *
50
 
 * i) Block the sink pads of both the switch elements.
51
 
 * ii) Retrieve the last-timestamp property from both the switch elements
52
 
 * iii) Set the stop-value property on both the switch elements to the 
53
 
 *      highest of the 2.
54
 
 * iv) Set the active-sinkpad property on the switch elements to the sink
55
 
 *     pads to switch to.
56
 
 * v) Set the queue-buffers property on both the switch elements to True.
57
 
 * vi) Add buffer pad probes on the active sinkpad of each of the switch 
58
 
 *     elements. 
59
 
 * vii) Unblock the sink pads of both the swith elements.
60
 
 * viii) In the pad probes, store the buffer timestamp of the buffer that 
61
 
 *       comes in and remove the pad probe.
62
 
 * ix) Once both pad probes have been fired (one for video, one for audio), 
63
 
 *     then set the start-value property on both the switch elements to the 
64
 
 *     lower of the 2 buffer timestamps and set the queue-buffers property 
65
 
 *     on both the switch elements to False.
66
 
 *
67
 
 */
68
 
enum
69
 
{
70
 
  ARG_0,
71
 
  ARG_NB_SOURCES,
72
 
  ARG_ACTIVE_SOURCE,
73
 
  ARG_START_VALUE,
74
 
  ARG_STOP_VALUE,
75
 
  ARG_LAST_TS,
76
 
  ARG_QUEUE_BUFFERS
77
 
};
78
 
 
79
 
GST_DEBUG_CATEGORY_STATIC (switch_debug);
80
 
#define GST_CAT_DEFAULT switch_debug
81
 
/* ElementFactory information */
82
 
static const GstElementDetails gst_switch_details =
83
 
GST_ELEMENT_DETAILS ("Switch",
84
 
    "Generic",
85
 
    "N-to-1 input switching",
86
 
    "Julien Moutte <julien@moutte.net>\n"
87
 
    "Zaheer Merali <zaheerabbas at merali dot org>");
88
 
 
89
 
static GstStaticPadTemplate gst_switch_sink_factory =
90
 
GST_STATIC_PAD_TEMPLATE ("sink%d",
91
 
    GST_PAD_SINK,
92
 
    GST_PAD_REQUEST,
93
 
    GST_STATIC_CAPS_ANY);
94
 
 
95
 
static GstStaticPadTemplate gst_switch_src_factory =
96
 
GST_STATIC_PAD_TEMPLATE ("src",
97
 
    GST_PAD_SRC,
98
 
    GST_PAD_ALWAYS,
99
 
    GST_STATIC_CAPS_ANY);
100
 
 
101
 
static GstElementClass *parent_class = NULL;
102
 
static GstCaps *gst_switch_getcaps (GstPad * pad);
103
 
static GList *gst_switch_get_linked_pads (GstPad * pad);
104
 
static GstFlowReturn gst_switch_bufferalloc (GstPad * pad, guint64 offset,
105
 
    guint size, GstCaps * caps, GstBuffer ** buf);
106
 
static GstFlowReturn gst_switch_chain (GstPad * pad, GstBuffer * buf);
107
 
static gboolean gst_switch_event (GstPad * pad, GstEvent * event);
108
 
 
109
 
/* ============================================================= */
110
 
/*                                                               */
111
 
/*                       Private Methods                         */
112
 
/*                                                               */
113
 
/* ============================================================= */
114
 
 
115
 
 
116
 
static void
117
 
gst_switch_release_pad (GstElement * element, GstPad * pad)
118
 
{
119
 
  GstSwitch *gstswitch = NULL;
120
 
 
121
 
  g_return_if_fail (GST_IS_SWITCH (element));
122
 
 
123
 
  gstswitch = GST_SWITCH (element);
124
 
 
125
 
  GST_LOG_OBJECT (gstswitch, "releasing requested pad %p", pad);
126
 
 
127
 
  gst_element_remove_pad (element, pad);
128
 
  GST_OBJECT_LOCK (gstswitch);
129
 
  gstswitch->nb_sinkpads--;
130
 
  if (gstswitch->active_sinkpad == pad) {
131
 
    gst_object_unref (gstswitch->active_sinkpad);
132
 
    gstswitch->active_sinkpad = NULL;
133
 
    if (gstswitch->nb_sinkpads == 0) {
134
 
      GstIterator *iter =
135
 
          gst_element_iterate_sink_pads (GST_ELEMENT (gstswitch));
136
 
      gpointer active_sinkpad_store = (gpointer) gstswitch->active_sinkpad;
137
 
 
138
 
      if (gst_iterator_next (iter, &active_sinkpad_store) == GST_ITERATOR_DONE) {
139
 
        GST_LOG_OBJECT (gstswitch, "active pad now %p",
140
 
            gstswitch->active_sinkpad);
141
 
      } else {
142
 
        GST_LOG_OBJECT (gstswitch, "could not get first sinkpad");
143
 
      }
144
 
      gst_iterator_free (iter);
145
 
    }
146
 
  }
147
 
  GST_OBJECT_UNLOCK (gstswitch);
148
 
}
149
 
 
150
 
static GstPad *
151
 
gst_switch_request_new_pad (GstElement * element,
152
 
    GstPadTemplate * templ, const gchar * unused)
153
 
{
154
 
  gchar *name = NULL;
155
 
  GstPad *sinkpad = NULL;
156
 
  GstSwitch *gstswitch = NULL;
157
 
 
158
 
  g_return_val_if_fail (GST_IS_SWITCH (element), NULL);
159
 
 
160
 
  gstswitch = GST_SWITCH (element);
161
 
 
162
 
  /* We only provide requested sink pads */
163
 
  if (templ->direction != GST_PAD_SINK) {
164
 
    GST_LOG_OBJECT (gstswitch, "requested a non sink pad");
165
 
    return NULL;
166
 
  }
167
 
 
168
 
  GST_OBJECT_LOCK (gstswitch);
169
 
  name = g_strdup_printf ("sink%d", gstswitch->nb_sinkpads);
170
 
 
171
 
  sinkpad = gst_pad_new_from_template (templ, name);
172
 
 
173
 
  if (name)
174
 
    g_free (name);
175
 
 
176
 
  if (gstswitch->active_sinkpad == NULL)
177
 
    gstswitch->active_sinkpad = gst_object_ref (sinkpad);
178
 
  GST_OBJECT_UNLOCK (gstswitch);
179
 
 
180
 
  gst_pad_set_getcaps_function (sinkpad,
181
 
      GST_DEBUG_FUNCPTR (gst_switch_getcaps));
182
 
  gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_switch_chain));
183
 
  gst_pad_set_internal_link_function (sinkpad,
184
 
      GST_DEBUG_FUNCPTR (gst_switch_get_linked_pads));
185
 
  gst_pad_set_bufferalloc_function (sinkpad,
186
 
      GST_DEBUG_FUNCPTR (gst_switch_bufferalloc));
187
 
  gst_pad_set_event_function (sinkpad, GST_DEBUG_FUNCPTR (gst_switch_event));
188
 
  gst_pad_set_active (sinkpad, TRUE);
189
 
 
190
 
  gst_element_add_pad (GST_ELEMENT (gstswitch), sinkpad);
191
 
 
192
 
  gstswitch->nb_sinkpads++;
193
 
 
194
 
  return sinkpad;
195
 
}
196
 
 
197
 
static GstFlowReturn
198
 
gst_switch_chain (GstPad * pad, GstBuffer * buf)
199
 
{
200
 
  GstSwitch *gstswitch = GST_SWITCH (gst_pad_get_parent (pad));
201
 
  GstFlowReturn res;
202
 
  GstPad *active_sinkpad;
203
 
 
204
 
  GST_OBJECT_LOCK (gstswitch);
205
 
  active_sinkpad = gstswitch->active_sinkpad;
206
 
  GST_OBJECT_UNLOCK (gstswitch);
207
 
 
208
 
  /* Ignore buffers from pads except the selected one */
209
 
  if (pad != active_sinkpad) {
210
 
    GST_DEBUG_OBJECT (gstswitch, "Ignoring buffer %p from pad %s:%s",
211
 
        buf, GST_DEBUG_PAD_NAME (pad));
212
 
 
213
 
    gst_object_unref (gstswitch);
214
 
    gst_buffer_unref (buf);
215
 
    return GST_FLOW_OK;
216
 
  }
217
 
 
218
 
  /* check if we need to send a new segment event */
219
 
  GST_OBJECT_LOCK (gstswitch);
220
 
  if (gstswitch->need_to_send_newsegment && !gstswitch->queue_buffers) {
221
 
    /* check to see if we need to send a new segment update for stop */
222
 
    if (gstswitch->previous_sinkpad != NULL) {
223
 
      if (gstswitch->stop_value != GST_CLOCK_TIME_NONE) {
224
 
        GstEvent *prev_newsegment =
225
 
            (GstEvent *) g_hash_table_lookup (gstswitch->newsegment_events,
226
 
            gstswitch->previous_sinkpad);
227
 
 
228
 
        if (prev_newsegment) {
229
 
          /* need to send a new segment update changing stop */
230
 
          gboolean update;
231
 
          gdouble rate, applied_rate;
232
 
          GstFormat format;
233
 
          gint64 start, stop, position;
234
 
 
235
 
          gst_event_parse_new_segment_full (prev_newsegment, &update, &rate,
236
 
              &applied_rate, &format, &start, &stop, &position);
237
 
          GST_DEBUG_OBJECT (gstswitch,
238
 
              "Sending new segment update with stop of %" G_GUINT64_FORMAT,
239
 
              gstswitch->stop_value);
240
 
          gst_pad_push_event (gstswitch->srcpad,
241
 
              gst_event_new_new_segment_full (TRUE, rate, applied_rate, format,
242
 
                  gstswitch->current_start, gstswitch->stop_value, position));
243
 
        }
244
 
      }
245
 
      gst_object_unref (GST_OBJECT (gstswitch->previous_sinkpad));
246
 
      gstswitch->previous_sinkpad = NULL;
247
 
    }
248
 
    /* retrieve event from hash table */
249
 
    GstEvent *event =
250
 
        (GstEvent *) g_hash_table_lookup (gstswitch->newsegment_events, pad);
251
 
    if (event) {
252
 
      /* create a copy of this event so we can change start to match
253
 
       * the start time of this buffer */
254
 
      gboolean update;
255
 
      gdouble rate, applied_rate;
256
 
      GstFormat format;
257
 
      gint64 start, stop, position;
258
 
 
259
 
      gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
260
 
          &format, &start, &stop, &position);
261
 
      if (gstswitch->start_value != GST_CLOCK_TIME_NONE &&
262
 
          gstswitch->start_value <= GST_BUFFER_TIMESTAMP (buf)) {
263
 
        start = gstswitch->start_value;
264
 
      } else {
265
 
        start = GST_BUFFER_TIMESTAMP (buf);
266
 
      }
267
 
      gst_pad_push_event (gstswitch->srcpad,
268
 
          gst_event_new_new_segment_full (FALSE, rate, applied_rate, format,
269
 
              start, stop, position));
270
 
      gstswitch->need_to_send_newsegment = FALSE;
271
 
      gstswitch->current_start = start;
272
 
      GST_DEBUG_OBJECT (gstswitch,
273
 
          "Sending new segment with start of %" G_GUINT64_FORMAT, start);
274
 
    } else {
275
 
      GST_WARNING_OBJECT (pad,
276
 
          "Couldn't find new segment for pad in hashtable");
277
 
    }
278
 
    /* reset stop and start value */
279
 
    gstswitch->start_value = GST_CLOCK_TIME_NONE;
280
 
    gstswitch->stop_value = GST_CLOCK_TIME_NONE;
281
 
    /* send all the stored buffers if any */
282
 
    GList *buffers =
283
 
        g_hash_table_lookup (gstswitch->stored_buffers, active_sinkpad);
284
 
    while (buffers != NULL) {
285
 
      gst_buffer_ref (GST_BUFFER (buffers->data));
286
 
      gst_pad_push (gstswitch->srcpad, GST_BUFFER (buffers->data));
287
 
      buffers = buffers->next;
288
 
    }
289
 
    g_hash_table_remove (gstswitch->stored_buffers, active_sinkpad);
290
 
  }
291
 
  gstswitch->last_ts = GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf);
292
 
  if (!gstswitch->queue_buffers) {
293
 
    GST_OBJECT_UNLOCK (gstswitch);
294
 
    /* forward */
295
 
    GST_DEBUG_OBJECT (gstswitch, "Forwarding buffer %p from pad %s:%s",
296
 
        buf, GST_DEBUG_PAD_NAME (pad));
297
 
    res = gst_pad_push (gstswitch->srcpad, buf);
298
 
  } else {
299
 
    GList *buffers;
300
 
    gboolean lookup_res = TRUE;
301
 
 
302
 
    buffers = g_hash_table_lookup (gstswitch->stored_buffers, active_sinkpad);
303
 
    if (buffers == NULL)
304
 
      lookup_res = FALSE;
305
 
    buffers = g_list_append (buffers, buf);
306
 
    /* only need to insert it if it was NULL before because we appended */
307
 
    if (!lookup_res)
308
 
      g_hash_table_insert (gstswitch->stored_buffers, active_sinkpad, buffers);
309
 
    GST_OBJECT_UNLOCK (gstswitch);
310
 
    res = GST_FLOW_OK;
311
 
  }
312
 
 
313
 
  gst_object_unref (gstswitch);
314
 
 
315
 
  return res;
316
 
}
317
 
 
318
 
static gboolean
319
 
gst_switch_event (GstPad * pad, GstEvent * event)
320
 
{
321
 
  GstSwitch *gstswitch = GST_SWITCH (gst_pad_get_parent (pad));
322
 
  gboolean ret = TRUE;
323
 
 
324
 
  switch (GST_EVENT_TYPE (event)) {
325
 
    case GST_EVENT_NEWSEGMENT:
326
 
      GST_OBJECT_LOCK (gstswitch);
327
 
      /* need to put in or replace what's in hash table */
328
 
      g_hash_table_replace (gstswitch->newsegment_events, pad, event);
329
 
      if (pad == gstswitch->active_sinkpad) {
330
 
        if (!gstswitch->need_to_send_newsegment) {
331
 
          gstswitch->need_to_send_newsegment = TRUE;
332
 
        }
333
 
      }
334
 
      GST_OBJECT_UNLOCK (gstswitch);
335
 
      break;
336
 
    default:
337
 
      ret = gst_pad_event_default (pad, event);
338
 
      break;
339
 
  }
340
 
  gst_object_unref (gstswitch);
341
 
  return ret;
342
 
}
343
 
 
344
 
/* =========================================== */
345
 
/*                                             */
346
 
/*                 Properties                  */
347
 
/*                                             */
348
 
/* =========================================== */
349
 
 
350
 
static void
351
 
gst_switch_set_property (GObject * object, guint prop_id,
352
 
    const GValue * value, GParamSpec * pspec)
353
 
{
354
 
  GstSwitch *gstswitch = NULL;
355
 
  const gchar *pad_name;
356
 
  GstPad *pad = NULL;
357
 
  GstPad **active_pad_p;
358
 
 
359
 
  g_return_if_fail (GST_IS_SWITCH (object));
360
 
 
361
 
  gstswitch = GST_SWITCH (object);
362
 
 
363
 
  switch (prop_id) {
364
 
    case ARG_ACTIVE_SOURCE:
365
 
      pad_name = g_value_get_string (value);
366
 
      if (strcmp (pad_name, "") != 0) {
367
 
        pad = gst_element_get_pad (GST_ELEMENT (object), pad_name);
368
 
      }
369
 
 
370
 
      GST_OBJECT_LOCK (object);
371
 
      if (pad == gstswitch->active_sinkpad) {
372
 
        GST_OBJECT_UNLOCK (object);
373
 
        if (pad)
374
 
          gst_object_unref (pad);
375
 
        break;
376
 
      }
377
 
      active_pad_p = &gstswitch->active_sinkpad;
378
 
      if (gstswitch->previous_sinkpad != NULL) {
379
 
        gst_object_unref (GST_OBJECT (gstswitch->previous_sinkpad));
380
 
      }
381
 
      gstswitch->previous_sinkpad = gstswitch->active_sinkpad;
382
 
      gst_object_ref (GST_OBJECT (gstswitch->previous_sinkpad));
383
 
      gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
384
 
      if (pad)
385
 
        gst_object_unref (pad);
386
 
      GST_DEBUG_OBJECT (gstswitch, "New active pad is %" GST_PTR_FORMAT,
387
 
          gstswitch->active_sinkpad);
388
 
      gstswitch->need_to_send_newsegment = TRUE;
389
 
      GST_OBJECT_UNLOCK (object);
390
 
      break;
391
 
    case ARG_START_VALUE:
392
 
      GST_OBJECT_LOCK (object);
393
 
      gstswitch->start_value = g_value_get_uint64 (value);
394
 
      GST_OBJECT_UNLOCK (object);
395
 
      break;
396
 
    case ARG_STOP_VALUE:
397
 
      GST_OBJECT_LOCK (object);
398
 
      gstswitch->stop_value = g_value_get_uint64 (value);
399
 
      GST_OBJECT_UNLOCK (object);
400
 
      break;
401
 
    case ARG_QUEUE_BUFFERS:
402
 
      GST_OBJECT_LOCK (object);
403
 
      gstswitch->queue_buffers = g_value_get_boolean (value);
404
 
      GST_OBJECT_UNLOCK (object);
405
 
      break;
406
 
    default:
407
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
408
 
      break;
409
 
  }
410
 
}
411
 
 
412
 
static void
413
 
gst_switch_get_property (GObject * object, guint prop_id,
414
 
    GValue * value, GParamSpec * pspec)
415
 
{
416
 
  GstSwitch *gstswitch = NULL;
417
 
 
418
 
  g_return_if_fail (GST_IS_SWITCH (object));
419
 
 
420
 
  gstswitch = GST_SWITCH (object);
421
 
 
422
 
  switch (prop_id) {
423
 
    case ARG_ACTIVE_SOURCE:
424
 
      GST_OBJECT_LOCK (object);
425
 
      if (gstswitch->active_sinkpad != NULL) {
426
 
        g_value_take_string (value,
427
 
            gst_pad_get_name (gstswitch->active_sinkpad));
428
 
      } else {
429
 
        g_value_set_string (value, "");
430
 
      }
431
 
      GST_OBJECT_UNLOCK (object);
432
 
      break;
433
 
    case ARG_NB_SOURCES:
434
 
      GST_OBJECT_LOCK (object);
435
 
      g_value_set_uint (value, gstswitch->nb_sinkpads);
436
 
      GST_OBJECT_UNLOCK (object);
437
 
      break;
438
 
    case ARG_START_VALUE:
439
 
      GST_OBJECT_LOCK (object);
440
 
      g_value_set_uint64 (value, gstswitch->start_value);
441
 
      GST_OBJECT_UNLOCK (object);
442
 
      break;
443
 
    case ARG_STOP_VALUE:
444
 
      GST_OBJECT_LOCK (object);
445
 
      g_value_set_uint64 (value, gstswitch->stop_value);
446
 
      GST_OBJECT_UNLOCK (object);
447
 
      break;
448
 
    case ARG_LAST_TS:
449
 
      GST_OBJECT_LOCK (object);
450
 
      g_value_set_uint64 (value, gstswitch->last_ts);
451
 
      GST_OBJECT_UNLOCK (object);
452
 
      break;
453
 
    case ARG_QUEUE_BUFFERS:
454
 
      GST_OBJECT_LOCK (object);
455
 
      g_value_set_boolean (value, gstswitch->queue_buffers);
456
 
      GST_OBJECT_UNLOCK (object);
457
 
      break;
458
 
    default:
459
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
460
 
      break;
461
 
  }
462
 
}
463
 
 
464
 
static GstPad *
465
 
gst_switch_get_linked_pad (GstPad * pad, gboolean strict)
466
 
{
467
 
  GstSwitch *gstswitch = GST_SWITCH (gst_pad_get_parent (pad));
468
 
  GstPad *otherpad = NULL;
469
 
 
470
 
  if (pad == gstswitch->srcpad)
471
 
    otherpad = gstswitch->active_sinkpad;
472
 
  else if (pad == gstswitch->active_sinkpad || !strict)
473
 
    otherpad = gstswitch->srcpad;
474
 
 
475
 
  gst_object_unref (gstswitch);
476
 
 
477
 
  return otherpad;
478
 
}
479
 
 
480
 
static GstCaps *
481
 
gst_switch_getcaps (GstPad * pad)
482
 
{
483
 
  GstPad *otherpad = gst_switch_get_linked_pad (pad, FALSE);
484
 
  GstObject *parent;
485
 
  GstCaps *caps;
486
 
 
487
 
  parent = gst_object_get_parent (GST_OBJECT (pad));
488
 
  if (!otherpad) {
489
 
    GST_DEBUG_OBJECT (parent,
490
 
        "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
491
 
 
492
 
    gst_object_unref (parent);
493
 
    return gst_caps_new_any ();
494
 
  }
495
 
 
496
 
  GST_DEBUG_OBJECT (parent,
497
 
      "Pad %s:%s is linked (to %s:%s), returning allowed-caps",
498
 
      GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
499
 
 
500
 
  gst_object_unref (parent);
501
 
 
502
 
  caps = gst_pad_peer_get_caps (otherpad);
503
 
  if (caps == NULL) {
504
 
    caps = gst_caps_new_any ();
505
 
  }
506
 
  return caps;
507
 
}
508
 
 
509
 
static GstFlowReturn
510
 
gst_switch_bufferalloc (GstPad * pad, guint64 offset,
511
 
    guint size, GstCaps * caps, GstBuffer ** buf)
512
 
{
513
 
  GstSwitch *gstswitch = GST_SWITCH (gst_pad_get_parent (pad));
514
 
  GstFlowReturn result;
515
 
  GstPad *active_sinkpad;
516
 
 
517
 
  GST_OBJECT_LOCK (gstswitch);
518
 
  active_sinkpad = gstswitch->active_sinkpad;
519
 
  GST_OBJECT_UNLOCK (gstswitch);
520
 
 
521
 
  /* Fallback allocation for buffers from pads except the selected one */
522
 
  if (pad != active_sinkpad) {
523
 
    GST_DEBUG_OBJECT (gstswitch,
524
 
        "Pad %s:%s is not selected. Performing fallback allocation",
525
 
        GST_DEBUG_PAD_NAME (pad));
526
 
 
527
 
    *buf = NULL;
528
 
    result = GST_FLOW_OK;
529
 
  } else {
530
 
    result = gst_pad_alloc_buffer (gstswitch->srcpad, offset, size, caps, buf);
531
 
 
532
 
    /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
533
 
     * allocation.  This should NOT be necessary, because playbin should
534
 
     * properly block the source pad from running until it's finished hooking 
535
 
     * everything up, but playbin needs refactoring first. */
536
 
    if (result == GST_FLOW_NOT_LINKED) {
537
 
      GST_DEBUG_OBJECT (gstswitch,
538
 
          "No peer pad yet - performing fallback allocation for pad %s:%s",
539
 
          GST_DEBUG_PAD_NAME (pad));
540
 
 
541
 
      *buf = NULL;
542
 
      result = GST_FLOW_OK;
543
 
    }
544
 
  }
545
 
 
546
 
  gst_object_unref (gstswitch);
547
 
 
548
 
  return result;
549
 
}
550
 
 
551
 
static GList *
552
 
gst_switch_get_linked_pads (GstPad * pad)
553
 
{
554
 
  GstPad *otherpad = gst_switch_get_linked_pad (pad, TRUE);
555
 
 
556
 
  if (!otherpad)
557
 
    return NULL;
558
 
 
559
 
  return g_list_append (NULL, otherpad);
560
 
}
561
 
 
562
 
 
563
 
/* =========================================== */
564
 
/*                                             */
565
 
/*              Init & Class init              */
566
 
/*                                             */
567
 
/* =========================================== */
568
 
 
569
 
static void
570
 
gst_switch_dispose (GObject * object)
571
 
{
572
 
  GstSwitch *gstswitch = NULL;
573
 
 
574
 
  gstswitch = GST_SWITCH (object);
575
 
 
576
 
  if (gstswitch->active_sinkpad) {
577
 
    gst_object_unref (gstswitch->active_sinkpad);
578
 
    gstswitch->active_sinkpad = NULL;
579
 
  }
580
 
  if (gstswitch->newsegment_events) {
581
 
    g_hash_table_destroy (gstswitch->newsegment_events);
582
 
  }
583
 
  if (gstswitch->stored_buffers) {
584
 
    g_hash_table_destroy (gstswitch->stored_buffers);
585
 
  }
586
 
  if (gstswitch->previous_sinkpad) {
587
 
    gst_object_unref (GST_OBJECT (gstswitch->previous_sinkpad));
588
 
    gstswitch->previous_sinkpad = NULL;
589
 
  }
590
 
  G_OBJECT_CLASS (parent_class)->dispose (object);
591
 
}
592
 
 
593
 
static void
594
 
unref_buffer (GstBuffer * buf, gpointer user_data)
595
 
{
596
 
  gst_buffer_unref (buf);
597
 
}
598
 
 
599
 
static void
600
 
unref_buffers_and_destroy_list (GList * bufferlist)
601
 
{
602
 
  g_list_foreach (bufferlist, (GFunc) unref_buffer, NULL);
603
 
  g_list_free (bufferlist);
604
 
}
605
 
 
606
 
static void
607
 
gst_switch_init (GstSwitch * gstswitch)
608
 
{
609
 
  gstswitch->srcpad = gst_pad_new ("src", GST_PAD_SRC);
610
 
  gst_pad_set_internal_link_function (gstswitch->srcpad,
611
 
      GST_DEBUG_FUNCPTR (gst_switch_get_linked_pads));
612
 
  gst_pad_set_getcaps_function (gstswitch->srcpad,
613
 
      GST_DEBUG_FUNCPTR (gst_switch_getcaps));
614
 
  gst_element_add_pad (GST_ELEMENT (gstswitch), gstswitch->srcpad);
615
 
 
616
 
  gstswitch->active_sinkpad = NULL;
617
 
  gstswitch->previous_sinkpad = NULL;
618
 
  gstswitch->nb_sinkpads = 0;
619
 
  gstswitch->newsegment_events = g_hash_table_new_full (g_direct_hash,
620
 
      g_direct_equal, NULL, (GDestroyNotify) gst_mini_object_unref);
621
 
  gstswitch->stored_buffers = g_hash_table_new_full (g_direct_hash,
622
 
      g_direct_equal, NULL, (GDestroyNotify) unref_buffers_and_destroy_list);
623
 
  gstswitch->need_to_send_newsegment = FALSE;
624
 
  gstswitch->queue_buffers = FALSE;
625
 
  gstswitch->stop_value = GST_CLOCK_TIME_NONE;
626
 
  gstswitch->start_value = GST_CLOCK_TIME_NONE;
627
 
  gstswitch->current_start = 0;
628
 
  gstswitch->last_ts = GST_CLOCK_TIME_NONE;
629
 
}
630
 
 
631
 
static void
632
 
gst_switch_base_init (gpointer g_class)
633
 
{
634
 
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
635
 
 
636
 
  gst_element_class_set_details (element_class, &gst_switch_details);
637
 
 
638
 
  gst_element_class_add_pad_template (element_class,
639
 
      gst_static_pad_template_get (&gst_switch_sink_factory));
640
 
  gst_element_class_add_pad_template (element_class,
641
 
      gst_static_pad_template_get (&gst_switch_src_factory));
642
 
}
643
 
 
644
 
static void
645
 
gst_switch_class_init (GstSwitchClass * klass)
646
 
{
647
 
  GObjectClass *gobject_class;
648
 
  GstElementClass *gstelement_class;
649
 
 
650
 
  gobject_class = (GObjectClass *) klass;
651
 
  gstelement_class = (GstElementClass *) klass;
652
 
 
653
 
  parent_class = g_type_class_peek_parent (klass);
654
 
  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_switch_set_property);
655
 
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_switch_get_property);
656
 
 
657
 
  g_object_class_install_property (gobject_class,
658
 
      ARG_NB_SOURCES,
659
 
      g_param_spec_uint ("num-sources",
660
 
          "number of sources",
661
 
          "number of sources", 0, G_MAXUINT, 0, G_PARAM_READABLE));
662
 
  g_object_class_install_property (gobject_class,
663
 
      ARG_ACTIVE_SOURCE,
664
 
      g_param_spec_string ("active-pad",
665
 
          "Active Pad",
666
 
          "Name of the currently active sink pad", NULL, G_PARAM_READWRITE));
667
 
  g_object_class_install_property (gobject_class,
668
 
      ARG_START_VALUE,
669
 
      g_param_spec_uint64 ("start-value",
670
 
          "Start Value",
671
 
          "Timestamp that next segment will start at (-1 to use first buffer)",
672
 
          0, G_MAXUINT64, GST_CLOCK_TIME_NONE, G_PARAM_READWRITE));
673
 
  g_object_class_install_property (gobject_class,
674
 
      ARG_STOP_VALUE,
675
 
      g_param_spec_uint64 ("stop-value",
676
 
          "Stop Value",
677
 
          "Timestamp that current source will stop at (-1 if unknown or don't care)",
678
 
          0, G_MAXUINT64, GST_CLOCK_TIME_NONE, G_PARAM_READWRITE));
679
 
 
680
 
  g_object_class_install_property (gobject_class,
681
 
      ARG_LAST_TS,
682
 
      g_param_spec_uint64 ("last-timestamp",
683
 
          "Time at the end of the last buffer",
684
 
          "Time at the end of the last buffer", 0, G_MAXUINT, 0,
685
 
          G_PARAM_READABLE));
686
 
 
687
 
  g_object_class_install_property (gobject_class,
688
 
      ARG_QUEUE_BUFFERS,
689
 
      g_param_spec_boolean ("queue-buffers",
690
 
          "Queue new segment and buffers instead of sending them",
691
 
          "Queue new segment and buffers instead of sending them",
692
 
          FALSE, G_PARAM_READWRITE));
693
 
 
694
 
  gobject_class->dispose = gst_switch_dispose;
695
 
 
696
 
  gstelement_class->request_new_pad = gst_switch_request_new_pad;
697
 
  gstelement_class->release_pad = gst_switch_release_pad;
698
 
}
699
 
 
700
 
/* ============================================================= */
701
 
/*                                                               */
702
 
/*                       Public Methods                          */
703
 
/*                                                               */
704
 
/* ============================================================= */
705
 
 
706
 
GType
707
 
gst_switch_get_type (void)
708
 
{
709
 
  static GType switch_type = 0;
710
 
 
711
 
  if (!switch_type) {
712
 
    static const GTypeInfo switch_info = {
713
 
      sizeof (GstSwitchClass),
714
 
      gst_switch_base_init,
715
 
      NULL,
716
 
      (GClassInitFunc) gst_switch_class_init,
717
 
      NULL,
718
 
      NULL,
719
 
      sizeof (GstSwitch),
720
 
      0,
721
 
      (GInstanceInitFunc) gst_switch_init,
722
 
    };
723
 
 
724
 
    switch_type = g_type_register_static (GST_TYPE_ELEMENT,
725
 
        "GstSwitch", &switch_info, 0);
726
 
 
727
 
    GST_DEBUG_CATEGORY_INIT (switch_debug, "switch", 0, "the switch element");
728
 
  }
729
 
 
730
 
  return switch_type;
731
 
}
732
 
 
733
 
static gboolean
734
 
plugin_init (GstPlugin * plugin)
735
 
{
736
 
  return gst_element_register (plugin, "switch", GST_RANK_NONE,
737
 
      GST_TYPE_SWITCH);
738
 
}
739
 
 
740
 
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
741
 
    GST_VERSION_MINOR,
742
 
    "switch",
743
 
    "N-to-1 input switching",
744
 
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)