2 * Copyright 2006, 2007, 2008, 2009, 2010 Fluendo S.A.
3 * Authors: Jan Schmidt <jan@fluendo.com>
4 * Kapil Agrawal <kapil@fluendo.com>
5 * Julien Moutte <julien@fluendo.com>
7 * Copyright (C) 2011 Jan Schmidt <thaytan@noraisin.net>
9 * This library is licensed under 4 different licenses and you
10 * can choose to use it under the terms of any one of them. The
11 * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
16 * The contents of this file are subject to the Mozilla Public License
17 * Version 1.1 (the "License"); you may not use this file except in
18 * compliance with the License. You may obtain a copy of the License at
19 * http://www.mozilla.org/MPL/.
21 * Software distributed under the License is distributed on an "AS IS"
22 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
23 * License for the specific language governing rights and limitations
28 * This library is free software; you can redistribute it and/or
29 * modify it under the terms of the GNU Library General Public
30 * License as published by the Free Software Foundation; either
31 * version 2 of the License, or (at your option) any later version.
33 * This library is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 * Library General Public License for more details.
38 * You should have received a copy of the GNU Library General Public
39 * License along with this library; if not, write to the
40 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
41 * Boston, MA 02110-1301, USA.
45 * This program is free software; you can redistribute it and/or modify
46 * it under the terms of the GNU General Public License as published by
47 * the Free Software Foundation; either version 2 of the License, or
48 * (at your option) any later version.
50 * This program is distributed in the hope that it will be useful,
51 * but WITHOUT ANY WARRANTY; without even the implied warranty of
52 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 * GNU General Public License for more details.
55 * You should have received a copy of the GNU General Public License
56 * along with this program; if not, write to the Free Software
57 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
61 * Unless otherwise indicated, Source Code is licensed under MIT license.
62 * See further explanation attached in License Statement (distributed in the file
65 * Permission is hereby granted, free of charge, to any person obtaining a copy of
66 * this software and associated documentation files (the "Software"), to deal in
67 * the Software without restriction, including without limitation the rights to
68 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
69 * of the Software, and to permit persons to whom the Software is furnished to do
70 * so, subject to the following conditions:
72 * The above copyright notice and this permission notice shall be included in all
73 * copies or substantial portions of the Software.
75 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
76 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
77 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
78 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
79 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
80 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
91 #include <gst/tag/tag.h>
92 #include <gst/video/video.h>
93 #include <gst/mpegts/mpegts.h>
94 #include <gst/pbutils/pbutils.h>
96 #include "mpegtsmux.h"
98 #include "mpegtsmux_aac.h"
99 #include "mpegtsmux_ttxt.h"
100 #include "mpegtsmux_opus.h"
101 #include "mpegtsmux_jpeg2000.h"
102 #include <gst/videoparsers/gstjpeg2000parse.h>
103 #include <gst/video/video-color.h>
105 GST_DEBUG_CATEGORY (mpegtsmux_debug);
106 #define GST_CAT_DEFAULT mpegtsmux_debug
108 #define COLLECT_DATA_PAD(collect_data) (((GstCollectData *)(collect_data))->pad)
121 #define MPEGTSMUX_DEFAULT_ALIGNMENT -1
122 #define MPEGTSMUX_DEFAULT_M2TS FALSE
124 static GstStaticPadTemplate mpegtsmux_sink_factory =
125 GST_STATIC_PAD_TEMPLATE ("sink_%d",
128 GST_STATIC_CAPS ("video/mpeg, "
129 "parsed = (boolean) TRUE, "
130 "mpegversion = (int) { 1, 2, 4 }, "
131 "systemstream = (boolean) false; "
134 "video/x-h264,stream-format=(string)byte-stream,"
135 "alignment=(string){au, nal}; "
136 "video/x-h265,stream-format=(string)byte-stream,"
137 "alignment=(string){au, nal}; "
139 "parsed = (boolean) TRUE, "
140 "mpegversion = (int) { 1, 2 };"
142 "framed = (boolean) TRUE, "
143 "mpegversion = (int) 4, stream-format = (string) adts;"
145 "mpegversion = (int) 4, stream-format = (string) raw;"
147 "width = (int) { 16, 20, 24 }, "
148 "rate = (int) { 48000, 96000 }, "
149 "channels = (int) [ 1, 8 ], "
150 "dynamic_range = (int) [ 0, 255 ], "
151 "emphasis = (boolean) { FALSE, TRUE }, "
152 "mute = (boolean) { FALSE, TRUE }; "
153 "audio/x-ac3, framed = (boolean) TRUE;"
154 "audio/x-dts, framed = (boolean) TRUE;"
156 "channels = (int) [1, 8], "
157 "channel-mapping-family = (int) {0, 1};"
158 "subpicture/x-dvb; application/x-teletext; meta/x-klv, parsed=true;"
159 "image/x-jpc, profile = (int)[0, 49151];"));
161 static GstStaticPadTemplate mpegtsmux_src_factory =
162 GST_STATIC_PAD_TEMPLATE ("src",
165 GST_STATIC_CAPS ("video/mpegts, "
166 "systemstream = (boolean) true, " "packetsize = (int) { 188, 192} ")
169 static void gst_mpegtsmux_set_property (GObject * object, guint prop_id,
170 const GValue * value, GParamSpec * pspec);
171 static void gst_mpegtsmux_get_property (GObject * object, guint prop_id,
172 GValue * value, GParamSpec * pspec);
174 static void mpegtsmux_reset (MpegTsMux * mux, gboolean alloc);
175 static void mpegtsmux_dispose (GObject * object);
176 static void alloc_packet_cb (GstBuffer ** _buf, void *user_data);
177 static gboolean new_packet_cb (GstBuffer * buf, void *user_data,
179 static void release_buffer_cb (guint8 * data, void *user_data);
180 static GstFlowReturn mpegtsmux_collect_packet (MpegTsMux * mux,
182 static GstFlowReturn mpegtsmux_push_packets (MpegTsMux * mux, gboolean force);
183 static gboolean new_packet_m2ts (MpegTsMux * mux, GstBuffer * buf,
186 static void mpegtsmux_prepare_srcpad (MpegTsMux * mux);
187 GstFlowReturn mpegtsmux_clip_inc_running_time (GstCollectPads * pads,
188 GstCollectData * cdata, GstBuffer * buf, GstBuffer ** outbuf,
190 static GstFlowReturn mpegtsmux_collected_buffer (GstCollectPads * pads,
191 GstCollectData * data, GstBuffer * buf, MpegTsMux * mux);
193 static gboolean mpegtsmux_sink_event (GstCollectPads * pads,
194 GstCollectData * data, GstEvent * event, gpointer user_data);
195 static GstPad *mpegtsmux_request_new_pad (GstElement * element,
196 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
197 static void mpegtsmux_release_pad (GstElement * element, GstPad * pad);
198 static GstStateChangeReturn mpegtsmux_change_state (GstElement * element,
199 GstStateChange transition);
200 static gboolean mpegtsmux_send_event (GstElement * element, GstEvent * event);
201 static void mpegtsmux_set_header_on_caps (MpegTsMux * mux);
202 static gboolean mpegtsmux_src_event (GstPad * pad, GstObject * parent,
206 static void mpegtsmux_set_index (GstElement * element, GstIndex * index);
207 static GstIndex *mpegtsmux_get_index (GstElement * element);
209 static GstFormat pts_format;
210 static GstFormat spn_format;
219 G_DEFINE_TYPE (MpegTsMux, mpegtsmux, GST_TYPE_ELEMENT)
221 /* Takes over the ref on the buffer */
222 static StreamData *stream_data_new (GstBuffer * buffer)
224 StreamData *res = g_new (StreamData, 1);
225 res->buffer = buffer;
226 gst_buffer_map (buffer, &(res->map_info), GST_MAP_READ);
232 stream_data_free (StreamData * data)
235 gst_buffer_unmap (data->buffer, &data->map_info);
236 gst_buffer_unref (data->buffer);
241 #define parent_class mpegtsmux_parent_class
244 mpegtsmux_class_init (MpegTsMuxClass * klass)
246 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
247 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
249 gst_element_class_add_static_pad_template (gstelement_class,
250 &mpegtsmux_sink_factory);
251 gst_element_class_add_static_pad_template (gstelement_class,
252 &mpegtsmux_src_factory);
254 gst_element_class_set_static_metadata (gstelement_class,
255 "MPEG Transport Stream Muxer", "Codec/Muxer",
256 "Multiplexes media streams into an MPEG Transport Stream",
257 "Fluendo <contact@fluendo.com>");
259 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_mpegtsmux_set_property);
260 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_mpegtsmux_get_property);
261 gobject_class->dispose = mpegtsmux_dispose;
263 gstelement_class->request_new_pad = mpegtsmux_request_new_pad;
264 gstelement_class->release_pad = mpegtsmux_release_pad;
265 gstelement_class->change_state = mpegtsmux_change_state;
266 gstelement_class->send_event = mpegtsmux_send_event;
269 gstelement_class->set_index = GST_DEBUG_FUNCPTR (mpegtsmux_set_index);
270 gstelement_class->get_index = GST_DEBUG_FUNCPTR (mpegtsmux_get_index);
273 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PROG_MAP,
274 g_param_spec_boxed ("prog-map", "Program map",
275 "A GstStructure specifies the mapping from elementary streams to programs",
276 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
278 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_M2TS_MODE,
279 g_param_spec_boolean ("m2ts-mode", "M2TS(192 bytes) Mode",
280 "Set to TRUE to output Blu-Ray disc format with 192 byte packets. "
281 "FALSE for standard TS format with 188 byte packets.",
282 MPEGTSMUX_DEFAULT_M2TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PAT_INTERVAL,
285 g_param_spec_uint ("pat-interval", "PAT interval",
286 "Set the interval (in ticks of the 90kHz clock) for writing out the PAT table",
287 1, G_MAXUINT, TSMUX_DEFAULT_PAT_INTERVAL,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PMT_INTERVAL,
291 g_param_spec_uint ("pmt-interval", "PMT interval",
292 "Set the interval (in ticks of the 90kHz clock) for writing out the PMT table",
293 1, G_MAXUINT, TSMUX_DEFAULT_PMT_INTERVAL,
294 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ALIGNMENT,
297 g_param_spec_int ("alignment", "packet alignment",
298 "Number of packets per buffer (padded with dummy packets on EOS) "
299 "(-1 = auto, 0 = all available packets, 7 for UDP streaming)",
300 -1, G_MAXINT, MPEGTSMUX_DEFAULT_ALIGNMENT,
301 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SI_INTERVAL,
304 g_param_spec_uint ("si-interval", "SI interval",
305 "Set the interval (in ticks of the 90kHz clock) for writing out the Service"
306 "Information tables", 1, G_MAXUINT, TSMUX_DEFAULT_SI_INTERVAL,
307 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 mpegtsmux_init (MpegTsMux * mux)
314 gst_pad_new_from_static_template (&mpegtsmux_src_factory, "src");
315 gst_pad_use_fixed_caps (mux->srcpad);
316 gst_pad_set_event_function (mux->srcpad,
317 GST_DEBUG_FUNCPTR (mpegtsmux_src_event));
318 gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);
320 mux->collect = gst_collect_pads_new ();
321 gst_collect_pads_set_buffer_function (mux->collect,
322 (GstCollectPadsBufferFunction)
323 GST_DEBUG_FUNCPTR (mpegtsmux_collected_buffer), mux);
325 gst_collect_pads_set_event_function (mux->collect,
326 (GstCollectPadsEventFunction) GST_DEBUG_FUNCPTR (mpegtsmux_sink_event),
328 gst_collect_pads_set_clip_function (mux->collect, (GstCollectPadsClipFunction)
329 GST_DEBUG_FUNCPTR (mpegtsmux_clip_inc_running_time), mux);
331 mux->adapter = gst_adapter_new ();
332 mux->out_adapter = gst_adapter_new ();
335 mux->m2ts_mode = MPEGTSMUX_DEFAULT_M2TS;
336 mux->pat_interval = TSMUX_DEFAULT_PAT_INTERVAL;
337 mux->pmt_interval = TSMUX_DEFAULT_PMT_INTERVAL;
338 mux->si_interval = TSMUX_DEFAULT_SI_INTERVAL;
339 mux->prog_map = NULL;
340 mux->alignment = MPEGTSMUX_DEFAULT_ALIGNMENT;
343 mpegtsmux_reset (mux, TRUE);
347 mpegtsmux_pad_reset (MpegTsPadData * pad_data)
349 pad_data->dts = GST_CLOCK_STIME_NONE;
350 pad_data->prog_id = -1;
352 pad_data->prog_id = -1;
353 pad_data->element_index_writer_id = -1;
356 if (pad_data->free_func)
357 pad_data->free_func (pad_data->prepare_data);
358 pad_data->prepare_data = NULL;
359 pad_data->prepare_func = NULL;
360 pad_data->free_func = NULL;
362 if (pad_data->codec_data)
363 gst_buffer_replace (&pad_data->codec_data, NULL);
365 /* reference owned elsewhere */
366 pad_data->stream = NULL;
367 pad_data->prog = NULL;
369 if (pad_data->language) {
370 g_free (pad_data->language);
371 pad_data->language = NULL;
377 mpegtsmux_reset (MpegTsMux * mux, gboolean alloc)
383 mux->last_flow_ret = GST_FLOW_OK;
384 mux->previous_pcr = -1;
385 mux->previous_offset = 0;
386 mux->pcr_rate_num = mux->pcr_rate_den = 1;
388 mux->is_delta = TRUE;
389 mux->is_header = FALSE;
391 mux->streamheader_sent = FALSE;
392 mux->pending_key_unit_ts = GST_CLOCK_TIME_NONE;
393 gst_event_replace (&mux->force_key_unit_event, NULL);
397 if (mux->element_index) {
398 gst_object_unref (mux->element_index);
399 mux->element_index = NULL;
403 gst_adapter_clear (mux->adapter);
404 if (mux->out_adapter)
405 gst_adapter_clear (mux->out_adapter);
408 tsmux_free (mux->tsmux);
413 g_hash_table_destroy (mux->programs);
415 mux->programs = g_hash_table_new (g_direct_hash, g_direct_equal);
417 while ((buf = g_queue_pop_head (&mux->streamheader)))
418 gst_buffer_unref (buf);
420 gst_event_replace (&mux->force_key_unit_event, NULL);
421 gst_buffer_replace (&mux->out_buffer, NULL);
424 GST_COLLECT_PADS_STREAM_LOCK (mux->collect);
425 for (walk = mux->collect->data; walk != NULL; walk = g_slist_next (walk))
426 mpegtsmux_pad_reset ((MpegTsPadData *) walk->data);
427 GST_COLLECT_PADS_STREAM_UNLOCK (mux->collect);
431 mux->tsmux = tsmux_new ();
432 tsmux_set_write_func (mux->tsmux, new_packet_cb, mux);
433 tsmux_set_alloc_func (mux->tsmux, alloc_packet_cb, mux);
438 mpegtsmux_dispose (GObject * object)
440 MpegTsMux *mux = GST_MPEG_TSMUX (object);
442 mpegtsmux_reset (mux, FALSE);
445 g_object_unref (mux->adapter);
448 if (mux->out_adapter) {
449 g_object_unref (mux->out_adapter);
450 mux->out_adapter = NULL;
453 gst_object_unref (mux->collect);
457 gst_structure_free (mux->prog_map);
458 mux->prog_map = NULL;
461 g_hash_table_destroy (mux->programs);
462 mux->programs = NULL;
464 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
468 gst_mpegtsmux_set_property (GObject * object, guint prop_id,
469 const GValue * value, GParamSpec * pspec)
471 MpegTsMux *mux = GST_MPEG_TSMUX (object);
476 /*set incase if the output stream need to be of 192 bytes */
477 mux->m2ts_mode = g_value_get_boolean (value);
481 const GstStructure *s = gst_value_get_structure (value);
483 gst_structure_free (mux->prog_map);
486 mux->prog_map = gst_structure_copy (s);
488 mux->prog_map = NULL;
491 case PROP_PAT_INTERVAL:
492 mux->pat_interval = g_value_get_uint (value);
494 tsmux_set_pat_interval (mux->tsmux, mux->pat_interval);
496 case PROP_PMT_INTERVAL:
497 walk = mux->collect->data;
498 mux->pmt_interval = g_value_get_uint (value);
501 MpegTsPadData *ts_data = (MpegTsPadData *) walk->data;
503 tsmux_set_pmt_interval (ts_data->prog, mux->pmt_interval);
504 walk = g_slist_next (walk);
508 mux->alignment = g_value_get_int (value);
510 case PROP_SI_INTERVAL:
511 mux->si_interval = g_value_get_uint (value);
512 tsmux_set_si_interval (mux->tsmux, mux->si_interval);
515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
521 gst_mpegtsmux_get_property (GObject * object, guint prop_id,
522 GValue * value, GParamSpec * pspec)
524 MpegTsMux *mux = GST_MPEG_TSMUX (object);
528 g_value_set_boolean (value, mux->m2ts_mode);
531 gst_value_set_structure (value, mux->prog_map);
533 case PROP_PAT_INTERVAL:
534 g_value_set_uint (value, mux->pat_interval);
536 case PROP_PMT_INTERVAL:
537 g_value_set_uint (value, mux->pmt_interval);
540 g_value_set_int (value, mux->alignment);
542 case PROP_SI_INTERVAL:
543 g_value_set_uint (value, mux->si_interval);
546 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
553 mpegtsmux_set_index (GstElement * element, GstIndex * index)
555 MpegTsMux *mux = GST_MPEG_TSMUX (element);
557 GST_OBJECT_LOCK (mux);
558 if (mux->element_index)
559 gst_object_unref (mux->element_index);
560 mux->element_index = index ? gst_object_ref (index) : NULL;
561 GST_OBJECT_UNLOCK (mux);
563 GST_DEBUG_OBJECT (mux, "Set index %" GST_PTR_FORMAT, mux->element_index);
567 mpegtsmux_get_index (GstElement * element)
569 GstIndex *result = NULL;
570 MpegTsMux *mux = GST_MPEG_TSMUX (element);
572 GST_OBJECT_LOCK (mux);
573 if (mux->element_index)
574 result = gst_object_ref (mux->element_index);
575 GST_OBJECT_UNLOCK (mux);
577 GST_DEBUG_OBJECT (mux, "Returning index %" GST_PTR_FORMAT, result);
584 release_buffer_cb (guint8 * data, void *user_data)
586 stream_data_free (user_data);
590 mpegtsmux_create_stream (MpegTsMux * mux, MpegTsPadData * ts_data)
592 GstFlowReturn ret = GST_FLOW_ERROR;
596 TsMuxStreamType st = TSMUX_ST_RESERVED;
598 const GValue *value = NULL;
599 GstBuffer *codec_data = NULL;
600 guint8 opus_channel_config_code = 0;
602 guint8 main_level = 0;
603 guint32 max_rate = 0;
604 guint8 color_spec = 0;
605 j2k_private_data *private_data = NULL;
607 pad = ts_data->collect.pad;
608 caps = gst_pad_get_current_caps (pad);
612 GST_DEBUG_OBJECT (pad, "Creating stream with PID 0x%04x for caps %"
613 GST_PTR_FORMAT, ts_data->pid, caps);
615 s = gst_caps_get_structure (caps, 0);
617 mt = gst_structure_get_name (s);
618 value = gst_structure_get_value (s, "codec_data");
620 codec_data = gst_value_get_buffer (value);
622 if (strcmp (mt, "video/x-dirac") == 0) {
623 st = TSMUX_ST_VIDEO_DIRAC;
624 } else if (strcmp (mt, "audio/x-ac3") == 0) {
625 st = TSMUX_ST_PS_AUDIO_AC3;
626 } else if (strcmp (mt, "audio/x-dts") == 0) {
627 st = TSMUX_ST_PS_AUDIO_DTS;
628 } else if (strcmp (mt, "audio/x-lpcm") == 0) {
629 st = TSMUX_ST_PS_AUDIO_LPCM;
630 } else if (strcmp (mt, "video/x-h264") == 0) {
631 st = TSMUX_ST_VIDEO_H264;
632 } else if (strcmp (mt, "video/x-h265") == 0) {
633 st = TSMUX_ST_VIDEO_HEVC;
634 } else if (strcmp (mt, "audio/mpeg") == 0) {
637 if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
638 GST_ERROR_OBJECT (pad, "caps missing mpegversion");
642 switch (mpegversion) {
644 st = TSMUX_ST_AUDIO_MPEG1;
647 st = TSMUX_ST_AUDIO_MPEG2;
651 st = TSMUX_ST_AUDIO_AAC;
652 if (codec_data) { /* TODO - Check stream format - codec data should only come with RAW stream */
653 GST_DEBUG_OBJECT (pad,
654 "we have additional codec data (%" G_GSIZE_FORMAT " bytes)",
655 gst_buffer_get_size (codec_data));
656 ts_data->codec_data = gst_buffer_ref (codec_data);
657 ts_data->prepare_func = mpegtsmux_prepare_aac;
659 ts_data->codec_data = NULL;
664 GST_WARNING_OBJECT (pad, "unsupported mpegversion %d", mpegversion);
667 } else if (strcmp (mt, "video/mpeg") == 0) {
670 if (!gst_structure_get_int (s, "mpegversion", &mpegversion)) {
671 GST_ERROR_OBJECT (pad, "caps missing mpegversion");
675 switch (mpegversion) {
677 st = TSMUX_ST_VIDEO_MPEG1;
680 st = TSMUX_ST_VIDEO_MPEG2;
683 st = TSMUX_ST_VIDEO_MPEG4;
686 GST_WARNING_OBJECT (pad, "unsupported mpegversion %d", mpegversion);
689 } else if (strcmp (mt, "subpicture/x-dvb") == 0) {
690 st = TSMUX_ST_PS_DVB_SUBPICTURE;
691 } else if (strcmp (mt, "application/x-teletext") == 0) {
692 st = TSMUX_ST_PS_TELETEXT;
693 /* needs a particularly sized layout */
694 ts_data->prepare_func = mpegtsmux_prepare_teletext;
695 } else if (strcmp (mt, "audio/x-opus") == 0) {
696 guint8 channels, mapping_family, stream_count, coupled_count;
697 guint8 channel_mapping[256];
699 if (!gst_codec_utils_opus_parse_caps (caps, NULL, &channels,
700 &mapping_family, &stream_count, &coupled_count, channel_mapping)) {
701 GST_ERROR_OBJECT (pad, "Incomplete Opus caps");
705 if (channels <= 2 && mapping_family == 0) {
706 opus_channel_config_code = channels;
707 } else if (channels == 2 && mapping_family == 255 && stream_count == 1
708 && coupled_count == 1) {
710 opus_channel_config_code = 0;
711 } else if (channels >= 2 && channels <= 8 && mapping_family == 1) {
712 static const guint8 coupled_stream_counts[9] = {
713 1, 0, 1, 1, 2, 2, 2, 3, 3
715 static const guint8 channel_map_a[8][8] = {
722 {0, 4, 1, 2, 3, 5, 6},
723 {0, 6, 1, 2, 3, 4, 5, 7},
725 static const guint8 channel_map_b[8][8] = {
732 {0, 1, 2, 3, 4, 5, 6},
733 {0, 1, 2, 3, 4, 5, 6, 7},
737 if (stream_count == channels - coupled_stream_counts[channels] &&
738 coupled_count == coupled_stream_counts[channels] &&
739 memcmp (channel_mapping, channel_map_a[channels - 1],
741 opus_channel_config_code = channels;
742 } else if (stream_count == channels - coupled_stream_counts[channels] &&
743 coupled_count == coupled_stream_counts[channels] &&
744 memcmp (channel_mapping, channel_map_b[channels - 1],
746 opus_channel_config_code = channels | 0x80;
748 GST_FIXME_OBJECT (pad, "Opus channel mapping not handled");
753 st = TSMUX_ST_PS_OPUS;
754 ts_data->prepare_func = mpegtsmux_prepare_opus;
755 } else if (strcmp (mt, "meta/x-klv") == 0) {
756 st = TSMUX_ST_PS_KLV;
757 } else if (strcmp (mt, "image/x-jpc") == 0) {
759 * See this document for more details on standard:
761 * https://www.itu.int/rec/T-REC-H.222.0-201206-S/en
762 * Annex S describes J2K details
763 * Page 104 of this document describes J2k video descriptor
766 const GValue *vProfile = gst_structure_get_value (s, "profile");
767 const GValue *vMainlevel = gst_structure_get_value (s, "main-level");
768 const GValue *vFramerate = gst_structure_get_value (s, "framerate");
769 const GValue *vColorimetry = gst_structure_get_value (s, "colorimetry");
770 private_data = g_new0 (j2k_private_data, 1);
771 profile = g_value_get_uint (vProfile);
772 if (profile != GST_JPEG2000_PARSE_PROFILE_BC_SINGLE) {
773 /* for now, we will relax the condition that the profile must equal GST_JPEG2000_PARSE_PROFILE_BC_SINGLE */
774 /*GST_ERROR_OBJECT (pad, "Invalid JPEG 2000 profile %d", profile);
775 goto not_negotiated; */
777 /* for now, we will relax the condition that the main level must be present */
779 main_level = g_value_get_uint (vMainlevel);
780 if (main_level > 11) {
781 GST_ERROR_OBJECT (pad, "Invalid main level %d", main_level);
784 if (main_level >= 6) {
785 max_rate = 2 ^ (main_level - 6) * 1600 * 1000000;
787 switch (main_level) {
792 max_rate = 200 * 1000000;
795 max_rate = 400 * 1000000;
798 max_rate = 800 * 1000000;
805 /*GST_ERROR_OBJECT (pad, "Missing main level");
806 goto not_negotiated; */
808 /* We always mux video in J2K-over-MPEG-TS non-interlaced mode */
809 private_data->interlace = FALSE;
810 private_data->den = 0;
811 private_data->num = 0;
812 private_data->max_bitrate = max_rate;
813 private_data->color_spec = 1;
814 /* these two fields are not used, since we always mux as non-interlaced */
815 private_data->Fic = 1;
816 private_data->Fio = 0;
819 if (vFramerate != NULL) {
820 /* Data for ELSM header */
821 private_data->num = gst_value_get_fraction_numerator (vFramerate);
822 private_data->den = gst_value_get_fraction_denominator (vFramerate);
824 /* Get Colorimetry */
826 const char *colorimetry = g_value_get_string (vColorimetry);
827 color_spec = GST_MPEGTS_JPEG2000_COLORSPEC_SRGB; /* RGB as default */
828 if (g_str_equal (colorimetry, GST_VIDEO_COLORIMETRY_BT601)) {
829 color_spec = GST_MPEGTS_JPEG2000_COLORSPEC_REC601;
831 if (g_str_equal (colorimetry, GST_VIDEO_COLORIMETRY_BT709)
832 || g_str_equal (colorimetry, GST_VIDEO_COLORIMETRY_SMPTE240M)) {
833 color_spec = GST_MPEGTS_JPEG2000_COLORSPEC_REC709;
836 private_data->color_spec = color_spec;
838 GST_ERROR_OBJECT (pad, "Colorimetry not present in caps");
841 st = TSMUX_ST_VIDEO_JP2K;
842 ts_data->prepare_func = mpegtsmux_prepare_jpeg2000;
843 ts_data->prepare_data = private_data;
844 ts_data->free_func = mpegtsmux_free_jpeg2000;
847 if (st != TSMUX_ST_RESERVED) {
848 ts_data->stream = tsmux_create_stream (mux->tsmux, st, ts_data->pid,
851 GST_DEBUG_OBJECT (pad, "Failed to determine stream type");
854 if (ts_data->stream != NULL) {
855 const char *interlace_mode = gst_structure_get_string (s, "interlace-mode");
856 gst_structure_get_int (s, "rate", &ts_data->stream->audio_sampling);
857 gst_structure_get_int (s, "channels", &ts_data->stream->audio_channels);
858 gst_structure_get_int (s, "bitrate", &ts_data->stream->audio_bitrate);
861 gst_structure_get_fraction (s, "framerate", &ts_data->stream->num,
862 &ts_data->stream->den);
865 ts_data->stream->interlace_mode = FALSE;
866 if (interlace_mode) {
867 ts_data->stream->interlace_mode =
868 g_str_equal (interlace_mode, "interleaved");
870 /* Width and Height */
871 gst_structure_get_int (s, "width", &ts_data->stream->horizontal_size);
872 gst_structure_get_int (s, "height", &ts_data->stream->vertical_size);
874 ts_data->stream->color_spec = color_spec;
875 ts_data->stream->max_bitrate = max_rate;
876 ts_data->stream->profile_and_level = profile | main_level;
878 ts_data->stream->opus_channel_config_code = opus_channel_config_code;
880 tsmux_stream_set_buffer_release_func (ts_data->stream, release_buffer_cb);
881 tsmux_program_add_stream (ts_data->prog, ts_data->stream);
886 GST_OBJECT_LOCK (mux);
887 if (mux->element_index) {
888 gboolean parsed = FALSE;
890 if (ts_data->stream->is_video_stream) {
891 if (gst_structure_get_boolean (s, "parsed", &parsed) && parsed) {
892 if (ts_data->element_index_writer_id == -1) {
893 gst_index_get_writer_id (mux->element_index, GST_OBJECT (mux),
894 &ts_data->element_index_writer_id);
895 GST_DEBUG_OBJECT (mux, "created GstIndex writer_id = %d for stream",
896 ts_data->element_index_writer_id);
897 gst_index_add_format (mux->element_index,
898 ts_data->element_index_writer_id, pts_format);
899 gst_index_add_format (mux->element_index,
900 ts_data->element_index_writer_id, spn_format);
903 GST_WARNING_OBJECT (pad, "no indexing for (unparsed) stream !");
907 GST_OBJECT_UNLOCK (mux);
909 gst_caps_unref (caps);
914 g_free (private_data);
915 GST_DEBUG_OBJECT (pad, "Sink pad caps were not set before pushing");
917 gst_caps_unref (caps);
918 return GST_FLOW_NOT_NEGOTIATED;
923 mpegtsmux_create_streams (MpegTsMux * mux)
925 GstFlowReturn ret = GST_FLOW_OK;
926 GSList *walk = mux->collect->data;
928 /* Create the streams */
930 GstCollectData *c_data = (GstCollectData *) walk->data;
931 MpegTsPadData *ts_data = (MpegTsPadData *) walk->data;
934 walk = g_slist_next (walk);
936 if (ts_data->prog_id == -1) {
937 name = GST_PAD_NAME (c_data->pad);
938 if (mux->prog_map != NULL && gst_structure_has_field (mux->prog_map,
941 gboolean ret = gst_structure_get_int (mux->prog_map, name, &idx);
943 GST_ELEMENT_ERROR (mux, STREAM, MUX,
944 ("Reading program map failed. Assuming default"), (NULL));
945 idx = DEFAULT_PROG_ID;
948 GST_DEBUG_OBJECT (mux, "Program number %d associate with pad %s less "
949 "than zero; DEFAULT_PROGRAM = %d is used instead",
950 idx, name, DEFAULT_PROG_ID);
951 idx = DEFAULT_PROG_ID;
953 ts_data->prog_id = idx;
955 ts_data->prog_id = DEFAULT_PROG_ID;
960 g_hash_table_lookup (mux->programs, GINT_TO_POINTER (ts_data->prog_id));
961 if (ts_data->prog == NULL) {
962 ts_data->prog = tsmux_program_new (mux->tsmux, ts_data->prog_id);
963 if (ts_data->prog == NULL)
965 tsmux_set_pmt_interval (ts_data->prog, mux->pmt_interval);
966 g_hash_table_insert (mux->programs,
967 GINT_TO_POINTER (ts_data->prog_id), ts_data->prog);
969 /* Take the first stream of the program for the PCR */
970 GST_DEBUG_OBJECT (COLLECT_DATA_PAD (ts_data),
971 "Use stream (pid=%d) from pad as PCR for program (prog_id = %d)",
972 ts_data->pid, ts_data->prog_id);
974 tsmux_program_set_pcr_stream (ts_data->prog, ts_data->stream);
977 if (ts_data->stream == NULL) {
978 ret = mpegtsmux_create_stream (mux, ts_data);
979 if (ret != GST_FLOW_OK)
989 GST_ELEMENT_ERROR (mux, STREAM, MUX,
990 ("Could not create new program"), (NULL));
991 return GST_FLOW_ERROR;
995 GST_ELEMENT_ERROR (mux, STREAM, MUX,
996 ("Could not create handler for stream"), (NULL));
1002 mpegtsmux_sink_event (GstCollectPads * pads, GstCollectData * data,
1003 GstEvent * event, gpointer user_data)
1005 MpegTsMux *mux = GST_MPEG_TSMUX (user_data);
1006 gboolean res = FALSE;
1007 gboolean forward = TRUE;
1008 MpegTsPadData *pad_data = (MpegTsPadData *) data;
1010 #ifndef GST_DISABLE_GST_DEBUG
1016 switch (GST_EVENT_TYPE (event)) {
1017 case GST_EVENT_CUSTOM_DOWNSTREAM:
1019 GstClockTime timestamp, stream_time, running_time;
1020 gboolean all_headers;
1023 if (!gst_video_event_is_force_key_unit (event))
1029 gst_video_event_parse_downstream_force_key_unit (event,
1030 ×tamp, &stream_time, &running_time, &all_headers, &count);
1031 GST_INFO_OBJECT (pad, "have downstream force-key-unit event, "
1032 "seqnum %d, running-time %" GST_TIME_FORMAT " count %d",
1033 gst_event_get_seqnum (event), GST_TIME_ARGS (running_time), count);
1035 if (mux->force_key_unit_event != NULL) {
1036 GST_INFO_OBJECT (mux, "skipping downstream force key unit event "
1037 "as an upstream force key unit is already queued");
1044 mux->pending_key_unit_ts = running_time;
1045 gst_event_replace (&mux->force_key_unit_event, event);
1048 case GST_EVENT_TAG:{
1052 GST_DEBUG_OBJECT (mux, "received tag event");
1053 gst_event_parse_tag (event, &list);
1055 /* Matroska wants ISO 639-2B code, taglist most likely contains 639-1 */
1056 if (gst_tag_list_get_string (list, GST_TAG_LANGUAGE_CODE, &lang)) {
1057 const gchar *lang_code;
1059 lang_code = gst_tag_get_language_code_iso_639_2B (lang);
1061 GST_DEBUG_OBJECT (pad, "Setting language to '%s'", lang_code);
1063 g_free (pad_data->language);
1064 pad_data->language = g_strdup (lang_code);
1066 GST_WARNING_OBJECT (pad, "Did not get language code for '%s'", lang);
1071 /* handled this, don't want collectpads to forward it downstream */
1073 forward = gst_tag_list_get_scope (list) == GST_TAG_SCOPE_GLOBAL;
1076 case GST_EVENT_STREAM_START:{
1077 GstStreamFlags flags;
1079 gst_event_parse_stream_flags (event, &flags);
1081 /* Don't wait for data on sparse inputs like metadata streams */
1082 if ((flags & GST_STREAM_FLAG_SPARSE)) {
1083 GST_COLLECT_PADS_STATE_UNSET (data, GST_COLLECT_PADS_STATE_LOCKED);
1084 gst_collect_pads_set_waiting (pads, data, FALSE);
1085 GST_COLLECT_PADS_STATE_SET (data, GST_COLLECT_PADS_STATE_LOCKED);
1095 gst_event_unref (event);
1097 res = gst_collect_pads_event_default (pads, data, event, FALSE);
1103 mpegtsmux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1105 MpegTsMux *mux = GST_MPEG_TSMUX (parent);
1106 gboolean res = TRUE, forward = TRUE;
1108 switch (GST_EVENT_TYPE (event)) {
1109 case GST_EVENT_CUSTOM_UPSTREAM:
1112 GstIteratorResult iter_ret;
1114 GValue sinkpad_value = G_VALUE_INIT;
1115 GstClockTime running_time;
1116 gboolean all_headers, done, res = FALSE;
1119 if (!gst_video_event_is_force_key_unit (event))
1124 gst_video_event_parse_upstream_force_key_unit (event,
1125 &running_time, &all_headers, &count);
1127 GST_INFO_OBJECT (mux, "received upstream force-key-unit event, "
1128 "seqnum %d running_time %" GST_TIME_FORMAT " all_headers %d count %d",
1129 gst_event_get_seqnum (event), GST_TIME_ARGS (running_time),
1130 all_headers, count);
1135 mux->pending_key_unit_ts = running_time;
1136 gst_event_replace (&mux->force_key_unit_event, event);
1138 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (mux));
1143 iter_ret = gst_iterator_next (iter, &sinkpad_value);
1144 sinkpad = g_value_get_object (&sinkpad_value);
1147 case GST_ITERATOR_DONE:
1150 case GST_ITERATOR_OK:
1151 GST_INFO_OBJECT (pad, "forwarding");
1152 tmp = gst_pad_push_event (sinkpad, gst_event_ref (event));
1153 GST_INFO_OBJECT (mux, "result %d", tmp);
1154 /* succeed if at least one pad succeeds */
1157 case GST_ITERATOR_ERROR:
1160 case GST_ITERATOR_RESYNC:
1163 g_value_reset (&sinkpad_value);
1165 g_value_unset (&sinkpad_value);
1166 gst_iterator_free (iter);
1174 res = gst_pad_event_default (pad, parent, event);
1176 gst_event_unref (event);
1182 check_pending_key_unit_event (GstEvent * pending_event, GstSegment * segment,
1183 GstClockTime timestamp, guint flags, GstClockTime pending_key_unit_ts)
1185 GstClockTime running_time, stream_time;
1186 gboolean all_headers;
1188 GstEvent *event = NULL;
1190 g_assert (segment != NULL);
1192 if (pending_event == NULL)
1195 if (GST_CLOCK_TIME_IS_VALID (pending_key_unit_ts) &&
1196 timestamp == GST_CLOCK_TIME_NONE)
1199 running_time = timestamp;
1201 GST_INFO ("now %" GST_TIME_FORMAT " wanted %" GST_TIME_FORMAT,
1202 GST_TIME_ARGS (running_time), GST_TIME_ARGS (pending_key_unit_ts));
1203 if (GST_CLOCK_TIME_IS_VALID (pending_key_unit_ts) &&
1204 running_time < pending_key_unit_ts)
1207 if (flags & GST_BUFFER_FLAG_DELTA_UNIT) {
1208 GST_INFO ("pending force key unit, waiting for keyframe");
1212 stream_time = gst_segment_to_stream_time (segment,
1213 GST_FORMAT_TIME, timestamp);
1215 if (GST_EVENT_TYPE (pending_event) == GST_EVENT_CUSTOM_DOWNSTREAM) {
1216 gst_video_event_parse_downstream_force_key_unit (pending_event,
1217 NULL, NULL, NULL, &all_headers, &count);
1219 gst_video_event_parse_upstream_force_key_unit (pending_event, NULL,
1220 &all_headers, &count);
1224 gst_video_event_new_downstream_force_key_unit (timestamp, stream_time,
1225 running_time, all_headers, count);
1226 gst_event_set_seqnum (event, gst_event_get_seqnum (pending_event));
1233 mpegtsmux_clip_inc_running_time (GstCollectPads * pads,
1234 GstCollectData * cdata, GstBuffer * buf, GstBuffer ** outbuf,
1237 MpegTsPadData *pad_data = (MpegTsPadData *) cdata;
1243 time = GST_BUFFER_PTS (buf);
1245 /* invalid left alone and passed */
1246 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
1247 time = gst_segment_to_running_time (&cdata->segment, GST_FORMAT_TIME, time);
1248 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time))) {
1249 GST_DEBUG_OBJECT (cdata->pad, "clipping buffer on pad outside segment");
1250 gst_buffer_unref (buf);
1254 GST_LOG_OBJECT (cdata->pad, "buffer pts %" GST_TIME_FORMAT " -> %"
1255 GST_TIME_FORMAT " running time",
1256 GST_TIME_ARGS (GST_BUFFER_PTS (buf)), GST_TIME_ARGS (time));
1257 buf = *outbuf = gst_buffer_make_writable (buf);
1258 GST_BUFFER_PTS (*outbuf) = time;
1263 time = GST_BUFFER_DTS (buf);
1265 /* invalid left alone and passed */
1266 if (G_LIKELY (GST_CLOCK_TIME_IS_VALID (time))) {
1270 sign = gst_segment_to_running_time_full (&cdata->segment, GST_FORMAT_TIME,
1274 dts = (gint64) time;
1276 dts = -((gint64) time);
1278 GST_LOG_OBJECT (cdata->pad, "buffer dts %" GST_TIME_FORMAT " -> %"
1279 GST_STIME_FORMAT " running time", GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
1280 GST_STIME_ARGS (dts));
1282 if (GST_CLOCK_STIME_IS_VALID (pad_data->dts) && dts < pad_data->dts) {
1283 /* Ignore DTS going backward */
1284 GST_WARNING_OBJECT (cdata->pad, "ignoring DTS going backward");
1285 dts = pad_data->dts;
1288 *outbuf = gst_buffer_make_writable (buf);
1290 GST_BUFFER_DTS (*outbuf) = time;
1292 GST_BUFFER_DTS (*outbuf) = GST_CLOCK_TIME_NONE;
1294 pad_data->dts = dts;
1296 pad_data->dts = GST_CLOCK_STIME_NONE;
1303 static GstFlowReturn
1304 mpegtsmux_collected_buffer (GstCollectPads * pads, GstCollectData * data,
1305 GstBuffer * buf, MpegTsMux * mux)
1307 GstFlowReturn ret = GST_FLOW_OK;
1308 MpegTsPadData *best = (MpegTsPadData *) data;
1310 gint64 pts = GST_CLOCK_STIME_NONE;
1311 gint64 dts = GST_CLOCK_STIME_NONE;
1312 gboolean delta = TRUE, header = FALSE;
1313 StreamData *stream_data;
1315 GST_DEBUG_OBJECT (mux, "Pads collected");
1317 if (G_UNLIKELY (mux->first)) {
1318 ret = mpegtsmux_create_streams (mux);
1319 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1321 gst_buffer_unref (buf);
1325 mpegtsmux_prepare_srcpad (mux);
1330 if (G_UNLIKELY (best == NULL)) {
1332 GST_INFO_OBJECT (mux, "EOS");
1333 /* drain some possibly cached data */
1334 new_packet_m2ts (mux, NULL, -1);
1335 mpegtsmux_push_packets (mux, TRUE);
1336 gst_pad_push_event (mux->srcpad, gst_event_new_eos ());
1339 gst_buffer_unref (buf);
1348 g_assert (buf != NULL);
1350 if (best->prepare_func) {
1353 tmp = best->prepare_func (buf, best, mux);
1355 gst_buffer_unref (buf);
1359 if (mux->force_key_unit_event != NULL && best->stream->is_video_stream) {
1362 event = check_pending_key_unit_event (mux->force_key_unit_event,
1363 &best->collect.segment, GST_BUFFER_PTS (buf),
1364 GST_BUFFER_FLAGS (buf), mux->pending_key_unit_ts);
1366 GstClockTime running_time;
1370 mux->pending_key_unit_ts = GST_CLOCK_TIME_NONE;
1371 gst_event_replace (&mux->force_key_unit_event, NULL);
1373 gst_video_event_parse_downstream_force_key_unit (event,
1374 NULL, NULL, &running_time, NULL, &count);
1376 GST_INFO_OBJECT (mux, "pushing downstream force-key-unit event %d "
1377 "%" GST_TIME_FORMAT " count %d", gst_event_get_seqnum (event),
1378 GST_TIME_ARGS (running_time), count);
1379 gst_pad_push_event (mux->srcpad, event);
1382 mux->tsmux->last_pat_ts = -1;
1384 /* output PMT for each program */
1385 for (cur = mux->tsmux->programs; cur; cur = cur->next) {
1386 TsMuxProgram *program = (TsMuxProgram *) cur->data;
1388 program->last_pmt_ts = -1;
1390 tsmux_program_set_pcr_stream (prog, NULL);
1394 if (G_UNLIKELY (prog->pcr_stream == NULL)) {
1395 /* Take the first data stream for the PCR */
1396 GST_DEBUG_OBJECT (COLLECT_DATA_PAD (best),
1397 "Use stream (pid=%d) from pad as PCR for program (prog_id = %d)",
1398 best->pid, best->prog_id);
1400 /* Set the chosen PCR stream */
1401 tsmux_program_set_pcr_stream (prog, best->stream);
1404 GST_DEBUG_OBJECT (COLLECT_DATA_PAD (best),
1405 "Chose stream for output (PID: 0x%04x)", best->pid);
1407 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_PTS (buf))) {
1408 pts = GSTTIME_TO_MPEGTIME (GST_BUFFER_PTS (buf));
1409 GST_DEBUG_OBJECT (mux, "Buffer has PTS %" GST_TIME_FORMAT " pts %"
1410 G_GINT64_FORMAT, GST_TIME_ARGS (GST_BUFFER_PTS (buf)), pts);
1413 if (GST_CLOCK_STIME_IS_VALID (best->dts)) {
1414 dts = GSTTIME_TO_MPEGTIME (best->dts);
1415 GST_DEBUG_OBJECT (mux, "Buffer has DTS %" GST_STIME_FORMAT " dts %"
1416 G_GINT64_FORMAT, GST_STIME_ARGS (best->dts), dts);
1419 /* should not have a DTS without PTS */
1420 if (!GST_CLOCK_STIME_IS_VALID (pts) && GST_CLOCK_STIME_IS_VALID (dts)) {
1421 GST_DEBUG_OBJECT (mux, "using DTS for unknown PTS");
1425 if (best->stream->is_video_stream) {
1426 delta = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
1427 header = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_HEADER);
1429 GST_OBJECT_LOCK (mux);
1430 if (mux->element_index && !delta && best->element_index_writer_id != -1) {
1431 gst_index_add_association (mux->element_index,
1432 best->element_index_writer_id,
1433 GST_ASSOCIATION_FLAG_KEY_UNIT, spn_format, mux->spn_count,
1434 pts_format, pts, NULL);
1436 GST_OBJECT_UNLOCK (mux);
1440 if (best->stream->is_meta && gst_buffer_get_size (buf) > (G_MAXUINT16 - 3)) {
1441 GST_WARNING_OBJECT (mux, "KLV meta unit too big, splitting not supported");
1443 gst_buffer_unref (buf);
1447 GST_DEBUG_OBJECT (mux, "delta: %d", delta);
1449 stream_data = stream_data_new (buf);
1450 tsmux_stream_add_data (best->stream, stream_data->map_info.data,
1451 stream_data->map_info.size, stream_data, pts, dts, !delta);
1453 /* outgoing ts follows ts of PCR program stream */
1454 if (prog->pcr_stream == best->stream) {
1455 /* prefer DTS if present for PCR as it should be monotone */
1457 GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (buf)) ?
1458 GST_BUFFER_DTS (buf) : GST_BUFFER_PTS (buf);
1461 mux->is_delta = delta;
1462 mux->is_header = header;
1463 while (tsmux_stream_bytes_in_buffer (best->stream) > 0) {
1464 if (!tsmux_write_stream_packet (mux->tsmux, best->stream)) {
1465 /* Failed writing data for some reason. Set appropriate error */
1466 GST_DEBUG_OBJECT (mux, "Failed to write data packet");
1467 GST_ELEMENT_ERROR (mux, STREAM, MUX,
1468 ("Failed writing output data to stream %04x", best->stream->id),
1473 /* flush packet cache */
1474 return mpegtsmux_push_packets (mux, FALSE);
1479 return mux->last_flow_ret;
1484 gst_buffer_unref (buf);
1485 GST_ELEMENT_ERROR (mux, STREAM, MUX,
1486 ("Stream on pad %" GST_PTR_FORMAT
1487 " is not associated with any program", COLLECT_DATA_PAD (best)),
1489 return GST_FLOW_ERROR;
1494 mpegtsmux_request_new_pad (GstElement * element, GstPadTemplate * templ,
1495 const gchar * name, const GstCaps * caps)
1497 MpegTsMux *mux = GST_MPEG_TSMUX (element);
1499 gchar *pad_name = NULL;
1501 MpegTsPadData *pad_data = NULL;
1503 if (name != NULL && sscanf (name, "sink_%d", &pid) == 1) {
1504 if (tsmux_find_stream (mux->tsmux, pid))
1507 pid = tsmux_get_new_pid (mux->tsmux);
1510 pad_name = g_strdup_printf ("sink_%d", pid);
1511 pad = gst_pad_new_from_template (templ, pad_name);
1514 pad_data = (MpegTsPadData *)
1515 gst_collect_pads_add_pad (mux->collect, pad, sizeof (MpegTsPadData),
1516 (GstCollectDataDestroyNotify) (mpegtsmux_pad_reset), TRUE);
1517 if (pad_data == NULL)
1520 mpegtsmux_pad_reset (pad_data);
1521 pad_data->pid = pid;
1523 if (G_UNLIKELY (!gst_element_add_pad (element, pad)))
1531 GST_ELEMENT_ERROR (element, STREAM, MUX, ("Duplicate PID requested"),
1537 GST_ELEMENT_ERROR (element, STREAM, FAILED,
1538 ("Internal data stream error."), ("Could not add pad to element"));
1539 gst_collect_pads_remove_pad (mux->collect, pad);
1540 gst_object_unref (pad);
1545 GST_ELEMENT_ERROR (element, STREAM, FAILED,
1546 ("Internal data stream error."), ("Could not add pad to collectpads"));
1547 gst_object_unref (pad);
1553 mpegtsmux_release_pad (GstElement * element, GstPad * pad)
1555 MpegTsMux *mux = GST_MPEG_TSMUX (element);
1557 GST_DEBUG_OBJECT (mux, "Pad %" GST_PTR_FORMAT " being released", pad);
1560 gst_collect_pads_remove_pad (mux->collect, pad);
1564 gst_element_remove_pad (element, pad);
1568 new_packet_common_init (MpegTsMux * mux, GstBuffer * buf, guint8 * data,
1571 /* Packets should be at least 188 bytes, but check anyway */
1572 g_assert (len >= 2 || !data);
1574 if (!mux->streamheader_sent && data) {
1575 guint pid = ((data[1] & 0x1f) << 8) | data[2];
1576 /* if it's a PAT or a PMT */
1577 if (pid == 0x00 || (pid >= TSMUX_START_PMT_PID && pid < TSMUX_START_ES_PID)) {
1581 hbuf = gst_buffer_new_and_alloc (len);
1582 gst_buffer_fill (hbuf, 0, data, len);
1584 hbuf = gst_buffer_copy (buf);
1586 GST_LOG_OBJECT (mux,
1587 "Collecting packet with pid 0x%04x into streamheaders", pid);
1589 g_queue_push_tail (&mux->streamheader, hbuf);
1590 } else if (!g_queue_is_empty (&mux->streamheader)) {
1591 mpegtsmux_set_header_on_caps (mux);
1592 mux->streamheader_sent = TRUE;
1597 if (mux->is_header) {
1598 GST_LOG_OBJECT (mux, "marking as header buffer");
1599 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
1601 if (mux->is_delta) {
1602 GST_LOG_OBJECT (mux, "marking as delta unit");
1603 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
1605 GST_DEBUG_OBJECT (mux, "marking as non-delta unit");
1606 mux->is_delta = TRUE;
1611 static GstFlowReturn
1612 mpegtsmux_push_packets (MpegTsMux * mux, gboolean force)
1614 GstBufferList *buffer_list;
1615 gint align = mux->alignment;
1616 gint av, packet_size;
1618 if (mux->m2ts_mode) {
1619 packet_size = M2TS_PACKET_LENGTH;
1623 packet_size = NORMAL_TS_PACKET_LENGTH;
1628 av = gst_adapter_available (mux->out_adapter);
1629 GST_LOG_OBJECT (mux, "align %d, av %d", align, av);
1634 /* no alignment, just push all available data */
1636 buffer_list = gst_adapter_take_buffer_list (mux->out_adapter, av);
1637 return gst_pad_push_list (mux->srcpad, buffer_list);
1640 align *= packet_size;
1642 if (!force && align > av)
1645 buffer_list = gst_buffer_list_new_sized ((av / align) + 1);
1647 GST_LOG_OBJECT (mux, "aligning to %d bytes", align);
1648 while (align <= av) {
1652 pts = gst_adapter_prev_pts (mux->out_adapter, NULL);
1653 buf = gst_adapter_take_buffer (mux->out_adapter, align);
1655 GST_BUFFER_PTS (buf) = pts;
1657 gst_buffer_list_add (buffer_list, buf);
1661 if (av > 0 && force) {
1669 GST_LOG_OBJECT (mux, "handling %d leftover bytes", av);
1671 pts = gst_adapter_prev_pts (mux->out_adapter, NULL);
1672 buf = gst_buffer_new_and_alloc (align);
1674 GST_BUFFER_PTS (buf) = pts;
1676 gst_buffer_map (buf, &map, GST_MAP_READ);
1679 gst_adapter_copy (mux->out_adapter, data, 0, av);
1680 gst_adapter_clear (mux->out_adapter);
1683 header = GST_READ_UINT32_BE (data - packet_size);
1685 dummy = (map.size - av) / packet_size;
1686 GST_LOG_OBJECT (mux, "adding %d null packets", dummy);
1688 for (; dummy > 0; dummy--) {
1691 if (packet_size > NORMAL_TS_PACKET_LENGTH) {
1692 GST_WRITE_UINT32_BE (data, header);
1693 /* simply increase header a bit and never mind too much */
1699 GST_WRITE_UINT8 (data + offset, TSMUX_SYNC_BYTE);
1700 /* null packet PID */
1701 GST_WRITE_UINT16_BE (data + offset + 1, 0x1FFF);
1702 /* no adaptation field exists | continuity counter undefined */
1703 GST_WRITE_UINT8 (data + offset + 3, 0x10);
1705 memset (data + offset + 4, 0, NORMAL_TS_PACKET_LENGTH - 4);
1706 data += packet_size;
1709 gst_buffer_unmap (buf, &map);
1710 gst_buffer_list_add (buffer_list, buf);
1713 return gst_pad_push_list (mux->srcpad, buffer_list);
1716 static GstFlowReturn
1717 mpegtsmux_collect_packet (MpegTsMux * mux, GstBuffer * buf)
1719 GST_LOG_OBJECT (mux, "collecting packet size %" G_GSIZE_FORMAT,
1720 gst_buffer_get_size (buf));
1721 gst_adapter_push (mux->out_adapter, buf);
1727 new_packet_m2ts (MpegTsMux * mux, GstBuffer * buf, gint64 new_pcr)
1733 GST_LOG_OBJECT (mux, "Have buffer %p with new_pcr=%" G_GINT64_FORMAT,
1736 chunk_bytes = gst_adapter_available (mux->adapter);
1738 if (G_LIKELY (buf)) {
1740 /* If there is no pcr in current ts packet then just add the packet
1741 to the adapter for later output when we see a PCR */
1742 GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
1743 gst_adapter_push (mux->adapter, buf);
1747 /* no first interpolation point yet, then this is the one,
1748 * otherwise it is the second interpolation point */
1749 if (mux->previous_pcr < 0 && chunk_bytes) {
1750 mux->previous_pcr = new_pcr;
1751 mux->previous_offset = chunk_bytes;
1752 GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
1753 gst_adapter_push (mux->adapter, buf);
1757 g_assert (new_pcr == -1);
1760 /* interpolate if needed, and 2 points available */
1761 if (chunk_bytes && (new_pcr != mux->previous_pcr)) {
1764 GST_LOG_OBJECT (mux, "Processing pending packets; "
1765 "previous pcr %" G_GINT64_FORMAT ", previous offset %d, "
1766 "current pcr %" G_GINT64_FORMAT ", current offset %d",
1767 mux->previous_pcr, (gint) mux->previous_offset,
1768 new_pcr, (gint) chunk_bytes);
1770 g_assert (chunk_bytes > mux->previous_offset);
1771 /* if draining, use previous rate */
1772 if (G_LIKELY (new_pcr > 0)) {
1773 mux->pcr_rate_num = new_pcr - mux->previous_pcr;
1774 mux->pcr_rate_den = chunk_bytes - mux->previous_offset;
1777 while (offset < chunk_bytes) {
1778 guint64 cur_pcr, ts;
1780 /* Loop, pulling packets of the adapter, updating their 4 byte
1781 * timestamp header and pushing */
1783 /* interpolate PCR */
1784 if (G_LIKELY (offset >= mux->previous_offset))
1785 cur_pcr = mux->previous_pcr +
1786 gst_util_uint64_scale (offset - mux->previous_offset,
1787 mux->pcr_rate_num, mux->pcr_rate_den);
1789 cur_pcr = mux->previous_pcr -
1790 gst_util_uint64_scale (mux->previous_offset - offset,
1791 mux->pcr_rate_num, mux->pcr_rate_den);
1793 /* FIXME: what about DTS here? */
1794 ts = gst_adapter_prev_pts (mux->adapter, NULL);
1795 out_buf = gst_adapter_take_buffer (mux->adapter, M2TS_PACKET_LENGTH);
1797 offset += M2TS_PACKET_LENGTH;
1799 GST_BUFFER_PTS (out_buf) = ts;
1801 gst_buffer_map (out_buf, &map, GST_MAP_WRITE);
1803 /* The header is the bottom 30 bits of the PCR, apparently not
1804 * encoded into base + ext as in the packets themselves */
1805 GST_WRITE_UINT32_BE (map.data, cur_pcr & 0x3FFFFFFF);
1806 gst_buffer_unmap (out_buf, &map);
1808 GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
1809 G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, cur_pcr);
1810 mpegtsmux_collect_packet (mux, out_buf);
1814 if (G_UNLIKELY (!buf))
1817 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1819 /* Finally, output the passed in packet */
1820 /* Only write the bottom 30 bits of the PCR */
1821 GST_WRITE_UINT32_BE (map.data, new_pcr & 0x3FFFFFFF);
1823 gst_buffer_unmap (buf, &map);
1825 GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
1826 G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, new_pcr);
1827 mpegtsmux_collect_packet (mux, buf);
1829 if (new_pcr != mux->previous_pcr) {
1830 mux->previous_pcr = new_pcr;
1831 mux->previous_offset = -M2TS_PACKET_LENGTH;
1838 /* Called when the TsMux has prepared a packet for output. Return FALSE
1841 new_packet_cb (GstBuffer * buf, void *user_data, gint64 new_pcr)
1843 MpegTsMux *mux = (MpegTsMux *) user_data;
1848 GST_LOG_OBJECT (mux, "handling packet %d", mux->spn_count);
1852 if (mux->m2ts_mode) {
1854 gst_buffer_set_size (buf, NORMAL_TS_PACKET_LENGTH + offset);
1857 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
1860 /* there should be a better way to do this */
1861 memmove (map.data + offset, map.data, map.size - offset);
1864 GST_BUFFER_PTS (buf) = mux->last_ts;
1865 /* do common init (flags and streamheaders) */
1866 new_packet_common_init (mux, buf, map.data + offset, map.size);
1868 gst_buffer_unmap (buf, &map);
1870 /* all is meant for downstream, including any prefix */
1872 return new_packet_m2ts (mux, buf, new_pcr);
1874 mpegtsmux_collect_packet (mux, buf);
1879 /* called when TsMux needs new packet to write into */
1881 alloc_packet_cb (GstBuffer ** _buf, void *user_data)
1883 MpegTsMux *mux = (MpegTsMux *) user_data;
1887 if (mux->m2ts_mode == TRUE)
1890 buf = gst_buffer_new_and_alloc (NORMAL_TS_PACKET_LENGTH + offset);
1891 gst_buffer_set_size (buf, NORMAL_TS_PACKET_LENGTH);
1897 mpegtsmux_set_header_on_caps (MpegTsMux * mux)
1900 GstStructure *structure;
1901 GValue array = { 0 };
1902 GValue value = { 0 };
1905 caps = gst_caps_make_writable (gst_pad_get_current_caps (mux->srcpad));
1906 structure = gst_caps_get_structure (caps, 0);
1908 g_value_init (&array, GST_TYPE_ARRAY);
1910 GST_LOG_OBJECT (mux, "setting %u packets into streamheader",
1911 g_queue_get_length (&mux->streamheader));
1913 while ((buf = g_queue_pop_head (&mux->streamheader))) {
1914 g_value_init (&value, GST_TYPE_BUFFER);
1915 gst_value_take_buffer (&value, buf);
1916 gst_value_array_append_value (&array, &value);
1917 g_value_unset (&value);
1920 gst_structure_set_value (structure, "streamheader", &array);
1921 gst_pad_set_caps (mux->srcpad, caps);
1922 g_value_unset (&array);
1923 gst_caps_unref (caps);
1927 mpegtsmux_prepare_srcpad (MpegTsMux * mux)
1930 /* we are not going to seek */
1933 GstCaps *caps = gst_caps_new_simple ("video/mpegts",
1934 "systemstream", G_TYPE_BOOLEAN, TRUE,
1935 "packetsize", G_TYPE_INT,
1936 (mux->m2ts_mode ? M2TS_PACKET_LENGTH : NORMAL_TS_PACKET_LENGTH),
1939 /* stream-start (FIXME: create id based on input ids) */
1940 g_snprintf (s_id, sizeof (s_id), "mpegtsmux-%08x", g_random_int ());
1941 gst_pad_push_event (mux->srcpad, gst_event_new_stream_start (s_id));
1943 gst_segment_init (&seg, GST_FORMAT_TIME);
1944 new_seg = gst_event_new_segment (&seg);
1946 /* Set caps on src pad from our template and push new segment */
1947 gst_pad_set_caps (mux->srcpad, caps);
1948 gst_caps_unref (caps);
1950 if (!gst_pad_push_event (mux->srcpad, new_seg)) {
1951 GST_WARNING_OBJECT (mux, "New segment event was not handled downstream");
1955 static GstStateChangeReturn
1956 mpegtsmux_change_state (GstElement * element, GstStateChange transition)
1958 MpegTsMux *mux = GST_MPEG_TSMUX (element);
1959 GstStateChangeReturn ret;
1961 switch (transition) {
1962 case GST_STATE_CHANGE_NULL_TO_READY:
1964 case GST_STATE_CHANGE_READY_TO_PAUSED:
1965 gst_collect_pads_start (mux->collect);
1967 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1969 case GST_STATE_CHANGE_PAUSED_TO_READY:
1970 gst_collect_pads_stop (mux->collect);
1972 case GST_STATE_CHANGE_READY_TO_NULL:
1978 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1980 switch (transition) {
1981 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1983 case GST_STATE_CHANGE_PAUSED_TO_READY:
1984 mpegtsmux_reset (mux, TRUE);
1986 case GST_STATE_CHANGE_READY_TO_NULL:
1996 mpegtsmux_send_event (GstElement * element, GstEvent * event)
1998 GstMpegtsSection *section;
1999 MpegTsMux *mux = GST_MPEG_TSMUX (element);
2001 section = gst_event_parse_mpegts_section (event);
2002 gst_event_unref (event);
2005 GST_DEBUG ("Received event with mpegts section");
2007 /* TODO: Check that the section type is supported */
2008 tsmux_add_mpegts_si_section (mux->tsmux, section);
2017 plugin_init (GstPlugin * plugin)
2019 gst_mpegts_initialize ();
2020 if (!gst_element_register (plugin, "mpegtsmux", GST_RANK_PRIMARY,
2021 mpegtsmux_get_type ()))
2024 GST_DEBUG_CATEGORY_INIT (mpegtsmux_debug, "mpegtsmux", 0,
2025 "MPEG Transport Stream muxer");
2030 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
2031 mpegtsmux, "MPEG-TS muxer",
2032 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);