souphttpsrc: get seekable info from dlna op code
[platform/upstream/gst-plugins-good.git] / gst / isomp4 / gstqtmux.h
1 /* Quicktime muxer plugin for GStreamer
2  * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /*
20  * Unless otherwise indicated, Source Code is licensed under MIT license.
21  * See further explanation attached in License Statement (distributed in the file
22  * LICENSE).
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining a copy of
25  * this software and associated documentation files (the "Software"), to deal in
26  * the Software without restriction, including without limitation the rights to
27  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28  * of the Software, and to permit persons to whom the Software is furnished to do
29  * so, subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be included in all
32  * copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40  * SOFTWARE.
41  */
42
43 #ifndef __GST_QT_MUX_H__
44 #define __GST_QT_MUX_H__
45
46 #include <gst/gst.h>
47 #include <gst/base/gstcollectpads.h>
48
49 #include "fourcc.h"
50 #include "atoms.h"
51 #include "atomsrecovery.h"
52 #include "gstqtmuxmap.h"
53
54 G_BEGIN_DECLS
55
56 #define GST_TYPE_QT_MUX (gst_qt_mux_get_type())
57 #define GST_QT_MUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_QT_MUX, GstQTMux))
58 #define GST_QT_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_QT_MUX, GstQTMux))
59 #define GST_IS_QT_MUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_QT_MUX))
60 #define GST_IS_QT_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_QT_MUX))
61 #define GST_QT_MUX_CAST(obj) ((GstQTMux*)(obj))
62
63
64 typedef struct _GstQTMux GstQTMux;
65 typedef struct _GstQTMuxClass GstQTMuxClass;
66 typedef struct _GstQTPad GstQTPad;
67
68 /*
69  * GstQTPadPrepareBufferFunc
70  *
71  * Receives a buffer (takes ref) and returns a new buffer that should
72  * replace the passed one.
73  *
74  * Useful for when the pad/datatype needs some manipulation before
75  * being muxed. (Originally added for image/x-jpc support, for which buffers
76  * need to be wrapped into a isom box)
77  */
78 typedef GstBuffer * (*GstQTPadPrepareBufferFunc) (GstQTPad * pad,
79     GstBuffer * buf, GstQTMux * qtmux);
80
81 typedef gboolean (*GstQTPadSetCapsFunc) (GstQTPad * pad, GstCaps * caps);
82 typedef GstBuffer * (*GstQTPadCreateEmptyBufferFunc) (GstQTPad * pad, gint64 duration);
83
84 #define QTMUX_NO_OF_TS   10
85
86 struct _GstQTPad
87 {
88   GstCollectData collect;       /* we extend the CollectData */
89
90   /* fourcc id of stream */
91   guint32 fourcc;
92   /* whether using format that have out of order buffers */
93   gboolean is_out_of_order;
94   /* if not 0, track with constant sized samples, e.g. raw audio */
95   guint sample_size;
96   /* make sync table entry */
97   gboolean sync;
98   /* if it is a sparse stream
99    * (meaning we can't use PTS differences to compute duration) */
100   gboolean sparse;
101   /* bitrates */
102   guint32 avg_bitrate, max_bitrate;
103
104   /* for avg bitrate calculation */
105   guint64 total_bytes;
106   guint64 total_duration;
107
108   GstBuffer *last_buf;
109   /* dts of last_buf */
110   GstClockTime last_dts;
111
112   /* This is compensate for CTTS */
113   GstClockTime dts_adjustment;
114
115   /* store the first timestamp for comparing with other streams and
116    * know if there are late streams */
117   GstClockTime first_ts;
118   GstClockTime first_dts;
119
120   /* all the atom and chunk book-keeping is delegated here
121    * unowned/uncounted reference, parent MOOV owns */
122   AtomTRAK *trak;
123   AtomTRAK *tc_trak;
124   SampleTableEntry *trak_ste;
125   /* fragmented support */
126   /* meta data book-keeping delegated here */
127   AtomTRAF *traf;
128   /* fragment buffers */
129   ATOM_ARRAY (GstBuffer *) fragment_buffers;
130   /* running fragment duration */
131   gint64 fragment_duration;
132   /* optional fragment index book-keeping */
133   AtomTFRA *tfra;
134
135   /* Set when tags are received, cleared when written to moov */
136   gboolean tags_changed;
137
138   GstTagList *tags;
139
140   /* if nothing is set, it won't be called */
141   GstQTPadPrepareBufferFunc prepare_buf_func;
142   GstQTPadSetCapsFunc set_caps;
143   GstQTPadCreateEmptyBufferFunc create_empty_buffer;
144
145   /* SMPTE timecode */
146   GstVideoTimeCode *first_tc;
147   GstClockTime first_pts;
148   guint64 tc_pos;
149 };
150
151 typedef enum _GstQTMuxState
152 {
153   GST_QT_MUX_STATE_NONE,
154   GST_QT_MUX_STATE_STARTED,
155   GST_QT_MUX_STATE_DATA,
156   GST_QT_MUX_STATE_EOS
157 } GstQTMuxState;
158
159 typedef enum _GstQtMuxMode {
160     GST_QT_MUX_MODE_MOOV_AT_END,
161     GST_QT_MUX_MODE_FRAGMENTED,
162     GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE,
163     GST_QT_MUX_MODE_FAST_START,
164     GST_QT_MUX_MODE_ROBUST_RECORDING
165 } GstQtMuxMode;
166
167 struct _GstQTMux
168 {
169   GstElement element;
170
171   GstPad *srcpad;
172   GstCollectPads *collect;
173   GSList *sinkpads;
174
175   /* state */
176   GstQTMuxState state;
177
178   /* Mux mode, inferred from property
179    * set in gst_qt_mux_start_file() */
180   GstQtMuxMode mux_mode;
181
182   /* size of header (prefix, atoms (ftyp, possibly moov, mdat header)) */
183   guint64 header_size;
184   /* accumulated size of raw media data (not including mdat header) */
185   guint64 mdat_size;
186   /* position of the moov (for fragmented mode) or reserved moov atom
187    * area (for robust-muxing mode) */
188   guint64 moov_pos;
189   /* position of mdat atom header (for later updating of size) in
190    * moov-at-end, fragmented and robust-muxing modes */
191   guint64 mdat_pos;
192
193   /* keep track of the largest chunk to fine-tune brands */
194   GstClockTime longest_chunk;
195
196   /* Earliest timestamp across all pads/traks */
197   GstClockTime first_ts;
198   /* Last DTS across all pads (= duration) */
199   GstClockTime last_dts;
200
201   /* Last pad we used for writing the current chunk */
202   GstQTPad *current_pad;
203   guint64 current_chunk_size;
204   GstClockTime current_chunk_duration;
205   guint64 current_chunk_offset;
206
207   /* atom helper objects */
208   AtomsContext *context;
209   AtomFTYP *ftyp;
210   AtomMOOV *moov;
211   GSList *extra_atoms; /* list of extra top-level atoms (e.g. UUID for xmp)
212                         * Stored as AtomInfo structs */
213
214   /* Set when tags are received, cleared when written to moov */
215   gboolean tags_changed;
216
217   /* fragmented file index */
218   AtomMFRA *mfra;
219
220   /* fast start */
221   FILE *fast_start_file;
222
223   /* moov recovery */
224   FILE *moov_recov_file;
225
226   /* fragment sequence */
227   guint32 fragment_sequence;
228
229   /* properties */
230   guint32 timescale;
231   guint32 trak_timescale;
232   AtomsTreeFlavor flavor;
233   gboolean fast_start;
234   gboolean guess_pts;
235 #ifndef GST_REMOVE_DEPRECATED
236   gint dts_method;
237 #endif
238   gchar *fast_start_file_path;
239   gchar *moov_recov_file_path;
240   guint32 fragment_duration;
241   /* Whether or not to work in 'streamable' mode and not
242    * seek to rewrite headers - only valid for fragmented
243    * mode. */
244   gboolean streamable;
245
246   /* Requested target maximum duration */
247   GstClockTime reserved_max_duration;
248   /* Estimate of remaining reserved header space (in ns of recording) */
249   GstClockTime reserved_duration_remaining;
250   /* Multiplier for conversion from reserved_max_duration to bytes */
251   guint reserved_bytes_per_sec_per_trak;
252
253   guint64 interleave_bytes;
254   GstClockTime interleave_time;
255   gboolean interleave_bytes_set, interleave_time_set;
256
257   /* Reserved minimum MOOV size in bytes
258    * This is converted from reserved_max_duration
259    * using the bytes/trak/sec estimate */
260   guint32 reserved_moov_size;
261   /* Basic size of the moov (static headers + tags) */
262   guint32 base_moov_size;
263   /* Size of the most recently generated moov header */
264   guint32 last_moov_size;
265   /* True if the first moov in the ping-pong buffers
266    * is the active one. See gst_qt_mux_robust_recording_rewrite_moov() */
267   gboolean reserved_moov_first_active;
268
269   /* Tracking of periodic MOOV updates */
270   GstClockTime last_moov_update;
271   GstClockTime reserved_moov_update_period;
272   GstClockTime muxed_since_last_update;
273
274   /* for request pad naming */
275   guint video_pads, audio_pads, subtitle_pads;
276
277 #ifdef TIZEN_FEATURE_GST_MUX_ENHANCEMENT
278   guint expected_trailer_size;
279   guint audio_expected_trailer_size;
280   guint video_expected_trailer_size;
281 #endif /* TIZEN_FEATURE_GST_MUX_ENHANCEMENT */
282 };
283
284 struct _GstQTMuxClass
285 {
286   GstElementClass parent_class;
287
288   GstQTMuxFormat format;
289 };
290
291 /* type register helper struct */
292 typedef struct _GstQTMuxClassParams
293 {
294   GstQTMuxFormatProp *prop;
295   GstCaps *src_caps;
296   GstCaps *video_sink_caps;
297   GstCaps *audio_sink_caps;
298   GstCaps *subtitle_sink_caps;
299 } GstQTMuxClassParams;
300
301 #define GST_QT_MUX_PARAMS_QDATA g_quark_from_static_string("qt-mux-params")
302
303 GType gst_qt_mux_get_type (void);
304 gboolean gst_qt_mux_register (GstPlugin * plugin);
305
306 /* FIXME: ideally classification tag should be added and
307  * registered in gstreamer core gsttaglist
308  *
309  * this tag is a string in the format: entityfourcc://table_num/content
310  * FIXME Shouldn't we add a field for 'language'?
311  */
312 #define GST_TAG_3GP_CLASSIFICATION "classification"
313
314 G_END_DECLS
315
316 #endif /* __GST_QT_MUX_H__ */