3 * Copyright (C) 2007 Alessandro Decina
6 * Alessandro Decina <alessandro@nnva.org>
7 * Zaheer Abbas Merali <zaheerabbas at merali dot org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
33 #include "mpegtsbase.h"
34 #include "mpegtsparse.h"
35 #include "gstmpegdesc.h"
37 /* latency in mseconds is maximum 100 ms between PCR */
38 #define TS_LATENCY 100
40 #define TABLE_ID_UNSET 0xFF
41 #define RUNNING_STATUS_RUNNING 4
42 #define SYNC_BYTE 0x47
44 GST_DEBUG_CATEGORY_STATIC (mpegts_parse_debug);
45 #define GST_CAT_DEFAULT mpegts_parse_debug
47 typedef struct _MpegTSParsePad MpegTSParsePad;
51 MpegTSBaseProgram program;
52 MpegTSParsePad *tspad;
55 struct _MpegTSParsePad
59 /* the program number that the peer wants on this pad */
61 MpegTSParseProgram *program;
63 /* set to FALSE before a push and TRUE after */
66 /* the return of the latest push */
67 GstFlowReturn flow_return;
69 MpegTSParse2Adapter ts_adapter;
72 static GstStaticPadTemplate src_template =
73 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
75 GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ")
78 static GstStaticPadTemplate program_template =
79 GST_STATIC_PAD_TEMPLATE ("program_%u", GST_PAD_SRC,
81 GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ")
88 PROP_SMOOTHING_LATENCY,
95 static void mpegts_parse_set_property (GObject * object, guint prop_id,
96 const GValue * value, GParamSpec * pspec);
97 static void mpegts_parse_get_property (GObject * object, guint prop_id,
98 GValue * value, GParamSpec * pspec);
101 mpegts_parse_program_started (MpegTSBase * base, MpegTSBaseProgram * program);
103 mpegts_parse_program_stopped (MpegTSBase * base, MpegTSBaseProgram * program);
106 mpegts_parse_push (MpegTSBase * base, MpegTSPacketizerPacket * packet,
107 GstMpegtsSection * section);
108 static void mpegts_parse_inspect_packet (MpegTSBase * base,
109 MpegTSPacketizerPacket * packet);
110 static GstFlowReturn mpegts_parse_have_buffer (MpegTSBase * base,
113 static MpegTSParsePad *mpegts_parse_create_tspad (MpegTSParse2 * parse,
115 static void mpegts_parse_destroy_tspad (MpegTSParse2 * parse,
116 MpegTSParsePad * tspad);
118 static void mpegts_parse_pad_removed (GstElement * element, GstPad * pad);
119 static GstPad *mpegts_parse_request_new_pad (GstElement * element,
120 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
121 static void mpegts_parse_release_pad (GstElement * element, GstPad * pad);
122 static gboolean mpegts_parse_src_pad_query (GstPad * pad, GstObject * parent,
124 static gboolean push_event (MpegTSBase * base, GstEvent * event);
126 #define mpegts_parse_parent_class parent_class
127 G_DEFINE_TYPE (MpegTSParse2, mpegts_parse, GST_TYPE_MPEGTS_BASE);
128 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (tsparse, "tsparse",
129 GST_RANK_NONE, GST_TYPE_MPEGTS_PARSE,
130 GST_DEBUG_CATEGORY_INIT (mpegts_parse_debug, "tsparse", 0,
131 "MPEG transport stream parser"));
133 static void mpegts_parse_reset (MpegTSBase * base);
134 static GstFlowReturn mpegts_parse_input_done (MpegTSBase * base);
136 drain_pending_buffers (MpegTSParse2 * parse, gboolean drain_all);
139 mpegts_parse_finalize (GObject * object)
141 MpegTSParse2 *parse = (MpegTSParse2 *) object;
143 gst_flow_combiner_free (parse->flowcombiner);
145 gst_adapter_clear (parse->ts_adapter.adapter);
146 g_object_unref (parse->ts_adapter.adapter);
148 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
152 mpegts_parse_class_init (MpegTSParse2Class * klass)
154 GObjectClass *gobject_class = (GObjectClass *) (klass);
155 GstElementClass *element_class;
156 MpegTSBaseClass *ts_class;
158 gobject_class->set_property = mpegts_parse_set_property;
159 gobject_class->get_property = mpegts_parse_get_property;
160 gobject_class->finalize = mpegts_parse_finalize;
162 g_object_class_install_property (gobject_class, PROP_SET_TIMESTAMPS,
163 g_param_spec_boolean ("set-timestamps",
164 "Timestamp (or re-timestamp) the output stream",
165 "If set, timestamps will be set on the output buffers using "
166 "PCRs and smoothed over the smoothing-latency period", FALSE,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (gobject_class, PROP_SMOOTHING_LATENCY,
169 g_param_spec_uint ("smoothing-latency", "Smoothing Latency",
170 "Additional latency in microseconds for smoothing jitter in input timestamps on live capture",
171 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172 g_object_class_install_property (gobject_class, PROP_PCR_PID,
173 g_param_spec_int ("pcr-pid", "PID containing PCR",
174 "Set the PID to use for PCR values (-1 for auto)",
175 -1, G_MAXINT, -1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176 g_object_class_install_property (gobject_class, PROP_ALIGNMENT,
177 g_param_spec_uint ("alignment", "Alignment",
178 "Number of packets per buffer (padded with dummy packets on EOS) (0 = auto)",
179 0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (gobject_class, PROP_SPLIT_ON_RAI,
181 g_param_spec_boolean ("split-on-rai", "Split on RAI",
182 "If set, buffers sized smaller than the alignment will be sent "
183 "so that RAI packets are at the start of a new buffer", FALSE,
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 element_class = GST_ELEMENT_CLASS (klass);
187 element_class->pad_removed = mpegts_parse_pad_removed;
188 element_class->request_new_pad = mpegts_parse_request_new_pad;
189 element_class->release_pad = mpegts_parse_release_pad;
191 gst_element_class_add_static_pad_template (element_class, &src_template);
192 gst_element_class_add_static_pad_template (element_class, &program_template);
194 gst_element_class_set_static_metadata (element_class,
195 "MPEG transport stream parser", "Codec/Parser",
196 "Parses MPEG2 transport streams",
197 "Alessandro Decina <alessandro@nnva.org>, "
198 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
200 ts_class = GST_MPEGTS_BASE_CLASS (klass);
201 ts_class->push = GST_DEBUG_FUNCPTR (mpegts_parse_push);
202 ts_class->push_event = GST_DEBUG_FUNCPTR (push_event);
203 ts_class->program_started = GST_DEBUG_FUNCPTR (mpegts_parse_program_started);
204 ts_class->program_stopped = GST_DEBUG_FUNCPTR (mpegts_parse_program_stopped);
205 ts_class->reset = GST_DEBUG_FUNCPTR (mpegts_parse_reset);
206 ts_class->input_done = GST_DEBUG_FUNCPTR (mpegts_parse_input_done);
207 ts_class->inspect_packet = GST_DEBUG_FUNCPTR (mpegts_parse_inspect_packet);
211 mpegts_parse_init (MpegTSParse2 * parse)
213 MpegTSBase *base = (MpegTSBase *) parse;
215 base->program_size = sizeof (MpegTSParseProgram);
216 base->push_data = TRUE;
217 base->push_section = TRUE;
218 base->push_unknown = TRUE;
220 parse->user_pcr_pid = parse->pcr_pid = -1;
222 parse->flowcombiner = gst_flow_combiner_new ();
224 parse->srcpad = gst_pad_new_from_static_template (&src_template, "src");
225 gst_flow_combiner_add_pad (parse->flowcombiner, parse->srcpad);
227 gst_pad_set_query_function (parse->srcpad,
228 GST_DEBUG_FUNCPTR (mpegts_parse_src_pad_query));
229 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
231 parse->have_group_id = FALSE;
232 parse->group_id = G_MAXUINT;
234 parse->ts_adapter.adapter = gst_adapter_new ();
235 parse->ts_adapter.packets_in_adapter = 0;
236 parse->ts_adapter.first_is_keyframe = TRUE;
237 parse->alignment = 0;
238 parse->is_eos = FALSE;
240 parse->split_on_rai = FALSE;
244 mpegts_parse_reset (MpegTSBase * base)
246 MpegTSParse2 *parse = (MpegTSParse2 *) base;
248 /* Set the various know PIDs we are interested in */
251 MPEGTS_BIT_SET (base->known_psi, 1);
253 MPEGTS_BIT_SET (base->known_psi, 0x10);
255 MPEGTS_BIT_SET (base->known_psi, 0x11);
256 /* EIT, ST, CIT (TS 102 323) */
257 MPEGTS_BIT_SET (base->known_psi, 0x12);
259 MPEGTS_BIT_SET (base->known_psi, 0x13);
260 /* RNT (TS 102 323) */
261 MPEGTS_BIT_SET (base->known_psi, 0x16);
262 /* inband signalling */
263 MPEGTS_BIT_SET (base->known_psi, 0x1c);
265 MPEGTS_BIT_SET (base->known_psi, 0x1d);
267 MPEGTS_BIT_SET (base->known_psi, 0x1e);
269 MPEGTS_BIT_SET (base->known_psi, 0x1f);
272 parse->have_group_id = FALSE;
273 parse->group_id = G_MAXUINT;
275 g_list_free_full (parse->pending_buffers, (GDestroyNotify) gst_buffer_unref);
276 parse->pending_buffers = NULL;
278 parse->current_pcr = GST_CLOCK_TIME_NONE;
279 parse->previous_pcr = GST_CLOCK_TIME_NONE;
280 parse->base_pcr = GST_CLOCK_TIME_NONE;
281 parse->bytes_since_pcr = 0;
282 parse->pcr_pid = parse->user_pcr_pid;
283 parse->ts_offset = 0;
285 gst_adapter_clear (parse->ts_adapter.adapter);
286 parse->ts_adapter.packets_in_adapter = 0;
287 parse->ts_adapter.first_is_keyframe = TRUE;
288 parse->is_eos = FALSE;
293 mpegts_parse_set_property (GObject * object, guint prop_id,
294 const GValue * value, GParamSpec * pspec)
296 MpegTSParse2 *parse = (MpegTSParse2 *) object;
299 case PROP_SET_TIMESTAMPS:
300 parse->set_timestamps = g_value_get_boolean (value);
302 case PROP_SMOOTHING_LATENCY:
303 parse->smoothing_latency = GST_USECOND * g_value_get_uint (value);
304 mpegts_packetizer_set_pcr_discont_threshold (GST_MPEGTS_BASE
305 (parse)->packetizer, parse->smoothing_latency);
308 parse->pcr_pid = parse->user_pcr_pid = g_value_get_int (value);
311 parse->alignment = g_value_get_uint (value);
313 case PROP_SPLIT_ON_RAI:
314 parse->split_on_rai = g_value_get_boolean (value);
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322 mpegts_parse_get_property (GObject * object, guint prop_id,
323 GValue * value, GParamSpec * pspec)
325 MpegTSParse2 *parse = (MpegTSParse2 *) object;
328 case PROP_SET_TIMESTAMPS:
329 g_value_set_boolean (value, parse->set_timestamps);
331 case PROP_SMOOTHING_LATENCY:
332 g_value_set_uint (value, parse->smoothing_latency / GST_USECOND);
335 g_value_set_int (value, parse->pcr_pid);
338 g_value_set_uint (value, parse->alignment);
340 case PROP_SPLIT_ON_RAI:
341 g_value_set_boolean (value, parse->split_on_rai);
344 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349 prepare_src_pad (MpegTSBase * base, MpegTSParse2 * parse)
358 /* If there's no packet_size yet, we can't set caps yet */
359 if (G_UNLIKELY (base->packetizer->packet_size == 0))
363 gst_pad_create_stream_id (parse->srcpad, GST_ELEMENT_CAST (base),
367 gst_pad_get_sticky_event (parse->parent.sinkpad, GST_EVENT_STREAM_START,
370 if (gst_event_parse_group_id (event, &parse->group_id))
371 parse->have_group_id = TRUE;
373 parse->have_group_id = FALSE;
374 gst_event_unref (event);
375 } else if (!parse->have_group_id) {
376 parse->have_group_id = TRUE;
377 parse->group_id = gst_util_group_id_next ();
379 event = gst_event_new_stream_start (stream_id);
380 if (parse->have_group_id)
381 gst_event_set_group_id (event, parse->group_id);
383 gst_pad_push_event (parse->srcpad, event);
386 caps = gst_caps_new_simple ("video/mpegts",
387 "systemstream", G_TYPE_BOOLEAN, TRUE,
388 "packetsize", G_TYPE_INT, base->packetizer->packet_size, NULL);
390 gst_pad_set_caps (parse->srcpad, caps);
391 gst_caps_unref (caps);
393 /* If setting output timestamps, ensure that the output segment is TIME */
394 if (parse->set_timestamps == FALSE || base->segment.format == GST_FORMAT_TIME)
395 /* Just use the upstream segment */
396 base->out_segment = base->segment;
398 GstSegment *seg = &base->out_segment;
399 gst_segment_init (seg, GST_FORMAT_TIME);
400 GST_DEBUG_OBJECT (parse,
401 "Generating time output segment %" GST_SEGMENT_FORMAT, seg);
403 gst_pad_push_event (parse->srcpad,
404 gst_event_new_segment (&base->out_segment));
406 parse->first = FALSE;
412 push_event (MpegTSBase * base, GstEvent * event)
414 MpegTSParse2 *parse = (MpegTSParse2 *) base;
417 if (G_UNLIKELY (parse->first)) {
418 /* We will send the segment when really starting */
419 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT)) {
420 gst_event_unref (event);
423 prepare_src_pad (base, parse);
425 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_EOS)) {
426 gsize packet_size = base->packetizer->packet_size;
428 parse->is_eos = TRUE;
430 if (packet_size > 0 && parse->alignment > 0 &&
431 parse->ts_adapter.packets_in_adapter > 0
432 && parse->ts_adapter.packets_in_adapter < parse->alignment) {
436 gint missing_packets =
437 parse->alignment - parse->ts_adapter.packets_in_adapter;
438 gint i = missing_packets;
440 GST_DEBUG_OBJECT (parse, "Adding %d dummy packets", missing_packets);
442 buf = gst_buffer_new_and_alloc (missing_packets * packet_size);
443 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
449 if (packet_size > MPEGTS_NORMAL_PACKETSIZE) {
451 GST_WRITE_UINT32_BE (data, parse->header);
456 GST_WRITE_UINT8 (data + offset, SYNC_BYTE);
457 /* null packet PID */
458 GST_WRITE_UINT16_BE (data + offset + 1, 0x1FFF);
459 /* no adaptation field exists | continuity counter undefined */
460 GST_WRITE_UINT8 (data + offset + 3, 0x10);
462 memset (data + offset + 4, 0, MPEGTS_NORMAL_PACKETSIZE - 4);
465 gst_buffer_unmap (buf, &map);
466 gst_adapter_push (parse->ts_adapter.adapter, buf);
467 parse->ts_adapter.packets_in_adapter += missing_packets;
469 drain_pending_buffers (parse, TRUE);
472 if (G_UNLIKELY (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT))
473 parse->ts_offset = 0;
475 for (tmp = parse->srcpads; tmp; tmp = tmp->next) {
476 GstPad *pad = (GstPad *) tmp->data;
478 gst_event_ref (event);
479 gst_pad_push_event (pad, event);
483 gst_pad_push_event (parse->srcpad, event);
488 static MpegTSParsePad *
489 mpegts_parse_create_tspad (MpegTSParse2 * parse, const gchar * pad_name)
492 MpegTSParsePad *tspad;
494 pad = gst_pad_new_from_static_template (&program_template, pad_name);
495 gst_pad_set_query_function (pad,
496 GST_DEBUG_FUNCPTR (mpegts_parse_src_pad_query));
498 /* create our wrapper */
499 tspad = g_new0 (MpegTSParsePad, 1);
501 tspad->program_number = -1;
502 tspad->program = NULL;
503 tspad->pushed = FALSE;
504 tspad->flow_return = GST_FLOW_NOT_LINKED;
505 tspad->ts_adapter.adapter = gst_adapter_new ();
506 tspad->ts_adapter.packets_in_adapter = 0;
507 tspad->ts_adapter.first_is_keyframe = TRUE;
508 gst_pad_set_element_private (pad, tspad);
509 gst_flow_combiner_add_pad (parse->flowcombiner, pad);
515 mpegts_parse_destroy_tspad (MpegTSParse2 * parse, MpegTSParsePad * tspad)
517 gst_adapter_clear (tspad->ts_adapter.adapter);
518 g_object_unref (tspad->ts_adapter.adapter);
520 /* free the wrapper */
525 mpegts_parse_pad_removed (GstElement * element, GstPad * pad)
527 MpegTSParsePad *tspad;
528 MpegTSParse2 *parse = GST_MPEGTS_PARSE (element);
530 if (gst_pad_get_direction (pad) == GST_PAD_SINK)
533 tspad = (MpegTSParsePad *) gst_pad_get_element_private (pad);
535 mpegts_parse_destroy_tspad (parse, tspad);
537 parse->srcpads = g_list_remove_all (parse->srcpads, pad);
540 if (GST_ELEMENT_CLASS (parent_class)->pad_removed)
541 GST_ELEMENT_CLASS (parent_class)->pad_removed (element, pad);
545 mpegts_parse_request_new_pad (GstElement * element, GstPadTemplate * template,
546 const gchar * padname, const GstCaps * caps)
549 MpegTSParsePad *tspad;
550 MpegTSParseProgram *parseprogram;
552 gint program_num = -1;
556 g_return_val_if_fail (template != NULL, NULL);
557 g_return_val_if_fail (GST_IS_MPEGTS_PARSE (element), NULL);
558 g_return_val_if_fail (padname != NULL, NULL);
560 sscanf (padname + 8, "%d", &program_num);
562 GST_DEBUG_OBJECT (element, "padname:%s, program:%d", padname, program_num);
564 parse = GST_MPEGTS_PARSE (element);
566 tspad = mpegts_parse_create_tspad (parse, padname);
567 tspad->program_number = program_num;
569 /* Find if the program is already active */
571 (MpegTSParseProgram *) mpegts_base_get_program (GST_MPEGTS_BASE (parse),
574 tspad->program = parseprogram;
575 parseprogram->tspad = tspad;
579 parse->srcpads = g_list_append (parse->srcpads, pad);
581 gst_pad_set_active (pad, TRUE);
583 stream_id = gst_pad_create_stream_id (pad, element, padname + 8);
586 gst_pad_get_sticky_event (parse->parent.sinkpad, GST_EVENT_STREAM_START,
589 if (gst_event_parse_group_id (event, &parse->group_id))
590 parse->have_group_id = TRUE;
592 parse->have_group_id = FALSE;
593 gst_event_unref (event);
594 } else if (!parse->have_group_id) {
595 parse->have_group_id = TRUE;
596 parse->group_id = gst_util_group_id_next ();
598 event = gst_event_new_stream_start (stream_id);
599 if (parse->have_group_id)
600 gst_event_set_group_id (event, parse->group_id);
602 gst_pad_push_event (pad, event);
605 gst_element_add_pad (element, pad);
611 mpegts_packet_to_buffer (MpegTSPacketizerPacket * packet)
614 gst_buffer_new_and_alloc (packet->data_end - packet->data_start);
615 gst_buffer_fill (buf, 0, packet->data_start,
616 packet->data_end - packet->data_start);
621 mpegts_parse_release_pad (GstElement * element, GstPad * pad)
623 MpegTSParse2 *parse = (MpegTSParse2 *) element;
625 gst_pad_set_active (pad, FALSE);
626 /* we do the cleanup in GstElement::pad-removed */
627 gst_flow_combiner_remove_pad (parse->flowcombiner, pad);
628 gst_element_remove_pad (element, pad);
632 empty_adapter_into_pad (MpegTSParse2 * parse, MpegTSParse2Adapter * ts_adapter,
635 GstAdapter *adapter = ts_adapter->adapter;
636 GstBuffer *buf = NULL;
637 guint64 pts_dist, dts_dist;
638 GstClockTime pts, dts;
639 gsize avail = gst_adapter_available (adapter);
640 GstFlowReturn ret = GST_FLOW_OK;
644 buf = gst_adapter_take_buffer (adapter, avail);
645 /* Find the previous PTS/DTS. We also handle un-aligned input since want to
646 * use the most recent PTS/DTS if present */
647 offset = MIN (GST_MPEGTS_BASE (parse)->packetizer->packet_size, 188);
648 pts = gst_adapter_prev_pts_at_offset (adapter, offset, &pts_dist);
649 dts = gst_adapter_prev_dts_at_offset (adapter, offset, &dts_dist);
652 "prev pts:%" GST_TIME_FORMAT " (dist:%" G_GUINT64_FORMAT ") dts:%"
653 GST_TIME_FORMAT " (dist:%" G_GUINT64_FORMAT ")",
654 GST_TIME_ARGS (pts), pts_dist, GST_TIME_ARGS (dts), dts_dist);
656 ts_adapter->packets_in_adapter = 0;
659 GST_BUFFER_PTS (buf) = pts;
660 GST_BUFFER_DTS (buf) = dts;
661 if (!ts_adapter->first_is_keyframe)
662 gst_buffer_set_flags (buf, GST_BUFFER_FLAG_DELTA_UNIT);
663 ret = gst_pad_push (pad, buf);
670 enqueue_and_maybe_push_buffer (MpegTSParse2 * parse, GstPad * pad,
671 MpegTSParse2Adapter * ts_adapter, GstBuffer * buffer)
673 GstFlowReturn ret = GST_FLOW_OK;
675 if (buffer != NULL) {
676 if (parse->alignment == 1) {
677 ret = gst_pad_push (pad, buffer);
678 ret = gst_flow_combiner_update_flow (parse->flowcombiner, ret);
680 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)
681 && parse->split_on_rai) {
682 ret = empty_adapter_into_pad (parse, ts_adapter, pad);
683 ret = gst_flow_combiner_update_flow (parse->flowcombiner, ret);
685 gst_adapter_push (ts_adapter->adapter, buffer);
686 ts_adapter->packets_in_adapter++;
687 if (ts_adapter->packets_in_adapter == 1 && parse->split_on_rai) {
688 ts_adapter->first_is_keyframe =
689 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
692 if (ts_adapter->packets_in_adapter == parse->alignment
693 && ts_adapter->packets_in_adapter > 0) {
694 ret = empty_adapter_into_pad (parse, ts_adapter, pad);
695 ret = gst_flow_combiner_update_flow (parse->flowcombiner, ret);
704 mpegts_parse_tspad_push_section (MpegTSParse2 * parse, MpegTSParsePad * tspad,
705 GstMpegtsSection * section, MpegTSPacketizerPacket * packet,
708 GstFlowReturn ret = GST_FLOW_OK;
709 gboolean to_push = TRUE;
711 if (tspad->program_number != -1) {
712 if (tspad->program) {
713 /* we push all sections to all pads except PMTs which we
714 * only push to pads meant to receive that program number */
715 if (section->table_id == 0x02) {
717 if (section->subtable_extension != tspad->program_number)
720 } else if (section->table_id != 0x00) {
721 /* there's a program filter on the pad but the PMT for the program has not
722 * been parsed yet, ignore the pad until we get a PMT.
723 * But we always allow PAT to go through */
728 GST_DEBUG_OBJECT (parse,
729 "pushing section: %d program number: %d table_id: %d", to_push,
730 tspad->program_number, section->table_id);
734 enqueue_and_maybe_push_buffer (parse, tspad->pad,
735 &tspad->ts_adapter, gst_buffer_ref (buf));
738 GST_LOG_OBJECT (parse, "Returning %s", gst_flow_get_name (ret));
743 mpegts_parse_tspad_push (MpegTSParse2 * parse, MpegTSParsePad * tspad,
744 MpegTSPacketizerPacket * packet, GstBuffer * buf)
746 GstFlowReturn ret = GST_FLOW_OK;
747 MpegTSBaseProgram *bp = NULL;
749 if (tspad->program_number != -1) {
751 bp = (MpegTSBaseProgram *) tspad->program;
753 bp = mpegts_base_get_program ((MpegTSBase *) parse,
754 tspad->program_number);
758 if (packet->pid == bp->pmt_pid || bp->streams == NULL
759 || bp->streams[packet->pid]) {
760 /* push if there's no filter or if the pid is in the filter */
761 ret = gst_pad_push (tspad->pad, gst_buffer_ref (buf));
762 ret = gst_flow_combiner_update_flow (parse->flowcombiner, ret);
765 GST_DEBUG_OBJECT (parse, "Returning %s", gst_flow_get_name (ret));
771 pad_clear_for_push (GstPad * pad, MpegTSParse2 * parse)
773 MpegTSParsePad *tspad = (MpegTSParsePad *) gst_pad_get_element_private (pad);
775 tspad->flow_return = GST_FLOW_NOT_LINKED;
776 tspad->pushed = FALSE;
780 mpegts_parse_push (MpegTSBase * base, MpegTSPacketizerPacket * packet,
781 GstMpegtsSection * section)
783 MpegTSParse2 *parse = (MpegTSParse2 *) base;
785 gboolean done = FALSE;
787 MpegTSParsePad *tspad;
792 GST_OBJECT_LOCK (parse);
793 srcpads = parse->srcpads;
795 /* clear tspad->pushed on pads */
796 g_list_foreach (srcpads, (GFunc) pad_clear_for_push, parse);
798 ret = GST_FLOW_NOT_LINKED;
802 /* Get cookie and source pads list */
803 pads_cookie = GST_ELEMENT_CAST (parse)->pads_cookie;
804 if (G_LIKELY (srcpads)) {
805 pad = GST_PAD_CAST (srcpads->data);
808 GST_OBJECT_UNLOCK (parse);
810 buf = mpegts_packet_to_buffer (packet);
811 if (parse->split_on_rai
812 && !(packet->afc_flags & MPEGTS_AFC_RANDOM_ACCESS_FLAG)) {
813 gst_buffer_set_flags (buf, GST_BUFFER_FLAG_DELTA_UNIT);
816 /* Copy over input PTS/DTS (if present) */
817 GST_BUFFER_DTS (buf) = base->packetizer->last_dts;
818 GST_BUFFER_PTS (buf) = base->packetizer->last_pts;
819 ret = mpegts_parse_have_buffer (base, gst_buffer_ref (buf));
821 while (pad && !done) {
822 tspad = gst_pad_get_element_private (pad);
824 if (G_LIKELY (!tspad->pushed)) {
827 mpegts_parse_tspad_push_section (parse, tspad, section, packet,
831 mpegts_parse_tspad_push (parse, tspad, packet, buf);
833 tspad->pushed = TRUE;
835 if (G_UNLIKELY (tspad->flow_return != GST_FLOW_OK
836 && tspad->flow_return != GST_FLOW_NOT_LINKED)) {
837 /* return the error upstream */
838 ret = tspad->flow_return;
844 if (ret == GST_FLOW_NOT_LINKED)
845 ret = tspad->flow_return;
847 g_object_unref (pad);
849 if (G_UNLIKELY (!done)) {
850 GST_OBJECT_LOCK (parse);
851 if (G_UNLIKELY (pads_cookie != GST_ELEMENT_CAST (parse)->pads_cookie)) {
853 GST_DEBUG ("resync");
854 pads_cookie = GST_ELEMENT_CAST (parse)->pads_cookie;
855 srcpads = parse->srcpads;
857 GST_DEBUG ("getting next pad");
859 srcpads = g_list_next (srcpads);
863 pad = GST_PAD_CAST (srcpads->data);
867 GST_OBJECT_UNLOCK (parse);
871 gst_buffer_unref (buf);
876 mpegts_parse_inspect_packet (MpegTSBase * base, MpegTSPacketizerPacket * packet)
878 MpegTSParse2 *parse = GST_MPEGTS_PARSE (base);
879 GST_LOG ("pid 0x%04x pusi:%d, afc:%d, cont:%d, payload:%p PCR %"
880 G_GUINT64_FORMAT, packet->pid, packet->payload_unit_start_indicator,
881 packet->scram_afc_cc & 0x30,
882 FLAGS_CONTINUITY_COUNTER (packet->scram_afc_cc), packet->payload,
885 /* Store the PCR if desired */
886 if (parse->current_pcr == GST_CLOCK_TIME_NONE &&
887 packet->afc_flags & MPEGTS_AFC_PCR_FLAG) {
888 /* Take this as the pcr_pid if set to auto-select */
889 if (parse->pcr_pid == -1)
890 parse->pcr_pid = packet->pid;
891 /* Check the PCR-PID matches the program we want for multiple programs */
892 if (parse->pcr_pid == packet->pid) {
893 parse->current_pcr = mpegts_packetizer_pts_to_ts (base->packetizer,
894 PCRTIME_TO_GSTTIME (packet->pcr), parse->pcr_pid);
895 GST_DEBUG ("Got new PCR %" GST_TIME_FORMAT " raw %" G_GUINT64_FORMAT,
896 GST_TIME_ARGS (parse->current_pcr), packet->pcr);
897 if (parse->base_pcr == GST_CLOCK_TIME_NONE) {
898 parse->base_pcr = parse->current_pcr;
905 get_pending_timestamp_diff (MpegTSParse2 * parse)
908 GstClockTime first_ts, last_ts;
910 if (parse->pending_buffers == NULL)
911 return GST_CLOCK_TIME_NONE;
913 l = g_list_last (parse->pending_buffers);
914 first_ts = GST_BUFFER_PTS (l->data);
915 if (first_ts == GST_CLOCK_TIME_NONE)
916 return GST_CLOCK_TIME_NONE;
918 l = g_list_first (parse->pending_buffers);
919 last_ts = GST_BUFFER_PTS (l->data);
920 if (last_ts == GST_CLOCK_TIME_NONE)
921 return GST_CLOCK_TIME_NONE;
923 return last_ts - first_ts;
927 drain_pending_buffers (MpegTSParse2 * parse, gboolean drain_all)
929 GstFlowReturn ret = GST_FLOW_OK;
930 GstClockTime start_ts;
931 GstClockTime pcr = GST_CLOCK_TIME_NONE;
932 GstClockTime pcr_diff = 0;
933 gsize pcr_bytes, bytes_since_pcr, pos;
935 GList *l, *end = NULL;
937 if (parse->pending_buffers == NULL)
938 return GST_FLOW_OK; /* Nothing to push */
942 * 1 We get a buffer with no PCR -> it's the head of the list
943 * -> Do nothing, unless it's EOS
944 * 2 We get a buffer with a PCR, it's the first PCR we've seen, and belongs
945 * to the buffer at the head of the list
946 * -> Push any buffers in the list except the head,
947 * using a smoothing of their timestamps to land at the PCR
948 * -> store new PCR as the previous PCR, bytes_since_pcr = sizeof (buffer);
949 * 3 It's EOS (drain_all == TRUE, current_pcr == NONE)
950 * -> Push any buffers in the list using a smoothing of their timestamps
951 * starting at the previous PCR or first TS
952 * 4 We get a buffer with a PCR, and have a previous PCR
953 * -> If distance > smoothing_latency,
954 * output buffers except the last in the pending queue using
955 * piecewise-linear timestamps
956 * -> store new PCR as the previous PCR, bytes_since_pcr = sizeof (buffer);
960 if (!GST_CLOCK_TIME_IS_VALID (parse->current_pcr) && !drain_all)
963 if (GST_CLOCK_TIME_IS_VALID (parse->current_pcr)) {
964 pcr = parse->current_pcr;
965 parse->current_pcr = GST_CLOCK_TIME_NONE;
968 /* The bytes of the last buffer are after the PCR */
969 buffer = GST_BUFFER (g_list_nth_data (parse->pending_buffers, 0));
970 bytes_since_pcr = gst_buffer_get_size (buffer);
972 pcr_bytes = parse->bytes_since_pcr - bytes_since_pcr;
975 end = g_list_first (parse->pending_buffers);
978 if (!GST_CLOCK_TIME_IS_VALID (parse->previous_pcr)) {
979 pcr_diff = get_pending_timestamp_diff (parse);
981 /* Calculate the start_ts that ends at the end timestamp */
982 start_ts = GST_CLOCK_TIME_NONE;
984 start_ts = GST_BUFFER_PTS (GST_BUFFER (end->data));
985 if (start_ts > pcr_diff)
986 start_ts -= pcr_diff;
988 } else if (drain_all) { /* Case 3 */
989 start_ts = parse->previous_pcr;
990 pcr_diff = get_pending_timestamp_diff (parse);
991 } else { /* Case 4 */
992 start_ts = parse->previous_pcr;
993 if (GST_CLOCK_TIME_IS_VALID (pcr) && pcr > start_ts)
994 pcr_diff = GST_CLOCK_DIFF (start_ts, pcr);
996 /* Make sure PCR observations are sufficiently far apart */
997 if (drain_all == FALSE && pcr_diff < parse->smoothing_latency)
1001 GST_INFO_OBJECT (parse, "Pushing buffers - startTS %" GST_TIME_FORMAT
1002 " duration %" GST_TIME_FORMAT " %" G_GSIZE_FORMAT " bytes",
1003 GST_TIME_ARGS (start_ts), GST_TIME_ARGS (pcr_diff), pcr_bytes);
1005 /* Now, push buffers out pacing timestamps over pcr_diff time and pcr_bytes */
1007 l = g_list_last (parse->pending_buffers);
1010 GstClockTime out_ts = start_ts;
1012 buffer = gst_buffer_make_writable (GST_BUFFER (l->data));
1014 if (out_ts != GST_CLOCK_TIME_NONE && pcr_diff != GST_CLOCK_TIME_NONE &&
1016 out_ts += gst_util_uint64_scale (pcr_diff, pos, pcr_bytes);
1018 pos += gst_buffer_get_size (buffer);
1020 GST_DEBUG_OBJECT (parse,
1021 "InputTS %" GST_TIME_FORMAT " out %" GST_TIME_FORMAT,
1022 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)), GST_TIME_ARGS (out_ts));
1024 GST_BUFFER_PTS (buffer) = out_ts + parse->ts_offset;
1025 GST_BUFFER_DTS (buffer) = out_ts + parse->ts_offset;
1026 if (ret == GST_FLOW_OK) {
1028 enqueue_and_maybe_push_buffer (parse, parse->srcpad,
1029 &parse->ts_adapter, buffer);
1031 gst_buffer_unref (buffer);
1034 /* Free this list node and move to the next */
1035 p = g_list_previous (l);
1036 parse->pending_buffers = g_list_delete_link (parse->pending_buffers, l);
1040 if (parse->is_eos) {
1041 empty_adapter_into_pad (parse, &parse->ts_adapter, parse->srcpad);
1044 parse->pending_buffers = end;
1045 parse->bytes_since_pcr = bytes_since_pcr;
1046 parse->previous_pcr = pcr;
1050 static GstFlowReturn
1051 mpegts_parse_have_buffer (MpegTSBase * base, GstBuffer * buffer)
1053 MpegTSParse2 *parse = GST_MPEGTS_PARSE (base);
1054 GstFlowReturn ret = GST_FLOW_OK;
1056 GST_LOG_OBJECT (parse, "Received buffer %" GST_PTR_FORMAT, buffer);
1058 /* Assume all packets have equal size */
1059 if (parse->alignment > 0 &&
1060 base->packetizer->packet_size != MPEGTS_NORMAL_PACKETSIZE) {
1064 gst_buffer_map (buffer, &map, GST_MAP_READ);
1067 parse->header = GST_READ_UINT32_BE (data);
1068 gst_buffer_unmap (buffer, &map);
1071 if (parse->current_pcr != GST_CLOCK_TIME_NONE) {
1072 GST_DEBUG_OBJECT (parse,
1073 "InputTS %" GST_TIME_FORMAT " PCR %" GST_TIME_FORMAT,
1074 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
1075 GST_TIME_ARGS (parse->current_pcr));
1078 if (parse->set_timestamps || parse->first) {
1079 parse->pending_buffers = g_list_prepend (parse->pending_buffers, buffer);
1080 parse->bytes_since_pcr += gst_buffer_get_size (buffer);
1084 if (!prepare_src_pad (base, parse))
1087 if (parse->pending_buffers != NULL) {
1088 /* Don't keep pending_buffers if not setting output timestamps */
1089 gboolean drain_all = (parse->set_timestamps == FALSE);
1090 ret = drain_pending_buffers (parse, drain_all);
1091 if (ret != GST_FLOW_OK) {
1093 gst_buffer_unref (buffer);
1099 enqueue_and_maybe_push_buffer (parse, parse->srcpad,
1100 &parse->ts_adapter, buffer);
1105 empty_pad (GstPad * pad, MpegTSParse2 * parse)
1107 MpegTSParsePad *tspad = (MpegTSParsePad *) gst_pad_get_element_private (pad);
1109 ret = empty_adapter_into_pad (parse, &tspad->ts_adapter, tspad->pad);
1110 ret = gst_flow_combiner_update_flow (parse->flowcombiner, ret);
1113 static GstFlowReturn
1114 mpegts_parse_input_done (MpegTSBase * base)
1116 MpegTSParse2 *parse = GST_MPEGTS_PARSE (base);
1117 GstFlowReturn ret = GST_FLOW_OK;
1119 if (!prepare_src_pad (base, parse))
1122 if (parse->alignment == 0) {
1123 ret = empty_adapter_into_pad (parse, &parse->ts_adapter, parse->srcpad);
1124 ret = gst_flow_combiner_update_flow (parse->flowcombiner, ret);
1125 g_list_foreach (parse->srcpads, (GFunc) empty_pad, parse);
1130 static MpegTSParsePad *
1131 find_pad_for_program (MpegTSParse2 * parse, guint program_number)
1135 for (tmp = parse->srcpads; tmp; tmp = tmp->next) {
1136 MpegTSParsePad *tspad = gst_pad_get_element_private ((GstPad *) tmp->data);
1138 if (tspad->program_number == program_number)
1146 mpegts_parse_program_started (MpegTSBase * base, MpegTSBaseProgram * program)
1148 MpegTSParse2 *parse = GST_MPEGTS_PARSE (base);
1149 MpegTSParseProgram *parseprogram = (MpegTSParseProgram *) program;
1150 MpegTSParsePad *tspad;
1152 /* If we have a request pad for that program, activate it */
1153 tspad = find_pad_for_program (parse, program->program_number);
1156 tspad->program = parseprogram;
1157 parseprogram->tspad = tspad;
1162 mpegts_parse_program_stopped (MpegTSBase * base, MpegTSBaseProgram * program)
1164 MpegTSParse2 *parse = GST_MPEGTS_PARSE (base);
1165 MpegTSParseProgram *parseprogram = (MpegTSParseProgram *) program;
1166 MpegTSParsePad *tspad;
1168 /* If we have a request pad for that program, activate it */
1169 tspad = find_pad_for_program (parse, program->program_number);
1172 tspad->program = NULL;
1173 parseprogram->tspad = NULL;
1176 parse->pcr_pid = -1;
1177 parse->ts_offset += parse->current_pcr - parse->base_pcr;
1178 parse->base_pcr = GST_CLOCK_TIME_NONE;
1182 mpegts_parse_src_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1184 MpegTSParse2 *parse = GST_MPEGTS_PARSE (parent);
1187 switch (GST_QUERY_TYPE (query)) {
1188 case GST_QUERY_LATENCY:
1190 if ((res = gst_pad_peer_query (((MpegTSBase *) parse)->sinkpad, query))) {
1192 GstClockTime min_latency, max_latency;
1194 gst_query_parse_latency (query, &is_live, &min_latency, &max_latency);
1196 GstClockTime extra_latency = TS_LATENCY * GST_MSECOND;
1197 if (parse->set_timestamps) {
1198 extra_latency = MAX (extra_latency, parse->smoothing_latency);
1200 min_latency += extra_latency;
1201 if (max_latency != GST_CLOCK_TIME_NONE)
1202 max_latency += extra_latency;
1205 gst_query_set_latency (query, is_live, min_latency, max_latency);
1210 res = gst_pad_query_default (pad, parent, query);