1 /* GStreamer Split Demuxer bin that recombines files created by
2 * the splitmuxsink element.
3 *
4 * Copyright (C) <2014> Jan Schmidt <jan@centricular.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:element-splitmuxsrc
24 * @title: splitmuxsrc
25 * @short_description: Split Demuxer bin that recombines files created by
26 * the splitmuxsink element.
27 *
28 * This element reads a set of input files created by the splitmuxsink element
29 * containing contiguous elementary streams split across multiple files.
30 *
31 * This element is similar to splitfilesrc, except that it recombines the
32 * streams in each file part at the demuxed elementary level, rather than
33 * as a single larger bytestream.
34 *
35 * ## Example pipelines
36 * |[
37 * gst-launch-1.0 splitmuxsrc location=video*.mov ! decodebin ! xvimagesink
38 * ]| Demux each file part and output the video stream as one continuous stream
39 * |[
40 * gst-launch-1.0 playbin uri="splitmux://path/to/foo.mp4.*"
41 * ]| Play back a set of files created by splitmuxsink
42 *
43 */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <string.h>
50 #include "gstsplitmuxsrc.h"
51 #include "gstsplitutils.h"
52
53 #include "../../gst-libs/gst/gst-i18n-plugin.h"
54
55 GST_DEBUG_CATEGORY (splitmux_debug);
56 #define GST_CAT_DEFAULT splitmux_debug
57
58 #define FIXED_TS_OFFSET (1000*GST_SECOND)
59
60 enum
61 {
62 PROP_0,
63 PROP_LOCATION
64 };
65
66 enum
67 {
68 SIGNAL_FORMAT_LOCATION,
69 SIGNAL_LAST
70 };
71
72 static guint signals[SIGNAL_LAST];
73
74 static GstStaticPadTemplate video_src_template =
75 GST_STATIC_PAD_TEMPLATE ("video",
76 GST_PAD_SRC,
77 GST_PAD_SOMETIMES,
78 GST_STATIC_CAPS_ANY);
79
80 static GstStaticPadTemplate video_aux_src_template =
81 GST_STATIC_PAD_TEMPLATE ("video_%u",
82 GST_PAD_SRC,
83 GST_PAD_SOMETIMES,
84 GST_STATIC_CAPS_ANY);
85
86 static GstStaticPadTemplate audio_src_template =
87 GST_STATIC_PAD_TEMPLATE ("audio_%u",
88 GST_PAD_SRC,
89 GST_PAD_SOMETIMES,
90 GST_STATIC_CAPS_ANY);
91
92 static GstStaticPadTemplate subtitle_src_template =
93 GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
94 GST_PAD_SRC,
95 GST_PAD_SOMETIMES,
96 GST_STATIC_CAPS_ANY);
97
98 static GstStateChangeReturn gst_splitmux_src_change_state (GstElement *
99 element, GstStateChange transition);
100 static void gst_splitmux_src_set_property (GObject * object, guint prop_id,
101 const GValue * value, GParamSpec * pspec);
102 static void gst_splitmux_src_get_property (GObject * object, guint prop_id,
103 GValue * value, GParamSpec * pspec);
104 static void gst_splitmux_src_dispose (GObject * object);
105 static void gst_splitmux_src_finalize (GObject * object);
106 static gboolean gst_splitmux_src_start (GstSplitMuxSrc * splitmux);
107 static gboolean gst_splitmux_src_stop (GstSplitMuxSrc * splitmux);
108 static void splitmux_src_pad_constructed (GObject * pad);
109 static gboolean splitmux_src_pad_event (GstPad * pad, GstObject * parent,
110 GstEvent * event);
111 static gboolean splitmux_src_pad_query (GstPad * pad, GstObject * parent,
112 GstQuery * query);
113 static void splitmux_src_uri_handler_init (gpointer g_iface,
114 gpointer iface_data);
115
116
117 static GstPad *gst_splitmux_find_output_pad (GstSplitMuxPartReader * part,
118 GstPad * pad, GstSplitMuxSrc * splitmux);
119 static gboolean gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux,
120 SplitMuxSrcPad * pad);
121 static gboolean gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad,
122 GstEvent * event);
123 static gboolean gst_splitmux_src_prepare_next_part (GstSplitMuxSrc * splitmux);
124 static gboolean gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux,
125 guint part, GstSeekFlags extra_flags);
126
127 #define _do_init \
128 G_IMPLEMENT_INTERFACE(GST_TYPE_URI_HANDLER, splitmux_src_uri_handler_init); \
129 GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsrc", 0, "Split File Demuxing Source");
130 #define gst_splitmux_src_parent_class parent_class
131
132 G_DEFINE_TYPE_EXTENDED (GstSplitMuxSrc, gst_splitmux_src, GST_TYPE_BIN, 0,
133 _do_init);
134 GST_ELEMENT_REGISTER_DEFINE (splitmuxsrc, "splitmuxsrc", GST_RANK_NONE,
135 GST_TYPE_SPLITMUX_SRC);
136
137 static GstURIType
splitmux_src_uri_get_type(GType type)138 splitmux_src_uri_get_type (GType type)
139 {
140 return GST_URI_SRC;
141 }
142
143 static const gchar *const *
splitmux_src_uri_get_protocols(GType type)144 splitmux_src_uri_get_protocols (GType type)
145 {
146 static const gchar *protocols[] = { "splitmux", NULL };
147
148 return protocols;
149 }
150
151 static gchar *
splitmux_src_uri_get_uri(GstURIHandler * handler)152 splitmux_src_uri_get_uri (GstURIHandler * handler)
153 {
154 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
155 gchar *ret = NULL;
156
157 GST_OBJECT_LOCK (splitmux);
158 if (splitmux->location)
159 ret = g_strdup_printf ("splitmux://%s", splitmux->location);
160 GST_OBJECT_UNLOCK (splitmux);
161 return ret;
162 }
163
164 static gboolean
splitmux_src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** err)165 splitmux_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
166 GError ** err)
167 {
168 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
169 gchar *protocol, *location;
170
171 protocol = gst_uri_get_protocol (uri);
172 if (protocol == NULL || !g_str_equal (protocol, "splitmux"))
173 goto wrong_uri;
174 g_free (protocol);
175
176 location = gst_uri_get_location (uri);
177 GST_OBJECT_LOCK (splitmux);
178 g_free (splitmux->location);
179 splitmux->location = location;
180 GST_OBJECT_UNLOCK (splitmux);
181
182 return TRUE;
183
184 wrong_uri:
185 g_free (protocol);
186 GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
187 ("Error parsing uri %s", uri));
188 g_set_error_literal (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
189 "Could not parse splitmux URI");
190 return FALSE;
191 }
192
193 static void
splitmux_src_uri_handler_init(gpointer g_iface,gpointer iface_data)194 splitmux_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
195 {
196 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) (g_iface);
197
198 iface->get_type = splitmux_src_uri_get_type;
199 iface->get_protocols = splitmux_src_uri_get_protocols;
200 iface->set_uri = splitmux_src_uri_set_uri;
201 iface->get_uri = splitmux_src_uri_get_uri;
202 }
203
204
205 static void
gst_splitmux_src_class_init(GstSplitMuxSrcClass * klass)206 gst_splitmux_src_class_init (GstSplitMuxSrcClass * klass)
207 {
208 GObjectClass *gobject_class = (GObjectClass *) klass;
209 GstElementClass *gstelement_class = (GstElementClass *) klass;
210
211 gobject_class->set_property = gst_splitmux_src_set_property;
212 gobject_class->get_property = gst_splitmux_src_get_property;
213 gobject_class->dispose = gst_splitmux_src_dispose;
214 gobject_class->finalize = gst_splitmux_src_finalize;
215
216 gst_element_class_set_static_metadata (gstelement_class,
217 "Split File Demuxing Bin", "Generic/Bin/Demuxer",
218 "Source that reads a set of files created by splitmuxsink",
219 "Jan Schmidt <jan@centricular.com>");
220
221 gst_element_class_add_static_pad_template (gstelement_class,
222 &video_src_template);
223 gst_element_class_add_static_pad_template (gstelement_class,
224 &video_aux_src_template);
225 gst_element_class_add_static_pad_template (gstelement_class,
226 &audio_src_template);
227 gst_element_class_add_static_pad_template (gstelement_class,
228 &subtitle_src_template);
229
230 gstelement_class->change_state =
231 GST_DEBUG_FUNCPTR (gst_splitmux_src_change_state);
232
233 g_object_class_install_property (gobject_class, PROP_LOCATION,
234 g_param_spec_string ("location", "File Input Pattern",
235 "Glob pattern for the location of the files to read", NULL,
236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237
238 /**
239 * GstSplitMuxSrc::format-location:
240 * @splitmux: the #GstSplitMuxSrc
241 *
242 * Returns: A NULL-terminated sorted array of strings containing the
243 * filenames of the input files. The array will be freed internally
244 * using g_strfreev()
245 *
246 * Since: 1.8
247 */
248 signals[SIGNAL_FORMAT_LOCATION] =
249 g_signal_new ("format-location", G_TYPE_FROM_CLASS (klass),
250 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_STRV, 0);
251 }
252
253 static void
gst_splitmux_src_init(GstSplitMuxSrc * splitmux)254 gst_splitmux_src_init (GstSplitMuxSrc * splitmux)
255 {
256 g_mutex_init (&splitmux->lock);
257 g_rw_lock_init (&splitmux->pads_rwlock);
258 splitmux->total_duration = GST_CLOCK_TIME_NONE;
259 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
260 }
261
262 static void
gst_splitmux_src_dispose(GObject * object)263 gst_splitmux_src_dispose (GObject * object)
264 {
265 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
266 GList *cur;
267
268 SPLITMUX_SRC_PADS_WLOCK (splitmux);
269
270 for (cur = g_list_first (splitmux->pads);
271 cur != NULL; cur = g_list_next (cur)) {
272 GstPad *pad = GST_PAD (cur->data);
273 gst_element_remove_pad (GST_ELEMENT (splitmux), pad);
274 }
275 g_list_free (splitmux->pads);
276 splitmux->n_pads = 0;
277 splitmux->pads = NULL;
278 SPLITMUX_SRC_PADS_WUNLOCK (splitmux);
279
280 G_OBJECT_CLASS (parent_class)->dispose (object);
281 }
282
283 static void
gst_splitmux_src_finalize(GObject * object)284 gst_splitmux_src_finalize (GObject * object)
285 {
286 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
287 g_mutex_clear (&splitmux->lock);
288 g_rw_lock_clear (&splitmux->pads_rwlock);
289 g_free (splitmux->location);
290
291 G_OBJECT_CLASS (parent_class)->finalize (object);
292 }
293
294 static void
gst_splitmux_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)295 gst_splitmux_src_set_property (GObject * object, guint prop_id,
296 const GValue * value, GParamSpec * pspec)
297 {
298 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
299
300 switch (prop_id) {
301 case PROP_LOCATION:{
302 GST_OBJECT_LOCK (splitmux);
303 g_free (splitmux->location);
304 splitmux->location = g_value_dup_string (value);
305 GST_OBJECT_UNLOCK (splitmux);
306 break;
307 }
308 default:
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310 break;
311 }
312 }
313
314 static void
gst_splitmux_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)315 gst_splitmux_src_get_property (GObject * object, guint prop_id,
316 GValue * value, GParamSpec * pspec)
317 {
318 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
319
320 switch (prop_id) {
321 case PROP_LOCATION:
322 GST_OBJECT_LOCK (splitmux);
323 g_value_set_string (value, splitmux->location);
324 GST_OBJECT_UNLOCK (splitmux);
325 break;
326 default:
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
328 break;
329 }
330 }
331
332 static void
do_async_start(GstSplitMuxSrc * splitmux)333 do_async_start (GstSplitMuxSrc * splitmux)
334 {
335 GstMessage *message;
336
337 SPLITMUX_SRC_MSG_LOCK (splitmux);
338 splitmux->async_pending = TRUE;
339
340 message = gst_message_new_async_start (GST_OBJECT_CAST (splitmux));
341 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (splitmux),
342 message);
343 SPLITMUX_SRC_MSG_UNLOCK (splitmux);
344 }
345
346 static void
do_async_done(GstSplitMuxSrc * splitmux)347 do_async_done (GstSplitMuxSrc * splitmux)
348 {
349 GstMessage *message;
350
351 SPLITMUX_SRC_MSG_LOCK (splitmux);
352 if (splitmux->async_pending) {
353 message =
354 gst_message_new_async_done (GST_OBJECT_CAST (splitmux),
355 GST_CLOCK_TIME_NONE);
356 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (splitmux),
357 message);
358
359 splitmux->async_pending = FALSE;
360 }
361 SPLITMUX_SRC_MSG_UNLOCK (splitmux);
362 }
363
364 static GstStateChangeReturn
gst_splitmux_src_change_state(GstElement * element,GstStateChange transition)365 gst_splitmux_src_change_state (GstElement * element, GstStateChange transition)
366 {
367 GstStateChangeReturn ret;
368 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) element;
369
370 switch (transition) {
371 case GST_STATE_CHANGE_NULL_TO_READY:{
372 break;
373 }
374 case GST_STATE_CHANGE_READY_TO_PAUSED:{
375 do_async_start (splitmux);
376
377 if (!gst_splitmux_src_start (splitmux)) {
378 do_async_done (splitmux);
379 return GST_STATE_CHANGE_FAILURE;
380 }
381 break;
382 }
383 case GST_STATE_CHANGE_PAUSED_TO_READY:
384 case GST_STATE_CHANGE_READY_TO_NULL:
385 /* Make sure the element will shut down */
386 if (!gst_splitmux_src_stop (splitmux))
387 return GST_STATE_CHANGE_FAILURE;
388 break;
389 default:
390 break;
391 }
392
393 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
394 if (ret == GST_STATE_CHANGE_FAILURE) {
395 do_async_done (splitmux);
396 return ret;
397 }
398
399 switch (transition) {
400 case GST_STATE_CHANGE_READY_TO_PAUSED:
401 ret = GST_STATE_CHANGE_ASYNC;
402 break;
403 default:
404 break;
405 }
406
407
408 return ret;
409 }
410
411 static void
gst_splitmux_src_activate_first_part(GstSplitMuxSrc * splitmux)412 gst_splitmux_src_activate_first_part (GstSplitMuxSrc * splitmux)
413 {
414 SPLITMUX_SRC_LOCK (splitmux);
415 if (splitmux->running) {
416 if (!gst_splitmux_src_activate_part (splitmux, 0, GST_SEEK_FLAG_NONE)) {
417 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
418 ("Failed to activate first part for playback"));
419 }
420 }
421 SPLITMUX_SRC_UNLOCK (splitmux);
422 }
423
424 static GstBusSyncReply
gst_splitmux_part_bus_handler(GstBus * bus,GstMessage * msg,gpointer user_data)425 gst_splitmux_part_bus_handler (GstBus * bus, GstMessage * msg,
426 gpointer user_data)
427 {
428 GstSplitMuxSrc *splitmux = user_data;
429
430 switch (GST_MESSAGE_TYPE (msg)) {
431 case GST_MESSAGE_ASYNC_DONE:{
432 guint idx = splitmux->num_prepared_parts;
433 gboolean need_no_more_pads;
434
435 if (idx >= splitmux->num_parts) {
436 /* Shouldn't really happen! */
437 do_async_done (splitmux);
438 g_warn_if_reached ();
439 break;
440 }
441
442 GST_DEBUG_OBJECT (splitmux, "Prepared file part %s (%u)",
443 splitmux->parts[idx]->path, idx);
444
445 /* signal no-more-pads as we have all pads at this point now */
446 SPLITMUX_SRC_LOCK (splitmux);
447 need_no_more_pads = !splitmux->pads_complete;
448 splitmux->pads_complete = TRUE;
449 SPLITMUX_SRC_UNLOCK (splitmux);
450
451 if (need_no_more_pads) {
452 GST_DEBUG_OBJECT (splitmux, "Signalling no-more-pads");
453 gst_element_no_more_pads (GST_ELEMENT_CAST (splitmux));
454 }
455
456 /* Extend our total duration to cover this part */
457 GST_OBJECT_LOCK (splitmux);
458 splitmux->total_duration +=
459 gst_splitmux_part_reader_get_duration (splitmux->parts[idx]);
460 splitmux->play_segment.duration = splitmux->total_duration;
461 GST_OBJECT_UNLOCK (splitmux);
462
463 splitmux->end_offset =
464 gst_splitmux_part_reader_get_end_offset (splitmux->parts[idx]);
465
466 GST_DEBUG_OBJECT (splitmux,
467 "Duration %" GST_TIME_FORMAT ", total duration now: %" GST_TIME_FORMAT
468 " and end offset %" GST_TIME_FORMAT,
469 GST_TIME_ARGS (gst_splitmux_part_reader_get_duration (splitmux->parts
470 [idx])), GST_TIME_ARGS (splitmux->total_duration),
471 GST_TIME_ARGS (splitmux->end_offset));
472
473 splitmux->num_prepared_parts++;
474
475 /* If we're done or preparing the next part fails, finish here */
476 if (splitmux->num_prepared_parts >= splitmux->num_parts
477 || !gst_splitmux_src_prepare_next_part (splitmux)) {
478 /* Store how many parts we actually prepared in the end */
479 splitmux->num_parts = splitmux->num_prepared_parts;
480 do_async_done (splitmux);
481
482 /* All done preparing, activate the first part */
483 GST_INFO_OBJECT (splitmux,
484 "All parts prepared. Total duration %" GST_TIME_FORMAT
485 " Activating first part", GST_TIME_ARGS (splitmux->total_duration));
486 gst_element_call_async (GST_ELEMENT_CAST (splitmux),
487 (GstElementCallAsyncFunc) gst_splitmux_src_activate_first_part,
488 NULL, NULL);
489 }
490
491 break;
492 }
493 case GST_MESSAGE_ERROR:{
494 GST_ERROR_OBJECT (splitmux,
495 "Got error message from part %" GST_PTR_FORMAT ": %" GST_PTR_FORMAT,
496 GST_MESSAGE_SRC (msg), msg);
497 if (splitmux->num_prepared_parts < splitmux->num_parts) {
498 guint idx = splitmux->num_prepared_parts;
499
500 if (idx == 0) {
501 GST_ERROR_OBJECT (splitmux,
502 "Failed to prepare first file part %s for playback",
503 splitmux->parts[idx]->path);
504 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
505 ("Failed to prepare first file part %s for playback",
506 splitmux->parts[idx]->path));
507 } else {
508 GST_WARNING_OBJECT (splitmux,
509 "Failed to prepare file part %s. Cannot play past there.",
510 splitmux->parts[idx]->path);
511 GST_ELEMENT_WARNING (splitmux, RESOURCE, READ, (NULL),
512 ("Failed to prepare file part %s. Cannot play past there.",
513 splitmux->parts[idx]->path));
514 }
515
516 /* Store how many parts we actually prepared in the end */
517 splitmux->num_parts = splitmux->num_prepared_parts;
518 do_async_done (splitmux);
519
520 if (idx > 0) {
521 /* All done preparing, activate the first part */
522 GST_INFO_OBJECT (splitmux,
523 "All parts prepared. Total duration %" GST_TIME_FORMAT
524 " Activating first part",
525 GST_TIME_ARGS (splitmux->total_duration));
526 gst_element_call_async (GST_ELEMENT_CAST (splitmux),
527 (GstElementCallAsyncFunc) gst_splitmux_src_activate_first_part,
528 NULL, NULL);
529 }
530 } else {
531 /* Need to update the message source so that it's part of the element
532 * hierarchy the application would expect */
533 msg = gst_message_copy (msg);
534 gst_object_replace ((GstObject **) & msg->src, (GstObject *) splitmux);
535 gst_element_post_message (GST_ELEMENT_CAST (splitmux), msg);
536 }
537 break;
538 }
539 default:
540 break;
541 }
542
543 return GST_BUS_PASS;
544 }
545
546 static GstSplitMuxPartReader *
gst_splitmux_part_create(GstSplitMuxSrc * splitmux,char * filename)547 gst_splitmux_part_create (GstSplitMuxSrc * splitmux, char *filename)
548 {
549 GstSplitMuxPartReader *r;
550 GstBus *bus;
551
552 r = g_object_new (GST_TYPE_SPLITMUX_PART_READER, NULL);
553
554 gst_splitmux_part_reader_set_callbacks (r, splitmux,
555 (GstSplitMuxPartReaderPadCb) gst_splitmux_find_output_pad);
556 gst_splitmux_part_reader_set_location (r, filename);
557
558 bus = gst_element_get_bus (GST_ELEMENT_CAST (r));
559 gst_bus_set_sync_handler (bus, gst_splitmux_part_bus_handler, splitmux, NULL);
560 gst_object_unref (bus);
561
562 return r;
563 }
564
565 static gboolean
gst_splitmux_check_new_caps(SplitMuxSrcPad * splitpad,GstEvent * event)566 gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad, GstEvent * event)
567 {
568 GstCaps *curcaps = gst_pad_get_current_caps ((GstPad *) (splitpad));
569 GstCaps *newcaps;
570 GstCaps *tmpcaps;
571 GstCaps *tmpcurcaps;
572
573 GstStructure *s;
574 gboolean res = TRUE;
575
576 gst_event_parse_caps (event, &newcaps);
577
578 GST_LOG_OBJECT (splitpad, "Comparing caps %" GST_PTR_FORMAT
579 " and %" GST_PTR_FORMAT, curcaps, newcaps);
580
581 if (curcaps == NULL)
582 return TRUE;
583
584 /* If caps are exactly equal exit early */
585 if (gst_caps_is_equal (curcaps, newcaps)) {
586 gst_caps_unref (curcaps);
587 return FALSE;
588 }
589
590 /* More extensive check, ignore changes in framerate, because
591 * demuxers get that wrong */
592 tmpcaps = gst_caps_copy (newcaps);
593 s = gst_caps_get_structure (tmpcaps, 0);
594 gst_structure_remove_field (s, "framerate");
595
596 tmpcurcaps = gst_caps_copy (curcaps);
597 gst_caps_unref (curcaps);
598 s = gst_caps_get_structure (tmpcurcaps, 0);
599 gst_structure_remove_field (s, "framerate");
600
601 /* Now check if these filtered caps are equal */
602 if (gst_caps_is_equal (tmpcurcaps, tmpcaps)) {
603 GST_INFO_OBJECT (splitpad, "Ignoring framerate-only caps change");
604 res = FALSE;
605 }
606
607 gst_caps_unref (tmpcaps);
608 gst_caps_unref (tmpcurcaps);
609 return res;
610 }
611
612 static void
gst_splitmux_handle_event(GstSplitMuxSrc * splitmux,SplitMuxSrcPad * splitpad,GstPad * part_pad,GstEvent * event)613 gst_splitmux_handle_event (GstSplitMuxSrc * splitmux,
614 SplitMuxSrcPad * splitpad, GstPad * part_pad, GstEvent * event)
615 {
616 switch (GST_EVENT_TYPE (event)) {
617 case GST_EVENT_STREAM_START:{
618 if (splitpad->sent_stream_start)
619 goto drop_event;
620 splitpad->sent_stream_start = TRUE;
621 break;
622 }
623 case GST_EVENT_EOS:{
624 if (gst_splitmux_end_of_part (splitmux, splitpad))
625 // Continuing to next part, drop the EOS
626 goto drop_event;
627 if (splitmux->segment_seqnum) {
628 event = gst_event_make_writable (event);
629 gst_event_set_seqnum (event, splitmux->segment_seqnum);
630 }
631 break;
632 }
633 case GST_EVENT_SEGMENT:{
634 GstClockTime duration;
635 GstSegment seg;
636
637 gst_event_copy_segment (event, &seg);
638
639 splitpad->segment.position = seg.position;
640
641 if (splitpad->sent_segment)
642 goto drop_event; /* We already forwarded a segment event */
643
644 /* Calculate output segment */
645 GST_LOG_OBJECT (splitpad, "Pad seg %" GST_SEGMENT_FORMAT
646 " got seg %" GST_SEGMENT_FORMAT
647 " play seg %" GST_SEGMENT_FORMAT,
648 &splitpad->segment, &seg, &splitmux->play_segment);
649
650 /* If playing forward, take the stop time from the overall
651 * seg or play_segment */
652 if (splitmux->play_segment.rate > 0.0) {
653 if (splitmux->play_segment.stop != -1)
654 seg.stop = splitmux->play_segment.stop + FIXED_TS_OFFSET;
655 else
656 seg.stop = splitpad->segment.stop;
657 } else {
658 /* Reverse playback from stop time to start time */
659 /* See if an end point was requested in the seek */
660 if (splitmux->play_segment.start != -1) {
661 seg.start = splitmux->play_segment.start + FIXED_TS_OFFSET;
662 seg.time = splitmux->play_segment.time;
663 } else {
664 seg.start = splitpad->segment.start;
665 seg.time = splitpad->segment.time;
666 }
667 }
668
669 GST_OBJECT_LOCK (splitmux);
670 duration = splitmux->total_duration;
671 GST_OBJECT_UNLOCK (splitmux);
672
673 if (duration > 0)
674 seg.duration = duration;
675 else
676 seg.duration = GST_CLOCK_TIME_NONE;
677
678 GST_INFO_OBJECT (splitpad,
679 "Forwarding segment %" GST_SEGMENT_FORMAT, &seg);
680
681 gst_event_unref (event);
682 event = gst_event_new_segment (&seg);
683 if (splitmux->segment_seqnum)
684 gst_event_set_seqnum (event, splitmux->segment_seqnum);
685 splitpad->sent_segment = TRUE;
686 break;
687 }
688 case GST_EVENT_CAPS:{
689 if (!gst_splitmux_check_new_caps (splitpad, event))
690 goto drop_event;
691 splitpad->sent_caps = TRUE;
692 break;
693 }
694 default:
695 break;
696 }
697
698 gst_pad_push_event ((GstPad *) (splitpad), event);
699 return;
700 drop_event:
701 gst_event_unref (event);
702 return;
703 }
704
705 static GstFlowReturn
gst_splitmux_handle_buffer(GstSplitMuxSrc * splitmux,SplitMuxSrcPad * splitpad,GstBuffer * buf)706 gst_splitmux_handle_buffer (GstSplitMuxSrc * splitmux,
707 SplitMuxSrcPad * splitpad, GstBuffer * buf)
708 {
709 GstFlowReturn ret;
710
711 if (splitpad->clear_next_discont) {
712 GST_LOG_OBJECT (splitpad, "Clearing discont flag on buffer");
713 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
714 splitpad->clear_next_discont = FALSE;
715 }
716 if (splitpad->set_next_discont) {
717 GST_LOG_OBJECT (splitpad, "Setting discont flag on buffer");
718 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
719 splitpad->set_next_discont = FALSE;
720 }
721
722 ret = gst_pad_push (GST_PAD_CAST (splitpad), buf);
723
724 GST_LOG_OBJECT (splitpad, "Pad push returned %d", ret);
725 return ret;
726 }
727
728 static guint
count_not_linked(GstSplitMuxSrc * splitmux)729 count_not_linked (GstSplitMuxSrc * splitmux)
730 {
731 GList *cur;
732 guint ret = 0;
733
734 for (cur = g_list_first (splitmux->pads);
735 cur != NULL; cur = g_list_next (cur)) {
736 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
737 if (GST_PAD_LAST_FLOW_RETURN (splitpad) == GST_FLOW_NOT_LINKED)
738 ret++;
739 }
740
741 return ret;
742 }
743
744 static void
gst_splitmux_pad_loop(GstPad * pad)745 gst_splitmux_pad_loop (GstPad * pad)
746 {
747 /* Get one event/buffer from the associated part and push */
748 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (pad);
749 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) gst_pad_get_parent (pad);
750 GstDataQueueItem *item = NULL;
751 GstSplitMuxPartReader *reader;
752 GstPad *part_pad;
753 GstFlowReturn ret;
754
755 GST_OBJECT_LOCK (splitpad);
756 if (splitpad->part_pad == NULL) {
757 GST_DEBUG_OBJECT (splitmux,
758 "Pausing task because part reader is not present");
759 GST_OBJECT_UNLOCK (splitpad);
760 gst_pad_pause_task (pad);
761 gst_object_unref (splitmux);
762 return;
763 }
764 part_pad = gst_object_ref (splitpad->part_pad);
765 reader = splitpad->reader;
766 GST_OBJECT_UNLOCK (splitpad);
767
768 GST_LOG_OBJECT (splitpad, "Popping data queue item from %" GST_PTR_FORMAT
769 " pad %" GST_PTR_FORMAT, reader, part_pad);
770 ret = gst_splitmux_part_reader_pop (reader, part_pad, &item);
771 if (ret == GST_FLOW_ERROR)
772 goto error;
773 if (ret == GST_FLOW_FLUSHING || item == NULL)
774 goto flushing;
775
776 GST_DEBUG_OBJECT (splitpad, "Got data queue item %" GST_PTR_FORMAT,
777 item->object);
778
779 if (GST_IS_EVENT (item->object)) {
780 GstEvent *event = (GstEvent *) (item->object);
781 gst_splitmux_handle_event (splitmux, splitpad, part_pad, event);
782 } else {
783 GstBuffer *buf = (GstBuffer *) (item->object);
784 GstFlowReturn ret = gst_splitmux_handle_buffer (splitmux, splitpad, buf);
785 if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)) {
786 /* Stop immediately on error or flushing */
787 GST_INFO_OBJECT (splitpad, "Stopping due to pad_push() result %d", ret);
788 gst_pad_pause_task (pad);
789 if (ret < GST_FLOW_EOS) {
790 GST_ELEMENT_FLOW_ERROR (splitmux, ret);
791 } else if (ret == GST_FLOW_NOT_LINKED) {
792 gboolean post_error;
793 guint n_notlinked;
794
795 /* Only post not-linked if all pads are not-linked */
796 SPLITMUX_SRC_PADS_RLOCK (splitmux);
797 n_notlinked = count_not_linked (splitmux);
798 post_error = (splitmux->pads_complete
799 && n_notlinked == splitmux->n_pads);
800 SPLITMUX_SRC_PADS_RUNLOCK (splitmux);
801
802 if (post_error)
803 GST_ELEMENT_FLOW_ERROR (splitmux, ret);
804 }
805 }
806 }
807 g_slice_free (GstDataQueueItem, item);
808
809 gst_object_unref (part_pad);
810 gst_object_unref (splitmux);
811 return;
812
813 error:
814 /* Fall through */
815 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
816 ("Error reading part file %s", GST_STR_NULL (reader->path)));
817 flushing:
818 gst_pad_pause_task (pad);
819 gst_object_unref (part_pad);
820 gst_object_unref (splitmux);
821 return;
822 }
823
824 static gboolean
gst_splitmux_src_activate_part(GstSplitMuxSrc * splitmux,guint part,GstSeekFlags extra_flags)825 gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux, guint part,
826 GstSeekFlags extra_flags)
827 {
828 GList *cur;
829
830 GST_DEBUG_OBJECT (splitmux, "Activating part %d", part);
831
832 splitmux->cur_part = part;
833 if (!gst_splitmux_part_reader_activate (splitmux->parts[part],
834 &splitmux->play_segment, extra_flags))
835 return FALSE;
836
837 SPLITMUX_SRC_PADS_RLOCK (splitmux);
838 for (cur = g_list_first (splitmux->pads);
839 cur != NULL; cur = g_list_next (cur)) {
840 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
841 GST_OBJECT_LOCK (splitpad);
842 splitpad->cur_part = part;
843 splitpad->reader = splitmux->parts[splitpad->cur_part];
844 if (splitpad->part_pad)
845 gst_object_unref (splitpad->part_pad);
846 splitpad->part_pad =
847 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
848 (GstPad *) (splitpad));
849 GST_OBJECT_UNLOCK (splitpad);
850
851 /* Make sure we start with a DISCONT */
852 splitpad->set_next_discont = TRUE;
853 splitpad->clear_next_discont = FALSE;
854
855 gst_pad_start_task (GST_PAD (splitpad),
856 (GstTaskFunction) gst_splitmux_pad_loop, splitpad, NULL);
857 }
858 SPLITMUX_SRC_PADS_RUNLOCK (splitmux);
859
860 return TRUE;
861 }
862
863 static gboolean
gst_splitmux_src_prepare_next_part(GstSplitMuxSrc * splitmux)864 gst_splitmux_src_prepare_next_part (GstSplitMuxSrc * splitmux)
865 {
866 guint idx = splitmux->num_prepared_parts;
867
868 g_assert (idx < splitmux->num_parts);
869
870 GST_DEBUG_OBJECT (splitmux, "Preparing file part %s (%u)",
871 splitmux->parts[idx]->path, idx);
872
873 gst_splitmux_part_reader_set_start_offset (splitmux->parts[idx],
874 splitmux->end_offset, FIXED_TS_OFFSET);
875 if (!gst_splitmux_part_reader_prepare (splitmux->parts[idx])) {
876 GST_WARNING_OBJECT (splitmux,
877 "Failed to prepare file part %s. Cannot play past there.",
878 splitmux->parts[idx]->path);
879 GST_ELEMENT_WARNING (splitmux, RESOURCE, READ, (NULL),
880 ("Failed to prepare file part %s. Cannot play past there.",
881 splitmux->parts[idx]->path));
882 gst_splitmux_part_reader_unprepare (splitmux->parts[idx]);
883 g_object_unref (splitmux->parts[idx]);
884 splitmux->parts[idx] = NULL;
885 return FALSE;
886 }
887
888 return TRUE;
889 }
890
891 static gboolean
gst_splitmux_src_start(GstSplitMuxSrc * splitmux)892 gst_splitmux_src_start (GstSplitMuxSrc * splitmux)
893 {
894 gboolean ret = FALSE;
895 GError *err = NULL;
896 gchar *basename = NULL;
897 gchar *dirname = NULL;
898 gchar **files;
899 guint i;
900
901 SPLITMUX_SRC_LOCK (splitmux);
902 if (splitmux->running) {
903 /* splitmux is still running / stopping. We can't start again yet */
904 SPLITMUX_SRC_UNLOCK (splitmux);
905 return FALSE;
906 }
907 SPLITMUX_SRC_UNLOCK (splitmux);
908
909 GST_DEBUG_OBJECT (splitmux, "Starting");
910
911 g_signal_emit (splitmux, signals[SIGNAL_FORMAT_LOCATION], 0, &files);
912
913 if (files == NULL || *files == NULL) {
914 GST_OBJECT_LOCK (splitmux);
915 if (splitmux->location != NULL && splitmux->location[0] != '\0') {
916 basename = g_path_get_basename (splitmux->location);
917 dirname = g_path_get_dirname (splitmux->location);
918 }
919 GST_OBJECT_UNLOCK (splitmux);
920
921 g_strfreev (files);
922 files = gst_split_util_find_files (dirname, basename, &err);
923
924 if (files == NULL || *files == NULL)
925 goto no_files;
926 }
927
928 SPLITMUX_SRC_LOCK (splitmux);
929 splitmux->pads_complete = FALSE;
930 splitmux->running = TRUE;
931 SPLITMUX_SRC_UNLOCK (splitmux);
932
933 splitmux->num_parts = g_strv_length (files);
934
935 splitmux->parts = g_new0 (GstSplitMuxPartReader *, splitmux->num_parts);
936
937 /* Create all part pipelines */
938 for (i = 0; i < splitmux->num_parts; i++) {
939 splitmux->parts[i] = gst_splitmux_part_create (splitmux, files[i]);
940 if (splitmux->parts[i] == NULL)
941 break;
942 }
943
944 /* Store how many parts we actually created */
945 splitmux->num_created_parts = splitmux->num_parts = i;
946 splitmux->num_prepared_parts = 0;
947
948 /* Update total_duration state variable */
949 GST_OBJECT_LOCK (splitmux);
950 splitmux->total_duration = 0;
951 splitmux->end_offset = 0;
952 GST_OBJECT_UNLOCK (splitmux);
953
954 /* Then start the first: it will asynchronously go to PAUSED
955 * or error out and then we can proceed with the next one
956 */
957 if (!gst_splitmux_src_prepare_next_part (splitmux) || splitmux->num_parts < 1)
958 goto failed_part;
959
960 /* All good now: we have to wait for all parts to be asynchronously
961 * prepared to know the total duration we can play */
962 ret = TRUE;
963
964 done:
965 if (err != NULL)
966 g_error_free (err);
967 g_strfreev (files);
968 g_free (basename);
969 g_free (dirname);
970
971 return ret;
972
973 /* ERRORS */
974 no_files:
975 {
976 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, ("%s", err->message),
977 ("Failed to find files in '%s' for pattern '%s'",
978 GST_STR_NULL (dirname), GST_STR_NULL (basename)));
979 goto done;
980 }
981 failed_part:
982 {
983 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
984 ("Failed to open any files for reading"));
985 goto done;
986 }
987 }
988
989 static gboolean
gst_splitmux_src_stop(GstSplitMuxSrc * splitmux)990 gst_splitmux_src_stop (GstSplitMuxSrc * splitmux)
991 {
992 gboolean ret = TRUE;
993 guint i;
994 GList *cur, *pads_list;
995
996 SPLITMUX_SRC_LOCK (splitmux);
997 if (!splitmux->running)
998 goto out;
999 splitmux->running = FALSE;
1000 GST_DEBUG_OBJECT (splitmux, "Stopping");
1001
1002 SPLITMUX_SRC_UNLOCK (splitmux);
1003
1004 /* Stop and destroy all parts. We don't need the lock here,
1005 * because all parts were created in _start() */
1006 for (i = 0; i < splitmux->num_created_parts; i++) {
1007 if (splitmux->parts[i] == NULL)
1008 continue;
1009 gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
1010 g_object_unref (splitmux->parts[i]);
1011 splitmux->parts[i] = NULL;
1012 }
1013 SPLITMUX_SRC_LOCK (splitmux);
1014
1015 SPLITMUX_SRC_PADS_WLOCK (splitmux);
1016 pads_list = splitmux->pads;
1017 splitmux->pads = NULL;
1018 SPLITMUX_SRC_PADS_WUNLOCK (splitmux);
1019
1020 SPLITMUX_SRC_UNLOCK (splitmux);
1021 for (cur = g_list_first (pads_list); cur != NULL; cur = g_list_next (cur)) {
1022 SplitMuxSrcPad *tmp = (SplitMuxSrcPad *) (cur->data);
1023 gst_pad_stop_task (GST_PAD (tmp));
1024 gst_element_remove_pad (GST_ELEMENT (splitmux), GST_PAD (tmp));
1025 }
1026 g_list_free (pads_list);
1027 SPLITMUX_SRC_LOCK (splitmux);
1028
1029 g_free (splitmux->parts);
1030 splitmux->parts = NULL;
1031 splitmux->num_parts = 0;
1032 splitmux->num_prepared_parts = 0;
1033 splitmux->num_created_parts = 0;
1034 splitmux->total_duration = GST_CLOCK_TIME_NONE;
1035 /* Reset playback segment */
1036 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
1037 out:
1038 SPLITMUX_SRC_UNLOCK (splitmux);
1039 return ret;
1040 }
1041
1042 typedef struct
1043 {
1044 GstSplitMuxSrc *splitmux;
1045 SplitMuxSrcPad *splitpad;
1046 } SplitMuxAndPad;
1047
1048 static gboolean
handle_sticky_events(GstPad * pad,GstEvent ** event,gpointer user_data)1049 handle_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1050 {
1051 SplitMuxAndPad *splitmux_and_pad;
1052 GstSplitMuxSrc *splitmux;
1053 SplitMuxSrcPad *splitpad;
1054
1055 splitmux_and_pad = user_data;
1056 splitmux = splitmux_and_pad->splitmux;
1057 splitpad = splitmux_and_pad->splitpad;
1058
1059 GST_DEBUG_OBJECT (splitpad, "handle sticky event %" GST_PTR_FORMAT, *event);
1060 gst_event_ref (*event);
1061 gst_splitmux_handle_event (splitmux, splitpad, pad, *event);
1062
1063 return TRUE;
1064 }
1065
1066 static GstPad *
gst_splitmux_find_output_pad(GstSplitMuxPartReader * part,GstPad * pad,GstSplitMuxSrc * splitmux)1067 gst_splitmux_find_output_pad (GstSplitMuxPartReader * part, GstPad * pad,
1068 GstSplitMuxSrc * splitmux)
1069 {
1070 GList *cur;
1071 gchar *pad_name = gst_pad_get_name (pad);
1072 GstPad *target = NULL;
1073 gboolean is_new_pad = FALSE;
1074
1075 SPLITMUX_SRC_LOCK (splitmux);
1076 SPLITMUX_SRC_PADS_WLOCK (splitmux);
1077 for (cur = g_list_first (splitmux->pads);
1078 cur != NULL; cur = g_list_next (cur)) {
1079 GstPad *tmp = (GstPad *) (cur->data);
1080 if (g_str_equal (GST_PAD_NAME (tmp), pad_name)) {
1081 target = tmp;
1082 break;
1083 }
1084 }
1085
1086 if (target == NULL && !splitmux->pads_complete) {
1087 SplitMuxAndPad splitmux_and_pad;
1088
1089 /* No pad found, create one */
1090 target = g_object_new (SPLITMUX_TYPE_SRC_PAD,
1091 "name", pad_name, "direction", GST_PAD_SRC, NULL);
1092 splitmux->pads = g_list_prepend (splitmux->pads, target);
1093 splitmux->n_pads++;
1094
1095 gst_pad_set_active (target, TRUE);
1096
1097 splitmux_and_pad.splitmux = splitmux;
1098 splitmux_and_pad.splitpad = (SplitMuxSrcPad *) target;
1099 gst_pad_sticky_events_foreach (pad, handle_sticky_events,
1100 &splitmux_and_pad);
1101 is_new_pad = TRUE;
1102 }
1103 SPLITMUX_SRC_PADS_WUNLOCK (splitmux);
1104 SPLITMUX_SRC_UNLOCK (splitmux);
1105
1106 g_free (pad_name);
1107
1108 if (target == NULL)
1109 goto pad_not_found;
1110
1111 if (is_new_pad)
1112 gst_element_add_pad (GST_ELEMENT_CAST (splitmux), target);
1113
1114 return target;
1115
1116 pad_not_found:
1117 GST_ELEMENT_ERROR (splitmux, STREAM, FAILED, (NULL),
1118 ("Stream part %s contains extra unknown pad %" GST_PTR_FORMAT,
1119 part->path, pad));
1120 return NULL;
1121 }
1122
1123 static void
gst_splitmux_push_event(GstSplitMuxSrc * splitmux,GstEvent * e,guint32 seqnum)1124 gst_splitmux_push_event (GstSplitMuxSrc * splitmux, GstEvent * e,
1125 guint32 seqnum)
1126 {
1127 GList *cur;
1128
1129 if (seqnum) {
1130 e = gst_event_make_writable (e);
1131 gst_event_set_seqnum (e, seqnum);
1132 }
1133
1134 SPLITMUX_SRC_PADS_RLOCK (splitmux);
1135 for (cur = g_list_first (splitmux->pads);
1136 cur != NULL; cur = g_list_next (cur)) {
1137 GstPad *pad = GST_PAD_CAST (cur->data);
1138 gst_event_ref (e);
1139 gst_pad_push_event (pad, e);
1140 }
1141 SPLITMUX_SRC_PADS_RUNLOCK (splitmux);
1142
1143 gst_event_unref (e);
1144 }
1145
1146 static void
gst_splitmux_push_flush_stop(GstSplitMuxSrc * splitmux,guint32 seqnum)1147 gst_splitmux_push_flush_stop (GstSplitMuxSrc * splitmux, guint32 seqnum)
1148 {
1149 GstEvent *e = gst_event_new_flush_stop (TRUE);
1150 GList *cur;
1151
1152 if (seqnum) {
1153 e = gst_event_make_writable (e);
1154 gst_event_set_seqnum (e, seqnum);
1155 }
1156
1157 SPLITMUX_SRC_PADS_RLOCK (splitmux);
1158 for (cur = g_list_first (splitmux->pads);
1159 cur != NULL; cur = g_list_next (cur)) {
1160 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
1161
1162 gst_event_ref (e);
1163 gst_pad_push_event (GST_PAD_CAST (target), e);
1164 target->sent_caps = FALSE;
1165 target->sent_stream_start = FALSE;
1166 target->sent_segment = FALSE;
1167 }
1168 SPLITMUX_SRC_PADS_RUNLOCK (splitmux);
1169
1170 gst_event_unref (e);
1171 }
1172
1173 /* Callback for when a part finishes and we need to move to the next */
1174 static gboolean
gst_splitmux_end_of_part(GstSplitMuxSrc * splitmux,SplitMuxSrcPad * splitpad)1175 gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux, SplitMuxSrcPad * splitpad)
1176 {
1177 gint next_part = -1;
1178 gint cur_part = splitpad->cur_part;
1179 gboolean res = FALSE;
1180
1181 if (splitmux->play_segment.rate >= 0.0) {
1182 if (cur_part + 1 < splitmux->num_parts)
1183 next_part = cur_part + 1;
1184 /* Make sure the transition is seamless */
1185 splitpad->set_next_discont = FALSE;
1186 splitpad->clear_next_discont = TRUE;
1187 } else {
1188 /* Reverse play - move to previous segment */
1189 if (cur_part > 0) {
1190 next_part = cur_part - 1;
1191 /* Non-seamless transition in reverse */
1192 splitpad->set_next_discont = TRUE;
1193 splitpad->clear_next_discont = FALSE;
1194 }
1195 }
1196
1197 SPLITMUX_SRC_LOCK (splitmux);
1198
1199 /* If all pads are done with this part, deactivate it */
1200 if (gst_splitmux_part_is_eos (splitmux->parts[splitpad->cur_part]))
1201 gst_splitmux_part_reader_deactivate (splitmux->parts[cur_part]);
1202
1203 if (splitmux->play_segment.rate >= 0.0) {
1204 if (splitmux->play_segment.stop != -1) {
1205 GstClockTime part_end =
1206 gst_splitmux_part_reader_get_end_offset (splitmux->parts[cur_part]);
1207 if (part_end >= splitmux->play_segment.stop) {
1208 GST_DEBUG_OBJECT (splitmux,
1209 "Stop position was within that part. Finishing");
1210 next_part = -1;
1211 }
1212 }
1213 } else {
1214 if (splitmux->play_segment.start != -1) {
1215 GstClockTime part_start =
1216 gst_splitmux_part_reader_get_start_offset (splitmux->parts[cur_part]);
1217 if (part_start <= splitmux->play_segment.start) {
1218 GST_DEBUG_OBJECT (splitmux,
1219 "Start position %" GST_TIME_FORMAT
1220 " was within that part. Finishing",
1221 GST_TIME_ARGS (splitmux->play_segment.start));
1222 next_part = -1;
1223 }
1224 }
1225 }
1226
1227 if (next_part != -1) {
1228 GST_DEBUG_OBJECT (splitmux, "At EOS on pad %" GST_PTR_FORMAT
1229 " moving to part %d", splitpad, next_part);
1230 splitpad->cur_part = next_part;
1231 splitpad->reader = splitmux->parts[splitpad->cur_part];
1232 if (splitpad->part_pad)
1233 gst_object_unref (splitpad->part_pad);
1234 splitpad->part_pad =
1235 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
1236 (GstPad *) (splitpad));
1237
1238 if (splitmux->cur_part != next_part) {
1239 if (!gst_splitmux_part_reader_is_active (splitpad->reader)) {
1240 GstSegment tmp;
1241 /* If moving backward into a new part, set stop
1242 * to -1 to ensure we play the entire file - workaround
1243 * a bug in qtdemux that misses bits at the end */
1244 gst_segment_copy_into (&splitmux->play_segment, &tmp);
1245 if (tmp.rate < 0)
1246 tmp.stop = -1;
1247
1248 /* This is the first pad to move to the new part, activate it */
1249 GST_DEBUG_OBJECT (splitpad,
1250 "First pad to change part. Activating part %d with seg %"
1251 GST_SEGMENT_FORMAT, next_part, &tmp);
1252 if (!gst_splitmux_part_reader_activate (splitpad->reader, &tmp,
1253 GST_SEEK_FLAG_NONE))
1254 goto error;
1255 }
1256 splitmux->cur_part = next_part;
1257 }
1258 res = TRUE;
1259 }
1260
1261 SPLITMUX_SRC_UNLOCK (splitmux);
1262 return res;
1263 error:
1264 SPLITMUX_SRC_UNLOCK (splitmux);
1265 GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
1266 ("Failed to activate part %d", splitmux->cur_part));
1267 return FALSE;
1268 }
1269
1270 G_DEFINE_TYPE (SplitMuxSrcPad, splitmux_src_pad, GST_TYPE_PAD);
1271
1272 static void
splitmux_src_pad_constructed(GObject * pad)1273 splitmux_src_pad_constructed (GObject * pad)
1274 {
1275 gst_pad_set_event_function (GST_PAD (pad),
1276 GST_DEBUG_FUNCPTR (splitmux_src_pad_event));
1277 gst_pad_set_query_function (GST_PAD (pad),
1278 GST_DEBUG_FUNCPTR (splitmux_src_pad_query));
1279
1280 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->constructed (pad);
1281 }
1282
1283 static void
gst_splitmux_src_pad_dispose(GObject * object)1284 gst_splitmux_src_pad_dispose (GObject * object)
1285 {
1286 SplitMuxSrcPad *pad = (SplitMuxSrcPad *) (object);
1287
1288 GST_OBJECT_LOCK (pad);
1289 if (pad->part_pad) {
1290 gst_object_unref (pad->part_pad);
1291 pad->part_pad = NULL;
1292 }
1293 GST_OBJECT_UNLOCK (pad);
1294
1295 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->dispose (object);
1296 }
1297
1298 static void
splitmux_src_pad_class_init(SplitMuxSrcPadClass * klass)1299 splitmux_src_pad_class_init (SplitMuxSrcPadClass * klass)
1300 {
1301 GObjectClass *gobject_klass = (GObjectClass *) (klass);
1302
1303 gobject_klass->constructed = splitmux_src_pad_constructed;
1304 gobject_klass->dispose = gst_splitmux_src_pad_dispose;
1305 }
1306
1307 static void
splitmux_src_pad_init(SplitMuxSrcPad * pad)1308 splitmux_src_pad_init (SplitMuxSrcPad * pad)
1309 {
1310 }
1311
1312 /* Event handler for source pads. Proxy events into the child
1313 * parts as needed
1314 */
1315 static gboolean
splitmux_src_pad_event(GstPad * pad,GstObject * parent,GstEvent * event)1316 splitmux_src_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
1317 {
1318 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1319 gboolean ret = FALSE;
1320
1321 GST_DEBUG_OBJECT (parent, "event %" GST_PTR_FORMAT
1322 " on %" GST_PTR_FORMAT, event, pad);
1323
1324 switch (GST_EVENT_TYPE (event)) {
1325 case GST_EVENT_SEEK:{
1326 GstFormat format;
1327 gdouble rate;
1328 GstSeekFlags flags;
1329 GstSeekType start_type, stop_type;
1330 gint64 start, stop;
1331 guint32 seqnum;
1332 gint i;
1333 GstClockTime part_start, position;
1334 GList *cur;
1335 GstSegment tmp;
1336
1337 gst_event_parse_seek (event, &rate, &format, &flags,
1338 &start_type, &start, &stop_type, &stop);
1339
1340 if (format != GST_FORMAT_TIME) {
1341 GST_DEBUG_OBJECT (splitmux, "can only seek on TIME");
1342 goto error;
1343 }
1344 /* FIXME: Support non-flushing seeks, which might never wake up */
1345 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1346 GST_DEBUG_OBJECT (splitmux, "Only flushing seeks supported");
1347 goto error;
1348 }
1349 seqnum = gst_event_get_seqnum (event);
1350
1351 SPLITMUX_SRC_LOCK (splitmux);
1352 if (!splitmux->running || splitmux->num_parts < 1) {
1353 /* Not started yet */
1354 SPLITMUX_SRC_UNLOCK (splitmux);
1355 goto error;
1356 }
1357 if (splitmux->segment_seqnum == seqnum) {
1358 GST_DEBUG_OBJECT (splitmux, "Ignoring duplicate seek event");
1359 SPLITMUX_SRC_UNLOCK (splitmux);
1360 ret = TRUE;
1361 goto done;
1362 }
1363
1364 gst_segment_copy_into (&splitmux->play_segment, &tmp);
1365
1366 if (!gst_segment_do_seek (&tmp, rate,
1367 format, flags, start_type, start, stop_type, stop, NULL)) {
1368 /* Invalid seek requested, ignore it */
1369 SPLITMUX_SRC_UNLOCK (splitmux);
1370 goto error;
1371 }
1372 position = tmp.position;
1373
1374 GST_DEBUG_OBJECT (splitmux, "Performing seek with seg %"
1375 GST_SEGMENT_FORMAT, &tmp);
1376
1377 GST_DEBUG_OBJECT (splitmux,
1378 "Handling flushing seek. Sending flush start");
1379
1380 /* Send flush_start */
1381 gst_splitmux_push_event (splitmux, gst_event_new_flush_start (), seqnum);
1382
1383 /* Stop all parts, which will work because of the flush */
1384 SPLITMUX_SRC_PADS_RLOCK (splitmux);
1385 SPLITMUX_SRC_UNLOCK (splitmux);
1386 for (cur = g_list_first (splitmux->pads);
1387 cur != NULL; cur = g_list_next (cur)) {
1388 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
1389 GstSplitMuxPartReader *reader = splitmux->parts[target->cur_part];
1390 gst_splitmux_part_reader_deactivate (reader);
1391 }
1392
1393 /* Shut down pad tasks */
1394 GST_DEBUG_OBJECT (splitmux, "Pausing pad tasks");
1395 for (cur = g_list_first (splitmux->pads);
1396 cur != NULL; cur = g_list_next (cur)) {
1397 GstPad *splitpad = (GstPad *) (cur->data);
1398 gst_pad_pause_task (GST_PAD (splitpad));
1399 }
1400 SPLITMUX_SRC_PADS_RUNLOCK (splitmux);
1401 SPLITMUX_SRC_LOCK (splitmux);
1402
1403 /* Send flush stop */
1404 GST_DEBUG_OBJECT (splitmux, "Sending flush stop");
1405 gst_splitmux_push_flush_stop (splitmux, seqnum);
1406
1407 /* Everything is stopped, so update the play_segment */
1408 gst_segment_copy_into (&tmp, &splitmux->play_segment);
1409 splitmux->segment_seqnum = seqnum;
1410
1411 /* Work out where to start from now */
1412 for (i = 0; i < splitmux->num_parts; i++) {
1413 GstSplitMuxPartReader *reader = splitmux->parts[i];
1414 GstClockTime part_end =
1415 gst_splitmux_part_reader_get_end_offset (reader);
1416
1417 if (part_end > position)
1418 break;
1419 }
1420 if (i == splitmux->num_parts)
1421 i = splitmux->num_parts - 1;
1422
1423 part_start =
1424 gst_splitmux_part_reader_get_start_offset (splitmux->parts[i]);
1425
1426 GST_DEBUG_OBJECT (splitmux,
1427 "Seek to time %" GST_TIME_FORMAT " landed in part %d offset %"
1428 GST_TIME_FORMAT, GST_TIME_ARGS (position),
1429 i, GST_TIME_ARGS (position - part_start));
1430
1431 ret = gst_splitmux_src_activate_part (splitmux, i, flags);
1432 SPLITMUX_SRC_UNLOCK (splitmux);
1433 }
1434 case GST_EVENT_RECONFIGURE:{
1435 GST_DEBUG_OBJECT (splitmux, "reconfigure event on pad %" GST_PTR_FORMAT,
1436 pad);
1437
1438 SPLITMUX_SRC_PADS_RLOCK (splitmux);
1439 /* Restart the task on this pad */
1440 gst_pad_start_task (GST_PAD (pad),
1441 (GstTaskFunction) gst_splitmux_pad_loop, pad, NULL);
1442 SPLITMUX_SRC_PADS_RUNLOCK (splitmux);
1443 break;
1444 }
1445 default:
1446 break;
1447 }
1448
1449 done:
1450 gst_event_unref (event);
1451 error:
1452 return ret;
1453 }
1454
1455 static gboolean
splitmux_src_pad_query(GstPad * pad,GstObject * parent,GstQuery * query)1456 splitmux_src_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1457 {
1458 /* Query handler for source pads. Proxy queries into the child
1459 * parts as needed
1460 */
1461 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1462 gboolean ret = FALSE;
1463
1464 GST_LOG_OBJECT (parent, "query %" GST_PTR_FORMAT
1465 " on %" GST_PTR_FORMAT, query, pad);
1466 switch (GST_QUERY_TYPE (query)) {
1467 case GST_QUERY_CAPS:
1468 case GST_QUERY_POSITION:{
1469 GstSplitMuxPartReader *part;
1470 SplitMuxSrcPad *anypad;
1471
1472 SPLITMUX_SRC_LOCK (splitmux);
1473 SPLITMUX_SRC_PADS_RLOCK (splitmux);
1474 anypad = (SplitMuxSrcPad *) (splitmux->pads->data);
1475 part = splitmux->parts[anypad->cur_part];
1476 ret = gst_splitmux_part_reader_src_query (part, pad, query);
1477 SPLITMUX_SRC_PADS_RUNLOCK (splitmux);
1478 SPLITMUX_SRC_UNLOCK (splitmux);
1479 break;
1480 }
1481 case GST_QUERY_DURATION:{
1482 GstClockTime duration;
1483 GstFormat fmt;
1484
1485 gst_query_parse_duration (query, &fmt, NULL);
1486 if (fmt != GST_FORMAT_TIME)
1487 break;
1488
1489 GST_OBJECT_LOCK (splitmux);
1490 duration = splitmux->total_duration;
1491 GST_OBJECT_UNLOCK (splitmux);
1492
1493 if (duration > 0 && duration != GST_CLOCK_TIME_NONE) {
1494 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
1495 ret = TRUE;
1496 }
1497 break;
1498 }
1499 case GST_QUERY_SEEKING:{
1500 GstFormat format;
1501
1502 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1503 if (format != GST_FORMAT_TIME)
1504 break;
1505
1506 GST_OBJECT_LOCK (splitmux);
1507 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
1508 splitmux->total_duration);
1509 ret = TRUE;
1510 GST_OBJECT_UNLOCK (splitmux);
1511
1512 break;
1513 }
1514 case GST_QUERY_SEGMENT:{
1515 GstFormat format;
1516 gint64 start, stop;
1517
1518 SPLITMUX_SRC_LOCK (splitmux);
1519 format = splitmux->play_segment.format;
1520
1521 start =
1522 gst_segment_to_stream_time (&splitmux->play_segment, format,
1523 splitmux->play_segment.start);
1524 if (splitmux->play_segment.stop == GST_CLOCK_TIME_NONE) {
1525 if (splitmux->play_segment.duration == GST_CLOCK_TIME_NONE)
1526 stop = GST_CLOCK_TIME_NONE;
1527 else
1528 stop = start + splitmux->play_segment.duration;
1529 } else {
1530 stop = gst_segment_to_stream_time (&splitmux->play_segment, format,
1531 splitmux->play_segment.stop);
1532 }
1533
1534 gst_query_set_segment (query, splitmux->play_segment.rate, format, start,
1535 stop);
1536 ret = TRUE;
1537
1538 SPLITMUX_SRC_UNLOCK (splitmux);
1539 }
1540 default:
1541 break;
1542 }
1543 return ret;
1544 }
1545