3 * Copyright (C) 2007 Alessandro Decina
5 * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
6 * Author: Youness Alaoui <youness.alaoui@collabora.co.uk>, Collabora Ltd.
7 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
8 * Author: Edward Hervey <bilboed@bilboed.com>, Collabora Ltd.
11 * Alessandro Decina <alessandro@nnva.org>
12 * Zaheer Abbas Merali <zaheerabbas at merali dot org>
13 * Edward Hervey <edward.hervey@collabora.co.uk>
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Library General Public
17 * License as published by the Free Software Foundation; either
18 * version 2 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Library General Public License for more details.
25 * You should have received a copy of the GNU Library General Public
26 * License along with this library; if not, write to the
27 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
28 * Boston, MA 02110-1301, USA.
40 #include <glib/gi18n-lib.h>
41 #include "mpegtsbase.h"
42 #include "gstmpegdesc.h"
44 #define RUNNING_STATUS_RUNNING 4
46 GST_DEBUG_CATEGORY_STATIC (mpegts_base_debug);
47 #define GST_CAT_DEFAULT mpegts_base_debug
49 static GQuark QUARK_PROGRAMS;
50 static GQuark QUARK_PROGRAM_NUMBER;
51 static GQuark QUARK_PID;
52 static GQuark QUARK_PCR_PID;
53 static GQuark QUARK_STREAMS;
54 static GQuark QUARK_STREAM_TYPE;
56 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ")
62 #define DEFAULT_IGNORE_PCR FALSE
67 PROP_PARSE_PRIVATE_SECTIONS,
72 static void mpegts_base_dispose (GObject * object);
73 static void mpegts_base_finalize (GObject * object);
74 static void mpegts_base_set_property (GObject * object, guint prop_id,
75 const GValue * value, GParamSpec * pspec);
76 static void mpegts_base_get_property (GObject * object, guint prop_id,
77 GValue * value, GParamSpec * pspec);
79 static void mpegts_base_free_program (MpegTSBaseProgram * program);
80 static void mpegts_base_deactivate_program (MpegTSBase * base,
81 MpegTSBaseProgram * program);
82 static gboolean mpegts_base_sink_activate (GstPad * pad, GstObject * parent);
83 static gboolean mpegts_base_sink_activate_mode (GstPad * pad,
84 GstObject * parent, GstPadMode mode, gboolean active);
85 static GstFlowReturn mpegts_base_chain (GstPad * pad, GstObject * parent,
87 static gboolean mpegts_base_sink_event (GstPad * pad, GstObject * parent,
89 static gboolean mpegts_base_sink_query (GstPad * pad, GstObject * parent,
91 static gboolean mpegts_base_default_sink_query (MpegTSBase * base,
93 static GstStateChangeReturn mpegts_base_change_state (GstElement * element,
94 GstStateChange transition);
95 static gboolean mpegts_base_get_tags_from_eit (MpegTSBase * base,
96 GstMpegtsSection * section);
97 static gboolean mpegts_base_parse_atsc_mgt (MpegTSBase * base,
98 GstMpegtsSection * section);
99 static void remove_each_program (MpegTSBaseProgram * program,
105 QUARK_PROGRAMS = g_quark_from_string ("programs");
106 QUARK_PROGRAM_NUMBER = g_quark_from_string ("program-number");
107 QUARK_PID = g_quark_from_string ("pid");
108 QUARK_PCR_PID = g_quark_from_string ("pcr-pid");
109 QUARK_STREAMS = g_quark_from_string ("streams");
110 QUARK_STREAM_TYPE = g_quark_from_string ("stream-type");
111 GST_DEBUG_CATEGORY_INIT (mpegts_base_debug, "mpegtsbase", 0,
112 "MPEG transport stream base class");
113 gst_mpegts_initialize ();
116 #define mpegts_base_parent_class parent_class
117 G_DEFINE_TYPE_WITH_CODE (MpegTSBase, mpegts_base, GST_TYPE_ELEMENT,
120 /* Default implementation is that mpegtsbase can remove any program */
122 mpegts_base_can_remove_program (MpegTSBase * base, MpegTSBaseProgram * program)
128 mpegts_base_class_init (MpegTSBaseClass * klass)
130 GObjectClass *gobject_class;
131 GstElementClass *element_class;
133 klass->can_remove_program = mpegts_base_can_remove_program;
135 element_class = GST_ELEMENT_CLASS (klass);
136 element_class->change_state = mpegts_base_change_state;
138 gst_element_class_add_static_pad_template (element_class, &sink_template);
140 gobject_class = G_OBJECT_CLASS (klass);
141 gobject_class->dispose = mpegts_base_dispose;
142 gobject_class->finalize = mpegts_base_finalize;
143 gobject_class->set_property = mpegts_base_set_property;
144 gobject_class->get_property = mpegts_base_get_property;
146 g_object_class_install_property (gobject_class, PROP_PARSE_PRIVATE_SECTIONS,
147 g_param_spec_boolean ("parse-private-sections", "Parse private sections",
148 "Parse private sections", FALSE,
149 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 * GstMpegtsBase:ignore-pcr:
154 * Ignore PCR (Program Clock Reference) data from MPEG-TS PSI.
155 * This can help with playback of some broken files.
159 g_object_class_install_property (gobject_class, PROP_IGNORE_PCR,
160 g_param_spec_boolean ("ignore-pcr", "Ignore PCR stream for timing",
161 "Ignore PCR stream for timing", DEFAULT_IGNORE_PCR,
162 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164 klass->sink_query = GST_DEBUG_FUNCPTR (mpegts_base_default_sink_query);
165 klass->handle_psi = NULL;
167 gst_type_mark_as_plugin_api (GST_TYPE_MPEGTS_BASE, 0);
171 mpegts_base_set_property (GObject * object, guint prop_id,
172 const GValue * value, GParamSpec * pspec)
174 MpegTSBase *base = GST_MPEGTS_BASE (object);
177 case PROP_PARSE_PRIVATE_SECTIONS:
178 base->parse_private_sections = g_value_get_boolean (value);
180 case PROP_IGNORE_PCR:
181 base->ignore_pcr = g_value_get_boolean (value);
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 mpegts_base_get_property (GObject * object, guint prop_id,
190 GValue * value, GParamSpec * pspec)
192 MpegTSBase *base = GST_MPEGTS_BASE (object);
195 case PROP_PARSE_PRIVATE_SECTIONS:
196 g_value_set_boolean (value, base->parse_private_sections);
198 case PROP_IGNORE_PCR:
199 g_value_set_boolean (value, base->ignore_pcr);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 mpegts_base_reset (MpegTSBase * base)
210 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
212 mpegts_packetizer_clear (base->packetizer);
213 memset (base->is_pes, 0, 1024);
214 memset (base->known_psi, 0, 1024);
216 /* FIXME : Actually these are not *always* know SI streams
217 * depending on the variant of mpeg-ts being used. */
219 /* Known PIDs : PAT, TSDT, IPMP CIT */
220 MPEGTS_BIT_SET (base->known_psi, 0);
221 MPEGTS_BIT_SET (base->known_psi, 2);
222 MPEGTS_BIT_SET (base->known_psi, 3);
224 MPEGTS_BIT_SET (base->known_psi, 0x14);
225 /* network synchronization */
226 MPEGTS_BIT_SET (base->known_psi, 0x15);
229 MPEGTS_BIT_SET (base->known_psi, 0x1ffb);
232 g_ptr_array_unref (base->pat);
236 gst_segment_init (&base->segment, GST_FORMAT_UNDEFINED);
237 gst_segment_init (&base->out_segment, GST_FORMAT_UNDEFINED);
238 base->last_seek_seqnum = GST_SEQNUM_INVALID;
240 base->mode = BASE_MODE_STREAMING;
241 base->seen_pat = FALSE;
242 base->seek_offset = -1;
244 g_ptr_array_foreach (base->programs, (GFunc) remove_each_program, base);
245 g_ptr_array_remove_range (base->programs, 0, base->programs->len);
247 base->streams_aware = GST_OBJECT_PARENT (base)
248 && GST_OBJECT_FLAG_IS_SET (GST_OBJECT_PARENT (base),
249 GST_BIN_FLAG_STREAMS_AWARE);
250 GST_DEBUG_OBJECT (base, "Streams aware : %d", base->streams_aware);
252 gst_event_replace (&base->seek_event, NULL);
259 mpegts_base_init (MpegTSBase * base)
261 base->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
262 gst_pad_set_activate_function (base->sinkpad, mpegts_base_sink_activate);
263 gst_pad_set_activatemode_function (base->sinkpad,
264 mpegts_base_sink_activate_mode);
265 gst_pad_set_chain_function (base->sinkpad, mpegts_base_chain);
266 gst_pad_set_event_function (base->sinkpad, mpegts_base_sink_event);
267 gst_pad_set_query_function (base->sinkpad, mpegts_base_sink_query);
268 gst_element_add_pad (GST_ELEMENT (base), base->sinkpad);
270 base->disposed = FALSE;
271 base->packetizer = mpegts_packetizer_new ();
273 g_ptr_array_new_full (16, (GDestroyNotify) mpegts_base_free_program);
275 base->parse_private_sections = FALSE;
276 base->is_pes = g_new0 (guint8, 1024);
277 base->known_psi = g_new0 (guint8, 1024);
278 base->program_size = sizeof (MpegTSBaseProgram);
279 base->stream_size = sizeof (MpegTSBaseStream);
281 base->push_data = TRUE;
282 base->push_section = TRUE;
283 base->ignore_pcr = DEFAULT_IGNORE_PCR;
285 mpegts_base_reset (base);
289 mpegts_base_dispose (GObject * object)
291 MpegTSBase *base = GST_MPEGTS_BASE (object);
293 if (!base->disposed) {
294 g_object_unref (base->packetizer);
295 base->disposed = TRUE;
296 g_free (base->known_psi);
297 g_free (base->is_pes);
300 if (G_OBJECT_CLASS (parent_class)->dispose)
301 G_OBJECT_CLASS (parent_class)->dispose (object);
305 mpegts_base_finalize (GObject * object)
307 MpegTSBase *base = GST_MPEGTS_BASE (object);
310 g_ptr_array_unref (base->pat);
313 g_ptr_array_free (base->programs, TRUE);
315 gst_event_replace (&base->seek_event, NULL);
317 if (G_OBJECT_CLASS (parent_class)->finalize)
318 G_OBJECT_CLASS (parent_class)->finalize (object);
322 /* returns NULL if no matching descriptor found *
323 * otherwise returns a descriptor that needs to *
325 const GstMpegtsDescriptor *
326 mpegts_get_descriptor_from_stream (MpegTSBaseStream * stream, guint8 tag)
328 GstMpegtsPMTStream *pmt = stream->stream;
330 GST_DEBUG ("Searching for tag 0x%02x in stream 0x%04x (stream_type 0x%02x)",
331 tag, stream->pid, stream->stream_type);
333 return gst_mpegts_find_descriptor (pmt->descriptors, tag);
336 const GstMpegtsDescriptor *
337 mpegts_get_descriptor_from_stream_with_extension (MpegTSBaseStream * stream,
338 guint8 tag, guint8 tag_extension)
340 GstMpegtsPMTStream *pmt = stream->stream;
342 GST_DEBUG ("Searching for tag 0x%02x tag_extension 0x%02x "
343 "in stream 0x%04x (stream_type 0x%02x)",
344 tag, tag_extension, stream->pid, stream->stream_type);
346 return gst_mpegts_find_descriptor_with_extension (pmt->descriptors, tag,
357 foreach_pid_in_program (MpegTSBaseProgram * program, PIDLookup * lookup)
359 if (!program->active)
361 if (program->streams[lookup->pid])
366 mpegts_pid_in_active_programs (MpegTSBase * base, guint16 pid)
372 g_ptr_array_foreach (base->programs, (GFunc) foreach_pid_in_program, &lookup);
377 /* returns NULL if no matching descriptor found *
378 * otherwise returns a descriptor that needs to *
380 const GstMpegtsDescriptor *
381 mpegts_get_descriptor_from_program (MpegTSBaseProgram * program, guint8 tag)
383 const GstMpegtsPMT *pmt = program->pmt;
385 return gst_mpegts_find_descriptor (pmt->descriptors, tag);
389 _get_upstream_id (GstElement * element, GstPad * sinkpad)
391 gchar *upstream_id = gst_pad_get_stream_id (sinkpad);
394 /* Try to create one from the upstream URI, else use a randome number */
398 /* Try to generate one from the URI query and
399 * if it fails take a random number instead */
400 query = gst_query_new_uri ();
401 if (gst_element_query (element, query)) {
402 gst_query_parse_uri (query, &uri);
408 /* And then generate an SHA256 sum of the URI */
409 cs = g_checksum_new (G_CHECKSUM_SHA256);
410 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
412 upstream_id = g_strdup (g_checksum_get_string (cs));
413 g_checksum_free (cs);
415 /* Just get some random number if the URI query fails */
416 GST_FIXME_OBJECT (element, "Creating random stream-id, consider "
417 "implementing a deterministic way of creating a stream-id");
419 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
420 g_random_int (), g_random_int ());
423 gst_query_unref (query);
428 static MpegTSBaseProgram *
429 mpegts_base_new_program (MpegTSBase * base,
430 gint program_number, guint16 pmt_pid)
432 MpegTSBaseProgram *program;
433 gchar *upstream_id, *stream_id;
435 GST_DEBUG_OBJECT (base, "program_number : %d, pmt_pid : 0x%04x",
436 program_number, pmt_pid);
438 program = g_malloc0 (base->program_size);
439 program->program_number = program_number;
440 program->pmt_pid = pmt_pid;
441 program->pcr_pid = G_MAXUINT16;
442 program->streams = g_new0 (MpegTSBaseStream *, 0x2000);
443 program->patcount = 0;
445 upstream_id = _get_upstream_id ((GstElement *) base, base->sinkpad);
446 stream_id = g_strdup_printf ("%s:%d", upstream_id, program_number);
447 program->collection = gst_stream_collection_new (stream_id);
449 g_free (upstream_id);
455 mpegts_base_add_program (MpegTSBase * base,
456 gint program_number, guint16 pmt_pid)
458 MpegTSBaseProgram *program;
460 GST_DEBUG_OBJECT (base, "program_number : %d, pmt_pid : 0x%04x",
461 program_number, pmt_pid);
463 program = mpegts_base_new_program (base, program_number, pmt_pid);
465 /* Mark the PMT PID as being a known PSI PID */
466 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, pmt_pid))) {
467 GST_FIXME ("Refcounting. Setting twice a PID (0x%04x) as known PSI",
470 MPEGTS_BIT_SET (base->known_psi, pmt_pid);
472 /* Ensure the PMT PID was not used by some PES stream */
473 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->is_pes, pmt_pid))) {
474 GST_DEBUG ("New program PMT PID was previously used by a PES stream");
475 MPEGTS_BIT_UNSET (base->is_pes, pmt_pid);
479 g_ptr_array_add (base->programs, program);
485 mpegts_base_get_program (MpegTSBase * base, gint program_number)
489 for (i = 0; i < base->programs->len; i++) {
490 MpegTSBaseProgram *program = g_ptr_array_index (base->programs, i);
491 if (program->program_number == program_number)
497 static MpegTSBaseProgram *
498 mpegts_base_steal_program (MpegTSBase * base, gint program_number)
502 for (i = 0; i < base->programs->len; i++) {
503 MpegTSBaseProgram *program = g_ptr_array_index (base->programs, i);
504 if (program->program_number == program_number) {
505 #if GLIB_CHECK_VERSION(2, 58, 0)
506 return g_ptr_array_steal_index (base->programs, i);
508 program->recycle = TRUE;
509 g_ptr_array_remove_index (base->programs, i);
519 mpegts_base_free_stream (MpegTSBaseStream * stream)
521 if (stream->stream_object)
522 gst_object_unref (stream->stream_object);
523 if (stream->stream_id)
524 g_free (stream->stream_id);
529 mpegts_base_free_program (MpegTSBaseProgram * program)
533 if (program->recycle) {
534 program->recycle = FALSE;
539 gst_mpegts_section_unref (program->section);
543 /* FIXME FIXME FIXME FREE STREAM OBJECT ! */
544 for (tmp = program->stream_list; tmp; tmp = tmp->next)
545 mpegts_base_free_stream ((MpegTSBaseStream *) tmp->data);
547 if (program->stream_list)
548 g_list_free (program->stream_list);
550 g_free (program->streams);
553 gst_tag_list_unref (program->tags);
554 if (program->collection)
555 gst_object_unref (program->collection);
561 mpegts_base_deactivate_and_free_program (MpegTSBase * base,
562 MpegTSBaseProgram * program)
564 GST_DEBUG_OBJECT (base, "program_number : %d", program->program_number);
566 mpegts_base_deactivate_program (base, program);
567 mpegts_base_free_program (program);
571 mpegts_base_remove_program (MpegTSBase * base, MpegTSBaseProgram * program)
573 GST_DEBUG_OBJECT (base, "program_number : %d", program->program_number);
575 g_ptr_array_remove (base->programs, program);
579 get_registration_from_descriptors (GPtrArray * descriptors)
581 const GstMpegtsDescriptor *desc;
584 gst_mpegts_find_descriptor (descriptors,
585 GST_MTS_DESC_REGISTRATION))) {
586 if (G_UNLIKELY (desc->length < 4)) {
587 GST_WARNING ("Registration descriptor with length < 4. (Corrupted ?)");
589 return GST_READ_UINT32_BE (desc->data + 2);
596 find_registration_in_descriptors (GPtrArray * descriptors,
597 guint32 registration_id)
605 nb_desc = descriptors->len;
606 for (i = 0; i < nb_desc; i++) {
607 GstMpegtsDescriptor *desc = g_ptr_array_index (descriptors, i);
608 if (desc->tag == GST_MTS_DESC_REGISTRATION) {
609 guint32 reg_desc = GST_READ_UINT32_BE (desc->data + 2);
610 if (reg_desc == registration_id)
617 static MpegTSBaseStream *
618 mpegts_base_program_add_stream (MpegTSBase * base,
619 MpegTSBaseProgram * program, guint16 pid, guint8 stream_type,
620 GstMpegtsPMTStream * stream)
622 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
623 MpegTSBaseStream *bstream;
625 GST_DEBUG ("pid:0x%04x, stream_type:0x%03x", pid, stream_type);
627 /* FIXME : PID information/nature might change through time.
628 * We therefore *do* want to be able to replace an existing stream
629 * with updated information */
630 if (G_UNLIKELY (program->streams[pid])) {
631 if (stream_type != 0xff)
632 GST_WARNING ("Stream already present !");
636 bstream = g_malloc0 (base->stream_size);
638 g_strdup_printf ("%s/%08x",
639 gst_stream_collection_get_upstream_id (program->collection), pid);
641 bstream->stream_type = stream_type;
642 bstream->stream = stream;
643 /* We don't yet know the stream type, subclasses will fill that */
644 bstream->stream_object = gst_stream_new (bstream->stream_id, NULL,
645 GST_STREAM_TYPE_UNKNOWN, GST_STREAM_FLAG_NONE);
647 bstream->registration_id =
648 get_registration_from_descriptors (stream->descriptors);
649 GST_DEBUG ("PID 0x%04x, registration_id %" SAFE_FOURCC_FORMAT,
650 bstream->pid, SAFE_FOURCC_ARGS (bstream->registration_id));
653 program->streams[pid] = bstream;
654 program->stream_list = g_list_append (program->stream_list, bstream);
656 if (klass->stream_added)
657 if (klass->stream_added (base, bstream, program)) {
658 gst_stream_collection_add_stream (program->collection,
659 (GstStream *) gst_object_ref (bstream->stream_object));
660 bstream->in_collection = TRUE;
668 mpegts_base_program_remove_stream (MpegTSBase * base,
669 MpegTSBaseProgram * program, guint16 pid)
671 MpegTSBaseClass *klass;
672 MpegTSBaseStream *stream = program->streams[pid];
674 GST_DEBUG ("pid:0x%04x", pid);
676 if (G_UNLIKELY (stream == NULL)) {
677 /* Can happen if the PCR PID is the same as a audio/video PID */
678 GST_DEBUG ("Stream already removed");
682 klass = GST_MPEGTS_BASE_GET_CLASS (base);
684 /* If subclass needs it, inform it of the stream we are about to remove */
685 if (klass->stream_removed)
686 klass->stream_removed (base, stream);
688 program->stream_list = g_list_remove_all (program->stream_list, stream);
689 mpegts_base_free_stream (stream);
690 program->streams[pid] = NULL;
693 /* Check if pmtstream is already present in the program */
694 static inline gboolean
695 _stream_in_pmt (const GstMpegtsPMT * pmt, MpegTSBaseStream * stream)
697 guint i, nbstreams = pmt->streams->len;
699 for (i = 0; i < nbstreams; i++) {
700 GstMpegtsPMTStream *pmt_stream = g_ptr_array_index (pmt->streams, i);
702 if (pmt_stream->pid == stream->pid &&
703 pmt_stream->stream_type == stream->stream_type)
710 static inline gboolean
711 _pmt_stream_in_program (MpegTSBaseProgram * program,
712 GstMpegtsPMTStream * stream)
714 MpegTSBaseStream *old_stream = program->streams[stream->pid];
717 return old_stream->stream_type == stream->stream_type;
721 mpegts_base_update_program (MpegTSBase * base, MpegTSBaseProgram * program,
722 GstMpegtsSection * section, const GstMpegtsPMT * pmt)
724 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
725 const gchar *stream_id =
726 gst_stream_collection_get_upstream_id (program->collection);
727 GstStreamCollection *collection;
728 GList *tmp, *toremove;
731 /* Create new collection */
732 collection = gst_stream_collection_new (stream_id);
733 gst_object_unref (program->collection);
734 program->collection = collection;
736 /* Replace section and pmt with the new one */
737 gst_mpegts_section_unref (program->section);
738 program->section = gst_mpegts_section_ref (section);
741 /* Copy over gststream that still exist into the collection */
742 for (tmp = program->stream_list; tmp; tmp = tmp->next) {
743 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
744 if (_stream_in_pmt (pmt, stream) && stream->in_collection) {
745 gst_stream_collection_add_stream (program->collection,
746 gst_object_ref (stream->stream_object));
750 /* Add new streams (will also create and add gststream to the collection) */
751 nbstreams = pmt->streams->len;
752 for (i = 0; i < nbstreams; i++) {
753 GstMpegtsPMTStream *stream = g_ptr_array_index (pmt->streams, i);
754 if (!_pmt_stream_in_program (program, stream))
755 mpegts_base_program_add_stream (base, program, stream->pid,
756 stream->stream_type, stream);
759 /* Call subclass update */
760 if (klass->update_program)
761 klass->update_program (base, program);
763 /* Remove streams no longer present */
765 for (tmp = program->stream_list; tmp; tmp = tmp->next) {
766 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
767 if (!_stream_in_pmt (pmt, stream))
768 toremove = g_list_prepend (toremove, stream);
770 for (tmp = toremove; tmp; tmp = tmp->next) {
771 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
772 mpegts_base_program_remove_stream (base, program, stream->pid);
779 _stream_is_private_section (const GstMpegtsPMT * pmt,
780 GstMpegtsPMTStream * stream)
782 switch (stream->stream_type) {
783 case GST_MPEGTS_STREAM_TYPE_SCTE_DSMCC_DCB:
784 case GST_MPEGTS_STREAM_TYPE_SCTE_SIGNALING:
786 guint32 registration_id =
787 get_registration_from_descriptors (stream->descriptors);
788 /* Not a private section stream */
789 if (registration_id != DRF_ID_CUEI && registration_id != DRF_ID_ETV1)
792 case GST_MPEGTS_STREAM_TYPE_PRIVATE_SECTIONS:
793 case GST_MPEGTS_STREAM_TYPE_MHEG:
794 case GST_MPEGTS_STREAM_TYPE_DSM_CC:
795 case GST_MPEGTS_STREAM_TYPE_DSMCC_A:
796 case GST_MPEGTS_STREAM_TYPE_DSMCC_B:
797 case GST_MPEGTS_STREAM_TYPE_DSMCC_C:
798 case GST_MPEGTS_STREAM_TYPE_DSMCC_D:
799 case GST_MPEGTS_STREAM_TYPE_SL_FLEXMUX_SECTIONS:
800 case GST_MPEGTS_STREAM_TYPE_METADATA_SECTIONS:
801 /* known PSI streams */
803 case GST_MPEGTS_STREAM_TYPE_SCTE_SIT:
805 /* Not a private section stream */
806 if (!find_registration_in_descriptors (pmt->descriptors, DRF_ID_CUEI))
815 /* Return TRUE if programs are equal */
817 mpegts_base_is_same_program (MpegTSBase * base, MpegTSBaseProgram * oldprogram,
818 guint16 new_pmt_pid, const GstMpegtsPMT * new_pmt)
821 MpegTSBaseStream *oldstream;
822 gboolean sawpcrpid = FALSE;
824 if (oldprogram->pmt_pid != new_pmt_pid) {
825 GST_DEBUG ("Different pmt_pid (new:0x%04x, old:0x%04x)", new_pmt_pid,
826 oldprogram->pmt_pid);
830 if (!base->ignore_pcr && oldprogram->pcr_pid != new_pmt->pcr_pid) {
831 GST_DEBUG ("Different pcr_pid (new:0x%04x, old:0x%04x)",
832 new_pmt->pcr_pid, oldprogram->pcr_pid);
836 /* Check the streams */
837 nbstreams = new_pmt->streams->len;
838 for (i = 0; i < nbstreams; ++i) {
839 GstMpegtsPMTStream *stream = g_ptr_array_index (new_pmt->streams, i);
841 oldstream = oldprogram->streams[stream->pid];
843 GST_DEBUG ("New stream 0x%04x not present in old program", stream->pid);
846 if (oldstream->stream_type != stream->stream_type) {
848 ("New stream 0x%04x has a different stream type (new:%d, old:%d)",
849 stream->pid, stream->stream_type, oldstream->stream_type);
852 if (stream->pid == oldprogram->pcr_pid)
856 /* If the pcr is not shared with an existing stream, we'll have one extra stream */
860 if (nbstreams != g_list_length (oldprogram->stream_list)) {
861 GST_DEBUG ("Different number of streams (new:%d, old:%d)",
862 nbstreams, g_list_length (oldprogram->stream_list));
866 GST_DEBUG ("Programs are equal");
870 /* Return TRUE if program is an update
872 * A program is equal if:
873 * * The program number is the same (will be if it enters this function)
874 * * AND The PMT PID is equal to the old one
875 * * AND It contains at least one stream from the previous program
877 * Changes that are acceptable are therefore:
878 * * New streams appearing
879 * * Old streams going away
883 * * PMT PID being changed ?
884 * * Properties of elementary stream being changed ? (new tags ? metadata ?)
887 mpegts_base_is_program_update (MpegTSBase * base,
888 MpegTSBaseProgram * oldprogram, guint16 new_pmt_pid,
889 const GstMpegtsPMT * new_pmt)
892 MpegTSBaseStream *oldstream;
894 if (oldprogram->pmt_pid != new_pmt_pid) {
895 /* FIXME/CHECK: Can a program be updated by just changing its PID
897 GST_DEBUG ("Different pmt_pid (new:0x%04x, old:0x%04x)", new_pmt_pid,
898 oldprogram->pmt_pid);
902 /* Check if at least one stream from the previous program is still present
903 * in the new program */
905 /* Check the streams */
906 nbstreams = new_pmt->streams->len;
907 for (i = 0; i < nbstreams; ++i) {
908 GstMpegtsPMTStream *stream = g_ptr_array_index (new_pmt->streams, i);
910 oldstream = oldprogram->streams[stream->pid];
912 GST_DEBUG ("New stream 0x%04x not present in old program", stream->pid);
913 } else if (oldstream->stream_type != stream->stream_type) {
915 ("New stream 0x%04x has a different stream type (new:%d, old:%d)",
916 stream->pid, stream->stream_type, oldstream->stream_type);
917 } else if (!_stream_is_private_section (new_pmt, stream)) {
918 /* FIXME : We should actually be checking a bit deeper,
919 * especially for private streams (where the differentiation is
920 * done at the registration level) */
922 ("Stream 0x%04x is identical (stream_type %d) ! Program is an update",
923 stream->pid, stream->stream_type);
928 GST_DEBUG ("Program is not an update of the previous one");
933 mpegts_base_deactivate_program (MpegTSBase * base, MpegTSBaseProgram * program)
936 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
938 if (G_UNLIKELY (program->active == FALSE))
941 GST_DEBUG_OBJECT (base, "Deactivating PMT");
943 program->active = FALSE;
946 for (i = 0; i < program->pmt->streams->len; ++i) {
947 GstMpegtsPMTStream *stream = g_ptr_array_index (program->pmt->streams, i);
949 mpegts_base_program_remove_stream (base, program, stream->pid);
951 /* Only unset the is_pes/known_psi bit if the PID isn't used in any other active
953 if (!mpegts_pid_in_active_programs (base, stream->pid)) {
954 if (_stream_is_private_section (program->pmt, stream)) {
955 if (base->parse_private_sections)
956 MPEGTS_BIT_UNSET (base->known_psi, stream->pid);
958 MPEGTS_BIT_UNSET (base->is_pes, stream->pid);
963 /* remove pcr stream */
964 /* FIXME : This might actually be shared with another stream ? */
965 mpegts_base_program_remove_stream (base, program, program->pcr_pid);
966 if (!mpegts_pid_in_active_programs (base, program->pcr_pid))
967 MPEGTS_BIT_UNSET (base->is_pes, program->pcr_pid);
969 GST_DEBUG ("program stream_list is now %p", program->stream_list);
972 /* Inform subclasses we're deactivating this program */
973 if (klass->program_stopped)
974 klass->program_stopped (base, program);
978 mpegts_base_activate_program (MpegTSBase * base, MpegTSBaseProgram * program,
979 guint16 pmt_pid, GstMpegtsSection * section, const GstMpegtsPMT * pmt,
980 gboolean initial_program)
983 MpegTSBaseClass *klass;
985 if (G_UNLIKELY (program->active))
988 GST_DEBUG ("Activating program %d", program->program_number);
990 /* activate new pmt */
991 if (program->section)
992 gst_mpegts_section_unref (program->section);
993 program->section = gst_mpegts_section_ref (section);
996 program->pmt_pid = pmt_pid;
997 if (!base->ignore_pcr)
998 program->pcr_pid = pmt->pcr_pid;
1000 program->pcr_pid = 0x1fff;
1002 /* extract top-level registration_id if present */
1003 program->registration_id =
1004 get_registration_from_descriptors (pmt->descriptors);
1005 GST_DEBUG ("program 0x%04x, registration_id %" SAFE_FOURCC_FORMAT,
1006 program->program_number, SAFE_FOURCC_ARGS (program->registration_id));
1008 for (i = 0; i < pmt->streams->len; ++i) {
1009 GstMpegtsPMTStream *stream = g_ptr_array_index (pmt->streams, i);
1010 if (_stream_is_private_section (pmt, stream)) {
1011 if (base->parse_private_sections)
1012 MPEGTS_BIT_SET (base->known_psi, stream->pid);
1014 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->is_pes, stream->pid)))
1016 ("Refcounting issue. Setting twice a PID (0x%04x) as known PES",
1018 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, stream->pid))) {
1020 ("Refcounting issue. Setting a known PSI PID (0x%04x) as known PES",
1022 MPEGTS_BIT_UNSET (base->known_psi, stream->pid);
1024 MPEGTS_BIT_SET (base->is_pes, stream->pid);
1026 mpegts_base_program_add_stream (base, program,
1027 stream->pid, stream->stream_type, stream);
1029 /* We add the PCR pid last. If that PID is already used by one of the media
1030 * streams above, no new stream will be created */
1031 mpegts_base_program_add_stream (base, program, program->pcr_pid, -1, NULL);
1032 MPEGTS_BIT_SET (base->is_pes, program->pcr_pid);
1034 program->active = TRUE;
1035 program->initial_program = initial_program;
1037 klass = GST_MPEGTS_BASE_GET_CLASS (base);
1038 if (klass->program_started != NULL)
1039 klass->program_started (base, program);
1041 GST_DEBUG_OBJECT (base, "new pmt activated");
1046 mpegts_base_apply_pat (MpegTSBase * base, GstMpegtsSection * section)
1048 GPtrArray *pat = gst_mpegts_section_get_pat (section);
1050 MpegTSBaseProgram *program;
1053 if (G_UNLIKELY (pat == NULL))
1056 GST_INFO_OBJECT (base, "PAT");
1058 /* Applying a new PAT does two things:
1059 * * It adds the new programs to the list of programs this element handles
1060 * and increments at the same time the number of times a program is referenced.
1062 * * If there was a previously active PAT, It decrements the reference count
1063 * of all program it used. If a program is no longer needed, it is removed.
1066 old_pat = base->pat;
1069 GST_LOG ("Activating new Program Association Table");
1070 /* activate the new table */
1071 for (i = 0; i < pat->len; ++i) {
1072 GstMpegtsPatProgram *patp = g_ptr_array_index (pat, i);
1074 GST_LOG ("Looking for program %d / 0x%04x", patp->program_number,
1075 patp->network_or_program_map_PID);
1076 program = mpegts_base_get_program (base, patp->program_number);
1078 GST_LOG ("Program exists on pid 0x%04x", program->pmt_pid);
1079 /* If the new PMT PID clashes with an existing known PES stream, we know
1080 * it is not an update */
1081 if (MPEGTS_BIT_IS_SET (base->is_pes, patp->network_or_program_map_PID)) {
1082 GST_LOG ("Program is not an update");
1084 mpegts_base_add_program (base, patp->program_number,
1085 patp->network_or_program_map_PID);
1086 } else if (program->pmt_pid != patp->network_or_program_map_PID) {
1087 /* IF the program already existed, just check if the PMT PID changed */
1088 GST_LOG ("PMT is on a different PID");
1089 if (program->pmt_pid != G_MAXUINT16) {
1090 /* pmt pid changed */
1091 /* FIXME: when this happens it may still be pmt pid of another
1092 * program, so setting to False may make it go through expensive
1093 * path in is_psi unnecessarily */
1094 MPEGTS_BIT_UNSET (base->known_psi, program->pmt_pid);
1097 program->pmt_pid = patp->network_or_program_map_PID;
1098 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, program->pmt_pid)))
1100 ("Refcounting issue. Setting twice a PMT PID (0x%04x) as know PSI",
1102 MPEGTS_BIT_SET (base->known_psi, patp->network_or_program_map_PID);
1104 GST_LOG ("Regular program update");
1107 /* Create a new program */
1109 mpegts_base_add_program (base, patp->program_number,
1110 patp->network_or_program_map_PID);
1112 /* We mark this program as being referenced by one PAT */
1113 program->patcount += 1;
1117 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1118 /* deactivate the old table */
1119 GST_LOG ("Deactivating old Program Association Table");
1121 for (i = 0; i < old_pat->len; ++i) {
1122 GstMpegtsPatProgram *patp = g_ptr_array_index (old_pat, i);
1124 program = mpegts_base_get_program (base, patp->program_number);
1125 if (G_UNLIKELY (program == NULL)) {
1126 GST_DEBUG_OBJECT (base, "broken PAT, duplicated entry for program %d",
1127 patp->program_number);
1131 GST_LOG ("Deactivating program %d / 0x%04x", patp->program_number,
1132 patp->network_or_program_map_PID);
1134 if (--program->patcount > 0) {
1135 GST_LOG ("Referenced by new program, keeping");
1136 /* the program has been referenced by the new pat, keep it */
1140 GST_INFO_OBJECT (base, "PAT removing program 0x%04x 0x%04x",
1141 patp->program_number, patp->network_or_program_map_PID);
1143 if (klass->can_remove_program (base, program)) {
1144 mpegts_base_deactivate_program (base, program);
1145 mpegts_base_remove_program (base, program);
1147 /* sub-class now owns the program and must call
1148 * mpegts_base_deactivate_and_free_program later */
1149 mpegts_base_steal_program (base, patp->program_number);
1151 /* FIXME: when this happens it may still be pmt pid of another
1152 * program, so setting to False may make it go through expensive
1153 * path in is_psi unnecessarily */
1154 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi,
1155 patp->network_or_program_map_PID))) {
1157 ("Program refcounting : Setting twice a pid (0x%04x) as known PSI",
1158 patp->network_or_program_map_PID);
1160 MPEGTS_BIT_SET (base->known_psi, patp->network_or_program_map_PID);
1161 mpegts_packetizer_remove_stream (base->packetizer,
1162 patp->network_or_program_map_PID);
1165 g_ptr_array_unref (old_pat);
1172 mpegts_base_apply_pmt (MpegTSBase * base, GstMpegtsSection * section)
1174 const GstMpegtsPMT *pmt;
1175 MpegTSBaseProgram *program, *old_program;
1176 guint program_number;
1177 gboolean initial_program = TRUE;
1179 pmt = gst_mpegts_section_get_pmt (section);
1180 if (G_UNLIKELY (pmt == NULL)) {
1181 GST_ERROR ("Could not get PMT (corrupted ?)");
1185 /* FIXME : not so sure this is valid anymore */
1186 if (G_UNLIKELY (base->seen_pat == FALSE)) {
1187 GST_WARNING ("Got pmt without pat first. Returning");
1188 /* remove the stream since we won't get another PMT otherwise */
1189 mpegts_packetizer_remove_stream (base->packetizer, section->pid);
1193 program_number = section->subtable_extension;
1194 GST_DEBUG ("Applying PMT (program_number:%d, pid:0x%04x)",
1195 program_number, section->pid);
1197 /* In order for stream switching to happen properly in decodebin(2),
1198 * we need to first add the new pads (i.e. activate the new program)
1199 * before removing the old ones (i.e. deactivating the old program)
1202 old_program = mpegts_base_get_program (base, program_number);
1203 if (G_UNLIKELY (old_program == NULL))
1206 if (base->streams_aware
1207 && mpegts_base_is_program_update (base, old_program, section->pid, pmt)) {
1208 GST_FIXME ("We are streams_aware and new program is an update");
1209 /* The program is an update, and we can add/remove pads dynamically */
1210 mpegts_base_update_program (base, old_program, section, pmt);
1214 if (G_UNLIKELY (mpegts_base_is_same_program (base, old_program, section->pid,
1218 /* If the current program is active, this means we have a new program */
1219 if (old_program->active) {
1220 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1221 old_program = mpegts_base_steal_program (base, program_number);
1222 program = mpegts_base_new_program (base, program_number, section->pid);
1223 program->patcount = old_program->patcount;
1225 /* Deactivate the old program */
1226 /* FIXME : THIS IS BREAKING THE STREAM SWITCHING LOGIC !
1228 if (klass->can_remove_program (base, old_program)) {
1229 mpegts_base_deactivate_program (base, old_program);
1230 mpegts_base_free_program (old_program);
1232 /* sub-class now owns the program and must call
1233 * mpegts_base_deactivate_and_free_program later */
1234 mpegts_base_steal_program (base, old_program->program_number);
1236 /* Add new program to the programs we track */
1237 g_ptr_array_add (base->programs, program);
1238 initial_program = FALSE;
1240 GST_DEBUG ("Program update, re-using same program");
1241 program = old_program;
1244 /* activate program */
1245 /* Ownership of pmt_info is given to the program */
1246 mpegts_base_activate_program (base, program, section->pid, section, pmt,
1250 GST_DEBUG ("Done activating program");
1255 GST_ERROR ("Attempted to apply a PMT on a program that wasn't created");
1261 GST_DEBUG ("Not applying identical program");
1267 mpegts_base_handle_psi (MpegTSBase * base, GstMpegtsSection * section)
1269 gboolean post_message = TRUE;
1271 GST_DEBUG ("Handling PSI (pid: 0x%04x , table_id: 0x%02x)",
1272 section->pid, section->table_id);
1274 switch (section->section_type) {
1275 case GST_MPEGTS_SECTION_PAT:
1276 post_message = mpegts_base_apply_pat (base, section);
1277 if (base->seen_pat == FALSE) {
1278 base->seen_pat = TRUE;
1279 GST_DEBUG ("First PAT offset: %" G_GUINT64_FORMAT, section->offset);
1280 mpegts_packetizer_set_reference_offset (base->packetizer,
1284 case GST_MPEGTS_SECTION_PMT:
1285 post_message = mpegts_base_apply_pmt (base, section);
1287 case GST_MPEGTS_SECTION_EIT:
1288 /* some tag xtraction + posting */
1289 post_message = mpegts_base_get_tags_from_eit (base, section);
1291 case GST_MPEGTS_SECTION_ATSC_MGT:
1292 post_message = mpegts_base_parse_atsc_mgt (base, section);
1298 /* Give the subclass a chance to look at the section */
1299 if (GST_MPEGTS_BASE_GET_CLASS (base)->handle_psi)
1300 GST_MPEGTS_BASE_GET_CLASS (base)->handle_psi (base, section);
1302 /* Finally post message (if it wasn't corrupted) */
1304 gst_element_post_message (GST_ELEMENT_CAST (base),
1305 gst_message_new_mpegts_section (GST_OBJECT (base), section));
1306 gst_mpegts_section_unref (section);
1310 mpegts_base_parse_atsc_mgt (MpegTSBase * base, GstMpegtsSection * section)
1312 const GstMpegtsAtscMGT *mgt;
1315 mgt = gst_mpegts_section_get_atsc_mgt (section);
1316 if (G_UNLIKELY (mgt == NULL))
1319 for (i = 0; i < mgt->tables->len; ++i) {
1320 GstMpegtsAtscMGTTable *table = g_ptr_array_index (mgt->tables, i);
1322 if ((table->table_type >= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_EIT0 &&
1323 table->table_type <= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_EIT127) ||
1324 (table->table_type >= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_ETT0 &&
1325 table->table_type <= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_ETT127)) {
1326 MPEGTS_BIT_SET (base->known_psi, table->pid);
1334 mpegts_base_get_tags_from_eit (MpegTSBase * base, GstMpegtsSection * section)
1336 const GstMpegtsEIT *eit;
1338 MpegTSBaseProgram *program;
1340 /* Early exit if it's not from the present/following table_id */
1341 if (section->table_id != GST_MTS_TABLE_ID_EVENT_INFORMATION_ACTUAL_TS_PRESENT
1342 && section->table_id !=
1343 GST_MTS_TABLE_ID_EVENT_INFORMATION_OTHER_TS_PRESENT)
1346 eit = gst_mpegts_section_get_eit (section);
1347 if (G_UNLIKELY (eit == NULL))
1350 program = mpegts_base_get_program (base, section->subtable_extension);
1353 ("program_id:0x%04x, table_id:0x%02x, actual_stream:%d, present_following:%d, program:%p",
1354 section->subtable_extension, section->table_id, eit->actual_stream,
1355 eit->present_following, program);
1357 if (program && eit->present_following) {
1358 for (i = 0; i < eit->events->len; i++) {
1359 GstMpegtsEITEvent *event = g_ptr_array_index (eit->events, i);
1360 const GstMpegtsDescriptor *desc;
1362 if (event->running_status == RUNNING_STATUS_RUNNING) {
1363 program->event_id = event->event_id;
1365 gst_mpegts_find_descriptor (event->descriptors,
1366 GST_MTS_DESC_DVB_SHORT_EVENT))) {
1367 gchar *name = NULL, *text = NULL;
1369 if (gst_mpegts_descriptor_parse_dvb_short_event (desc, NULL, &name,
1372 program->tags = gst_tag_list_new_empty ();
1375 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1376 GST_TAG_TITLE, name, NULL);
1380 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1381 GST_TAG_DESCRIPTION, text, NULL);
1384 /* FIXME : Is it correct to post an event duration as a GST_TAG_DURATION ??? */
1385 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1386 GST_TAG_DURATION, event->duration * GST_SECOND, NULL);
1398 remove_each_program (MpegTSBaseProgram * program, MpegTSBase * base)
1400 /* First deactivate it */
1401 mpegts_base_deactivate_program (base, program);
1404 static inline GstFlowReturn
1405 mpegts_base_drain (MpegTSBase * base)
1407 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1409 /* Call implementation */
1411 return klass->drain (base);
1417 mpegts_base_flush (MpegTSBase * base, gboolean hard)
1419 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1421 /* Call implementation */
1423 klass->flush (base, hard);
1427 mpegts_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1429 gboolean res = TRUE;
1431 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1432 gboolean is_sticky = GST_EVENT_IS_STICKY (event);
1434 GST_DEBUG_OBJECT (base, "Got event %s",
1435 gst_event_type_get_name (GST_EVENT_TYPE (event)));
1437 switch (GST_EVENT_TYPE (event)) {
1438 case GST_EVENT_SEGMENT:
1439 gst_event_copy_segment (event, &base->segment);
1440 GST_DEBUG_OBJECT (base, "Received segment %" GST_SEGMENT_FORMAT,
1442 /* Check if we need to switch PCR/PTS handling */
1443 if (base->segment.format == GST_FORMAT_TIME) {
1444 base->packetizer->calculate_offset = FALSE;
1445 base->packetizer->calculate_skew = TRUE;
1446 /* Seek was handled upstream */
1447 base->last_seek_seqnum = gst_event_get_seqnum (event);
1449 base->packetizer->calculate_offset = TRUE;
1450 base->packetizer->calculate_skew = FALSE;
1453 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1455 case GST_EVENT_STREAM_START:
1456 gst_event_unref (event);
1458 case GST_EVENT_CAPS:
1459 /* FIXME, do something */
1460 gst_event_unref (event);
1462 case GST_EVENT_FLUSH_STOP:
1463 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1464 hard = (base->mode != BASE_MODE_SEEKING);
1465 mpegts_packetizer_flush (base->packetizer, hard);
1466 mpegts_base_flush (base, hard);
1467 gst_segment_init (&base->segment, GST_FORMAT_UNDEFINED);
1468 base->seen_pat = FALSE;
1471 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1474 /* Always return TRUE for sticky events */
1482 mpegts_base_default_sink_query (MpegTSBase * base, GstQuery * query)
1484 return gst_pad_query_default (base->sinkpad, GST_OBJECT (base), query);
1488 mpegts_base_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1490 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1492 GST_DEBUG_OBJECT (base, "Got query %s",
1493 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1495 return GST_MPEGTS_BASE_GET_CLASS (base)->sink_query (base, query);
1498 static GstFlowReturn
1499 mpegts_base_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1501 GstFlowReturn res = GST_FLOW_OK;
1503 MpegTSPacketizerPacketReturn pret;
1504 MpegTSPacketizer2 *packetizer;
1505 MpegTSPacketizerPacket packet;
1506 MpegTSBaseClass *klass;
1508 base = GST_MPEGTS_BASE (parent);
1509 klass = GST_MPEGTS_BASE_GET_CLASS (base);
1511 packetizer = base->packetizer;
1513 if (GST_BUFFER_IS_DISCONT (buf)) {
1514 GST_DEBUG_OBJECT (base, "Got DISCONT buffer, flushing");
1515 res = mpegts_base_drain (base);
1516 if (G_UNLIKELY (res != GST_FLOW_OK))
1519 mpegts_base_flush (base, FALSE);
1520 /* In the case of discontinuities in push-mode with TIME segment
1521 * we want to drop all previous observations (hard:TRUE) from
1523 if (base->mode == BASE_MODE_PUSHING
1524 && base->segment.format == GST_FORMAT_TIME) {
1525 mpegts_packetizer_flush (base->packetizer, TRUE);
1526 mpegts_packetizer_clear (base->packetizer);
1528 mpegts_packetizer_flush (base->packetizer, FALSE);
1531 mpegts_packetizer_push (base->packetizer, buf);
1533 while (res == GST_FLOW_OK) {
1534 pret = mpegts_packetizer_next_packet (base->packetizer, &packet);
1536 /* If we don't have enough data, return */
1537 if (G_UNLIKELY (pret == PACKET_NEED_MORE))
1540 if (G_UNLIKELY (pret == PACKET_BAD)) {
1541 /* bad header, skip the packet */
1542 GST_DEBUG_OBJECT (base, "bad packet, skipping");
1546 if (klass->inspect_packet)
1547 klass->inspect_packet (base, &packet);
1549 /* If it's a known PES, push it */
1550 if (MPEGTS_BIT_IS_SET (base->is_pes, packet.pid)) {
1551 /* push the packet downstream */
1552 if (base->push_data)
1553 res = klass->push (base, &packet, NULL);
1554 } else if (packet.payload
1555 && MPEGTS_BIT_IS_SET (base->known_psi, packet.pid)) {
1557 GList *others, *tmp;
1558 GstMpegtsSection *section;
1560 section = mpegts_packetizer_push_section (packetizer, &packet, &others);
1562 mpegts_base_handle_psi (base, section);
1563 if (G_UNLIKELY (others)) {
1564 for (tmp = others; tmp; tmp = tmp->next)
1565 mpegts_base_handle_psi (base, (GstMpegtsSection *) tmp->data);
1566 g_list_free (others);
1569 /* we need to push section packet downstream */
1570 if (base->push_section)
1571 res = klass->push (base, &packet, section);
1573 } else if (base->push_unknown) {
1574 res = klass->push (base, &packet, NULL);
1575 } else if (packet.payload && packet.pid != 0x1fff)
1576 GST_LOG ("PID 0x%04x Saw packet on a pid we don't handle", packet.pid);
1579 mpegts_packetizer_clear_packet (base->packetizer, &packet);
1582 if (res == GST_FLOW_OK && klass->input_done)
1583 res = klass->input_done (base);
1588 static GstFlowReturn
1589 mpegts_base_scan (MpegTSBase * base)
1591 GstFlowReturn ret = GST_FLOW_OK;
1592 GstBuffer *buf = NULL;
1594 gboolean done = FALSE;
1595 MpegTSPacketizerPacketReturn pret;
1597 gint64 upstream_size, seek_pos, reverse_limit;
1599 guint initial_pcr_seen;
1601 GST_DEBUG ("Scanning for initial sync point");
1603 /* Find initial sync point and at least 5 PCR values */
1604 for (i = 0; i < 20 && !done; i++) {
1605 GST_DEBUG ("Grabbing %d => %d", i * 65536, (i + 1) * 65536);
1607 ret = gst_pad_pull_range (base->sinkpad, i * 65536, 65536, &buf);
1608 if (G_UNLIKELY (ret == GST_FLOW_EOS))
1610 if (G_UNLIKELY (ret != GST_FLOW_OK))
1613 /* Push to packetizer */
1614 mpegts_packetizer_push (base->packetizer, buf);
1617 if (mpegts_packetizer_has_packets (base->packetizer)) {
1618 if (base->seek_offset == -1) {
1619 /* Mark the initial sync point and remember the packetsize */
1620 base->seek_offset = base->packetizer->offset;
1621 GST_DEBUG ("Sync point is now %" G_GUINT64_FORMAT, base->seek_offset);
1622 base->packetsize = base->packetizer->packet_size;
1625 /* Eat up all packets */
1626 pret = mpegts_packetizer_process_next_packet (base->packetizer);
1627 if (pret == PACKET_NEED_MORE)
1629 if (pret != PACKET_BAD && base->packetizer->nb_seen_offsets >= 5) {
1630 GST_DEBUG ("Got enough initial PCR");
1638 initial_pcr_seen = base->packetizer->nb_seen_offsets;
1639 if (G_UNLIKELY (initial_pcr_seen == 0))
1640 goto no_initial_pcr;
1641 GST_DEBUG ("Seen %d initial PCR", initial_pcr_seen);
1643 /* Now send data from the end */
1645 /* Get the size of upstream */
1646 format = GST_FORMAT_BYTES;
1647 if (!gst_pad_peer_query_duration (base->sinkpad, format, &tmpval))
1649 upstream_size = tmpval;
1651 /* The scanning takes place on the last 2048kB. Considering PCR should
1652 * be present at least every 100ms, this should cope with streams
1653 * up to 160Mbit/s */
1654 reverse_limit = MAX (0, upstream_size - 2097152);
1656 /* Find last PCR value, searching backwards by chunks of 300 MPEG-ts packets */
1657 for (seek_pos = MAX (0, upstream_size - 56400);
1658 seek_pos >= reverse_limit; seek_pos -= 56400) {
1659 mpegts_packetizer_clear (base->packetizer);
1660 GST_DEBUG ("Grabbing %" G_GUINT64_FORMAT " => %" G_GUINT64_FORMAT, seek_pos,
1663 ret = gst_pad_pull_range (base->sinkpad, seek_pos, 56400, &buf);
1664 if (G_UNLIKELY (ret == GST_FLOW_EOS))
1666 if (G_UNLIKELY (ret != GST_FLOW_OK))
1669 /* Push to packetizer */
1670 mpegts_packetizer_push (base->packetizer, buf);
1673 if (mpegts_packetizer_has_packets (base->packetizer)) {
1675 /* Eat up all packets, really try to get last PCR(s) */
1676 while (pret != PACKET_NEED_MORE)
1677 pret = mpegts_packetizer_process_next_packet (base->packetizer);
1679 if (base->packetizer->nb_seen_offsets > initial_pcr_seen) {
1680 GST_DEBUG ("Got last PCR(s) (total seen:%d)",
1681 base->packetizer->nb_seen_offsets);
1688 mpegts_packetizer_clear (base->packetizer);
1692 mpegts_packetizer_clear (base->packetizer);
1693 GST_WARNING_OBJECT (base, "Couldn't find any PCR within the first %d bytes",
1700 mpegts_base_loop (MpegTSBase * base)
1702 GstFlowReturn ret = GST_FLOW_ERROR;
1704 switch (base->mode) {
1705 case BASE_MODE_SCANNING:
1706 /* Find first sync point */
1707 ret = mpegts_base_scan (base);
1708 if (G_UNLIKELY (ret != GST_FLOW_OK))
1710 base->mode = BASE_MODE_STREAMING;
1711 GST_DEBUG ("Changing to Streaming");
1713 case BASE_MODE_SEEKING:
1714 /* FIXME : unclear if we still need mode_seeking... */
1715 base->mode = BASE_MODE_STREAMING;
1717 case BASE_MODE_STREAMING:
1719 GstBuffer *buf = NULL;
1721 GST_DEBUG ("Pulling data from %" G_GUINT64_FORMAT, base->seek_offset);
1723 if (G_UNLIKELY (base->last_seek_seqnum == GST_SEQNUM_INVALID)) {
1724 /* No configured seek, set a valid seqnum */
1725 base->last_seek_seqnum = gst_util_seqnum_next ();
1727 ret = gst_pad_pull_range (base->sinkpad, base->seek_offset,
1728 100 * base->packetsize, &buf);
1729 if (G_UNLIKELY (ret != GST_FLOW_OK))
1731 base->seek_offset += gst_buffer_get_size (buf);
1732 ret = mpegts_base_chain (base->sinkpad, GST_OBJECT_CAST (base), buf);
1733 if (G_UNLIKELY (ret != GST_FLOW_OK))
1737 case BASE_MODE_PUSHING:
1738 GST_WARNING ("wrong BASE_MODE_PUSHING mode in pull loop");
1746 GST_DEBUG_OBJECT (base, "Pausing task, reason %s", gst_flow_get_name (ret));
1747 if (ret == GST_FLOW_EOS) {
1748 if (!GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base,
1749 gst_event_new_eos ()))
1750 GST_ELEMENT_ERROR (base, STREAM, FAILED,
1751 (_("Internal data stream error.")),
1752 ("No program activated before EOS"));
1753 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1754 GST_ELEMENT_FLOW_ERROR (base, ret);
1755 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, gst_event_new_eos ());
1757 gst_pad_pause_task (base->sinkpad);
1763 mpegts_base_handle_seek_event (MpegTSBase * base, GstPad * pad,
1766 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1767 GstFlowReturn ret = GST_FLOW_ERROR;
1769 gboolean flush, instant_rate_change;
1772 GstSeekType start_type, stop_type;
1774 GstEvent *flush_event = NULL;
1776 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
1779 if (format != GST_FORMAT_TIME)
1782 if (GST_EVENT_SEQNUM (event) == base->last_seek_seqnum) {
1783 GST_DEBUG_OBJECT (base, "Skipping already handled seek");
1787 if (base->mode == BASE_MODE_PUSHING) {
1788 /* First try if upstream supports seeking in TIME format */
1789 if (gst_pad_push_event (base->sinkpad, gst_event_ref (event))) {
1790 GST_DEBUG ("upstream handled SEEK event");
1794 /* If the subclass can seek, do that */
1796 ret = klass->seek (base, event);
1797 if (G_UNLIKELY (ret != GST_FLOW_OK))
1798 GST_WARNING ("seeking failed %s", gst_flow_get_name (ret));
1802 if (GST_CLOCK_TIME_IS_VALID (base->seek_offset)) {
1803 base->mode = BASE_MODE_SEEKING;
1804 new_seek = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
1805 GST_SEEK_TYPE_SET, base->seek_offset, GST_SEEK_TYPE_NONE, -1);
1806 gst_event_set_seqnum (new_seek, GST_EVENT_SEQNUM (event));
1807 if (!gst_pad_push_event (base->sinkpad, new_seek))
1808 ret = GST_FLOW_ERROR;
1810 base->last_seek_seqnum = GST_EVENT_SEQNUM (event);
1812 base->mode = BASE_MODE_PUSHING;
1815 GST_WARNING ("subclass has no seek implementation");
1818 return ret == GST_FLOW_OK;
1822 GST_WARNING ("subclass has no seek implementation");
1827 GST_WARNING ("Negative rate not supported");
1831 GST_DEBUG ("seek event, rate: %f start: %" GST_TIME_FORMAT
1832 " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
1833 GST_TIME_ARGS (stop));
1835 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
1836 instant_rate_change = ! !(flags & GST_SEEK_FLAG_INSTANT_RATE_CHANGE);
1838 /* Directly send the instant-rate-change event here before taking the
1839 * stream-lock so that it can be applied as soon as possible */
1840 if (base->mode != BASE_MODE_PUSHING && instant_rate_change) {
1843 /* instant rate change only supported if direction does not change. All
1844 * other requirements are already checked before creating the seek event
1845 * but let's double-check here to be sure */
1846 if ((rate > 0 && base->out_segment.rate < 0) ||
1847 (rate < 0 && base->out_segment.rate > 0) ||
1848 start_type != GST_SEEK_TYPE_NONE ||
1849 stop_type != GST_SEEK_TYPE_NONE || flush) {
1850 GST_ERROR_OBJECT (base,
1851 "Instant rate change seeks only supported in the "
1852 "same direction, without flushing and position change");
1856 ev = gst_event_new_instant_rate_change (rate / base->out_segment.rate,
1857 (GstSegmentFlags) (flags));
1858 gst_event_set_seqnum (ev, GST_EVENT_SEQNUM (event));
1859 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, ev);
1863 /* stop streaming, either by flushing or by pausing the task */
1864 base->mode = BASE_MODE_SEEKING;
1866 GST_DEBUG_OBJECT (base, "sending flush start");
1867 flush_event = gst_event_new_flush_start ();
1868 gst_event_set_seqnum (flush_event, GST_EVENT_SEQNUM (event));
1869 gst_pad_push_event (base->sinkpad, gst_event_ref (flush_event));
1870 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, flush_event);
1872 gst_pad_pause_task (base->sinkpad);
1874 /* wait for streaming to finish */
1875 GST_PAD_STREAM_LOCK (base->sinkpad);
1878 /* send a FLUSH_STOP for the sinkpad, since we need data for seeking */
1879 GST_DEBUG_OBJECT (base, "sending flush stop");
1880 flush_event = gst_event_new_flush_stop (TRUE);
1881 gst_event_set_seqnum (flush_event, GST_EVENT_SEQNUM (event));
1883 /* ref for it to be reused later */
1884 gst_pad_push_event (base->sinkpad, gst_event_ref (flush_event));
1885 /* And actually flush our pending data but allow to preserve some info
1886 * to perform the seek */
1887 mpegts_base_flush (base, FALSE);
1888 mpegts_packetizer_flush (base->packetizer, FALSE);
1891 if (flags & (GST_SEEK_FLAG_SEGMENT)) {
1892 GST_WARNING ("seek flags 0x%x are not supported", (int) flags);
1897 /* If the subclass can seek, do that */
1898 ret = klass->seek (base, event);
1899 if (G_UNLIKELY (ret != GST_FLOW_OK))
1900 GST_WARNING ("seeking failed %s", gst_flow_get_name (ret));
1902 base->last_seek_seqnum = GST_EVENT_SEQNUM (event);
1905 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
1906 GST_DEBUG_OBJECT (base, "sending flush stop");
1907 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, flush_event);
1912 gst_event_unref (flush_event);
1913 gst_pad_start_task (base->sinkpad, (GstTaskFunction) mpegts_base_loop, base,
1916 GST_PAD_STREAM_UNLOCK (base->sinkpad);
1917 return ret == GST_FLOW_OK;
1922 mpegts_base_sink_activate (GstPad * sinkpad, GstObject * parent)
1927 query = gst_query_new_scheduling ();
1929 if (!gst_pad_peer_query (sinkpad, query)) {
1930 gst_query_unref (query);
1934 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
1935 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
1936 gst_query_unref (query);
1941 GST_DEBUG_OBJECT (sinkpad, "activating pull");
1942 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
1946 GST_DEBUG_OBJECT (sinkpad, "activating push");
1947 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
1952 mpegts_base_sink_activate_mode (GstPad * pad, GstObject * parent,
1953 GstPadMode mode, gboolean active)
1956 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1959 case GST_PAD_MODE_PUSH:
1960 base->mode = BASE_MODE_PUSHING;
1963 case GST_PAD_MODE_PULL:
1965 base->mode = BASE_MODE_SCANNING;
1966 /* When working pull-based, we always use offsets for estimation */
1967 base->packetizer->calculate_offset = TRUE;
1968 base->packetizer->calculate_skew = FALSE;
1969 gst_segment_init (&base->segment, GST_FORMAT_BYTES);
1971 gst_pad_start_task (pad, (GstTaskFunction) mpegts_base_loop, base,
1974 res = gst_pad_stop_task (pad);
1983 static GstStateChangeReturn
1984 mpegts_base_change_state (GstElement * element, GstStateChange transition)
1987 GstStateChangeReturn ret;
1989 base = GST_MPEGTS_BASE (element);
1991 switch (transition) {
1992 case GST_STATE_CHANGE_READY_TO_PAUSED:
1993 mpegts_base_reset (base);
1999 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2001 switch (transition) {
2002 case GST_STATE_CHANGE_PAUSED_TO_READY:
2003 mpegts_base_reset (base);
2004 if (base->mode != BASE_MODE_PUSHING)
2005 base->mode = BASE_MODE_SCANNING;
2015 gst_mpegtsbase_plugin_init (GstPlugin * plugin)
2017 GST_DEBUG_CATEGORY_INIT (mpegts_base_debug, "mpegtsbase", 0,
2018 "MPEG transport stream base class");