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 4 different licenses and you
8  * can choose to use it under the terms of any one of them. The
9  * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
10  * license.
11  *
12  * MPL:
13  *
14  * The contents of this file are subject to the Mozilla Public License
15  * Version 1.1 (the "License"); you may not use this file except in
16  * compliance with the License. You may obtain a copy of the License at
17  * http://www.mozilla.org/MPL/.
18  *
19  * Software distributed under the License is distributed on an "AS IS"
20  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
21  * License for the specific language governing rights and limitations
22  * under the License.
23  *
24  * LGPL:
25  *
26  * This library is free software; you can redistribute it and/or
27  * modify it under the terms of the GNU Library General Public
28  * License as published by the Free Software Foundation; either
29  * version 2 of the License, or (at your option) any later version.
30  *
31  * This library is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
34  * Library General Public License for more details.
35  *
36  * You should have received a copy of the GNU Library General Public
37  * License along with this library; if not, write to the
38  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
39  * Boston, MA 02110-1301, USA.
40  *
41  * GPL:
42  *
43  * This program is free software; you can redistribute it and/or modify
44  * it under the terms of the GNU General Public License as published by
45  * the Free Software Foundation; either version 2 of the License, or
46  * (at your option) any later version.
47  *
48  * This program is distributed in the hope that it will be useful,
49  * but WITHOUT ANY WARRANTY; without even the implied warranty of
50  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51  * GNU General Public License for more details.
52  *
53  * You should have received a copy of the GNU General Public License
54  * along with this program; if not, write to the Free Software
55  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
56  *
57  * MIT:
58  *
59  * Unless otherwise indicated, Source Code is licensed under MIT license.
60  * See further explanation attached in License Statement (distributed in the file
61  * LICENSE).
62  *
63  * Permission is hereby granted, free of charge, to any person obtaining a copy of
64  * this software and associated documentation files (the "Software"), to deal in
65  * the Software without restriction, including without limitation the rights to
66  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
67  * of the Software, and to permit persons to whom the Software is furnished to do
68  * so, subject to the following conditions:
69  *
70  * The above copyright notice and this permission notice shall be included in all
71  * copies or substantial portions of the Software.
72  *
73  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
74  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
75  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
76  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
77  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
78  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
79  * SOFTWARE.
80  *
81  */
82 
83 #ifndef __MPEGTSMUX_H__
84 #define __MPEGTSMUX_H__
85 
86 #include <gst/gst.h>
87 #include <gst/base/gstcollectpads.h>
88 #include <gst/base/gstadapter.h>
89 
90 G_BEGIN_DECLS
91 
92 #include <tsmux/tsmux.h>
93 
94 #define GST_TYPE_MPEG_TSMUX  (mpegtsmux_get_type())
95 #define GST_MPEG_TSMUX(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_MPEG_TSMUX, MpegTsMux))
96 
97 #define CLOCK_BASE 9LL
98 #define CLOCK_FREQ (CLOCK_BASE * 10000)   /* 90 kHz PTS clock */
99 #define CLOCK_FREQ_SCR (CLOCK_FREQ * 300) /* 27 MHz SCR clock */
100 
101 #define GSTTIME_TO_MPEGTIME(time) \
102     (((time) > 0 ? (gint64) 1 : (gint64) -1) * \
103     (gint64) gst_util_uint64_scale (ABS(time), CLOCK_BASE, GST_MSECOND/10))
104 
105 /* 27 MHz SCR conversions: */
106 #define MPEG_SYS_TIME_TO_GSTTIME(time) (gst_util_uint64_scale ((time), \
107                         GST_USECOND, CLOCK_FREQ_SCR / 1000000))
108 #define GSTTIME_TO_MPEG_SYS_TIME(time) (gst_util_uint64_scale ((time), \
109                         CLOCK_FREQ_SCR / 1000000, GST_USECOND))
110 
111 #define NORMAL_TS_PACKET_LENGTH 188
112 #define M2TS_PACKET_LENGTH      192
113 
114 #define DEFAULT_PROG_ID	0
115 
116 typedef struct MpegTsMux MpegTsMux;
117 typedef struct MpegTsMuxClass MpegTsMuxClass;
118 typedef struct MpegTsPadData MpegTsPadData;
119 
120 typedef GstBuffer * (*MpegTsPadDataPrepareFunction) (GstBuffer * buf,
121     MpegTsPadData * data, MpegTsMux * mux);
122 
123 typedef void (*MpegTsPadDataFreePrepareDataFunction) (gpointer prepare_data);
124 
125 struct MpegTsMux {
126   GstElement parent;
127 
128   GstPad *srcpad;
129 
130   GstCollectPads *collect;
131 
132   TsMux *tsmux;
133   GHashTable *programs;
134 
135   /* properties */
136   gboolean m2ts_mode;
137   GstStructure *prog_map;
138   guint pat_interval;
139   guint pmt_interval;
140   gint alignment;
141   guint si_interval;
142 
143   /* state */
144   gboolean first;
145   GstClockTime pending_key_unit_ts;
146   GstEvent *force_key_unit_event;
147 
148   /* write callback handling/state */
149   GstFlowReturn last_flow_ret;
150   GQueue streamheader;
151   gboolean streamheader_sent;
152   gboolean is_delta;
153   gboolean is_header;
154   GstClockTime last_ts;
155 
156   /* m2ts specific */
157   gint64 previous_pcr;
158   gint64 previous_offset;
159   gint64 pcr_rate_num;
160   gint64 pcr_rate_den;
161   GstAdapter *adapter;
162 
163   /* output buffer aggregation */
164   GstAdapter *out_adapter;
165   GstBuffer *out_buffer;
166 
167 #if 0
168   /* SPN/PTS index handling */
169   GstIndex *element_index;
170   gint spn_count;
171 #endif
172 };
173 
174 struct MpegTsMuxClass {
175   GstElementClass parent_class;
176 };
177 
178 struct MpegTsPadData {
179   /* parent */
180   GstCollectData collect;
181 
182   gint pid;
183   TsMuxStream *stream;
184 
185   /* most recent DTS */
186   gint64 dts;
187 
188 #if 0
189   /* (optional) index writing */
190   gint element_index_writer_id;
191 #endif
192 
193   /* optional codec data available in the caps */
194   GstBuffer *codec_data;
195 
196   /* Opaque data pointer to a structure used by the prepare function */
197   gpointer prepare_data;
198 
199   /* handler to prepare input data */
200   MpegTsPadDataPrepareFunction prepare_func;
201   /* handler to free the private data */
202   MpegTsPadDataFreePrepareDataFunction free_func;
203 
204   /* program id to which it is attached to (not program pid) */
205   gint prog_id;
206   /* program this stream belongs to */
207   TsMuxProgram *prog;
208 
209   gchar *language;
210 };
211 
212 GType mpegtsmux_get_type (void);
213 
214 
215 G_END_DECLS
216 
217 #endif
218