summaryrefslogtreecommitdiff
path: root/ext/gconf/gstswitchsink.c
blob: e93bd055ad2005eaed0b45217a9c56639411dd6c (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
/* GStreamer
 * Copyright (c) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
 * Copyright (c) 2006 Jürg Billeter <j@bitron.ch>
 * Copyright (c) 2007 Jan Schmidt <thaytan@noraisin.net>
 * Copyright (c) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
 *
 * 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 <string.h>

#include "gstswitchsink.h"

GST_DEBUG_CATEGORY_STATIC (switch_debug);
#define GST_CAT_DEFAULT switch_debug

static void gst_switch_sink_dispose (GObject * object);
static GstStateChangeReturn
gst_switch_sink_change_state (GstElement * element, GstStateChange transition);
static gboolean gst_switch_sink_commit_new_kid (GstSwitchSink * sink);

static gboolean gst_switch_sink_event (GstPad * pad, GstEvent * event);
static GstFlowReturn gst_switch_sink_bufferalloc (GstPad * pad, guint64 offset,
    guint size, GstCaps * caps, GstBuffer ** buf);

GST_BOILERPLATE (GstSwitchSink, gst_switch_sink, GstBin, GST_TYPE_BIN);

static void
gst_switch_sink_base_init (gpointer klass)
{
  GST_DEBUG_CATEGORY_INIT (switch_debug, "switchsink", 0, "switchsink element");
}

static void
gst_switch_sink_class_init (GstSwitchSinkClass * klass)
{
  GObjectClass *oklass = G_OBJECT_CLASS (klass);
  GstElementClass *eklass = GST_ELEMENT_CLASS (klass);
  static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
      GST_PAD_SINK,
      GST_PAD_ALWAYS,
      GST_STATIC_CAPS_ANY);
  GstPadTemplate *child_pad_templ;

  oklass->dispose = gst_switch_sink_dispose;
  eklass->change_state = gst_switch_sink_change_state;

  /* Provide a default pad template if the child didn't */
  child_pad_templ = gst_element_class_get_pad_template (eklass, "sink");
  if (child_pad_templ == NULL) {
    gst_element_class_add_pad_template (eklass,
        gst_static_pad_template_get (&sink_template));
  }
}

static gboolean
gst_switch_sink_reset (GstSwitchSink * sink)
{
  if (sink->blocking) {
    gst_pad_set_blocked (sink->pad, FALSE);
    sink->blocking = FALSE;
  }

  /* this will install fakesink if no other child has been set,
   * otherwise we rely on the subclass to know when to unset its
   * custom kid */
  if (sink->kid == NULL) {
    return gst_switch_sink_set_child (sink, NULL);
  }

  return TRUE;
}

static void
gst_switch_sink_init (GstSwitchSink * sink, GstSwitchSinkClass * g_class)
{
  GstElementClass *eklass = GST_ELEMENT_GET_CLASS (sink);
  GstPadTemplate *templ;

  templ = gst_element_class_get_pad_template (eklass, "sink");
  sink->pad = gst_ghost_pad_new_no_target_from_template ("sink", templ);
  sink->ghostpad_event_func = GST_PAD_EVENTFUNC (sink->pad);
  gst_pad_set_event_function (sink->pad,
      GST_DEBUG_FUNCPTR (gst_switch_sink_event));
  sink->ghostpad_bufferalloc_func = GST_PAD_BUFFERALLOCFUNC (sink->pad);
  gst_pad_set_bufferalloc_function (sink->pad,
      GST_DEBUG_FUNCPTR (gst_switch_sink_bufferalloc));
  gst_element_add_pad (GST_ELEMENT (sink), sink->pad);

  gst_switch_sink_reset (sink);

  GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);
}

static void
gst_switch_sink_dispose (GObject * object)
{
  GstSwitchSink *sink = GST_SWITCH_SINK (object);
  GstObject *new_kid, *kid;

  GST_OBJECT_LOCK (sink);
  new_kid = GST_OBJECT_CAST (sink->new_kid);
  sink->new_kid = NULL;

  kid = GST_OBJECT_CAST (sink->kid);
  sink->kid = NULL;
  GST_OBJECT_UNLOCK (sink);

  gst_object_replace (&new_kid, NULL);
  gst_object_replace (&kid, NULL);

  GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
}

