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;
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 /* construct the element details struct */
207 replacement = gst_ffmpegmux_get_replacement (in_plugin->name);
208 is_formatter = gst_ffmpegmux_is_formatter (in_plugin->name);
209 if (replacement != NULL) {
211 g_strdup_printf ("libav %s %s (not recommended, use %s instead)",
212 in_plugin->long_name, is_formatter ? "formatter" : "muxer",
215 g_strdup_printf ("libav %s %s (not recommended, use %s instead)",
216 in_plugin->long_name, is_formatter ? "formatter" : "muxer",
219 longname = g_strdup_printf ("libav %s %s", in_plugin->long_name,
220 is_formatter ? "formatter" : "muxer");
221 description = g_strdup_printf ("libav %s %s", in_plugin->long_name,
222 is_formatter ? "formatter" : "muxer");
224 gst_element_class_set_metadata (element_class, longname,
225 is_formatter ? "Formatter/Metadata" : "Codec/Muxer", description,
226 "Wim Taymans <wim.taymans@chello.be>, "
227 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
229 g_free (description);
231 /* Try to find the caps that belongs here */
232 srccaps = gst_ffmpeg_formatid_to_caps (in_plugin->name);
234 GST_DEBUG ("Couldn't get source caps for muxer '%s', skipping format",
239 if (!gst_ffmpeg_formatid_get_codecids (in_plugin->name,
240 &video_ids, &audio_ids, in_plugin)) {
241 gst_caps_unref (srccaps);
243 ("Couldn't get sink caps for muxer '%s'. Most likely because no input format mapping exists.",
248 videosinkcaps = video_ids ? gst_ffmpegmux_get_id_caps (video_ids) : NULL;
249 audiosinkcaps = audio_ids ? gst_ffmpegmux_get_id_caps (audio_ids) : NULL;
251 /* fix up allowed caps for some muxers */
252 /* FIXME : This should be in gstffmpegcodecmap.c ! */
253 if (strcmp (in_plugin->name, "flv") == 0) {
254 const gint rates[] = { 44100, 22050, 11025 };
256 gst_ffmpeg_mux_simple_caps_set_int_list (audiosinkcaps, "rate", 3, rates);
257 } else if (strcmp (in_plugin->name, "gif") == 0) {
259 gst_caps_unref (videosinkcaps);
261 videosinkcaps = gst_caps_from_string ("video/x-raw, format=(string)RGB");
265 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
266 gst_element_class_add_pad_template (element_class, srctempl);
269 audiosinktempl = gst_pad_template_new ("audio_%u",
270 GST_PAD_SINK, GST_PAD_REQUEST, audiosinkcaps);
271 gst_element_class_add_pad_template (element_class, audiosinktempl);
275 videosinktempl = gst_pad_template_new ("video_%u",
276 GST_PAD_SINK, GST_PAD_REQUEST, videosinkcaps);
277 gst_element_class_add_pad_template (element_class, videosinktempl);
281 klass->in_plugin = in_plugin;
285 gst_ffmpegmux_class_init (GstFFMpegMuxClass * klass)
287 GObjectClass *gobject_class;
288 GstElementClass *gstelement_class;
290 gobject_class = (GObjectClass *) klass;
291 gstelement_class = (GstElementClass *) klass;
293 parent_class = g_type_class_peek_parent (klass);
295 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_ffmpegmux_set_property);
296 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_ffmpegmux_get_property);
298 g_object_class_install_property (gobject_class, PROP_PRELOAD,
299 g_param_spec_int ("preload", "preload",
300 "Set the initial demux-decode delay (in microseconds)",
301 0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
303 g_object_class_install_property (gobject_class, PROP_MAXDELAY,
304 g_param_spec_int ("maxdelay", "maxdelay",
305 "Set the maximum demux-decode delay (in microseconds)", 0, G_MAXINT,
306 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
308 gstelement_class->request_new_pad = gst_ffmpegmux_request_new_pad;
309 gstelement_class->change_state = gst_ffmpegmux_change_state;
310 gobject_class->finalize = gst_ffmpegmux_finalize;
314 gst_ffmpegmux_init (GstFFMpegMux * ffmpegmux, GstFFMpegMuxClass * g_class)
316 GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
317 GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass *) klass;
318 GstPadTemplate *templ = gst_element_class_get_pad_template (klass, "src");
320 ffmpegmux->srcpad = gst_pad_new_from_template (templ, "src");
321 gst_pad_set_caps (ffmpegmux->srcpad, gst_pad_template_get_caps (templ));
322 gst_element_add_pad (GST_ELEMENT (ffmpegmux), ffmpegmux->srcpad);
324 ffmpegmux->collect = gst_collect_pads_new ();
325 gst_collect_pads_set_function (ffmpegmux->collect,
326 (GstCollectPadsFunction) gst_ffmpegmux_collected, ffmpegmux);
328 ffmpegmux->context = avformat_alloc_context ();
329 ffmpegmux->context->oformat = oclass->in_plugin;
330 ffmpegmux->context->nb_streams = 0;
331 ffmpegmux->opened = FALSE;
333 ffmpegmux->videopads = 0;
334 ffmpegmux->audiopads = 0;
335 ffmpegmux->max_delay = 0;
339 gst_ffmpegmux_set_property (GObject * object, guint prop_id,
340 const GValue * value, GParamSpec * pspec)
344 src = (GstFFMpegMux *) object;
348 src->preload = g_value_get_int (value);
351 src->max_delay = g_value_get_int (value);
354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360 gst_ffmpegmux_get_property (GObject * object, guint prop_id, GValue * value,
365 src = (GstFFMpegMux *) object;
369 g_value_set_int (value, src->preload);
372 g_value_set_int (value, src->max_delay);
375 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382 gst_ffmpegmux_finalize (GObject * object)
384 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) object;
386 avformat_free_context (ffmpegmux->context);
387 ffmpegmux->context = NULL;
389 gst_object_unref (ffmpegmux->collect);
391 if (G_OBJECT_CLASS (parent_class)->finalize)
392 G_OBJECT_CLASS (parent_class)->finalize (object);
396 gst_ffmpegmux_request_new_pad (GstElement * element,
397 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
399 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
400 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
401 GstFFMpegMuxPad *collect_pad;
405 enum AVMediaType type;
406 gint bitrate = 0, framesize = 0;
408 g_return_val_if_fail (templ != NULL, NULL);
409 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
410 g_return_val_if_fail (ffmpegmux->opened == FALSE, NULL);
412 /* figure out a name that *we* like */
413 if (templ == gst_element_class_get_pad_template (klass, "video_%u")) {
414 padname = g_strdup_printf ("video_%u", ffmpegmux->videopads++);
415 type = AVMEDIA_TYPE_VIDEO;
418 } else if (templ == gst_element_class_get_pad_template (klass, "audio_%u")) {
419 padname = g_strdup_printf ("audio_%u", ffmpegmux->audiopads++);
420 type = AVMEDIA_TYPE_AUDIO;
421 bitrate = 285 * 1024;
423 g_warning ("avmux: unknown pad template!");
428 pad = gst_pad_new_from_template (templ, padname);
429 collect_pad = (GstFFMpegMuxPad *)
430 gst_collect_pads_add_pad (ffmpegmux->collect, pad,
431 sizeof (GstFFMpegMuxPad), NULL, TRUE);
432 collect_pad->padnum = ffmpegmux->context->nb_streams;
434 /* small hack to put our own event pad function and chain up to collect pad */
435 ffmpegmux->event_function = GST_PAD_EVENTFUNC (pad);
436 gst_pad_set_event_function (pad,
437 GST_DEBUG_FUNCPTR (gst_ffmpegmux_sink_event));
439 gst_element_add_pad (element, pad);
441 /* AVStream needs to be created */
442 st = avformat_new_stream (ffmpegmux->context, NULL);
443 st->id = collect_pad->padnum;
444 st->codec->codec_type = type;
445 st->codec->codec_id = AV_CODEC_ID_NONE; /* this is a check afterwards */
446 st->codec->bit_rate = bitrate;
447 st->codec->frame_size = framesize;
448 /* we fill in codec during capsnego */
450 /* we love debug output (c) (tm) (r) */
451 GST_DEBUG ("Created %s pad for avmux_%s element",
452 padname, ((GstFFMpegMuxClass *) klass)->in_plugin->name);
459 * gst_ffmpegmux_setcaps
465 * Returns: #TRUE on success.
468 gst_ffmpegmux_setcaps (GstPad * pad, GstCaps * caps)
470 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (gst_pad_get_parent (pad));
471 GstFFMpegMuxPad *collect_pad;
474 collect_pad = (GstFFMpegMuxPad *) gst_pad_get_element_private (pad);
476 st = ffmpegmux->context->streams[collect_pad->padnum];
477 av_opt_set_int (&ffmpegmux->context, "preload", ffmpegmux->preload, 0);
478 ffmpegmux->context->max_delay = ffmpegmux->max_delay;
480 /* for the format-specific guesses, we'll go to
481 * our famous codec mapper */
482 if (gst_ffmpeg_caps_to_codecid (caps, st->codec) == AV_CODEC_ID_NONE)
485 /* copy over the aspect ratios, ffmpeg expects the stream aspect to match the
487 st->sample_aspect_ratio = st->codec->sample_aspect_ratio;
489 GST_LOG_OBJECT (pad, "accepted caps %" GST_PTR_FORMAT, caps);
495 GST_LOG_OBJECT (pad, "rejecting caps %" GST_PTR_FORMAT, caps);
502 gst_ffmpegmux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
504 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) parent;
507 switch (GST_EVENT_TYPE (event)) {
510 GstTagSetter *setter = GST_TAG_SETTER (ffmpegmux);
511 const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
513 gst_event_parse_tag (event, &taglist);
514 gst_tag_setter_merge_tags (setter, taglist, mode);
517 case GST_EVENT_CAPS:{
519 gst_event_parse_caps (event, &caps);
520 if (!(res = gst_ffmpegmux_setcaps (pad, caps)))
528 /* chaining up to collectpads default event function */
529 res = ffmpegmux->event_function (pad, parent, event);
536 gst_ffmpegmux_collected (GstCollectPads * pads, gpointer user_data)
538 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) user_data;
540 GstFFMpegMuxPad *best_pad;
541 GstClockTime best_time;
543 /* Re-enable once converted to new AVMetaData API
546 const GstTagList *tags;
549 /* open "file" (gstreamer protocol to next element) */
550 if (!ffmpegmux->opened) {
551 int open_flags = AVIO_FLAG_WRITE;
553 /* we do need all streams to have started capsnego,
554 * or things will go horribly wrong */
555 for (collected = ffmpegmux->collect->data; collected;
556 collected = g_slist_next (collected)) {
557 GstFFMpegMuxPad *collect_pad = (GstFFMpegMuxPad *) collected->data;
558 AVStream *st = ffmpegmux->context->streams[collect_pad->padnum];
560 /* check whether the pad has successfully completed capsnego */
561 if (st->codec->codec_id == AV_CODEC_ID_NONE) {
562 GST_ELEMENT_ERROR (ffmpegmux, CORE, NEGOTIATION, (NULL),
563 ("no caps set on stream %d (%s)", collect_pad->padnum,
564 (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) ?
566 return GST_FLOW_ERROR;
568 /* set framerate for audio */
569 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
570 switch (st->codec->codec_id) {
571 case AV_CODEC_ID_PCM_S16LE:
572 case AV_CODEC_ID_PCM_S16BE:
573 case AV_CODEC_ID_PCM_U16LE:
574 case AV_CODEC_ID_PCM_U16BE:
575 case AV_CODEC_ID_PCM_S8:
576 case AV_CODEC_ID_PCM_U8:
577 st->codec->frame_size = 1;
583 /* FIXME : This doesn't work for RAW AUDIO...
584 * in fact I'm wondering if it even works for any kind of audio... */
585 buffer = gst_collect_pads_peek (ffmpegmux->collect,
586 (GstCollectData *) collect_pad);
588 st->codec->frame_size =
589 st->codec->sample_rate *
590 GST_BUFFER_DURATION (buffer) / GST_SECOND;
591 gst_buffer_unref (buffer);
599 /* Re-enable once converted to new AVMetaData API
604 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (ffmpegmux));
609 /* get the interesting ones */
610 if (gst_tag_list_get_string (tags, GST_TAG_TITLE, &s)) {
611 strncpy (ffmpegmux->context->title, s,
612 sizeof (ffmpegmux->context->title));
614 if (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &s)) {
615 strncpy (ffmpegmux->context->author, s,
616 sizeof (ffmpegmux->context->author));
618 if (gst_tag_list_get_string (tags, GST_TAG_COPYRIGHT, &s)) {
619 strncpy (ffmpegmux->context->copyright, s,
620 sizeof (ffmpegmux->context->copyright));
622 if (gst_tag_list_get_string (tags, GST_TAG_COMMENT, &s)) {
623 strncpy (ffmpegmux->context->comment, s,
624 sizeof (ffmpegmux->context->comment));
626 if (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &s)) {
627 strncpy (ffmpegmux->context->album, s,
628 sizeof (ffmpegmux->context->album));
630 if (gst_tag_list_get_string (tags, GST_TAG_GENRE, &s)) {
631 strncpy (ffmpegmux->context->genre, s,
632 sizeof (ffmpegmux->context->genre));
634 if (gst_tag_list_get_int (tags, GST_TAG_TRACK_NUMBER, &i)) {
635 ffmpegmux->context->track = i;
640 /* set the streamheader flag for gstffmpegprotocol if codec supports it */
641 if (!strcmp (ffmpegmux->context->oformat->name, "flv")) {
642 open_flags |= GST_FFMPEG_URL_STREAMHEADER;
645 if (gst_ffmpegdata_open (ffmpegmux->srcpad, open_flags,
646 &ffmpegmux->context->pb) < 0) {
647 GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, TOO_LAZY, (NULL),
648 ("Failed to open stream context in avmux"));
649 return GST_FLOW_ERROR;
652 /* now open the mux format */
653 if (avformat_write_header (ffmpegmux->context, NULL) < 0) {
654 GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, SETTINGS, (NULL),
655 ("Failed to write file header - check codec settings"));
656 return GST_FLOW_ERROR;
659 /* we're now opened */
660 ffmpegmux->opened = TRUE;
662 /* flush the header so it will be used as streamheader */
663 avio_flush (ffmpegmux->context->pb);
666 /* take the one with earliest timestamp,
667 * and push it forward */
669 best_time = GST_CLOCK_TIME_NONE;
670 for (collected = ffmpegmux->collect->data; collected;
671 collected = g_slist_next (collected)) {
672 GstFFMpegMuxPad *collect_pad = (GstFFMpegMuxPad *) collected->data;
673 GstBuffer *buffer = gst_collect_pads_peek (ffmpegmux->collect,
674 (GstCollectData *) collect_pad);
676 /* if there's no buffer, just continue */
677 if (buffer == NULL) {
681 /* if we have no buffer yet, just use the first one */
682 if (best_pad == NULL) {
683 best_pad = collect_pad;
684 best_time = GST_BUFFER_TIMESTAMP (buffer);
688 /* if we do have one, only use this one if it's older */
689 if (GST_BUFFER_TIMESTAMP (buffer) < best_time) {
690 best_time = GST_BUFFER_TIMESTAMP (buffer);
691 best_pad = collect_pad;
695 gst_buffer_unref (buffer);
697 /* Mux buffers with invalid timestamp first */
698 if (!GST_CLOCK_TIME_IS_VALID (best_time))
702 /* now handle the buffer, or signal EOS if we have
704 if (best_pad != NULL) {
707 gboolean need_free = FALSE;
710 /* push out current buffer */
712 gst_collect_pads_pop (ffmpegmux->collect, (GstCollectData *) best_pad);
714 ffmpegmux->context->streams[best_pad->padnum]->codec->frame_number++;
717 pkt.pts = gst_ffmpeg_time_gst_to_ff (GST_BUFFER_TIMESTAMP (buf),
718 ffmpegmux->context->streams[best_pad->padnum]->time_base);
721 if (strcmp (ffmpegmux->context->oformat->name, "gif") == 0) {
722 AVStream *st = ffmpegmux->context->streams[best_pad->padnum];
726 pkt.size = st->codec->width * st->codec->height * 3;
727 pkt.data = g_malloc (pkt.size);
729 dst.data[0] = pkt.data;
732 dst.linesize[0] = st->codec->width * 3;
734 gst_buffer_map (buf, &map, GST_MAP_READ);
735 gst_ffmpeg_avpicture_fill (&src, map.data,
736 PIX_FMT_RGB24, st->codec->width, st->codec->height);
738 av_picture_copy (&dst, &src, PIX_FMT_RGB24,
739 st->codec->width, st->codec->height);
740 gst_buffer_unmap (buf, &map);
742 gst_buffer_map (buf, &map, GST_MAP_READ);
747 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);
759 av_write_frame (ffmpegmux->context, &pkt);
763 gst_buffer_unmap (buf, &map);
765 gst_buffer_unref (buf);
768 av_write_trailer (ffmpegmux->context);
769 ffmpegmux->opened = FALSE;
770 avio_flush (ffmpegmux->context->pb);
771 gst_ffmpegdata_close (ffmpegmux->context->pb);
772 gst_pad_push_event (ffmpegmux->srcpad, gst_event_new_eos ());
779 static GstStateChangeReturn
780 gst_ffmpegmux_change_state (GstElement * element, GstStateChange transition)
782 GstStateChangeReturn ret;
783 GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (element);
785 switch (transition) {
786 case GST_STATE_CHANGE_NULL_TO_READY:
788 case GST_STATE_CHANGE_READY_TO_PAUSED:
789 gst_collect_pads_start (ffmpegmux->collect);
791 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
793 case GST_STATE_CHANGE_PAUSED_TO_READY:
794 gst_collect_pads_stop (ffmpegmux->collect);
800 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
802 switch (transition) {
803 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
805 case GST_STATE_CHANGE_PAUSED_TO_READY:
806 gst_tag_setter_reset_tags (GST_TAG_SETTER (ffmpegmux));
807 if (ffmpegmux->opened) {
808 ffmpegmux->opened = FALSE;
809 gst_ffmpegdata_close (ffmpegmux->context->pb);
812 case GST_STATE_CHANGE_READY_TO_NULL:
822 gst_ffmpegmux_get_id_caps (enum AVCodecID *id_list)
827 caps = gst_caps_new_empty ();
828 for (i = 0; id_list[i] != AV_CODEC_ID_NONE; i++) {
829 if ((t = gst_ffmpeg_codecid_to_caps (id_list[i], NULL, TRUE)))
830 gst_caps_append (caps, t);
832 if (gst_caps_is_empty (caps)) {
833 gst_caps_unref (caps);
840 /* set a list of integer values on the caps, e.g. for sample rates */
842 gst_ffmpeg_mux_simple_caps_set_int_list (GstCaps * caps, const gchar * field,
843 guint num, const gint * values)
845 GValue list = { 0, };
849 g_return_if_fail (GST_CAPS_IS_SIMPLE (caps));
851 g_value_init (&list, GST_TYPE_LIST);
852 g_value_init (&val, G_TYPE_INT);
854 for (i = 0; i < num; ++i) {
855 g_value_set_int (&val, values[i]);
856 gst_value_list_append_value (&list, &val);
859 gst_structure_set_value (gst_caps_get_structure (caps, 0), field, &list);
861 g_value_unset (&val);
862 g_value_unset (&list);
866 gst_ffmpegmux_register (GstPlugin * plugin)
868 GTypeInfo typeinfo = {
869 sizeof (GstFFMpegMuxClass),
870 (GBaseInitFunc) gst_ffmpegmux_base_init,
872 (GClassInitFunc) gst_ffmpegmux_class_init,
875 sizeof (GstFFMpegMux),
877 (GInstanceInitFunc) gst_ffmpegmux_init,
879 static const GInterfaceInfo tag_setter_info = {
883 AVOutputFormat *in_plugin;
885 in_plugin = av_oformat_next (NULL);
887 GST_LOG ("Registering muxers");
892 GstRank rank = GST_RANK_MARGINAL;
894 if ((!strncmp (in_plugin->name, "u16", 3)) ||
895 (!strncmp (in_plugin->name, "s16", 3)) ||
896 (!strncmp (in_plugin->name, "u24", 3)) ||
897 (!strncmp (in_plugin->name, "s24", 3)) ||
898 (!strncmp (in_plugin->name, "u8", 2)) ||
899 (!strncmp (in_plugin->name, "s8", 2)) ||
900 (!strncmp (in_plugin->name, "u32", 3)) ||
901 (!strncmp (in_plugin->name, "s32", 3)) ||
902 (!strncmp (in_plugin->name, "f32", 3)) ||
903 (!strncmp (in_plugin->name, "f64", 3)) ||
904 (!strncmp (in_plugin->name, "raw", 3)) ||
905 (!strncmp (in_plugin->name, "crc", 3)) ||
906 (!strncmp (in_plugin->name, "null", 4)) ||
907 (!strncmp (in_plugin->name, "gif", 3)) ||
908 (!strncmp (in_plugin->name, "frame", 5)) ||
909 (!strncmp (in_plugin->name, "image", 5)) ||
910 (!strncmp (in_plugin->name, "mulaw", 5)) ||
911 (!strncmp (in_plugin->name, "alaw", 4)) ||
912 (!strncmp (in_plugin->name, "h26", 3)) ||
913 (!strncmp (in_plugin->name, "rtp", 3)) ||
914 (!strncmp (in_plugin->name, "ass", 3)) ||
915 (!strncmp (in_plugin->name, "ffmetadata", 10)) ||
916 (!strncmp (in_plugin->name, "srt", 3)) ||
917 !strcmp (in_plugin->name, "segment")
919 GST_LOG ("Ignoring muxer %s", in_plugin->name);
923 if ((!strncmp (in_plugin->long_name, "raw ", 4))) {
924 GST_LOG ("Ignoring raw muxer %s", in_plugin->name);
928 if (gst_ffmpegmux_get_replacement (in_plugin->name))
929 rank = GST_RANK_NONE;
931 /* FIXME : We need a fast way to know whether we have mappings for this
934 /* construct the type */
935 type_name = g_strdup_printf ("avmux_%s", in_plugin->name);
947 type = g_type_from_name (type_name);
950 /* create the type now */
951 type = g_type_register_static (GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
952 g_type_set_qdata (type, GST_FFMUX_PARAMS_QDATA, (gpointer) in_plugin);
953 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, &tag_setter_info);
956 if (!gst_element_register (plugin, type_name, rank, type)) {
964 in_plugin = av_oformat_next (in_plugin);
967 GST_LOG ("Finished registering muxers");