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 return g_ptr_array_steal_index (base->programs, i);
513 mpegts_base_free_stream (MpegTSBaseStream * stream)
515 if (stream->stream_object)
516 gst_object_unref (stream->stream_object);
517 if (stream->stream_id)
518 g_free (stream->stream_id);
523 mpegts_base_free_program (MpegTSBaseProgram * program)
527 if (program->recycle) {
528 program->recycle = FALSE;
533 gst_mpegts_section_unref (program->section);
537 /* FIXME FIXME FIXME FREE STREAM OBJECT ! */
538 for (tmp = program->stream_list; tmp; tmp = tmp->next)
539 mpegts_base_free_stream ((MpegTSBaseStream *) tmp->data);
541 if (program->stream_list)
542 g_list_free (program->stream_list);
544 g_free (program->streams);
547 gst_tag_list_unref (program->tags);
548 if (program->collection)
549 gst_object_unref (program->collection);
555 mpegts_base_deactivate_and_free_program (MpegTSBase * base,
556 MpegTSBaseProgram * program)
558 GST_DEBUG_OBJECT (base, "program_number : %d", program->program_number);
560 mpegts_base_deactivate_program (base, program);
561 mpegts_base_free_program (program);
565 mpegts_base_remove_program (MpegTSBase * base, MpegTSBaseProgram * program)
567 GST_DEBUG_OBJECT (base, "program_number : %d", program->program_number);
569 g_ptr_array_remove (base->programs, program);
573 get_registration_from_descriptors (GPtrArray * descriptors)
575 const GstMpegtsDescriptor *desc;
578 gst_mpegts_find_descriptor (descriptors,
579 GST_MTS_DESC_REGISTRATION))) {
580 if (G_UNLIKELY (desc->length < 4)) {
581 GST_WARNING ("Registration descriptor with length < 4. (Corrupted ?)");
583 return GST_READ_UINT32_BE (desc->data + 2);
590 find_registration_in_descriptors (GPtrArray * descriptors,
591 guint32 registration_id)
599 nb_desc = descriptors->len;
600 for (i = 0; i < nb_desc; i++) {
601 GstMpegtsDescriptor *desc = g_ptr_array_index (descriptors, i);
602 if (desc->tag == GST_MTS_DESC_REGISTRATION) {
603 guint32 reg_desc = GST_READ_UINT32_BE (desc->data + 2);
604 if (reg_desc == registration_id)
611 static MpegTSBaseStream *
612 mpegts_base_program_add_stream (MpegTSBase * base,
613 MpegTSBaseProgram * program, guint16 pid, guint8 stream_type,
614 GstMpegtsPMTStream * stream)
616 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
617 MpegTSBaseStream *bstream;
619 GST_DEBUG ("pid:0x%04x, stream_type:0x%03x", pid, stream_type);
621 /* FIXME : PID information/nature might change through time.
622 * We therefore *do* want to be able to replace an existing stream
623 * with updated information */
624 if (G_UNLIKELY (program->streams[pid])) {
625 if (stream_type != 0xff)
626 GST_WARNING ("Stream already present !");
630 bstream = g_malloc0 (base->stream_size);
632 g_strdup_printf ("%s/%08x",
633 gst_stream_collection_get_upstream_id (program->collection), pid);
635 bstream->stream_type = stream_type;
636 bstream->stream = stream;
637 /* We don't yet know the stream type, subclasses will fill that */
638 bstream->stream_object = gst_stream_new (bstream->stream_id, NULL,
639 GST_STREAM_TYPE_UNKNOWN, GST_STREAM_FLAG_NONE);
641 bstream->registration_id =
642 get_registration_from_descriptors (stream->descriptors);
643 GST_DEBUG ("PID 0x%04x, registration_id %" SAFE_FOURCC_FORMAT,
644 bstream->pid, SAFE_FOURCC_ARGS (bstream->registration_id));
647 program->streams[pid] = bstream;
648 program->stream_list = g_list_append (program->stream_list, bstream);
650 if (klass->stream_added)
651 if (klass->stream_added (base, bstream, program)) {
652 gst_stream_collection_add_stream (program->collection,
653 (GstStream *) gst_object_ref (bstream->stream_object));
654 bstream->in_collection = TRUE;
662 mpegts_base_program_remove_stream (MpegTSBase * base,
663 MpegTSBaseProgram * program, guint16 pid)
665 MpegTSBaseClass *klass;
666 MpegTSBaseStream *stream = program->streams[pid];
668 GST_DEBUG ("pid:0x%04x", pid);
670 if (G_UNLIKELY (stream == NULL)) {
671 /* Can happen if the PCR PID is the same as a audio/video PID */
672 GST_DEBUG ("Stream already removed");
676 klass = GST_MPEGTS_BASE_GET_CLASS (base);
678 /* If subclass needs it, inform it of the stream we are about to remove */
679 if (klass->stream_removed)
680 klass->stream_removed (base, stream);
682 program->stream_list = g_list_remove_all (program->stream_list, stream);
683 mpegts_base_free_stream (stream);
684 program->streams[pid] = NULL;
687 /* Check if pmtstream is already present in the program */
688 static inline gboolean
689 _stream_in_pmt (const GstMpegtsPMT * pmt, MpegTSBaseStream * stream)
691 guint i, nbstreams = pmt->streams->len;
693 for (i = 0; i < nbstreams; i++) {
694 GstMpegtsPMTStream *pmt_stream = g_ptr_array_index (pmt->streams, i);
696 if (pmt_stream->pid == stream->pid &&
697 pmt_stream->stream_type == stream->stream_type)
704 static inline gboolean
705 _pmt_stream_in_program (MpegTSBaseProgram * program,
706 GstMpegtsPMTStream * stream)
708 MpegTSBaseStream *old_stream = program->streams[stream->pid];
711 return old_stream->stream_type == stream->stream_type;
715 mpegts_base_update_program (MpegTSBase * base, MpegTSBaseProgram * program,
716 GstMpegtsSection * section, const GstMpegtsPMT * pmt)
718 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
719 const gchar *stream_id =
720 gst_stream_collection_get_upstream_id (program->collection);
721 GstStreamCollection *collection;
722 GList *tmp, *toremove;
725 /* Create new collection */
726 collection = gst_stream_collection_new (stream_id);
727 gst_object_unref (program->collection);
728 program->collection = collection;
730 /* Replace section and pmt with the new one */
731 gst_mpegts_section_unref (program->section);
732 program->section = gst_mpegts_section_ref (section);
735 /* Copy over gststream that still exist into the collection */
736 for (tmp = program->stream_list; tmp; tmp = tmp->next) {
737 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
738 if (_stream_in_pmt (pmt, stream) && stream->in_collection) {
739 gst_stream_collection_add_stream (program->collection,
740 gst_object_ref (stream->stream_object));
744 /* Add new streams (will also create and add gststream to the collection) */
745 nbstreams = pmt->streams->len;
746 for (i = 0; i < nbstreams; i++) {
747 GstMpegtsPMTStream *stream = g_ptr_array_index (pmt->streams, i);
748 if (!_pmt_stream_in_program (program, stream))
749 mpegts_base_program_add_stream (base, program, stream->pid,
750 stream->stream_type, stream);
753 /* Call subclass update */
754 if (klass->update_program)
755 klass->update_program (base, program);
757 /* Remove streams no longer present */
759 for (tmp = program->stream_list; tmp; tmp = tmp->next) {
760 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
761 if (!_stream_in_pmt (pmt, stream))
762 toremove = g_list_prepend (toremove, stream);
764 for (tmp = toremove; tmp; tmp = tmp->next) {
765 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
766 mpegts_base_program_remove_stream (base, program, stream->pid);
768 g_list_free (toremove);
774 _stream_is_private_section (const GstMpegtsPMT * pmt,
775 GstMpegtsPMTStream * stream)
777 switch (stream->stream_type) {
778 case GST_MPEGTS_STREAM_TYPE_SCTE_DSMCC_DCB:
779 case GST_MPEGTS_STREAM_TYPE_SCTE_SIGNALING:
781 guint32 registration_id =
782 get_registration_from_descriptors (stream->descriptors);
783 /* Not a private section stream */
784 if (registration_id != DRF_ID_CUEI && registration_id != DRF_ID_ETV1)
787 case GST_MPEGTS_STREAM_TYPE_PRIVATE_SECTIONS:
788 case GST_MPEGTS_STREAM_TYPE_MHEG:
789 case GST_MPEGTS_STREAM_TYPE_DSM_CC:
790 case GST_MPEGTS_STREAM_TYPE_DSMCC_A:
791 case GST_MPEGTS_STREAM_TYPE_DSMCC_B:
792 case GST_MPEGTS_STREAM_TYPE_DSMCC_C:
793 case GST_MPEGTS_STREAM_TYPE_DSMCC_D:
794 case GST_MPEGTS_STREAM_TYPE_SL_FLEXMUX_SECTIONS:
795 case GST_MPEGTS_STREAM_TYPE_METADATA_SECTIONS:
796 /* known PSI streams */
798 case GST_MPEGTS_STREAM_TYPE_SCTE_SIT:
800 /* Not a private section stream */
801 if (!find_registration_in_descriptors (pmt->descriptors, DRF_ID_CUEI))
810 /* Return TRUE if programs are equal */
812 mpegts_base_is_same_program (MpegTSBase * base, MpegTSBaseProgram * oldprogram,
813 guint16 new_pmt_pid, const GstMpegtsPMT * new_pmt)
816 MpegTSBaseStream *oldstream;
817 gboolean sawpcrpid = FALSE;
819 if (oldprogram->pmt_pid != new_pmt_pid) {
820 GST_DEBUG ("Different pmt_pid (new:0x%04x, old:0x%04x)", new_pmt_pid,
821 oldprogram->pmt_pid);
825 if (!base->ignore_pcr && oldprogram->pcr_pid != new_pmt->pcr_pid) {
826 GST_DEBUG ("Different pcr_pid (new:0x%04x, old:0x%04x)",
827 new_pmt->pcr_pid, oldprogram->pcr_pid);
831 /* Check the streams */
832 nbstreams = new_pmt->streams->len;
833 for (i = 0; i < nbstreams; ++i) {
834 GstMpegtsPMTStream *stream = g_ptr_array_index (new_pmt->streams, i);
836 oldstream = oldprogram->streams[stream->pid];
838 GST_DEBUG ("New stream 0x%04x not present in old program", stream->pid);
841 if (oldstream->stream_type != stream->stream_type) {
843 ("New stream 0x%04x has a different stream type (new:%d, old:%d)",
844 stream->pid, stream->stream_type, oldstream->stream_type);
847 if (stream->pid == oldprogram->pcr_pid)
851 /* If we have a PCR PID and the pcr is not shared with an existing stream, we'll have one extra stream */
852 if (!sawpcrpid && !base->ignore_pcr)
855 if (nbstreams != g_list_length (oldprogram->stream_list)) {
856 GST_DEBUG ("Different number of streams (new:%d, old:%d)",
857 nbstreams, g_list_length (oldprogram->stream_list));
861 GST_DEBUG ("Programs are equal");
865 /* Return TRUE if program is an update
867 * A program is equal if:
868 * * The program number is the same (will be if it enters this function)
869 * * AND The PMT PID is equal to the old one
870 * * AND It contains at least one stream from the previous program
872 * Changes that are acceptable are therefore:
873 * * New streams appearing
874 * * Old streams going away
878 * * PMT PID being changed ?
879 * * Properties of elementary stream being changed ? (new tags ? metadata ?)
882 mpegts_base_is_program_update (MpegTSBase * base,
883 MpegTSBaseProgram * oldprogram, guint16 new_pmt_pid,
884 const GstMpegtsPMT * new_pmt)
887 MpegTSBaseStream *oldstream;
889 if (oldprogram->pmt_pid != new_pmt_pid) {
890 /* FIXME/CHECK: Can a program be updated by just changing its PID
892 GST_DEBUG ("Different pmt_pid (new:0x%04x, old:0x%04x)", new_pmt_pid,
893 oldprogram->pmt_pid);
897 /* Check if at least one stream from the previous program is still present
898 * in the new program */
900 /* Check the streams */
901 nbstreams = new_pmt->streams->len;
902 for (i = 0; i < nbstreams; ++i) {
903 GstMpegtsPMTStream *stream = g_ptr_array_index (new_pmt->streams, i);
905 oldstream = oldprogram->streams[stream->pid];
907 GST_DEBUG ("New stream 0x%04x not present in old program", stream->pid);
908 } else if (oldstream->stream_type != stream->stream_type) {
910 ("New stream 0x%04x has a different stream type (new:%d, old:%d)",
911 stream->pid, stream->stream_type, oldstream->stream_type);
912 } else if (!_stream_is_private_section (new_pmt, stream)) {
913 /* FIXME : We should actually be checking a bit deeper,
914 * especially for private streams (where the differentiation is
915 * done at the registration level) */
917 ("Stream 0x%04x is identical (stream_type %d) ! Program is an update",
918 stream->pid, stream->stream_type);
923 GST_DEBUG ("Program is not an update of the previous one");
928 mpegts_base_deactivate_program (MpegTSBase * base, MpegTSBaseProgram * program)
931 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
933 if (G_UNLIKELY (program->active == FALSE))
936 GST_DEBUG_OBJECT (base, "Deactivating PMT");
938 program->active = FALSE;
941 for (i = 0; i < program->pmt->streams->len; ++i) {
942 GstMpegtsPMTStream *stream = g_ptr_array_index (program->pmt->streams, i);
944 mpegts_base_program_remove_stream (base, program, stream->pid);
946 /* Only unset the is_pes/known_psi bit if the PID isn't used in any other active
948 if (!mpegts_pid_in_active_programs (base, stream->pid)) {
949 if (_stream_is_private_section (program->pmt, stream)) {
950 if (base->parse_private_sections)
951 MPEGTS_BIT_UNSET (base->known_psi, stream->pid);
953 MPEGTS_BIT_UNSET (base->is_pes, stream->pid);
958 /* remove pcr stream */
959 /* FIXME : This might actually be shared with another stream ? */
960 mpegts_base_program_remove_stream (base, program, program->pcr_pid);
961 if (!mpegts_pid_in_active_programs (base, program->pcr_pid))
962 MPEGTS_BIT_UNSET (base->is_pes, program->pcr_pid);
964 GST_DEBUG ("program stream_list is now %p", program->stream_list);
967 /* Inform subclasses we're deactivating this program */
968 if (klass->program_stopped)
969 klass->program_stopped (base, program);
973 mpegts_base_activate_program (MpegTSBase * base, MpegTSBaseProgram * program,
974 guint16 pmt_pid, GstMpegtsSection * section, const GstMpegtsPMT * pmt,
975 gboolean initial_program)
978 MpegTSBaseClass *klass;
980 if (G_UNLIKELY (program->active))
983 GST_DEBUG ("Activating program %d", program->program_number);
985 /* activate new pmt */
986 if (program->section)
987 gst_mpegts_section_unref (program->section);
988 program->section = gst_mpegts_section_ref (section);
991 program->pmt_pid = pmt_pid;
992 if (!base->ignore_pcr)
993 program->pcr_pid = pmt->pcr_pid;
995 program->pcr_pid = 0x1fff;
997 /* extract top-level registration_id if present */
998 program->registration_id =
999 get_registration_from_descriptors (pmt->descriptors);
1000 GST_DEBUG ("program 0x%04x, registration_id %" SAFE_FOURCC_FORMAT,
1001 program->program_number, SAFE_FOURCC_ARGS (program->registration_id));
1003 for (i = 0; i < pmt->streams->len; ++i) {
1004 GstMpegtsPMTStream *stream = g_ptr_array_index (pmt->streams, i);
1005 if (_stream_is_private_section (pmt, stream)) {
1006 if (base->parse_private_sections)
1007 MPEGTS_BIT_SET (base->known_psi, stream->pid);
1009 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->is_pes, stream->pid)))
1011 ("Refcounting issue. Setting twice a PID (0x%04x) as known PES",
1013 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, stream->pid))) {
1015 ("Refcounting issue. Setting a known PSI PID (0x%04x) as known PES",
1017 MPEGTS_BIT_UNSET (base->known_psi, stream->pid);
1019 MPEGTS_BIT_SET (base->is_pes, stream->pid);
1021 mpegts_base_program_add_stream (base, program,
1022 stream->pid, stream->stream_type, stream);
1024 /* We add the PCR pid last. If that PID is already used by one of the media
1025 * streams above, no new stream will be created */
1026 mpegts_base_program_add_stream (base, program, program->pcr_pid, -1, NULL);
1027 MPEGTS_BIT_SET (base->is_pes, program->pcr_pid);
1029 program->active = TRUE;
1030 program->initial_program = initial_program;
1032 klass = GST_MPEGTS_BASE_GET_CLASS (base);
1033 if (klass->program_started != NULL)
1034 klass->program_started (base, program);
1036 GST_DEBUG_OBJECT (base, "new pmt activated");
1041 mpegts_base_apply_pat (MpegTSBase * base, GstMpegtsSection * section)
1043 GPtrArray *pat = gst_mpegts_section_get_pat (section);
1045 MpegTSBaseProgram *program;
1048 if (G_UNLIKELY (pat == NULL))
1051 GST_INFO_OBJECT (base, "PAT");
1053 /* Applying a new PAT does two things:
1054 * * It adds the new programs to the list of programs this element handles
1055 * and increments at the same time the number of times a program is referenced.
1057 * * If there was a previously active PAT, It decrements the reference count
1058 * of all program it used. If a program is no longer needed, it is removed.
1061 old_pat = base->pat;
1064 GST_LOG ("Activating new Program Association Table");
1065 /* activate the new table */
1066 for (i = 0; i < pat->len; ++i) {
1067 GstMpegtsPatProgram *patp = g_ptr_array_index (pat, i);
1069 GST_LOG ("Looking for program %d / 0x%04x", patp->program_number,
1070 patp->network_or_program_map_PID);
1071 program = mpegts_base_get_program (base, patp->program_number);
1073 GST_LOG ("Program exists on pid 0x%04x", program->pmt_pid);
1074 /* If the new PMT PID clashes with an existing known PES stream, we know
1075 * it is not an update */
1076 if (MPEGTS_BIT_IS_SET (base->is_pes, patp->network_or_program_map_PID)) {
1077 GST_LOG ("Program is not an update");
1079 mpegts_base_add_program (base, patp->program_number,
1080 patp->network_or_program_map_PID);
1081 } else if (program->pmt_pid != patp->network_or_program_map_PID) {
1082 /* IF the program already existed, just check if the PMT PID changed */
1083 GST_LOG ("PMT is on a different PID");
1084 if (program->pmt_pid != G_MAXUINT16) {
1085 /* pmt pid changed */
1086 /* FIXME: when this happens it may still be pmt pid of another
1087 * program, so setting to False may make it go through expensive
1088 * path in is_psi unnecessarily */
1089 MPEGTS_BIT_UNSET (base->known_psi, program->pmt_pid);
1092 program->pmt_pid = patp->network_or_program_map_PID;
1093 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, program->pmt_pid)))
1095 ("Refcounting issue. Setting twice a PMT PID (0x%04x) as know PSI",
1097 MPEGTS_BIT_SET (base->known_psi, patp->network_or_program_map_PID);
1099 GST_LOG ("Regular program update");
1102 /* Create a new program */
1104 mpegts_base_add_program (base, patp->program_number,
1105 patp->network_or_program_map_PID);
1107 /* We mark this program as being referenced by one PAT */
1108 program->patcount += 1;
1112 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1113 /* deactivate the old table */
1114 GST_LOG ("Deactivating old Program Association Table");
1116 for (i = 0; i < old_pat->len; ++i) {
1117 GstMpegtsPatProgram *patp = g_ptr_array_index (old_pat, i);
1119 program = mpegts_base_get_program (base, patp->program_number);
1120 if (G_UNLIKELY (program == NULL)) {
1121 GST_DEBUG_OBJECT (base, "broken PAT, duplicated entry for program %d",
1122 patp->program_number);
1126 GST_LOG ("Deactivating program %d / 0x%04x", patp->program_number,
1127 patp->network_or_program_map_PID);
1129 if (--program->patcount > 0) {
1130 GST_LOG ("Referenced by new program, keeping");
1131 /* the program has been referenced by the new pat, keep it */
1135 GST_INFO_OBJECT (base, "PAT removing program 0x%04x 0x%04x",
1136 patp->program_number, patp->network_or_program_map_PID);
1138 if (klass->can_remove_program (base, program)) {
1139 mpegts_base_deactivate_program (base, program);
1140 mpegts_base_remove_program (base, program);
1142 /* sub-class now owns the program and must call
1143 * mpegts_base_deactivate_and_free_program later */
1144 mpegts_base_steal_program (base, patp->program_number);
1146 /* FIXME: when this happens it may still be pmt pid of another
1147 * program, so setting to False may make it go through expensive
1148 * path in is_psi unnecessarily */
1149 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi,
1150 patp->network_or_program_map_PID))) {
1152 ("Program refcounting : Setting twice a pid (0x%04x) as known PSI",
1153 patp->network_or_program_map_PID);
1155 MPEGTS_BIT_SET (base->known_psi, patp->network_or_program_map_PID);
1156 mpegts_packetizer_remove_stream (base->packetizer,
1157 patp->network_or_program_map_PID);
1160 g_ptr_array_unref (old_pat);
1167 mpegts_base_apply_pmt (MpegTSBase * base, GstMpegtsSection * section)
1169 const GstMpegtsPMT *pmt;
1170 MpegTSBaseProgram *program, *old_program;
1171 guint program_number;
1172 gboolean initial_program = TRUE;
1174 pmt = gst_mpegts_section_get_pmt (section);
1175 if (G_UNLIKELY (pmt == NULL)) {
1176 GST_ERROR ("Could not get PMT (corrupted ?)");
1180 /* FIXME : not so sure this is valid anymore */
1181 if (G_UNLIKELY (base->seen_pat == FALSE)) {
1182 GST_WARNING ("Got pmt without pat first. Returning");
1183 /* remove the stream since we won't get another PMT otherwise */
1184 mpegts_packetizer_remove_stream (base->packetizer, section->pid);
1188 /* Don't attempt to handle pmt without any streams */
1189 if (G_UNLIKELY (pmt->streams->len == 0)) {
1190 GST_WARNING ("Skipping PMT without any entries");
1194 program_number = section->subtable_extension;
1195 GST_DEBUG ("Applying PMT (program_number:%d, pid:0x%04x)",
1196 program_number, section->pid);
1198 /* In order for stream switching to happen properly in decodebin(2),
1199 * we need to first add the new pads (i.e. activate the new program)
1200 * before removing the old ones (i.e. deactivating the old program)
1203 old_program = mpegts_base_get_program (base, program_number);
1204 if (G_UNLIKELY (old_program == NULL))
1207 if (G_UNLIKELY (mpegts_base_is_same_program (base, old_program, section->pid,
1211 if (base->streams_aware
1212 && mpegts_base_is_program_update (base, old_program, section->pid, pmt)) {
1213 GST_FIXME ("We are streams_aware and new program is an update");
1214 /* The program is an update, and we can add/remove pads dynamically */
1215 mpegts_base_update_program (base, old_program, section, pmt);
1219 /* If the current program is active, this means we have a new program */
1220 if (old_program->active) {
1221 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1222 old_program = mpegts_base_steal_program (base, program_number);
1223 program = mpegts_base_new_program (base, program_number, section->pid);
1224 program->patcount = old_program->patcount;
1226 /* Deactivate the old program */
1227 /* FIXME : THIS IS BREAKING THE STREAM SWITCHING LOGIC !
1229 if (klass->can_remove_program (base, old_program)) {
1230 mpegts_base_deactivate_program (base, old_program);
1231 mpegts_base_free_program (old_program);
1233 /* sub-class now owns the program and must call
1234 * mpegts_base_deactivate_and_free_program later */
1235 mpegts_base_steal_program (base, old_program->program_number);
1237 /* Add new program to the programs we track */
1238 g_ptr_array_add (base->programs, program);
1239 initial_program = FALSE;
1241 GST_DEBUG ("Program update, re-using same program");
1242 program = old_program;
1245 /* activate program */
1246 /* Ownership of pmt_info is given to the program */
1247 mpegts_base_activate_program (base, program, section->pid, section, pmt,
1251 GST_DEBUG ("Done activating program");
1256 GST_ERROR ("Attempted to apply a PMT on a program that wasn't created");
1262 GST_DEBUG ("Not applying identical program");
1268 mpegts_base_handle_psi (MpegTSBase * base, GstMpegtsSection * section)
1270 gboolean post_message = TRUE;
1272 GST_DEBUG ("Handling PSI (pid: 0x%04x , table_id: 0x%02x)",
1273 section->pid, section->table_id);
1275 switch (section->section_type) {
1276 case GST_MPEGTS_SECTION_PAT:
1277 post_message = mpegts_base_apply_pat (base, section);
1278 if (base->seen_pat == FALSE) {
1279 base->seen_pat = TRUE;
1280 GST_DEBUG ("First PAT offset: %" G_GUINT64_FORMAT, section->offset);
1281 mpegts_packetizer_set_reference_offset (base->packetizer,
1285 case GST_MPEGTS_SECTION_PMT:
1286 post_message = mpegts_base_apply_pmt (base, section);
1288 case GST_MPEGTS_SECTION_EIT:
1289 /* some tag xtraction + posting */
1290 post_message = mpegts_base_get_tags_from_eit (base, section);
1292 case GST_MPEGTS_SECTION_ATSC_MGT:
1293 post_message = mpegts_base_parse_atsc_mgt (base, section);
1299 /* Give the subclass a chance to look at the section */
1300 if (GST_MPEGTS_BASE_GET_CLASS (base)->handle_psi)
1301 GST_MPEGTS_BASE_GET_CLASS (base)->handle_psi (base, section);
1303 /* Finally post message (if it wasn't corrupted) */
1305 gst_element_post_message (GST_ELEMENT_CAST (base),
1306 gst_message_new_mpegts_section (GST_OBJECT (base), section));
1307 gst_mpegts_section_unref (section);
1311 mpegts_base_parse_atsc_mgt (MpegTSBase * base, GstMpegtsSection * section)
1313 const GstMpegtsAtscMGT *mgt;
1316 mgt = gst_mpegts_section_get_atsc_mgt (section);
1317 if (G_UNLIKELY (mgt == NULL))
1320 for (i = 0; i < mgt->tables->len; ++i) {
1321 GstMpegtsAtscMGTTable *table = g_ptr_array_index (mgt->tables, i);
1323 if ((table->table_type >= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_EIT0 &&
1324 table->table_type <= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_EIT127) ||
1325 (table->table_type >= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_ETT0 &&
1326 table->table_type <= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_ETT127)) {
1327 MPEGTS_BIT_SET (base->known_psi, table->pid);
1335 mpegts_base_get_tags_from_eit (MpegTSBase * base, GstMpegtsSection * section)
1337 const GstMpegtsEIT *eit;
1339 MpegTSBaseProgram *program;
1341 /* Early exit if it's not from the present/following table_id */
1342 if (section->table_id != GST_MTS_TABLE_ID_EVENT_INFORMATION_ACTUAL_TS_PRESENT
1343 && section->table_id !=
1344 GST_MTS_TABLE_ID_EVENT_INFORMATION_OTHER_TS_PRESENT)
1347 eit = gst_mpegts_section_get_eit (section);
1348 if (G_UNLIKELY (eit == NULL))
1351 program = mpegts_base_get_program (base, section->subtable_extension);
1354 ("program_id:0x%04x, table_id:0x%02x, actual_stream:%d, present_following:%d, program:%p",
1355 section->subtable_extension, section->table_id, eit->actual_stream,
1356 eit->present_following, program);
1358 if (program && eit->present_following) {
1359 for (i = 0; i < eit->events->len; i++) {
1360 GstMpegtsEITEvent *event = g_ptr_array_index (eit->events, i);
1361 const GstMpegtsDescriptor *desc;
1363 if (event->running_status == RUNNING_STATUS_RUNNING) {
1364 program->event_id = event->event_id;
1366 gst_mpegts_find_descriptor (event->descriptors,
1367 GST_MTS_DESC_DVB_SHORT_EVENT))) {
1368 gchar *name = NULL, *text = NULL;
1370 if (gst_mpegts_descriptor_parse_dvb_short_event (desc, NULL, &name,
1373 program->tags = gst_tag_list_new_empty ();
1376 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1377 GST_TAG_TITLE, name, NULL);
1381 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1382 GST_TAG_DESCRIPTION, text, NULL);
1385 /* FIXME : Is it correct to post an event duration as a GST_TAG_DURATION ??? */
1386 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1387 GST_TAG_DURATION, event->duration * GST_SECOND, NULL);
1399 remove_each_program (MpegTSBaseProgram * program, MpegTSBase * base)
1401 /* First deactivate it */
1402 mpegts_base_deactivate_program (base, program);
1405 static inline GstFlowReturn
1406 mpegts_base_drain (MpegTSBase * base)
1408 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1410 /* Call implementation */
1412 return klass->drain (base);
1418 mpegts_base_flush (MpegTSBase * base, gboolean hard)
1420 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1422 /* Call implementation */
1424 klass->flush (base, hard);
1428 mpegts_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1430 gboolean res = TRUE;
1432 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1433 gboolean is_sticky = GST_EVENT_IS_STICKY (event);
1435 GST_DEBUG_OBJECT (base, "Got event %s",
1436 gst_event_type_get_name (GST_EVENT_TYPE (event)));
1438 switch (GST_EVENT_TYPE (event)) {
1439 case GST_EVENT_SEGMENT:
1440 gst_event_copy_segment (event, &base->segment);
1441 GST_DEBUG_OBJECT (base, "Received segment %" GST_SEGMENT_FORMAT,
1443 /* Check if we need to switch PCR/PTS handling */
1444 if (base->segment.format == GST_FORMAT_TIME) {
1445 base->packetizer->calculate_offset = FALSE;
1446 base->packetizer->calculate_skew = TRUE;
1447 /* Seek was handled upstream */
1448 base->last_seek_seqnum = gst_event_get_seqnum (event);
1450 base->packetizer->calculate_offset = TRUE;
1451 base->packetizer->calculate_skew = FALSE;
1454 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1456 case GST_EVENT_STREAM_START:
1457 gst_event_unref (event);
1459 case GST_EVENT_CAPS:
1460 /* FIXME, do something */
1461 gst_event_unref (event);
1463 case GST_EVENT_FLUSH_STOP:
1464 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1465 hard = (base->mode != BASE_MODE_SEEKING);
1466 mpegts_packetizer_flush (base->packetizer, hard);
1467 mpegts_base_flush (base, hard);
1468 gst_segment_init (&base->segment, GST_FORMAT_UNDEFINED);
1469 base->seen_pat = FALSE;
1472 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1475 /* Always return TRUE for sticky events */
1483 mpegts_base_default_sink_query (MpegTSBase * base, GstQuery * query)
1485 return gst_pad_query_default (base->sinkpad, GST_OBJECT (base), query);
1489 mpegts_base_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1491 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1493 GST_DEBUG_OBJECT (base, "Got query %s",
1494 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1496 return GST_MPEGTS_BASE_GET_CLASS (base)->sink_query (base, query);
1499 static GstFlowReturn
1500 mpegts_base_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1502 GstFlowReturn res = GST_FLOW_OK;
1504 MpegTSPacketizerPacketReturn pret;
1505 MpegTSPacketizer2 *packetizer;
1506 MpegTSPacketizerPacket packet;
1507 MpegTSBaseClass *klass;
1509 base = GST_MPEGTS_BASE (parent);
1510 klass = GST_MPEGTS_BASE_GET_CLASS (base);
1512 packetizer = base->packetizer;
1514 if (GST_BUFFER_IS_DISCONT (buf)) {
1515 GST_DEBUG_OBJECT (base, "Got DISCONT buffer, flushing");
1516 res = mpegts_base_drain (base);
1517 if (G_UNLIKELY (res != GST_FLOW_OK))
1520 mpegts_base_flush (base, FALSE);
1521 if (base->mode == BASE_MODE_PUSHING) {
1522 if (base->segment.format == GST_FORMAT_TIME) {
1523 /* In the case of discontinuities in push-mode with TIME segment
1524 * we want to drop all previous observations (hard:TRUE) from
1526 mpegts_packetizer_flush (base->packetizer, TRUE);
1528 /* In all cases, we clear observations when we get a discontinuity in
1529 * push-mode to re-check if the sections (PAT/PMT) changed or not */
1530 mpegts_packetizer_clear (base->packetizer);
1532 mpegts_packetizer_flush (base->packetizer, FALSE);
1536 mpegts_packetizer_push (base->packetizer, buf);
1538 while (res == GST_FLOW_OK) {
1539 pret = mpegts_packetizer_next_packet (base->packetizer, &packet);
1541 /* If we don't have enough data, return */
1542 if (G_UNLIKELY (pret == PACKET_NEED_MORE))
1545 if (G_UNLIKELY (pret == PACKET_BAD)) {
1546 /* bad header, skip the packet */
1547 GST_DEBUG_OBJECT (base, "bad packet, skipping");
1551 if (klass->inspect_packet)
1552 klass->inspect_packet (base, &packet);
1554 /* If it's a known PES, push it */
1555 if (MPEGTS_BIT_IS_SET (base->is_pes, packet.pid)) {
1556 /* push the packet downstream */
1557 if (base->push_data)
1558 res = klass->push (base, &packet, NULL);
1559 } else if (packet.payload
1560 && MPEGTS_BIT_IS_SET (base->known_psi, packet.pid)) {
1562 GList *others, *tmp;
1563 GstMpegtsSection *section;
1565 section = mpegts_packetizer_push_section (packetizer, &packet, &others);
1567 mpegts_base_handle_psi (base, section);
1568 if (G_UNLIKELY (others)) {
1569 for (tmp = others; tmp; tmp = tmp->next)
1570 mpegts_base_handle_psi (base, (GstMpegtsSection *) tmp->data);
1571 g_list_free (others);
1574 /* we need to push section packet downstream */
1575 if (base->push_section)
1576 res = klass->push (base, &packet, section);
1578 } else if (base->push_unknown) {
1579 res = klass->push (base, &packet, NULL);
1580 } else if (packet.payload && packet.pid != 0x1fff)
1581 GST_LOG ("PID 0x%04x Saw packet on a pid we don't handle", packet.pid);
1584 mpegts_packetizer_clear_packet (base->packetizer, &packet);
1587 if (res == GST_FLOW_OK && klass->input_done)
1588 res = klass->input_done (base);
1593 static GstFlowReturn
1594 mpegts_base_scan (MpegTSBase * base)
1596 GstFlowReturn ret = GST_FLOW_OK;
1597 GstBuffer *buf = NULL;
1599 gboolean done = FALSE;
1600 MpegTSPacketizerPacketReturn pret;
1602 gint64 upstream_size, seek_pos, reverse_limit;
1604 guint initial_pcr_seen;
1606 GST_DEBUG ("Scanning for initial sync point");
1608 /* Find initial sync point and at least 5 PCR values */
1609 for (i = 0; i < 20 && !done; i++) {
1610 GST_DEBUG ("Grabbing %d => %d", i * 65536, (i + 1) * 65536);
1612 ret = gst_pad_pull_range (base->sinkpad, i * 65536, 65536, &buf);
1613 if (G_UNLIKELY (ret == GST_FLOW_EOS))
1615 if (G_UNLIKELY (ret != GST_FLOW_OK))
1618 /* Push to packetizer */
1619 mpegts_packetizer_push (base->packetizer, buf);
1622 if (mpegts_packetizer_has_packets (base->packetizer)) {
1623 if (base->seek_offset == -1) {
1624 /* Mark the initial sync point and remember the packetsize */
1625 base->seek_offset = base->packetizer->offset;
1626 GST_DEBUG ("Sync point is now %" G_GUINT64_FORMAT, base->seek_offset);
1627 base->packetsize = base->packetizer->packet_size;
1630 /* Eat up all packets */
1631 pret = mpegts_packetizer_process_next_packet (base->packetizer);
1632 if (pret == PACKET_NEED_MORE)
1634 if (pret != PACKET_BAD && base->packetizer->nb_seen_offsets >= 5) {
1635 GST_DEBUG ("Got enough initial PCR");
1643 initial_pcr_seen = base->packetizer->nb_seen_offsets;
1644 if (G_UNLIKELY (initial_pcr_seen == 0))
1645 goto no_initial_pcr;
1646 GST_DEBUG ("Seen %d initial PCR", initial_pcr_seen);
1648 /* Now send data from the end */
1650 /* Get the size of upstream */
1651 format = GST_FORMAT_BYTES;
1652 if (!gst_pad_peer_query_duration (base->sinkpad, format, &tmpval))
1654 upstream_size = tmpval;
1656 /* The scanning takes place on the last 2048kB. Considering PCR should
1657 * be present at least every 100ms, this should cope with streams
1658 * up to 160Mbit/s */
1659 reverse_limit = MAX (0, upstream_size - 2097152);
1661 /* Find last PCR value, searching backwards by chunks of 300 MPEG-ts packets */
1662 for (seek_pos = MAX (0, upstream_size - 56400);
1663 seek_pos >= reverse_limit; seek_pos -= 56400) {
1664 mpegts_packetizer_clear (base->packetizer);
1665 GST_DEBUG ("Grabbing %" G_GUINT64_FORMAT " => %" G_GUINT64_FORMAT, seek_pos,
1668 ret = gst_pad_pull_range (base->sinkpad, seek_pos, 56400, &buf);
1669 if (G_UNLIKELY (ret == GST_FLOW_EOS))
1671 if (G_UNLIKELY (ret != GST_FLOW_OK))
1674 /* Push to packetizer */
1675 mpegts_packetizer_push (base->packetizer, buf);
1678 if (mpegts_packetizer_has_packets (base->packetizer)) {
1680 /* Eat up all packets, really try to get last PCR(s) */
1681 while (pret != PACKET_NEED_MORE)
1682 pret = mpegts_packetizer_process_next_packet (base->packetizer);
1684 if (base->packetizer->nb_seen_offsets > initial_pcr_seen) {
1685 GST_DEBUG ("Got last PCR(s) (total seen:%d)",
1686 base->packetizer->nb_seen_offsets);
1693 mpegts_packetizer_clear (base->packetizer);
1697 mpegts_packetizer_clear (base->packetizer);
1698 GST_WARNING_OBJECT (base, "Couldn't find any PCR within the first %d bytes",
1705 mpegts_base_loop (MpegTSBase * base)
1707 GstFlowReturn ret = GST_FLOW_ERROR;
1709 switch (base->mode) {
1710 case BASE_MODE_SCANNING:
1711 /* Find first sync point */
1712 ret = mpegts_base_scan (base);
1713 if (G_UNLIKELY (ret != GST_FLOW_OK))
1715 base->mode = BASE_MODE_STREAMING;
1716 GST_DEBUG ("Changing to Streaming");
1718 case BASE_MODE_SEEKING:
1719 /* FIXME : unclear if we still need mode_seeking... */
1720 base->mode = BASE_MODE_STREAMING;
1722 case BASE_MODE_STREAMING:
1724 GstBuffer *buf = NULL;
1726 GST_DEBUG ("Pulling data from %" G_GUINT64_FORMAT, base->seek_offset);
1728 if (G_UNLIKELY (base->last_seek_seqnum == GST_SEQNUM_INVALID)) {
1729 /* No configured seek, set a valid seqnum */
1730 base->last_seek_seqnum = gst_util_seqnum_next ();
1732 ret = gst_pad_pull_range (base->sinkpad, base->seek_offset,
1733 100 * base->packetsize, &buf);
1734 if (G_UNLIKELY (ret != GST_FLOW_OK))
1736 base->seek_offset += gst_buffer_get_size (buf);
1737 ret = mpegts_base_chain (base->sinkpad, GST_OBJECT_CAST (base), buf);
1738 if (G_UNLIKELY (ret != GST_FLOW_OK))
1742 case BASE_MODE_PUSHING:
1743 GST_WARNING ("wrong BASE_MODE_PUSHING mode in pull loop");
1751 GST_DEBUG_OBJECT (base, "Pausing task, reason %s", gst_flow_get_name (ret));
1752 if (ret == GST_FLOW_EOS) {
1753 if (!GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base,
1754 gst_event_new_eos ()))
1755 GST_ELEMENT_ERROR (base, STREAM, FAILED,
1756 (_("Internal data stream error.")),
1757 ("No program activated before EOS"));
1758 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1759 GST_ELEMENT_FLOW_ERROR (base, ret);
1760 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, gst_event_new_eos ());
1762 gst_pad_pause_task (base->sinkpad);
1768 mpegts_base_handle_seek_event (MpegTSBase * base, GstPad * pad,
1771 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1772 GstFlowReturn ret = GST_FLOW_ERROR;
1774 gboolean flush, instant_rate_change;
1777 GstSeekType start_type, stop_type;
1779 GstEvent *flush_event = NULL;
1781 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
1784 if (format != GST_FORMAT_TIME)
1787 if (GST_EVENT_SEQNUM (event) == base->last_seek_seqnum) {
1788 GST_DEBUG_OBJECT (base, "Skipping already handled seek");
1792 if (base->mode == BASE_MODE_PUSHING) {
1793 /* First try if upstream supports seeking in TIME format */
1794 if (gst_pad_push_event (base->sinkpad, gst_event_ref (event))) {
1795 GST_DEBUG ("upstream handled SEEK event");
1799 /* If the subclass can seek, do that */
1801 ret = klass->seek (base, event);
1802 if (G_UNLIKELY (ret != GST_FLOW_OK))
1803 GST_WARNING ("seeking failed %s", gst_flow_get_name (ret));
1807 if (GST_CLOCK_TIME_IS_VALID (base->seek_offset)) {
1808 base->mode = BASE_MODE_SEEKING;
1809 new_seek = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
1810 GST_SEEK_TYPE_SET, base->seek_offset, GST_SEEK_TYPE_NONE, -1);
1811 gst_event_set_seqnum (new_seek, GST_EVENT_SEQNUM (event));
1812 if (!gst_pad_push_event (base->sinkpad, new_seek))
1813 ret = GST_FLOW_ERROR;
1815 base->last_seek_seqnum = GST_EVENT_SEQNUM (event);
1817 base->mode = BASE_MODE_PUSHING;
1820 GST_WARNING ("subclass has no seek implementation");
1823 return ret == GST_FLOW_OK;
1827 GST_WARNING ("subclass has no seek implementation");
1832 GST_WARNING ("Negative rate not supported");
1836 GST_DEBUG ("seek event, rate: %f start: %" GST_TIME_FORMAT
1837 " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
1838 GST_TIME_ARGS (stop));
1840 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
1841 instant_rate_change = ! !(flags & GST_SEEK_FLAG_INSTANT_RATE_CHANGE);
1843 /* Directly send the instant-rate-change event here before taking the
1844 * stream-lock so that it can be applied as soon as possible */
1845 if (base->mode != BASE_MODE_PUSHING && instant_rate_change) {
1848 /* instant rate change only supported if direction does not change. All
1849 * other requirements are already checked before creating the seek event
1850 * but let's double-check here to be sure */
1851 if ((rate > 0 && base->out_segment.rate < 0) ||
1852 (rate < 0 && base->out_segment.rate > 0) ||
1853 start_type != GST_SEEK_TYPE_NONE ||
1854 stop_type != GST_SEEK_TYPE_NONE || flush) {
1855 GST_ERROR_OBJECT (base,
1856 "Instant rate change seeks only supported in the "
1857 "same direction, without flushing and position change");
1861 ev = gst_event_new_instant_rate_change (rate / base->out_segment.rate,
1862 (GstSegmentFlags) (flags));
1863 gst_event_set_seqnum (ev, GST_EVENT_SEQNUM (event));
1864 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, ev);
1868 /* stop streaming, either by flushing or by pausing the task */
1869 base->mode = BASE_MODE_SEEKING;
1871 GST_DEBUG_OBJECT (base, "sending flush start");
1872 flush_event = gst_event_new_flush_start ();
1873 gst_event_set_seqnum (flush_event, GST_EVENT_SEQNUM (event));
1874 gst_pad_push_event (base->sinkpad, gst_event_ref (flush_event));
1875 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, flush_event);
1877 gst_pad_pause_task (base->sinkpad);
1879 /* wait for streaming to finish */
1880 GST_PAD_STREAM_LOCK (base->sinkpad);
1883 /* send a FLUSH_STOP for the sinkpad, since we need data for seeking */
1884 GST_DEBUG_OBJECT (base, "sending flush stop");
1885 flush_event = gst_event_new_flush_stop (TRUE);
1886 gst_event_set_seqnum (flush_event, GST_EVENT_SEQNUM (event));
1888 /* ref for it to be reused later */
1889 gst_pad_push_event (base->sinkpad, gst_event_ref (flush_event));
1890 /* And actually flush our pending data but allow to preserve some info
1891 * to perform the seek */
1892 mpegts_base_flush (base, FALSE);
1893 mpegts_packetizer_flush (base->packetizer, FALSE);
1896 if (flags & (GST_SEEK_FLAG_SEGMENT)) {
1897 GST_WARNING ("seek flags 0x%x are not supported", (int) flags);
1902 /* If the subclass can seek, do that */
1903 ret = klass->seek (base, event);
1904 if (G_UNLIKELY (ret != GST_FLOW_OK))
1905 GST_WARNING ("seeking failed %s", gst_flow_get_name (ret));
1907 base->last_seek_seqnum = GST_EVENT_SEQNUM (event);
1910 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
1911 GST_DEBUG_OBJECT (base, "sending flush stop");
1912 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, flush_event);
1917 gst_event_unref (flush_event);
1918 gst_pad_start_task (base->sinkpad, (GstTaskFunction) mpegts_base_loop, base,
1921 GST_PAD_STREAM_UNLOCK (base->sinkpad);
1922 return ret == GST_FLOW_OK;
1927 mpegts_base_sink_activate (GstPad * sinkpad, GstObject * parent)
1932 query = gst_query_new_scheduling ();
1934 if (!gst_pad_peer_query (sinkpad, query)) {
1935 gst_query_unref (query);
1939 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
1940 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
1941 gst_query_unref (query);
1946 GST_DEBUG_OBJECT (sinkpad, "activating pull");
1947 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
1951 GST_DEBUG_OBJECT (sinkpad, "activating push");
1952 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
1957 mpegts_base_sink_activate_mode (GstPad * pad, GstObject * parent,
1958 GstPadMode mode, gboolean active)
1961 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1964 case GST_PAD_MODE_PUSH:
1965 base->mode = BASE_MODE_PUSHING;
1968 case GST_PAD_MODE_PULL:
1970 base->mode = BASE_MODE_SCANNING;
1971 /* When working pull-based, we always use offsets for estimation */
1972 base->packetizer->calculate_offset = TRUE;
1973 base->packetizer->calculate_skew = FALSE;
1974 gst_segment_init (&base->segment, GST_FORMAT_BYTES);
1976 gst_pad_start_task (pad, (GstTaskFunction) mpegts_base_loop, base,
1979 res = gst_pad_stop_task (pad);
1988 static GstStateChangeReturn
1989 mpegts_base_change_state (GstElement * element, GstStateChange transition)
1992 GstStateChangeReturn ret;
1994 base = GST_MPEGTS_BASE (element);
1996 switch (transition) {
1997 case GST_STATE_CHANGE_READY_TO_PAUSED:
1998 mpegts_base_reset (base);
2004 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2006 switch (transition) {
2007 case GST_STATE_CHANGE_PAUSED_TO_READY:
2008 mpegts_base_reset (base);
2009 if (base->mode != BASE_MODE_PUSHING)
2010 base->mode = BASE_MODE_SCANNING;
2020 gst_mpegtsbase_plugin_init (GstPlugin * plugin)
2022 GST_DEBUG_CATEGORY_INIT (mpegts_base_debug, "mpegtsbase", 0,
2023 "MPEG transport stream base class");