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 <gst/gst-i18n-plugin.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 gboolean remove_each_program (gpointer key, 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);
166 gst_type_mark_as_plugin_api (GST_TYPE_MPEGTS_BASE, 0);
170 mpegts_base_set_property (GObject * object, guint prop_id,
171 const GValue * value, GParamSpec * pspec)
173 MpegTSBase *base = GST_MPEGTS_BASE (object);
176 case PROP_PARSE_PRIVATE_SECTIONS:
177 base->parse_private_sections = g_value_get_boolean (value);
179 case PROP_IGNORE_PCR:
180 base->ignore_pcr = g_value_get_boolean (value);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 mpegts_base_get_property (GObject * object, guint prop_id,
189 GValue * value, GParamSpec * pspec)
191 MpegTSBase *base = GST_MPEGTS_BASE (object);
194 case PROP_PARSE_PRIVATE_SECTIONS:
195 g_value_set_boolean (value, base->parse_private_sections);
197 case PROP_IGNORE_PCR:
198 g_value_set_boolean (value, base->ignore_pcr);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 mpegts_base_reset (MpegTSBase * base)
209 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
211 mpegts_packetizer_clear (base->packetizer);
212 memset (base->is_pes, 0, 1024);
213 memset (base->known_psi, 0, 1024);
215 /* FIXME : Actually these are not *always* know SI streams
216 * depending on the variant of mpeg-ts being used. */
218 /* Known PIDs : PAT, TSDT, IPMP CIT */
219 MPEGTS_BIT_SET (base->known_psi, 0);
220 MPEGTS_BIT_SET (base->known_psi, 2);
221 MPEGTS_BIT_SET (base->known_psi, 3);
223 MPEGTS_BIT_SET (base->known_psi, 0x14);
224 /* network synchronization */
225 MPEGTS_BIT_SET (base->known_psi, 0x15);
228 MPEGTS_BIT_SET (base->known_psi, 0x1ffb);
231 g_ptr_array_unref (base->pat);
235 gst_segment_init (&base->segment, GST_FORMAT_UNDEFINED);
236 gst_segment_init (&base->out_segment, GST_FORMAT_UNDEFINED);
237 base->last_seek_seqnum = GST_SEQNUM_INVALID;
239 base->mode = BASE_MODE_STREAMING;
240 base->seen_pat = FALSE;
241 base->seek_offset = -1;
243 g_hash_table_foreach_remove (base->programs, (GHRFunc) remove_each_program,
246 base->streams_aware = GST_OBJECT_PARENT (base)
247 && GST_OBJECT_FLAG_IS_SET (GST_OBJECT_PARENT (base),
248 GST_BIN_FLAG_STREAMS_AWARE);
249 GST_DEBUG_OBJECT (base, "Streams aware : %d", base->streams_aware);
256 mpegts_base_init (MpegTSBase * base)
258 base->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
259 gst_pad_set_activate_function (base->sinkpad, mpegts_base_sink_activate);
260 gst_pad_set_activatemode_function (base->sinkpad,
261 mpegts_base_sink_activate_mode);
262 gst_pad_set_chain_function (base->sinkpad, mpegts_base_chain);
263 gst_pad_set_event_function (base->sinkpad, mpegts_base_sink_event);
264 gst_pad_set_query_function (base->sinkpad, mpegts_base_sink_query);
265 gst_element_add_pad (GST_ELEMENT (base), base->sinkpad);
267 base->disposed = FALSE;
268 base->packetizer = mpegts_packetizer_new ();
269 base->programs = g_hash_table_new_full (g_direct_hash, g_direct_equal,
270 NULL, (GDestroyNotify) mpegts_base_free_program);
272 base->parse_private_sections = FALSE;
273 base->is_pes = g_new0 (guint8, 1024);
274 base->known_psi = g_new0 (guint8, 1024);
275 base->program_size = sizeof (MpegTSBaseProgram);
276 base->stream_size = sizeof (MpegTSBaseStream);
278 base->push_data = TRUE;
279 base->push_section = TRUE;
280 base->ignore_pcr = DEFAULT_IGNORE_PCR;
282 mpegts_base_reset (base);
286 mpegts_base_dispose (GObject * object)
288 MpegTSBase *base = GST_MPEGTS_BASE (object);
290 if (!base->disposed) {
291 g_object_unref (base->packetizer);
292 base->disposed = TRUE;
293 g_free (base->known_psi);
294 g_free (base->is_pes);
297 if (G_OBJECT_CLASS (parent_class)->dispose)
298 G_OBJECT_CLASS (parent_class)->dispose (object);
302 mpegts_base_finalize (GObject * object)
304 MpegTSBase *base = GST_MPEGTS_BASE (object);
307 g_ptr_array_unref (base->pat);
310 g_hash_table_destroy (base->programs);
312 if (G_OBJECT_CLASS (parent_class)->finalize)
313 G_OBJECT_CLASS (parent_class)->finalize (object);
317 /* returns NULL if no matching descriptor found *
318 * otherwise returns a descriptor that needs to *
320 const GstMpegtsDescriptor *
321 mpegts_get_descriptor_from_stream (MpegTSBaseStream * stream, guint8 tag)
323 GstMpegtsPMTStream *pmt = stream->stream;
325 GST_DEBUG ("Searching for tag 0x%02x in stream 0x%04x (stream_type 0x%02x)",
326 tag, stream->pid, stream->stream_type);
328 return gst_mpegts_find_descriptor (pmt->descriptors, tag);
331 const GstMpegtsDescriptor *
332 mpegts_get_descriptor_from_stream_with_extension (MpegTSBaseStream * stream,
333 guint8 tag, guint8 tag_extension)
335 GstMpegtsPMTStream *pmt = stream->stream;
337 GST_DEBUG ("Searching for tag 0x%02x tag_extension 0x%02x "
338 "in stream 0x%04x (stream_type 0x%02x)",
339 tag, tag_extension, stream->pid, stream->stream_type);
341 return gst_mpegts_find_descriptor_with_extension (pmt->descriptors, tag,
352 foreach_pid_in_program (gpointer key, MpegTSBaseProgram * program,
355 if (!program->active)
357 if (program->streams[lookup->pid])
362 mpegts_pid_in_active_programs (MpegTSBase * base, guint16 pid)
368 g_hash_table_foreach (base->programs, (GHFunc) foreach_pid_in_program,
374 /* returns NULL if no matching descriptor found *
375 * otherwise returns a descriptor that needs to *
377 const GstMpegtsDescriptor *
378 mpegts_get_descriptor_from_program (MpegTSBaseProgram * program, guint8 tag)
380 const GstMpegtsPMT *pmt = program->pmt;
382 return gst_mpegts_find_descriptor (pmt->descriptors, tag);
386 _get_upstream_id (GstElement * element, GstPad * sinkpad)
388 gchar *upstream_id = gst_pad_get_stream_id (sinkpad);
391 /* Try to create one from the upstream URI, else use a randome number */
395 /* Try to generate one from the URI query and
396 * if it fails take a random number instead */
397 query = gst_query_new_uri ();
398 if (gst_element_query (element, query)) {
399 gst_query_parse_uri (query, &uri);
405 /* And then generate an SHA256 sum of the URI */
406 cs = g_checksum_new (G_CHECKSUM_SHA256);
407 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
409 upstream_id = g_strdup (g_checksum_get_string (cs));
410 g_checksum_free (cs);
412 /* Just get some random number if the URI query fails */
413 GST_FIXME_OBJECT (element, "Creating random stream-id, consider "
414 "implementing a deterministic way of creating a stream-id");
416 g_strdup_printf ("%08x%08x%08x%08x", g_random_int (), g_random_int (),
417 g_random_int (), g_random_int ());
420 gst_query_unref (query);
425 static MpegTSBaseProgram *
426 mpegts_base_new_program (MpegTSBase * base,
427 gint program_number, guint16 pmt_pid)
429 MpegTSBaseProgram *program;
430 gchar *upstream_id, *stream_id;
432 GST_DEBUG_OBJECT (base, "program_number : %d, pmt_pid : %d",
433 program_number, pmt_pid);
435 program = g_malloc0 (base->program_size);
436 program->program_number = program_number;
437 program->pmt_pid = pmt_pid;
438 program->pcr_pid = G_MAXUINT16;
439 program->streams = g_new0 (MpegTSBaseStream *, 0x2000);
440 program->patcount = 0;
442 upstream_id = _get_upstream_id ((GstElement *) base, base->sinkpad);
443 stream_id = g_strdup_printf ("%s:%d", upstream_id, program_number);
444 program->collection = gst_stream_collection_new (stream_id);
446 g_free (upstream_id);
452 mpegts_base_add_program (MpegTSBase * base,
453 gint program_number, guint16 pmt_pid)
455 MpegTSBaseProgram *program;
457 GST_DEBUG_OBJECT (base, "program_number : %d, pmt_pid : %d",
458 program_number, pmt_pid);
460 program = mpegts_base_new_program (base, program_number, pmt_pid);
462 /* Mark the PMT PID as being a known PSI PID */
463 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, pmt_pid))) {
464 GST_FIXME ("Refcounting. Setting twice a PID (0x%04x) as known PSI",
467 MPEGTS_BIT_SET (base->known_psi, pmt_pid);
469 g_hash_table_insert (base->programs,
470 GINT_TO_POINTER (program_number), program);
476 mpegts_base_get_program (MpegTSBase * base, gint program_number)
478 MpegTSBaseProgram *program;
480 program = (MpegTSBaseProgram *) g_hash_table_lookup (base->programs,
481 GINT_TO_POINTER ((gint) program_number));
486 static MpegTSBaseProgram *
487 mpegts_base_steal_program (MpegTSBase * base, gint program_number)
489 MpegTSBaseProgram *program;
491 program = (MpegTSBaseProgram *) g_hash_table_lookup (base->programs,
492 GINT_TO_POINTER ((gint) program_number));
495 g_hash_table_steal (base->programs,
496 GINT_TO_POINTER ((gint) program_number));
502 mpegts_base_free_stream (MpegTSBaseStream * stream)
504 if (stream->stream_object)
505 gst_object_unref (stream->stream_object);
506 if (stream->stream_id)
507 g_free (stream->stream_id);
512 mpegts_base_free_program (MpegTSBaseProgram * program)
517 gst_mpegts_section_unref (program->section);
521 /* FIXME FIXME FIXME FREE STREAM OBJECT ! */
522 for (tmp = program->stream_list; tmp; tmp = tmp->next)
523 mpegts_base_free_stream ((MpegTSBaseStream *) tmp->data);
525 if (program->stream_list)
526 g_list_free (program->stream_list);
528 g_free (program->streams);
531 gst_tag_list_unref (program->tags);
532 if (program->collection)
533 gst_object_unref (program->collection);
539 mpegts_base_deactivate_and_free_program (MpegTSBase * base,
540 MpegTSBaseProgram * program)
542 GST_DEBUG_OBJECT (base, "program_number : %d", program->program_number);
544 mpegts_base_deactivate_program (base, program);
545 mpegts_base_free_program (program);
549 mpegts_base_remove_program (MpegTSBase * base, gint program_number)
551 GST_DEBUG_OBJECT (base, "program_number : %d", program_number);
553 g_hash_table_remove (base->programs, GINT_TO_POINTER (program_number));
557 get_registration_from_descriptors (GPtrArray * descriptors)
559 const GstMpegtsDescriptor *desc;
562 gst_mpegts_find_descriptor (descriptors,
563 GST_MTS_DESC_REGISTRATION))) {
564 if (G_UNLIKELY (desc->length < 4)) {
565 GST_WARNING ("Registration descriptor with length < 4. (Corrupted ?)");
567 return GST_READ_UINT32_BE (desc->data + 2);
573 static MpegTSBaseStream *
574 mpegts_base_program_add_stream (MpegTSBase * base,
575 MpegTSBaseProgram * program, guint16 pid, guint8 stream_type,
576 GstMpegtsPMTStream * stream)
578 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
579 MpegTSBaseStream *bstream;
581 GST_DEBUG ("pid:0x%04x, stream_type:0x%03x", pid, stream_type);
583 /* FIXME : PID information/nature might change through time.
584 * We therefore *do* want to be able to replace an existing stream
585 * with updated information */
586 if (G_UNLIKELY (program->streams[pid])) {
587 if (stream_type != 0xff)
588 GST_WARNING ("Stream already present !");
592 bstream = g_malloc0 (base->stream_size);
594 g_strdup_printf ("%s/%08x",
595 gst_stream_collection_get_upstream_id (program->collection), pid);
597 bstream->stream_type = stream_type;
598 bstream->stream = stream;
599 /* We don't yet know the stream type, subclasses will fill that */
600 bstream->stream_object = gst_stream_new (bstream->stream_id, NULL,
601 GST_STREAM_TYPE_UNKNOWN, GST_STREAM_FLAG_NONE);
603 bstream->registration_id =
604 get_registration_from_descriptors (stream->descriptors);
605 GST_DEBUG ("PID 0x%04x, registration_id %" SAFE_FOURCC_FORMAT,
606 bstream->pid, SAFE_FOURCC_ARGS (bstream->registration_id));
609 program->streams[pid] = bstream;
610 program->stream_list = g_list_append (program->stream_list, bstream);
612 if (klass->stream_added)
613 if (klass->stream_added (base, bstream, program)) {
614 gst_stream_collection_add_stream (program->collection,
615 (GstStream *) gst_object_ref (bstream->stream_object));
616 bstream->in_collection = TRUE;
624 mpegts_base_program_remove_stream (MpegTSBase * base,
625 MpegTSBaseProgram * program, guint16 pid)
627 MpegTSBaseClass *klass;
628 MpegTSBaseStream *stream = program->streams[pid];
630 GST_DEBUG ("pid:0x%04x", pid);
632 if (G_UNLIKELY (stream == NULL)) {
633 /* Can happen if the PCR PID is the same as a audio/video PID */
634 GST_DEBUG ("Stream already removed");
638 klass = GST_MPEGTS_BASE_GET_CLASS (base);
640 /* If subclass needs it, inform it of the stream we are about to remove */
641 if (klass->stream_removed)
642 klass->stream_removed (base, stream);
644 program->stream_list = g_list_remove_all (program->stream_list, stream);
645 mpegts_base_free_stream (stream);
646 program->streams[pid] = NULL;
649 /* Check if pmtstream is already present in the program */
650 static inline gboolean
651 _stream_in_pmt (const GstMpegtsPMT * pmt, MpegTSBaseStream * stream)
653 guint i, nbstreams = pmt->streams->len;
655 for (i = 0; i < nbstreams; i++) {
656 GstMpegtsPMTStream *pmt_stream = g_ptr_array_index (pmt->streams, i);
658 if (pmt_stream->pid == stream->pid &&
659 pmt_stream->stream_type == stream->stream_type)
666 static inline gboolean
667 _pmt_stream_in_program (MpegTSBaseProgram * program,
668 GstMpegtsPMTStream * stream)
670 MpegTSBaseStream *old_stream = program->streams[stream->pid];
673 return old_stream->stream_type == stream->stream_type;
677 mpegts_base_update_program (MpegTSBase * base, MpegTSBaseProgram * program,
678 GstMpegtsSection * section, const GstMpegtsPMT * pmt)
680 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
681 const gchar *stream_id =
682 gst_stream_collection_get_upstream_id (program->collection);
683 GstStreamCollection *collection;
684 GList *tmp, *toremove;
687 /* Create new collection */
688 collection = gst_stream_collection_new (stream_id);
689 gst_object_unref (program->collection);
690 program->collection = collection;
692 /* Replace section and pmt with the new one */
693 gst_mpegts_section_unref (program->section);
694 program->section = gst_mpegts_section_ref (section);
697 /* Copy over gststream that still exist into the collection */
698 for (tmp = program->stream_list; tmp; tmp = tmp->next) {
699 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
700 if (_stream_in_pmt (pmt, stream) && stream->in_collection) {
701 gst_stream_collection_add_stream (program->collection,
702 gst_object_ref (stream->stream_object));
706 /* Add new streams (will also create and add gststream to the collection) */
707 nbstreams = pmt->streams->len;
708 for (i = 0; i < nbstreams; i++) {
709 GstMpegtsPMTStream *stream = g_ptr_array_index (pmt->streams, i);
710 if (!_pmt_stream_in_program (program, stream))
711 mpegts_base_program_add_stream (base, program, stream->pid,
712 stream->stream_type, stream);
715 /* Call subclass update */
716 if (klass->update_program)
717 klass->update_program (base, program);
719 /* Remove streams no longer present */
721 for (tmp = program->stream_list; tmp; tmp = tmp->next) {
722 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
723 if (!_stream_in_pmt (pmt, stream))
724 toremove = g_list_prepend (toremove, stream);
726 for (tmp = toremove; tmp; tmp = tmp->next) {
727 MpegTSBaseStream *stream = (MpegTSBaseStream *) tmp->data;
728 mpegts_base_program_remove_stream (base, program, stream->pid);
735 _stream_is_private_section (const GstMpegtsPMT * pmt,
736 GstMpegtsPMTStream * stream)
738 switch (stream->stream_type) {
739 case GST_MPEGTS_STREAM_TYPE_SCTE_DSMCC_DCB:
740 case GST_MPEGTS_STREAM_TYPE_SCTE_SIGNALING:
742 guint32 registration_id =
743 get_registration_from_descriptors (stream->descriptors);
744 /* Not a private section stream */
745 if (registration_id != DRF_ID_CUEI && registration_id != DRF_ID_ETV1)
748 case GST_MPEGTS_STREAM_TYPE_PRIVATE_SECTIONS:
749 case GST_MPEGTS_STREAM_TYPE_MHEG:
750 case GST_MPEGTS_STREAM_TYPE_DSM_CC:
751 case GST_MPEGTS_STREAM_TYPE_DSMCC_A:
752 case GST_MPEGTS_STREAM_TYPE_DSMCC_B:
753 case GST_MPEGTS_STREAM_TYPE_DSMCC_C:
754 case GST_MPEGTS_STREAM_TYPE_DSMCC_D:
755 case GST_MPEGTS_STREAM_TYPE_SL_FLEXMUX_SECTIONS:
756 case GST_MPEGTS_STREAM_TYPE_METADATA_SECTIONS:
757 /* known PSI streams */
759 case GST_MPEGTS_STREAM_TYPE_SCTE_SIT:
761 guint32 registration_id =
762 get_registration_from_descriptors (pmt->descriptors);
763 /* Not a private section stream */
764 if (registration_id != DRF_ID_CUEI)
773 /* Return TRUE if programs are equal */
775 mpegts_base_is_same_program (MpegTSBase * base, MpegTSBaseProgram * oldprogram,
776 guint16 new_pmt_pid, const GstMpegtsPMT * new_pmt)
779 MpegTSBaseStream *oldstream;
780 gboolean sawpcrpid = FALSE;
782 if (oldprogram->pmt_pid != new_pmt_pid) {
783 GST_DEBUG ("Different pmt_pid (new:0x%04x, old:0x%04x)", new_pmt_pid,
784 oldprogram->pmt_pid);
788 if (!base->ignore_pcr && oldprogram->pcr_pid != new_pmt->pcr_pid) {
789 GST_DEBUG ("Different pcr_pid (new:0x%04x, old:0x%04x)",
790 new_pmt->pcr_pid, oldprogram->pcr_pid);
794 /* Check the streams */
795 nbstreams = new_pmt->streams->len;
796 for (i = 0; i < nbstreams; ++i) {
797 GstMpegtsPMTStream *stream = g_ptr_array_index (new_pmt->streams, i);
799 oldstream = oldprogram->streams[stream->pid];
801 GST_DEBUG ("New stream 0x%04x not present in old program", stream->pid);
804 if (oldstream->stream_type != stream->stream_type) {
806 ("New stream 0x%04x has a different stream type (new:%d, old:%d)",
807 stream->pid, stream->stream_type, oldstream->stream_type);
810 if (stream->pid == oldprogram->pcr_pid)
814 /* If the pcr is not shared with an existing stream, we'll have one extra stream */
818 if (nbstreams != g_list_length (oldprogram->stream_list)) {
819 GST_DEBUG ("Different number of streams (new:%d, old:%d)",
820 nbstreams, g_list_length (oldprogram->stream_list));
824 GST_DEBUG ("Programs are equal");
828 /* Return TRUE if program is an update
830 * A program is equal if:
831 * * The program number is the same (will be if it enters this function)
832 * * AND The PMT PID is equal to the old one
833 * * AND It contains at least one stream from the previous program
835 * Changes that are acceptable are therefore:
836 * * New streams appearing
837 * * Old streams going away
841 * * PMT PID being changed ?
842 * * Properties of elementary stream being changed ? (new tags ? metadata ?)
845 mpegts_base_is_program_update (MpegTSBase * base,
846 MpegTSBaseProgram * oldprogram, guint16 new_pmt_pid,
847 const GstMpegtsPMT * new_pmt)
850 MpegTSBaseStream *oldstream;
852 if (oldprogram->pmt_pid != new_pmt_pid) {
853 /* FIXME/CHECK: Can a program be updated by just changing its PID
855 GST_DEBUG ("Different pmt_pid (new:0x%04x, old:0x%04x)", new_pmt_pid,
856 oldprogram->pmt_pid);
860 /* Check if at least one stream from the previous program is still present
861 * in the new program */
863 /* Check the streams */
864 nbstreams = new_pmt->streams->len;
865 for (i = 0; i < nbstreams; ++i) {
866 GstMpegtsPMTStream *stream = g_ptr_array_index (new_pmt->streams, i);
868 oldstream = oldprogram->streams[stream->pid];
870 GST_DEBUG ("New stream 0x%04x not present in old program", stream->pid);
871 } else if (oldstream->stream_type != stream->stream_type) {
873 ("New stream 0x%04x has a different stream type (new:%d, old:%d)",
874 stream->pid, stream->stream_type, oldstream->stream_type);
875 } else if (!_stream_is_private_section (new_pmt, stream)) {
876 /* FIXME : We should actually be checking a bit deeper,
877 * especially for private streams (where the differentiation is
878 * done at the registration level) */
880 ("Stream 0x%04x is identical (stream_type %d) ! Program is an update",
881 stream->pid, stream->stream_type);
886 GST_DEBUG ("Program is not an update of the previous one");
891 mpegts_base_deactivate_program (MpegTSBase * base, MpegTSBaseProgram * program)
894 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
896 if (G_UNLIKELY (program->active == FALSE))
899 GST_DEBUG_OBJECT (base, "Deactivating PMT");
901 program->active = FALSE;
904 for (i = 0; i < program->pmt->streams->len; ++i) {
905 GstMpegtsPMTStream *stream = g_ptr_array_index (program->pmt->streams, i);
907 mpegts_base_program_remove_stream (base, program, stream->pid);
909 /* Only unset the is_pes/known_psi bit if the PID isn't used in any other active
911 if (!mpegts_pid_in_active_programs (base, stream->pid)) {
912 if (_stream_is_private_section (program->pmt, stream)) {
913 if (base->parse_private_sections)
914 MPEGTS_BIT_UNSET (base->known_psi, stream->pid);
916 MPEGTS_BIT_UNSET (base->is_pes, stream->pid);
921 /* remove pcr stream */
922 /* FIXME : This might actually be shared with another stream ? */
923 mpegts_base_program_remove_stream (base, program, program->pcr_pid);
924 if (!mpegts_pid_in_active_programs (base, program->pcr_pid))
925 MPEGTS_BIT_UNSET (base->is_pes, program->pcr_pid);
927 GST_DEBUG ("program stream_list is now %p", program->stream_list);
930 /* Inform subclasses we're deactivating this program */
931 if (klass->program_stopped)
932 klass->program_stopped (base, program);
936 mpegts_base_activate_program (MpegTSBase * base, MpegTSBaseProgram * program,
937 guint16 pmt_pid, GstMpegtsSection * section, const GstMpegtsPMT * pmt,
938 gboolean initial_program)
941 MpegTSBaseClass *klass;
943 if (G_UNLIKELY (program->active))
946 GST_DEBUG ("Activating program %d", program->program_number);
948 /* activate new pmt */
949 if (program->section)
950 gst_mpegts_section_unref (program->section);
951 program->section = gst_mpegts_section_ref (section);
954 program->pmt_pid = pmt_pid;
955 if (!base->ignore_pcr)
956 program->pcr_pid = pmt->pcr_pid;
958 program->pcr_pid = 0x1fff;
960 /* extract top-level registration_id if present */
961 program->registration_id =
962 get_registration_from_descriptors (pmt->descriptors);
963 GST_DEBUG ("program 0x%04x, registration_id %" SAFE_FOURCC_FORMAT,
964 program->program_number, SAFE_FOURCC_ARGS (program->registration_id));
966 for (i = 0; i < pmt->streams->len; ++i) {
967 GstMpegtsPMTStream *stream = g_ptr_array_index (pmt->streams, i);
968 if (_stream_is_private_section (pmt, stream)) {
969 if (base->parse_private_sections)
970 MPEGTS_BIT_SET (base->known_psi, stream->pid);
972 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->is_pes, stream->pid)))
974 ("Refcounting issue. Setting twice a PID (0x%04x) as known PES",
976 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, stream->pid))) {
978 ("Refcounting issue. Setting a known PSI PID (0x%04x) as known PES",
980 MPEGTS_BIT_UNSET (base->known_psi, stream->pid);
982 MPEGTS_BIT_SET (base->is_pes, stream->pid);
984 mpegts_base_program_add_stream (base, program,
985 stream->pid, stream->stream_type, stream);
987 /* We add the PCR pid last. If that PID is already used by one of the media
988 * streams above, no new stream will be created */
989 mpegts_base_program_add_stream (base, program, program->pcr_pid, -1, NULL);
990 MPEGTS_BIT_SET (base->is_pes, program->pcr_pid);
992 program->active = TRUE;
993 program->initial_program = initial_program;
995 klass = GST_MPEGTS_BASE_GET_CLASS (base);
996 if (klass->program_started != NULL)
997 klass->program_started (base, program);
999 GST_DEBUG_OBJECT (base, "new pmt activated");
1004 mpegts_base_apply_pat (MpegTSBase * base, GstMpegtsSection * section)
1006 GPtrArray *pat = gst_mpegts_section_get_pat (section);
1008 MpegTSBaseProgram *program;
1011 if (G_UNLIKELY (pat == NULL))
1014 GST_INFO_OBJECT (base, "PAT");
1016 /* Applying a new PAT does two things:
1017 * * It adds the new programs to the list of programs this element handles
1018 * and increments at the same time the number of times a program is referenced.
1020 * * If there was a previously active PAT, It decrements the reference count
1021 * of all program it used. If a program is no longer needed, it is removed.
1024 old_pat = base->pat;
1027 GST_LOG ("Activating new Program Association Table");
1028 /* activate the new table */
1029 for (i = 0; i < pat->len; ++i) {
1030 GstMpegtsPatProgram *patp = g_ptr_array_index (pat, i);
1032 program = mpegts_base_get_program (base, patp->program_number);
1034 /* IF the program already existed, just check if the PMT PID changed */
1035 if (program->pmt_pid != patp->network_or_program_map_PID) {
1036 if (program->pmt_pid != G_MAXUINT16) {
1037 /* pmt pid changed */
1038 /* FIXME: when this happens it may still be pmt pid of another
1039 * program, so setting to False may make it go through expensive
1040 * path in is_psi unnecessarily */
1041 MPEGTS_BIT_UNSET (base->known_psi, program->pmt_pid);
1044 program->pmt_pid = patp->network_or_program_map_PID;
1045 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, program->pmt_pid)))
1047 ("Refcounting issue. Setting twice a PMT PID (0x%04x) as know PSI",
1049 MPEGTS_BIT_SET (base->known_psi, patp->network_or_program_map_PID);
1052 /* Create a new program */
1054 mpegts_base_add_program (base, patp->program_number,
1055 patp->network_or_program_map_PID);
1057 /* We mark this program as being referenced by one PAT */
1058 program->patcount += 1;
1062 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1063 /* deactivate the old table */
1064 GST_LOG ("Deactivating old Program Association Table");
1066 for (i = 0; i < old_pat->len; ++i) {
1067 GstMpegtsPatProgram *patp = g_ptr_array_index (old_pat, i);
1069 program = mpegts_base_get_program (base, patp->program_number);
1070 if (G_UNLIKELY (program == NULL)) {
1071 GST_DEBUG_OBJECT (base, "broken PAT, duplicated entry for program %d",
1072 patp->program_number);
1076 if (--program->patcount > 0)
1077 /* the program has been referenced by the new pat, keep it */
1080 GST_INFO_OBJECT (base, "PAT removing program 0x%04x 0x%04x",
1081 patp->program_number, patp->network_or_program_map_PID);
1083 if (klass->can_remove_program (base, program)) {
1084 mpegts_base_deactivate_program (base, program);
1085 mpegts_base_remove_program (base, patp->program_number);
1087 /* sub-class now owns the program and must call
1088 * mpegts_base_deactivate_and_free_program later */
1089 g_hash_table_steal (base->programs,
1090 GINT_TO_POINTER ((gint) patp->program_number));
1092 /* FIXME: when this happens it may still be pmt pid of another
1093 * program, so setting to False may make it go through expensive
1094 * path in is_psi unnecessarily */
1095 if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi,
1096 patp->network_or_program_map_PID))) {
1098 ("Program refcounting : Setting twice a pid (0x%04x) as known PSI",
1099 patp->network_or_program_map_PID);
1101 MPEGTS_BIT_SET (base->known_psi, patp->network_or_program_map_PID);
1102 mpegts_packetizer_remove_stream (base->packetizer,
1103 patp->network_or_program_map_PID);
1106 g_ptr_array_unref (old_pat);
1113 mpegts_base_apply_pmt (MpegTSBase * base, GstMpegtsSection * section)
1115 const GstMpegtsPMT *pmt;
1116 MpegTSBaseProgram *program, *old_program;
1117 guint program_number;
1118 gboolean initial_program = TRUE;
1120 pmt = gst_mpegts_section_get_pmt (section);
1121 if (G_UNLIKELY (pmt == NULL)) {
1122 GST_ERROR ("Could not get PMT (corrupted ?)");
1126 /* FIXME : not so sure this is valid anymore */
1127 if (G_UNLIKELY (base->seen_pat == FALSE)) {
1128 GST_WARNING ("Got pmt without pat first. Returning");
1129 /* remove the stream since we won't get another PMT otherwise */
1130 mpegts_packetizer_remove_stream (base->packetizer, section->pid);
1134 program_number = section->subtable_extension;
1135 GST_DEBUG ("Applying PMT (program_number:%d, pid:0x%04x)",
1136 program_number, section->pid);
1138 /* In order for stream switching to happen properly in decodebin(2),
1139 * we need to first add the new pads (i.e. activate the new program)
1140 * before removing the old ones (i.e. deactivating the old program)
1143 old_program = mpegts_base_get_program (base, program_number);
1144 if (G_UNLIKELY (old_program == NULL))
1147 if (base->streams_aware
1148 && mpegts_base_is_program_update (base, old_program, section->pid, pmt)) {
1149 GST_FIXME ("We are streams_aware and new program is an update");
1150 /* The program is an update, and we can add/remove pads dynamically */
1151 mpegts_base_update_program (base, old_program, section, pmt);
1155 if (G_UNLIKELY (mpegts_base_is_same_program (base, old_program, section->pid,
1159 /* If the current program is active, this means we have a new program */
1160 if (old_program->active) {
1161 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1162 old_program = mpegts_base_steal_program (base, program_number);
1163 program = mpegts_base_new_program (base, program_number, section->pid);
1164 program->patcount = old_program->patcount;
1166 /* Deactivate the old program */
1167 /* FIXME : THIS IS BREAKING THE STREAM SWITCHING LOGIC !
1169 if (klass->can_remove_program (base, old_program)) {
1170 mpegts_base_deactivate_program (base, old_program);
1171 mpegts_base_free_program (old_program);
1173 /* sub-class now owns the program and must call
1174 * mpegts_base_deactivate_and_free_program later */
1175 g_hash_table_steal (base->programs,
1176 GINT_TO_POINTER ((gint) old_program->program_number));
1178 /* Add new program to the programs we track */
1179 g_hash_table_insert (base->programs,
1180 GINT_TO_POINTER (program_number), program);
1181 initial_program = FALSE;
1183 GST_DEBUG ("Program update, re-using same program");
1184 program = old_program;
1187 /* activate program */
1188 /* Ownership of pmt_info is given to the program */
1189 mpegts_base_activate_program (base, program, section->pid, section, pmt,
1193 GST_DEBUG ("Done activating program");
1198 GST_ERROR ("Attempted to apply a PMT on a program that wasn't created");
1204 GST_DEBUG ("Not applying identical program");
1210 mpegts_base_handle_psi (MpegTSBase * base, GstMpegtsSection * section)
1212 gboolean post_message = TRUE;
1214 GST_DEBUG ("Handling PSI (pid: 0x%04x , table_id: 0x%02x)",
1215 section->pid, section->table_id);
1217 switch (section->section_type) {
1218 case GST_MPEGTS_SECTION_PAT:
1219 post_message = mpegts_base_apply_pat (base, section);
1220 if (base->seen_pat == FALSE) {
1221 base->seen_pat = TRUE;
1222 GST_DEBUG ("First PAT offset: %" G_GUINT64_FORMAT, section->offset);
1223 mpegts_packetizer_set_reference_offset (base->packetizer,
1227 case GST_MPEGTS_SECTION_PMT:
1228 post_message = mpegts_base_apply_pmt (base, section);
1230 case GST_MPEGTS_SECTION_EIT:
1231 /* some tag xtraction + posting */
1232 post_message = mpegts_base_get_tags_from_eit (base, section);
1234 case GST_MPEGTS_SECTION_ATSC_MGT:
1235 post_message = mpegts_base_parse_atsc_mgt (base, section);
1241 /* Finally post message (if it wasn't corrupted) */
1243 gst_element_post_message (GST_ELEMENT_CAST (base),
1244 gst_message_new_mpegts_section (GST_OBJECT (base), section));
1245 gst_mpegts_section_unref (section);
1249 mpegts_base_parse_atsc_mgt (MpegTSBase * base, GstMpegtsSection * section)
1251 const GstMpegtsAtscMGT *mgt;
1254 mgt = gst_mpegts_section_get_atsc_mgt (section);
1255 if (G_UNLIKELY (mgt == NULL))
1258 for (i = 0; i < mgt->tables->len; ++i) {
1259 GstMpegtsAtscMGTTable *table = g_ptr_array_index (mgt->tables, i);
1261 if ((table->table_type >= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_EIT0 &&
1262 table->table_type <= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_EIT127) ||
1263 (table->table_type >= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_ETT0 &&
1264 table->table_type <= GST_MPEGTS_ATSC_MGT_TABLE_TYPE_ETT127)) {
1265 MPEGTS_BIT_SET (base->known_psi, table->pid);
1273 mpegts_base_get_tags_from_eit (MpegTSBase * base, GstMpegtsSection * section)
1275 const GstMpegtsEIT *eit;
1277 MpegTSBaseProgram *program;
1279 /* Early exit if it's not from the present/following table_id */
1280 if (section->table_id != GST_MTS_TABLE_ID_EVENT_INFORMATION_ACTUAL_TS_PRESENT
1281 && section->table_id !=
1282 GST_MTS_TABLE_ID_EVENT_INFORMATION_OTHER_TS_PRESENT)
1285 eit = gst_mpegts_section_get_eit (section);
1286 if (G_UNLIKELY (eit == NULL))
1289 program = mpegts_base_get_program (base, section->subtable_extension);
1292 ("program_id:0x%04x, table_id:0x%02x, actual_stream:%d, present_following:%d, program:%p",
1293 section->subtable_extension, section->table_id, eit->actual_stream,
1294 eit->present_following, program);
1296 if (program && eit->present_following) {
1297 for (i = 0; i < eit->events->len; i++) {
1298 GstMpegtsEITEvent *event = g_ptr_array_index (eit->events, i);
1299 const GstMpegtsDescriptor *desc;
1301 if (event->running_status == RUNNING_STATUS_RUNNING) {
1302 program->event_id = event->event_id;
1304 gst_mpegts_find_descriptor (event->descriptors,
1305 GST_MTS_DESC_DVB_SHORT_EVENT))) {
1306 gchar *name = NULL, *text = NULL;
1308 if (gst_mpegts_descriptor_parse_dvb_short_event (desc, NULL, &name,
1311 program->tags = gst_tag_list_new_empty ();
1314 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1315 GST_TAG_TITLE, name, NULL);
1319 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1320 GST_TAG_DESCRIPTION, text, NULL);
1323 /* FIXME : Is it correct to post an event duration as a GST_TAG_DURATION ??? */
1324 gst_tag_list_add (program->tags, GST_TAG_MERGE_APPEND,
1325 GST_TAG_DURATION, event->duration * GST_SECOND, NULL);
1337 remove_each_program (gpointer key, MpegTSBaseProgram * program,
1340 /* First deactivate it */
1341 mpegts_base_deactivate_program (base, program);
1346 static inline GstFlowReturn
1347 mpegts_base_drain (MpegTSBase * base)
1349 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1351 /* Call implementation */
1353 return klass->drain (base);
1359 mpegts_base_flush (MpegTSBase * base, gboolean hard)
1361 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1363 /* Call implementation */
1365 klass->flush (base, hard);
1369 mpegts_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1371 gboolean res = TRUE;
1373 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1374 gboolean is_sticky = GST_EVENT_IS_STICKY (event);
1376 GST_DEBUG_OBJECT (base, "Got event %s",
1377 gst_event_type_get_name (GST_EVENT_TYPE (event)));
1379 switch (GST_EVENT_TYPE (event)) {
1380 case GST_EVENT_SEGMENT:
1381 gst_event_copy_segment (event, &base->segment);
1382 GST_DEBUG_OBJECT (base, "Received segment %" GST_SEGMENT_FORMAT,
1384 /* Check if we need to switch PCR/PTS handling */
1385 if (base->segment.format == GST_FORMAT_TIME) {
1386 base->packetizer->calculate_offset = FALSE;
1387 base->packetizer->calculate_skew = TRUE;
1388 /* Seek was handled upstream */
1389 base->last_seek_seqnum = gst_event_get_seqnum (event);
1391 base->packetizer->calculate_offset = TRUE;
1392 base->packetizer->calculate_skew = FALSE;
1395 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1397 case GST_EVENT_STREAM_START:
1398 gst_event_unref (event);
1400 case GST_EVENT_CAPS:
1401 /* FIXME, do something */
1402 gst_event_unref (event);
1404 case GST_EVENT_FLUSH_STOP:
1405 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1406 hard = (base->mode != BASE_MODE_SEEKING);
1407 mpegts_packetizer_flush (base->packetizer, hard);
1408 mpegts_base_flush (base, hard);
1409 gst_segment_init (&base->segment, GST_FORMAT_UNDEFINED);
1410 base->seen_pat = FALSE;
1413 res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
1416 /* Always return TRUE for sticky events */
1424 mpegts_base_default_sink_query (MpegTSBase * base, GstQuery * query)
1426 return gst_pad_query_default (base->sinkpad, GST_OBJECT (base), query);
1430 mpegts_base_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1432 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1434 GST_DEBUG_OBJECT (base, "Got query %s",
1435 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1437 return GST_MPEGTS_BASE_GET_CLASS (base)->sink_query (base, query);
1440 static GstFlowReturn
1441 mpegts_base_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
1443 GstFlowReturn res = GST_FLOW_OK;
1445 MpegTSPacketizerPacketReturn pret;
1446 MpegTSPacketizer2 *packetizer;
1447 MpegTSPacketizerPacket packet;
1448 MpegTSBaseClass *klass;
1450 base = GST_MPEGTS_BASE (parent);
1451 klass = GST_MPEGTS_BASE_GET_CLASS (base);
1453 packetizer = base->packetizer;
1455 if (GST_BUFFER_IS_DISCONT (buf)) {
1456 GST_DEBUG_OBJECT (base, "Got DISCONT buffer, flushing");
1457 res = mpegts_base_drain (base);
1458 if (G_UNLIKELY (res != GST_FLOW_OK))
1461 mpegts_base_flush (base, FALSE);
1462 /* In the case of discontinuities in push-mode with TIME segment
1463 * we want to drop all previous observations (hard:TRUE) from
1465 if (base->mode == BASE_MODE_PUSHING
1466 && base->segment.format == GST_FORMAT_TIME) {
1467 mpegts_packetizer_flush (base->packetizer, TRUE);
1468 mpegts_packetizer_clear (base->packetizer);
1470 mpegts_packetizer_flush (base->packetizer, FALSE);
1473 mpegts_packetizer_push (base->packetizer, buf);
1475 while (res == GST_FLOW_OK) {
1476 pret = mpegts_packetizer_next_packet (base->packetizer, &packet);
1478 /* If we don't have enough data, return */
1479 if (G_UNLIKELY (pret == PACKET_NEED_MORE))
1482 if (G_UNLIKELY (pret == PACKET_BAD)) {
1483 /* bad header, skip the packet */
1484 GST_DEBUG_OBJECT (base, "bad packet, skipping");
1488 if (klass->inspect_packet)
1489 klass->inspect_packet (base, &packet);
1491 /* If it's a known PES, push it */
1492 if (MPEGTS_BIT_IS_SET (base->is_pes, packet.pid)) {
1493 /* push the packet downstream */
1494 if (base->push_data)
1495 res = klass->push (base, &packet, NULL);
1496 } else if (packet.payload
1497 && MPEGTS_BIT_IS_SET (base->known_psi, packet.pid)) {
1499 GList *others, *tmp;
1500 GstMpegtsSection *section;
1502 section = mpegts_packetizer_push_section (packetizer, &packet, &others);
1504 mpegts_base_handle_psi (base, section);
1505 if (G_UNLIKELY (others)) {
1506 for (tmp = others; tmp; tmp = tmp->next)
1507 mpegts_base_handle_psi (base, (GstMpegtsSection *) tmp->data);
1508 g_list_free (others);
1511 /* we need to push section packet downstream */
1512 if (base->push_section)
1513 res = klass->push (base, &packet, section);
1515 } else if (base->push_unknown) {
1516 res = klass->push (base, &packet, NULL);
1517 } else if (packet.payload && packet.pid != 0x1fff)
1518 GST_LOG ("PID 0x%04x Saw packet on a pid we don't handle", packet.pid);
1521 mpegts_packetizer_clear_packet (base->packetizer, &packet);
1524 if (res == GST_FLOW_OK && klass->input_done)
1525 res = klass->input_done (base);
1530 static GstFlowReturn
1531 mpegts_base_scan (MpegTSBase * base)
1533 GstFlowReturn ret = GST_FLOW_OK;
1534 GstBuffer *buf = NULL;
1536 gboolean done = FALSE;
1537 MpegTSPacketizerPacketReturn pret;
1539 gint64 upstream_size, seek_pos, reverse_limit;
1541 guint initial_pcr_seen;
1543 GST_DEBUG ("Scanning for initial sync point");
1545 /* Find initial sync point and at least 5 PCR values */
1546 for (i = 0; i < 20 && !done; i++) {
1547 GST_DEBUG ("Grabbing %d => %d", i * 65536, (i + 1) * 65536);
1549 ret = gst_pad_pull_range (base->sinkpad, i * 65536, 65536, &buf);
1550 if (G_UNLIKELY (ret == GST_FLOW_EOS))
1552 if (G_UNLIKELY (ret != GST_FLOW_OK))
1555 /* Push to packetizer */
1556 mpegts_packetizer_push (base->packetizer, buf);
1559 if (mpegts_packetizer_has_packets (base->packetizer)) {
1560 if (base->seek_offset == -1) {
1561 /* Mark the initial sync point and remember the packetsize */
1562 base->seek_offset = base->packetizer->offset;
1563 GST_DEBUG ("Sync point is now %" G_GUINT64_FORMAT, base->seek_offset);
1564 base->packetsize = base->packetizer->packet_size;
1567 /* Eat up all packets */
1568 pret = mpegts_packetizer_process_next_packet (base->packetizer);
1569 if (pret == PACKET_NEED_MORE)
1571 if (pret != PACKET_BAD && base->packetizer->nb_seen_offsets >= 5) {
1572 GST_DEBUG ("Got enough initial PCR");
1580 initial_pcr_seen = base->packetizer->nb_seen_offsets;
1581 if (G_UNLIKELY (initial_pcr_seen == 0))
1582 goto no_initial_pcr;
1583 GST_DEBUG ("Seen %d initial PCR", initial_pcr_seen);
1585 /* Now send data from the end */
1587 /* Get the size of upstream */
1588 format = GST_FORMAT_BYTES;
1589 if (!gst_pad_peer_query_duration (base->sinkpad, format, &tmpval))
1591 upstream_size = tmpval;
1593 /* The scanning takes place on the last 2048kB. Considering PCR should
1594 * be present at least every 100ms, this should cope with streams
1595 * up to 160Mbit/s */
1596 reverse_limit = MAX (0, upstream_size - 2097152);
1598 /* Find last PCR value, searching backwards by chunks of 300 MPEG-ts packets */
1599 for (seek_pos = MAX (0, upstream_size - 56400);
1600 seek_pos >= reverse_limit; seek_pos -= 56400) {
1601 mpegts_packetizer_clear (base->packetizer);
1602 GST_DEBUG ("Grabbing %" G_GUINT64_FORMAT " => %" G_GUINT64_FORMAT, seek_pos,
1605 ret = gst_pad_pull_range (base->sinkpad, seek_pos, 56400, &buf);
1606 if (G_UNLIKELY (ret == GST_FLOW_EOS))
1608 if (G_UNLIKELY (ret != GST_FLOW_OK))
1611 /* Push to packetizer */
1612 mpegts_packetizer_push (base->packetizer, buf);
1615 if (mpegts_packetizer_has_packets (base->packetizer)) {
1617 /* Eat up all packets, really try to get last PCR(s) */
1618 while (pret != PACKET_NEED_MORE)
1619 pret = mpegts_packetizer_process_next_packet (base->packetizer);
1621 if (base->packetizer->nb_seen_offsets > initial_pcr_seen) {
1622 GST_DEBUG ("Got last PCR(s) (total seen:%d)",
1623 base->packetizer->nb_seen_offsets);
1630 mpegts_packetizer_clear (base->packetizer);
1634 mpegts_packetizer_clear (base->packetizer);
1635 GST_WARNING_OBJECT (base, "Couldn't find any PCR within the first %d bytes",
1642 mpegts_base_loop (MpegTSBase * base)
1644 GstFlowReturn ret = GST_FLOW_ERROR;
1646 switch (base->mode) {
1647 case BASE_MODE_SCANNING:
1648 /* Find first sync point */
1649 ret = mpegts_base_scan (base);
1650 if (G_UNLIKELY (ret != GST_FLOW_OK))
1652 base->mode = BASE_MODE_STREAMING;
1653 GST_DEBUG ("Changing to Streaming");
1655 case BASE_MODE_SEEKING:
1656 /* FIXME : unclear if we still need mode_seeking... */
1657 base->mode = BASE_MODE_STREAMING;
1659 case BASE_MODE_STREAMING:
1661 GstBuffer *buf = NULL;
1663 GST_DEBUG ("Pulling data from %" G_GUINT64_FORMAT, base->seek_offset);
1665 if (G_UNLIKELY (base->last_seek_seqnum == GST_SEQNUM_INVALID)) {
1666 /* No configured seek, set a valid seqnum */
1667 base->last_seek_seqnum = gst_util_seqnum_next ();
1669 ret = gst_pad_pull_range (base->sinkpad, base->seek_offset,
1670 100 * base->packetsize, &buf);
1671 if (G_UNLIKELY (ret != GST_FLOW_OK))
1673 base->seek_offset += gst_buffer_get_size (buf);
1674 ret = mpegts_base_chain (base->sinkpad, GST_OBJECT_CAST (base), buf);
1675 if (G_UNLIKELY (ret != GST_FLOW_OK))
1679 case BASE_MODE_PUSHING:
1680 GST_WARNING ("wrong BASE_MODE_PUSHING mode in pull loop");
1688 GST_DEBUG_OBJECT (base, "Pausing task, reason %s", gst_flow_get_name (ret));
1689 if (ret == GST_FLOW_EOS) {
1690 if (!GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base,
1691 gst_event_new_eos ()))
1692 GST_ELEMENT_ERROR (base, STREAM, FAILED,
1693 (_("Internal data stream error.")),
1694 ("No program activated before EOS"));
1695 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
1696 GST_ELEMENT_FLOW_ERROR (base, ret);
1697 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, gst_event_new_eos ());
1699 gst_pad_pause_task (base->sinkpad);
1705 mpegts_base_handle_seek_event (MpegTSBase * base, GstPad * pad,
1708 MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
1709 GstFlowReturn ret = GST_FLOW_ERROR;
1711 gboolean flush, instant_rate_change;
1714 GstSeekType start_type, stop_type;
1716 GstEvent *flush_event = NULL;
1718 gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
1721 if (format != GST_FORMAT_TIME)
1724 if (GST_EVENT_SEQNUM (event) == base->last_seek_seqnum) {
1725 GST_DEBUG_OBJECT (base, "Skipping already handled seek");
1729 if (base->mode == BASE_MODE_PUSHING) {
1730 /* First try if upstream supports seeking in TIME format */
1731 if (gst_pad_push_event (base->sinkpad, gst_event_ref (event))) {
1732 GST_DEBUG ("upstream handled SEEK event");
1736 /* If the subclass can seek, do that */
1738 ret = klass->seek (base, event);
1739 if (G_UNLIKELY (ret != GST_FLOW_OK))
1740 GST_WARNING ("seeking failed %s", gst_flow_get_name (ret));
1744 if (GST_CLOCK_TIME_IS_VALID (base->seek_offset)) {
1745 base->mode = BASE_MODE_SEEKING;
1746 new_seek = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
1747 GST_SEEK_TYPE_SET, base->seek_offset, GST_SEEK_TYPE_NONE, -1);
1748 gst_event_set_seqnum (new_seek, GST_EVENT_SEQNUM (event));
1749 if (!gst_pad_push_event (base->sinkpad, new_seek))
1750 ret = GST_FLOW_ERROR;
1752 base->last_seek_seqnum = GST_EVENT_SEQNUM (event);
1754 base->mode = BASE_MODE_PUSHING;
1757 GST_WARNING ("subclass has no seek implementation");
1760 return ret == GST_FLOW_OK;
1764 GST_WARNING ("subclass has no seek implementation");
1769 GST_WARNING ("Negative rate not supported");
1773 GST_DEBUG ("seek event, rate: %f start: %" GST_TIME_FORMAT
1774 " stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
1775 GST_TIME_ARGS (stop));
1777 flush = ! !(flags & GST_SEEK_FLAG_FLUSH);
1778 instant_rate_change = ! !(flags & GST_SEEK_FLAG_INSTANT_RATE_CHANGE);
1780 /* Directly send the instant-rate-change event here before taking the
1781 * stream-lock so that it can be applied as soon as possible */
1782 if (base->mode != BASE_MODE_PUSHING && instant_rate_change) {
1785 /* instant rate change only supported if direction does not change. All
1786 * other requirements are already checked before creating the seek event
1787 * but let's double-check here to be sure */
1788 if ((rate > 0 && base->out_segment.rate < 0) ||
1789 (rate < 0 && base->out_segment.rate > 0) ||
1790 start_type != GST_SEEK_TYPE_NONE ||
1791 stop_type != GST_SEEK_TYPE_NONE || flush) {
1792 GST_ERROR_OBJECT (base,
1793 "Instant rate change seeks only supported in the "
1794 "same direction, without flushing and position change");
1798 ev = gst_event_new_instant_rate_change (rate / base->out_segment.rate,
1799 (GstSegmentFlags) (flags));
1800 gst_event_set_seqnum (ev, GST_EVENT_SEQNUM (event));
1801 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, ev);
1805 /* stop streaming, either by flushing or by pausing the task */
1806 base->mode = BASE_MODE_SEEKING;
1808 GST_DEBUG_OBJECT (base, "sending flush start");
1809 flush_event = gst_event_new_flush_start ();
1810 gst_event_set_seqnum (flush_event, GST_EVENT_SEQNUM (event));
1811 gst_pad_push_event (base->sinkpad, gst_event_ref (flush_event));
1812 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, flush_event);
1814 gst_pad_pause_task (base->sinkpad);
1816 /* wait for streaming to finish */
1817 GST_PAD_STREAM_LOCK (base->sinkpad);
1820 /* send a FLUSH_STOP for the sinkpad, since we need data for seeking */
1821 GST_DEBUG_OBJECT (base, "sending flush stop");
1822 flush_event = gst_event_new_flush_stop (TRUE);
1823 gst_event_set_seqnum (flush_event, GST_EVENT_SEQNUM (event));
1825 /* ref for it to be reused later */
1826 gst_pad_push_event (base->sinkpad, gst_event_ref (flush_event));
1827 /* And actually flush our pending data but allow to preserve some info
1828 * to perform the seek */
1829 mpegts_base_flush (base, FALSE);
1830 mpegts_packetizer_flush (base->packetizer, FALSE);
1833 if (flags & (GST_SEEK_FLAG_SEGMENT)) {
1834 GST_WARNING ("seek flags 0x%x are not supported", (int) flags);
1839 /* If the subclass can seek, do that */
1840 ret = klass->seek (base, event);
1841 if (G_UNLIKELY (ret != GST_FLOW_OK))
1842 GST_WARNING ("seeking failed %s", gst_flow_get_name (ret));
1844 base->last_seek_seqnum = GST_EVENT_SEQNUM (event);
1847 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
1848 GST_DEBUG_OBJECT (base, "sending flush stop");
1849 GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, flush_event);
1854 gst_event_unref (flush_event);
1855 gst_pad_start_task (base->sinkpad, (GstTaskFunction) mpegts_base_loop, base,
1858 GST_PAD_STREAM_UNLOCK (base->sinkpad);
1859 return ret == GST_FLOW_OK;
1864 mpegts_base_sink_activate (GstPad * sinkpad, GstObject * parent)
1869 query = gst_query_new_scheduling ();
1871 if (!gst_pad_peer_query (sinkpad, query)) {
1872 gst_query_unref (query);
1876 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
1877 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
1878 gst_query_unref (query);
1883 GST_DEBUG_OBJECT (sinkpad, "activating pull");
1884 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
1888 GST_DEBUG_OBJECT (sinkpad, "activating push");
1889 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
1894 mpegts_base_sink_activate_mode (GstPad * pad, GstObject * parent,
1895 GstPadMode mode, gboolean active)
1898 MpegTSBase *base = GST_MPEGTS_BASE (parent);
1901 case GST_PAD_MODE_PUSH:
1902 base->mode = BASE_MODE_PUSHING;
1905 case GST_PAD_MODE_PULL:
1907 base->mode = BASE_MODE_SCANNING;
1908 /* When working pull-based, we always use offsets for estimation */
1909 base->packetizer->calculate_offset = TRUE;
1910 base->packetizer->calculate_skew = FALSE;
1911 gst_segment_init (&base->segment, GST_FORMAT_BYTES);
1913 gst_pad_start_task (pad, (GstTaskFunction) mpegts_base_loop, base,
1916 res = gst_pad_stop_task (pad);
1925 static GstStateChangeReturn
1926 mpegts_base_change_state (GstElement * element, GstStateChange transition)
1929 GstStateChangeReturn ret;
1931 base = GST_MPEGTS_BASE (element);
1933 switch (transition) {
1934 case GST_STATE_CHANGE_READY_TO_PAUSED:
1935 mpegts_base_reset (base);
1941 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1943 switch (transition) {
1944 case GST_STATE_CHANGE_PAUSED_TO_READY:
1945 mpegts_base_reset (base);
1946 if (base->mode != BASE_MODE_PUSHING)
1947 base->mode = BASE_MODE_SCANNING;
1957 gst_mpegtsbase_plugin_init (GstPlugin * plugin)
1959 GST_DEBUG_CATEGORY_INIT (mpegts_base_debug, "mpegtsbase", 0,
1960 "MPEG transport stream base class");