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 memset (&tmp, 0, sizeof (tmp));
486
487 /* for the format-specific guesses, we'll go to
488 * our famous codec mapper */
489 if (gst_ffmpeg_caps_to_codecid (caps, &tmp) == AV_CODEC_ID_NONE)
490 goto not_accepted;
491
492 avcodec_parameters_from_context (st->codecpar, &tmp);
493
494 /* copy over the aspect ratios, ffmpeg expects the stream aspect to match the
495 * codec aspect. */
496 st->sample_aspect_ratio = st->codecpar->sample_aspect_ratio;
497
498 GST_LOG_OBJECT (pad, "accepted caps %" GST_PTR_FORMAT, caps);
499 return TRUE;
500
501 /* ERRORS */
502 not_accepted:
503 {
504 GST_LOG_OBJECT (pad, "rejecting caps %" GST_PTR_FORMAT, caps);
505 return FALSE;
506 }
507 }
508
509
510 static gboolean
gst_ffmpegmux_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)511 gst_ffmpegmux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
512 {
513 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) parent;
514 gboolean res = TRUE;
515
516 switch (GST_EVENT_TYPE (event)) {
517 case GST_EVENT_TAG:{
518 GstTagList *taglist;
519 GstTagSetter *setter = GST_TAG_SETTER (ffmpegmux);
520 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
521
522 gst_event_parse_tag (event, &taglist);
523 gst_tag_setter_merge_tags (setter, taglist, mode);
524 break;
525 }
526 case GST_EVENT_CAPS:{
527 GstCaps *caps;
528 gst_event_parse_caps (event, &caps);
529 if (!(res = gst_ffmpegmux_setcaps (pad, caps)))
530 goto beach;
531 break;
532 }
533 default:
534 break;
535 }
536
537 /* chaining up to collectpads default event function */
538 res = ffmpegmux->event_function (pad, parent, event);
539
540 beach:
541 return res;
542 }
543
544 static GstFlowReturn
gst_ffmpegmux_collected(GstCollectPads * pads,gpointer user_data)545 gst_ffmpegmux_collected (GstCollectPads * pads, gpointer user_data)
546 {
547 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) user_data;
548 GSList *collected;
549 GstFFMpegMuxPad *best_pad;
550 GstClockTime best_time;
551 #if 0
552 /* Re-enable once converted to new AVMetaData API
553 * See #566605
554 */
555 const GstTagList *tags;
556 #endif
557
558 /* open "file" (gstreamer protocol to next element) */
559 if (!ffmpegmux->opened) {
560 int open_flags = AVIO_FLAG_WRITE;
561
562 /* we do need all streams to have started capsnego,
563 * or things will go horribly wrong */
564 for (collected = ffmpegmux->collect->data; collected;
565 collected = g_slist_next (collected)) {
566 GstFFMpegMuxPad *collect_pad = (GstFFMpegMuxPad *) collected->data;
567 AVStream *st = ffmpegmux->context->streams[collect_pad->padnum];
568
569 /* check whether the pad has successfully completed capsnego */
570 if (st->codecpar->codec_id == AV_CODEC_ID_NONE) {
571 GST_ELEMENT_ERROR (ffmpegmux, CORE, NEGOTIATION, (NULL),
572 ("no caps set on stream %d (%s)", collect_pad->padnum,
573 (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ?
574 "video" : "audio"));
575 return GST_FLOW_ERROR;
576 }
577 /* set framerate for audio */
578 if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
579 switch (st->codecpar->codec_id) {
580 case AV_CODEC_ID_PCM_S16LE:
581 case AV_CODEC_ID_PCM_S16BE:
582 case AV_CODEC_ID_PCM_U16LE:
583 case AV_CODEC_ID_PCM_U16BE:
584 case AV_CODEC_ID_PCM_S8:
585 case AV_CODEC_ID_PCM_U8:
586 st->codecpar->frame_size = 1;
587 break;
588 default:
589 {
590 GstBuffer *buffer;
591
592 /* FIXME : This doesn't work for RAW AUDIO...
593 * in fact I'm wondering if it even works for any kind of audio... */
594 buffer = gst_collect_pads_peek (ffmpegmux->collect,
595 (GstCollectData *) collect_pad);
596 if (buffer) {
597 st->codecpar->frame_size =
598 st->codecpar->sample_rate *
599 GST_BUFFER_DURATION (buffer) / GST_SECOND;
600 gst_buffer_unref (buffer);
601 }
602 }
603 }
604 }
605 }
606
607 #if 0
608 /* Re-enable once converted to new AVMetaData API
609 * See #566605
610 */
611
612 /* tags */
613 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (ffmpegmux));
614 if (tags) {
615 gint i;
616 gchar *s;
617
618 /* get the interesting ones */
619 if (gst_tag_list_get_string (tags, GST_TAG_TITLE, &s)) {
620 strncpy (ffmpegmux->context->title, s,
621 sizeof (ffmpegmux->context->title));
622 }
623 if (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &s)) {
624 strncpy (ffmpegmux->context->author, s,
625 sizeof (ffmpegmux->context->author));
626 }
627 if (gst_tag_list_get_string (tags, GST_TAG_COPYRIGHT, &s)) {
628 strncpy (ffmpegmux->context->copyright, s,
629 sizeof (ffmpegmux->context->copyright));
630 }
631 if (gst_tag_list_get_string (tags, GST_TAG_COMMENT, &s)) {
632 strncpy (ffmpegmux->context->comment, s,
633 sizeof (ffmpegmux->context->comment));
634 }
635 if (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &s)) {
636 strncpy (ffmpegmux->context->album, s,
637 sizeof (ffmpegmux->context->album));
638 }
639 if (gst_tag_list_get_string (tags, GST_TAG_GENRE, &s)) {
640 strncpy (ffmpegmux->context->genre, s,
641 sizeof (ffmpegmux->context->genre));
642 }
643 if (gst_tag_list_get_int (tags, GST_TAG_TRACK_NUMBER, &i)) {
644 ffmpegmux->context->track = i;
645 }
646 }
647 #endif
648
649 /* set the streamheader flag for gstffmpegprotocol if codec supports it */
650 if (!strcmp (ffmpegmux->context->oformat->name, "flv")) {
651 open_flags |= GST_FFMPEG_URL_STREAMHEADER;
652 }
653
654 /* some house-keeping for downstream before starting data flow */
655 /* stream-start (FIXME: create id based on input ids) */
656 {
657 gchar s_id[32];
658
659 g_snprintf (s_id, sizeof (s_id), "avmux-%08x", g_random_int ());
660 gst_pad_push_event (ffmpegmux->srcpad, gst_event_new_stream_start (s_id));
661 }
662 /* segment */
663 {
664 GstSegment segment;
665
666 /* let downstream know we think in BYTES and expect to do seeking later on */
667 gst_segment_init (&segment, GST_FORMAT_BYTES);
668 gst_pad_push_event (ffmpegmux->srcpad, gst_event_new_segment (&segment));
669 }
670
671 if (gst_ffmpegdata_open (ffmpegmux->srcpad, open_flags,
672 &ffmpegmux->context->pb) < 0) {
673 GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, TOO_LAZY, (NULL),
674 ("Failed to open stream context in avmux"));
675 return GST_FLOW_ERROR;
676 }
677
678 /* now open the mux format */
679 if (avformat_write_header (ffmpegmux->context, NULL) < 0) {
680 GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, SETTINGS, (NULL),
681 ("Failed to write file header - check codec settings"));
682 return GST_FLOW_ERROR;
683 }
684
685 /* we're now opened */
686 ffmpegmux->opened = TRUE;
687
688 /* flush the header so it will be used as streamheader */
689 avio_flush (ffmpegmux->context->pb);
690 }
691
692 /* take the one with earliest timestamp,
693 * and push it forward */
694 best_pad = NULL;
695 best_time = GST_CLOCK_TIME_NONE;
696 for (collected = ffmpegmux->collect->data; collected;
697 collected = g_slist_next (collected)) {
698 GstFFMpegMuxPad *collect_pad = (GstFFMpegMuxPad *) collected->data;
699 GstBuffer *buffer = gst_collect_pads_peek (ffmpegmux->collect,
700 (GstCollectData *) collect_pad);
701
702 /* if there's no buffer, just continue */
703 if (buffer == NULL) {
704 continue;
705 }
706
707 /* if we have no buffer yet, just use the first one */
708 if (best_pad == NULL) {
709 best_pad = collect_pad;
710 best_time = GST_BUFFER_TIMESTAMP (buffer);
711 goto next_pad;
712 }
713
714 /* if we do have one, only use this one if it's older */
715 if (GST_BUFFER_TIMESTAMP (buffer) < best_time) {
716 best_time = GST_BUFFER_TIMESTAMP (buffer);
717 best_pad = collect_pad;
718 }
719
720 next_pad:
721 gst_buffer_unref (buffer);
722
723 /* Mux buffers with invalid timestamp first */
724 if (!GST_CLOCK_TIME_IS_VALID (best_time))
725 break;
726 }
727
728 /* now handle the buffer, or signal EOS if we have
729 * no buffers left */
730 if (best_pad != NULL) {
731 GstBuffer *buf;
732 AVPacket pkt = { 0, };
733 GstMapInfo map;
734
735 /* push out current buffer */
736 buf =
737 gst_collect_pads_pop (ffmpegmux->collect, (GstCollectData *) best_pad);
738
739 /* set time */
740 pkt.pts = gst_ffmpeg_time_gst_to_ff (GST_BUFFER_TIMESTAMP (buf),
741 ffmpegmux->context->streams[best_pad->padnum]->time_base);
742 pkt.dts = pkt.pts;
743
744 gst_buffer_map (buf, &map, GST_MAP_READ);
745 pkt.data = map.data;
746 pkt.size = map.size;
747
748 pkt.stream_index = best_pad->padnum;
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 av_write_frame (ffmpegmux->context, &pkt);
758 gst_buffer_unmap (buf, &map);
759 gst_buffer_unref (buf);
760 } else {
761 /* close down */
762 av_write_trailer (ffmpegmux->context);
763 ffmpegmux->opened = FALSE;
764 avio_flush (ffmpegmux->context->pb);
765 gst_ffmpegdata_close (ffmpegmux->context->pb);
766 gst_pad_push_event (ffmpegmux->srcpad, gst_event_new_eos ());
767 return GST_FLOW_EOS;
768 }
769
770 return GST_FLOW_OK;
771 }
772
773 static GstStateChangeReturn
gst_ffmpegmux_change_state(GstElement * element,GstStateChange transition)774 gst_ffmpegmux_change_state (GstElement * element, GstStateChange transition)
775 {
776 GstStateChangeReturn ret;
777 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (element);
778
779 switch (transition) {
780 case GST_STATE_CHANGE_NULL_TO_READY:
781 break;
782 case GST_STATE_CHANGE_READY_TO_PAUSED:
783 gst_collect_pads_start (ffmpegmux->collect);
784 break;
785 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
786 break;
787 case GST_STATE_CHANGE_PAUSED_TO_READY:
788 gst_collect_pads_stop (ffmpegmux->collect);
789 break;
790 default:
791 break;
792 }
793
794 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
795
796 switch (transition) {
797 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
798 break;
799 case GST_STATE_CHANGE_PAUSED_TO_READY:
800 gst_tag_setter_reset_tags (GST_TAG_SETTER (ffmpegmux));
801 if (ffmpegmux->opened) {
802 ffmpegmux->opened = FALSE;
803 gst_ffmpegdata_close (ffmpegmux->context->pb);
804 }
805 break;
806 case GST_STATE_CHANGE_READY_TO_NULL:
807 break;
808 default:
809 break;
810 }
811
812 return ret;
813 }
814
815 static GstCaps *
gst_ffmpegmux_get_id_caps(enum AVCodecID * id_list)816 gst_ffmpegmux_get_id_caps (enum AVCodecID *id_list)
817 {
818 GstCaps *caps, *t;
819 gint i;
820
821 caps = gst_caps_new_empty ();
822 for (i = 0; id_list[i] != AV_CODEC_ID_NONE; i++) {
823 if ((t = gst_ffmpeg_codecid_to_caps (id_list[i], NULL, TRUE)))
824 gst_caps_append (caps, t);
825 }
826 if (gst_caps_is_empty (caps)) {
827 gst_caps_unref (caps);
828 return NULL;
829 }
830
831 return caps;
832 }
833
834 /* set a list of integer values on the caps, e.g. for sample rates */
835 static void
gst_ffmpeg_mux_simple_caps_set_int_list(GstCaps * caps,const gchar * field,guint num,const gint * values)836 gst_ffmpeg_mux_simple_caps_set_int_list (GstCaps * caps, const gchar * field,
837 guint num, const gint * values)
838 {
839 GValue list = { 0, };
840 GValue val = { 0, };
841 guint i;
842
843 g_return_if_fail (GST_CAPS_IS_SIMPLE (caps));
844
845 g_value_init (&list, GST_TYPE_LIST);
846 g_value_init (&val, G_TYPE_INT);
847
848 for (i = 0; i < num; ++i) {
849 g_value_set_int (&val, values[i]);
850 gst_value_list_append_value (&list, &val);
851 }
852
853 gst_structure_set_value (gst_caps_get_structure (caps, 0), field, &list);
854
855 g_value_unset (&val);
856 g_value_unset (&list);
857 }
858
859 gboolean
gst_ffmpegmux_register(GstPlugin * plugin)860 gst_ffmpegmux_register (GstPlugin * plugin)
861 {
862 GTypeInfo typeinfo = {
863 sizeof (GstFFMpegMuxClass),
864 (GBaseInitFunc) gst_ffmpegmux_base_init,
865 NULL,
866 (GClassInitFunc) gst_ffmpegmux_class_init,
867 NULL,
868 NULL,
869 sizeof (GstFFMpegMux),
870 0,
871 (GInstanceInitFunc) gst_ffmpegmux_init,
872 };
873 static const GInterfaceInfo tag_setter_info = {
874 NULL, NULL, NULL
875 };
876 GType type;
877 const AVOutputFormat *in_plugin;
878 void *i = 0;
879
880 GST_LOG ("Registering muxers");
881
882 while ((in_plugin = av_muxer_iterate (&i))) {
883 gchar *type_name;
884 GstRank rank = GST_RANK_MARGINAL;
885
886 if ((!strncmp (in_plugin->name, "u16", 3)) ||
887 (!strncmp (in_plugin->name, "s16", 3)) ||
888 (!strncmp (in_plugin->name, "u24", 3)) ||
889 (!strncmp (in_plugin->name, "s24", 3)) ||
890 (!strncmp (in_plugin->name, "u8", 2)) ||
891 (!strncmp (in_plugin->name, "s8", 2)) ||
892 (!strncmp (in_plugin->name, "u32", 3)) ||
893 (!strncmp (in_plugin->name, "s32", 3)) ||
894 (!strncmp (in_plugin->name, "f32", 3)) ||
895 (!strncmp (in_plugin->name, "f64", 3)) ||
896 (!strncmp (in_plugin->name, "raw", 3)) ||
897 (!strncmp (in_plugin->name, "crc", 3)) ||
898 (!strncmp (in_plugin->name, "null", 4)) ||
899 (!strncmp (in_plugin->name, "gif", 3)) ||
900 (!strncmp (in_plugin->name, "fifo", 4)) ||
901 (!strncmp (in_plugin->name, "frame", 5)) ||
902 (!strncmp (in_plugin->name, "image", 5)) ||
903 (!strncmp (in_plugin->name, "mulaw", 5)) ||
904 (!strncmp (in_plugin->name, "alaw", 4)) ||
905 (!strncmp (in_plugin->name, "h26", 3)) ||
906 (!strncmp (in_plugin->name, "rtp", 3)) ||
907 (!strncmp (in_plugin->name, "ass", 3)) ||
908 (!strncmp (in_plugin->name, "ffmetadata", 10)) ||
909 (!strncmp (in_plugin->name, "srt", 3)) ||
910 (!strncmp (in_plugin->name, "scc", 3)) ||
911 !strcmp (in_plugin->name, "ttml") ||
912 !strcmp (in_plugin->name, "segment") ||
913 !strcmp (in_plugin->name, "stream_segment,ssegment") ||
914 !strcmp (in_plugin->name, "jacosub") ||
915 !strcmp (in_plugin->name, "webvtt") ||
916 !strcmp (in_plugin->name, "lrc") ||
917 !strcmp (in_plugin->name, "microdvd") ||
918 !strcmp (in_plugin->name, "tee") ||
919 !strncmp (in_plugin->name, "webm", 4)
920 ) {
921 GST_LOG ("Ignoring muxer %s", in_plugin->name);
922 continue;
923 }
924
925 if (in_plugin->long_name != NULL) {
926 if ((!strncmp (in_plugin->long_name, "raw ", 4))) {
927 GST_LOG ("Ignoring raw muxer %s", in_plugin->name);
928 continue;
929 }
930 }
931
932 if (gst_ffmpegmux_get_replacement (in_plugin->name))
933 rank = GST_RANK_NONE;
934
935 /* FIXME : We need a fast way to know whether we have mappings for this
936 * muxer type. */
937
938 /* construct the type */
939 type_name = g_strdup_printf ("avmux_%s", in_plugin->name);
940 g_strdelimit (type_name, ".,|-<> ", '_');
941
942 type = g_type_from_name (type_name);
943
944 if (!type) {
945 /* create the type now */
946 type = g_type_register_static (GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
947 g_type_set_qdata (type, GST_FFMUX_PARAMS_QDATA, (gpointer) in_plugin);
948 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, &tag_setter_info);
949 }
950
951 if (!gst_element_register (plugin, type_name, rank, type)) {
952 g_free (type_name);
953 return FALSE;
954 }
955
956 g_free (type_name);
957 }
958
959 GST_LOG ("Finished registering muxers");
960
961 return TRUE;
962 }
963