static void
gst_switch_sink_reset_old_kids (GstElement * old_kid, GstSwitchSink * sink)
{
  GST_DEBUG_OBJECT (sink, "Resetting old kid: %s", GST_ELEMENT_NAME (old_kid));
  gst_element_set_state (old_kid, GST_STATE_NULL);
  gst_object_unref (old_kid);
}

static void
gst_switch_sink_pad_blocked (GstPad * pad, gboolean blocked,
    GstSwitchSink * sink)
{
  if (!blocked) {
    GST_DEBUG_OBJECT (sink, "Unblocked pad");
    sink->blocking = FALSE;
    return;
  }

  sink->blocking = TRUE;

  GST_DEBUG_OBJECT (sink, "Blocked pad");
  gst_switch_sink_commit_new_kid (sink);
}

static gboolean
gst_switch_sink_commit_new_kid (GstSwitchSink * sink)
{
  GstPad *targetpad;
  GstState kid_state;
  GstElement *new_kid, *old_kid;
  gboolean is_fakesink = FALSE;
  GstBus *bus;

  /* need locking around member accesses */
  GST_OBJECT_LOCK (sink);
  /* If we're currently changing state, set the child to the next state
   * we're transitioning too, rather than our current state which is 
   * about to change */
  if (GST_STATE_NEXT (sink) != GST_STATE_VOID_PENDING)
    kid_state = GST_STATE_NEXT (sink);
  else
    kid_state = GST_STATE (sink);

  new_kid = sink->new_kid ? gst_object_ref (sink->new_kid) : NULL;
  sink->new_kid = NULL;
  GST_OBJECT_UNLOCK (sink);

  /* Fakesink by default if NULL is passed as the new child */
  if (new_kid == NULL) {
    GST_DEBUG_OBJECT (sink, "Replacing kid with fakesink");
    new_kid = gst_element_factory_make ("fakesink", "testsink");
    if (new_kid == NULL) {
      GST_ERROR_OBJECT (sink, "Failed to create fakesink");
      return FALSE;
    }
    /* Add a reference, as it would if the element came from sink->new_kid */
    gst_object_ref (new_kid);
    g_object_set (new_kid, "sync", TRUE, NULL);
    is_fakesink = TRUE;
  } else {
    GST_DEBUG_OBJECT (sink, "Setting new kid");
  }

  /* set temporary bus of our own to catch error messages from the child
   * (could we just set our own bus on it, or would the state change messages
   * from the not-yet-added element confuse the state change algorithm? Let's
   * play it safe for now) */
  bus = gst_bus_new ();
  gst_element_set_bus (new_kid, bus);
  gst_object_unref (bus);

  if (gst_element_set_state (new_kid,
          GST_STATE_READY) == GST_STATE_CHANGE_FAILURE) {
    GstMessage *msg;

    /* check if child posted an error message and if so re-post it on our bus
     * so that the application gets to see a decent error and not our generic
     * fallback error message which is completely indecipherable to the user */
    msg = gst_bus_pop_filtered (GST_ELEMENT_BUS (new_kid), GST_MESSAGE_ERROR);
    if (msg) {
      GST_INFO_OBJECT (sink, "Forwarding kid error: %" GST_PTR_FORMAT, msg);
      gst_element_post_message (GST_ELEMENT (sink), msg);
    }
    GST_ELEMENT_ERROR (sink, CORE, STATE_CHANGE, (NULL),
        ("Failed to set state on new child."));
    gst_element_set_bus (new_kid, NULL);
    gst_object_unref (new_kid);
    return FALSE;
  }
  gst_element_set_bus (new_kid, NULL);
  gst_bin_add (GST_BIN (sink), new_kid);

  /* Now, replace the existing child */
  GST_OBJECT_LOCK (sink);
  old_kid = sink->kid;
  sink->kid = new_kid;
  /* Mark whether a custom kid or fakesink has been installed */
  sink->have_kid = !is_fakesink;
  GST_OBJECT_UNLOCK (sink);

  /* kill old element */
  if (old_kid) {
    GST_DEBUG_OBJECT (sink, "Removing old kid %" GST_PTR_FORMAT, old_kid);
    gst_bin_remove (GST_BIN (sink), old_kid);
    /* Don't lose the SINK flag */
    GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_IS_SINK);

    if (!sink->blocking) {
      gst_element_set_state (old_kid, GST_STATE_NULL);
      gst_object_unref (old_kid);
    } else {
      g_thread_pool_push (sink->thread_pool, old_kid, NULL);
    }
  }

  /* re-attach ghostpad */
  GST_DEBUG_OBJECT (sink, "Creating new ghostpad");
  targetpad = gst_element_get_static_pad (sink->kid, "sink");
  gst_ghost_pad_set_target (GST_GHOST_PAD (sink->pad), targetpad);
  gst_object_unref (targetpad);
  GST_DEBUG_OBJECT (sink, "done changing child of switchsink");

  if (gst_element_set_state (new_kid, kid_state) == GST_STATE_CHANGE_FAILURE) {
    GST_ELEMENT_ERROR (sink, CORE, STATE_CHANGE, (NULL),
        ("Failed to set state on new child."));
    GST_OBJECT_LOCK (sink);
    gst_object_unref (sink->kid);
    sink->kid = NULL;
    GST_OBJECT_UNLOCK (sink);
    return FALSE;
  }

  if (sink->segment.format != GST_FORMAT_UNDEFINED) {
    GstEvent *event;
    GstSegment *segment = &sink->segment;

    event = gst_event_new_new_segment_full (FALSE, segment->rate,
        segment->applied_rate, segment->format, 0, segment->accum, 0);

    gst_pad_send_event (sink->pad, event);

    event = gst_event_new_new_segment_full (FALSE, segment->rate,
        segment->applied_rate, segment->format,
        segment->start, segment->stop, segment->time);

    gst_pad_send_event (sink->pad, event);
  }

  /* Unblock the target pad if necessary */
  if (sink->blocking) {
    gst_pad_set_blocked_async_full (sink->pad, FALSE,
        (GstPadBlockCallback) gst_switch_sink_pad_blocked,
        gst_object_ref (sink), (GDestroyNotify) gst_object_unref);
  }

  return TRUE;
}

