• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 BBC and Fluendo S.A.
3  *
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.
7  *
8  * MPL:
9  *
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/.
14  *
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
18  * under the License.
19  *
20  * LGPL:
21  *
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.
26  *
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.
31  *
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.
36  *
37  * MIT:
38  *
39  * Unless otherwise indicated, Source Code is licensed under MIT license.
40  * See further explanation attached in License Statement (distributed in the file
41  * LICENSE).
42  *
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:
49  *
50  * The above copyright notice and this permission notice shall be included in all
51  * copies or substantial portions of the Software.
52  *
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
59  * SOFTWARE.
60  *
61  * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
62  */
63 
64 #ifndef __TSMUX_H__
65 #define __TSMUX_H__
66 
67 #include <glib.h>
68 
69 #include <gst/mpegts/mpegts.h>
70 
71 #include "tsmuxcommon.h"
72 #include "tsmuxstream.h"
73 
74 G_BEGIN_DECLS
75 
76 #define TSMUX_MAX_ES_INFO_LENGTH ((1 << 12) - 1)
77 
78 #define TSMUX_PID_AUTO ((guint16)-1)
79 
80 #define TSMUX_START_PROGRAM_ID 0x0001
81 #define TSMUX_START_PMT_PID 0x0020
82 #define TSMUX_START_ES_PID 0x0040
83 
84 typedef struct TsMuxSection TsMuxSection;
85 typedef struct TsMux TsMux;
86 
87 typedef gboolean (*TsMuxWriteFunc) (GstBuffer * buf, void *user_data, gint64 new_pcr);
88 typedef void (*TsMuxAllocFunc) (GstBuffer ** buf, void *user_data);
89 typedef TsMuxStream * (*TsMuxNewStreamFunc) (guint16 new_pid, guint stream_type, void *user_data);
90 
91 struct TsMuxSection {
92   TsMuxPacketInfo pi;
93   GstMpegtsSection *section;
94 };
95 
96 /* Information for the streams associated with one program */
97 struct TsMuxProgram {
98   /* TRUE if the SI has been written at least once */
99   gboolean wrote_si;
100 
101   TsMuxSection pmt;
102   /* PMT version */
103   guint8   pmt_version;
104   /* trigger for writing PMT */
105   gboolean pmt_changed;
106 
107   /* interval between PMT in MPEG PTS clock time */
108   guint    pmt_interval;
109 
110   /* Next PMT position, 27 MHz */
111   gint64   next_pmt_pcr;
112 
113   /* program ID for the PAT */
114   guint16 pgm_number;
115   /* PID to write the PMT */
116   guint16 pmt_pid;
117 
118   TsMuxSection *scte35_null_section;
119   /* SCTE-35 pid (0 if inactive/unused) */
120   guint16 scte35_pid;
121   /* Interval between SCTE-35 NULL packets in MPEG PTS clock time */
122   guint   scte35_null_interval;
123   /* Next SCTE-35 position, 27 MHz */
124   gint64  next_scte35_pcr;
125 
126   /* stream which carries the PCR */
127   TsMuxStream *pcr_stream;
128 
129   /* programs TsMuxStream's */
130   GPtrArray *streams;
131 };
132 
133 struct TsMux {
134   /* TsMuxStream* array of all streams */
135   guint nb_streams;
136   GList *streams;
137 
138   /* TsMuxProgram* array of all programs */
139   guint nb_programs;
140   GList *programs;
141 
142   /* next auto-generated misc id */
143   guint16 next_pgm_no;
144   guint16 next_pmt_pid;
145   guint16 next_stream_pid;
146 
147   /* Table with TsMuxSection to write */
148   GHashTable *si_sections;
149 
150   TsMuxSection pat;
151   /* PAT transport_stream_id */
152   guint16 transport_id;
153   /* PAT version */
154   guint8   pat_version;
155   /* trigger writing PAT */
156   gboolean pat_changed;
157   /* interval between PAT in MPEG PTS clock time */
158   guint    pat_interval;
159   /* Next PAT position, 27 MHz */
160   gint64   next_pat_pcr;
161 
162   /* interval between PCR in MPEG PTS clock time */
163   guint    pcr_interval;
164 
165   /* trigger writing Service Information Tables */
166   gboolean si_changed;
167   /* interval between SIT in MPEG PTS clock time */
168   guint    si_interval;
169   /* Next SIT position, 27 MHz */
170   gint64   next_si_pcr;
171 
172   /* callback to write finished packet */
173   TsMuxWriteFunc write_func;
174   void *write_func_data;
175   /* callback to alloc new packet buffer */
176   TsMuxAllocFunc alloc_func;
177   void *alloc_func_data;
178   /* callback to create a new stream */
179   TsMuxNewStreamFunc new_stream_func;
180   void *new_stream_data;
181 
182   /* scratch space for writing ES_info descriptors */
183   guint8 es_info_buf[TSMUX_MAX_ES_INFO_LENGTH];
184 
185   guint64 bitrate;
186   guint64 n_bytes;
187 
188   /* For the per-PID continuity counter */
189   guint8 pid_packet_counts[8192];
190 
191   gint64 first_pcr_ts;
192 };
193 
194 /* create/free new muxer session */
195 TsMux *		tsmux_new 			(void);
196 void 		tsmux_free 			(TsMux *mux);
197 
198 /* Setting muxing session properties */
199 void 		tsmux_set_write_func 		(TsMux *mux, TsMuxWriteFunc func, void *user_data);
200 void 		tsmux_set_alloc_func 		(TsMux *mux, TsMuxAllocFunc func, void *user_data);
201 void    tsmux_set_new_stream_func (TsMux * mux, TsMuxNewStreamFunc func, void *user_data);
202 void 		tsmux_set_pat_interval          (TsMux *mux, guint interval);
203 guint 		tsmux_get_pat_interval          (TsMux *mux);
204 void 		tsmux_resend_pat                (TsMux *mux);
205 guint16		tsmux_get_new_pid 		(TsMux *mux);
206 void    tsmux_set_bitrate       (TsMux *mux, guint64 bitrate);
207 
208 /* pid/program management */
209 TsMuxProgram *	tsmux_program_new 		(TsMux *mux, gint prog_id);
210 void 		tsmux_program_free 		(TsMuxProgram *program);
211 void 		tsmux_program_set_pmt_pid (TsMuxProgram *program, guint16 pmt_pid);
212 void 		tsmux_set_pmt_interval          (TsMuxProgram *program, guint interval);
213 guint 		tsmux_get_pmt_interval   	(TsMuxProgram *program);
214 void 		tsmux_resend_pmt                (TsMuxProgram *program);
215 void            tsmux_program_set_scte35_pid    (TsMuxProgram *program, guint16 pid);
216 guint16         tsmux_program_get_scte35_pid    (TsMuxProgram *program);
217 void            tsmux_program_set_scte35_interval (TsMuxProgram *mux, guint interval);
218 gboolean        tsmux_program_delete            (TsMux *mux, TsMuxProgram *program);
219 
220 
221 /* SI table management */
222 void            tsmux_set_si_interval           (TsMux *mux, guint interval);
223 guint           tsmux_get_si_interval           (TsMux *mux);
224 void            tsmux_resend_si                 (TsMux *mux);
225 gboolean        tsmux_add_mpegts_si_section     (TsMux * mux, GstMpegtsSection * section);
226 
227 /* One-time sections */
228 gboolean        tsmux_send_section              (TsMux *mux, GstMpegtsSection *section);
229 
230 /* stream management */
231 TsMuxStream *	tsmux_create_stream 		(TsMux *mux, guint stream_type, guint16 pid, gchar *language);
232 TsMuxStream *	tsmux_find_stream 		(TsMux *mux, guint16 pid);
233 gboolean        tsmux_remove_stream             (TsMux *mux, guint16 pid, TsMuxProgram *program);
234 
235 void 		tsmux_program_add_stream 	(TsMuxProgram *program, TsMuxStream *stream);
236 void 		tsmux_program_set_pcr_stream 	(TsMuxProgram *program, TsMuxStream *stream);
237 void    tsmux_set_pcr_interval (TsMux * mux, guint freq);
238 
239 /* writing stuff */
240 gboolean 	tsmux_write_stream_packet 	(TsMux *mux, TsMuxStream *stream);
241 
242 G_END_DECLS
243 
244 #endif
245