1 /*
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>
6 *
7 * Copyright (C) 2011 Jan Schmidt <thaytan@noraisin.net>
8 *
9 * This library is licensed under 3 different licenses and you
10 * can choose to use it under the terms of any one of them. The
11 * three licenses are the MPL 1.1, the LGPL and the MIT license.
12 *
13 * MPL:
14 *
15 * The contents of this file are subject to the Mozilla Public License
16 * Version 1.1 (the "License"); you may not use this file except in
17 * compliance with the License. You may obtain a copy of the License at
18 * http://www.mozilla.org/MPL/.
19 *
20 * Software distributed under the License is distributed on an "AS IS"
21 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
22 * License for the specific language governing rights and limitations
23 * under the License.
24 *
25 * LGPL:
26 *
27 * This library is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU Library General Public
29 * License as published by the Free Software Foundation; either
30 * version 2 of the License, or (at your option) any later version.
31 *
32 * This library is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35 * Library General Public License for more details.
36 *
37 * You should have received a copy of the GNU Library General Public
38 * License along with this library; if not, write to the
39 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
40 * Boston, MA 02110-1301, USA.
41 *
42 * MIT:
43 *
44 * Unless otherwise indicated, Source Code is licensed under MIT license.
45 * See further explanation attached in License Statement (distributed in the file
46 * LICENSE).
47 *
48 * Permission is hereby granted, free of charge, to any person obtaining a copy of
49 * this software and associated documentation files (the "Software"), to deal in
50 * the Software without restriction, including without limitation the rights to
51 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
52 * of the Software, and to permit persons to whom the Software is furnished to do
53 * so, subject to the following conditions:
54 *
55 * The above copyright notice and this permission notice shall be included in all
56 * copies or substantial portions of the Software.
57 *
58 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
64 * SOFTWARE.
65 *
66 * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
67 */
68
69 /**
70 * SECTION: element-mpegtsmux
71 * @title: MPEG Transport Stream muxer
72 *
73 * mpegtsmux muxes different streams into an MPEG Transport stream
74 *
75 * SI sections can be specified through a custom event:
76 *
77 * {{ tests/examples/mpegts/ts-section-writer.c }}
78 */
79 #ifdef HAVE_CONFIG_H
80 #include "config.h"
81 #endif
82
83 #include "gstmpegtsmux.h"
84 #include <string.h>
85
86 #define MPEGTSMUX_DEFAULT_M2TS FALSE
87
88 #define M2TS_PACKET_LENGTH 192
89
90 enum
91 {
92 PROP_0,
93 PROP_M2TS_MODE,
94 };
95
96 static GstStaticPadTemplate gst_mpeg_ts_mux_sink_factory =
97 GST_STATIC_PAD_TEMPLATE ("sink_%d",
98 GST_PAD_SINK,
99 GST_PAD_REQUEST,
100 GST_STATIC_CAPS ("video/mpeg, "
101 "parsed = (boolean) TRUE, "
102 "mpegversion = (int) { 1, 2, 4 }, "
103 "systemstream = (boolean) false; "
104 "video/x-dirac;"
105 "image/x-jpc, alignment = (string) frame;"
106 "video/x-h264,stream-format=(string)byte-stream,"
107 "alignment=(string){au, nal}; "
108 "video/x-h265,stream-format=(string)byte-stream,"
109 "alignment=(string){au, nal}; "
110 "audio/mpeg, "
111 "parsed = (boolean) TRUE, "
112 "mpegversion = (int) 1;"
113 "audio/mpeg, "
114 "framed = (boolean) TRUE, "
115 "mpegversion = (int) {2, 4}, stream-format = (string) { adts, raw };"
116 "audio/x-lpcm, "
117 "width = (int) { 16, 20, 24 }, "
118 "rate = (int) { 48000, 96000 }, "
119 "channels = (int) [ 1, 8 ], "
120 "dynamic_range = (int) [ 0, 255 ], "
121 "emphasis = (boolean) { FALSE, TRUE }, "
122 "mute = (boolean) { FALSE, TRUE }; "
123 "audio/x-ac3, framed = (boolean) TRUE;"
124 "audio/x-dts, framed = (boolean) TRUE;"
125 "audio/x-opus, "
126 "channels = (int) [1, 8], "
127 "channel-mapping-family = (int) {0, 1};"
128 "subpicture/x-dvb; application/x-teletext; meta/x-klv, parsed=true;"
129 "image/x-jpc, alignment = (string) frame, profile = (int)[0, 49151];"));
130
131 static GstStaticPadTemplate gst_mpeg_ts_mux_src_factory =
132 GST_STATIC_PAD_TEMPLATE ("src",
133 GST_PAD_SRC,
134 GST_PAD_ALWAYS,
135 GST_STATIC_CAPS ("video/mpegts, "
136 "systemstream = (boolean) true, " "packetsize = (int) { 188, 192} ")
137 );
138
139 GST_DEBUG_CATEGORY (gst_mpeg_ts_mux_debug);
140 #define GST_CAT_DEFAULT gst_mpeg_ts_mux_debug
141
142 #define parent_class gst_mpeg_ts_mux_parent_class
143 G_DEFINE_TYPE (GstMpegTsMux, gst_mpeg_ts_mux, GST_TYPE_BASE_TS_MUX);
144 GST_ELEMENT_REGISTER_DEFINE (mpegtsmux, "mpegtsmux", GST_RANK_PRIMARY,
145 gst_mpeg_ts_mux_get_type ());
146
147 /* Internals */
148
149 static gboolean
new_packet_m2ts(GstMpegTsMux * mux,GstBuffer * buf,gint64 new_pcr)150 new_packet_m2ts (GstMpegTsMux * mux, GstBuffer * buf, gint64 new_pcr)
151 {
152 GstBuffer *out_buf;
153 int chunk_bytes;
154 GstMapInfo map;
155
156 GST_LOG_OBJECT (mux, "Have buffer %p with new_pcr=%" G_GINT64_FORMAT,
157 buf, new_pcr);
158
159 chunk_bytes = gst_adapter_available (mux->adapter);
160
161 if (G_LIKELY (buf)) {
162 if (new_pcr < 0) {
163 /* If there is no pcr in current ts packet then just add the packet
164 to the adapter for later output when we see a PCR */
165 GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
166 gst_adapter_push (mux->adapter, buf);
167 goto exit;
168 }
169
170 /* no first interpolation point yet, then this is the one,
171 * otherwise it is the second interpolation point */
172 if (mux->previous_pcr < 0 && chunk_bytes) {
173 mux->previous_pcr = new_pcr;
174 mux->previous_offset = chunk_bytes;
175 GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
176 gst_adapter_push (mux->adapter, buf);
177 goto exit;
178 }
179 } else {
180 g_assert (new_pcr == -1);
181 }
182
183 /* interpolate if needed, and 2 points available */
184 if (chunk_bytes && (new_pcr != mux->previous_pcr)) {
185 gint64 offset = 0;
186
187 GST_LOG_OBJECT (mux, "Processing pending packets; "
188 "previous pcr %" G_GINT64_FORMAT ", previous offset %d, "
189 "current pcr %" G_GINT64_FORMAT ", current offset %d",
190 mux->previous_pcr, (gint) mux->previous_offset,
191 new_pcr, (gint) chunk_bytes);
192
193 g_assert (chunk_bytes > mux->previous_offset);
194 /* if draining, use previous rate */
195 if (G_LIKELY (new_pcr > 0)) {
196 mux->pcr_rate_num = new_pcr - mux->previous_pcr;
197 mux->pcr_rate_den = chunk_bytes - mux->previous_offset;
198 }
199
200 while (offset < chunk_bytes) {
201 guint64 cur_pcr, ts;
202
203 /* Loop, pulling packets of the adapter, updating their 4 byte
204 * timestamp header and pushing */
205
206 /* interpolate PCR */
207 if (G_LIKELY (offset >= mux->previous_offset))
208 cur_pcr = mux->previous_pcr +
209 gst_util_uint64_scale (offset - mux->previous_offset,
210 mux->pcr_rate_num, mux->pcr_rate_den);
211 else
212 cur_pcr = mux->previous_pcr -
213 gst_util_uint64_scale (mux->previous_offset - offset,
214 mux->pcr_rate_num, mux->pcr_rate_den);
215
216 /* FIXME: what about DTS here? */
217 ts = gst_adapter_prev_pts (mux->adapter, NULL);
218 out_buf = gst_adapter_take_buffer (mux->adapter, M2TS_PACKET_LENGTH);
219 g_assert (out_buf);
220 offset += M2TS_PACKET_LENGTH;
221
222 GST_BUFFER_PTS (out_buf) = ts;
223
224 gst_buffer_map (out_buf, &map, GST_MAP_WRITE);
225
226 /* The header is the bottom 30 bits of the PCR, apparently not
227 * encoded into base + ext as in the packets themselves */
228 GST_WRITE_UINT32_BE (map.data, cur_pcr & 0x3FFFFFFF);
229 gst_buffer_unmap (out_buf, &map);
230
231 GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
232 G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, cur_pcr);
233 ((GstBaseTsMuxClass *)
234 parent_class)->output_packet (GST_BASE_TS_MUX (mux), out_buf, -1);
235 }
236 }
237
238 if (G_UNLIKELY (!buf))
239 goto exit;
240
241 gst_buffer_map (buf, &map, GST_MAP_WRITE);
242
243 /* Finally, output the passed in packet */
244 /* Only write the bottom 30 bits of the PCR */
245 GST_WRITE_UINT32_BE (map.data, new_pcr & 0x3FFFFFFF);
246
247 gst_buffer_unmap (buf, &map);
248
249 GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
250 G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, new_pcr);
251
252 ((GstBaseTsMuxClass *) parent_class)->output_packet (GST_BASE_TS_MUX (mux),
253 buf, -1);
254
255 if (new_pcr != mux->previous_pcr) {
256 mux->previous_pcr = new_pcr;
257 mux->previous_offset = -M2TS_PACKET_LENGTH;
258 }
259
260 exit:
261 return TRUE;
262 }
263
264 /* GstBaseTsMux implementation */
265
266 static void
gst_mpeg_ts_mux_allocate_packet(GstBaseTsMux * mux,GstBuffer ** buffer)267 gst_mpeg_ts_mux_allocate_packet (GstBaseTsMux * mux, GstBuffer ** buffer)
268 {
269 ((GstBaseTsMuxClass *) parent_class)->allocate_packet (mux, buffer);
270
271 gst_buffer_set_size (*buffer, GST_BASE_TS_MUX_NORMAL_PACKET_LENGTH);
272 }
273
274 static gboolean
gst_mpeg_ts_mux_output_packet(GstBaseTsMux * base_tsmux,GstBuffer * buffer,gint64 new_pcr)275 gst_mpeg_ts_mux_output_packet (GstBaseTsMux * base_tsmux, GstBuffer * buffer,
276 gint64 new_pcr)
277 {
278 GstMpegTsMux *mux = GST_MPEG_TS_MUX (base_tsmux);
279 GstMapInfo map;
280
281 if (!mux->m2ts_mode)
282 return ((GstBaseTsMuxClass *) parent_class)->output_packet (base_tsmux,
283 buffer, new_pcr);
284
285 gst_buffer_set_size (buffer, M2TS_PACKET_LENGTH);
286
287 gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
288
289 /* there should be a better way to do this */
290 memmove (map.data + 4, map.data, map.size - 4);
291
292 gst_buffer_unmap (buffer, &map);
293
294 return new_packet_m2ts (mux, buffer, new_pcr);
295 }
296
297 static void
gst_mpeg_ts_mux_reset(GstBaseTsMux * base_tsmux)298 gst_mpeg_ts_mux_reset (GstBaseTsMux * base_tsmux)
299 {
300 GstMpegTsMux *mux = GST_MPEG_TS_MUX (base_tsmux);
301
302 if (mux->adapter)
303 gst_adapter_clear (mux->adapter);
304
305 mux->previous_pcr = -1;
306 mux->previous_offset = 0;
307 mux->pcr_rate_num = mux->pcr_rate_den = 1;
308 }
309
310 static gboolean
gst_mpeg_ts_mux_drain(GstBaseTsMux * mux)311 gst_mpeg_ts_mux_drain (GstBaseTsMux * mux)
312 {
313 return new_packet_m2ts (GST_MPEG_TS_MUX (mux), NULL, -1);
314 }
315
316 /* GObject implementation */
317
318 static void
gst_mpeg_ts_mux_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)319 gst_mpeg_ts_mux_set_property (GObject * object, guint prop_id,
320 const GValue * value, GParamSpec * pspec)
321 {
322 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
323
324 switch (prop_id) {
325 case PROP_M2TS_MODE:
326 /* set in case if the output stream need to be of 192 bytes */
327 mux->m2ts_mode = g_value_get_boolean (value);
328 gst_base_ts_mux_set_packet_size (GST_BASE_TS_MUX (mux),
329 mux->m2ts_mode ? M2TS_PACKET_LENGTH :
330 GST_BASE_TS_MUX_NORMAL_PACKET_LENGTH);
331 gst_base_ts_mux_set_automatic_alignment (GST_BASE_TS_MUX (mux),
332 mux->m2ts_mode ? 32 : 0);
333 break;
334 default:
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336 break;
337 }
338 }
339
340 static void
gst_mpeg_ts_mux_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)341 gst_mpeg_ts_mux_get_property (GObject * object, guint prop_id,
342 GValue * value, GParamSpec * pspec)
343 {
344 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
345
346 switch (prop_id) {
347 case PROP_M2TS_MODE:
348 g_value_set_boolean (value, mux->m2ts_mode);
349 break;
350 default:
351 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
352 break;
353 }
354 }
355
356 static void
gst_mpeg_ts_mux_dispose(GObject * object)357 gst_mpeg_ts_mux_dispose (GObject * object)
358 {
359 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
360
361 if (mux->adapter) {
362 g_object_unref (mux->adapter);
363 mux->adapter = NULL;
364 }
365
366 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
367 }
368
369 static void
gst_mpeg_ts_mux_class_init(GstMpegTsMuxClass * klass)370 gst_mpeg_ts_mux_class_init (GstMpegTsMuxClass * klass)
371 {
372 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
373 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
374 GstBaseTsMuxClass *base_tsmux_class = GST_BASE_TS_MUX_CLASS (klass);
375
376 GST_DEBUG_CATEGORY_INIT (gst_mpeg_ts_mux_debug, "mpegtsmux", 0,
377 "MPEG Transport Stream muxer");
378
379 gobject_class->set_property =
380 GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_set_property);
381 gobject_class->get_property =
382 GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_get_property);
383 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_dispose);
384
385 base_tsmux_class->allocate_packet =
386 GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_allocate_packet);
387 base_tsmux_class->output_packet =
388 GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_output_packet);
389 base_tsmux_class->reset = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_reset);
390 base_tsmux_class->drain = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_drain);
391
392 gst_element_class_set_static_metadata (gstelement_class,
393 "MPEG Transport Stream Muxer", "Codec/Muxer",
394 "Multiplexes media streams into an MPEG Transport Stream",
395 "Fluendo <contact@fluendo.com>");
396
397 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
398 &gst_mpeg_ts_mux_sink_factory, GST_TYPE_BASE_TS_MUX_PAD);
399
400 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
401 &gst_mpeg_ts_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
402
403 g_object_class_install_property (gobject_class, PROP_M2TS_MODE,
404 g_param_spec_boolean ("m2ts-mode", "M2TS(192 bytes) Mode",
405 "Set to TRUE to output Blu-Ray disc format with 192 byte packets. "
406 "FALSE for standard TS format with 188 byte packets.",
407 MPEGTSMUX_DEFAULT_M2TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 }
409
410 static void
gst_mpeg_ts_mux_init(GstMpegTsMux * mux)411 gst_mpeg_ts_mux_init (GstMpegTsMux * mux)
412 {
413 mux->m2ts_mode = MPEGTSMUX_DEFAULT_M2TS;
414 mux->adapter = gst_adapter_new ();
415 }
416