gboolean
gst_switch_sink_set_child (GstSwitchSink * sink, GstElement * new_kid)
{
  GstState cur, next;
  GstElement **p_kid;

  /* Nothing to do if clearing the child and we've already installed fakesink */
  if (new_kid == NULL && sink->kid != NULL && sink->have_kid == FALSE)
    return TRUE;

  /* Store the new kid to be committed later */
  GST_OBJECT_LOCK (sink);
  cur = GST_STATE (sink);
  next = GST_STATE_NEXT (sink);
  p_kid = &sink->new_kid;
  gst_object_replace ((GstObject **) p_kid, (GstObject *) new_kid);
  GST_OBJECT_UNLOCK (sink);
  if (new_kid)
    gst_object_unref (new_kid);

  if (cur > GST_STATE_READY || next == GST_STATE_PAUSED) {
    GST_DEBUG_OBJECT (sink, "Already running, switching child after pad-block");
    if (!sink->blocking) {
      sink->blocking = TRUE;
      gst_pad_set_blocked_async_full (sink->pad, TRUE,
          (GstPadBlockCallback) gst_switch_sink_pad_blocked,
          gst_object_ref (sink), (GDestroyNotify) gst_object_unref);
    }
    return TRUE;
  }

  return gst_switch_sink_commit_new_kid (sink);
}

static GstStateChangeReturn
gst_switch_sink_change_state (GstElement * element, GstStateChange transition)
{
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
  GstSwitchSink *sink = GST_SWITCH_SINK (element);

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      sink->thread_pool =
          g_thread_pool_new ((GFunc) gst_switch_sink_reset_old_kids, sink, 1,
          FALSE, NULL);
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      gst_segment_init (&sink->segment, GST_FORMAT_UNDEFINED);
      break;
    default:
      break;
  }

  ret = GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
      (element, transition), GST_STATE_CHANGE_SUCCESS);

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:
      if (!gst_switch_sink_reset (sink))
        ret = GST_STATE_CHANGE_FAILURE;

      if (sink->thread_pool) {
        g_thread_pool_free (sink->thread_pool, FALSE, TRUE);
        sink->thread_pool = NULL;
      }
      break;
    default:
      break;
  }

  return ret;
}

