1 /*
2  * Copyright 2006 BBC and Fluendo S.A.
3  *
4  * This library is licensed under 4 different licenses and you
5  * can choose to use it under the terms of any one of them. The
6  * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
7  * license.
8  *
9  * MPL:
10  *
11  * The contents of this file are subject to the Mozilla Public License
12  * Version 1.1 (the "License"); you may not use this file except in
13  * compliance with the License. You may obtain a copy of the License at
14  * http://www.mozilla.org/MPL/.
15  *
16  * Software distributed under the License is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18  * License for the specific language governing rights and limitations
19  * under the License.
20  *
21  * LGPL:
22  *
23  * This library is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU Library General Public
25  * License as published by the Free Software Foundation; either
26  * version 2 of the License, or (at your option) any later version.
27  *
28  * This library is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31  * Library General Public License for more details.
32  *
33  * You should have received a copy of the GNU Library General Public
34  * License along with this library; if not, write to the
35  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
36  * Boston, MA 02110-1301, USA.
37  *
38  * GPL:
39  *
40  * This program is free software; you can redistribute it and/or modify
41  * it under the terms of the GNU General Public License as published by
42  * the Free Software Foundation; either version 2 of the License, or
43  * (at your option) any later version.
44  *
45  * This program is distributed in the hope that it will be useful,
46  * but WITHOUT ANY WARRANTY; without even the implied warranty of
47  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48  * GNU General Public License for more details.
49  *
50  * You should have received a copy of the GNU General Public License
51  * along with this program; if not, write to the Free Software
52  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
53  *
54  * MIT:
55  *
56  * Unless otherwise indicated, Source Code is licensed under MIT license.
57  * See further explanation attached in License Statement (distributed in the file
58  * LICENSE).
59  *
60  * Permission is hereby granted, free of charge, to any person obtaining a copy of
61  * this software and associated documentation files (the "Software"), to deal in
62  * the Software without restriction, including without limitation the rights to
63  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
64  * of the Software, and to permit persons to whom the Software is furnished to do
65  * so, subject to the following conditions:
66  *
67  * The above copyright notice and this permission notice shall be included in all
68  * copies or substantial portions of the Software.
69  *
70  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
73  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
75  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
76  * SOFTWARE.
77  *
78  */
79 
80 #ifndef __TSMUX_H__
81 #define __TSMUX_H__
82 
83 #include <glib.h>
84 
85 #include <gst/mpegts/mpegts.h>
86 
87 #include "tsmuxcommon.h"
88 #include "tsmuxstream.h"
89 
90 G_BEGIN_DECLS
91 
92 #define TSMUX_MAX_ES_INFO_LENGTH ((1 << 12) - 1)
93 
94 #define TSMUX_PID_AUTO ((guint16)-1)
95 
96 #define TSMUX_START_PROGRAM_ID 0x0001
97 #define TSMUX_START_PMT_PID 0x0020
98 #define TSMUX_START_ES_PID 0x0040
99 
100 typedef struct TsMuxSection TsMuxSection;
101 typedef struct TsMux TsMux;
102 
103 typedef gboolean (*TsMuxWriteFunc) (GstBuffer * buf, void *user_data, gint64 new_pcr);
104 typedef void (*TsMuxAllocFunc) (GstBuffer ** buf, void *user_data);
105 
106 struct TsMuxSection {
107   TsMuxPacketInfo pi;
108   GstMpegtsSection *section;
109 };
110 
111 /* Information for the streams associated with one program */
112 struct TsMuxProgram {
113   TsMuxSection pmt;
114   /* PMT version */
115   guint8   pmt_version;
116   /* trigger for writing PMT */
117   gboolean pmt_changed;
118 
119   /* interval between PMT in MPEG PTS clock time */
120   guint    pmt_interval;
121   /* last time PMT written in MPEG PTS clock time */
122   gint64   last_pmt_ts;
123 
124   /* program ID for the PAT */
125   guint16 pgm_number;
126   /* PID to write the PMT */
127   guint16 pmt_pid;
128 
129   /* stream which carries the PCR */
130   TsMuxStream *pcr_stream;
131 
132   /* programs TsMuxStream's */
133   GArray *streams;
134 };
135 
136 struct TsMux {
137   /* TsMuxStream* array of all streams */
138   guint nb_streams;
139   GList *streams;
140 
141   /* TsMuxProgram* array of all programs */
142   guint nb_programs;
143   GList *programs;
144 
145   /* next auto-generated misc id */
146   guint16 next_pgm_no;
147   guint16 next_pmt_pid;
148   guint16 next_stream_pid;
149 
150   /* Table with TsMuxSection to write */
151   GHashTable *si_sections;
152 
153   TsMuxSection pat;
154   /* PAT transport_stream_id */
155   guint16 transport_id;
156   /* PAT version */
157   guint8   pat_version;
158   /* trigger writing PAT */
159   gboolean pat_changed;
160   /* interval between PAT in MPEG PTS clock time */
161   guint    pat_interval;
162   /* last time PAT written in MPEG PTS clock time */
163   gint64   last_pat_ts;
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   /* last time SIT written in MPEG PTS clock time */
170   gint64   last_si_ts;
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 
179   /* scratch space for writing ES_info descriptors */
180   guint8 es_info_buf[TSMUX_MAX_ES_INFO_LENGTH];
181 };
182 
183 /* create/free new muxer session */
184 TsMux *		tsmux_new 			(void);
185 void 		tsmux_free 			(TsMux *mux);
186 
187 /* Setting muxing session properties */
188 void 		tsmux_set_write_func 		(TsMux *mux, TsMuxWriteFunc func, void *user_data);
189 void 		tsmux_set_alloc_func 		(TsMux *mux, TsMuxAllocFunc func, void *user_data);
190 void 		tsmux_set_pat_interval          (TsMux *mux, guint interval);
191 guint 		tsmux_get_pat_interval          (TsMux *mux);
192 void 		tsmux_resend_pat                (TsMux *mux);
193 guint16		tsmux_get_new_pid 		(TsMux *mux);
194 
195 /* pid/program management */
196 TsMuxProgram *	tsmux_program_new 		(TsMux *mux, gint prog_id);
197 void 		tsmux_program_free 		(TsMuxProgram *program);
198 void 		tsmux_set_pmt_interval          (TsMuxProgram *program, guint interval);
199 guint 		tsmux_get_pmt_interval   	(TsMuxProgram *program);
200 void 		tsmux_resend_pmt                (TsMuxProgram *program);
201 
202 /* SI table management */
203 void            tsmux_set_si_interval           (TsMux *mux, guint interval);
204 guint           tsmux_get_si_interval           (TsMux *mux);
205 void            tsmux_resend_si                 (TsMux *mux);
206 gboolean        tsmux_add_mpegts_si_section     (TsMux * mux, GstMpegtsSection * section);
207 
208 /* stream management */
209 TsMuxStream *	tsmux_create_stream 		(TsMux *mux, TsMuxStreamType stream_type, guint16 pid, gchar *language);
210 TsMuxStream *	tsmux_find_stream 		(TsMux *mux, guint16 pid);
211 
212 void 		tsmux_program_add_stream 	(TsMuxProgram *program, TsMuxStream *stream);
213 void 		tsmux_program_set_pcr_stream 	(TsMuxProgram *program, TsMuxStream *stream);
214 
215 /* writing stuff */
216 gboolean 	tsmux_write_stream_packet 	(TsMux *mux, TsMuxStream *stream);
217 
218 G_END_DECLS
219 
220 #endif
221