2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
26 #include <libavformat/avformat.h>
27 #include <libavutil/opt.h>
29 #include <gst/base/gstcollectpads.h>
32 #include "gstavcodecmap.h"
33 #include "gstavutils.h"
34 #include "gstavprotocol.h"
36 typedef struct _GstFFMpegMux GstFFMpegMux;
37 typedef struct _GstFFMpegMuxPad GstFFMpegMuxPad;
39 struct _GstFFMpegMuxPad
41 GstCollectData collect; /* we extend the CollectData */
50 GstCollectPads *collect;
51 /* We need to keep track of our pads, so we do so here. */
54 AVFormatContext *context;
57 guint videopads, audiopads;
60 /* event_function is the collectpads default eventfunction */
61 GstPadEventFunction event_function;
66 typedef struct _GstFFMpegMuxClass GstFFMpegMuxClass;
68 struct _GstFFMpegMuxClass
70 GstElementClass parent_class;
72 AVOutputFormat *in_plugin;
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))
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);
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,
112 static gboolean gst_ffmpegmux_sink_event (GstPad * pad, GstObject * parent,
115 static GstStateChangeReturn gst_ffmpegmux_change_state (GstElement * element,
116 GstStateChange transition);
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);
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);
127 #define GST_FFMUX_PARAMS_QDATA g_quark_from_static_string("avmux-params")
129 static GstElementClass *parent_class = NULL;
131 /*static guint gst_ffmpegmux_signals[LAST_SIGNAL] = { 0 }; */
136 const char *replacement;
137 } GstFFMpegMuxReplacement;
140 gst_ffmpegmux_get_replacement (const char *name)
142 static const GstFFMpegMuxReplacement blacklist[] = {
144 {"matroska", "matroskamux"},
146 {"mpegts", "mpegtsmux"},
148 {"mpjpeg", "multipartmux"},
154 {"yuv4mpegpipe", "y4menc"},
156 {"adts", "aacparse"},
158 {"asf_stream", "asfmux"},
165 for (i = 0; i < sizeof (blacklist) / sizeof (blacklist[0]); i++) {
166 if (strcmp (blacklist[i].name, name) == 0) {
167 return blacklist[i].replacement;
175 gst_ffmpegmux_is_formatter (const char *name)
177 static const char *replace[] = {
182 for (i = 0; replace[i]; i++)
183 if (strcmp (replace[i], name) == 0)
189 gst_ffmpegmux_base_init (gpointer g_class)
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;
202 (AVOutputFormat *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
203 GST_FFMUX_PARAMS_QDATA);
204 g_assert (in_plugin != NULL);
206 name = g_strdup (in_plugin->name);
207 g_strdelimit (name, ".,|-<> ", '_');
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) {
214 g_strdup_printf ("libav %s %s (not recommended, use %s instead)",
215 in_plugin->long_name, is_formatter ? "formatter" : "muxer",
218 g_strdup_printf ("libav %s %s (not recommended, use %s instead)",
219 in_plugin->long_name, is_formatter ? "formatter" : "muxer",
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");
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>");
232 g_free (description);
234 /* Try to find the caps that belongs here */
235 srccaps = gst_ffmpeg_formatid_to_caps (name);
237 GST_DEBUG ("Couldn't get source caps for muxer '%s', skipping", name);
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);
249 videosinkcaps = video_ids ? gst_ffmpegmux_get_id_caps (video_ids) : NULL;
250 audiosinkcaps = audio_ids ? gst_ffmpegmux_get_id_caps (audio_ids) : NULL;
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 };
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);
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);
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);
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);
284 klass->in_plugin = in_plugin;
290 gst_ffmpegmux_class_init (GstFFMpegMuxClass * klass)
292 GObjectClass *gobject_class;
293 GstElementClass *gstelement_class;
295 gobject_class = (GObjectClass *) klass;
296 gstelement_class = (GstElementClass *) klass;
298 parent_class = g_type_class_peek_parent (klass);
300 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_ffmpegmux_set_property);
301 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_ffmpegmux_get_property);
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));
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));
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;
319 gst_ffmpegmux_init (GstFFMpegMux * ffmpegmux, GstFFMpegMuxClass * g_class)
321 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
322 GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass *) klass;
323 GstPadTemplate *templ = gst_element_class_get_pad_template (klass, "src");
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);
329 ffmpegmux->collect = gst_collect_pads_new ();
330 gst_collect_pads_set_function (ffmpegmux->collect,
331 (GstCollectPadsFunction) gst_ffmpegmux_collected, ffmpegmux);
333 ffmpegmux->context = avformat_alloc_context ();
334 ffmpegmux->context->oformat = oclass->in_plugin;
335 ffmpegmux->context->nb_streams = 0;
336 ffmpegmux->opened = FALSE;
338 ffmpegmux->videopads = 0;
339 ffmpegmux->audiopads = 0;
340 ffmpegmux->max_delay = 0;
344 gst_ffmpegmux_set_property (GObject * object, guint prop_id,
345 const GValue * value, GParamSpec * pspec)
349 src = (GstFFMpegMux *) object;
353 src->preload = g_value_get_int (value);
356 src->max_delay = g_value_get_int (value);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 gst_ffmpegmux_get_property (GObject * object, guint prop_id, GValue * value,
370 src = (GstFFMpegMux *) object;
374 g_value_set_int (value, src->preload);
377 g_value_set_int (value, src->max_delay);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 gst_ffmpegmux_finalize (GObject * object)
389 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) object;
391 avformat_free_context (ffmpegmux->context);
392 ffmpegmux->context = NULL;
394 gst_object_unref (ffmpegmux->collect);
396 if (G_OBJECT_CLASS (parent_class)->finalize)
397 G_OBJECT_CLASS (parent_class)->finalize (object);
401 gst_ffmpegmux_request_new_pad (GstElement * element,
402 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
404 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
405 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
406 GstFFMpegMuxPad *collect_pad;
410 enum AVMediaType type;
411 gint bitrate = 0, framesize = 0;
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);
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;
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;
428 g_warning ("avmux: unknown pad template!");
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;
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));
444 gst_element_add_pad (element, pad);
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 */
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);
464 * gst_ffmpegmux_setcaps
470 * Returns: #TRUE on success.
473 gst_ffmpegmux_setcaps (GstPad * pad, GstCaps * caps)
475 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (gst_pad_get_parent (pad));
476 GstFFMpegMuxPad *collect_pad;
480 collect_pad = (GstFFMpegMuxPad *) gst_pad_get_element_private (pad);
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));
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)
492 avcodec_parameters_from_context (st->codecpar, &tmp);
494 /* copy over the aspect ratios, ffmpeg expects the stream aspect to match the
496 st->sample_aspect_ratio = st->codecpar->sample_aspect_ratio;
498 GST_LOG_OBJECT (pad, "accepted caps %" GST_PTR_FORMAT, caps);
504 GST_LOG_OBJECT (pad, "rejecting caps %" GST_PTR_FORMAT, caps);
511 gst_ffmpegmux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
513 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) parent;
516 switch (GST_EVENT_TYPE (event)) {
519 GstTagSetter *setter = GST_TAG_SETTER (ffmpegmux);
520 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
522 gst_event_parse_tag (event, &taglist);
523 gst_tag_setter_merge_tags (setter, taglist, mode);
526 case GST_EVENT_CAPS:{
528 gst_event_parse_caps (event, &caps);
529 if (!(res = gst_ffmpegmux_setcaps (pad, caps)))
537 /* chaining up to collectpads default event function */
538 res = ffmpegmux->event_function (pad, parent, event);
545 gst_ffmpegmux_collected (GstCollectPads * pads, gpointer user_data)
547 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) user_data;
549 GstFFMpegMuxPad *best_pad;
550 GstClockTime best_time;
552 /* Re-enable once converted to new AVMetaData API
555 const GstTagList *tags;
558 /* open "file" (gstreamer protocol to next element) */
559 if (!ffmpegmux->opened) {
560 int open_flags = AVIO_FLAG_WRITE;
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];
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) ?
575 return GST_FLOW_ERROR;
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;
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);
597 st->codecpar->frame_size =
598 st->codecpar->sample_rate *
599 GST_BUFFER_DURATION (buffer) / GST_SECOND;
600 gst_buffer_unref (buffer);
608 /* Re-enable once converted to new AVMetaData API
613 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (ffmpegmux));
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));
623 if (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &s)) {
624 strncpy (ffmpegmux->context->author, s,
625 sizeof (ffmpegmux->context->author));
627 if (gst_tag_list_get_string (tags, GST_TAG_COPYRIGHT, &s)) {
628 strncpy (ffmpegmux->context->copyright, s,
629 sizeof (ffmpegmux->context->copyright));
631 if (gst_tag_list_get_string (tags, GST_TAG_COMMENT, &s)) {
632 strncpy (ffmpegmux->context->comment, s,
633 sizeof (ffmpegmux->context->comment));
635 if (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &s)) {
636 strncpy (ffmpegmux->context->album, s,
637 sizeof (ffmpegmux->context->album));
639 if (gst_tag_list_get_string (tags, GST_TAG_GENRE, &s)) {
640 strncpy (ffmpegmux->context->genre, s,
641 sizeof (ffmpegmux->context->genre));
643 if (gst_tag_list_get_int (tags, GST_TAG_TRACK_NUMBER, &i)) {
644 ffmpegmux->context->track = i;
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;
654 /* some house-keeping for downstream before starting data flow */
655 /* stream-start (FIXME: create id based on input ids) */
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));
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));
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;
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;
685 /* we're now opened */
686 ffmpegmux->opened = TRUE;
688 /* flush the header so it will be used as streamheader */
689 avio_flush (ffmpegmux->context->pb);
692 /* take the one with earliest timestamp,
693 * and push it forward */
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);
702 /* if there's no buffer, just continue */
703 if (buffer == NULL) {
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);
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;
721 gst_buffer_unref (buffer);
723 /* Mux buffers with invalid timestamp first */
724 if (!GST_CLOCK_TIME_IS_VALID (best_time))
728 /* now handle the buffer, or signal EOS if we have
730 if (best_pad != NULL) {
732 AVPacket pkt = { 0, };
735 /* push out current buffer */
737 gst_collect_pads_pop (ffmpegmux->collect, (GstCollectData *) best_pad);
740 pkt.pts = gst_ffmpeg_time_gst_to_ff (GST_BUFFER_TIMESTAMP (buf),
741 ffmpegmux->context->streams[best_pad->padnum]->time_base);
744 gst_buffer_map (buf, &map, GST_MAP_READ);
748 pkt.stream_index = best_pad->padnum;
750 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT))
751 pkt.flags |= AV_PKT_FLAG_KEY;
753 if (GST_BUFFER_DURATION_IS_VALID (buf))
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);
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 ());
773 static GstStateChangeReturn
774 gst_ffmpegmux_change_state (GstElement * element, GstStateChange transition)
776 GstStateChangeReturn ret;
777 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (element);
779 switch (transition) {
780 case GST_STATE_CHANGE_NULL_TO_READY:
782 case GST_STATE_CHANGE_READY_TO_PAUSED:
783 gst_collect_pads_start (ffmpegmux->collect);
785 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
787 case GST_STATE_CHANGE_PAUSED_TO_READY:
788 gst_collect_pads_stop (ffmpegmux->collect);
794 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
796 switch (transition) {
797 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
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);
806 case GST_STATE_CHANGE_READY_TO_NULL:
816 gst_ffmpegmux_get_id_caps (enum AVCodecID *id_list)
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);
826 if (gst_caps_is_empty (caps)) {
827 gst_caps_unref (caps);
834 /* set a list of integer values on the caps, e.g. for sample rates */
836 gst_ffmpeg_mux_simple_caps_set_int_list (GstCaps * caps, const gchar * field,
837 guint num, const gint * values)
839 GValue list = { 0, };
843 g_return_if_fail (GST_CAPS_IS_SIMPLE (caps));
845 g_value_init (&list, GST_TYPE_LIST);
846 g_value_init (&val, G_TYPE_INT);
848 for (i = 0; i < num; ++i) {
849 g_value_set_int (&val, values[i]);
850 gst_value_list_append_value (&list, &val);
853 gst_structure_set_value (gst_caps_get_structure (caps, 0), field, &list);
855 g_value_unset (&val);
856 g_value_unset (&list);
860 gst_ffmpegmux_register (GstPlugin * plugin)
862 GTypeInfo typeinfo = {
863 sizeof (GstFFMpegMuxClass),
864 (GBaseInitFunc) gst_ffmpegmux_base_init,
866 (GClassInitFunc) gst_ffmpegmux_class_init,
869 sizeof (GstFFMpegMux),
871 (GInstanceInitFunc) gst_ffmpegmux_init,
873 static const GInterfaceInfo tag_setter_info = {
877 const AVOutputFormat *in_plugin;
880 GST_LOG ("Registering muxers");
882 while ((in_plugin = av_muxer_iterate (&i))) {
884 GstRank rank = GST_RANK_MARGINAL;
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)
921 GST_LOG ("Ignoring muxer %s", in_plugin->name);
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);
932 if (gst_ffmpegmux_get_replacement (in_plugin->name))
933 rank = GST_RANK_NONE;
935 /* FIXME : We need a fast way to know whether we have mappings for this
938 /* construct the type */
939 type_name = g_strdup_printf ("avmux_%s", in_plugin->name);
940 g_strdelimit (type_name, ".,|-<> ", '_');
942 type = g_type_from_name (type_name);
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);
951 if (!gst_element_register (plugin, type_name, rank, type)) {
959 GST_LOG ("Finished registering muxers");