static gboolean
gst_switch_sink_event (GstPad * pad, GstEvent * event)
{
  GstSwitchSink *sink = GST_SWITCH_SINK (gst_pad_get_parent (pad));
  gboolean ret;

  if (GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
    GST_DEBUG_OBJECT (sink, "Resetting segment because of flush-stop event");
    gst_segment_init (&sink->segment, GST_FORMAT_UNDEFINED);
  }

  ret = sink->ghostpad_event_func (pad, gst_event_ref (event));

  if (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT) {
    gboolean update;
    gdouble rate, applied_rate;
    GstFormat format;
    gint64 start, stop, position;

    GST_DEBUG_OBJECT (sink, "Newsegment event: %" GST_PTR_FORMAT,
        event->structure);
    gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
        &format, &start, &stop, &position);

    if (sink->segment.format != GST_FORMAT_UNDEFINED
        && format != sink->segment.format) {
      GST_ERROR_OBJECT (sink,
          "Newsegment event in different format: %s (expected %s)",
          gst_format_get_name (format),
          gst_format_get_name (sink->segment.format));
      gst_object_unref (event);
      gst_object_unref (sink);
      return FALSE;
    }

    GST_DEBUG_OBJECT (pad, "Old segment: %" GST_SEGMENT_FORMAT, &sink->segment);
    gst_segment_set_newsegment_full (&sink->segment, update, rate,
        applied_rate, format, start, stop, position);
    GST_DEBUG_OBJECT (sink, "New video segment: %" GST_SEGMENT_FORMAT,
        &sink->segment);
  }

  gst_event_unref (event);
  gst_object_unref (sink);

  return ret;
}

static GstFlowReturn
gst_switch_sink_bufferalloc (GstPad * pad, guint64 offset, guint size,
    GstCaps * caps, GstBuffer ** buf)
{
  GstSwitchSink *sink = GST_SWITCH_SINK (gst_pad_get_parent (pad));
  GstFlowReturn ret;
  GstPad *target;

  target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
  if (target) {
    if (GST_PAD_CAPS (target) != caps
        && !gst_caps_is_equal (caps, GST_PAD_CAPS (target))) {
      GST_DEBUG_OBJECT (sink,
          "Have new caps: %" GST_PTR_FORMAT " -> %" GST_PTR_FORMAT,
          GST_PAD_CAPS (target), caps);

      if (!gst_pad_accept_caps (target, caps)) {
        GST_DEBUG_OBJECT (sink,
            "New target did not accept caps, trying to get possible caps");
        caps = gst_pad_get_caps (target);

        if (caps) {
          GST_DEBUG_OBJECT (sink, "Sink has caps %" GST_PTR_FORMAT, caps);
          if (!gst_caps_is_fixed (caps)) {
            gst_caps_truncate (caps);
            gst_pad_fixate_caps (target, caps);

            if (!gst_caps_is_fixed (caps)) {
              GST_ERROR_OBJECT (sink, "Unable to fixate caps");
              gst_caps_unref (caps);
              ret = GST_FLOW_NOT_NEGOTIATED;
              *buf = NULL;
              goto done;
            }

            GST_DEBUG_OBJECT (sink, "Caps fixated to %" GST_PTR_FORMAT, caps);
          }

          ret = GST_FLOW_OK;
          *buf = gst_buffer_new ();
          gst_buffer_set_caps (*buf, caps);
          gst_caps_unref (caps);
        }

        if (!*buf)
          ret = GST_FLOW_NOT_NEGOTIATED;
        goto done;
      }
    }
    gst_object_unref (target);
  }

  ret = sink->ghostpad_bufferalloc_func (pad, offset, size, caps, buf);

done:
  gst_object_unref (sink);
  return ret;
}