2002-11-06 23:53:46 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2002-11-26 14:50:05 +00:00
|
|
|
#include "config.h"
|
2003-06-09 15:04:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
2002-11-26 14:50:05 +00:00
|
|
|
#ifdef HAVE_FFMPEG_UNINSTALLED
|
|
|
|
#include <avformat.h>
|
|
|
|
#else
|
|
|
|
#include <ffmpeg/avformat.h>
|
|
|
|
#endif
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
#include <gst/gst.h>
|
2002-11-26 14:50:05 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
#include "gstffmpegcodecmap.h"
|
|
|
|
|
2002-11-06 23:53:46 +00:00
|
|
|
typedef struct _GstFFMpegMux GstFFMpegMux;
|
|
|
|
|
|
|
|
struct _GstFFMpegMux {
|
2003-06-09 15:04:26 +00:00
|
|
|
GstElement element;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
/* We need to keep track of our pads, so we do so here. */
|
2003-06-09 15:04:26 +00:00
|
|
|
GstPad *srcpad;
|
|
|
|
|
|
|
|
AVFormatContext *context;
|
|
|
|
gboolean opened;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
GstPad *sinkpads[MAX_STREAMS];
|
|
|
|
gint videopads, audiopads;
|
|
|
|
GstBuffer *bufferqueue[MAX_STREAMS];
|
|
|
|
gboolean eos[MAX_STREAMS];
|
2002-11-06 23:53:46 +00:00
|
|
|
};
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
typedef struct _GstFFMpegMuxClassParams {
|
|
|
|
AVOutputFormat *in_plugin;
|
2003-11-02 13:12:14 +00:00
|
|
|
GstCaps *srccaps, *videosinkcaps, *audiosinkcaps;
|
2003-06-09 15:04:26 +00:00
|
|
|
} GstFFMpegMuxClassParams;
|
|
|
|
|
2002-11-06 23:53:46 +00:00
|
|
|
typedef struct _GstFFMpegMuxClass GstFFMpegMuxClass;
|
|
|
|
|
|
|
|
struct _GstFFMpegMuxClass {
|
2003-06-09 15:04:26 +00:00
|
|
|
GstElementClass parent_class;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
AVOutputFormat *in_plugin;
|
|
|
|
GstPadTemplate *srctempl;
|
|
|
|
GstPadTemplate *videosinktempl;
|
|
|
|
GstPadTemplate *audiosinktempl;
|
2002-11-06 23:53:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define GST_TYPE_FFMPEGMUX \
|
|
|
|
(gst_ffmpegdec_get_type())
|
|
|
|
#define GST_FFMPEGMUX(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGMUX,GstFFMpegMux))
|
|
|
|
#define GST_FFMPEGMUX_CLASS(klass) \
|
|
|
|
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGMUX,GstFFMpegMuxClass))
|
|
|
|
#define GST_IS_FFMPEGMUX(obj) \
|
|
|
|
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGMUX))
|
|
|
|
#define GST_IS_FFMPEGMUX_CLASS(obj) \
|
|
|
|
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGMUX))
|
|
|
|
|
|
|
|
enum {
|
|
|
|
/* FILL ME */
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_0,
|
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
|
|
|
static GHashTable *global_plugins;
|
|
|
|
|
|
|
|
/* A number of functon prototypes are given so we can refer to them later. */
|
|
|
|
static void gst_ffmpegmux_class_init (GstFFMpegMuxClass *klass);
|
2003-11-02 13:12:14 +00:00
|
|
|
static void gst_ffmpegmux_base_init (GstFFMpegMuxClass *klass);
|
2002-11-06 23:53:46 +00:00
|
|
|
static void gst_ffmpegmux_init (GstFFMpegMux *ffmpegmux);
|
2003-06-09 15:04:26 +00:00
|
|
|
static void gst_ffmpegmux_dispose (GObject *object);
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
static GstPadLinkReturn
|
|
|
|
gst_ffmpegmux_connect (GstPad *pad,
|
2003-12-22 01:46:58 +00:00
|
|
|
const GstCaps *caps);
|
2003-06-09 15:04:26 +00:00
|
|
|
static GstPad * gst_ffmpegmux_request_new_pad (GstElement *element,
|
|
|
|
GstPadTemplate *templ,
|
|
|
|
const gchar *name);
|
|
|
|
static void gst_ffmpegmux_loop (GstElement *element);
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
static GstElementStateReturn
|
|
|
|
gst_ffmpegmux_change_state (GstElement *element);
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
static GstElementClass *parent_class = NULL;
|
|
|
|
|
|
|
|
/*static guint gst_ffmpegmux_signals[LAST_SIGNAL] = { 0 }; */
|
|
|
|
|
2003-11-02 13:12:14 +00:00
|
|
|
static void
|
|
|
|
gst_ffmpegmux_base_init (GstFFMpegMuxClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
GstFFMpegMuxClassParams *params;
|
|
|
|
GstElementDetails *details;
|
|
|
|
GstPadTemplate *videosinktempl, *audiosinktempl, *srctempl;
|
|
|
|
|
|
|
|
params = g_hash_table_lookup (global_plugins,
|
|
|
|
GINT_TO_POINTER (G_OBJECT_CLASS_TYPE (gobject_class)));
|
2003-11-02 19:07:41 +00:00
|
|
|
if (!params)
|
|
|
|
params = g_hash_table_lookup (global_plugins,
|
|
|
|
GINT_TO_POINTER (0));
|
|
|
|
g_assert (params);
|
2003-11-02 13:12:14 +00:00
|
|
|
|
|
|
|
/* construct the element details struct */
|
|
|
|
details = g_new0 (GstElementDetails, 1);
|
|
|
|
details->longname = g_strdup_printf ("FFMPEG %s Muxer",
|
|
|
|
params->in_plugin->name);
|
|
|
|
details->klass = g_strdup ("Codec/Muxer");
|
|
|
|
details->description = g_strdup_printf ("FFMPEG %s Muxer",
|
|
|
|
params->in_plugin->name);
|
|
|
|
details->author = g_strdup ("Wim Taymans <wim.taymans@chello.be>\n"
|
|
|
|
"Ronald Bultje <rbultje@ronald.bitfreak.net>");
|
|
|
|
|
|
|
|
/* pad templates */
|
|
|
|
srctempl = gst_pad_template_new ("sink", GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
2003-12-22 01:46:58 +00:00
|
|
|
params->srccaps);
|
2003-11-02 13:12:14 +00:00
|
|
|
audiosinktempl = gst_pad_template_new ("audio_%d",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
2003-12-22 01:46:58 +00:00
|
|
|
params->audiosinkcaps);
|
2003-11-02 13:12:14 +00:00
|
|
|
videosinktempl = gst_pad_template_new ("video_%d",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
2003-12-22 01:46:58 +00:00
|
|
|
params->videosinkcaps);
|
2003-11-02 13:12:14 +00:00
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class, srctempl);
|
|
|
|
gst_element_class_add_pad_template (element_class, videosinktempl);
|
|
|
|
gst_element_class_add_pad_template (element_class, audiosinktempl);
|
|
|
|
gst_element_class_set_details (element_class, details);
|
|
|
|
|
|
|
|
klass->in_plugin = params->in_plugin;
|
|
|
|
klass->srctempl = srctempl;
|
|
|
|
klass->videosinktempl = videosinktempl;
|
|
|
|
klass->audiosinktempl = audiosinktempl;
|
|
|
|
}
|
|
|
|
|
2002-11-06 23:53:46 +00:00
|
|
|
static void
|
|
|
|
gst_ffmpegmux_class_init (GstFFMpegMuxClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass*)klass;
|
|
|
|
gstelement_class = (GstElementClass*)klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
gstelement_class->request_new_pad = gst_ffmpegmux_request_new_pad;
|
|
|
|
gstelement_class->change_state = gst_ffmpegmux_change_state;
|
|
|
|
gobject_class->dispose = gst_ffmpegmux_dispose;
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
static void
|
|
|
|
gst_ffmpegmux_init(GstFFMpegMux *ffmpegmux)
|
2002-11-06 23:53:46 +00:00
|
|
|
{
|
|
|
|
GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass*)(G_OBJECT_GET_CLASS (ffmpegmux));
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
ffmpegmux->srcpad = gst_pad_new_from_template (oclass->srctempl,
|
|
|
|
"src");
|
|
|
|
gst_element_add_pad (GST_ELEMENT (ffmpegmux),
|
|
|
|
ffmpegmux->srcpad);
|
|
|
|
gst_element_set_loop_function (GST_ELEMENT (ffmpegmux),
|
|
|
|
gst_ffmpegmux_loop);
|
|
|
|
|
|
|
|
ffmpegmux->context = g_new0 (AVFormatContext, 1);
|
|
|
|
memset (ffmpegmux->context, 0, sizeof (AVFormatContext));
|
|
|
|
ffmpegmux->context->oformat = oclass->in_plugin;
|
|
|
|
ffmpegmux->context->nb_streams = 0;
|
|
|
|
sprintf (ffmpegmux->context->filename, "gstreamer://%p",
|
|
|
|
ffmpegmux->srcpad);
|
|
|
|
ffmpegmux->opened = FALSE;
|
|
|
|
|
|
|
|
ffmpegmux->videopads = 0;
|
|
|
|
ffmpegmux->audiopads = 0;
|
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
static void
|
|
|
|
gst_ffmpegmux_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) object;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
if (ffmpegmux->opened) {
|
|
|
|
url_fclose (&ffmpegmux->context->pb);
|
|
|
|
ffmpegmux->opened = FALSE;
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
g_free (ffmpegmux->context);
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
static GstPad *
|
|
|
|
gst_ffmpegmux_request_new_pad (GstElement *element,
|
|
|
|
GstPadTemplate *templ,
|
|
|
|
const gchar *name)
|
2002-11-06 23:53:46 +00:00
|
|
|
{
|
2003-06-09 15:04:26 +00:00
|
|
|
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
|
2002-11-06 23:53:46 +00:00
|
|
|
GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass*)(G_OBJECT_GET_CLASS (ffmpegmux));
|
2003-06-09 15:04:26 +00:00
|
|
|
gchar *padname;
|
|
|
|
GstPad *pad;
|
|
|
|
AVStream *st;
|
|
|
|
enum CodecType type;
|
|
|
|
gint padnum;
|
|
|
|
|
|
|
|
g_return_val_if_fail (templ != NULL, NULL);
|
|
|
|
g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
|
|
|
|
g_return_val_if_fail (ffmpegmux->opened == FALSE, NULL);
|
|
|
|
|
|
|
|
/* figure out a name that *we* like */
|
|
|
|
if (templ == oclass->videosinktempl) {
|
|
|
|
padname = g_strdup_printf ("video_%02d",
|
|
|
|
ffmpegmux->videopads++);
|
|
|
|
type = CODEC_TYPE_VIDEO;
|
|
|
|
} else if (templ == oclass->audiosinktempl) {
|
|
|
|
padname = g_strdup_printf ("audio_%02d",
|
|
|
|
ffmpegmux->audiopads++);
|
|
|
|
type = CODEC_TYPE_AUDIO;
|
|
|
|
} else {
|
|
|
|
g_warning("ffmux: unknown pad template!");
|
|
|
|
return NULL;
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* create pad */
|
|
|
|
pad = gst_pad_new_from_template (templ, padname);
|
|
|
|
padnum = ffmpegmux->context->nb_streams++;
|
|
|
|
ffmpegmux->sinkpads[padnum] = pad;
|
|
|
|
gst_pad_set_link_function (pad, gst_ffmpegmux_connect);
|
|
|
|
gst_element_add_pad (element, pad);
|
|
|
|
|
|
|
|
/* AVStream needs to be created */
|
|
|
|
st = g_malloc (sizeof (AVStream));
|
|
|
|
memset(st, 0, sizeof (AVStream));
|
|
|
|
avcodec_get_context_defaults (&st->codec);
|
|
|
|
st->codec.codec_type = type;
|
|
|
|
st->codec.codec_id = CODEC_ID_NONE; /* this is a check afterwards */
|
|
|
|
st->index = padnum;
|
|
|
|
st->stream_copy = 1; /* we're not the actual encoder */
|
|
|
|
/* we fill in other codec-related info during capsnego */
|
|
|
|
ffmpegmux->context->streams[padnum] = st;
|
|
|
|
|
|
|
|
/* we love debug output (c) (tm) (r) */
|
2003-06-29 19:45:00 +00:00
|
|
|
GST_DEBUG (
|
2003-06-09 15:04:26 +00:00
|
|
|
"Created %s pad for ffmux_%s element",
|
|
|
|
padname, oclass->in_plugin->name);
|
|
|
|
g_free (padname);
|
|
|
|
|
|
|
|
return pad;
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
static GstPadLinkReturn
|
|
|
|
gst_ffmpegmux_connect (GstPad *pad,
|
2003-12-22 01:46:58 +00:00
|
|
|
const GstCaps *caps)
|
2002-11-06 23:53:46 +00:00
|
|
|
{
|
2003-06-09 15:04:26 +00:00
|
|
|
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *)(gst_pad_get_parent (pad));
|
|
|
|
gint i;
|
|
|
|
AVStream *st;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
g_return_val_if_fail (ffmpegmux->opened == FALSE,
|
|
|
|
GST_PAD_LINK_REFUSED);
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
for (i = 0; i < ffmpegmux->context->nb_streams; i++) {
|
|
|
|
if (pad == ffmpegmux->sinkpads[i]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == ffmpegmux->context->nb_streams) {
|
|
|
|
g_warning ("Unknown pad given during capsnego: %p", pad);
|
|
|
|
return GST_PAD_LINK_REFUSED;
|
|
|
|
}
|
|
|
|
st = ffmpegmux->context->streams[i];
|
|
|
|
|
|
|
|
/* for the format-specific guesses, we'll go to
|
|
|
|
* our famous codec mapper */
|
2003-12-22 01:46:58 +00:00
|
|
|
if (gst_ffmpeg_caps_to_codecid (caps,
|
|
|
|
&st->codec) != CODEC_ID_NONE) {
|
|
|
|
ffmpegmux->eos[i] = FALSE;
|
|
|
|
return GST_PAD_LINK_OK;
|
2003-06-09 15:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GST_PAD_LINK_REFUSED;
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-06-09 15:04:26 +00:00
|
|
|
gst_ffmpegmux_loop (GstElement *element)
|
2002-11-06 23:53:46 +00:00
|
|
|
{
|
2003-06-09 15:04:26 +00:00
|
|
|
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
|
|
|
|
GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass*)(G_OBJECT_GET_CLASS (ffmpegmux));
|
|
|
|
gint i, bufnum;
|
|
|
|
GstBuffer *buf;
|
|
|
|
|
|
|
|
/* we do need all streams to have started capsnego,
|
|
|
|
* or things will go horribly wrong */
|
|
|
|
for (i = 0; i < ffmpegmux->context->nb_streams; i++) {
|
|
|
|
AVStream *st = ffmpegmux->context->streams[i];
|
|
|
|
|
|
|
|
/* check whether the pad has successfully completed capsnego */
|
|
|
|
if (st->codec.codec_id == CODEC_ID_NONE) {
|
2003-06-29 19:45:00 +00:00
|
|
|
GST_DEBUG (
|
2003-06-09 15:04:26 +00:00
|
|
|
"ffmux loop function called without capsnego on pad %d",
|
|
|
|
i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* open "file" (gstreamer protocol to next element) */
|
|
|
|
if (!ffmpegmux->opened) {
|
|
|
|
if (url_fopen (&ffmpegmux->context->pb,
|
|
|
|
ffmpegmux->context->filename,
|
|
|
|
URL_WRONLY) < 0) {
|
2004-01-29 23:20:39 +00:00
|
|
|
GST_ELEMENT_ERROR (element, LIBRARY, TOO_LAZY, NULL,
|
2004-01-18 21:46:52 +00:00
|
|
|
("Failed to open stream context in ffmux"));
|
2003-06-09 15:04:26 +00:00
|
|
|
return;
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* we're now opened */
|
|
|
|
ffmpegmux->opened = TRUE;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* now open the mux format */
|
|
|
|
oclass->in_plugin->write_header (ffmpegmux->context);
|
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* FIXME: do we support more events? */
|
|
|
|
|
|
|
|
/* start by filling an internal queue of buffers */
|
|
|
|
for (i = 0; i < ffmpegmux->context->nb_streams; i++) {
|
|
|
|
GstPad *pad = ffmpegmux->sinkpads[i];
|
|
|
|
|
|
|
|
/* check for "pull'ability" */
|
|
|
|
while (pad != NULL &&
|
|
|
|
GST_PAD_IS_USABLE (pad) &&
|
|
|
|
/*GST_PAD_IS_ACTIVE (pad) &&*/
|
|
|
|
GST_PAD_IS_LINKED (pad) &&
|
|
|
|
ffmpegmux->eos[i] == FALSE &&
|
|
|
|
ffmpegmux->bufferqueue[i] == NULL) {
|
|
|
|
/* we can pull a buffer! */
|
2003-10-08 16:07:38 +00:00
|
|
|
buf = GST_BUFFER (gst_pad_pull (pad));
|
2003-06-09 15:04:26 +00:00
|
|
|
if (GST_IS_EVENT (buf)) {
|
|
|
|
if (GST_EVENT_TYPE (GST_EVENT (buf)) == GST_EVENT_EOS) {
|
|
|
|
/* flag EOS on this stream */
|
|
|
|
ffmpegmux->eos[i] = TRUE;
|
|
|
|
gst_data_unref (GST_DATA (buf));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ffmpegmux->bufferqueue[i] = buf;
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
2003-06-09 15:04:26 +00:00
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* take the one with earliest timestamp,
|
|
|
|
* and push it forward */
|
|
|
|
bufnum = -1;
|
|
|
|
for (i = 0; i < ffmpegmux->context->nb_streams; i++) {
|
|
|
|
/* if there's no buffer, just continue */
|
|
|
|
if (ffmpegmux->bufferqueue[i] == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* if we have no buffer yet, just use the first one */
|
|
|
|
if (bufnum == -1) {
|
|
|
|
bufnum = i;
|
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* if we do have one, only use this one if it's older */
|
|
|
|
if (GST_BUFFER_TIMESTAMP (ffmpegmux->bufferqueue[i]) <
|
|
|
|
GST_BUFFER_TIMESTAMP (ffmpegmux->bufferqueue[bufnum])) {
|
|
|
|
bufnum = i;
|
|
|
|
}
|
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* now handle the buffer, or signal EOS if we have
|
|
|
|
* no buffers left */
|
|
|
|
if (bufnum >= 0) {
|
|
|
|
/* push out current buffer */
|
|
|
|
buf = ffmpegmux->bufferqueue[bufnum];
|
|
|
|
ffmpegmux->bufferqueue[bufnum] = NULL;
|
|
|
|
oclass->in_plugin->write_packet (ffmpegmux->context,
|
|
|
|
bufnum,
|
|
|
|
GST_BUFFER_DATA (buf),
|
|
|
|
GST_BUFFER_SIZE (buf), 0);
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
} else {
|
|
|
|
/* close down */
|
|
|
|
oclass->in_plugin->write_trailer (ffmpegmux->context);
|
|
|
|
url_fclose (&ffmpegmux->context->pb);
|
|
|
|
ffmpegmux->opened = FALSE;
|
|
|
|
gst_element_set_eos (element);
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
static GstElementStateReturn
|
|
|
|
gst_ffmpegmux_change_state (GstElement *element)
|
2002-11-06 23:53:46 +00:00
|
|
|
{
|
2003-06-09 15:04:26 +00:00
|
|
|
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *)(element);
|
|
|
|
gint transition = GST_STATE_TRANSITION (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_PAUSED_TO_READY:
|
|
|
|
if (ffmpegmux->opened) {
|
|
|
|
url_fclose (&ffmpegmux->context->pb);
|
|
|
|
ffmpegmux->opened = FALSE;
|
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-06-09 15:04:26 +00:00
|
|
|
|
|
|
|
if (GST_ELEMENT_CLASS (parent_class)->change_state)
|
|
|
|
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
|
|
|
|
|
|
|
|
return GST_STATE_SUCCESS;
|
2002-11-06 23:53:46 +00:00
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
|
2002-11-06 23:53:46 +00:00
|
|
|
gboolean
|
|
|
|
gst_ffmpegmux_register (GstPlugin *plugin)
|
|
|
|
{
|
|
|
|
GTypeInfo typeinfo = {
|
|
|
|
sizeof(GstFFMpegMuxClass),
|
2003-11-02 13:12:14 +00:00
|
|
|
(GBaseInitFunc)gst_ffmpegmux_base_init,
|
2002-11-06 23:53:46 +00:00
|
|
|
NULL,
|
|
|
|
(GClassInitFunc)gst_ffmpegmux_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof(GstFFMpegMux),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc)gst_ffmpegmux_init,
|
|
|
|
};
|
|
|
|
GType type;
|
2003-06-09 15:04:26 +00:00
|
|
|
AVOutputFormat *in_plugin;
|
|
|
|
GstFFMpegMuxClassParams *params;
|
|
|
|
AVCodec *in_codec;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
in_plugin = first_oformat;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
global_plugins = g_hash_table_new (NULL, NULL);
|
|
|
|
|
|
|
|
while (in_plugin) {
|
|
|
|
gchar *type_name;
|
2003-06-09 15:04:26 +00:00
|
|
|
gchar *p;
|
|
|
|
GstCaps *srccaps, *audiosinkcaps, *videosinkcaps;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* Try to find the caps that belongs here */
|
|
|
|
srccaps = gst_ffmpeg_formatid_to_caps (in_plugin->name);
|
|
|
|
if (!srccaps) {
|
2002-11-06 23:53:46 +00:00
|
|
|
goto next;
|
|
|
|
}
|
2003-06-09 15:04:26 +00:00
|
|
|
/* This is a bit ugly, but we just take all formats
|
|
|
|
* for the pad template. We'll get an exact match
|
|
|
|
* when we open the stream */
|
|
|
|
audiosinkcaps = NULL;
|
|
|
|
videosinkcaps = NULL;
|
|
|
|
for (in_codec = first_avcodec; in_codec != NULL;
|
|
|
|
in_codec = in_codec->next) {
|
|
|
|
GstCaps *temp = gst_ffmpeg_codecid_to_caps (in_codec->id, NULL);
|
|
|
|
if (!temp) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (in_codec->type) {
|
|
|
|
case CODEC_TYPE_VIDEO:
|
2003-12-22 01:46:58 +00:00
|
|
|
gst_caps_append (videosinkcaps, temp);
|
2003-06-09 15:04:26 +00:00
|
|
|
break;
|
|
|
|
case CODEC_TYPE_AUDIO:
|
2003-12-22 01:46:58 +00:00
|
|
|
gst_caps_append (audiosinkcaps, temp);
|
2003-06-09 15:04:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
2003-12-22 01:46:58 +00:00
|
|
|
gst_caps_free (temp);
|
2003-06-09 15:04:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-06 23:53:46 +00:00
|
|
|
/* construct the type */
|
2003-06-09 15:04:26 +00:00
|
|
|
type_name = g_strdup_printf("ffmux_%s", in_plugin->name);
|
|
|
|
|
|
|
|
p = type_name;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '.') *p = '_';
|
|
|
|
p++;
|
|
|
|
}
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
/* if it's already registered, drop it */
|
|
|
|
if (g_type_from_name(type_name)) {
|
|
|
|
g_free(type_name);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
/* create a cache for these properties */
|
|
|
|
params = g_new0 (GstFFMpegMuxClassParams, 1);
|
|
|
|
params->in_plugin = in_plugin;
|
2003-11-02 13:12:14 +00:00
|
|
|
params->srccaps = srccaps;
|
|
|
|
params->videosinkcaps = videosinkcaps;
|
|
|
|
params->audiosinkcaps = audiosinkcaps;
|
2002-11-06 23:53:46 +00:00
|
|
|
|
2003-11-02 19:07:41 +00:00
|
|
|
g_hash_table_insert (global_plugins,
|
|
|
|
GINT_TO_POINTER (0),
|
|
|
|
(gpointer) params);
|
|
|
|
|
|
|
|
/* create the type now */
|
|
|
|
type = g_type_register_static(GST_TYPE_ELEMENT, type_name , &typeinfo, 0);
|
|
|
|
if (!gst_element_register (plugin, type_name, GST_RANK_NONE, type))
|
|
|
|
return FALSE;
|
|
|
|
|
2003-06-09 15:04:26 +00:00
|
|
|
g_hash_table_insert (global_plugins,
|
|
|
|
GINT_TO_POINTER (type),
|
|
|
|
(gpointer) params);
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
next:
|
|
|
|
in_plugin = in_plugin->next;
|
|
|
|
}
|
2003-11-02 19:07:41 +00:00
|
|
|
g_hash_table_remove (global_plugins, GINT_TO_POINTER (0));
|
2002-11-06 23:53:46 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|