• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * This library is licensed under 3 different licenses and you
8  * can choose to use it under the terms of any one of them. The
9  * three licenses are the MPL 1.1, the LGPL and the MIT license.
10  *
11  * MPL:
12  *
13  * The contents of this file are subject to the Mozilla Public License
14  * Version 1.1 (the "License"); you may not use this file except in
15  * compliance with the License. You may obtain a copy of the License at
16  * http://www.mozilla.org/MPL/.
17  *
18  * Software distributed under the License is distributed on an "AS IS"
19  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
20  * License for the specific language governing rights and limitations
21  * under the License.
22  *
23  * LGPL:
24  *
25  * This library is free software; you can redistribute it and/or
26  * modify it under the terms of the GNU Library General Public
27  * License as published by the Free Software Foundation; either
28  * version 2 of the License, or (at your option) any later version.
29  *
30  * This library is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
33  * Library General Public License for more details.
34  *
35  * You should have received a copy of the GNU Library General Public
36  * License along with this library; if not, write to the
37  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
38  * Boston, MA 02110-1301, USA.
39  *
40  * MIT:
41  *
42  * Unless otherwise indicated, Source Code is licensed under MIT license.
43  * See further explanation attached in License Statement (distributed in the file
44  * LICENSE).
45  *
46  * Permission is hereby granted, free of charge, to any person obtaining a copy of
47  * this software and associated documentation files (the "Software"), to deal in
48  * the Software without restriction, including without limitation the rights to
49  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
50  * of the Software, and to permit persons to whom the Software is furnished to do
51  * so, subject to the following conditions:
52  *
53  * The above copyright notice and this permission notice shall be included in all
54  * copies or substantial portions of the Software.
55  *
56  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62  * SOFTWARE.
63  *
64  * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
65  */
66 
67 #ifndef __BASETSMUX_H__
68 #define __BASETSMUX_H__
69 
70 #include <gst/gst.h>
71 #include <gst/base/gstcollectpads.h>
72 #include <gst/base/gstadapter.h>
73 #include <gst/base/gstaggregator.h>
74 
75 G_BEGIN_DECLS
76 
77 #include "tsmux/tsmux.h"
78 
79 #define GST_TYPE_BASE_TS_MUX_PAD (gst_base_ts_mux_pad_get_type())
80 #define GST_BASE_TS_MUX_PAD(obj) \
81         (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BASE_TS_MUX_PAD, GstBaseTsMuxPad))
82 #define GST_BASE_TS_MUX_PAD_CAST(obj) ((GstBaseTsMuxPad *)(obj))
83 #define GST_BASE_TS_MUX_PAD_CLASS(klass) \
84         (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BASE_TS_MUX_PAD, GstBaseTsMuxPadClass))
85 #define GST_IS_BASE_TS_MUX_PAD(obj) \
86         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BASE_TS_MUX_PAD))
87 #define GST_IS_BASE_TS_MUX_PAD_CLASS(klass) \
88         (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BASE_TS_MUX_PAD))
89 #define GST_BASE_TS_MUX_PAD_GET_CLASS(obj) \
90         (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_BASE_TS_MUX_PAD,GstBaseTsMuxPadClass))
91 
92 typedef struct _GstBaseTsMuxPad GstBaseTsMuxPad;
93 typedef struct _GstBaseTsMuxPadClass GstBaseTsMuxPadClass;
94 typedef struct _GstBaseTsMuxPadPrivate GstBaseTsMuxPadPrivate;
95 typedef struct GstBaseTsMux GstBaseTsMux;
96 typedef struct GstBaseTsMuxClass GstBaseTsMuxClass;
97 typedef struct GstBaseTsPadData GstBaseTsPadData;
98 
99 typedef GstBuffer * (*GstBaseTsMuxPadPrepareFunction) (GstBuffer * buf,
100     GstBaseTsMuxPad * data, GstBaseTsMux * mux);
101 
102 typedef void (*GstBaseTsMuxPadFreePrepareDataFunction) (gpointer prepare_data);
103 
104 struct _GstBaseTsMuxPad
105 {
106   GstAggregatorPad              parent;
107 
108   gint pid;
109   TsMuxStream *stream;
110 
111   /* most recent DTS */
112   gint64 dts;
113 
114   /* optional codec data available in the caps */
115   GstBuffer *codec_data;
116 
117   /* Opaque data pointer to a structure used by the prepare function */
118   gpointer prepare_data;
119 
120   /* handler to prepare input data */
121   GstBaseTsMuxPadPrepareFunction prepare_func;
122   /* handler to free the private data */
123   GstBaseTsMuxPadFreePrepareDataFunction free_func;
124 
125   /* program id to which it is attached to (not program pid) */
126   gint prog_id;
127   /* program this stream belongs to */
128   TsMuxProgram *prog;
129 
130   gchar *language;
131 };
132 
133 struct _GstBaseTsMuxPadClass
134 {
135   GstAggregatorPadClass parent_class;
136 };
137 
138 GType gst_base_ts_mux_pad_get_type   (void);
139 
140 #define GST_TYPE_BASE_TS_MUX  (gst_base_ts_mux_get_type())
141 #define GST_BASE_TS_MUX(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_BASE_TS_MUX, GstBaseTsMux))
142 #define GST_BASE_TS_MUX_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_BASE_TS_MUX, GstBaseTsMuxClass))
143 #define GST_BASE_TS_MUX_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_BASE_TS_MUX,GstBaseTsMuxClass))
144 
145 #define GST_BASE_TS_MUX_NORMAL_PACKET_LENGTH 188
146 
147 struct GstBaseTsMux {
148   GstAggregator parent;
149 
150   TsMux *tsmux;
151   GHashTable *programs;
152 
153   /* properties */
154   GstStructure *prog_map;
155   guint pat_interval;
156   guint pmt_interval;
157   gint alignment;
158   guint si_interval;
159   guint64 bitrate;
160   guint pcr_interval;
161   guint scte35_pid;
162   guint scte35_null_interval;
163   guint32 last_scte35_event_seqnum;
164 
165   /* state */
166   gboolean first;
167   GstClockTime pending_key_unit_ts;
168   GstEvent *force_key_unit_event;
169   GstMpegtsSection *pending_scte35_section;
170 
171   /* write callback handling/state */
172   GstFlowReturn last_flow_ret;
173   GQueue streamheader;
174   gboolean streamheader_sent;
175   gboolean is_delta;
176   gboolean is_header;
177   GstClockTime last_ts;
178 
179   gsize packet_size;
180   gsize automatic_alignment;
181 
182   /* output buffer aggregation */
183   GstAdapter *out_adapter;
184   GstBuffer *out_buffer;
185   GstClockTimeDiff output_ts_offset;
186 
187   /* protects the tsmux object, the programs hash table, and pad streams */
188   GMutex lock;
189 };
190 
191 /**
192  * GstBaseTsMuxClass:
193  * @create_ts_mux: Optional.
194  *                 Called in order to create the #TsMux object.
195  * @handle_media_type: Optional.
196  *                 Called in order to determine the stream-type for a given
197  *                 @media_type (eg. video/x-h264).
198  * @allocate_packet: Optional.
199  *                 Called when the underlying #TsMux object needs a packet
200  *                 to write into.
201  * @output_packet: Optional.
202  *                 Called when the underlying #TsMux object has a packet
203  *                 ready to output.
204  * @reset:         Optional.
205  *                 Called when the subclass needs to reset.
206  * @drain:         Optional.
207  *                 Called at EOS, if the subclass has data it needs to drain.
208  */
209 struct GstBaseTsMuxClass {
210   GstAggregatorClass parent_class;
211 
212   TsMux *   (*create_ts_mux) (GstBaseTsMux *mux);
213   guint     (*handle_media_type) (GstBaseTsMux *mux, const gchar *media_type, GstBaseTsMuxPad * pad);
214   void      (*allocate_packet) (GstBaseTsMux *mux, GstBuffer **buffer);
215   gboolean  (*output_packet) (GstBaseTsMux *mux, GstBuffer *buffer, gint64 new_pcr);
216   void      (*reset) (GstBaseTsMux *mux);
217   gboolean  (*drain) (GstBaseTsMux *mux);
218 };
219 
220 void gst_base_ts_mux_set_packet_size (GstBaseTsMux *mux, gsize size);
221 void gst_base_ts_mux_set_automatic_alignment (GstBaseTsMux *mux, gsize alignment);
222 
223 typedef GstBuffer * (*GstBaseTsPadDataPrepareFunction) (GstBuffer * buf,
224     GstBaseTsPadData * data, GstBaseTsMux * mux);
225 
226 typedef void (*GstBaseTsPadDataFreePrepareDataFunction) (gpointer prepare_data);
227 
228 struct GstBaseTsPadData {
229   /* parent */
230   GstCollectData collect;
231 
232   gint pid;
233   TsMuxStream *stream;
234 
235   /* most recent DTS */
236   gint64 dts;
237 
238   /* optional codec data available in the caps */
239   GstBuffer *codec_data;
240 
241   /* Opaque data pointer to a structure used by the prepare function */
242   gpointer prepare_data;
243 
244   /* handler to prepare input data */
245   GstBaseTsPadDataPrepareFunction prepare_func;
246   /* handler to free the private data */
247   GstBaseTsPadDataFreePrepareDataFunction free_func;
248 
249   /* program id to which it is attached to (not program pid) */
250   gint prog_id;
251   /* program this stream belongs to */
252   TsMuxProgram *prog;
253 
254   gchar *language;
255 };
256 
257 GType gst_base_ts_mux_get_type (void);
258 
259 G_END_DECLS
260 
261 #endif
262