2 * Copyright 2006 BBC and Fluendo S.A.
4 * This library is licensed under 3 different licenses and you
5 * can choose to use it under the terms of any one of them. The
6 * three licenses are the MPL 1.1, the LGPL, and the MIT license.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.1 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/.
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
17 * License for the specific language governing rights and limitations
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Library General Public
24 * License as published by the Free Software Foundation; either
25 * version 2 of the License, or (at your option) any later version.
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Library General Public License for more details.
32 * You should have received a copy of the GNU Library General Public
33 * License along with this library; if not, write to the
34 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
35 * Boston, MA 02110-1301, USA.
39 * Unless otherwise indicated, Source Code is licensed under MIT license.
40 * See further explanation attached in License Statement (distributed in the file
43 * Permission is hereby granted, free of charge, to any person obtaining a copy of
44 * this software and associated documentation files (the "Software"), to deal in
45 * the Software without restriction, including without limitation the rights to
46 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
47 * of the Software, and to permit persons to whom the Software is furnished to do
48 * so, subject to the following conditions:
50 * The above copyright notice and this permission notice shall be included in all
51 * copies or substantial portions of the Software.
53 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
61 * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
70 #include <gst/mpegts/mpegts.h>
73 #include "tsmuxstream.h"
75 #define GST_CAT_DEFAULT gst_base_ts_mux_debug
77 /* Maximum total data length for a PAT section is 1024 bytes, minus an
78 * 8 byte header, then the length of each program entry is 32 bits,
79 * then finally a 32 bit CRC. Thus the maximum number of programs in this mux
80 * is (1024 - 8 - 4) / 4 = 253 because it only supports single section PATs */
81 #define TSMUX_MAX_PROGRAMS 253
83 #define TSMUX_SECTION_HDR_SIZE 8
85 #define TSMUX_DEFAULT_NETWORK_ID 0x0001
86 #define TSMUX_DEFAULT_TS_ID 0x0001
88 /* The last byte of the PCR in the header defines the byte position
89 * at which PCR should be calculated */
90 #define PCR_BYTE_OFFSET 11
92 /* HACK: We use a fixed buffering offset for the PCR at the moment -
93 * this is the amount 'in advance' of the stream that the PCR sits.
95 #define TSMUX_PCR_OFFSET (TSMUX_CLOCK_FREQ / 8)
97 /* Base for all written PCR and DTS/PTS,
98 * so we have some slack to go backwards */
99 #define CLOCK_BASE (TSMUX_CLOCK_FREQ * 10 * 360)
101 static gboolean tsmux_write_pat (TsMux * mux);
102 static gboolean tsmux_write_pmt (TsMux * mux, TsMuxProgram * program);
103 static gboolean tsmux_write_scte_null (TsMux * mux, TsMuxProgram * program);
104 static gint64 get_next_pcr (TsMux * mux, gint64 cur_ts);
105 static gint64 get_current_pcr (TsMux * mux, gint64 cur_ts);
106 static gint64 write_new_pcr (TsMux * mux, TsMuxStream * stream, gint64 cur_pcr,
108 static gboolean tsmux_write_ts_header (TsMux * mux, guint8 * buf,
109 TsMuxPacketInfo * pi, guint * payload_len_out, guint * payload_offset_out,
113 tsmux_section_free (TsMuxSection * section)
115 gst_mpegts_section_unref (section->section);
116 g_slice_free (TsMuxSection, section);
122 * Create a new muxer session.
124 * Returns: A new #TsMux object.
131 mux = g_slice_new0 (TsMux);
133 mux->transport_id = TSMUX_DEFAULT_TS_ID;
135 mux->next_pgm_no = TSMUX_START_PROGRAM_ID;
136 mux->next_pmt_pid = TSMUX_START_PMT_PID;
137 mux->next_stream_pid = TSMUX_START_ES_PID;
139 mux->pat_changed = TRUE;
140 mux->next_pat_pcr = -1;
141 mux->pat_interval = TSMUX_DEFAULT_PAT_INTERVAL;
143 mux->si_changed = TRUE;
144 mux->si_interval = TSMUX_DEFAULT_SI_INTERVAL;
146 mux->pcr_interval = TSMUX_DEFAULT_PCR_INTERVAL;
148 mux->next_si_pcr = -1;
150 mux->si_sections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
151 NULL, (GDestroyNotify) tsmux_section_free);
153 mux->new_stream_func = (TsMuxNewStreamFunc) tsmux_stream_new;
154 mux->new_stream_data = NULL;
156 mux->first_pcr_ts = G_MININT64;
162 * tsmux_set_write_func:
164 * @func: a user callback function
165 * @user_data: user data passed to @func
167 * Set the callback function and user data to be called when @mux has output to
168 * produce. @user_data will be passed as user data in @func.
171 tsmux_set_write_func (TsMux * mux, TsMuxWriteFunc func, void *user_data)
173 g_return_if_fail (mux != NULL);
175 mux->write_func = func;
176 mux->write_func_data = user_data;
180 * tsmux_set_alloc_func:
182 * @func: a user callback function
183 * @user_data: user data passed to @func
185 * Set the callback function and user data to be called when @mux needs
186 * a new buffer to write a packet into.
187 * @user_data will be passed as user data in @func.
190 tsmux_set_alloc_func (TsMux * mux, TsMuxAllocFunc func, void *user_data)
192 g_return_if_fail (mux != NULL);
194 mux->alloc_func = func;
195 mux->alloc_func_data = user_data;
199 * tsmux_set_new_stream_func:
201 * @func: a user callback function
202 * @user_data: user data passed to @func
204 * Set the callback function and user data to be called when @mux needs
205 * to create a new stream.
206 * @user_data will be passed as user data in @func.
209 tsmux_set_new_stream_func (TsMux * mux, TsMuxNewStreamFunc func,
212 g_return_if_fail (mux != NULL);
214 mux->new_stream_func = func;
215 mux->new_stream_data = user_data;
219 * tsmux_set_pat_interval:
221 * @freq: a new PAT interval
223 * Set the interval (in cycles of the 90kHz clock) for writing out the PAT table.
225 * Many transport stream clients might have problems if the PAT table is not
226 * inserted in the stream at regular intervals, especially when initially trying
227 * to figure out the contents of the stream.
230 tsmux_set_pat_interval (TsMux * mux, guint freq)
232 g_return_if_fail (mux != NULL);
234 mux->pat_interval = freq;
238 * tsmux_set_pcr_interval:
240 * @freq: a new PCR interval
242 * Set the interval (in cycles of the 90kHz clock) for writing the PCR.
245 tsmux_set_pcr_interval (TsMux * mux, guint freq)
247 g_return_if_fail (mux != NULL);
249 mux->pcr_interval = freq;
253 * tsmux_get_pat_interval:
256 * Get the configured PAT interval. See also tsmux_set_pat_interval().
258 * Returns: the configured PAT interval
261 tsmux_get_pat_interval (TsMux * mux)
263 g_return_val_if_fail (mux != NULL, 0);
265 return mux->pat_interval;
272 * Resends the PAT before the next stream packet.
275 tsmux_resend_pat (TsMux * mux)
277 g_return_if_fail (mux != NULL);
279 mux->next_pat_pcr = -1;
283 * tsmux_set_si_interval:
285 * @freq: a new SI table interval
287 * Set the interval (in cycles of the 90kHz clock) for writing out the SI tables.
291 tsmux_set_si_interval (TsMux * mux, guint freq)
293 g_return_if_fail (mux != NULL);
295 mux->si_interval = freq;
299 * tsmux_get_si_interval:
302 * Get the configured SI table interval. See also tsmux_set_si_interval().
304 * Returns: the configured SI interval
307 tsmux_get_si_interval (TsMux * mux)
309 g_return_val_if_fail (mux != NULL, 0);
311 return mux->si_interval;
318 * Resends the SI tables before the next stream packet.
322 tsmux_resend_si (TsMux * mux)
324 g_return_if_fail (mux != NULL);
326 mux->next_si_pcr = -1;
330 * tsmux_add_mpegts_si_section:
332 * @section: (transfer full): a #GstMpegtsSection to add
334 * Add a Service Information #GstMpegtsSection to the stream
336 * Returns: %TRUE on success, %FALSE otherwise
339 tsmux_add_mpegts_si_section (TsMux * mux, GstMpegtsSection * section)
341 TsMuxSection *tsmux_section;
343 g_return_val_if_fail (mux != NULL, FALSE);
344 g_return_val_if_fail (section != NULL, FALSE);
345 g_return_val_if_fail (mux->si_sections != NULL, FALSE);
347 tsmux_section = g_slice_new0 (TsMuxSection);
349 GST_DEBUG ("Adding mpegts section with type %d to mux",
350 section->section_type);
352 tsmux_section->section = section;
353 tsmux_section->pi.pid = section->pid;
355 g_hash_table_insert (mux->si_sections,
356 GINT_TO_POINTER (section->section_type), tsmux_section);
358 mux->si_changed = TRUE;
368 * Free all resources associated with @mux. After calling this function @mux can
369 * not be used anymore.
372 tsmux_free (TsMux * mux)
376 g_return_if_fail (mux != NULL);
378 /* Free PAT section */
379 if (mux->pat.section)
380 gst_mpegts_section_unref (mux->pat.section);
382 /* Free all programs */
383 for (cur = mux->programs; cur; cur = cur->next) {
384 TsMuxProgram *program = (TsMuxProgram *) cur->data;
386 tsmux_program_free (program);
388 g_list_free (mux->programs);
390 /* Free all streams */
391 for (cur = mux->streams; cur; cur = cur->next) {
392 TsMuxStream *stream = (TsMuxStream *) cur->data;
394 tsmux_stream_free (stream);
396 g_list_free (mux->streams);
398 /* Free SI table sections */
399 g_hash_table_unref (mux->si_sections);
401 g_slice_free (TsMux, mux);
405 tsmux_program_compare (TsMuxProgram * program, gint * needle)
407 return (program->pgm_number - *needle);
414 * Create a new program in the missing session @mux.
416 * Returns: a new #TsMuxProgram or %NULL when the maximum number of programs has
420 tsmux_program_new (TsMux * mux, gint prog_id)
422 TsMuxProgram *program;
424 g_return_val_if_fail (mux != NULL, NULL);
426 /* Ensure we have room for another program */
427 if (mux->nb_programs == TSMUX_MAX_PROGRAMS)
430 program = g_slice_new0 (TsMuxProgram);
432 program->pmt_changed = TRUE;
433 program->pmt_interval = TSMUX_DEFAULT_PMT_INTERVAL;
435 program->next_pmt_pcr = -1;
438 program->pgm_number = mux->next_pgm_no++;
439 while (g_list_find_custom (mux->programs, &program->pgm_number,
440 (GCompareFunc) tsmux_program_compare) != NULL) {
441 program->pgm_number = mux->next_pgm_no++;
444 program->pgm_number = prog_id;
445 while (g_list_find_custom (mux->programs, &program->pgm_number,
446 (GCompareFunc) tsmux_program_compare) != NULL) {
447 program->pgm_number++;
451 program->pmt_pid = mux->next_pmt_pid++;
452 program->pcr_stream = NULL;
454 /* SCTE35 is disabled by default */
455 program->scte35_pid = 0;
456 program->scte35_null_interval = TSMUX_DEFAULT_SCTE_35_NULL_INTERVAL;
457 program->next_scte35_pcr = -1;
459 /* mux->streams owns the streams */
460 program->streams = g_ptr_array_new_full (1, NULL);
462 mux->programs = g_list_prepend (mux->programs, program);
464 mux->pat_changed = TRUE;
470 tsmux_program_delete (TsMux * mux, TsMuxProgram * program)
472 g_return_val_if_fail (mux != NULL, FALSE);
474 if (mux->nb_programs == 0)
480 mux->programs = g_list_remove (mux->programs, program);
482 mux->pat_changed = TRUE;
483 tsmux_program_free ((TsMuxProgram *) program);
489 * tsmux_set_pmt_interval:
490 * @program: a #TsMuxProgram
491 * @freq: a new PMT interval
493 * Set the interval (in cycles of the 90kHz clock) for writing out the PMT table.
495 * Many transport stream clients might have problems if the PMT table is not
496 * inserted in the stream at regular intervals, especially when initially trying
497 * to figure out the contents of the stream.
500 tsmux_set_pmt_interval (TsMuxProgram * program, guint freq)
502 g_return_if_fail (program != NULL);
504 program->pmt_interval = freq;
508 * tsmux_get_pmt_interval:
509 * @program: a #TsMuxProgram
511 * Get the configured PMT interval. See also tsmux_set_pmt_interval().
513 * Returns: the configured PMT interval
516 tsmux_get_pmt_interval (TsMuxProgram * program)
518 g_return_val_if_fail (program != NULL, 0);
520 return program->pmt_interval;
524 * tsmux_program_set_scte35_interval:
525 * @program: a #TsMuxProgram
526 * @freq: a new SCTE-35 NULL interval
528 * Set the interval (in cycles of the 90kHz clock) for sending out the SCTE-35
529 * NULL command. This is only effective is the SCTE-35 PID is not 0.
532 tsmux_program_set_scte35_interval (TsMuxProgram * program, guint interval)
534 g_return_if_fail (program != NULL);
536 program->scte35_null_interval = interval;
541 * @program: a #TsMuxProgram
543 * Resends the PMT before the next stream packet.
546 tsmux_resend_pmt (TsMuxProgram * program)
548 g_return_if_fail (program != NULL);
550 program->next_pmt_pcr = -1;
554 * tsmux_program_set_scte35_pid:
555 * @program: a #TsMuxProgram
556 * @pid: The pid to use, or 0 to deactivate usage.
558 * Set the @pid to use for sending SCTE-35 packets on the given
561 * This needs to be called as early as possible if SCTE-35 sections
562 * are even going to be used with the given @program so that the PMT
563 * can be properly configured.
566 tsmux_program_set_scte35_pid (TsMuxProgram * program, guint16 pid)
568 TsMuxSection *section;
569 GstMpegtsSCTESIT *sit;
570 g_return_if_fail (program != NULL);
572 program->scte35_pid = pid;
573 /* Create/Update the section */
574 if (program->scte35_null_section) {
575 tsmux_section_free (program->scte35_null_section);
576 program->scte35_null_section = NULL;
579 program->scte35_null_section = section = g_slice_new0 (TsMuxSection);
580 section->pi.pid = pid;
581 sit = gst_mpegts_scte_null_new ();
582 section->section = gst_mpegts_section_from_scte_sit (sit, pid);
587 * tsmux_program_get_scte35_pid:
588 * @program: a #TsMuxProgram
590 * Get the PID configured for sending SCTE-35 packets.
592 * Returns: the configured SCTE-35 PID, or 0 if not active.
595 tsmux_program_get_scte35_pid (TsMuxProgram * program)
597 g_return_val_if_fail (program != NULL, 0);
599 return program->scte35_pid;
603 * tsmux_program_add_stream:
604 * @program: a #TsMuxProgram
605 * @stream: a #TsMuxStream
607 * Add @stream to @program.
610 tsmux_program_add_stream (TsMuxProgram * program, TsMuxStream * stream)
614 gint pmt_index, array_index = -1 /* append */ ;
617 g_return_if_fail (program != NULL);
618 g_return_if_fail (stream != NULL);
620 streams = program->streams;
621 pmt_index = stream->pmt_index;
622 pid = tsmux_stream_get_pid (stream);
624 if (pmt_index >= 0) {
625 /* Insert into streams with known indices */
626 for (i = 0; i < streams->len; i++) {
627 TsMuxStream *s = g_ptr_array_index (streams, i);
629 if (s->pmt_index < 0 || pmt_index < s->pmt_index) {
631 GST_DEBUG ("PID 0x%04x: Using known-order index %d/%u",
632 pid, array_index, streams->len);
637 /* Insert after streams with known indices, sorted by PID */
638 for (i = 0; i < streams->len; i++) {
639 TsMuxStream *s = g_ptr_array_index (streams, i);
641 if (s->pmt_index < 0 && pid < tsmux_stream_get_pid (s)) {
643 GST_DEBUG ("PID 0x%04x: Using PID-order index %d/%u",
644 pid, array_index, streams->len);
650 g_ptr_array_insert (streams, array_index, stream);
651 program->pmt_changed = TRUE;
655 * tsmux_program_set_pcr_stream:
656 * @program: a #TsMuxProgram
657 * @stream: a #TsMuxStream
659 * Set @stream as the PCR stream for @program, overwriting the previously
660 * configured PCR stream. When @stream is NULL, program will have no PCR stream
664 tsmux_program_set_pcr_stream (TsMuxProgram * program, TsMuxStream * stream)
666 g_return_if_fail (program != NULL);
668 if (program->pcr_stream == stream)
671 if (program->pcr_stream != NULL)
672 tsmux_stream_pcr_unref (program->pcr_stream);
674 tsmux_stream_pcr_ref (stream);
675 program->pcr_stream = stream;
677 program->pmt_changed = TRUE;
684 * Get a new free PID.
686 * Returns: a new free PID.
689 tsmux_get_new_pid (TsMux * mux)
691 g_return_val_if_fail (mux != NULL, -1);
693 /* make sure this PID is free
694 * (and not taken by a specific earlier request) */
696 mux->next_stream_pid++;
697 } while (tsmux_find_stream (mux, mux->next_stream_pid));
699 return mux->next_stream_pid;
703 * tsmux_create_stream:
705 * @stream_type: the stream type
706 * @pid: the PID of the new stream.
708 * Create a new stream of @stream_type in the muxer session @mux.
710 * When @pid is set to #TSMUX_PID_AUTO, a new free PID will automatically
711 * be allocated for the new stream.
713 * Returns: a new #TsMuxStream.
716 tsmux_create_stream (TsMux * mux, guint stream_type, guint16 pid,
722 g_return_val_if_fail (mux != NULL, NULL);
723 g_return_val_if_fail (mux->new_stream_func != NULL, NULL);
725 if (pid == TSMUX_PID_AUTO) {
726 new_pid = tsmux_get_new_pid (mux);
728 new_pid = pid & 0x1FFF;
731 /* Ensure we're not creating a PID collision */
732 if (tsmux_find_stream (mux, new_pid))
735 stream = mux->new_stream_func (new_pid, stream_type, mux->new_stream_data);
737 mux->streams = g_list_prepend (mux->streams, stream);
741 strncpy (stream->language, language, 4);
742 stream->language[3] = 0;
744 stream->language[0] = 0;
753 * @pid: the PID to find.
755 * Find the stream associated with PID.
757 * Returns: a #TsMuxStream with @pid or NULL when the stream was not found.
760 tsmux_find_stream (TsMux * mux, guint16 pid)
762 TsMuxStream *found = NULL;
765 g_return_val_if_fail (mux != NULL, NULL);
767 for (cur = mux->streams; cur; cur = cur->next) {
768 TsMuxStream *stream = (TsMuxStream *) cur->data;
770 if (tsmux_stream_get_pid (stream) == pid) {
779 tsmux_program_remove_stream (TsMuxProgram * program, TsMuxStream * stream)
781 GPtrArray *streams = program->streams;
783 if (!g_ptr_array_remove (streams, stream)) {
784 g_warn_if_reached ();
788 return streams->len == 0;
793 tsmux_remove_stream (TsMux * mux, guint16 pid, TsMuxProgram * program)
796 gboolean ret = FALSE;
798 g_return_val_if_fail (mux != NULL, FALSE);
800 for (cur = mux->streams; cur; cur = cur->next) {
801 TsMuxStream *stream = (TsMuxStream *) cur->data;
803 if (tsmux_stream_get_pid (stream) == pid) {
804 ret = tsmux_program_remove_stream (program, stream);
805 mux->streams = g_list_remove (mux->streams, stream);
806 tsmux_stream_free (stream);
812 tsmux_program_delete (mux, program);
818 tsmux_get_buffer (TsMux * mux, GstBuffer ** buf)
820 g_return_val_if_fail (buf, FALSE);
822 if (G_UNLIKELY (!mux->alloc_func))
825 mux->alloc_func (buf, mux->alloc_func_data);
830 g_assert (gst_buffer_get_size (*buf) == TSMUX_PACKET_LENGTH);
835 tsmux_packet_out (TsMux * mux, GstBuffer * buf, gint64 pcr)
837 if (G_UNLIKELY (mux->write_func == NULL)) {
839 gst_buffer_unref (buf);
844 GST_BUFFER_PTS (buf) =
845 gst_util_uint64_scale (mux->n_bytes * 8, GST_SECOND, mux->bitrate);
847 /* Check and insert a PCR observation for each program if needed,
848 * but only for programs that have written their SI at least once,
849 * so the stream starts with PAT/PMT */
850 if (mux->first_pcr_ts != G_MININT64) {
853 for (cur = mux->programs; cur; cur = cur->next) {
854 TsMuxProgram *program = (TsMuxProgram *) cur->data;
855 TsMuxStream *stream = program->pcr_stream;
856 gint64 cur_pcr, next_pcr, new_pcr;
858 if (!program->wrote_si)
861 cur_pcr = get_current_pcr (mux, 0);
862 next_pcr = get_next_pcr (mux, 0);
863 new_pcr = write_new_pcr (mux, stream, cur_pcr, next_pcr);
866 GstBuffer *buf = NULL;
868 guint payload_len, payload_offs;
870 if (!tsmux_get_buffer (mux, &buf)) {
874 gst_buffer_map (buf, &map, GST_MAP_READ);
875 tsmux_write_ts_header (mux, map.data, &stream->pi, &payload_len,
877 gst_buffer_unmap (buf, &map);
879 stream->pi.flags &= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
880 if (!tsmux_packet_out (mux, buf, new_pcr))
887 mux->n_bytes += gst_buffer_get_size (buf);
889 return mux->write_func (buf, mux->write_func_data, pcr);
896 * adaptation_field() {
897 * adaptation_field_length 8 uimsbf
898 * if(adaptation_field_length >0) {
899 * discontinuity_indicator 1 bslbf
900 * random_access_indicator 1 bslbf
901 * elementary_stream_priority_indicator 1 bslbf
904 * splicing_point_flag 1 bslbf
905 * transport_private_data_flag 1 bslbf
906 * adaptation_field_extension_flag 1 bslbf
907 * if(PCR_flag == '1') {
908 * program_clock_reference_base 33 uimsbf
910 * program_clock_reference_extension 9 uimsbf
912 * if(OPCR_flag == '1') {
913 * original_program_clock_reference_base 33 uimsbf
915 * original_program_clock_reference_extension 9 uimsbf
917 * if (splicing_point_flag == '1') {
918 * splice_countdown 8 tcimsbf
920 * if(transport_private_data_flag == '1') {
921 * transport_private_data_length 8 uimsbf
922 * for (i=0; i<transport_private_data_length;i++){
923 * private_data_byte 8 bslbf
926 * if (adaptation_field_extension_flag == '1' ) {
927 * adaptation_field_extension_length 8 uimsbf
929 * piecewise_rate_flag 1 bslbf
930 * seamless_splice_flag 1 bslbf
932 * if (ltw_flag == '1') {
933 * ltw_valid_flag 1 bslbf
934 * ltw_offset 15 uimsbf
936 * if (piecewise_rate_flag == '1') {
938 * piecewise_rate 22 uimsbf
940 * if (seamless_splice_flag == '1'){
941 * splice_type 4 bslbf
942 * DTS_next_AU[32..30] 3 bslbf
944 * DTS_next_AU[29..15] 15 bslbf
946 * DTS_next_AU[14..0] 15 bslbf
949 * for ( i=0;i<N;i++) {
954 * stuffing_byte 8 bslbf
960 tsmux_write_adaptation_field (guint8 * buf,
961 TsMuxPacketInfo * pi, guint8 min_length, guint8 * written)
966 g_assert (min_length <= TSMUX_PAYLOAD_LENGTH);
968 /* Write out all the fields from the packet info only if the
969 * user set the flag to request the adaptation field - if the flag
970 * isn't set, we're just supposed to write stuffing bytes */
971 if (pi->flags & TSMUX_PACKET_FLAG_ADAPTATION) {
972 TS_DEBUG ("writing adaptation fields");
973 if (pi->flags & TSMUX_PACKET_FLAG_DISCONT)
975 if (pi->flags & TSMUX_PACKET_FLAG_RANDOM_ACCESS)
977 if (pi->flags & TSMUX_PACKET_FLAG_PRIORITY)
979 if (pi->flags & TSMUX_PACKET_FLAG_WRITE_PCR) {
983 pcr_base = (pi->pcr / 300);
984 pcr_ext = (pi->pcr % 300);
987 TS_DEBUG ("Writing PCR %" G_GUINT64_FORMAT " + ext %u", pcr_base,
989 buf[pos++] = (pcr_base >> 25) & 0xff;
990 buf[pos++] = (pcr_base >> 17) & 0xff;
991 buf[pos++] = (pcr_base >> 9) & 0xff;
992 buf[pos++] = (pcr_base >> 1) & 0xff;
993 buf[pos++] = ((pcr_base << 7) & 0x80) | 0x7e | ((pcr_ext >> 8) & 0x01); /* set 6 reserve bits to 1 */
994 buf[pos++] = (pcr_ext) & 0xff;
996 if (pi->flags & TSMUX_PACKET_FLAG_WRITE_OPCR) {
1000 opcr_base = (pi->opcr / 300);
1001 opcr_ext = (pi->opcr % 300);
1004 TS_DEBUG ("Writing OPCR");
1005 buf[pos++] = (opcr_base >> 25) & 0xff;
1006 buf[pos++] = (opcr_base >> 17) & 0xff;
1007 buf[pos++] = (opcr_base >> 9) & 0xff;
1008 buf[pos++] = (opcr_base >> 1) & 0xff;
1009 buf[pos++] = ((opcr_base << 7) & 0x80) | 0x7e | ((opcr_ext >> 8) & 0x01); /* set 6 reserve bits to 1 */
1010 buf[pos++] = (opcr_ext) & 0xff;
1012 if (pi->flags & TSMUX_PACKET_FLAG_WRITE_SPLICE) {
1014 buf[pos++] = pi->splice_countdown;
1016 if (pi->private_data_len > 0) {
1018 /* Private data to write, ensure we have enough room */
1019 if ((1 + pi->private_data_len) > (TSMUX_PAYLOAD_LENGTH - pos))
1021 buf[pos++] = pi->private_data_len;
1022 memcpy (&(buf[pos]), pi->private_data, pi->private_data_len);
1023 pos += pi->private_data_len;
1024 TS_DEBUG ("%u bytes of private data", pi->private_data_len);
1026 if (pi->flags & TSMUX_PACKET_FLAG_WRITE_ADAPT_EXT) {
1028 TS_DEBUG ("FIXME: write Adaptation extension");
1029 /* Write an empty extension for now */
1031 buf[pos++] = 0x1f; /* lower 5 bits are reserved, and should be all 1 */
1034 /* Write the flags at the start */
1037 /* Stuffing bytes if needed */
1038 while (pos < min_length)
1041 /* Write the adaptation field length, which doesn't include its own byte */
1051 tsmux_write_ts_header (TsMux * mux, guint8 * buf, TsMuxPacketInfo * pi,
1052 guint * payload_len_out, guint * payload_offset_out, guint stream_avail)
1055 guint8 adaptation_flag = 0;
1056 guint8 adapt_min_length = 0;
1057 guint8 adapt_len = 0;
1059 gboolean write_adapt = FALSE;
1062 buf[0] = TSMUX_SYNC_BYTE;
1064 TS_DEBUG ("PID 0x%04x, counter = 0x%01x, %u bytes avail", pi->pid,
1065 mux->pid_packet_counts[pi->pid] & 0x0f, stream_avail);
1068 * transport_error_indicator
1069 * payload_unit_start_indicator
1070 * transport_priority: (00)
1074 if (pi->packet_start_unit_indicator) {
1075 tsmux_put16 (&tmp, 0x4000 | pi->pid);
1077 tsmux_put16 (&tmp, pi->pid);
1079 /* 2 bits: scrambling_control (NOT SUPPORTED) (00)
1080 * 2 bits: adaptation field control (1x has_adaptation_field | x1 has_payload)
1081 * 4 bits: continuity counter (xxxx)
1084 if (pi->flags & TSMUX_PACKET_FLAG_ADAPTATION) {
1088 if (stream_avail < TSMUX_PAYLOAD_LENGTH) {
1089 /* Need an adaptation field regardless for stuffing */
1090 adapt_min_length = TSMUX_PAYLOAD_LENGTH - stream_avail;
1097 /* Flag the adaptation field presence */
1098 adaptation_flag |= 0x20;
1099 res = tsmux_write_adaptation_field (buf + TSMUX_HEADER_LENGTH,
1100 pi, adapt_min_length, &adapt_len);
1101 if (G_UNLIKELY (res == FALSE))
1104 /* Should have written at least the number of bytes we requested */
1105 g_assert (adapt_len >= adapt_min_length);
1108 /* The amount of packet data we wrote is the remaining space after
1109 * the adaptation field */
1110 *payload_len_out = payload_len = TSMUX_PAYLOAD_LENGTH - adapt_len;
1111 *payload_offset_out = TSMUX_HEADER_LENGTH + adapt_len;
1113 /* Now if we are going to write out some payload, flag that fact */
1114 if (payload_len > 0 && stream_avail > 0) {
1115 /* Flag the presence of a payload */
1116 adaptation_flag |= 0x10;
1118 /* We must have enough data to fill the payload, or some calculation
1120 g_assert (payload_len <= stream_avail);
1122 /* Packet with payload, increment the continuity counter */
1123 mux->pid_packet_counts[pi->pid]++;
1126 adaptation_flag |= mux->pid_packet_counts[pi->pid] & 0x0f;
1128 /* Write the byte of transport_scrambling_control, adaptation_field_control
1129 * + continuity counter out */
1130 buf[3] = adaptation_flag;
1134 TS_DEBUG ("Adaptation field of size >= %d + %d bytes payload",
1135 adapt_len, payload_len);
1137 TS_DEBUG ("Payload of %d bytes only", payload_len);
1143 /* The unused_arg is needed for g_hash_table_foreach() */
1145 tsmux_section_write_packet (gpointer unused_arg,
1146 TsMuxSection * section, TsMux * mux)
1148 GstBuffer *section_buffer;
1149 GstBuffer *packet_buffer = NULL;
1153 gsize data_size = 0;
1154 gsize payload_written;
1155 guint len = 0, offset = 0, payload_len = 0;
1156 guint extra_alloc_bytes = 0;
1158 g_return_val_if_fail (section != NULL, FALSE);
1159 g_return_val_if_fail (mux != NULL, FALSE);
1161 /* Mark the start of new PES unit */
1162 section->pi.packet_start_unit_indicator = TRUE;
1164 data = gst_mpegts_section_packetize (section->section, &data_size);
1167 TS_DEBUG ("Could not packetize section");
1171 /* Mark payload data size */
1172 section->pi.stream_avail = data_size;
1173 payload_written = 0;
1175 /* Wrap section data in a buffer without free function.
1176 The data will be freed when the GstMpegtsSection is destroyed. */
1177 section_buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
1178 data, data_size, 0, data_size, NULL, NULL);
1180 TS_DEBUG ("Section buffer with size %" G_GSIZE_FORMAT " created",
1181 gst_buffer_get_size (section_buffer));
1183 while (section->pi.stream_avail > 0) {
1185 packet = g_malloc (TSMUX_PACKET_LENGTH);
1187 if (section->pi.packet_start_unit_indicator) {
1188 /* Wee need room for a pointer byte */
1189 section->pi.stream_avail++;
1191 if (!tsmux_write_ts_header (mux, packet, §ion->pi, &len, &offset,
1192 section->pi.stream_avail))
1195 /* Write the pointer byte */
1196 packet[offset++] = 0x00;
1197 payload_len = len - 1;
1200 if (!tsmux_write_ts_header (mux, packet, §ion->pi, &len, &offset,
1201 section->pi.stream_avail))
1206 /* Wrap the TS header and adaption field in a GstMemory */
1207 mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
1208 packet, TSMUX_PACKET_LENGTH, 0, offset, packet, g_free);
1210 TS_DEBUG ("Creating packet buffer at offset "
1211 "%" G_GSIZE_FORMAT " with length %u", payload_written, payload_len);
1213 /* If in M2TS mode, we will need to resize to 4 bytes after the end
1214 of the buffer. For performance reasons, we will now try to include
1215 4 extra bytes from the source buffer, then resize down, to avoid
1216 having an extra 4 byte GstMemory appended. If the source buffer
1217 does not have enough data for this, a new GstMemory will be used */
1218 if (gst_buffer_get_size (section_buffer) - (payload_written +
1219 payload_len) >= 4) {
1221 extra_alloc_bytes = 4;
1223 extra_alloc_bytes = 0;
1225 packet_buffer = gst_buffer_copy_region (section_buffer, GST_BUFFER_COPY_ALL,
1226 payload_written, payload_len + extra_alloc_bytes);
1228 /* Prepend the header to the section data */
1229 gst_buffer_prepend_memory (packet_buffer, mem);
1231 /* add an extra 4 bytes if it could not be reserved already */
1232 if (extra_alloc_bytes == 4) {
1233 /* we allocated those already, resize */
1234 gst_buffer_set_size (packet_buffer,
1235 gst_buffer_get_size (packet_buffer) - extra_alloc_bytes);
1237 void *ptr = g_malloc (4);
1239 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, ptr, 4, 0, 0, ptr,
1241 gst_buffer_append_memory (packet_buffer, extra);
1244 TS_DEBUG ("Writing %d bytes to section. %d bytes remaining",
1245 len, section->pi.stream_avail - len);
1247 /* Push the packet without PCR */
1248 if (G_UNLIKELY (!tsmux_packet_out (mux, packet_buffer, -1))) {
1249 /* Buffer given away */
1250 packet_buffer = NULL;
1254 packet_buffer = NULL;
1255 section->pi.stream_avail -= len;
1256 payload_written += payload_len;
1257 section->pi.packet_start_unit_indicator = FALSE;
1260 gst_buffer_unref (section_buffer);
1267 gst_buffer_unref (section_buffer);
1272 * tsmux_send_section:
1274 * @section: (transfer full): a #GstMpegtsSection to add
1276 * Send a @section immediately on the stream.
1278 * Returns: %TRUE on success, %FALSE otherwise
1281 tsmux_send_section (TsMux * mux, GstMpegtsSection * section)
1284 TsMuxSection tsmux_section;
1286 g_return_val_if_fail (mux != NULL, FALSE);
1287 g_return_val_if_fail (section != NULL, FALSE);
1289 memset (&tsmux_section, 0, sizeof (tsmux_section));
1291 GST_DEBUG ("Sending mpegts section with type %d to mux",
1292 section->section_type);
1294 tsmux_section.section = section;
1295 tsmux_section.pi.pid = section->pid;
1297 ret = tsmux_section_write_packet (NULL, &tsmux_section, mux);
1298 gst_mpegts_section_unref (section);
1304 tsmux_write_si (TsMux * mux)
1306 g_hash_table_foreach (mux->si_sections,
1307 (GHFunc) tsmux_section_write_packet, mux);
1309 mux->si_changed = FALSE;
1316 tsmux_write_null_ts_header (guint8 * buf)
1318 *buf++ = TSMUX_SYNC_BYTE;
1325 ts_to_pcr (gint64 ts)
1327 if (ts == G_MININT64) {
1331 return (ts - TSMUX_PCR_OFFSET) * (TSMUX_SYS_CLOCK_FREQ / TSMUX_CLOCK_FREQ);
1334 /* Calculate the PCR to write into the current packet */
1336 get_current_pcr (TsMux * mux, gint64 cur_ts)
1339 return ts_to_pcr (cur_ts);
1341 if (mux->first_pcr_ts == G_MININT64) {
1342 g_assert (cur_ts != G_MININT64);
1343 mux->first_pcr_ts = cur_ts;
1344 GST_DEBUG ("First PCR offset is %" G_GUINT64_FORMAT, cur_ts);
1347 return ts_to_pcr (mux->first_pcr_ts) +
1348 gst_util_uint64_scale ((mux->n_bytes + PCR_BYTE_OFFSET) * 8,
1349 TSMUX_SYS_CLOCK_FREQ, mux->bitrate);
1352 /* Predict the PCR at the next packet if possible */
1354 get_next_pcr (TsMux * mux, gint64 cur_ts)
1357 return ts_to_pcr (cur_ts);
1359 if (mux->first_pcr_ts == G_MININT64) {
1360 g_assert (cur_ts != G_MININT64);
1361 mux->first_pcr_ts = cur_ts;
1362 GST_DEBUG ("First PCR offset is %" G_GUINT64_FORMAT, cur_ts);
1365 return ts_to_pcr (mux->first_pcr_ts) +
1366 gst_util_uint64_scale ((mux->n_bytes + TSMUX_PACKET_LENGTH +
1367 PCR_BYTE_OFFSET) * 8, TSMUX_SYS_CLOCK_FREQ, mux->bitrate);
1371 write_new_pcr (TsMux * mux, TsMuxStream * stream, gint64 cur_pcr,
1374 if (stream->next_pcr == -1 || next_pcr > stream->next_pcr) {
1376 TSMUX_PACKET_FLAG_ADAPTATION | TSMUX_PACKET_FLAG_WRITE_PCR;
1377 stream->pi.pcr = cur_pcr;
1379 if (mux->bitrate && stream->next_pcr != -1 && cur_pcr >= stream->next_pcr) {
1380 GST_WARNING ("Writing PCR %" G_GUINT64_FORMAT " missed the target %"
1381 G_GUINT64_FORMAT " by %f ms", cur_pcr, stream->next_pcr,
1382 (double) (cur_pcr - stream->next_pcr) / 27000.0);
1384 /* Next PCR deadline is now plus the scheduled interval */
1385 stream->next_pcr = cur_pcr + mux->pcr_interval * 300;
1394 rewrite_si (TsMux * mux, gint64 cur_ts)
1401 next_pcr = get_next_pcr (mux, cur_ts);
1403 /* check if we need to rewrite pat */
1404 if (mux->next_pat_pcr == -1 || mux->pat_changed)
1406 else if (next_pcr > mux->next_pat_pcr)
1412 if (mux->next_pat_pcr == -1)
1413 mux->next_pat_pcr = next_pcr + mux->pat_interval * 300;
1415 mux->next_pat_pcr += mux->pat_interval * 300;
1417 if (!tsmux_write_pat (mux))
1420 next_pcr = get_next_pcr (mux, cur_ts);
1423 /* check if we need to rewrite sit */
1424 if (mux->next_si_pcr == -1 || mux->si_changed)
1426 else if (next_pcr > mux->next_si_pcr)
1432 if (mux->next_si_pcr == -1)
1433 mux->next_si_pcr = next_pcr + mux->si_interval * 300;
1435 mux->next_si_pcr += mux->si_interval * 300;
1437 if (!tsmux_write_si (mux))
1440 next_pcr = get_current_pcr (mux, cur_ts);
1443 /* check if we need to rewrite any of the current pmts */
1444 for (cur = mux->programs; cur; cur = cur->next) {
1445 TsMuxProgram *program = (TsMuxProgram *) cur->data;
1448 if (program->next_pmt_pcr == -1 || program->pmt_changed)
1450 else if (next_pcr > program->next_pmt_pcr)
1456 if (program->next_pmt_pcr == -1)
1457 program->next_pmt_pcr = next_pcr + program->pmt_interval * 300;
1459 program->next_pmt_pcr += program->pmt_interval * 300;
1461 if (!tsmux_write_pmt (mux, program))
1464 next_pcr = get_current_pcr (mux, cur_ts);
1467 if (program->scte35_pid != 0) {
1468 gboolean write_scte_null = FALSE;
1469 if (program->next_scte35_pcr == -1)
1470 write_scte_null = TRUE;
1471 else if (next_pcr > program->next_scte35_pcr)
1472 write_scte_null = TRUE;
1474 if (write_scte_null) {
1475 GST_DEBUG ("next scte35 pcr %" G_GINT64_FORMAT,
1476 program->next_scte35_pcr);
1477 if (program->next_scte35_pcr == -1)
1478 program->next_scte35_pcr =
1479 next_pcr + program->scte35_null_interval * 300;
1481 program->next_scte35_pcr += program->scte35_null_interval * 300;
1482 GST_DEBUG ("next scte35 NOW pcr %" G_GINT64_FORMAT,
1483 program->next_scte35_pcr);
1485 if (!tsmux_write_scte_null (mux, program))
1488 next_pcr = get_current_pcr (mux, cur_ts);
1492 program->wrote_si = TRUE;
1499 pad_stream (TsMux * mux, TsMuxStream * stream, gint64 cur_ts)
1502 GstBuffer *buf = NULL;
1504 gboolean ret = TRUE;
1505 GstClockTimeDiff diff;
1506 guint64 start_n_bytes;
1511 if (!GST_CLOCK_STIME_IS_VALID (cur_ts))
1514 if (!GST_CLOCK_STIME_IS_VALID (stream->first_ts))
1515 stream->first_ts = cur_ts;
1517 diff = GST_CLOCK_DIFF (stream->first_ts, cur_ts);
1521 start_n_bytes = mux->n_bytes;
1523 GST_LOG ("Transport stream bitrate: %" G_GUINT64_FORMAT " over %"
1524 G_GUINT64_FORMAT " bytes, duration %" GST_TIME_FORMAT,
1525 gst_util_uint64_scale (mux->n_bytes * 8, TSMUX_CLOCK_FREQ, diff),
1526 mux->n_bytes, GST_TIME_ARGS (diff * GST_SECOND / TSMUX_CLOCK_FREQ));
1528 /* calculate what the overall bitrate will be if we add 1 more packet */
1530 gst_util_uint64_scale ((mux->n_bytes + TSMUX_PACKET_LENGTH) * 8,
1531 TSMUX_CLOCK_FREQ, diff);
1533 if (bitrate <= mux->bitrate) {
1535 guint payload_len, payload_offs;
1537 if (!tsmux_get_buffer (mux, &buf)) {
1542 if (!gst_buffer_map (buf, &map, GST_MAP_WRITE)) {
1543 gst_buffer_unref (buf);
1548 memset (map.data, 0xFF, map.size);
1550 new_pcr = write_new_pcr (mux, stream, get_current_pcr (mux, cur_ts),
1551 get_next_pcr (mux, cur_ts));
1552 if (new_pcr != -1) {
1553 GST_LOG ("Writing PCR-only packet on PID 0x%04x", stream->pi.pid);
1554 tsmux_write_ts_header (mux, map.data, &stream->pi, &payload_len,
1557 GST_LOG ("Writing null stuffing packet");
1558 if (!rewrite_si (mux, cur_ts)) {
1559 gst_buffer_unmap (buf, &map);
1560 gst_buffer_unref (buf);
1564 tsmux_write_null_ts_header (map.data);
1567 gst_buffer_unmap (buf, &map);
1569 stream->pi.flags &= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
1570 ret = tsmux_packet_out (mux, buf, new_pcr);
1572 gst_buffer_unref (buf);
1576 } while (bitrate < mux->bitrate);
1578 if (mux->n_bytes != start_n_bytes) {
1579 GST_LOG ("Finished padding the mux");
1587 * tsmux_write_stream_packet:
1589 * @stream: a #TsMuxStream
1591 * Write a packet of @stream.
1593 * Returns: TRUE if the packet could be written.
1596 tsmux_write_stream_packet (TsMux * mux, TsMuxStream * stream)
1598 guint payload_len, payload_offs;
1599 TsMuxPacketInfo *pi = &stream->pi;
1601 gint64 new_pcr = -1;
1602 GstBuffer *buf = NULL;
1605 g_return_val_if_fail (mux != NULL, FALSE);
1606 g_return_val_if_fail (stream != NULL, FALSE);
1608 if (tsmux_stream_is_pcr (stream)) {
1609 gint64 cur_ts = CLOCK_BASE;
1610 if (tsmux_stream_get_dts (stream) != G_MININT64)
1611 cur_ts += tsmux_stream_get_dts (stream);
1613 cur_ts += tsmux_stream_get_pts (stream);
1615 if (!rewrite_si (mux, cur_ts))
1618 if (!pad_stream (mux, stream, cur_ts))
1622 write_new_pcr (mux, stream, get_current_pcr (mux, cur_ts),
1623 get_next_pcr (mux, cur_ts));
1626 pi->packet_start_unit_indicator = tsmux_stream_at_pes_start (stream);
1627 if (pi->packet_start_unit_indicator) {
1628 tsmux_stream_initialize_pes_packet (stream);
1629 if (stream->dts != G_MININT64)
1630 stream->dts += CLOCK_BASE;
1631 if (stream->pts != G_MININT64)
1632 stream->pts += CLOCK_BASE;
1634 pi->stream_avail = tsmux_stream_bytes_avail (stream);
1637 if (!tsmux_get_buffer (mux, &buf))
1640 gst_buffer_map (buf, &map, GST_MAP_READ);
1642 if (!tsmux_write_ts_header (mux, map.data, pi, &payload_len, &payload_offs,
1647 if (!tsmux_stream_get_data (stream, map.data + payload_offs, payload_len))
1650 gst_buffer_unmap (buf, &map);
1652 GST_DEBUG ("Writing PES of size %d", (int) gst_buffer_get_size (buf));
1653 res = tsmux_packet_out (mux, buf, new_pcr);
1655 /* Reset all dynamic flags */
1656 stream->pi.flags &= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
1664 gst_buffer_unmap (buf, &map);
1665 gst_buffer_unref (buf);
1672 * tsmux_program_free:
1673 * @program: a #TsMuxProgram
1675 * Free the resources of @program. After this call @program can not be used
1679 tsmux_program_free (TsMuxProgram * program)
1681 g_return_if_fail (program != NULL);
1683 /* Free PMT section */
1684 if (program->pmt.section)
1685 gst_mpegts_section_unref (program->pmt.section);
1686 if (program->scte35_null_section)
1687 tsmux_section_free (program->scte35_null_section);
1689 g_ptr_array_free (program->streams, TRUE);
1690 g_slice_free (TsMuxProgram, program);
1694 * tsmux_program_set_pmt_pid:
1695 * @program: A #TsmuxProgram
1696 * @pmt_pid: PID to write PMT for this program
1699 tsmux_program_set_pmt_pid (TsMuxProgram * program, guint16 pmt_pid)
1701 program->pmt_pid = pmt_pid;
1705 compare_program_number (gconstpointer a, gconstpointer b)
1707 const GstMpegtsPatProgram *pgm1 = *(const GstMpegtsPatProgram * const *) a;
1708 const GstMpegtsPatProgram *pgm2 = *(const GstMpegtsPatProgram * const *) b;
1709 gint num1 = pgm1->program_number, num2 = pgm2->program_number;
1715 tsmux_write_pat (TsMux * mux)
1718 if (mux->pat_changed) {
1719 /* program_association_section ()
1720 * for (i = 0; i < N; i++) {
1721 * program_number 16 uimsbf
1723 * network_PID_or_program_map_PID 13 uimbsf
1730 pat = gst_mpegts_pat_new ();
1732 for (cur = mux->programs; cur; cur = cur->next) {
1733 GstMpegtsPatProgram *pat_pgm;
1734 TsMuxProgram *program = (TsMuxProgram *) cur->data;
1736 pat_pgm = gst_mpegts_pat_program_new ();
1737 pat_pgm->program_number = program->pgm_number;
1738 pat_pgm->network_or_program_map_PID = program->pmt_pid;
1740 g_ptr_array_add (pat, pat_pgm);
1743 g_ptr_array_sort (pat, compare_program_number);
1745 if (mux->pat.section)
1746 gst_mpegts_section_unref (mux->pat.section);
1748 mux->pat.section = gst_mpegts_section_from_pat (pat, mux->transport_id);
1750 mux->pat.section->version_number = mux->pat_version++;
1752 TS_DEBUG ("PAT has %d programs", mux->nb_programs);
1753 mux->pat_changed = FALSE;
1756 return tsmux_section_write_packet (NULL, &mux->pat, mux);
1760 tsmux_write_pmt (TsMux * mux, TsMuxProgram * program)
1763 if (program->pmt_changed) {
1764 /* program_association_section ()
1768 * program_info_length 12 uimsbf
1769 * for (i = 0; i < N; i++)
1772 * for (i = 0; i < N1; i++) {
1773 * stream_type 8 uimsbf
1775 * elementary_PID 13 uimbsf
1777 * ES_info_length 12 uimbsf
1778 * for (i = 0; i < N1; i++) {
1783 GstMpegtsDescriptor *descriptor;
1786 /* See note about bluray descriptors below */
1787 guint8 desc[] = { 0x0F, 0xFF, 0xFC, 0xFC };
1791 pmt = gst_mpegts_pmt_new ();
1793 if (program->pcr_stream == NULL)
1794 pmt->pcr_pid = 0x1FFF;
1796 pmt->pcr_pid = tsmux_stream_get_pid (program->pcr_stream);
1799 /* FIXME : These two descriptors should not be added in all PMT
1800 * but only in "bluray-compatible" mpeg-ts output. I even have my
1801 * doubt whether the DTCP descriptor is even needed */
1802 descriptor = gst_mpegts_descriptor_from_registration ("HDMV", NULL, 0);
1803 g_ptr_array_add (pmt->descriptors, descriptor);
1805 /* DTCP descriptor, see
1806 * http://www.dtcp.com/documents/dtcp/info-20150204-dtcp-v1-rev%201-71.pdf */
1807 descriptor = gst_mpegts_descriptor_from_custom (0x88, desc, 4);
1808 g_ptr_array_add (pmt->descriptors, descriptor);
1811 /* Will SCTE-35 be potentially used ? */
1812 if (program->scte35_pid != 0) {
1813 descriptor = gst_mpegts_descriptor_from_registration ("CUEI", NULL, 0);
1814 g_ptr_array_add (pmt->descriptors, descriptor);
1817 /* Write out the entries */
1818 for (i = 0; i < program->streams->len; i++) {
1819 GstMpegtsPMTStream *pmt_stream;
1820 TsMuxStream *stream = g_ptr_array_index (program->streams, i);
1822 pmt_stream = gst_mpegts_pmt_stream_new ();
1824 /* FIXME: Use API to retrieve this from the stream */
1825 pmt_stream->stream_type = stream->stream_type;
1826 pmt_stream->pid = tsmux_stream_get_pid (stream);
1828 /* Write any ES descriptors needed */
1829 tsmux_stream_get_es_descrs (stream, pmt_stream);
1830 g_ptr_array_add (pmt->streams, pmt_stream);
1833 /* Will SCTE-35 be potentially used ? */
1834 if (program->scte35_pid != 0) {
1835 GstMpegtsPMTStream *pmt_stream = gst_mpegts_pmt_stream_new ();
1836 pmt_stream->stream_type = GST_MPEGTS_STREAM_TYPE_SCTE_SIT;
1837 pmt_stream->pid = program->scte35_pid;
1838 g_ptr_array_add (pmt->streams, pmt_stream);
1841 TS_DEBUG ("PMT for program %d has %d streams",
1842 program->pgm_number, program->streams->len);
1844 pmt->program_number = program->pgm_number;
1846 program->pmt.pi.pid = program->pmt_pid;
1847 program->pmt_changed = FALSE;
1849 if (program->pmt.section)
1850 gst_mpegts_section_unref (program->pmt.section);
1852 program->pmt.section = gst_mpegts_section_from_pmt (pmt, program->pmt_pid);
1853 program->pmt.section->version_number = program->pmt_version++;
1856 return tsmux_section_write_packet (NULL, &program->pmt, mux);
1860 tsmux_write_scte_null (TsMux * mux, TsMuxProgram * program)
1862 /* SCTE-35 NULL section is created when PID is set */
1863 GST_LOG ("Writing SCTE NULL packet");
1864 return tsmux_section_write_packet (NULL, program->scte35_null_section, mux);
1868 tsmux_set_bitrate (TsMux * mux, guint64 bitrate)
1870 mux->bitrate = bitrate;