1 /* ASF muxer plugin for GStreamer
2 * Copyright (C) 2009 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 /* based on:
21 * - avimux (by Ronald Bultje and Mark Nauwelaerts)
22 * - qtmux (by Thiago Santos and Mark Nauwelaerts)
23 */
24
25 /**
26 * SECTION:element-asfmux
27 * @title: asfmux
28 *
29 * Muxes media into an ASF file/stream.
30 *
31 * Pad names are either video_xx or audio_xx, where 'xx' is the
32 * stream number of the stream that goes through that pad. Stream numbers
33 * are assigned sequentially, starting from 1.
34 *
35 * ## Example launch lines
36 *
37 * (write everything in one line, without the backslash characters)
38 * |[
39 * gst-launch-1.0 videotestsrc num-buffers=250 \
40 * ! "video/x-raw,format=(string)I420,framerate=(fraction)25/1" ! avenc_wmv2 \
41 * ! asfmux name=mux ! filesink location=test.asf \
42 * audiotestsrc num-buffers=440 ! audioconvert \
43 * ! "audio/x-raw,rate=44100" ! avenc_wmav2 ! mux.
44 * ]| This creates an ASF file containing an WMV video stream
45 * with a test picture and WMA audio stream of a test sound.
46 *
47 * ## Live streaming
48 * asfmux and rtpasfpay are capable of generating a live asf stream.
49 * asfmux has to set its 'streamable' property to true, because in this
50 * mode it won't try to seek back to the start of the file to replace
51 * some fields that couldn't be known at the file start. In this mode,
52 * it won't also send indexes at the end of the data packets (the actual
53 * media content)
54 * the following pipelines are an example of this usage.
55 * (write everything in one line, without the backslash characters)
56 * Server (sender)
57 * |[
58 * gst-launch-1.0 -ve videotestsrc ! avenc_wmv2 ! asfmux name=mux streamable=true \
59 * ! rtpasfpay ! udpsink host=127.0.0.1 port=3333 \
60 * audiotestsrc ! avenc_wmav2 ! mux.
61 * ]|
62 * Client (receiver)
63 * |[
64 * gst-launch-1.0 udpsrc port=3333 ! "caps_from_rtpasfpay_at_sender" \
65 * ! rtpasfdepay ! decodebin name=d ! queue \
66 * ! videoconvert ! autovideosink \
67 * d. ! queue ! audioconvert ! autoaudiosink
68 * ]|
69 *
70 */
71
72 #ifdef HAVE_CONFIG_H
73 #include "config.h"
74 #endif
75
76 #include <string.h>
77 #include <stdio.h>
78 #include <gst/gst-i18n-plugin.h>
79 #include "gstasfmux.h"
80
81 #define DEFAULT_SIMPLE_INDEX_TIME_INTERVAL G_GUINT64_CONSTANT (10000000)
82 #define MAX_PAYLOADS_IN_A_PACKET 63
83
84 GST_DEBUG_CATEGORY_STATIC (asfmux_debug);
85 #define GST_CAT_DEFAULT asfmux_debug
86
87 enum
88 {
89 PROP_0,
90 PROP_PACKET_SIZE,
91 PROP_PREROLL,
92 PROP_MERGE_STREAM_TAGS,
93 PROP_PADDING,
94 PROP_STREAMABLE
95 };
96
97 /* Stores a tag list for the available/known tags
98 * in an ASF file
99 * Also stores the sizes those entries would use in a
100 * content description object and extended content
101 * description object
102 */
103 typedef struct
104 {
105 GstTagList *tags;
106 guint64 cont_desc_size;
107 guint64 ext_cont_desc_size;
108 } GstAsfTags;
109
110 /* Helper struct to be used as user data
111 * in gst_tag_foreach function for writing
112 * each tag for the metadata objects
113 *
114 * stream_num is used only for stream dependent tags
115 */
116 typedef struct
117 {
118 GstAsfMux *asfmux;
119 guint8 *buf;
120 guint16 count;
121 guint64 size;
122 guint16 stream_num;
123 } GstAsfExtContDescData;
124
125 typedef GstAsfExtContDescData GstAsfMetadataObjData;
126
127 #define DEFAULT_PACKET_SIZE 4800
128 #define DEFAULT_PREROLL 5000
129 #define DEFAULT_MERGE_STREAM_TAGS TRUE
130 #define DEFAULT_PADDING 0
131 #define DEFAULT_STREAMABLE FALSE
132
133 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
134 GST_PAD_SRC,
135 GST_PAD_ALWAYS,
136 GST_STATIC_CAPS ("video/x-ms-asf, " "parsed = (boolean) true")
137 );
138
139 static GstStaticPadTemplate video_sink_factory =
140 GST_STATIC_PAD_TEMPLATE ("video_%u",
141 GST_PAD_SINK,
142 GST_PAD_REQUEST,
143 GST_STATIC_CAPS ("video/x-wmv, wmvversion = (int) [1,3]"));
144
145 static GstStaticPadTemplate audio_sink_factory =
146 GST_STATIC_PAD_TEMPLATE ("audio_%u",
147 GST_PAD_SINK,
148 GST_PAD_REQUEST,
149 GST_STATIC_CAPS ("audio/x-wma, wmaversion = (int) [1,3]; "
150 "audio/mpeg, layer = (int) 3, mpegversion = (int) 1, "
151 "channels = (int) [1,2], rate = (int) [8000,96000]"));
152
153 static gboolean gst_asf_mux_audio_set_caps (GstPad * pad, GstCaps * caps);
154 static gboolean gst_asf_mux_video_set_caps (GstPad * pad, GstCaps * caps);
155
156 static GstPad *gst_asf_mux_request_new_pad (GstElement * element,
157 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
158 static void gst_asf_mux_set_property (GObject * object,
159 guint prop_id, const GValue * value, GParamSpec * pspec);
160 static void gst_asf_mux_get_property (GObject * object,
161 guint prop_id, GValue * value, GParamSpec * pspec);
162 static GstStateChangeReturn gst_asf_mux_change_state (GstElement * element,
163 GstStateChange transition);
164
165 static gboolean gst_asf_mux_sink_event (GstCollectPads * pads,
166 GstCollectData * cdata, GstEvent * event, GstAsfMux * asfmux);
167
168 static void gst_asf_mux_pad_reset (GstAsfPad * data);
169 static GstFlowReturn gst_asf_mux_collected (GstCollectPads * collect,
170 gpointer data);
171
172 static GstElementClass *parent_class = NULL;
173
174 G_DEFINE_TYPE_WITH_CODE (GstAsfMux, gst_asf_mux, GST_TYPE_ELEMENT,
175 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
176
177 static void
gst_asf_mux_reset(GstAsfMux * asfmux)178 gst_asf_mux_reset (GstAsfMux * asfmux)
179 {
180 asfmux->state = GST_ASF_MUX_STATE_NONE;
181 asfmux->stream_number = 0;
182 asfmux->data_object_size = 0;
183 asfmux->data_object_position = 0;
184 asfmux->file_properties_object_position = 0;
185 asfmux->total_data_packets = 0;
186 asfmux->file_size = 0;
187 asfmux->packet_size = 0;
188 asfmux->first_ts = GST_CLOCK_TIME_NONE;
189
190 if (asfmux->payloads) {
191 GSList *walk;
192 for (walk = asfmux->payloads; walk; walk = g_slist_next (walk)) {
193 gst_asf_payload_free ((AsfPayload *) walk->data);
194 walk->data = NULL;
195 }
196 g_slist_free (asfmux->payloads);
197 }
198 asfmux->payloads = NULL;
199 asfmux->payload_data_size = 0;
200
201 asfmux->file_id.v1 = 0;
202 asfmux->file_id.v2 = 0;
203 asfmux->file_id.v3 = 0;
204 asfmux->file_id.v4 = 0;
205
206 gst_tag_setter_reset_tags (GST_TAG_SETTER (asfmux));
207 }
208
209 static void
gst_asf_mux_finalize(GObject * object)210 gst_asf_mux_finalize (GObject * object)
211 {
212 GstAsfMux *asfmux;
213
214 asfmux = GST_ASF_MUX (object);
215
216 gst_asf_mux_reset (asfmux);
217 gst_object_unref (asfmux->collect);
218
219 G_OBJECT_CLASS (parent_class)->finalize (object);
220 }
221
222 static void
gst_asf_mux_class_init(GstAsfMuxClass * klass)223 gst_asf_mux_class_init (GstAsfMuxClass * klass)
224 {
225 GObjectClass *gobject_class;
226 GstElementClass *gstelement_class;
227
228 gobject_class = (GObjectClass *) klass;
229 gstelement_class = (GstElementClass *) klass;
230
231 parent_class = g_type_class_peek_parent (klass);
232
233 gobject_class->get_property = gst_asf_mux_get_property;
234 gobject_class->set_property = gst_asf_mux_set_property;
235 gobject_class->finalize = gst_asf_mux_finalize;
236
237 g_object_class_install_property (gobject_class, PROP_PACKET_SIZE,
238 g_param_spec_uint ("packet-size", "Packet size",
239 "The ASF packets size (bytes)",
240 ASF_MULTIPLE_PAYLOAD_HEADER_SIZE + 1, G_MAXUINT32,
241 DEFAULT_PACKET_SIZE,
242 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class, PROP_PREROLL,
244 g_param_spec_uint64 ("preroll", "Preroll",
245 "The preroll time (milisecs)",
246 0, G_MAXUINT64,
247 DEFAULT_PREROLL,
248 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
249 g_object_class_install_property (gobject_class, PROP_MERGE_STREAM_TAGS,
250 g_param_spec_boolean ("merge-stream-tags", "Merge Stream Tags",
251 "If the stream metadata (received as events in the sink) should be "
252 "merged to the main file metadata.",
253 DEFAULT_MERGE_STREAM_TAGS,
254 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
255 g_object_class_install_property (gobject_class, PROP_PADDING,
256 g_param_spec_uint64 ("padding", "Padding",
257 "Size of the padding object to be added to the end of the header. "
258 "If this less than 24 (the smaller size of an ASF object), "
259 "no padding is added.",
260 0, G_MAXUINT64,
261 DEFAULT_PADDING,
262 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (gobject_class, PROP_STREAMABLE,
264 g_param_spec_boolean ("streamable", "Streamable",
265 "If set to true, the output should be as if it is to be streamed "
266 "and hence no indexes written or duration written.",
267 DEFAULT_STREAMABLE,
268 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
269
270 gstelement_class->request_new_pad =
271 GST_DEBUG_FUNCPTR (gst_asf_mux_request_new_pad);
272 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_asf_mux_change_state);
273
274 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
275 gst_element_class_add_static_pad_template (gstelement_class,
276 &audio_sink_factory);
277 gst_element_class_add_static_pad_template (gstelement_class,
278 &video_sink_factory);
279
280 gst_element_class_set_static_metadata (gstelement_class, "ASF muxer",
281 "Codec/Muxer",
282 "Muxes audio and video into an ASF stream",
283 "Thiago Santos <thiagoss@embedded.ufcg.edu.br>");
284
285 GST_DEBUG_CATEGORY_INIT (asfmux_debug, "asfmux", 0, "Muxer for ASF streams");
286 }
287
288 static void
gst_asf_mux_init(GstAsfMux * asfmux)289 gst_asf_mux_init (GstAsfMux * asfmux)
290 {
291 asfmux->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
292 gst_pad_use_fixed_caps (asfmux->srcpad);
293 gst_element_add_pad (GST_ELEMENT (asfmux), asfmux->srcpad);
294
295 asfmux->collect = gst_collect_pads_new ();
296 gst_collect_pads_set_function (asfmux->collect,
297 (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_asf_mux_collected),
298 asfmux);
299 gst_collect_pads_set_event_function (asfmux->collect,
300 (GstCollectPadsEventFunction) GST_DEBUG_FUNCPTR (gst_asf_mux_sink_event),
301 asfmux);
302
303 asfmux->payloads = NULL;
304 asfmux->prop_packet_size = DEFAULT_PACKET_SIZE;
305 asfmux->prop_preroll = DEFAULT_PREROLL;
306 asfmux->prop_merge_stream_tags = DEFAULT_MERGE_STREAM_TAGS;
307 asfmux->prop_padding = DEFAULT_PADDING;
308 asfmux->prop_streamable = DEFAULT_STREAMABLE;
309 gst_asf_mux_reset (asfmux);
310 }
311
312 static gboolean
gst_asf_mux_sink_event(GstCollectPads * pads,GstCollectData * cdata,GstEvent * event,GstAsfMux * asfmux)313 gst_asf_mux_sink_event (GstCollectPads * pads, GstCollectData * cdata,
314 GstEvent * event, GstAsfMux * asfmux)
315 {
316 GstAsfPad *asfpad = (GstAsfPad *) cdata;
317 gboolean ret = TRUE;
318
319 switch (GST_EVENT_TYPE (event)) {
320 case GST_EVENT_CAPS:
321 {
322 GstCaps *caps;
323
324 gst_event_parse_caps (event, &caps);
325 if (asfpad->is_audio)
326 ret = gst_asf_mux_audio_set_caps (cdata->pad, caps);
327 else
328 ret = gst_asf_mux_video_set_caps (cdata->pad, caps);
329 gst_event_unref (event);
330 event = NULL;
331 break;
332 }
333 case GST_EVENT_TAG:{
334 GST_DEBUG_OBJECT (asfmux, "received tag event");
335 /* we discard tag events that come after we started
336 * writing the headers, because tags are to be in
337 * the headers
338 */
339 if (asfmux->state == GST_ASF_MUX_STATE_NONE) {
340 GstTagList *list = NULL;
341 gst_event_parse_tag (event, &list);
342 if (asfmux->merge_stream_tags) {
343 GstTagSetter *setter = GST_TAG_SETTER (asfmux);
344 const GstTagMergeMode mode =
345 gst_tag_setter_get_tag_merge_mode (setter);
346 gst_tag_setter_merge_tags (setter, list, mode);
347 } else {
348 if (asfpad->taglist == NULL) {
349 asfpad->taglist = gst_tag_list_new_empty ();
350 }
351 gst_tag_list_insert (asfpad->taglist, list, GST_TAG_MERGE_REPLACE);
352 }
353 }
354 break;
355 }
356 default:
357 break;
358 }
359
360 if (event != NULL)
361 return gst_collect_pads_event_default (pads, cdata, event, FALSE);
362
363 return ret;
364 }
365
366 /**
367 * gst_asf_mux_push_buffer:
368 * @asfmux: #GstAsfMux that should push the buffer
369 * @buf: #GstBuffer to be pushed
370 *
371 * Pushes a buffer downstream and adds its size to the total file size
372 *
373 * Returns: the result of #gst_pad_push on the buffer
374 */
375 static GstFlowReturn
gst_asf_mux_push_buffer(GstAsfMux * asfmux,GstBuffer * buf,gsize bufsize)376 gst_asf_mux_push_buffer (GstAsfMux * asfmux, GstBuffer * buf, gsize bufsize)
377 {
378 GstFlowReturn ret;
379
380 ret = gst_pad_push (asfmux->srcpad, buf);
381
382 if (ret == GST_FLOW_OK)
383 asfmux->file_size += bufsize;
384
385 return ret;
386 }
387
388 /**
389 * content_description_calc_size_for_tag:
390 * @taglist: the #GstTagList that contains the tag
391 * @tag: the tag's name
392 * @user_data: a #GstAsfTags struct for putting the results
393 *
394 * Function that has the #GstTagForEach signature and
395 * is used to calculate the size in bytes for each tag
396 * that can be contained in asf's content description object
397 * and extended content description object. This size is added
398 * to the total size for each of that objects in the #GstAsfTags
399 * struct passed in the user_data pointer.
400 */
401 static void
content_description_calc_size_for_tag(const GstTagList * taglist,const gchar * tag,gpointer user_data)402 content_description_calc_size_for_tag (const GstTagList * taglist,
403 const gchar * tag, gpointer user_data)
404 {
405 const gchar *asftag = gst_asf_get_asf_tag (tag);
406 GValue value = { 0 };
407 guint type;
408 GstAsfTags *asftags = (GstAsfTags *) user_data;
409 guint content_size;
410
411 if (asftag == NULL)
412 return;
413
414 if (!gst_tag_list_copy_value (&value, taglist, tag)) {
415 return;
416 }
417 type = gst_asf_get_tag_field_type (&value);
418 switch (type) {
419 case ASF_TAG_TYPE_UNICODE_STR:
420 {
421 const gchar *text;
422
423 text = g_value_get_string (&value);
424 /* +1 -> because of the \0 at the end
425 * 2* -> because we have uft8, and asf demands utf16
426 */
427 content_size = 2 * (1 + g_utf8_strlen (text, -1));
428
429 if (gst_asf_tag_present_in_content_description (tag)) {
430 asftags->cont_desc_size += content_size;
431 }
432 }
433 break;
434 case ASF_TAG_TYPE_DWORD:
435 content_size = 4;
436 break;
437 default:
438 GST_WARNING ("Unhandled asf tag field type %u for tag %s", type, tag);
439 g_value_reset (&value);
440 return;
441 }
442 if (asftag) {
443 /* size of the tag content in utf16 +
444 * size of the tag name +
445 * 3 uint16 (size of the tag name string,
446 * size of the tag content string and
447 * type of content
448 */
449 asftags->ext_cont_desc_size += content_size +
450 (g_utf8_strlen (asftag, -1) + 1) * 2 + 6;
451 }
452 gst_tag_list_add_value (asftags->tags, GST_TAG_MERGE_REPLACE, tag, &value);
453 g_value_reset (&value);
454 }
455
456 /* FIXME
457 * it is awful to keep track of the size here
458 * and get the same tags in the writing function */
459 /**
460 * gst_asf_mux_get_content_description_tags:
461 * @asfmux: #GstAsfMux to have its tags proccessed
462 * @asftags: #GstAsfTags to hold the results
463 *
464 * Inspects the tags received by the GstTagSetter interface
465 * or possibly by sink tag events and calculates the total
466 * size needed for the default and extended content description objects.
467 * This results and a copy of the #GstTagList
468 * are stored in the #GstAsfTags. We store a copy so that
469 * the sizes estimated here mantain the same until they are
470 * written to the asf file.
471 */
472 static void
gst_asf_mux_get_content_description_tags(GstAsfMux * asfmux,GstAsfTags * asftags)473 gst_asf_mux_get_content_description_tags (GstAsfMux * asfmux,
474 GstAsfTags * asftags)
475 {
476 const GstTagList *tags;
477
478 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (asfmux));
479 if (tags && !gst_tag_list_is_empty (tags)) {
480 if (asftags->tags != NULL) {
481 gst_tag_list_unref (asftags->tags);
482 }
483 asftags->tags = gst_tag_list_new_empty ();
484 asftags->cont_desc_size = 0;
485 asftags->ext_cont_desc_size = 0;
486
487 GST_DEBUG_OBJECT (asfmux, "Processing tags");
488 gst_tag_list_foreach (tags, content_description_calc_size_for_tag, asftags);
489 } else {
490 GST_DEBUG_OBJECT (asfmux, "No tags received");
491 }
492
493 if (asftags->cont_desc_size > 0) {
494 asftags->cont_desc_size += ASF_CONTENT_DESCRIPTION_OBJECT_SIZE;
495 }
496 if (asftags->ext_cont_desc_size > 0) {
497 asftags->ext_cont_desc_size += ASF_EXT_CONTENT_DESCRIPTION_OBJECT_SIZE;
498 }
499 }
500
501 /**
502 * add_metadata_tag_size:
503 * @taglist: #GstTagList
504 * @tag: tag name
505 * @user_data: pointer to a guint to store the result
506 *
507 * GstTagForeachFunc implementation that accounts the size of
508 * each tag in the taglist and adds them to the guint pointed
509 * by the user_data
510 */
511 static void
add_metadata_tag_size(const GstTagList * taglist,const gchar * tag,gpointer user_data)512 add_metadata_tag_size (const GstTagList * taglist, const gchar * tag,
513 gpointer user_data)
514 {
515 const gchar *asftag = gst_asf_get_asf_tag (tag);
516 GValue value = { 0 };
517 guint type;
518 guint content_size;
519 guint *total_size = (guint *) user_data;
520
521 if (asftag == NULL)
522 return;
523
524 if (!gst_tag_list_copy_value (&value, taglist, tag)) {
525 return;
526 }
527 type = gst_asf_get_tag_field_type (&value);
528 switch (type) {
529 case ASF_TAG_TYPE_UNICODE_STR:
530 {
531 const gchar *text;
532
533 text = g_value_get_string (&value);
534 /* +1 -> because of the \0 at the end
535 * 2* -> because we have uft8, and asf demands utf16
536 */
537 content_size = 2 * (1 + g_utf8_strlen (text, -1));
538 }
539 break;
540 case ASF_TAG_TYPE_DWORD:
541 content_size = 4;
542 break;
543 default:
544 GST_WARNING ("Unhandled asf tag field type %u for tag %s", type, tag);
545 g_value_reset (&value);
546 return;
547 }
548 /* size of reserved (2) +
549 * size of stream number (2) +
550 * size of the tag content in utf16 +
551 * size of the tag name +
552 * 2 uint16 (size of the tag name string and type of content) +
553 * 1 uint32 (size of the data)
554 */
555 *total_size +=
556 4 + content_size + (g_utf8_strlen (asftag, -1) + 1) * 2 + 4 + 4;
557 g_value_reset (&value);
558 }
559
560 /**
561 * gst_asf_mux_get_metadata_object_size:
562 * @asfmux: #GstAsfMux
563 * @asfpad: pad for which the metadata object size should be calculated
564 *
565 * Calculates the size of the metadata object for the tags of the stream
566 * handled by the asfpad in the parameter
567 *
568 * Returns: The size calculated
569 */
570 static guint
gst_asf_mux_get_metadata_object_size(GstAsfMux * asfmux,GstAsfPad * asfpad)571 gst_asf_mux_get_metadata_object_size (GstAsfMux * asfmux, GstAsfPad * asfpad)
572 {
573 guint size = ASF_METADATA_OBJECT_SIZE;
574 if (asfpad->taglist == NULL || gst_tag_list_is_empty (asfpad->taglist))
575 return 0;
576
577 gst_tag_list_foreach (asfpad->taglist, add_metadata_tag_size, &size);
578 return size;
579 }
580
581 /**
582 * gst_asf_mux_get_headers_size:
583 * @asfmux: #GstAsfMux
584 *
585 * Calculates the size of the headers of the asf stream
586 * to be generated by this #GstAsfMux.
587 * Its used for determining the size of the buffer to allocate
588 * to exactly fit the headers in.
589 * Padding and metadata objects sizes are not included.
590 *
591 * Returns: the calculated size
592 */
593 static guint
gst_asf_mux_get_headers_size(GstAsfMux * asfmux)594 gst_asf_mux_get_headers_size (GstAsfMux * asfmux)
595 {
596 GSList *walk;
597 gint stream_num = 0;
598 guint size = ASF_HEADER_OBJECT_SIZE +
599 ASF_FILE_PROPERTIES_OBJECT_SIZE + ASF_HEADER_EXTENSION_OBJECT_SIZE;
600
601 /* per stream data */
602 for (walk = asfmux->collect->data; walk; walk = g_slist_next (walk)) {
603 GstAsfPad *asfpad = (GstAsfPad *) walk->data;
604
605 if (asfpad->is_audio)
606 size += ASF_AUDIO_SPECIFIC_DATA_SIZE;
607 else
608 size += ASF_VIDEO_SPECIFIC_DATA_SIZE;
609
610 if (asfpad->codec_data)
611 size += gst_buffer_get_size (asfpad->codec_data);
612
613 stream_num++;
614 }
615 size += stream_num * (ASF_STREAM_PROPERTIES_OBJECT_SIZE +
616 ASF_EXTENDED_STREAM_PROPERTIES_OBJECT_SIZE);
617
618 return size;
619 }
620
621 /**
622 * gst_asf_mux_write_header_object:
623 * @asfmux:
624 * @buf: pointer to the data pointer
625 * @size: size of the header object
626 * @child_objects: number of children objects inside the main header object
627 *
628 * Writes the main asf header object start. The buffer pointer
629 * is incremented to the next writing position.
630 */
631 static void
gst_asf_mux_write_header_object(GstAsfMux * asfmux,guint8 ** buf,guint64 size,guint32 child_objects)632 gst_asf_mux_write_header_object (GstAsfMux * asfmux, guint8 ** buf,
633 guint64 size, guint32 child_objects)
634 {
635 gst_asf_put_guid (*buf, guids[ASF_HEADER_OBJECT_INDEX]);
636 GST_WRITE_UINT64_LE (*buf + 16, size); /* object size */
637 GST_WRITE_UINT32_LE (*buf + 24, child_objects); /* # of child objects */
638 GST_WRITE_UINT8 (*buf + 28, 0x01); /* reserved */
639 GST_WRITE_UINT8 (*buf + 29, 0x02); /* reserved */
640 *buf += ASF_HEADER_OBJECT_SIZE;
641 }
642
643 /**
644 * gst_asf_mux_write_file_properties:
645 * @asfmux:
646 * @buf: pointer to the data pointer
647 *
648 * Writes the file properties object to the buffer. The buffer pointer
649 * is incremented to the next writing position.
650 */
651 static void
gst_asf_mux_write_file_properties(GstAsfMux * asfmux,guint8 ** buf)652 gst_asf_mux_write_file_properties (GstAsfMux * asfmux, guint8 ** buf)
653 {
654 gst_asf_put_guid (*buf, guids[ASF_FILE_PROPERTIES_OBJECT_INDEX]);
655 GST_WRITE_UINT64_LE (*buf + 16, ASF_FILE_PROPERTIES_OBJECT_SIZE); /* object size */
656 gst_asf_put_guid (*buf + 24, asfmux->file_id);
657 GST_WRITE_UINT64_LE (*buf + 40, 0); /* file size - needs update */
658 gst_asf_put_time (*buf + 48, gst_asf_get_current_time ()); /* creation time */
659 GST_WRITE_UINT64_LE (*buf + 56, 0); /* data packets - needs update */
660 GST_WRITE_UINT64_LE (*buf + 64, 0); /* play duration - needs update */
661 GST_WRITE_UINT64_LE (*buf + 72, 0); /* send duration - needs update */
662 GST_WRITE_UINT64_LE (*buf + 80, asfmux->preroll); /* preroll */
663 GST_WRITE_UINT32_LE (*buf + 88, 0x1); /* flags - broadcast on */
664 GST_WRITE_UINT32_LE (*buf + 92, asfmux->packet_size); /* minimum data packet size */
665 GST_WRITE_UINT32_LE (*buf + 96, asfmux->packet_size); /* maximum data packet size */
666 GST_WRITE_UINT32_LE (*buf + 100, 0); /* maximum bitrate TODO */
667
668 *buf += ASF_FILE_PROPERTIES_OBJECT_SIZE;
669 }
670
671 /**
672 * gst_asf_mux_write_stream_properties:
673 * @asfmux:
674 * @buf: pointer to the data pointer
675 * @asfpad: Pad that handles the stream
676 *
677 * Writes the stream properties object in the buffer
678 * for the stream handled by the #GstAsfPad passed.
679 * The pointer is incremented to the next writing position
680 */
681 static void
gst_asf_mux_write_stream_properties(GstAsfMux * asfmux,guint8 ** buf,GstAsfPad * asfpad)682 gst_asf_mux_write_stream_properties (GstAsfMux * asfmux, guint8 ** buf,
683 GstAsfPad * asfpad)
684 {
685 guint32 codec_data_length = 0;
686 guint32 media_specific_data_length = 0;
687 guint16 flags = 0;
688
689 /* codec specific data length */
690 if (asfpad->codec_data)
691 codec_data_length = gst_buffer_get_size (asfpad->codec_data);
692 if (asfpad->is_audio)
693 media_specific_data_length = ASF_AUDIO_SPECIFIC_DATA_SIZE;
694 else
695 media_specific_data_length = ASF_VIDEO_SPECIFIC_DATA_SIZE;
696
697 GST_DEBUG_OBJECT (asfmux, "Stream %" G_GUINT16_FORMAT " codec data length: %"
698 G_GUINT32_FORMAT ", media specific data length: %" G_GUINT32_FORMAT,
699 (guint16) asfpad->stream_number, codec_data_length,
700 media_specific_data_length);
701
702 gst_asf_put_guid (*buf, guids[ASF_STREAM_PROPERTIES_OBJECT_INDEX]);
703 GST_WRITE_UINT64_LE (*buf + 16, ASF_STREAM_PROPERTIES_OBJECT_SIZE + codec_data_length + media_specific_data_length); /* object size */
704
705 /* stream type */
706 if (asfpad->is_audio)
707 gst_asf_put_guid (*buf + 24, guids[ASF_AUDIO_MEDIA_INDEX]);
708 else
709 gst_asf_put_guid (*buf + 24, guids[ASF_VIDEO_MEDIA_INDEX]);
710 /* error correction */
711 gst_asf_put_guid (*buf + 40, guids[ASF_NO_ERROR_CORRECTION_INDEX]);
712 GST_WRITE_UINT64_LE (*buf + 56, 0); /* time offset */
713
714 GST_WRITE_UINT32_LE (*buf + 64, codec_data_length + media_specific_data_length); /* type specific data length */
715 GST_WRITE_UINT32_LE (*buf + 68, 0); /* error correction data length */
716
717 flags = (asfpad->stream_number & 0x7F);
718 GST_WRITE_UINT16_LE (*buf + 72, flags);
719 GST_WRITE_UINT32_LE (*buf + 74, 0); /* reserved */
720
721 *buf += ASF_STREAM_PROPERTIES_OBJECT_SIZE;
722 /* audio specific data */
723 if (asfpad->is_audio) {
724 GstAsfAudioPad *audiopad = (GstAsfAudioPad *) asfpad;
725 GST_WRITE_UINT16_LE (*buf, audiopad->audioinfo.format);
726 GST_WRITE_UINT16_LE (*buf + 2, audiopad->audioinfo.channels);
727 GST_WRITE_UINT32_LE (*buf + 4, audiopad->audioinfo.rate);
728 GST_WRITE_UINT32_LE (*buf + 8, audiopad->audioinfo.av_bps);
729 GST_WRITE_UINT16_LE (*buf + 12, audiopad->audioinfo.blockalign);
730 GST_WRITE_UINT16_LE (*buf + 14, audiopad->audioinfo.bits_per_sample);
731 GST_WRITE_UINT16_LE (*buf + 16, codec_data_length);
732
733 GST_DEBUG_OBJECT (asfmux,
734 "wave formatex values: codec_id=%" G_GUINT16_FORMAT ", channels=%"
735 G_GUINT16_FORMAT ", rate=%" G_GUINT32_FORMAT ", bytes_per_sec=%"
736 G_GUINT32_FORMAT ", block_alignment=%" G_GUINT16_FORMAT
737 ", bits_per_sample=%" G_GUINT16_FORMAT ", codec_data_length=%u",
738 audiopad->audioinfo.format, audiopad->audioinfo.channels,
739 audiopad->audioinfo.rate, audiopad->audioinfo.av_bps,
740 audiopad->audioinfo.blockalign, audiopad->audioinfo.bits_per_sample,
741 codec_data_length);
742
743
744 *buf += ASF_AUDIO_SPECIFIC_DATA_SIZE;
745 } else {
746 GstAsfVideoPad *videopad = (GstAsfVideoPad *) asfpad;
747 GST_WRITE_UINT32_LE (*buf, (guint32) videopad->vidinfo.width);
748 GST_WRITE_UINT32_LE (*buf + 4, (guint32) videopad->vidinfo.height);
749 GST_WRITE_UINT8 (*buf + 8, 2);
750
751 /* the BITMAPINFOHEADER size + codec_data size */
752 GST_WRITE_UINT16_LE (*buf + 9,
753 ASF_VIDEO_SPECIFIC_DATA_SIZE + codec_data_length - 11);
754
755 /* BITMAPINFOHEADER */
756 GST_WRITE_UINT32_LE (*buf + 11,
757 ASF_VIDEO_SPECIFIC_DATA_SIZE + codec_data_length - 11);
758 gst_asf_put_i32 (*buf + 15, videopad->vidinfo.width);
759 gst_asf_put_i32 (*buf + 19, videopad->vidinfo.height);
760 GST_WRITE_UINT16_LE (*buf + 23, 1); /* reserved */
761 GST_WRITE_UINT16_LE (*buf + 25, videopad->vidinfo.bit_cnt);
762 GST_WRITE_UINT32_LE (*buf + 27, videopad->vidinfo.compression);
763 GST_WRITE_UINT32_LE (*buf + 31, videopad->vidinfo.width *
764 videopad->vidinfo.height * videopad->vidinfo.bit_cnt);
765 GST_WRITE_UINT32_LE (*buf + 35, videopad->vidinfo.xpels_meter);
766 GST_WRITE_UINT32_LE (*buf + 39, videopad->vidinfo.ypels_meter);
767 GST_WRITE_UINT32_LE (*buf + 43, videopad->vidinfo.num_colors);
768 GST_WRITE_UINT32_LE (*buf + 47, videopad->vidinfo.imp_colors);
769
770 *buf += ASF_VIDEO_SPECIFIC_DATA_SIZE;
771 }
772
773 if (codec_data_length > 0)
774 gst_buffer_extract (asfpad->codec_data, 0, *buf, codec_data_length);
775
776 *buf += codec_data_length;
777 }
778
779 /**
780 * gst_asf_mux_write_header_extension:
781 * @asfmux:
782 * @buf: pointer to the buffer pointer
783 * @extension_size: size of the extensions
784 *
785 * Writes the header of the header extension object. The buffer pointer
786 * is incremented to the next writing position (the header extension object
787 * childs should be writen from that point)
788 */
789 static void
gst_asf_mux_write_header_extension(GstAsfMux * asfmux,guint8 ** buf,guint64 extension_size)790 gst_asf_mux_write_header_extension (GstAsfMux * asfmux, guint8 ** buf,
791 guint64 extension_size)
792 {
793 gst_asf_put_guid (*buf, guids[ASF_HEADER_EXTENSION_OBJECT_INDEX]);
794 GST_WRITE_UINT64_LE (*buf + 16, ASF_HEADER_EXTENSION_OBJECT_SIZE + extension_size); /* object size */
795 gst_asf_put_guid (*buf + 24, guids[ASF_RESERVED_1_INDEX]); /* reserved */
796 GST_WRITE_UINT16_LE (*buf + 40, 6); /* reserved */
797 GST_WRITE_UINT32_LE (*buf + 42, extension_size); /* header extension data size */
798 *buf += ASF_HEADER_EXTENSION_OBJECT_SIZE;
799 }
800
801 /**
802 * gst_asf_mux_write_extended_stream_properties:
803 * @asfmux:
804 * @buf: pointer to the buffer pointer
805 * @asfpad: Pad that handles the stream of the properties to be writen
806 *
807 * Writes the extended stream properties object (that is part of the
808 * header extension objects) for the stream handled by asfpad
809 */
810 static void
gst_asf_mux_write_extended_stream_properties(GstAsfMux * asfmux,guint8 ** buf,GstAsfPad * asfpad)811 gst_asf_mux_write_extended_stream_properties (GstAsfMux * asfmux, guint8 ** buf,
812 GstAsfPad * asfpad)
813 {
814 gst_asf_put_guid (*buf, guids[ASF_EXTENDED_STREAM_PROPERTIES_OBJECT_INDEX]);
815 GST_WRITE_UINT64_LE (*buf + 16, ASF_EXTENDED_STREAM_PROPERTIES_OBJECT_SIZE);
816 GST_WRITE_UINT64_LE (*buf + 24, 0); /* start time */
817 GST_WRITE_UINT64_LE (*buf + 32, 0); /* end time */
818 GST_WRITE_UINT32_LE (*buf + 40, asfpad->bitrate); /* bitrate */
819 GST_WRITE_UINT32_LE (*buf + 44, 0); /* buffer size */
820 GST_WRITE_UINT32_LE (*buf + 48, 0); /* initial buffer fullness */
821 GST_WRITE_UINT32_LE (*buf + 52, asfpad->bitrate); /* alternate data bitrate */
822 GST_WRITE_UINT32_LE (*buf + 56, 0); /* alternate buffer size */
823 GST_WRITE_UINT32_LE (*buf + 60, 0); /* alternate initial buffer fullness */
824 GST_WRITE_UINT32_LE (*buf + 64, 0); /* maximum object size */
825
826 /* flags */
827 if (asfpad->is_audio) {
828 /* TODO check if audio is seekable */
829 GST_WRITE_UINT32_LE (*buf + 68, 0x0);
830 } else {
831 /* video has indexes, so it is seekable unless we are streaming */
832 if (asfmux->prop_streamable)
833 GST_WRITE_UINT32_LE (*buf + 68, 0x0);
834 else
835 GST_WRITE_UINT32_LE (*buf + 68, 0x2);
836 }
837
838 GST_WRITE_UINT16_LE (*buf + 72, asfpad->stream_number);
839 GST_WRITE_UINT16_LE (*buf + 74, 0); /* language index */
840 GST_WRITE_UINT64_LE (*buf + 76, 0); /* avg time per frame */
841 GST_WRITE_UINT16_LE (*buf + 84, 0); /* stream name count */
842 GST_WRITE_UINT16_LE (*buf + 86, 0); /* payload extension count */
843
844 *buf += ASF_EXTENDED_STREAM_PROPERTIES_OBJECT_SIZE;
845 }
846
847 /**
848 * gst_asf_mux_write_string_with_size:
849 * @asfmux:
850 * @size_buf: pointer to the memory position to write the size of the string
851 * @str_buf: pointer to the memory position to write the string
852 * @str: the string to be writen (in UTF-8)
853 * @use32: if the string size should be writen with 32 bits (if true)
854 * or with 16 (if false)
855 *
856 * Writes a string with its size as it is needed in many asf objects.
857 * The size is writen to size_buf as a WORD field if use32 is false, and
858 * as a DWORD if use32 is true. The string is writen to str_buf in UTF16-LE.
859 * The string should be passed in UTF-8.
860 *
861 * The string size in UTF16-LE is returned.
862 */
863 static guint64
gst_asf_mux_write_string_with_size(GstAsfMux * asfmux,guint8 * size_buf,guint8 * str_buf,const gchar * str,gboolean use32)864 gst_asf_mux_write_string_with_size (GstAsfMux * asfmux,
865 guint8 * size_buf, guint8 * str_buf, const gchar * str, gboolean use32)
866 {
867 GError *error = NULL;
868 gsize str_size = 0;
869 gchar *str_utf16 = NULL;
870
871 GST_LOG_OBJECT (asfmux, "Writing extended content description string: "
872 "%s", str);
873
874 /*
875 * Covert the string to utf16
876 * Also force the last bytes to null terminated,
877 * tags were with extra weird characters without it.
878 */
879 str_utf16 = g_convert (str, -1, "UTF-16LE", "UTF-8", NULL, &str_size, &error);
880
881 /* sum up the null terminating char */
882 str_size += 2;
883
884 if (use32)
885 GST_WRITE_UINT32_LE (size_buf, str_size);
886 else
887 GST_WRITE_UINT16_LE (size_buf, str_size);
888 if (error) {
889 GST_WARNING_OBJECT (asfmux, "Error converting string "
890 "to UTF-16: %s - %s", str, error->message);
891 g_error_free (error);
892 memset (str_buf, 0, str_size);
893 } else {
894 /* HACK: g_convert seems to add only a single byte null char to
895 * the end of the stream, we force the second one */
896 memcpy (str_buf, str_utf16, str_size - 1);
897 str_buf[str_size - 1] = 0;
898 }
899 g_free (str_utf16);
900 return str_size;
901 }
902
903 /**
904 * gst_asf_mux_write_content_description_entry:
905 * @asfmux:
906 * @tags:
907 * @tagname:
908 * @size_buf:
909 * @data_buf:
910 *
911 * Checks if a string tag with tagname exists in the taglist. If it
912 * exists it is writen as an UTF-16LE to data_buf and its size in bytes
913 * is writen to size_buf. It is used for writing content description
914 * object fields.
915 *
916 * Returns: the size of the string
917 */
918 static guint16
gst_asf_mux_write_content_description_entry(GstAsfMux * asfmux,const GstTagList * tags,const gchar * tagname,guint8 * size_buf,guint8 * data_buf)919 gst_asf_mux_write_content_description_entry (GstAsfMux * asfmux,
920 const GstTagList * tags, const gchar * tagname,
921 guint8 * size_buf, guint8 * data_buf)
922 {
923 gchar *text = NULL;
924 guint16 text_size = 0;
925 if (gst_tag_list_get_string (tags, tagname, &text)) {
926 text_size = gst_asf_mux_write_string_with_size (asfmux, size_buf,
927 data_buf, text, FALSE);
928 g_free (text);
929 } else {
930 GST_WRITE_UINT16_LE (size_buf, 0);
931 }
932 return text_size;
933 }
934
935 static guint64
gst_asf_mux_write_ext_content_description_dword_entry(GstAsfMux * asfmux,guint8 * buf,const gchar * asf_tag,const guint32 value)936 gst_asf_mux_write_ext_content_description_dword_entry (GstAsfMux * asfmux,
937 guint8 * buf, const gchar * asf_tag, const guint32 value)
938 {
939 guint64 tag_size;
940 GST_DEBUG_OBJECT (asfmux, "Writing extended content description tag: "
941 "%s (%u)", asf_tag, value);
942
943 tag_size = gst_asf_mux_write_string_with_size (asfmux, buf, buf + 2,
944 asf_tag, FALSE);
945 buf += tag_size + 2;
946 GST_WRITE_UINT16_LE (buf, ASF_TAG_TYPE_DWORD);
947 GST_WRITE_UINT16_LE (buf + 2, 4);
948 GST_WRITE_UINT32_LE (buf + 4, value);
949
950 /* tagsize -> string size
951 * 2 -> string size field size
952 * 4 -> dword entry
953 * 4 -> type of entry + entry size
954 */
955 return tag_size + 2 + 4 + 4;
956 }
957
958 static guint64
gst_asf_mux_write_ext_content_description_string_entry(GstAsfMux * asfmux,guint8 * buf,const gchar * asf_tag,const gchar * text)959 gst_asf_mux_write_ext_content_description_string_entry (GstAsfMux * asfmux,
960 guint8 * buf, const gchar * asf_tag, const gchar * text)
961 {
962 guint64 tag_size = 0;
963 guint64 text_size = 0;
964
965 GST_DEBUG_OBJECT (asfmux, "Writing extended content description tag: "
966 "%s (%s)", asf_tag, text);
967
968 tag_size = gst_asf_mux_write_string_with_size (asfmux,
969 buf, buf + 2, asf_tag, FALSE);
970 GST_WRITE_UINT16_LE (buf + tag_size + 2, ASF_TAG_TYPE_UNICODE_STR);
971 buf += tag_size + 2 + 2;
972 text_size = gst_asf_mux_write_string_with_size (asfmux,
973 buf, buf + 2, text, FALSE);
974
975 /* the size of the strings in utf16-le plus the 3 WORD fields */
976 return tag_size + text_size + 6;
977 }
978
979 static void
gst_asf_mux_write_content_description(GstAsfMux * asfmux,guint8 ** buf,const GstTagList * tags)980 gst_asf_mux_write_content_description (GstAsfMux * asfmux, guint8 ** buf,
981 const GstTagList * tags)
982 {
983 guint8 *values = (*buf) + ASF_CONTENT_DESCRIPTION_OBJECT_SIZE;
984 guint64 size = 0;
985
986 GST_DEBUG_OBJECT (asfmux, "Writing content description object");
987
988 gst_asf_put_guid (*buf, guids[ASF_CONTENT_DESCRIPTION_INDEX]);
989
990 values += gst_asf_mux_write_content_description_entry (asfmux, tags,
991 GST_TAG_TITLE, *buf + 24, values);
992 values += gst_asf_mux_write_content_description_entry (asfmux, tags,
993 GST_TAG_ARTIST, *buf + 26, values);
994 values += gst_asf_mux_write_content_description_entry (asfmux, tags,
995 GST_TAG_COPYRIGHT, *buf + 28, values);
996 values += gst_asf_mux_write_content_description_entry (asfmux, tags,
997 GST_TAG_DESCRIPTION, *buf + 30, values);
998
999 /* rating is currently not present in gstreamer tags, so we put 0 */
1000 GST_WRITE_UINT16_LE (*buf + 32, 0);
1001
1002 size += values - *buf;
1003 GST_WRITE_UINT64_LE (*buf + 16, size);
1004 *buf += size;
1005 }
1006
1007 static void
write_ext_content_description_tag(const GstTagList * taglist,const gchar * tag,gpointer user_data)1008 write_ext_content_description_tag (const GstTagList * taglist,
1009 const gchar * tag, gpointer user_data)
1010 {
1011 const gchar *asftag = gst_asf_get_asf_tag (tag);
1012 GValue value = { 0 };
1013 guint type;
1014 GstAsfExtContDescData *data = (GstAsfExtContDescData *) user_data;
1015
1016 if (asftag == NULL)
1017 return;
1018
1019 if (!gst_tag_list_copy_value (&value, taglist, tag)) {
1020 return;
1021 }
1022
1023 type = gst_asf_get_tag_field_type (&value);
1024 switch (type) {
1025 case ASF_TAG_TYPE_UNICODE_STR:
1026 {
1027 const gchar *text;
1028 text = g_value_get_string (&value);
1029 data->size +=
1030 gst_asf_mux_write_ext_content_description_string_entry (data->asfmux,
1031 data->buf + data->size, asftag, text);
1032 }
1033 break;
1034 case ASF_TAG_TYPE_DWORD:
1035 {
1036 guint num = g_value_get_uint (&value);
1037 data->size +=
1038 gst_asf_mux_write_ext_content_description_dword_entry (data->asfmux,
1039 data->buf + data->size, asftag, num);
1040 }
1041 break;
1042 default:
1043 GST_WARNING_OBJECT (data->asfmux,
1044 "Unhandled asf tag field type %u for tag %s", type, tag);
1045 g_value_reset (&value);
1046 return;
1047 }
1048 data->count++;
1049 g_value_reset (&value);
1050 }
1051
1052 static void
gst_asf_mux_write_ext_content_description(GstAsfMux * asfmux,guint8 ** buf,GstTagList * tags)1053 gst_asf_mux_write_ext_content_description (GstAsfMux * asfmux, guint8 ** buf,
1054 GstTagList * tags)
1055 {
1056 GstAsfExtContDescData extContDesc;
1057 extContDesc.asfmux = asfmux;
1058 extContDesc.buf = *buf;
1059 extContDesc.count = 0;
1060 extContDesc.size = ASF_EXT_CONTENT_DESCRIPTION_OBJECT_SIZE;
1061
1062 GST_DEBUG_OBJECT (asfmux, "Writing extended content description object");
1063 gst_asf_put_guid (*buf, guids[ASF_EXT_CONTENT_DESCRIPTION_INDEX]);
1064
1065 gst_tag_list_foreach (tags, write_ext_content_description_tag, &extContDesc);
1066
1067 GST_WRITE_UINT64_LE (*buf + 16, extContDesc.size);
1068 GST_WRITE_UINT16_LE (*buf + 24, extContDesc.count);
1069
1070 *buf += extContDesc.size;
1071 }
1072
1073 static void
write_metadata_tag(const GstTagList * taglist,const gchar * tag,gpointer user_data)1074 write_metadata_tag (const GstTagList * taglist, const gchar * tag,
1075 gpointer user_data)
1076 {
1077 const gchar *asftag = gst_asf_get_asf_tag (tag);
1078 GValue value = { 0 };
1079 guint type;
1080 GstAsfMetadataObjData *data = (GstAsfMetadataObjData *) user_data;
1081 guint16 tag_size;
1082 guint32 content_size;
1083
1084 if (asftag == NULL)
1085 return;
1086
1087 if (!gst_tag_list_copy_value (&value, taglist, tag)) {
1088 return;
1089 }
1090
1091 type = gst_asf_get_tag_field_type (&value);
1092 switch (type) {
1093 case ASF_TAG_TYPE_UNICODE_STR:
1094 {
1095 const gchar *text;
1096 text = g_value_get_string (&value);
1097 GST_WRITE_UINT16_LE (data->buf + data->size, 0);
1098 GST_WRITE_UINT16_LE (data->buf + data->size + 2, data->stream_num);
1099 data->size += 4;
1100
1101 tag_size = gst_asf_mux_write_string_with_size (data->asfmux,
1102 data->buf + data->size, data->buf + data->size + 8, asftag, FALSE);
1103 data->size += 2;
1104
1105 GST_WRITE_UINT16_LE (data->buf + data->size, type);
1106 data->size += 2;
1107
1108 content_size = gst_asf_mux_write_string_with_size (data->asfmux,
1109 data->buf + data->size, data->buf + data->size + tag_size + 4, text,
1110 TRUE);
1111 data->size += tag_size + content_size + 4;
1112 }
1113 break;
1114 case ASF_TAG_TYPE_DWORD:
1115 {
1116 guint num = g_value_get_uint (&value);
1117 GST_WRITE_UINT16_LE (data->buf + data->size, 0);
1118 GST_WRITE_UINT16_LE (data->buf + data->size + 2, data->stream_num);
1119 data->size += 4;
1120
1121 tag_size = gst_asf_mux_write_string_with_size (data->asfmux,
1122 data->buf + data->size, data->buf + data->size + 8, asftag, FALSE);
1123 data->size += 2;
1124
1125 GST_WRITE_UINT16_LE (data->buf + data->size, type);
1126 data->size += 2;
1127 /* dword length */
1128 GST_WRITE_UINT32_LE (data->buf + data->size, 4);
1129 data->size += 4 + tag_size;
1130
1131 GST_WRITE_UINT32_LE (data->buf + data->size, num);
1132 data->size += 4;
1133 }
1134 break;
1135 default:
1136 GST_WARNING_OBJECT (data->asfmux,
1137 "Unhandled asf tag field type %u for tag %s", type, tag);
1138 g_value_reset (&value);
1139 return;
1140 }
1141
1142 data->count++;
1143 g_value_reset (&value);
1144 }
1145
1146 static void
gst_asf_mux_write_metadata_object(GstAsfMux * asfmux,guint8 ** buf,GstAsfPad * asfpad)1147 gst_asf_mux_write_metadata_object (GstAsfMux * asfmux, guint8 ** buf,
1148 GstAsfPad * asfpad)
1149 {
1150 GstAsfMetadataObjData metaObjData;
1151 metaObjData.asfmux = asfmux;
1152 metaObjData.buf = *buf;
1153 metaObjData.count = 0;
1154 metaObjData.size = ASF_METADATA_OBJECT_SIZE;
1155 metaObjData.stream_num = asfpad->stream_number;
1156
1157 if (asfpad->taglist == NULL || gst_tag_list_is_empty (asfpad->taglist))
1158 return;
1159
1160 GST_DEBUG_OBJECT (asfmux, "Writing metadata object");
1161 gst_asf_put_guid (*buf, guids[ASF_METADATA_OBJECT_INDEX]);
1162
1163 gst_tag_list_foreach (asfpad->taglist, write_metadata_tag, &metaObjData);
1164
1165 GST_WRITE_UINT64_LE (*buf + 16, metaObjData.size);
1166 GST_WRITE_UINT16_LE (*buf + 24, metaObjData.count);
1167
1168 *buf += metaObjData.size;
1169 }
1170
1171 static void
gst_asf_mux_write_padding_object(GstAsfMux * asfmux,guint8 ** buf,guint64 padding)1172 gst_asf_mux_write_padding_object (GstAsfMux * asfmux, guint8 ** buf,
1173 guint64 padding)
1174 {
1175 if (padding < ASF_PADDING_OBJECT_SIZE) {
1176 return;
1177 }
1178
1179 GST_DEBUG_OBJECT (asfmux, "Writing padding object of size %" G_GUINT64_FORMAT,
1180 padding);
1181 gst_asf_put_guid (*buf, guids[ASF_PADDING_OBJECT_INDEX]);
1182 GST_WRITE_UINT64_LE (*buf + 16, padding);
1183 memset (*buf + 24, 0, padding - ASF_PADDING_OBJECT_SIZE);
1184 *buf += padding;
1185 }
1186
1187 static void
gst_asf_mux_write_data_object(GstAsfMux * asfmux,guint8 ** buf)1188 gst_asf_mux_write_data_object (GstAsfMux * asfmux, guint8 ** buf)
1189 {
1190 gst_asf_put_guid (*buf, guids[ASF_DATA_OBJECT_INDEX]);
1191
1192 /* Data object size. This is always >= ASF_DATA_OBJECT_SIZE. The standard
1193 * specifically accepts the value 0 in live streams, but WMP is not accepting
1194 * this while streaming using WMSP, so we default to minimum size also for
1195 * live streams. Otherwise this field must be updated later on when we know
1196 * the complete stream size.
1197 */
1198 GST_WRITE_UINT64_LE (*buf + 16, ASF_DATA_OBJECT_SIZE);
1199
1200 gst_asf_put_guid (*buf + 24, asfmux->file_id);
1201 GST_WRITE_UINT64_LE (*buf + 40, 0); /* total data packets */
1202 GST_WRITE_UINT16_LE (*buf + 48, 0x0101); /* reserved */
1203 *buf += ASF_DATA_OBJECT_SIZE;
1204 }
1205
1206 static void
gst_asf_mux_put_buffer_in_streamheader(GValue * streamheader,GstBuffer * buffer)1207 gst_asf_mux_put_buffer_in_streamheader (GValue * streamheader,
1208 GstBuffer * buffer)
1209 {
1210 GValue value = { 0 };
1211 GstBuffer *buf;
1212
1213 g_value_init (&value, GST_TYPE_BUFFER);
1214 buf = gst_buffer_copy (buffer);
1215 gst_value_set_buffer (&value, buf);
1216 gst_buffer_unref (buf);
1217 gst_value_array_append_value (streamheader, &value);
1218 g_value_unset (&value);
1219 }
1220
1221 static guint
gst_asf_mux_find_payload_parsing_info_size(GstAsfMux * asfmux)1222 gst_asf_mux_find_payload_parsing_info_size (GstAsfMux * asfmux)
1223 {
1224 /* Minimum payload parsing information size is 8 bytes */
1225 guint size = 8;
1226
1227 if (asfmux->prop_packet_size > 65535)
1228 size += 4;
1229 else
1230 size += 2;
1231
1232 if (asfmux->prop_padding > 65535)
1233 size += 4;
1234 else
1235 size += 2;
1236
1237 return size;
1238 }
1239
1240 /**
1241 * gst_asf_mux_start_file:
1242 * @asfmux: #GstAsfMux
1243 *
1244 * Starts the asf file/stream by creating and pushing
1245 * the headers downstream.
1246 */
1247 static GstFlowReturn
gst_asf_mux_start_file(GstAsfMux * asfmux)1248 gst_asf_mux_start_file (GstAsfMux * asfmux)
1249 {
1250 GstBuffer *buf = NULL;
1251 guint8 *bufdata = NULL;
1252 GSList *walk;
1253 guint stream_num = g_slist_length (asfmux->collect->data);
1254 guint metadata_obj_size = 0;
1255 GstAsfTags *asftags;
1256 GValue streamheader = { 0 };
1257 GstCaps *caps;
1258 GstStructure *structure;
1259 guint64 padding = asfmux->prop_padding;
1260 GstSegment segment;
1261 GstMapInfo map;
1262 gsize bufsize;
1263 gchar s_id[32];
1264
1265 if (padding < ASF_PADDING_OBJECT_SIZE)
1266 padding = 0;
1267
1268 /* if not streaming, check if downstream is seekable */
1269 if (!asfmux->prop_streamable) {
1270 gboolean seekable;
1271 GstQuery *query;
1272
1273 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1274 if (gst_pad_peer_query (asfmux->srcpad, query)) {
1275 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1276 GST_INFO_OBJECT (asfmux, "downstream is %sseekable",
1277 seekable ? "" : "not ");
1278 } else {
1279 /* assume seeking is not supported if query not handled downstream */
1280 GST_WARNING_OBJECT (asfmux, "downstream did not handle seeking query");
1281 seekable = FALSE;
1282 }
1283 if (!seekable) {
1284 asfmux->prop_streamable = TRUE;
1285 g_object_notify (G_OBJECT (asfmux), "streamable");
1286 GST_WARNING_OBJECT (asfmux, "downstream is not seekable, but "
1287 "streamable=false. Will ignore that and create streamable output "
1288 "instead");
1289 }
1290 gst_query_unref (query);
1291 }
1292
1293 /* from this point we started writing the headers */
1294 GST_INFO_OBJECT (asfmux, "Writing headers");
1295 asfmux->state = GST_ASF_MUX_STATE_HEADERS;
1296
1297 /* stream-start (FIXME: create id based on input ids) */
1298 g_snprintf (s_id, sizeof (s_id), "asfmux-%08x", g_random_int ());
1299 gst_pad_push_event (asfmux->srcpad, gst_event_new_stream_start (s_id));
1300
1301 caps = gst_pad_get_pad_template_caps (asfmux->srcpad);
1302 gst_pad_set_caps (asfmux->srcpad, caps);
1303 gst_caps_unref (caps);
1304
1305 /* send a BYTE format segment if we're going to seek to fix up the headers
1306 * later, otherwise send a TIME segment */
1307 if (asfmux->prop_streamable)
1308 gst_segment_init (&segment, GST_FORMAT_TIME);
1309 else
1310 gst_segment_init (&segment, GST_FORMAT_BYTES);
1311 gst_pad_push_event (asfmux->srcpad, gst_event_new_segment (&segment));
1312
1313 gst_asf_generate_file_id (&asfmux->file_id);
1314
1315 /* Get the metadata for content description object.
1316 * We store our own taglist because it might get changed from now
1317 * to the time we actually add its contents to the file, changing
1318 * the size of the data we already calculated here.
1319 */
1320 asftags = g_new0 (GstAsfTags, 1);
1321 gst_asf_mux_get_content_description_tags (asfmux, asftags);
1322
1323 /* get the total metadata objects size */
1324 for (walk = asfmux->collect->data; walk; walk = g_slist_next (walk)) {
1325 metadata_obj_size += gst_asf_mux_get_metadata_object_size (asfmux,
1326 (GstAsfPad *) walk->data);
1327 }
1328
1329 /* alloc a buffer for all header objects */
1330 buf = gst_buffer_new_and_alloc (gst_asf_mux_get_headers_size (asfmux) +
1331 asftags->cont_desc_size +
1332 asftags->ext_cont_desc_size +
1333 metadata_obj_size + padding + ASF_DATA_OBJECT_SIZE);
1334
1335 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1336 bufdata = map.data;
1337 bufsize = map.size;
1338
1339 gst_asf_mux_write_header_object (asfmux, &bufdata, map.size -
1340 ASF_DATA_OBJECT_SIZE, 2 + stream_num);
1341
1342 /* get the position of the file properties object for
1343 * updating it in gst_asf_mux_stop_file */
1344 asfmux->file_properties_object_position = bufdata - map.data;
1345 gst_asf_mux_write_file_properties (asfmux, &bufdata);
1346
1347 for (walk = asfmux->collect->data; walk; walk = g_slist_next (walk)) {
1348 gst_asf_mux_write_stream_properties (asfmux, &bufdata,
1349 (GstAsfPad *) walk->data);
1350 }
1351
1352 if (asftags->cont_desc_size) {
1353 gst_asf_mux_write_content_description (asfmux, &bufdata, asftags->tags);
1354 }
1355 if (asftags->ext_cont_desc_size) {
1356 gst_asf_mux_write_ext_content_description (asfmux, &bufdata, asftags->tags);
1357 }
1358
1359 if (asftags->tags)
1360 gst_tag_list_unref (asftags->tags);
1361 g_free (asftags);
1362
1363 /* writing header extension objects */
1364 gst_asf_mux_write_header_extension (asfmux, &bufdata, stream_num *
1365 ASF_EXTENDED_STREAM_PROPERTIES_OBJECT_SIZE + metadata_obj_size);
1366 for (walk = asfmux->collect->data; walk; walk = g_slist_next (walk)) {
1367 gst_asf_mux_write_extended_stream_properties (asfmux, &bufdata,
1368 (GstAsfPad *) walk->data);
1369 }
1370 for (walk = asfmux->collect->data; walk; walk = g_slist_next (walk)) {
1371 gst_asf_mux_write_metadata_object (asfmux, &bufdata,
1372 (GstAsfPad *) walk->data);
1373 }
1374
1375 gst_asf_mux_write_padding_object (asfmux, &bufdata, padding);
1376
1377 /* store data object position for later updating some fields */
1378 asfmux->data_object_position = bufdata - map.data;
1379 gst_asf_mux_write_data_object (asfmux, &bufdata);
1380
1381 /* set streamheader in source pad if 'streamable' */
1382 if (asfmux->prop_streamable) {
1383 g_value_init (&streamheader, GST_TYPE_ARRAY);
1384 gst_asf_mux_put_buffer_in_streamheader (&streamheader, buf);
1385
1386 caps = gst_pad_get_current_caps (asfmux->srcpad);
1387 caps = gst_caps_make_writable (caps);
1388 structure = gst_caps_get_structure (caps, 0);
1389 gst_structure_set_value (structure, "streamheader", &streamheader);
1390 gst_pad_set_caps (asfmux->srcpad, caps);
1391 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
1392 g_value_unset (&streamheader);
1393 gst_caps_unref (caps);
1394 }
1395
1396 g_assert (bufdata - map.data == map.size);
1397 gst_buffer_unmap (buf, &map);
1398 return gst_asf_mux_push_buffer (asfmux, buf, bufsize);
1399 }
1400
1401 /**
1402 * gst_asf_mux_add_simple_index_entry:
1403 * @asfmux:
1404 * @videopad:
1405 *
1406 * Adds a new entry to the simple index of the stream handler by videopad.
1407 * This functions doesn't check if the time ellapsed
1408 * is larger than the established time interval between entries. The caller
1409 * is responsible for verifying this.
1410 */
1411 static void
gst_asf_mux_add_simple_index_entry(GstAsfMux * asfmux,GstAsfVideoPad * videopad)1412 gst_asf_mux_add_simple_index_entry (GstAsfMux * asfmux,
1413 GstAsfVideoPad * videopad)
1414 {
1415 SimpleIndexEntry *entry = NULL;
1416 GST_DEBUG_OBJECT (asfmux, "Adding new simple index entry "
1417 "packet number: %" G_GUINT32_FORMAT ", "
1418 "packet count: %" G_GUINT16_FORMAT,
1419 videopad->last_keyframe_packet, videopad->last_keyframe_packet_count);
1420 entry = g_malloc0 (sizeof (SimpleIndexEntry));
1421 entry->packet_number = videopad->last_keyframe_packet;
1422 entry->packet_count = videopad->last_keyframe_packet_count;
1423 if (entry->packet_count > videopad->max_keyframe_packet_count)
1424 videopad->max_keyframe_packet_count = entry->packet_count;
1425 videopad->simple_index = g_slist_append (videopad->simple_index, entry);
1426 }
1427
1428 /**
1429 * gst_asf_mux_send_packet:
1430 * @asfmux:
1431 * @buf: The asf data packet
1432 *
1433 * Pushes an asf data packet downstream. The total number
1434 * of packets and bytes of the stream are incremented.
1435 *
1436 * Returns: the result of pushing the buffer downstream
1437 */
1438 static GstFlowReturn
gst_asf_mux_send_packet(GstAsfMux * asfmux,GstBuffer * buf,gsize bufsize)1439 gst_asf_mux_send_packet (GstAsfMux * asfmux, GstBuffer * buf, gsize bufsize)
1440 {
1441 g_assert (bufsize == asfmux->packet_size);
1442 asfmux->total_data_packets++;
1443 GST_LOG_OBJECT (asfmux,
1444 "Pushing a packet of size %" G_GSIZE_FORMAT " and timestamp %"
1445 G_GUINT64_FORMAT, bufsize, GST_BUFFER_TIMESTAMP (buf));
1446 GST_LOG_OBJECT (asfmux, "Total data packets: %" G_GUINT64_FORMAT,
1447 asfmux->total_data_packets);
1448 return gst_asf_mux_push_buffer (asfmux, buf, bufsize);
1449 }
1450
1451 /**
1452 * gst_asf_mux_flush_payloads:
1453 * @asfmux: #GstAsfMux to flush the payloads from
1454 *
1455 * Fills an asf packet with asfmux queued payloads and
1456 * pushes it downstream.
1457 *
1458 * Returns: The result of pushing the packet
1459 */
1460 static GstFlowReturn
gst_asf_mux_flush_payloads(GstAsfMux * asfmux)1461 gst_asf_mux_flush_payloads (GstAsfMux * asfmux)
1462 {
1463 GstBuffer *buf;
1464 guint8 payloads_count = 0; /* we only use 6 bits, max is 63 */
1465 guint i;
1466 GstClockTime send_ts = GST_CLOCK_TIME_NONE;
1467 guint64 size_left;
1468 guint8 *data;
1469 gsize size;
1470 GSList *walk;
1471 GstAsfPad *pad;
1472 gboolean has_keyframe;
1473 AsfPayload *payload;
1474 guint32 payload_size;
1475 guint offset;
1476 GstMapInfo map;
1477
1478 if (asfmux->payloads == NULL)
1479 return GST_FLOW_OK; /* nothing to send is ok */
1480
1481 GST_LOG_OBJECT (asfmux, "Flushing payloads");
1482
1483 buf = gst_buffer_new_and_alloc (asfmux->packet_size);
1484 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1485 memset (map.data, 0, asfmux->packet_size);
1486
1487 /* 1 for the multiple payload flags */
1488 data = map.data + asfmux->payload_parsing_info_size + 1;
1489 size_left = asfmux->packet_size - asfmux->payload_parsing_info_size - 1;
1490
1491 has_keyframe = FALSE;
1492 walk = asfmux->payloads;
1493 while (walk && payloads_count < MAX_PAYLOADS_IN_A_PACKET) {
1494 payload = (AsfPayload *) walk->data;
1495 pad = (GstAsfPad *) payload->pad;
1496 payload_size = gst_asf_payload_get_size (payload);
1497 if (size_left < payload_size) {
1498 break; /* next payload doesn't fit fully */
1499 }
1500
1501 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (send_ts))) {
1502 send_ts = GST_BUFFER_TIMESTAMP (payload->data);
1503 }
1504
1505 /* adding new simple index entry (if needed) */
1506 if (!pad->is_audio
1507 && GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (payload->data))) {
1508 GstAsfVideoPad *videopad = (GstAsfVideoPad *) pad;
1509 if (videopad->has_keyframe) {
1510 for (; videopad->next_index_time <=
1511 ASF_MILI_TO_100NANO (payload->presentation_time);
1512 videopad->next_index_time += videopad->time_interval) {
1513 gst_asf_mux_add_simple_index_entry (asfmux, videopad);
1514 }
1515 }
1516 }
1517
1518 /* serialize our payload */
1519 GST_DEBUG_OBJECT (asfmux, "Serializing payload into packet");
1520 GST_DEBUG_OBJECT (asfmux, "stream number: %d", pad->stream_number & 0x7F);
1521 GST_DEBUG_OBJECT (asfmux, "media object number: %d",
1522 (gint) payload->media_obj_num);
1523 GST_DEBUG_OBJECT (asfmux, "offset into media object: %" G_GUINT32_FORMAT,
1524 payload->offset_in_media_obj);
1525 GST_DEBUG_OBJECT (asfmux, "media object size: %" G_GUINT32_FORMAT,
1526 payload->media_object_size);
1527 GST_DEBUG_OBJECT (asfmux, "replicated data length: %d",
1528 (gint) payload->replicated_data_length);
1529 GST_DEBUG_OBJECT (asfmux, "payload size: %" G_GSIZE_FORMAT,
1530 gst_buffer_get_size (payload->data));
1531 GST_DEBUG_OBJECT (asfmux, "presentation time: %" G_GUINT32_FORMAT " (%"
1532 GST_TIME_FORMAT ")", payload->presentation_time,
1533 GST_TIME_ARGS (payload->presentation_time * GST_MSECOND));
1534 GST_DEBUG_OBJECT (asfmux, "keyframe: %s",
1535 (payload->stream_number & 0x80 ? "yes" : "no"));
1536 GST_DEBUG_OBJECT (asfmux, "buffer timestamp: %" GST_TIME_FORMAT,
1537 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (payload->data)));
1538 GST_DEBUG_OBJECT (asfmux, "buffer duration %" GST_TIME_FORMAT,
1539 GST_TIME_ARGS (GST_BUFFER_DURATION (payload->data)));
1540
1541 gst_asf_put_payload (data, payload);
1542 if (!payload->has_packet_info) {
1543 payload->has_packet_info = TRUE;
1544 payload->packet_number = asfmux->total_data_packets;
1545 }
1546 GST_DEBUG_OBJECT (asfmux, "packet number: %" G_GUINT32_FORMAT,
1547 payload->packet_number);
1548
1549 if (ASF_PAYLOAD_IS_KEYFRAME (payload)) {
1550 has_keyframe = TRUE;
1551 if (!pad->is_audio) {
1552 GstAsfVideoPad *videopad = (GstAsfVideoPad *) pad;
1553 videopad->last_keyframe_packet = payload->packet_number;
1554 videopad->last_keyframe_packet_count = payload->packet_count;
1555 videopad->has_keyframe = TRUE;
1556 }
1557 }
1558
1559 /* update our variables */
1560 data += payload_size;
1561 size_left -= payload_size;
1562 payloads_count++;
1563 walk = g_slist_next (walk);
1564 }
1565
1566 /* remove flushed payloads */
1567 GST_LOG_OBJECT (asfmux, "Freeing already used payloads");
1568 for (i = 0; i < payloads_count; i++) {
1569 GSList *aux = g_slist_nth (asfmux->payloads, 0);
1570 AsfPayload *payload;
1571 g_assert (aux);
1572 payload = (AsfPayload *) aux->data;
1573 asfmux->payloads = g_slist_remove (asfmux->payloads, payload);
1574 asfmux->payload_data_size -=
1575 (gst_buffer_get_size (payload->data) +
1576 ASF_MULTIPLE_PAYLOAD_HEADER_SIZE);
1577 gst_asf_payload_free (payload);
1578 }
1579
1580 /* check if we can add part of the next payload */
1581 if (asfmux->payloads && size_left > ASF_MULTIPLE_PAYLOAD_HEADER_SIZE) {
1582 AsfPayload *payload =
1583 (AsfPayload *) g_slist_nth (asfmux->payloads, 0)->data;
1584 guint16 bytes_writen;
1585 GST_DEBUG_OBJECT (asfmux, "Adding part of a payload to a packet");
1586
1587 if (ASF_PAYLOAD_IS_KEYFRAME (payload))
1588 has_keyframe = TRUE;
1589
1590 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (send_ts))) {
1591 send_ts = GST_BUFFER_TIMESTAMP (payload->data);
1592 }
1593
1594 bytes_writen = gst_asf_put_subpayload (data, payload, size_left);
1595 if (!payload->has_packet_info) {
1596 payload->has_packet_info = TRUE;
1597 payload->packet_number = asfmux->total_data_packets;
1598 }
1599 asfmux->payload_data_size -= bytes_writen;
1600 size_left -= (bytes_writen + ASF_MULTIPLE_PAYLOAD_HEADER_SIZE);
1601 payloads_count++;
1602 }
1603
1604 GST_LOG_OBJECT (asfmux, "Payload data size: %" G_GUINT32_FORMAT,
1605 asfmux->payload_data_size);
1606
1607 /* fill payload parsing info */
1608 data = map.data;
1609 size = map.size;
1610
1611 /* flags */
1612 GST_WRITE_UINT8 (data, (0x0 << 7) | /* no error correction */
1613 (ASF_FIELD_TYPE_DWORD << 5) | /* packet length type */
1614 (ASF_FIELD_TYPE_DWORD << 3) | /* padding length type */
1615 (ASF_FIELD_TYPE_NONE << 1) | /* sequence type type */
1616 0x1); /* multiple payloads */
1617 offset = 1;
1618
1619 /* property flags - according to the spec, this should not change */
1620 GST_WRITE_UINT8 (data + offset, (ASF_FIELD_TYPE_BYTE << 6) | /* stream number length type */
1621 (ASF_FIELD_TYPE_BYTE << 4) | /* media obj number length type */
1622 (ASF_FIELD_TYPE_DWORD << 2) | /* offset info media object length type */
1623 (ASF_FIELD_TYPE_BYTE)); /* replicated data length type */
1624 offset++;
1625
1626 /* Due to a limitation in WMP while streaming through WMSP we reduce the
1627 * packet & padding size to 16bit if they are <= 65535 bytes
1628 */
1629 if (asfmux->packet_size > 65535) {
1630 GST_WRITE_UINT32_LE (data + offset, asfmux->packet_size - size_left);
1631 offset += 4;
1632 } else {
1633 *data &= ~(ASF_FIELD_TYPE_MASK << 5);
1634 *data |= ASF_FIELD_TYPE_WORD << 5;
1635 GST_WRITE_UINT16_LE (data + offset, asfmux->packet_size - size_left);
1636 offset += 2;
1637 }
1638 if (asfmux->prop_padding > 65535) {
1639 GST_WRITE_UINT32_LE (data + offset, size_left);
1640 offset += 4;
1641 } else {
1642 *data &= ~(ASF_FIELD_TYPE_MASK << 3);
1643 *data |= ASF_FIELD_TYPE_WORD << 3;
1644 GST_WRITE_UINT16_LE (data + offset, size_left);
1645 offset += 2;
1646 }
1647
1648 /* packet send time */
1649 if (GST_CLOCK_TIME_IS_VALID (send_ts)) {
1650 GST_WRITE_UINT32_LE (data + offset, (send_ts / GST_MSECOND));
1651 GST_BUFFER_TIMESTAMP (buf) = send_ts;
1652 }
1653 offset += 4;
1654
1655 /* packet duration */
1656 GST_WRITE_UINT16_LE (data + offset, 0); /* FIXME send duration needs to be estimated */
1657 offset += 2;
1658
1659 /* multiple payloads flags */
1660 GST_WRITE_UINT8 (data + offset, 0x2 << 6 | payloads_count);
1661 gst_buffer_unmap (buf, &map);
1662
1663 if (payloads_count == 0) {
1664 GST_WARNING_OBJECT (asfmux, "Sending packet without any payload");
1665 }
1666 asfmux->data_object_size += size;
1667
1668 if (!has_keyframe)
1669 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
1670
1671 return gst_asf_mux_send_packet (asfmux, buf, size);
1672 }
1673
1674 /**
1675 * stream_number_compare:
1676 * @a: a #GstAsfPad
1677 * @b: another #GstAsfPad
1678 *
1679 * Utility function to compare #GstAsfPad by their stream numbers
1680 *
1681 * Returns: The difference between their stream numbers
1682 */
1683 static gint
stream_number_compare(gconstpointer a,gconstpointer b)1684 stream_number_compare (gconstpointer a, gconstpointer b)
1685 {
1686 GstAsfPad *pad_a = (GstAsfPad *) a;
1687 GstAsfPad *pad_b = (GstAsfPad *) b;
1688 return pad_b->stream_number - pad_a->stream_number;
1689 }
1690
1691 static GstFlowReturn
gst_asf_mux_push_simple_index(GstAsfMux * asfmux,GstAsfVideoPad * pad)1692 gst_asf_mux_push_simple_index (GstAsfMux * asfmux, GstAsfVideoPad * pad)
1693 {
1694 guint64 object_size = ASF_SIMPLE_INDEX_OBJECT_SIZE +
1695 g_slist_length (pad->simple_index) * ASF_SIMPLE_INDEX_ENTRY_SIZE;
1696 GstBuffer *buf;
1697 GSList *walk;
1698 guint8 *data;
1699 guint32 entries_count = g_slist_length (pad->simple_index);
1700 GstMapInfo map;
1701 gsize bufsize;
1702
1703 buf = gst_buffer_new_and_alloc (object_size);
1704 bufsize = object_size;
1705
1706 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1707 data = map.data;
1708
1709 gst_asf_put_guid (data, guids[ASF_SIMPLE_INDEX_OBJECT_INDEX]);
1710 GST_WRITE_UINT64_LE (data + 16, object_size);
1711 gst_asf_put_guid (data + 24, asfmux->file_id);
1712 GST_WRITE_UINT64_LE (data + 40, pad->time_interval);
1713 GST_WRITE_UINT32_LE (data + 48, pad->max_keyframe_packet_count);
1714 GST_WRITE_UINT32_LE (data + 52, entries_count);
1715 data += ASF_SIMPLE_INDEX_OBJECT_SIZE;
1716
1717 GST_DEBUG_OBJECT (asfmux,
1718 "Simple index object values - size:%" G_GUINT64_FORMAT ", time interval:%"
1719 G_GUINT64_FORMAT ", max packet count:%" G_GUINT32_FORMAT ", entries:%"
1720 G_GUINT32_FORMAT, object_size, pad->time_interval,
1721 pad->max_keyframe_packet_count, entries_count);
1722
1723 for (walk = pad->simple_index; walk; walk = g_slist_next (walk)) {
1724 SimpleIndexEntry *entry = (SimpleIndexEntry *) walk->data;
1725 GST_DEBUG_OBJECT (asfmux, "Simple index entry: packet_number:%"
1726 G_GUINT32_FORMAT " packet_count:%" G_GUINT16_FORMAT,
1727 entry->packet_number, entry->packet_count);
1728 GST_WRITE_UINT32_LE (data, entry->packet_number);
1729 GST_WRITE_UINT16_LE (data + 4, entry->packet_count);
1730 data += ASF_SIMPLE_INDEX_ENTRY_SIZE;
1731 }
1732
1733 GST_DEBUG_OBJECT (asfmux, "Pushing the simple index");
1734 g_assert (data - map.data == object_size);
1735 gst_buffer_unmap (buf, &map);
1736 return gst_asf_mux_push_buffer (asfmux, buf, bufsize);
1737 }
1738
1739 static GstFlowReturn
gst_asf_mux_write_indexes(GstAsfMux * asfmux)1740 gst_asf_mux_write_indexes (GstAsfMux * asfmux)
1741 {
1742 GSList *ordered_pads;
1743 GSList *walker;
1744 GstFlowReturn ret = GST_FLOW_OK;
1745
1746 /* write simple indexes for video medias */
1747 ordered_pads =
1748 g_slist_sort (g_slist_copy (asfmux->collect->data),
1749 (GCompareFunc) stream_number_compare);
1750 for (walker = ordered_pads; walker; walker = g_slist_next (walker)) {
1751 GstAsfPad *pad = (GstAsfPad *) walker->data;
1752 if (!pad->is_audio) {
1753 ret = gst_asf_mux_push_simple_index (asfmux, (GstAsfVideoPad *) pad);
1754 if (ret != GST_FLOW_OK) {
1755 GST_ERROR_OBJECT (asfmux, "Failed to write simple index for stream %"
1756 G_GUINT16_FORMAT, (guint16) pad->stream_number);
1757 goto cleanup_and_return;
1758 }
1759 }
1760 }
1761 cleanup_and_return:
1762 g_slist_free (ordered_pads);
1763 return ret;
1764 }
1765
1766 /**
1767 * gst_asf_mux_stop_file:
1768 * @asfmux: #GstAsfMux
1769 *
1770 * Finalizes the asf stream by pushing the indexes after
1771 * the data object. Also seeks back to the header positions
1772 * to rewrite some fields such as the total number of bytes
1773 * of the file, or any other that couldn't be predicted/known
1774 * back on the header generation.
1775 *
1776 * Returns: GST_FLOW_OK on success
1777 */
1778 static GstFlowReturn
gst_asf_mux_stop_file(GstAsfMux * asfmux)1779 gst_asf_mux_stop_file (GstAsfMux * asfmux)
1780 {
1781 GstEvent *event;
1782 GstBuffer *buf;
1783 GstFlowReturn ret = GST_FLOW_OK;
1784 GSList *walk;
1785 GstClockTime play_duration = 0;
1786 guint32 bitrate = 0;
1787 GstSegment segment;
1788 GstMapInfo map;
1789 guint8 *data;
1790
1791 /* write indexes */
1792 ret = gst_asf_mux_write_indexes (asfmux);
1793 if (ret != GST_FLOW_OK) {
1794 GST_ERROR_OBJECT (asfmux, "Failed to write indexes");
1795 return ret;
1796 }
1797
1798 /* find max stream duration and bitrate */
1799 for (walk = asfmux->collect->data; walk; walk = g_slist_next (walk)) {
1800 GstAsfPad *pad = (GstAsfPad *) walk->data;
1801 bitrate += pad->bitrate;
1802 if (pad->play_duration > play_duration)
1803 play_duration = pad->play_duration;
1804 }
1805
1806 /* going back to file properties object to fill in
1807 * values we didn't know back then */
1808 GST_DEBUG_OBJECT (asfmux,
1809 "Sending new segment to file properties object position");
1810 gst_segment_init (&segment, GST_FORMAT_BYTES);
1811 segment.start = segment.position =
1812 asfmux->file_properties_object_position + 40;
1813 event = gst_event_new_segment (&segment);
1814 if (!gst_pad_push_event (asfmux->srcpad, event)) {
1815 GST_ERROR_OBJECT (asfmux, "Failed to update file properties object");
1816 return GST_FLOW_ERROR;
1817 }
1818 /* All file properties fields except the first 40 bytes */
1819 buf = gst_buffer_new_and_alloc (ASF_FILE_PROPERTIES_OBJECT_SIZE - 40);
1820 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1821 data = map.data;
1822
1823 GST_WRITE_UINT64_LE (data, asfmux->file_size);
1824 gst_asf_put_time (data + 8, gst_asf_get_current_time ());
1825 GST_WRITE_UINT64_LE (data + 16, asfmux->total_data_packets);
1826 GST_WRITE_UINT64_LE (data + 24, (play_duration / 100) +
1827 ASF_MILI_TO_100NANO (asfmux->preroll));
1828 GST_WRITE_UINT64_LE (data + 32, (play_duration / 100)); /* TODO send duration */
1829
1830 /* if play duration is smaller then preroll, player might have problems */
1831 if (asfmux->preroll > play_duration / GST_MSECOND) {
1832 GST_ELEMENT_WARNING (asfmux, STREAM, MUX, (_("Generated file has a larger"
1833 " preroll time than its streams duration")),
1834 ("Preroll time larger than streams duration, "
1835 "try setting a smaller preroll value next time"));
1836 }
1837 GST_WRITE_UINT64_LE (data + 40, asfmux->preroll);
1838 GST_WRITE_UINT32_LE (data + 48, 0x2); /* flags - seekable */
1839 GST_WRITE_UINT32_LE (data + 52, asfmux->packet_size);
1840 GST_WRITE_UINT32_LE (data + 56, asfmux->packet_size);
1841 /* FIXME - we want the max instantaneous bitrate, for vbr streams, we can't
1842 * get it this way, this would be the average, right? */
1843 GST_WRITE_UINT32_LE (data + 60, bitrate); /* max bitrate */
1844 gst_buffer_unmap (buf, &map);
1845
1846 /* we don't use gst_asf_mux_push_buffer because we are overwriting
1847 * already sent data */
1848 ret = gst_pad_push (asfmux->srcpad, buf);
1849 if (ret != GST_FLOW_OK) {
1850 GST_ERROR_OBJECT (asfmux, "Failed to update file properties object");
1851 return ret;
1852 }
1853
1854 GST_DEBUG_OBJECT (asfmux, "Seeking back to data object");
1855
1856 /* seek back to the data object */
1857 segment.start = segment.position = asfmux->data_object_position + 16;
1858 event = gst_event_new_segment (&segment);
1859 if (!gst_pad_push_event (asfmux->srcpad, event)) {
1860 GST_ERROR_OBJECT (asfmux, "Seek to update data object failed");
1861 return GST_FLOW_ERROR;
1862 }
1863
1864 buf = gst_buffer_new_and_alloc (32); /* qword+guid+qword */
1865 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1866 data = map.data;
1867 GST_WRITE_UINT64_LE (data, asfmux->data_object_size + ASF_DATA_OBJECT_SIZE);
1868 gst_asf_put_guid (data + 8, asfmux->file_id);
1869 GST_WRITE_UINT64_LE (data + 24, asfmux->total_data_packets);
1870 gst_buffer_unmap (buf, &map);
1871
1872 return gst_pad_push (asfmux->srcpad, buf);
1873 }
1874
1875 /**
1876 * gst_asf_mux_process_buffer:
1877 * @asfmux:
1878 * @pad: stream of the buffer
1879 * @buf: The buffer to be processed
1880 *
1881 * Processes the buffer by parsing it and
1882 * queueing it up as an asf payload for later
1883 * being added and pushed inside an asf packet.
1884 *
1885 * Returns: a #GstFlowReturn
1886 */
1887 static GstFlowReturn
gst_asf_mux_process_buffer(GstAsfMux * asfmux,GstAsfPad * pad,GstBuffer * buf)1888 gst_asf_mux_process_buffer (GstAsfMux * asfmux, GstAsfPad * pad,
1889 GstBuffer * buf)
1890 {
1891 guint8 keyframe;
1892 AsfPayload *payload;
1893
1894 payload = g_malloc0 (sizeof (AsfPayload));
1895 payload->pad = (GstCollectData *) pad;
1896 payload->data = buf;
1897
1898 GST_LOG_OBJECT (asfmux, "Processing payload data for stream number %u",
1899 pad->stream_number);
1900
1901 /* stream number */
1902 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
1903 keyframe = 0;
1904 } else {
1905 keyframe = 0x1 << 7;
1906 }
1907 payload->stream_number = keyframe | pad->stream_number;
1908
1909 payload->media_obj_num = pad->media_object_number;
1910 payload->offset_in_media_obj = 0;
1911 payload->replicated_data_length = 8;
1912
1913 /* replicated data - 1) media object size */
1914 payload->media_object_size = gst_buffer_get_size (buf);
1915 /* replicated data - 2) presentation time */
1916 if (!GST_CLOCK_TIME_IS_VALID (GST_BUFFER_TIMESTAMP (buf))) {
1917 GST_ERROR_OBJECT (asfmux, "Received buffer without timestamp");
1918 gst_asf_payload_free (payload);
1919 return GST_FLOW_ERROR;
1920 }
1921
1922 g_assert (GST_CLOCK_TIME_IS_VALID (asfmux->first_ts));
1923 g_assert (GST_CLOCK_TIME_IS_VALID (pad->first_ts));
1924
1925 payload->presentation_time = asfmux->preroll +
1926 ((GST_BUFFER_TIMESTAMP (buf) - asfmux->first_ts) / GST_MSECOND);
1927
1928 /* update counting values */
1929 pad->media_object_number = (pad->media_object_number + 1) % 256;
1930 if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
1931 pad->play_duration += GST_BUFFER_DURATION (buf);
1932 } else {
1933 GST_WARNING_OBJECT (asfmux, "Received buffer without duration, it will not "
1934 "be accounted in the total file time");
1935 }
1936
1937 asfmux->payloads = g_slist_append (asfmux->payloads, payload);
1938 asfmux->payload_data_size +=
1939 gst_buffer_get_size (buf) + ASF_MULTIPLE_PAYLOAD_HEADER_SIZE;
1940 GST_LOG_OBJECT (asfmux, "Payload data size: %" G_GUINT32_FORMAT,
1941 asfmux->payload_data_size);
1942
1943 while (asfmux->payload_data_size + asfmux->payload_parsing_info_size >=
1944 asfmux->packet_size) {
1945 GstFlowReturn ret = gst_asf_mux_flush_payloads (asfmux);
1946 if (ret != GST_FLOW_OK)
1947 return ret;
1948 }
1949
1950 return GST_FLOW_OK;
1951 }
1952
1953 static GstFlowReturn
gst_asf_mux_collected(GstCollectPads * collect,gpointer data)1954 gst_asf_mux_collected (GstCollectPads * collect, gpointer data)
1955 {
1956 GstAsfMux *asfmux = GST_ASF_MUX_CAST (data);
1957 GstFlowReturn ret = GST_FLOW_OK;
1958 GstAsfPad *best_pad = NULL;
1959 GstClockTime best_time = GST_CLOCK_TIME_NONE;
1960 GstBuffer *buf = NULL;
1961 GSList *walk;
1962
1963 if (G_UNLIKELY (asfmux->state == GST_ASF_MUX_STATE_NONE)) {
1964 ret = gst_asf_mux_start_file (asfmux);
1965 if (ret != GST_FLOW_OK) {
1966 GST_WARNING_OBJECT (asfmux, "Failed to send headers");
1967 return ret;
1968 } else {
1969 asfmux->state = GST_ASF_MUX_STATE_DATA;
1970 }
1971 }
1972
1973 if (G_UNLIKELY (asfmux->state == GST_ASF_MUX_STATE_EOS))
1974 return GST_FLOW_EOS;
1975
1976 /* select the earliest buffer */
1977 walk = asfmux->collect->data;
1978 while (walk) {
1979 GstAsfPad *pad;
1980 GstCollectData *data;
1981 GstClockTime time;
1982
1983 data = (GstCollectData *) walk->data;
1984 pad = (GstAsfPad *) data;
1985
1986 walk = g_slist_next (walk);
1987
1988 buf = gst_collect_pads_peek (collect, data);
1989 if (buf == NULL) {
1990 GST_LOG_OBJECT (asfmux, "Pad %s has no buffers",
1991 GST_PAD_NAME (pad->collect.pad));
1992 continue;
1993 }
1994 time = GST_BUFFER_TIMESTAMP (buf);
1995
1996 /* check the ts for getting the first time */
1997 if (!GST_CLOCK_TIME_IS_VALID (pad->first_ts) &&
1998 GST_CLOCK_TIME_IS_VALID (time)) {
1999 GST_DEBUG_OBJECT (asfmux,
2000 "First ts for stream number %u: %" GST_TIME_FORMAT,
2001 pad->stream_number, GST_TIME_ARGS (time));
2002 pad->first_ts = time;
2003 if (!GST_CLOCK_TIME_IS_VALID (asfmux->first_ts) ||
2004 time < asfmux->first_ts) {
2005 GST_DEBUG_OBJECT (asfmux, "New first ts for file %" GST_TIME_FORMAT,
2006 GST_TIME_ARGS (time));
2007 asfmux->first_ts = time;
2008 }
2009 }
2010
2011 gst_buffer_unref (buf);
2012
2013 if (best_pad == NULL || !GST_CLOCK_TIME_IS_VALID (time) ||
2014 (GST_CLOCK_TIME_IS_VALID (best_time) && time < best_time)) {
2015 best_pad = pad;
2016 best_time = time;
2017 }
2018 }
2019
2020 if (best_pad != NULL) {
2021 /* we have data */
2022 GST_LOG_OBJECT (asfmux, "selected pad %s with time %" GST_TIME_FORMAT,
2023 GST_PAD_NAME (best_pad->collect.pad), GST_TIME_ARGS (best_time));
2024 buf = gst_collect_pads_pop (collect, &best_pad->collect);
2025 ret = gst_asf_mux_process_buffer (asfmux, best_pad, buf);
2026 } else {
2027 /* no data, let's finish it up */
2028 while (asfmux->payloads) {
2029 ret = gst_asf_mux_flush_payloads (asfmux);
2030 if (ret != GST_FLOW_OK) {
2031 return ret;
2032 }
2033 }
2034 g_assert (asfmux->payloads == NULL);
2035 g_assert (asfmux->payload_data_size == 0);
2036 /* in not on 'streamable' mode we need to push indexes
2037 * and update headers */
2038 if (!asfmux->prop_streamable) {
2039 ret = gst_asf_mux_stop_file (asfmux);
2040 }
2041 if (ret == GST_FLOW_OK) {
2042 gst_pad_push_event (asfmux->srcpad, gst_event_new_eos ());
2043 ret = GST_FLOW_EOS;
2044 }
2045 asfmux->state = GST_ASF_MUX_STATE_EOS;
2046 }
2047
2048 return ret;
2049 }
2050
2051 static void
gst_asf_mux_pad_reset(GstAsfPad * pad)2052 gst_asf_mux_pad_reset (GstAsfPad * pad)
2053 {
2054 pad->stream_number = 0;
2055 pad->media_object_number = 0;
2056 pad->play_duration = (GstClockTime) 0;
2057 pad->bitrate = 0;
2058 if (pad->codec_data)
2059 gst_buffer_unref (pad->codec_data);
2060 pad->codec_data = NULL;
2061 if (pad->taglist)
2062 gst_tag_list_unref (pad->taglist);
2063 pad->taglist = NULL;
2064
2065 pad->first_ts = GST_CLOCK_TIME_NONE;
2066
2067 if (pad->is_audio) {
2068 GstAsfAudioPad *audiopad = (GstAsfAudioPad *) pad;
2069 audiopad->audioinfo.rate = 0;
2070 audiopad->audioinfo.channels = 0;
2071 audiopad->audioinfo.format = 0;
2072 audiopad->audioinfo.av_bps = 0;
2073 audiopad->audioinfo.blockalign = 0;
2074 audiopad->audioinfo.bits_per_sample = 0;
2075 } else {
2076 GstAsfVideoPad *videopad = (GstAsfVideoPad *) pad;
2077 videopad->vidinfo.size = 0;
2078 videopad->vidinfo.width = 0;
2079 videopad->vidinfo.height = 0;
2080 videopad->vidinfo.planes = 1;
2081 videopad->vidinfo.bit_cnt = 0;
2082 videopad->vidinfo.compression = 0;
2083 videopad->vidinfo.image_size = 0;
2084 videopad->vidinfo.xpels_meter = 0;
2085 videopad->vidinfo.ypels_meter = 0;
2086 videopad->vidinfo.num_colors = 0;
2087 videopad->vidinfo.imp_colors = 0;
2088
2089 videopad->last_keyframe_packet = 0;
2090 videopad->has_keyframe = FALSE;
2091 videopad->last_keyframe_packet_count = 0;
2092 videopad->max_keyframe_packet_count = 0;
2093 videopad->next_index_time = 0;
2094 videopad->time_interval = DEFAULT_SIMPLE_INDEX_TIME_INTERVAL;
2095 if (videopad->simple_index) {
2096 GSList *walk;
2097 for (walk = videopad->simple_index; walk; walk = g_slist_next (walk)) {
2098 g_free (walk->data);
2099 walk->data = NULL;
2100 }
2101 g_slist_free (videopad->simple_index);
2102 }
2103 videopad->simple_index = NULL;
2104 }
2105 }
2106
2107 static gboolean
gst_asf_mux_audio_set_caps(GstPad * pad,GstCaps * caps)2108 gst_asf_mux_audio_set_caps (GstPad * pad, GstCaps * caps)
2109 {
2110 GstAsfMux *asfmux;
2111 GstAsfAudioPad *audiopad;
2112 GstStructure *structure;
2113 const gchar *caps_name;
2114 gint channels, rate;
2115 gchar *aux;
2116 const GValue *codec_data;
2117
2118 asfmux = GST_ASF_MUX (gst_pad_get_parent (pad));
2119
2120 audiopad = (GstAsfAudioPad *) gst_pad_get_element_private (pad);
2121 g_assert (audiopad);
2122
2123 aux = gst_caps_to_string (caps);
2124 GST_DEBUG_OBJECT (asfmux, "%s:%s, caps=%s", GST_DEBUG_PAD_NAME (pad), aux);
2125 g_free (aux);
2126
2127 structure = gst_caps_get_structure (caps, 0);
2128 caps_name = gst_structure_get_name (structure);
2129
2130 if (!gst_structure_get_int (structure, "channels", &channels) ||
2131 !gst_structure_get_int (structure, "rate", &rate))
2132 goto refuse_caps;
2133
2134 audiopad->audioinfo.channels = (guint16) channels;
2135 audiopad->audioinfo.rate = (guint32) rate;
2136
2137 /* taken from avimux
2138 * codec initialization data, if any
2139 */
2140 codec_data = gst_structure_get_value (structure, "codec_data");
2141 if (codec_data) {
2142 audiopad->pad.codec_data = gst_value_get_buffer (codec_data);
2143 gst_buffer_ref (audiopad->pad.codec_data);
2144 }
2145
2146 if (strcmp (caps_name, "audio/x-wma") == 0) {
2147 gint version;
2148 gint block_align = 0;
2149 gint bitrate = 0;
2150
2151 if (!gst_structure_get_int (structure, "wmaversion", &version)) {
2152 goto refuse_caps;
2153 }
2154
2155 if (gst_structure_get_int (structure, "block_align", &block_align)) {
2156 audiopad->audioinfo.blockalign = (guint16) block_align;
2157 }
2158 if (gst_structure_get_int (structure, "bitrate", &bitrate)) {
2159 audiopad->pad.bitrate = (guint32) bitrate;
2160 audiopad->audioinfo.av_bps = bitrate / 8;
2161 }
2162
2163 if (version == 1) {
2164 audiopad->audioinfo.format = GST_RIFF_WAVE_FORMAT_WMAV1;
2165 } else if (version == 2) {
2166 audiopad->audioinfo.format = GST_RIFF_WAVE_FORMAT_WMAV2;
2167 } else if (version == 3) {
2168 audiopad->audioinfo.format = GST_RIFF_WAVE_FORMAT_WMAV3;
2169 } else {
2170 goto refuse_caps;
2171 }
2172 } else if (strcmp (caps_name, "audio/mpeg") == 0) {
2173 gint version;
2174 gint layer;
2175
2176 if (!gst_structure_get_int (structure, "mpegversion", &version) ||
2177 !gst_structure_get_int (structure, "layer", &layer)) {
2178 goto refuse_caps;
2179 }
2180 if (version != 1 || layer != 3) {
2181 goto refuse_caps;
2182 }
2183
2184 audiopad->audioinfo.format = GST_RIFF_WAVE_FORMAT_MPEGL3;
2185 } else {
2186 goto refuse_caps;
2187 }
2188
2189 gst_object_unref (asfmux);
2190 return TRUE;
2191
2192 refuse_caps:
2193 GST_WARNING_OBJECT (asfmux, "pad %s refused caps %" GST_PTR_FORMAT,
2194 GST_PAD_NAME (pad), caps);
2195 gst_object_unref (asfmux);
2196 return FALSE;
2197 }
2198
2199 /* TODO Read pixel aspect ratio */
2200 static gboolean
gst_asf_mux_video_set_caps(GstPad * pad,GstCaps * caps)2201 gst_asf_mux_video_set_caps (GstPad * pad, GstCaps * caps)
2202 {
2203 GstAsfMux *asfmux;
2204 GstAsfVideoPad *videopad;
2205 GstStructure *structure;
2206 const gchar *caps_name;
2207 gint width, height;
2208 gchar *aux;
2209 const GValue *codec_data;
2210
2211 asfmux = GST_ASF_MUX (gst_pad_get_parent (pad));
2212
2213 videopad = (GstAsfVideoPad *) gst_pad_get_element_private (pad);
2214 g_assert (videopad);
2215
2216 aux = gst_caps_to_string (caps);
2217 GST_DEBUG_OBJECT (asfmux, "%s:%s, caps=%s", GST_DEBUG_PAD_NAME (pad), aux);
2218 g_free (aux);
2219
2220 structure = gst_caps_get_structure (caps, 0);
2221 caps_name = gst_structure_get_name (structure);
2222
2223 if (!gst_structure_get_int (structure, "width", &width) ||
2224 !gst_structure_get_int (structure, "height", &height))
2225 goto refuse_caps;
2226
2227 videopad->vidinfo.width = (gint32) width;
2228 videopad->vidinfo.height = (gint32) height;
2229
2230 /* taken from avimux
2231 * codec initialization data, if any
2232 */
2233 codec_data = gst_structure_get_value (structure, "codec_data");
2234 if (codec_data) {
2235 videopad->pad.codec_data = gst_value_get_buffer (codec_data);
2236 gst_buffer_ref (videopad->pad.codec_data);
2237 }
2238
2239 if (strcmp (caps_name, "video/x-wmv") == 0) {
2240 gint wmvversion;
2241 const gchar *fstr;
2242
2243 videopad->vidinfo.bit_cnt = 24;
2244
2245 /* in case we have a format, we use it */
2246 fstr = gst_structure_get_string (structure, "format");
2247 if (fstr && strlen (fstr) == 4) {
2248 videopad->vidinfo.compression = GST_STR_FOURCC (fstr);
2249 } else if (gst_structure_get_int (structure, "wmvversion", &wmvversion)) {
2250 if (wmvversion == 2) {
2251 videopad->vidinfo.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '2');
2252 } else if (wmvversion == 1) {
2253 videopad->vidinfo.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '1');
2254 } else if (wmvversion == 3) {
2255 videopad->vidinfo.compression = GST_MAKE_FOURCC ('W', 'M', 'V', '3');
2256 }
2257 } else
2258 goto refuse_caps;
2259 } else {
2260 goto refuse_caps;
2261 }
2262
2263 gst_object_unref (asfmux);
2264 return TRUE;
2265
2266 refuse_caps:
2267 GST_WARNING_OBJECT (asfmux, "pad %s refused caps %" GST_PTR_FORMAT,
2268 GST_PAD_NAME (pad), caps);
2269 gst_object_unref (asfmux);
2270 return FALSE;
2271 }
2272
2273 static GstPad *
gst_asf_mux_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * req_name,const GstCaps * caps)2274 gst_asf_mux_request_new_pad (GstElement * element,
2275 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
2276 {
2277 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
2278 GstAsfMux *asfmux = GST_ASF_MUX_CAST (element);
2279 GstPad *newpad;
2280 GstAsfPad *collect_pad;
2281 gboolean is_audio;
2282 guint collect_size = 0;
2283 gchar *name = NULL;
2284 const gchar *pad_name = NULL;
2285 gint pad_id;
2286
2287 GST_DEBUG_OBJECT (asfmux, "Requested pad: %s", GST_STR_NULL (req_name));
2288
2289 if (asfmux->state != GST_ASF_MUX_STATE_NONE) {
2290 GST_WARNING_OBJECT (asfmux, "Not providing request pad after element is at "
2291 "paused/playing state.");
2292 return NULL;
2293 }
2294
2295 if (templ == gst_element_class_get_pad_template (klass, "audio_%u")) {
2296 /* don't mix named and unnamed pads, if the pad already exists we fail when
2297 * trying to add it */
2298 if (req_name != NULL && sscanf (req_name, "audio_%u", &pad_id) == 1) {
2299 pad_name = req_name;
2300 } else {
2301 name = g_strdup_printf ("audio_%u", asfmux->stream_number + 1);
2302 pad_name = name;
2303 }
2304 GST_DEBUG_OBJECT (asfmux, "Adding new pad %s", name);
2305 newpad = gst_pad_new_from_template (templ, pad_name);
2306 is_audio = TRUE;
2307 } else if (templ == gst_element_class_get_pad_template (klass, "video_%u")) {
2308 /* don't mix named and unnamed pads, if the pad already exists we fail when
2309 * trying to add it */
2310 if (req_name != NULL && sscanf (req_name, "video_%u", &pad_id) == 1) {
2311 pad_name = req_name;
2312 } else {
2313 name = g_strdup_printf ("video_%u", asfmux->stream_number + 1);
2314 pad_name = name;
2315 }
2316 GST_DEBUG_OBJECT (asfmux, "Adding new pad %s", name);
2317 newpad = gst_pad_new_from_template (templ, name);
2318 is_audio = FALSE;
2319 } else {
2320 GST_WARNING_OBJECT (asfmux, "This is not our template!");
2321 return NULL;
2322 }
2323
2324 g_free (name);
2325
2326 /* add pad to collections */
2327 if (is_audio) {
2328 collect_size = sizeof (GstAsfAudioPad);
2329 } else {
2330 collect_size = sizeof (GstAsfVideoPad);
2331 }
2332 collect_pad = (GstAsfPad *)
2333 gst_collect_pads_add_pad (asfmux->collect, newpad, collect_size,
2334 (GstCollectDataDestroyNotify) (gst_asf_mux_pad_reset), TRUE);
2335
2336 /* set up pad */
2337 collect_pad->is_audio = is_audio;
2338 if (!is_audio)
2339 ((GstAsfVideoPad *) collect_pad)->simple_index = NULL;
2340 collect_pad->taglist = NULL;
2341 gst_asf_mux_pad_reset (collect_pad);
2342
2343 /* set pad stream number */
2344 asfmux->stream_number += 1;
2345 collect_pad->stream_number = asfmux->stream_number;
2346
2347 gst_pad_set_active (newpad, TRUE);
2348 gst_element_add_pad (element, newpad);
2349
2350 return newpad;
2351 }
2352
2353 static void
gst_asf_mux_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)2354 gst_asf_mux_get_property (GObject * object,
2355 guint prop_id, GValue * value, GParamSpec * pspec)
2356 {
2357 GstAsfMux *asfmux;
2358
2359 asfmux = GST_ASF_MUX (object);
2360 switch (prop_id) {
2361 case PROP_PACKET_SIZE:
2362 g_value_set_uint (value, asfmux->prop_packet_size);
2363 break;
2364 case PROP_PREROLL:
2365 g_value_set_uint64 (value, asfmux->prop_preroll);
2366 break;
2367 case PROP_MERGE_STREAM_TAGS:
2368 g_value_set_boolean (value, asfmux->prop_merge_stream_tags);
2369 break;
2370 case PROP_PADDING:
2371 g_value_set_uint64 (value, asfmux->prop_padding);
2372 break;
2373 case PROP_STREAMABLE:
2374 g_value_set_boolean (value, asfmux->prop_streamable);
2375 break;
2376 default:
2377 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2378 break;
2379 }
2380 }
2381
2382 static void
gst_asf_mux_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)2383 gst_asf_mux_set_property (GObject * object,
2384 guint prop_id, const GValue * value, GParamSpec * pspec)
2385 {
2386 GstAsfMux *asfmux;
2387
2388 asfmux = GST_ASF_MUX (object);
2389 switch (prop_id) {
2390 case PROP_PACKET_SIZE:
2391 asfmux->prop_packet_size = g_value_get_uint (value);
2392 break;
2393 case PROP_PREROLL:
2394 asfmux->prop_preroll = g_value_get_uint64 (value);
2395 break;
2396 case PROP_MERGE_STREAM_TAGS:
2397 asfmux->prop_merge_stream_tags = g_value_get_boolean (value);
2398 break;
2399 case PROP_PADDING:
2400 asfmux->prop_padding = g_value_get_uint64 (value);
2401 break;
2402 case PROP_STREAMABLE:
2403 asfmux->prop_streamable = g_value_get_boolean (value);
2404 break;
2405 default:
2406 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2407 break;
2408 }
2409 }
2410
2411 static GstStateChangeReturn
gst_asf_mux_change_state(GstElement * element,GstStateChange transition)2412 gst_asf_mux_change_state (GstElement * element, GstStateChange transition)
2413 {
2414 GstAsfMux *asfmux;
2415 GstStateChangeReturn ret;
2416
2417 asfmux = GST_ASF_MUX (element);
2418
2419 switch (transition) {
2420 case GST_STATE_CHANGE_READY_TO_PAUSED:
2421 /* TODO - check if it is possible to mux 2 files without going
2422 * through here */
2423 asfmux->payload_parsing_info_size =
2424 gst_asf_mux_find_payload_parsing_info_size (asfmux);
2425 asfmux->packet_size = asfmux->prop_packet_size;
2426 asfmux->preroll = asfmux->prop_preroll;
2427 asfmux->merge_stream_tags = asfmux->prop_merge_stream_tags;
2428 gst_collect_pads_start (asfmux->collect);
2429 break;
2430 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2431 break;
2432 case GST_STATE_CHANGE_PAUSED_TO_READY:
2433 gst_collect_pads_stop (asfmux->collect);
2434 asfmux->state = GST_ASF_MUX_STATE_NONE;
2435 break;
2436 default:
2437 break;
2438 }
2439
2440 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2441 if (ret == GST_STATE_CHANGE_FAILURE)
2442 goto done;
2443
2444 switch (transition) {
2445 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2446 break;
2447 case GST_STATE_CHANGE_PAUSED_TO_READY:
2448 break;
2449 case GST_STATE_CHANGE_READY_TO_NULL:
2450 break;
2451 default:
2452 break;
2453 }
2454
2455 done:
2456 return ret;
2457 }
2458
2459 gboolean
gst_asf_mux_plugin_init(GstPlugin * plugin)2460 gst_asf_mux_plugin_init (GstPlugin * plugin)
2461 {
2462 return gst_element_register (plugin, "asfmux",
2463 GST_RANK_PRIMARY, GST_TYPE_ASF_MUX);
2464 }
2465