diff options
author | Wim Taymans <wim.taymans@gmail.com> | 2004-05-28 18:01:34 +0000 |
---|---|---|
committer | Wim Taymans <wim.taymans@gmail.com> | 2004-05-28 18:01:34 +0000 |
commit | 8b7c3ac223c33e03d34daddc2a6f4dd795d00f34 (patch) | |
tree | 458b6986b7b567290190919a62eceb66181a80b1 /gst/multipart | |
parent | 3837845a3b7ce5075c987f629a1c89155b9a94a7 (diff) |
A plugin to add an alpha channel to I420 video. Can optionally do chroma keying.
Original commit message from CVS:
* configure.ac:
* gst/alpha/Makefile.am:
* gst/alpha/gstalpha.c: (gst_alpha_method_get_type),
(gst_alpha_get_type), (gst_alpha_base_init),
(gst_alpha_class_init), (gst_alpha_init), (gst_alpha_set_property),
(gst_alpha_get_property), (gst_alpha_sink_link), (gst_alpha_add),
(gst_alpha_chroma_key), (gst_alpha_chain),
(gst_alpha_change_state), (plugin_init):
A plugin to add an alpha channel to I420 video. Can optionally do
chroma keying.
* gst/multipart/Makefile.am:
* gst/multipart/multipart.c: (plugin_init):
* gst/multipart/multipartdemux.c: (gst_multipart_demux_base_init),
(gst_multipart_demux_class_init), (gst_multipart_demux_init),
(gst_multipart_demux_finalize), (gst_multipart_demux_handle_event),
(gst_multipart_find_pad_by_mime), (gst_multipart_demux_chain),
(gst_multipart_demux_change_state),
(gst_multipart_demux_plugin_init):
* gst/multipart/multipartmux.c: (gst_multipart_mux_get_type),
(gst_multipart_mux_base_init), (gst_multipart_mux_class_init),
(gst_multipart_mux_get_sink_event_masks), (gst_multipart_mux_init),
(gst_multipart_mux_sinkconnect), (gst_multipart_mux_pad_link),
(gst_multipart_mux_pad_unlink),
(gst_multipart_mux_request_new_pad),
(gst_multipart_mux_handle_src_event),
(gst_multipart_mux_next_buffer), (gst_multipart_mux_compare_pads),
(gst_multipart_mux_queue_pads), (gst_multipart_mux_loop),
(gst_multipart_mux_get_property), (gst_multipart_mux_set_property),
(gst_multipart_mux_change_state), (gst_multipart_mux_plugin_init):
A Multipart demuxer/muxer. Not sure if it violates specs. Used to
send multipart jpeg images to a browser.
* gst/videobox/Makefile.am:
* gst/videobox/README:
* gst/videobox/gstvideobox.c: (gst_video_box_fill_get_type),
(gst_video_box_get_type), (gst_video_box_base_init),
(gst_video_box_class_init), (gst_video_box_init),
(gst_video_box_set_property), (gst_video_box_get_property),
(gst_video_box_sink_link), (gst_video_box_i420),
(gst_video_box_ayuv), (gst_video_box_chain),
(gst_video_box_change_state), (plugin_init):
Crops or adds borders around an image. can do alpha channel
borders as well.
* gst/videomixer/Makefile.am:
* gst/videomixer/README:
* gst/videomixer/videomixer.c: (gst_videomixer_pad_get_type),
(gst_videomixer_pad_base_init), (gst_videomixer_pad_class_init),
(gst_videomixer_pad_get_sink_event_masks),
(gst_videomixer_pad_get_property),
(gst_videomixer_pad_set_property),
(gst_videomixer_pad_sinkconnect), (gst_videomixer_pad_link),
(gst_videomixer_pad_unlink), (gst_videomixer_pad_init),
(gst_video_mixer_background_get_type), (gst_videomixer_get_type),
(gst_videomixer_base_init), (gst_videomixer_class_init),
(gst_videomixer_init), (gst_videomixer_request_new_pad),
(gst_videomixer_handle_src_event),
(gst_videomixer_blend_ayuv_i420), (gst_videomixer_fill_checker),
(gst_videomixer_fill_color), (gst_videomixer_fill_queues),
(gst_videomixer_blend_buffers), (gst_videomixer_update_queues),
(gst_videomixer_loop), (gst_videomixer_get_property),
(gst_videomixer_set_property), (gst_videomixer_change_state),
(plugin_init):
Generic video mixer plugin, can handle multiple inputs all with
different framerates and video sizes. Is fully alpha channel
aware.
Diffstat (limited to 'gst/multipart')
-rw-r--r-- | gst/multipart/Makefile.am | 9 | ||||
-rw-r--r-- | gst/multipart/multipart.c | 44 | ||||
-rw-r--r-- | gst/multipart/multipartdemux.c | 374 | ||||
-rw-r--r-- | gst/multipart/multipartmux.c | 593 |
4 files changed, 1020 insertions, 0 deletions
diff --git a/gst/multipart/Makefile.am b/gst/multipart/Makefile.am new file mode 100644 index 00000000..45632eff --- /dev/null +++ b/gst/multipart/Makefile.am @@ -0,0 +1,9 @@ +plugindir = $(libdir)/gstreamer-@GST_MAJORMINOR@ + +plugin_LTLIBRARIES = libmultipart.la + +libmultipart_la_SOURCES = multipart.c multipartdemux.c multipartmux.c +libmultipart_la_CFLAGS = $(GST_CFLAGS) +libmultipart_la_LIBADD = +libmultipart_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) + diff --git a/gst/multipart/multipart.c b/gst/multipart/multipart.c new file mode 100644 index 00000000..802653fb --- /dev/null +++ b/gst/multipart/multipart.c @@ -0,0 +1,44 @@ +/* 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> + +extern gboolean gst_multipart_demux_plugin_init (GstPlugin * plugin); +extern gboolean gst_multipart_mux_plugin_init (GstPlugin * plugin); + +GST_DEBUG_CATEGORY (vorbisdec_debug); + +static gboolean +plugin_init (GstPlugin * plugin) +{ + gst_multipart_demux_plugin_init (plugin); + gst_multipart_mux_plugin_init (plugin); + + return TRUE; +} + +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "multipart", + "multipart stream manipulation", + plugin_init, VERSION, GST_LICENSE, GST_PACKAGE, GST_ORIGIN) diff --git a/gst/multipart/multipartdemux.c b/gst/multipart/multipartdemux.c new file mode 100644 index 00000000..6406df27 --- /dev/null +++ b/gst/multipart/multipartdemux.c @@ -0,0 +1,374 @@ +/* GStreamer + * Copyright (C) 2004 Wim Taymans <wim@fluendo.com> + * + * gstmultipartdemux.c: multipart stream demuxer + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include <gst/gst.h> + +#include <string.h> + +GST_DEBUG_CATEGORY_STATIC (gst_multipart_demux_debug); +#define GST_CAT_DEFAULT gst_multipart_demux_debug + +#define GST_TYPE_MULTIPART_DEMUX (gst_multipart_demux_get_type()) +#define GST_MULTIPART_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTIPART_DEMUX, GstMultipartDemux)) +#define GST_MULTIPART_DEMUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MULTIPART_DEMUX, GstMultipartDemux)) +#define GST_IS_MULTIPART_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTIPART_DEMUX)) +#define GST_IS_MULTIPART_DEMUX_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MULTIPART_DEMUX)) + +#define MAX_LINE_LEN 500 + +typedef struct _GstMultipartDemux GstMultipartDemux; +typedef struct _GstMultipartDemuxClass GstMultipartDemuxClass; + +static gchar *toFind = "--ThisRandomString\nContent-type: "; //image/jpeg\n\n"; +static gint toFindLen; + +/* all information needed for one multipart stream */ +typedef struct +{ + GstPad *pad; /* reference for this pad is held by element we belong to */ + + gchar *mime; + + guint64 offset; /* end offset of last buffer */ + guint64 known_offset; /* last known offset */ + + guint flags; +} +GstMultipartPad; + +struct _GstMultipartDemux +{ + GstElement element; + + /* pad */ + GstPad *sinkpad; + + GSList *srcpads; + gint numpads; + + gchar *parsing_mime; + gchar *buffer; + gint maxlen; + gint bufsize; + gint scanpos; + gint lastpos; +}; + +struct _GstMultipartDemuxClass +{ + GstElementClass parent_class; +}; + +/* elementfactory information */ +static GstElementDetails gst_multipart_demux_details = +GST_ELEMENT_DETAILS ("multipart demuxer", + "Codec/Demuxer", + "demux multipart streams", + "Wim Taymans <wim@fluendo.com>"); + + +/* signals and args */ +enum +{ + /* FILL ME */ + LAST_SIGNAL +}; + +enum +{ + ARG_0, + /* FILL ME */ +}; + +static GstStaticPadTemplate multipart_demux_src_template_factory = +GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_SOMETIMES, + GST_STATIC_CAPS_ANY); + +static GstStaticPadTemplate multipart_demux_sink_template_factory = +GST_STATIC_PAD_TEMPLATE ("sink", + GST_PAD_SINK, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("multipart/x-mixed-replace") + ); + + +static void gst_multipart_demux_finalize (GObject * object); + +static void gst_multipart_demux_chain (GstPad * pad, GstData * buffer); + +static GstElementStateReturn gst_multipart_demux_change_state (GstElement * + element); + + +GST_BOILERPLATE (GstMultipartDemux, gst_multipart_demux, GstElement, + GST_TYPE_ELEMENT) + + static void gst_multipart_demux_base_init (gpointer g_class) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + + gst_element_class_set_details (element_class, &gst_multipart_demux_details); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&multipart_demux_sink_template_factory)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&multipart_demux_src_template_factory)); + + toFindLen = strlen (toFind); +} + +static void +gst_multipart_demux_class_init (GstMultipartDemuxClass * klass) +{ + GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gstelement_class->change_state = gst_multipart_demux_change_state; + + gobject_class->finalize = gst_multipart_demux_finalize; +} + +static void +gst_multipart_demux_init (GstMultipartDemux * multipart) +{ + /* create the sink pad */ + multipart->sinkpad = + gst_pad_new_from_template (gst_static_pad_template_get + (&multipart_demux_sink_template_factory), "sink"); + gst_element_add_pad (GST_ELEMENT (multipart), multipart->sinkpad); + gst_pad_set_chain_function (multipart->sinkpad, + GST_DEBUG_FUNCPTR (gst_multipart_demux_chain)); + + GST_FLAG_SET (multipart, GST_ELEMENT_EVENT_AWARE); + + multipart->maxlen = 4096; + multipart->buffer = g_malloc (multipart->maxlen); + multipart->parsing_mime = NULL; + multipart->numpads = 0; + multipart->scanpos = 0; + multipart->lastpos = 0; +} + +static void +gst_multipart_demux_finalize (GObject * object) +{ + GstMultipartDemux *multipart; + + multipart = GST_MULTIPART_DEMUX (object); +} + +static void +gst_multipart_demux_handle_event (GstPad * pad, GstEvent * event) +{ + //GstMultipartDemux *multipart = GST_MULTIPART_DEMUX (gst_pad_get_parent (pad)); + + switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_DISCONTINUOUS: + case GST_EVENT_EOS: + default: + gst_pad_event_default (pad, event); + break; + } + return; +} + +static GstMultipartPad * +gst_multipart_find_pad_by_mime (GstMultipartDemux * demux, gchar * mime) +{ + GSList *walk; + + walk = demux->srcpads; + while (walk) { + GstMultipartPad *pad = (GstMultipartPad *) walk->data; + + if (!strcmp (pad->mime, mime)) { + return pad; + } + + walk = walk->next; + } + // pad not found, create it + { + GstPad *pad; + GstMultipartPad *mppad; + gchar *name; + GstCaps *caps; + + mppad = g_new0 (GstMultipartPad, 1); + + name = g_strdup_printf ("src_%d", demux->numpads); + pad = gst_pad_new_from_template (gst_static_pad_template_get + (&multipart_demux_src_template_factory), name); + g_free (name); + caps = gst_caps_from_string (mime); + gst_pad_use_explicit_caps (pad); + gst_pad_set_explicit_caps (pad, caps); + + mppad->pad = pad; + mppad->mime = g_strdup (mime); + + demux->srcpads = g_slist_prepend (demux->srcpads, mppad); + demux->numpads++; + + gst_element_add_pad (GST_ELEMENT (demux), pad); + + return mppad; + } +} + +static void +gst_multipart_demux_chain (GstPad * pad, GstData * buffer) +{ + GstMultipartDemux *multipart; + gint size; + gchar *data; + gint matchpos; + + /* handle events */ + if (GST_IS_EVENT (buffer)) { + gst_multipart_demux_handle_event (pad, GST_EVENT (buffer)); + return; + } + + multipart = GST_MULTIPART_DEMUX (gst_pad_get_parent (pad)); + + data = GST_BUFFER_DATA (buffer); + size = GST_BUFFER_SIZE (buffer); + + // first make sure our buffer is long enough + if (multipart->bufsize + size > multipart->maxlen) { + gint newsize = (multipart->bufsize + size) * 2; + + multipart->buffer = g_realloc (multipart->buffer, newsize); + multipart->maxlen = newsize; + } + // copy bytes into the buffer + memcpy (multipart->buffer + multipart->bufsize, data, size); + multipart->bufsize += size; + + // find \n + while (multipart->scanpos < multipart->bufsize) { + if (multipart->buffer[multipart->scanpos] == '\n') { + + } + multipart->scanpos++; + } + + // then scan for the boundary + for (matchpos = 0; + multipart->scanpos + toFindLen + MAX_LINE_LEN - matchpos < + multipart->bufsize; multipart->scanpos++) { + if (multipart->buffer[multipart->scanpos] == toFind[matchpos]) { + matchpos++; + if (matchpos == toFindLen) { + int datalen; + int i, start; + gchar *mime_type; + + multipart->scanpos++; + + start = multipart->scanpos; + // find \n + for (i = 0; i < MAX_LINE_LEN; i++) { + if (multipart->buffer[multipart->scanpos] == '\n') + break; + multipart->scanpos++; + matchpos++; + } + mime_type = + g_strndup (multipart->buffer + start, multipart->scanpos - start); + multipart->scanpos += 2; + matchpos += 3; + + datalen = multipart->scanpos - matchpos; + if (datalen > 0 && multipart->parsing_mime) { + GstBuffer *outbuf; + GstMultipartPad *srcpad; + + srcpad = + gst_multipart_find_pad_by_mime (multipart, + multipart->parsing_mime); + if (srcpad != NULL) { + outbuf = gst_buffer_new_and_alloc (datalen); + + memcpy (GST_BUFFER_DATA (outbuf), multipart->buffer, datalen); + GST_BUFFER_TIMESTAMP (outbuf) = 0; + gst_pad_push (srcpad->pad, GST_DATA (outbuf)); + } + } + // move rest downward + multipart->bufsize -= multipart->scanpos; + memcpy (multipart->buffer, multipart->buffer + multipart->scanpos, + multipart->bufsize); + + multipart->parsing_mime = mime_type; + multipart->scanpos = 0; + } + } else { + matchpos = 0; + } + } + + gst_buffer_unref (buffer); +} + +static GstElementStateReturn +gst_multipart_demux_change_state (GstElement * element) +{ + GstMultipartDemux *multipart; + + multipart = GST_MULTIPART_DEMUX (element); + + switch (GST_STATE_TRANSITION (element)) { + case GST_STATE_NULL_TO_READY: + break; + case GST_STATE_READY_TO_PAUSED: + break; + case GST_STATE_PAUSED_TO_PLAYING: + break; + case GST_STATE_PLAYING_TO_PAUSED: + break; + case GST_STATE_PAUSED_TO_READY: + break; + case GST_STATE_READY_TO_NULL: + break; + default: + g_assert_not_reached (); + break; + } + + return parent_class->change_state (element); +} + +gboolean +gst_multipart_demux_plugin_init (GstPlugin * plugin) +{ + GST_DEBUG_CATEGORY_INIT (gst_multipart_demux_debug, + "multipartdemux", 0, "multipart demuxer"); + + return gst_element_register (plugin, "multipartdemux", GST_RANK_PRIMARY, + GST_TYPE_MULTIPART_DEMUX); +} diff --git a/gst/multipart/multipartmux.c b/gst/multipart/multipartmux.c new file mode 100644 index 00000000..d4d96bc6 --- /dev/null +++ b/gst/multipart/multipartmux.c @@ -0,0 +1,593 @@ +/* multipart muxer plugin for GStreamer + * Copyright (C) 2004 Wim Taymans <wim@fluendo.com> + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> +#include <string.h> + +GST_DEBUG_CATEGORY_STATIC (gst_multipart_mux_debug); +#define GST_CAT_DEFAULT gst_multipart_mux_debug + +#define GST_TYPE_MULTIPART_MUX (gst_multipart_mux_get_type()) +#define GST_MULTIPART_MUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTIPART_MUX, GstMultipartMux)) +#define GST_MULTIPART_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MULTIPART_MUX, GstMultipartMux)) +#define GST_IS_MULTIPART_MUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTIPART_MUX)) +#define GST_IS_MULTIPART_MUX_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MULTIPART_MUX)) + +typedef struct _GstMultipartMux GstMultipartMux; +typedef struct _GstMultipartMuxClass GstMultipartMuxClass; + +/* all information needed for one multipart stream */ +typedef struct +{ + GstPad *pad; /* reference for this pad is held by element we belong to */ + + GstBuffer *buffer; /* the queued buffer for this pad */ + + gboolean eos; + const gchar *mimetype; + + guint state; /* state of the pad */ +} +GstMultipartPad; + +struct _GstMultipartMux +{ + GstElement element; + + /* pad */ + GstPad *srcpad; + + /* sinkpads, a GSList of GstMultipartPads */ + GSList *sinkpads; + gint numpads; + + /* the pad we are currently pulling from to fill a page */ + GstMultipartPad *pulling; + + /* next timestamp for the page */ + GstClockTime next_ts; + + /* offset in stream */ + guint64 offset; +}; + +typedef enum +{ + GST_MULTIPART_FLAG_BOS = GST_ELEMENT_FLAG_LAST, + GST_MULTIPART_FLAG_EOS +} +GstMultipartFlag; + +struct _GstMultipartMuxClass +{ + GstElementClass parent_class; +}; + +/* elementfactory information */ +static GstElementDetails gst_multipart_mux_details = +GST_ELEMENT_DETAILS ("multipart muxer", + "Codec/Muxer", + "mux multipart streams", + "Wim Taymans <wim@fluendo.com>"); + +/* MultipartMux signals and args */ +enum +{ + /* FILL ME */ + LAST_SIGNAL +}; + +enum +{ + ARG_0, +}; + +static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS ("multipart/x-mixed-replace") + ); + +static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d", + GST_PAD_SINK, + GST_PAD_REQUEST, + GST_STATIC_CAPS_ANY /* we can take anything, really */ + ); + +static void gst_multipart_mux_base_init (gpointer g_class); +static void gst_multipart_mux_class_init (GstMultipartMuxClass * klass); +static void gst_multipart_mux_init (GstMultipartMux * multipart_mux); + +static void gst_multipart_mux_loop (GstElement * element); +static gboolean gst_multipart_mux_handle_src_event (GstPad * pad, + GstEvent * event); +static GstPad *gst_multipart_mux_request_new_pad (GstElement * element, + GstPadTemplate * templ, const gchar * name); +static void gst_multipart_mux_set_property (GObject * object, guint prop_id, + const GValue * value, GParamSpec * pspec); +static void gst_multipart_mux_get_property (GObject * object, guint prop_id, + GValue * value, GParamSpec * pspec); +static GstElementStateReturn gst_multipart_mux_change_state (GstElement * + element); + +static GstElementClass *parent_class = NULL; + +/*static guint gst_multipart_mux_signals[LAST_SIGNAL] = { 0 }; */ + +GType +gst_multipart_mux_get_type (void) +{ + static GType multipart_mux_type = 0; + + if (!multipart_mux_type) { + static const GTypeInfo multipart_mux_info = { + sizeof (GstMultipartMuxClass), + gst_multipart_mux_base_init, + NULL, + (GClassInitFunc) gst_multipart_mux_class_init, + NULL, + NULL, + sizeof (GstMultipartMux), + 0, + (GInstanceInitFunc) gst_multipart_mux_init, + }; + + multipart_mux_type = + g_type_register_static (GST_TYPE_ELEMENT, "GstMultipartMux", + &multipart_mux_info, 0); + } + return multipart_mux_type; +} + +static void +gst_multipart_mux_base_init (gpointer g_class) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS (g_class); + + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&src_factory)); + gst_element_class_add_pad_template (element_class, + gst_static_pad_template_get (&sink_factory)); + + gst_element_class_set_details (element_class, &gst_multipart_mux_details); +} + +static void +gst_multipart_mux_class_init (GstMultipartMuxClass * klass) +{ + GObjectClass *gobject_class; + GstElementClass *gstelement_class; + + gobject_class = (GObjectClass *) klass; + gstelement_class = (GstElementClass *) klass; + + parent_class = g_type_class_ref (GST_TYPE_ELEMENT); + + gstelement_class->request_new_pad = gst_multipart_mux_request_new_pad; + + gstelement_class->change_state = gst_multipart_mux_change_state; + + gstelement_class->get_property = gst_multipart_mux_get_property; + gstelement_class->set_property = gst_multipart_mux_set_property; +} + +static const GstEventMask * +gst_multipart_mux_get_sink_event_masks (GstPad * pad) +{ + static const GstEventMask gst_multipart_mux_sink_event_masks[] = { + {GST_EVENT_EOS, 0}, + {0,} + }; + + return gst_multipart_mux_sink_event_masks; +} + +static void +gst_multipart_mux_init (GstMultipartMux * multipart_mux) +{ + GstElementClass *klass = GST_ELEMENT_GET_CLASS (multipart_mux); + + multipart_mux->srcpad = + gst_pad_new_from_template (gst_element_class_get_pad_template (klass, + "src"), "src"); + gst_pad_set_event_function (multipart_mux->srcpad, + gst_multipart_mux_handle_src_event); + gst_element_add_pad (GST_ELEMENT (multipart_mux), multipart_mux->srcpad); + + GST_FLAG_SET (GST_ELEMENT (multipart_mux), GST_ELEMENT_EVENT_AWARE); + GST_FLAG_SET (GST_ELEMENT (multipart_mux), GST_MULTIPART_FLAG_BOS); + + multipart_mux->sinkpads = NULL; + multipart_mux->pulling = NULL; + + gst_element_set_loop_function (GST_ELEMENT (multipart_mux), + gst_multipart_mux_loop); +} + +static GstPadLinkReturn +gst_multipart_mux_sinkconnect (GstPad * pad, const GstCaps * vscaps) +{ + GstMultipartMux *multipart_mux; + GstMultipartPad *mppad; + GstStructure *structure; + + multipart_mux = GST_MULTIPART_MUX (gst_pad_get_parent (pad)); + + mppad = (GstMultipartPad *) gst_pad_get_element_private (pad); + + GST_DEBUG ("multipart_mux: sinkconnect triggered on %s", + gst_pad_get_name (pad)); + + structure = gst_caps_get_structure (vscaps, 0); + mppad->mimetype = gst_structure_get_name (structure); + + return GST_PAD_LINK_OK; +} + +static void +gst_multipart_mux_pad_link (GstPad * pad, GstPad * peer, gpointer data) +{ + //GstMultipartMux *multipart_mux = GST_MULTIPART_MUX (data); + const gchar *padname = gst_pad_get_name (pad); + + GST_DEBUG ("pad '%s' connected", padname); +} + +static void +gst_multipart_mux_pad_unlink (GstPad * pad, GstPad * peer, gpointer data) +{ + //GstMultipartMux *multipart_mux = GST_MULTIPART_MUX (data); + const gchar *padname = gst_pad_get_name (pad); + + GST_DEBUG ("pad '%s' unlinked", padname); +} + +static GstPad * +gst_multipart_mux_request_new_pad (GstElement * element, + GstPadTemplate * templ, const gchar * req_name) +{ + GstMultipartMux *multipart_mux; + GstPad *newpad; + GstElementClass *klass = GST_ELEMENT_GET_CLASS (element); + + g_return_val_if_fail (templ != NULL, NULL); + + if (templ->direction != GST_PAD_SINK) { + g_warning ("multipart_mux: request pad that is not a SINK pad\n"); + return NULL; + } + + g_return_val_if_fail (GST_IS_MULTIPART_MUX (element), NULL); + + multipart_mux = GST_MULTIPART_MUX (element); + + if (templ == gst_element_class_get_pad_template (klass, "sink_%d")) { + gchar *name; + + /* create new pad with the name */ + name = g_strdup_printf ("sink_%02d", multipart_mux->numpads); + newpad = gst_pad_new_from_template (templ, name); + g_free (name); + + /* construct our own wrapper data structure for the pad to + * keep track of its status */ + { + GstMultipartPad *multipartpad = g_new0 (GstMultipartPad, 1); + + multipartpad->pad = newpad; + multipartpad->eos = FALSE; + + /* save a pointer to our data in the pad */ + gst_pad_set_element_private (newpad, multipartpad); + /* store our data for the pad */ + multipart_mux->sinkpads = + g_slist_prepend (multipart_mux->sinkpads, multipartpad); + multipart_mux->numpads++; + } + } else { + g_warning ("multipart_mux: this is not our template!\n"); + return NULL; + } + + g_signal_connect (newpad, "linked", + G_CALLBACK (gst_multipart_mux_pad_link), (gpointer) multipart_mux); + g_signal_connect (newpad, "unlinked", + G_CALLBACK (gst_multipart_mux_pad_unlink), (gpointer) multipart_mux); + + /* setup some pad functions */ + gst_pad_set_link_function (newpad, gst_multipart_mux_sinkconnect); + gst_pad_set_event_mask_function (newpad, + gst_multipart_mux_get_sink_event_masks); + /* dd the pad to the element */ + gst_element_add_pad (element, newpad); + + return newpad; +} + +/* handle events */ +static gboolean +gst_multipart_mux_handle_src_event (GstPad * pad, GstEvent * event) +{ + GstMultipartMux *multipart_mux; + GstEventType type; + + multipart_mux = GST_MULTIPART_MUX (gst_pad_get_parent (pad)); + + type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN; + + switch (type) { + case GST_EVENT_SEEK: + /* disable seeking for now */ + return FALSE; + default: + break; + } + + return gst_pad_event_default (pad, event); +} + +static GstBuffer * +gst_multipart_mux_next_buffer (GstMultipartPad * pad) +{ + GstData *data = NULL; + + while (data == NULL) { + GST_LOG ("muxer: pulling %s:%s\n", GST_DEBUG_PAD_NAME (pad->pad)); + data = gst_pad_pull (pad->pad); + /* if it's an event, handle it */ + if (GST_IS_EVENT (data)) { + GstEventType type; + GstMultipartMux *multipart_mux; + GstEvent *event = GST_EVENT (data); + + multipart_mux = GST_MULTIPART_MUX (gst_pad_get_parent (pad->pad)); + type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN; + + switch (type) { + case GST_EVENT_EOS: + return NULL; + default: + gst_pad_event_default (pad->pad, event); + break; + } + data = NULL; + } + } + return GST_BUFFER (data); +} + +/* + * Given two pads, compare the buffers queued on it and return 0 if they have + * an equal priority, 1 if the new pad is better, -1 if the old pad is better + */ +static gint +gst_multipart_mux_compare_pads (GstMultipartMux * multipart_mux, + GstMultipartPad * old, GstMultipartPad * new) +{ + guint64 oldtime, newtime; + + /* if the old pad doesn't contain anything or is even NULL, return + * the new pad as best candidate and vice versa */ + if (old == NULL || old->buffer == NULL) + return 1; + if (new == NULL || new->buffer == NULL) + return -1; + + /* no timestamp on old buffer, it must go first */ + oldtime = GST_BUFFER_TIMESTAMP (old->buffer); + if (oldtime == GST_CLOCK_TIME_NONE) + return -1; + + /* no timestamp on new buffer, it must go first */ + newtime = GST_BUFFER_TIMESTAMP (new->buffer); + if (newtime == GST_CLOCK_TIME_NONE) + return 1; + + /* old buffer has higher timestamp, new one should go first */ + if (newtime < oldtime) + return 1; + /* new buffer has higher timestamp, old one should go first */ + else if (newtime > oldtime) + return -1; + + /* same priority if all of the above failed */ + return 0; +} + +/* make sure a buffer is queued on all pads, returns a pointer to an multipartpad + * that holds the best buffer or NULL when no pad was usable */ +static GstMultipartPad * +gst_multipart_mux_queue_pads (GstMultipartMux * multipart_mux) +{ + GstMultipartPad *bestpad = NULL; + GSList *walk; + + /* try to make sure we have a buffer from each usable pad first */ + walk = multipart_mux->sinkpads; + while (walk) { + GstMultipartPad *pad = (GstMultipartPad *) walk->data; + + walk = walk->next; + + /* try to get a new buffer for this pad if needed and possible */ + if (pad->buffer == NULL && GST_PAD_IS_USABLE (pad->pad)) { + pad->buffer = gst_multipart_mux_next_buffer (pad); + /* no next buffer, try another pad */ + if (pad->buffer == NULL) + continue; + } + + /* skip unusable pads */ + if (!GST_PAD_IS_USABLE (pad->pad)) + continue; + + /* we should have a buffer now, see if it is the best pad to + * pull on */ + if (pad->buffer != NULL) { + if (gst_multipart_mux_compare_pads (multipart_mux, bestpad, pad) > 0) { + bestpad = pad; + } + } + } + return bestpad; +} + +/* basic idea: + * + * 1) find a pad to pull on, this is done by pulling on all pads and + * looking at the buffers to decide which one should be muxed first. + * 2) push buffer on best pad, go to 1 + */ +static void +gst_multipart_mux_loop (GstElement * element) +{ + GstMultipartMux *mux; + + mux = GST_MULTIPART_MUX (element); + + /* if we don't know which pad to pull on, find one */ + if (mux->pulling == NULL) { + mux->pulling = gst_multipart_mux_queue_pads (mux); + /* remember timestamp of first buffer for this new pad */ + if (mux->pulling != NULL) { + mux->next_ts = GST_BUFFER_TIMESTAMP (mux->pulling->buffer); + } else { + /* no pad to pull on, send EOS */ + if (GST_PAD_IS_USABLE (mux->srcpad)) + gst_pad_push (mux->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS))); + gst_element_set_eos (element); + return; + } + } + + /* we are pulling from a pad, continue to do so until a page + * has been filled and pushed */ + if (mux->pulling != NULL) { + GstBuffer *buf, *tmpbuf; + GstMultipartPad *pad = mux->pulling; + GstBuffer *newbuf; + gchar *header; + gint headerlen; + + /* now see if we have a buffer */ + buf = pad->buffer; + if (buf == NULL) { + /* no buffer, get one */ + buf = gst_multipart_mux_next_buffer (pad); + /* data exhausted on this pad (EOS) */ + if (buf == NULL) { + /* stop pulling from the pad */ + mux->pulling = NULL; + return; + } + } + + /* read ahead one more buffer to find EOS */ + tmpbuf = gst_multipart_mux_next_buffer (pad); + /* data exhausted on this pad */ + if (tmpbuf == NULL) { + /* stop pulling from the pad */ + mux->pulling = NULL; + } + + header = g_strdup_printf ("\n--ThisRandomString\nContent-type: %s\n\n", + pad->mimetype); + headerlen = strlen (header); + newbuf = + gst_pad_alloc_buffer (mux->srcpad, GST_BUFFER_OFFSET_NONE, headerlen); + GST_BUFFER_DATA (newbuf) = header; + GST_BUFFER_SIZE (newbuf) = headerlen; + GST_BUFFER_TIMESTAMP (newbuf) = GST_BUFFER_TIMESTAMP (buf); + gst_pad_push (mux->srcpad, GST_DATA (newbuf)); + gst_pad_push (mux->srcpad, GST_DATA (buf)); + + /* store new readahead buffer */ + pad->buffer = tmpbuf; + + /* we're done pulling on this pad, make sure to choose a new + * pad for pulling in the next iteration */ + mux->pulling = NULL; + } +} + +static void +gst_multipart_mux_get_property (GObject * object, + guint prop_id, GValue * value, GParamSpec * pspec) +{ + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gst_multipart_mux_set_property (GObject * object, + guint prop_id, const GValue * value, GParamSpec * pspec) +{ + switch (prop_id) { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static GstElementStateReturn +gst_multipart_mux_change_state (GstElement * element) +{ + GstMultipartMux *multipart_mux; + gint transition = GST_STATE_TRANSITION (element); + + g_return_val_if_fail (GST_IS_MULTIPART_MUX (element), GST_STATE_FAILURE); + + multipart_mux = GST_MULTIPART_MUX (element); + + switch (transition) { + case GST_STATE_NULL_TO_READY: + case GST_STATE_READY_TO_PAUSED: + multipart_mux->next_ts = 0; + multipart_mux->offset = 0; + multipart_mux->pulling = NULL; + break; + case GST_STATE_PAUSED_TO_PLAYING: + case GST_STATE_PLAYING_TO_PAUSED: + case GST_STATE_PAUSED_TO_READY: + case GST_STATE_READY_TO_NULL: + break; + } + + if (GST_ELEMENT_CLASS (parent_class)->change_state) + return GST_ELEMENT_CLASS (parent_class)->change_state (element); + + return GST_STATE_SUCCESS; +} + +gboolean +gst_multipart_mux_plugin_init (GstPlugin * plugin) +{ + GST_DEBUG_CATEGORY_INIT (gst_multipart_mux_debug, "multipartmux", 0, + "multipart muxer"); + + return gst_element_register (plugin, "multipartmux", GST_RANK_PRIMARY, + GST_TYPE_MULTIPART_MUX); +} |