1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <libavformat/avformat.h>
27 #include <libavutil/opt.h>
28 #include <gst/gst.h>
29 #include <gst/base/gstcollectpads.h>
30
31 #include "gstav.h"
32 #include "gstavcodecmap.h"
33 #include "gstavutils.h"
34 #include "gstavprotocol.h"
35
36 typedef struct _GstFFMpegMux GstFFMpegMux;
37 typedef struct _GstFFMpegMuxPad GstFFMpegMuxPad;
38
39 struct _GstFFMpegMuxPad
40 {
41 GstCollectData collect; /* we extend the CollectData */
42
43 gint padnum;
44 };
45
46 struct _GstFFMpegMux
47 {
48 GstElement element;
49
50 GstCollectPads *collect;
51 /* We need to keep track of our pads, so we do so here. */
52 GstPad *srcpad;
53
54 AVFormatContext *context;
55 gboolean opened;
56
57 guint videopads, audiopads;
58
59 /*< private > */
60 /* event_function is the collectpads default eventfunction */
61 GstPadEventFunction event_function;
62 int max_delay;
63 int preload;
64 };
65
66 typedef struct _GstFFMpegMuxClass GstFFMpegMuxClass;
67
68 struct _GstFFMpegMuxClass
69 {
70 GstElementClass parent_class;
71
72 AVOutputFormat *in_plugin;
73 };
74
75 #define GST_TYPE_FFMPEGMUX \
76 (gst_ffmpegdec_get_type())
77 #define GST_FFMPEGMUX(obj) \
78 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGMUX,GstFFMpegMux))
79 #define GST_FFMPEGMUX_CLASS(klass) \
80 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGMUX,GstFFMpegMuxClass))
81 #define GST_IS_FFMPEGMUX(obj) \
82 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGMUX))
83 #define GST_IS_FFMPEGMUX_CLASS(klass) \
84 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGMUX))
85
86 enum
87 {
88 /* FILL ME */
89 LAST_SIGNAL
90 };
91
92 enum
93 {
94 PROP_0,
95 PROP_PRELOAD,
96 PROP_MAXDELAY
97 };
98
99 /* A number of function prototypes are given so we can refer to them later. */
100 static void gst_ffmpegmux_class_init (GstFFMpegMuxClass * klass);
101 static void gst_ffmpegmux_base_init (gpointer g_class);
102 static void gst_ffmpegmux_init (GstFFMpegMux * ffmpegmux,
103 GstFFMpegMuxClass * g_class);
104 static void gst_ffmpegmux_finalize (GObject * object);
105
106 static gboolean gst_ffmpegmux_setcaps (GstPad * pad, GstCaps * caps);
107 static GstPad *gst_ffmpegmux_request_new_pad (GstElement * element,
108 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
109 static GstFlowReturn gst_ffmpegmux_collected (GstCollectPads * pads,
110 gpointer user_data);
111
112 static gboolean gst_ffmpegmux_sink_event (GstPad * pad, GstObject * parent,
113 GstEvent * event);
114
115 static GstStateChangeReturn gst_ffmpegmux_change_state (GstElement * element,
116 GstStateChange transition);
117
118 static void gst_ffmpegmux_set_property (GObject * object, guint prop_id,
119 const GValue * value, GParamSpec * pspec);
120 static void gst_ffmpegmux_get_property (GObject * object, guint prop_id,
121 GValue * value, GParamSpec * pspec);
122
123 static GstCaps *gst_ffmpegmux_get_id_caps (enum AVCodecID *id_list);
124 static void gst_ffmpeg_mux_simple_caps_set_int_list (GstCaps * caps,
125 const gchar * field, guint num, const gint * values);
126
127 #define GST_FFMUX_PARAMS_QDATA g_quark_from_static_string("avmux-params")
128
129 static GstElementClass *parent_class = NULL;
130
131 /*static guint gst_ffmpegmux_signals[LAST_SIGNAL] = { 0 }; */
132
133 typedef struct
134 {
135 const char *name;
136 const char *replacement;
137 } GstFFMpegMuxReplacement;
138
139 static const char *
gst_ffmpegmux_get_replacement(const char * name)140 gst_ffmpegmux_get_replacement (const char *name)
141 {
142 static const GstFFMpegMuxReplacement blacklist[] = {
143 {"avi", "avimux"},
144 {"matroska", "matroskamux"},
145 {"mov", "qtmux"},
146 {"mpegts", "mpegtsmux"},
147 {"mp4", "mp4mux"},
148 {"mpjpeg", "multipartmux"},
149 {"ogg", "oggmux"},
150 {"wav", "wavenc"},
151 {"webm", "webmmux"},
152 {"mxf", "mxfmux"},
153 {"3gp", "gppmux"},
154 {"yuv4mpegpipe", "y4menc"},
155 {"aiff", "aiffmux"},
156 {"adts", "aacparse"},
157 {"asf", "asfmux"},
158 {"asf_stream", "asfmux"},
159 {"flv", "flvmux"},
160 {"mp3", "id3v2mux"},
161 {"mp2", "id3v2mux"}
162 };
163 guint i;
164
165 for (i = 0; i < sizeof (blacklist) / sizeof (blacklist[0]); i++) {
166 if (strcmp (blacklist[i].name, name) == 0) {
167 return blacklist[i].replacement;
168 }
169 }
170
171 return NULL;
172 }
173
174 static gboolean
gst_ffmpegmux_is_formatter(const char * name)175 gst_ffmpegmux_is_formatter (const char *name)
176 {
177 static const char *replace[] = {
178 "mp2", "mp3", NULL
179 };
180 int i;
181
182 for (i = 0; replace[i]; i++)
183 if (strcmp (replace[i], name) == 0)
184 return TRUE;
185 return FALSE;
186 }
187
188 static void
gst_ffmpegmux_base_init(gpointer g_class)189 gst_ffmpegmux_base_init (gpointer g_class)
190 {
191 GstFFMpegMuxClass *klass = (GstFFMpegMuxClass *) g_class;
192 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
193 GstPadTemplate *videosinktempl, *audiosinktempl, *srctempl;
194 AVOutputFormat *in_plugin;
195 GstCaps *srccaps, *audiosinkcaps, *videosinkcaps;
196 enum AVCodecID *video_ids = NULL, *audio_ids = NULL;
197 gchar *longname, *description, *name;
198 const char *replacement;
199 gboolean is_formatter;
200
201 in_plugin =
202 (AVOutputFormat *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
203 GST_FFMUX_PARAMS_QDATA);
204 g_assert (in_plugin != NULL);
205
206 name = g_strdup (in_plugin->name);
207 g_strdelimit (name, ".,|-<> ", '_');
208
209 /* construct the element details struct */
210 replacement = gst_ffmpegmux_get_replacement (in_plugin->name);
211 is_formatter = gst_ffmpegmux_is_formatter (in_plugin->name);
212 if (replacement != NULL) {
213 longname =
214 g_strdup_printf ("libav %s %s (not recommended, use %s instead)",
215 in_plugin->long_name, is_formatter ? "formatter" : "muxer",
216 replacement);
217 description =
218 g_strdup_printf ("libav %s %s (not recommended, use %s instead)",
219 in_plugin->long_name, is_formatter ? "formatter" : "muxer",
220 replacement);
221 } else {
222 longname = g_strdup_printf ("libav %s %s", in_plugin->long_name,
223 is_formatter ? "formatter" : "muxer");
224 description = g_strdup_printf ("libav %s %s", in_plugin->long_name,
225 is_formatter ? "formatter" : "muxer");
226 }
227 gst_element_class_set_metadata (element_class, longname,
228 is_formatter ? "Formatter/Metadata" : "Codec/Muxer", description,
229 "Wim Taymans <wim.taymans@chello.be>, "
230 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
231 g_free (longname);
232 g_free (description);
233
234 /* Try to find the caps that belongs here */
235 srccaps = gst_ffmpeg_formatid_to_caps (name);
236 if (!srccaps) {
237 GST_DEBUG ("Couldn't get source caps for muxer '%s', skipping", name);
238 goto beach;
239 }
240
241 if (!gst_ffmpeg_formatid_get_codecids (in_plugin->name,
242 &video_ids, &audio_ids, in_plugin)) {
243 gst_caps_unref (srccaps);
244 GST_DEBUG ("Couldn't get sink caps for muxer '%s'. Most likely because "
245 "no input format mapping exists.", name);
246 goto beach;
247 }
248
249 videosinkcaps = video_ids ? gst_ffmpegmux_get_id_caps (video_ids) : NULL;
250 audiosinkcaps = audio_ids ? gst_ffmpegmux_get_id_caps (audio_ids) : NULL;
251
252 /* fix up allowed caps for some muxers */
253 /* FIXME : This should be in gstffmpegcodecmap.c ! */
254 if (strcmp (in_plugin->name, "flv") == 0) {
255 const gint rates[] = { 44100, 22050, 11025 };
256
257 gst_ffmpeg_mux_simple_caps_set_int_list (audiosinkcaps, "rate", 3, rates);
258 } else if (strcmp (in_plugin->name, "dv") == 0) {
259 gst_caps_set_simple (audiosinkcaps,
260 "rate", G_TYPE_INT, 48000, "channels", G_TYPE_INT, 2, NULL);
261
262 }
263
264 /* pad templates */
265 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
266 gst_element_class_add_pad_template (element_class, srctempl);
267 gst_caps_unref (srccaps);
268
269 if (audiosinkcaps) {
270 audiosinktempl = gst_pad_template_new ("audio_%u",
271 GST_PAD_SINK, GST_PAD_REQUEST, audiosinkcaps);
272 gst_element_class_add_pad_template (element_class, audiosinktempl);
273 gst_caps_unref (audiosinkcaps);
274 }
275
276 if (videosinkcaps) {
277 videosinktempl = gst_pad_template_new ("video_%u",
278 GST_PAD_SINK, GST_PAD_REQUEST, videosinkcaps);
279 gst_element_class_add_pad_template (element_class, videosinktempl);
280 gst_caps_unref (videosinkcaps);
281 }
282
283 beach:
284 klass->in_plugin = in_plugin;
285
286 g_free (name);
287 }
288
289 static void
gst_ffmpegmux_class_init(GstFFMpegMuxClass * klass)290 gst_ffmpegmux_class_init (GstFFMpegMuxClass * klass)
291 {
292 GObjectClass *gobject_class;
293 GstElementClass *gstelement_class;
294
295 gobject_class = (GObjectClass *) klass;
296 gstelement_class = (GstElementClass *) klass;
297
298 parent_class = g_type_class_peek_parent (klass);
299
300 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_ffmpegmux_set_property);
301 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_ffmpegmux_get_property);
302
303 g_object_class_install_property (gobject_class, PROP_PRELOAD,
304 g_param_spec_int ("preload", "preload",
305 "Set the initial demux-decode delay (in microseconds)",
306 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307
308 g_object_class_install_property (gobject_class, PROP_MAXDELAY,
309 g_param_spec_int ("maxdelay", "maxdelay",
310 "Set the maximum demux-decode delay (in microseconds)", 0, G_MAXINT,
311 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312
313 gstelement_class->request_new_pad = gst_ffmpegmux_request_new_pad;
314 gstelement_class->change_state = gst_ffmpegmux_change_state;
315 gobject_class->finalize = gst_ffmpegmux_finalize;
316 }
317
318 static void
gst_ffmpegmux_init(GstFFMpegMux * ffmpegmux,GstFFMpegMuxClass * g_class)319 gst_ffmpegmux_init (GstFFMpegMux * ffmpegmux, GstFFMpegMuxClass * g_class)
320 {
321 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
322 GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass *) klass;
323 GstPadTemplate *templ = gst_element_class_get_pad_template (klass, "src");
324
325 ffmpegmux->srcpad = gst_pad_new_from_template (templ, "src");
326 gst_pad_set_caps (ffmpegmux->srcpad, gst_pad_template_get_caps (templ));
327 gst_element_add_pad (GST_ELEMENT (ffmpegmux), ffmpegmux->srcpad);
328
329 ffmpegmux->collect = gst_collect_pads_new ();
330 gst_collect_pads_set_function (ffmpegmux->collect,
331 (GstCollectPadsFunction) gst_ffmpegmux_collected, ffmpegmux);
332
333 ffmpegmux->context = avformat_alloc_context ();
334 ffmpegmux->context->oformat = oclass->in_plugin;
335 ffmpegmux->context->nb_streams = 0;
336 ffmpegmux->opened = FALSE;
337
338 ffmpegmux->videopads = 0;
339 ffmpegmux->audiopads = 0;
340 ffmpegmux->max_delay = 0;
341 }
342
343 static void
gst_ffmpegmux_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)344 gst_ffmpegmux_set_property (GObject * object, guint prop_id,
345 const GValue * value, GParamSpec * pspec)
346 {
347 GstFFMpegMux *src;
348
349 src = (GstFFMpegMux *) object;
350
351 switch (prop_id) {
352 case PROP_PRELOAD:
353 src->preload = g_value_get_int (value);
354 break;
355 case PROP_MAXDELAY:
356 src->max_delay = g_value_get_int (value);
357 break;
358 default:
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360 break;
361 }
362 }
363
364 static void
gst_ffmpegmux_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)365 gst_ffmpegmux_get_property (GObject * object, guint prop_id, GValue * value,
366 GParamSpec * pspec)
367 {
368 GstFFMpegMux *src;
369
370 src = (GstFFMpegMux *) object;
371
372 switch (prop_id) {
373 case PROP_PRELOAD:
374 g_value_set_int (value, src->preload);
375 break;
376 case PROP_MAXDELAY:
377 g_value_set_int (value, src->max_delay);
378 break;
379 default:
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 break;
382 }
383 }
384
385
386 static void
gst_ffmpegmux_finalize(GObject * object)387 gst_ffmpegmux_finalize (GObject * object)
388 {
389 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) object;
390
391 avformat_free_context (ffmpegmux->context);
392 ffmpegmux->context = NULL;
393
394 gst_object_unref (ffmpegmux->collect);
395
396 if (G_OBJECT_CLASS (parent_class)->finalize)
397 G_OBJECT_CLASS (parent_class)->finalize (object);
398 }
399
400 static GstPad *
gst_ffmpegmux_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)401 gst_ffmpegmux_request_new_pad (GstElement * element,
402 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
403 {
404 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
405 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
406 GstFFMpegMuxPad *collect_pad;
407 gchar *padname;
408 GstPad *pad;
409 AVStream *st;
410 enum AVMediaType type;
411 gint bitrate = 0, framesize = 0;
412
413 g_return_val_if_fail (templ != NULL, NULL);
414 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
415 g_return_val_if_fail (ffmpegmux->opened == FALSE, NULL);
416
417 /* figure out a name that *we* like */
418 if (templ == gst_element_class_get_pad_template (klass, "video_%u")) {
419 padname = g_strdup_printf ("video_%u", ffmpegmux->videopads++);
420 type = AVMEDIA_TYPE_VIDEO;
421 bitrate = 64 * 1024;
422 framesize = 1152;
423 } else if (templ == gst_element_class_get_pad_template (klass, "audio_%u")) {
424 padname = g_strdup_printf ("audio_%u", ffmpegmux->audiopads++);
425 type = AVMEDIA_TYPE_AUDIO;
426 bitrate = 285 * 1024;
427 } else {
428 g_warning ("avmux: unknown pad template!");
429 return NULL;
430 }
431
432 /* create pad */
433 pad = gst_pad_new_from_template (templ, padname);
434 collect_pad = (GstFFMpegMuxPad *)
435 gst_collect_pads_add_pad (ffmpegmux->collect, pad,
436 sizeof (GstFFMpegMuxPad), NULL, TRUE);
437 collect_pad->padnum = ffmpegmux->context->nb_streams;
438
439 /* small hack to put our own event pad function and chain up to collect pad */
440 ffmpegmux->event_function = GST_PAD_EVENTFUNC (pad);
441 gst_pad_set_event_function (pad,
442 GST_DEBUG_FUNCPTR (gst_ffmpegmux_sink_event));
443
444 gst_element_add_pad (element, pad);
445
446 /* AVStream needs to be created */
447 st = avformat_new_stream (ffmpegmux->context, NULL);
448 st->id = collect_pad->padnum;
449 st->codecpar->codec_type = type;
450 st->codecpar->codec_id = AV_CODEC_ID_NONE; /* this is a check afterwards */
451 st->codecpar->bit_rate = bitrate;
452 st->codecpar->frame_size = framesize;
453 /* we fill in codec during capsnego */
454
455 /* we love debug output (c) (tm) (r) */
456 GST_DEBUG ("Created %s pad for avmux_%s element",
457 padname, ((GstFFMpegMuxClass *) klass)->in_plugin->name);
458 g_free (padname);
459
460 return pad;
461 }
462
463 /**
464 * gst_ffmpegmux_setcaps
465 * @pad: #GstPad
466 * @caps: New caps.
467 *
468 * Set caps to pad.
469 *
470 * Returns: #TRUE on success.
471 */
472 static gboolean
gst_ffmpegmux_setcaps(GstPad * pad,GstCaps * caps)473 gst_ffmpegmux_setcaps (GstPad * pad, GstCaps * caps)
474 {
475 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (gst_pad_get_parent (pad));
476 GstFFMpegMuxPad *collect_pad;
477 AVStream *st;
478 AVCodecContext tmp;
479
480 collect_pad = (GstFFMpegMuxPad *) gst_pad_get_element_private (pad);
481
482 st = ffmpegmux->context->streams[collect_pad->padnum];
483 av_opt_set_int (ffmpegmux->context, "preload", ffmpegmux->preload, 0);
484 ffmpegmux->context->max_delay = ffmpegmux->max_delay;
485
486 /* for the format-specific guesses, we'll go to
487 * our famous codec mapper */
488 if (gst_ffmpeg_caps_to_codecid (caps, &tmp) == AV_CODEC_ID_NONE)
489 goto not_accepted;
490
491 avcodec_parameters_from_context (st->codecpar, &tmp);
492
493 /* copy over the aspect ratios, ffmpeg expects the stream aspect to match the
494 * codec aspect. */
495 st->sample_aspect_ratio = st->codecpar->sample_aspect_ratio;
496
497 GST_LOG_OBJECT (pad, "accepted caps %" GST_PTR_FORMAT, caps);
498 return TRUE;
499
500 /* ERRORS */
501 not_accepted:
502 {
503 GST_LOG_OBJECT (pad, "rejecting caps %" GST_PTR_FORMAT, caps);
504 return FALSE;
505 }
506 }
507
508
509 static gboolean
gst_ffmpegmux_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)510 gst_ffmpegmux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
511 {
512 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) parent;
513 gboolean res = TRUE;
514
515 switch (GST_EVENT_TYPE (event)) {
516 case GST_EVENT_TAG:{
517 GstTagList *taglist;
518 GstTagSetter *setter = GST_TAG_SETTER (ffmpegmux);
519 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
520
521 gst_event_parse_tag (event, &taglist);
522 gst_tag_setter_merge_tags (setter, taglist, mode);
523 break;
524 }
525 case GST_EVENT_CAPS:{
526 GstCaps *caps;
527 gst_event_parse_caps (event, &caps);
528 if (!(res = gst_ffmpegmux_setcaps (pad, caps)))
529 goto beach;
530 break;
531 }
532 default:
533 break;
534 }
535
536 /* chaining up to collectpads default event function */
537 res = ffmpegmux->event_function (pad, parent, event);
538
539 beach:
540 return res;
541 }
542
543 static GstFlowReturn
gst_ffmpegmux_collected(GstCollectPads * pads,gpointer user_data)544 gst_ffmpegmux_collected (GstCollectPads * pads, gpointer user_data)
545 {
546 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) user_data;
547 GSList *collected;
548 GstFFMpegMuxPad *best_pad;
549 GstClockTime best_time;
550 #if 0
551 /* Re-enable once converted to new AVMetaData API
552 * See #566605
553 */
554 const GstTagList *tags;
555 #endif
556
557 /* open "file" (gstreamer protocol to next element) */
558 if (!ffmpegmux->opened) {
559 int open_flags = AVIO_FLAG_WRITE;
560
561 /* we do need all streams to have started capsnego,
562 * or things will go horribly wrong */
563 for (collected = ffmpegmux->collect->data; collected;
564 collected = g_slist_next (collected)) {
565 GstFFMpegMuxPad *collect_pad = (GstFFMpegMuxPad *) collected->data;
566 AVStream *st = ffmpegmux->context->streams[collect_pad->padnum];
567
568 /* check whether the pad has successfully completed capsnego */
569 if (st->codecpar->codec_id == AV_CODEC_ID_NONE) {
570 GST_ELEMENT_ERROR (ffmpegmux, CORE, NEGOTIATION, (NULL),
571 ("no caps set on stream %d (%s)", collect_pad->padnum,
572 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ?
573 "video" : "audio"));
574 return GST_FLOW_ERROR;
575 }
576 /* set framerate for audio */
577 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
578 switch (st->codecpar->codec_id) {
579 case AV_CODEC_ID_PCM_S16LE:
580 case AV_CODEC_ID_PCM_S16BE:
581 case AV_CODEC_ID_PCM_U16LE:
582 case AV_CODEC_ID_PCM_U16BE:
583 case AV_CODEC_ID_PCM_S8:
584 case AV_CODEC_ID_PCM_U8:
585 st->codecpar->frame_size = 1;
586 break;
587 default:
588 {
589 GstBuffer *buffer;
590
591 /* FIXME : This doesn't work for RAW AUDIO...
592 * in fact I'm wondering if it even works for any kind of audio... */
593 buffer = gst_collect_pads_peek (ffmpegmux->collect,
594 (GstCollectData *) collect_pad);
595 if (buffer) {
596 st->codecpar->frame_size =
597 st->codecpar->sample_rate *
598 GST_BUFFER_DURATION (buffer) / GST_SECOND;
599 gst_buffer_unref (buffer);
600 }
601 }
602 }
603 }
604 }
605
606 #if 0
607 /* Re-enable once converted to new AVMetaData API
608 * See #566605
609 */
610
611 /* tags */
612 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (ffmpegmux));
613 if (tags) {
614 gint i;
615 gchar *s;
616
617 /* get the interesting ones */
618 if (gst_tag_list_get_string (tags, GST_TAG_TITLE, &s)) {
619 strncpy (ffmpegmux->context->title, s,
620 sizeof (ffmpegmux->context->title));
621 }
622 if (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &s)) {
623 strncpy (ffmpegmux->context->author, s,
624 sizeof (ffmpegmux->context->author));
625 }
626 if (gst_tag_list_get_string (tags, GST_TAG_COPYRIGHT, &s)) {
627 strncpy (ffmpegmux->context->copyright, s,
628 sizeof (ffmpegmux->context->copyright));
629 }
630 if (gst_tag_list_get_string (tags, GST_TAG_COMMENT, &s)) {
631 strncpy (ffmpegmux->context->comment, s,
632 sizeof (ffmpegmux->context->comment));
633 }
634 if (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &s)) {
635 strncpy (ffmpegmux->context->album, s,
636 sizeof (ffmpegmux->context->album));
637 }
638 if (gst_tag_list_get_string (tags, GST_TAG_GENRE, &s)) {
639 strncpy (ffmpegmux->context->genre, s,
640 sizeof (ffmpegmux->context->genre));
641 }
642 if (gst_tag_list_get_int (tags, GST_TAG_TRACK_NUMBER, &i)) {
643 ffmpegmux->context->track = i;
644 }
645 }
646 #endif
647
648 /* set the streamheader flag for gstffmpegprotocol if codec supports it */
649 if (!strcmp (ffmpegmux->context->oformat->name, "flv")) {
650 open_flags |= GST_FFMPEG_URL_STREAMHEADER;
651 }
652
653 /* some house-keeping for downstream before starting data flow */
654 /* stream-start (FIXME: create id based on input ids) */
655 {
656 gchar s_id[32];
657
658 g_snprintf (s_id, sizeof (s_id), "avmux-%08x", g_random_int ());
659 gst_pad_push_event (ffmpegmux->srcpad, gst_event_new_stream_start (s_id));
660 }
661 /* segment */
662 {
663 GstSegment segment;
664
665 /* let downstream know we think in BYTES and expect to do seeking later on */
666 gst_segment_init (&segment, GST_FORMAT_BYTES);
667 gst_pad_push_event (ffmpegmux->srcpad, gst_event_new_segment (&segment));
668 }
669
670 if (gst_ffmpegdata_open (ffmpegmux->srcpad, open_flags,
671 &ffmpegmux->context->pb) < 0) {
672 GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, TOO_LAZY, (NULL),
673 ("Failed to open stream context in avmux"));
674 return GST_FLOW_ERROR;
675 }
676
677 /* now open the mux format */
678 if (avformat_write_header (ffmpegmux->context, NULL) < 0) {
679 GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, SETTINGS, (NULL),
680 ("Failed to write file header - check codec settings"));
681 return GST_FLOW_ERROR;
682 }
683
684 /* we're now opened */
685 ffmpegmux->opened = TRUE;
686
687 /* flush the header so it will be used as streamheader */
688 avio_flush (ffmpegmux->context->pb);
689 }
690
691 /* take the one with earliest timestamp,
692 * and push it forward */
693 best_pad = NULL;
694 best_time = GST_CLOCK_TIME_NONE;
695 for (collected = ffmpegmux->collect->data; collected;
696 collected = g_slist_next (collected)) {
697 GstFFMpegMuxPad *collect_pad = (GstFFMpegMuxPad *) collected->data;
698 GstBuffer *buffer = gst_collect_pads_peek (ffmpegmux->collect,
699 (GstCollectData *) collect_pad);
700
701 /* if there's no buffer, just continue */
702 if (buffer == NULL) {
703 continue;
704 }
705
706 /* if we have no buffer yet, just use the first one */
707 if (best_pad == NULL) {
708 best_pad = collect_pad;
709 best_time = GST_BUFFER_TIMESTAMP (buffer);
710 goto next_pad;
711 }
712
713 /* if we do have one, only use this one if it's older */
714 if (GST_BUFFER_TIMESTAMP (buffer) < best_time) {
715 best_time = GST_BUFFER_TIMESTAMP (buffer);
716 best_pad = collect_pad;
717 }
718
719 next_pad:
720 gst_buffer_unref (buffer);
721
722 /* Mux buffers with invalid timestamp first */
723 if (!GST_CLOCK_TIME_IS_VALID (best_time))
724 break;
725 }
726
727 /* now handle the buffer, or signal EOS if we have
728 * no buffers left */
729 if (best_pad != NULL) {
730 GstBuffer *buf;
731 AVPacket pkt;
732 GstMapInfo map;
733
734 /* push out current buffer */
735 buf =
736 gst_collect_pads_pop (ffmpegmux->collect, (GstCollectData *) best_pad);
737
738 /* set time */
739 pkt.pts = gst_ffmpeg_time_gst_to_ff (GST_BUFFER_TIMESTAMP (buf),
740 ffmpegmux->context->streams[best_pad->padnum]->time_base);
741 pkt.dts = pkt.pts;
742
743 gst_buffer_map (buf, &map, GST_MAP_READ);
744 pkt.data = map.data;
745 pkt.size = map.size;
746
747 pkt.stream_index = best_pad->padnum;
748 pkt.flags = 0;
749
750 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT))
751 pkt.flags |= AV_PKT_FLAG_KEY;
752
753 if (GST_BUFFER_DURATION_IS_VALID (buf))
754 pkt.duration =
755 gst_ffmpeg_time_gst_to_ff (GST_BUFFER_DURATION (buf),
756 ffmpegmux->context->streams[best_pad->padnum]->time_base);
757 else
758 pkt.duration = 0;
759 av_write_frame (ffmpegmux->context, &pkt);
760 gst_buffer_unmap (buf, &map);
761 gst_buffer_unref (buf);
762 } else {
763 /* close down */
764 av_write_trailer (ffmpegmux->context);
765 ffmpegmux->opened = FALSE;
766 avio_flush (ffmpegmux->context->pb);
767 gst_ffmpegdata_close (ffmpegmux->context->pb);
768 gst_pad_push_event (ffmpegmux->srcpad, gst_event_new_eos ());
769 return GST_FLOW_EOS;
770 }
771
772 return GST_FLOW_OK;
773 }
774
775 static GstStateChangeReturn
gst_ffmpegmux_change_state(GstElement * element,GstStateChange transition)776 gst_ffmpegmux_change_state (GstElement * element, GstStateChange transition)
777 {
778 GstStateChangeReturn ret;
779 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (element);
780
781 switch (transition) {
782 case GST_STATE_CHANGE_NULL_TO_READY:
783 break;
784 case GST_STATE_CHANGE_READY_TO_PAUSED:
785 gst_collect_pads_start (ffmpegmux->collect);
786 break;
787 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
788 break;
789 case GST_STATE_CHANGE_PAUSED_TO_READY:
790 gst_collect_pads_stop (ffmpegmux->collect);
791 break;
792 default:
793 break;
794 }
795
796 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
797
798 switch (transition) {
799 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
800 break;
801 case GST_STATE_CHANGE_PAUSED_TO_READY:
802 gst_tag_setter_reset_tags (GST_TAG_SETTER (ffmpegmux));
803 if (ffmpegmux->opened) {
804 ffmpegmux->opened = FALSE;
805 gst_ffmpegdata_close (ffmpegmux->context->pb);
806 }
807 break;
808 case GST_STATE_CHANGE_READY_TO_NULL:
809 break;
810 default:
811 break;
812 }
813
814 return ret;
815 }
816
817 static GstCaps *
gst_ffmpegmux_get_id_caps(enum AVCodecID * id_list)818 gst_ffmpegmux_get_id_caps (enum AVCodecID *id_list)
819 {
820 GstCaps *caps, *t;
821 gint i;
822
823 caps = gst_caps_new_empty ();
824 for (i = 0; id_list[i] != AV_CODEC_ID_NONE; i++) {
825 if ((t = gst_ffmpeg_codecid_to_caps (id_list[i], NULL, TRUE)))
826 gst_caps_append (caps, t);
827 }
828 if (gst_caps_is_empty (caps)) {
829 gst_caps_unref (caps);
830 return NULL;
831 }
832
833 return caps;
834 }
835
836 /* set a list of integer values on the caps, e.g. for sample rates */
837 static void
gst_ffmpeg_mux_simple_caps_set_int_list(GstCaps * caps,const gchar * field,guint num,const gint * values)838 gst_ffmpeg_mux_simple_caps_set_int_list (GstCaps * caps, const gchar * field,
839 guint num, const gint * values)
840 {
841 GValue list = { 0, };
842 GValue val = { 0, };
843 guint i;
844
845 g_return_if_fail (GST_CAPS_IS_SIMPLE (caps));
846
847 g_value_init (&list, GST_TYPE_LIST);
848 g_value_init (&val, G_TYPE_INT);
849
850 for (i = 0; i < num; ++i) {
851 g_value_set_int (&val, values[i]);
852 gst_value_list_append_value (&list, &val);
853 }
854
855 gst_structure_set_value (gst_caps_get_structure (caps, 0), field, &list);
856
857 g_value_unset (&val);
858 g_value_unset (&list);
859 }
860
861 gboolean
gst_ffmpegmux_register(GstPlugin * plugin)862 gst_ffmpegmux_register (GstPlugin * plugin)
863 {
864 GTypeInfo typeinfo = {
865 sizeof (GstFFMpegMuxClass),
866 (GBaseInitFunc) gst_ffmpegmux_base_init,
867 NULL,
868 (GClassInitFunc) gst_ffmpegmux_class_init,
869 NULL,
870 NULL,
871 sizeof (GstFFMpegMux),
872 0,
873 (GInstanceInitFunc) gst_ffmpegmux_init,
874 };
875 static const GInterfaceInfo tag_setter_info = {
876 NULL, NULL, NULL
877 };
878 GType type;
879 const AVOutputFormat *in_plugin;
880 void *i = 0;
881
882 GST_LOG ("Registering muxers");
883
884 while ((in_plugin = av_muxer_iterate (&i))) {
885 gchar *type_name;
886 GstRank rank = GST_RANK_MARGINAL;
887
888 if ((!strncmp (in_plugin->name, "u16", 3)) ||
889 (!strncmp (in_plugin->name, "s16", 3)) ||
890 (!strncmp (in_plugin->name, "u24", 3)) ||
891 (!strncmp (in_plugin->name, "s24", 3)) ||
892 (!strncmp (in_plugin->name, "u8", 2)) ||
893 (!strncmp (in_plugin->name, "s8", 2)) ||
894 (!strncmp (in_plugin->name, "u32", 3)) ||
895 (!strncmp (in_plugin->name, "s32", 3)) ||
896 (!strncmp (in_plugin->name, "f32", 3)) ||
897 (!strncmp (in_plugin->name, "f64", 3)) ||
898 (!strncmp (in_plugin->name, "raw", 3)) ||
899 (!strncmp (in_plugin->name, "crc", 3)) ||
900 (!strncmp (in_plugin->name, "null", 4)) ||
901 (!strncmp (in_plugin->name, "gif", 3)) ||
902 (!strncmp (in_plugin->name, "fifo", 4)) ||
903 (!strncmp (in_plugin->name, "frame", 5)) ||
904 (!strncmp (in_plugin->name, "image", 5)) ||
905 (!strncmp (in_plugin->name, "mulaw", 5)) ||
906 (!strncmp (in_plugin->name, "alaw", 4)) ||
907 (!strncmp (in_plugin->name, "h26", 3)) ||
908 (!strncmp (in_plugin->name, "rtp", 3)) ||
909 (!strncmp (in_plugin->name, "ass", 3)) ||
910 (!strncmp (in_plugin->name, "ffmetadata", 10)) ||
911 (!strncmp (in_plugin->name, "srt", 3)) ||
912 (!strncmp (in_plugin->name, "scc", 3)) ||
913 !strcmp (in_plugin->name, "segment") ||
914 !strcmp (in_plugin->name, "stream_segment,ssegment") ||
915 !strcmp (in_plugin->name, "jacosub") ||
916 !strcmp (in_plugin->name, "webvtt") ||
917 !strcmp (in_plugin->name, "lrc") ||
918 !strcmp (in_plugin->name, "microdvd") ||
919 !strcmp (in_plugin->name, "tee") ||
920 !strncmp (in_plugin->name, "webm", 4)
921 ) {
922 GST_LOG ("Ignoring muxer %s", in_plugin->name);
923 continue;
924 }
925
926 if ((!strncmp (in_plugin->long_name, "raw ", 4))) {
927 GST_LOG ("Ignoring raw muxer %s", in_plugin->name);
928 continue;
929 }
930
931 if (gst_ffmpegmux_get_replacement (in_plugin->name))
932 rank = GST_RANK_NONE;
933
934 /* FIXME : We need a fast way to know whether we have mappings for this
935 * muxer type. */
936
937 /* construct the type */
938 type_name = g_strdup_printf ("avmux_%s", in_plugin->name);
939 g_strdelimit (type_name, ".,|-<> ", '_');
940
941 type = g_type_from_name (type_name);
942
943 if (!type) {
944 /* create the type now */
945 type = g_type_register_static (GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
946 g_type_set_qdata (type, GST_FFMUX_PARAMS_QDATA, (gpointer) in_plugin);
947 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, &tag_setter_info);
948 }
949
950 if (!gst_element_register (plugin, type_name, rank, type)) {
951 g_free (type_name);
952 return FALSE;
953 }
954
955 g_free (type_name);
956 }
957
958 GST_LOG ("Finished registering muxers");
959
960 return TRUE;
961 }
962