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 /* expected sample duration */ 104 guint expected_sample_duration_n; 105 guint expected_sample_duration_d; 106 107 /* for avg bitrate calculation */ 108 guint64 total_bytes; 109 guint64 total_duration; 110 111 GstBuffer *last_buf; 112 /* dts of last_buf */ 113 GstClockTime last_dts; 114 guint64 sample_offset; 115 116 /* This is compensate for CTTS */ 117 GstClockTime dts_adjustment; 118 119 /* store the first timestamp for comparing with other streams and 120 * know if there are late streams */ 121 /* subjected to dts adjustment */ 122 GstClockTime first_ts; 123 GstClockTime first_dts; 124 125 /* all the atom and chunk book-keeping is delegated here 126 * unowned/uncounted reference, parent MOOV owns */ 127 AtomTRAK *trak; 128 AtomTRAK *tc_trak; 129 SampleTableEntry *trak_ste; 130 /* fragmented support */ 131 /* meta data book-keeping delegated here */ 132 AtomTRAF *traf; 133 /* fragment buffers */ 134 ATOM_ARRAY (GstBuffer *) fragment_buffers; 135 /* running fragment duration */ 136 gint64 fragment_duration; 137 /* optional fragment index book-keeping */ 138 AtomTFRA *tfra; 139 140 /* Set when tags are received, cleared when written to moov */ 141 gboolean tags_changed; 142 143 GstTagList *tags; 144 145 /* if nothing is set, it won't be called */ 146 GstQTPadPrepareBufferFunc prepare_buf_func; 147 GstQTPadSetCapsFunc set_caps; 148 GstQTPadCreateEmptyBufferFunc create_empty_buffer; 149 150 /* SMPTE timecode */ 151 GstVideoTimeCode *first_tc; 152 GstClockTime first_pts; 153 guint64 tc_pos; 154 155 /* for keeping track in pre-fill mode */ 156 GArray *samples; 157 /* current sample */ 158 GstAdapter *raw_audio_adapter; 159 guint64 raw_audio_adapter_offset; 160 GstClockTime raw_audio_adapter_pts; 161 }; 162 163 typedef enum _GstQTMuxState 164 { 165 GST_QT_MUX_STATE_NONE, 166 GST_QT_MUX_STATE_STARTED, 167 GST_QT_MUX_STATE_DATA, 168 GST_QT_MUX_STATE_EOS 169 } GstQTMuxState; 170 171 typedef enum _GstQtMuxMode { 172 GST_QT_MUX_MODE_MOOV_AT_END, 173 GST_QT_MUX_MODE_FRAGMENTED, 174 GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE, 175 GST_QT_MUX_MODE_FAST_START, 176 GST_QT_MUX_MODE_ROBUST_RECORDING, 177 GST_QT_MUX_MODE_ROBUST_RECORDING_PREFILL, 178 } GstQtMuxMode; 179 180 struct _GstQTMux 181 { 182 GstElement element; 183 184 GstPad *srcpad; 185 GstCollectPads *collect; 186 GSList *sinkpads; 187 188 /* state */ 189 GstQTMuxState state; 190 191 /* Mux mode, inferred from property 192 * set in gst_qt_mux_start_file() */ 193 GstQtMuxMode mux_mode; 194 195 /* size of header (prefix, atoms (ftyp, possibly moov, mdat header)) */ 196 guint64 header_size; 197 /* accumulated size of raw media data (not including mdat header) */ 198 guint64 mdat_size; 199 /* position of the moov (for fragmented mode) or reserved moov atom 200 * area (for robust-muxing mode) */ 201 guint64 moov_pos; 202 /* position of mdat atom header (for later updating of size) in 203 * moov-at-end, fragmented and robust-muxing modes */ 204 guint64 mdat_pos; 205 206 /* keep track of the largest chunk to fine-tune brands */ 207 GstClockTime longest_chunk; 208 209 /* Earliest timestamp across all pads/traks 210 * (unadjusted incoming PTS) */ 211 GstClockTime first_ts; 212 /* Last DTS across all pads (= duration) */ 213 GstClockTime last_dts; 214 215 /* Last pad we used for writing the current chunk */ 216 GstQTPad *current_pad; 217 guint64 current_chunk_size; 218 GstClockTime current_chunk_duration; 219 guint64 current_chunk_offset; 220 221 /* atom helper objects */ 222 AtomsContext *context; 223 AtomFTYP *ftyp; 224 AtomMOOV *moov; 225 GSList *extra_atoms; /* list of extra top-level atoms (e.g. UUID for xmp) 226 * Stored as AtomInfo structs */ 227 228 /* Set when tags are received, cleared when written to moov */ 229 gboolean tags_changed; 230 231 /* fragmented file index */ 232 AtomMFRA *mfra; 233 234 /* fast start */ 235 FILE *fast_start_file; 236 237 /* moov recovery */ 238 FILE *moov_recov_file; 239 240 /* fragment sequence */ 241 guint32 fragment_sequence; 242 243 /* properties */ 244 guint32 timescale; 245 guint32 trak_timescale; 246 AtomsTreeFlavor flavor; 247 gboolean fast_start; 248 gboolean guess_pts; 249 #ifndef GST_REMOVE_DEPRECATED 250 gint dts_method; 251 #endif 252 gchar *fast_start_file_path; 253 gchar *moov_recov_file_path; 254 guint32 fragment_duration; 255 /* Whether or not to work in 'streamable' mode and not 256 * seek to rewrite headers - only valid for fragmented 257 * mode. */ 258 gboolean streamable; 259 260 /* Requested target maximum duration */ 261 GstClockTime reserved_max_duration; 262 /* Estimate of remaining reserved header space (in ns of recording) */ 263 GstClockTime reserved_duration_remaining; 264 /* Multiplier for conversion from reserved_max_duration to bytes */ 265 guint reserved_bytes_per_sec_per_trak; 266 267 guint64 interleave_bytes; 268 GstClockTime interleave_time; 269 gboolean interleave_bytes_set, interleave_time_set; 270 271 GstClockTime max_raw_audio_drift; 272 273 /* Reserved minimum MOOV size in bytes 274 * This is converted from reserved_max_duration 275 * using the bytes/trak/sec estimate */ 276 guint32 reserved_moov_size; 277 /* Basic size of the moov (static headers + tags) */ 278 guint32 base_moov_size; 279 /* Size of the most recently generated moov header */ 280 guint32 last_moov_size; 281 /* True if the first moov in the ping-pong buffers 282 * is the active one. See gst_qt_mux_robust_recording_rewrite_moov() */ 283 gboolean reserved_moov_first_active; 284 285 /* Tracking of periodic MOOV updates */ 286 GstClockTime last_moov_update; 287 GstClockTime reserved_moov_update_period; 288 GstClockTime muxed_since_last_update; 289 290 gboolean reserved_prefill; 291 292 GstClockTime start_gap_threshold; 293 294 /* ohos.ext.func.0016 295 * add additional features to set geographic location information in mp4 file 296 * enable_geolocation is the flag to enable this feature 297 * latitudex10000 is the latitude to set, multiply 10000 for the convenience of calculation. 298 * longitudex10000 is the longitude to set, multiply 10000 for the convenience of calculation. 299 */ 300 #ifdef OHOS_EXT_FUNC 301 gboolean enable_geolocation; 302 gint latitudex10000; 303 gint longitudex10000; 304 #endif 305 306 /* ohos.ext.func.0018 307 * add additional features to set orientationHint in mp4 file. 308 */ 309 #ifdef OHOS_EXT_FUNC 310 guint32 rotation; 311 #endif 312 313 /* ohos.opt.compat.0011 314 * qtmux itself does not handle flush events, so in extreme cases, the buffer is discarded by gstpad 315 * when it is passed forward, but qtmux thinks that the buffer writes the file successfully, 316 * resulting in a file exception. 317 * is_flushing: a flag to tell qtmux, in flushing progress. 318 * flush_lock: is a lock to make sure the flag Operating normally. 319 */ 320 #ifdef OHOS_OPT_COMPAT 321 GMutex flush_lock; 322 gboolean is_flushing; 323 #endif 324 325 /* for request pad naming */ 326 guint video_pads, audio_pads, subtitle_pads, caption_pads; 327 }; 328 329 struct _GstQTMuxClass 330 { 331 GstElementClass parent_class; 332 333 GstQTMuxFormat format; 334 }; 335 336 /* type register helper struct */ 337 typedef struct _GstQTMuxClassParams 338 { 339 GstQTMuxFormatProp *prop; 340 GstCaps *src_caps; 341 GstCaps *video_sink_caps; 342 GstCaps *audio_sink_caps; 343 GstCaps *subtitle_sink_caps; 344 GstCaps *caption_sink_caps; 345 } GstQTMuxClassParams; 346 347 #define GST_QT_MUX_PARAMS_QDATA g_quark_from_static_string("qt-mux-params") 348 349 GType gst_qt_mux_get_type (void); 350 gboolean gst_qt_mux_register (GstPlugin * plugin); 351 352 /* FIXME: ideally classification tag should be added and 353 * registered in gstreamer core gsttaglist 354 * 355 * this tag is a string in the format: entityfourcc://table_num/content 356 * FIXME Shouldn't we add a field for 'language'? 357 */ 358 #define GST_TAG_3GP_CLASSIFICATION "classification" 359 360 G_END_DECLS 361 362 #endif /* __GST_QT_MUX_H__ */ 363