Add Annodex elements from Alessendro Decina: skeleton and CMML.

Original commit message from CVS:
* configure.ac:
* docs/plugins/gst-plugins-good-plugins-sections.txt:
* ext/Makefile.am:
* ext/annodex/Makefile.am:
* ext/annodex/gstannodex.c:
* ext/annodex/gstannodex.h:
* ext/annodex/gstcmmldec.c:
* ext/annodex/gstcmmldec.h:
* ext/annodex/gstcmmlenc.c:
* ext/annodex/gstcmmlenc.h:
* ext/annodex/gstcmmlparser.c:
* ext/annodex/gstcmmlparser.h:
* ext/annodex/gstcmmltag.c:
* ext/annodex/gstcmmltag.h:
* ext/annodex/gstcmmlutils.c:
* ext/annodex/gstcmmlutils.h:
* ext/annodex/gstskeldec.c:
* ext/annodex/gstskeldec.h:
* ext/annodex/gstskeltag.c:
* ext/annodex/gstskeltag.h:
* tests/check/Makefile.am:
* tests/check/elements/cmmldec.c:
* tests/check/elements/cmmlenc.c:
* tests/check/elements/skeldec.c:
Add Annodex elements from Alessendro Decina: skeleton and CMML.
Includes tests & docs, oh my! Passes Thomas's -good checklist
entirely. Wow.
This commit is contained in:
Michael Smith 2006-02-24 19:07:10 +00:00
parent dd8f37faac
commit 73ce8c5b9e
25 changed files with 5537 additions and 1 deletions

View file

@ -1,3 +1,33 @@
2006-02-24 Michael Smith <msmith@fluendo.com>
* configure.ac:
* docs/plugins/gst-plugins-good-plugins-sections.txt:
* ext/Makefile.am:
* ext/annodex/Makefile.am:
* ext/annodex/gstannodex.c:
* ext/annodex/gstannodex.h:
* ext/annodex/gstcmmldec.c:
* ext/annodex/gstcmmldec.h:
* ext/annodex/gstcmmlenc.c:
* ext/annodex/gstcmmlenc.h:
* ext/annodex/gstcmmlparser.c:
* ext/annodex/gstcmmlparser.h:
* ext/annodex/gstcmmltag.c:
* ext/annodex/gstcmmltag.h:
* ext/annodex/gstcmmlutils.c:
* ext/annodex/gstcmmlutils.h:
* ext/annodex/gstskeldec.c:
* ext/annodex/gstskeldec.h:
* ext/annodex/gstskeltag.c:
* ext/annodex/gstskeltag.h:
* tests/check/Makefile.am:
* tests/check/elements/cmmldec.c:
* tests/check/elements/cmmlenc.c:
* tests/check/elements/skeldec.c:
Add Annodex elements from Alessendro Decina: skeleton and CMML.
Includes tests & docs, oh my! Passes Thomas's -good checklist
entirely. Wow.
2006-02-24 Michael Smith <msmith@fluendo.com>
* autogen.sh:

View file

@ -133,6 +133,8 @@ dnl common/m4/gst-arch.m4
dnl check CPU type
GST_ARCH
AC_SUBST(GST_CTRL_LIBS)
dnl Determine endianness
AC_C_BIGENDIAN
@ -326,6 +328,18 @@ GST_CHECK_FEATURE(AALIB, [aasink plug-in], aasink, [
AS_SCRUB_INCLUDE(AALIB_CFLAGS)
])
dnl *** annodex ***
translit(dnm, m, l) AM_CONDITIONAL(USE_ANNODEX, true)
GST_CHECK_FEATURE(ANNODEX, [annodex plug-in], skeldec cmmlenc cmmldec, [
PKG_CHECK_MODULES(XML, libxml-2.0 >= 2.4.9, [
HAVE_ANNODEX=yes
AC_SUBST(XML_CFLAGS)
AC_SUBST(XML_LIBS)
], [
HAVE_ANNODEX=no
])
])
dnl *** cairo ***
translit(dnm, m, l) AM_CONDITIONAL(USE_CAIRO, true)
GST_CHECK_FEATURE(CAIRO, [cairo plug-in], cairo, [
@ -618,6 +632,7 @@ gst/flx/Makefile
ext/jpeg/Makefile
ext/Makefile
ext/aalib/Makefile
ext/annodex/Makefile
ext/cairo/Makefile
ext/cdio/Makefile
ext/dv/Makefile

View file

@ -30,6 +30,14 @@ GstCairoTimeOverlay
GstCairoTimeOverlayClass
</SECTION>
<SECTION>
<FILE>element-cmmldec</FILE>
GstCmmlDec
<TITLE>cmmldec</TITLE>
<SUBSECTION Standard>
GstCmmlDecClass
</SECTION>
<SECTION>
<FILE>element-cdiocddasrc</FILE>
GstCdioCddaSrc

View file

@ -4,6 +4,12 @@ else
AALIB_DIR =
endif
if USE_ANNODEX
ANNODEX_DIR = annodex
else
ANNODEX_DIR =
endif
if USE_CAIRO
CAIRO_DIR = cairo
else
@ -108,6 +114,7 @@ endif
SUBDIRS = \
$(AALIB_DIR) \
$(ANNODEX_DIR) \
$(CAIRO_DIR) \
$(CDIO_DIR) \
$(DV1394_DIR) \

27
ext/annodex/Makefile.am Normal file
View file

@ -0,0 +1,27 @@
# plugindir is set in configure
# change libgstplugin.la to something more suitable
plugin_LTLIBRARIES = libgstannodex.la
# for the next set of variables, rename the prefix if you renamed the .la
# sources used to compile this plug-in
libgstannodex_la_SOURCES = \
gstannodex.c \
gstcmmlutils.c \
gstcmmldec.c \
gstcmmlenc.c \
gstcmmltag.c \
gstcmmlparser.c \
gstskeldec.c \
gstskeltag.c
# flags used to compile this plugin
# we use the GST_LIBS flags because we might be using plug-in libs
libgstannodex_la_CFLAGS = $(GST_CFLAGS) $(XML_CFLAGS)
libgstannodex_la_LIBADD = $(GST_BASE_LIBS) $(GST_LIBS) $(XML_LIBS)
libgstannodex_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
# headers we need but don't want installed
noinst_HEADERS = gstannodex.h gstcmmlutils.h gstcmmltag.h gstcmmlparser.h \
gstcmmldec.h gstskeldec.h gstcmmlenc.h

164
ext/annodex/gstannodex.c Normal file
View file

@ -0,0 +1,164 @@
/*
* gstannodex.c - GStreamer annodex plugin
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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 "gstcmmlenc.h"
#include "gstcmmldec.h"
#include "gstskeldec.h"
GstClockTime
gst_annodex_granule_to_time (gint64 granulepos, gint64 granulerate_n,
gint64 granulerate_d, guint8 granuleshift)
{
gint64 keyindex, keyoffset;
gint64 granulerate;
GstClockTime res;
if (granulepos == -1)
return GST_CLOCK_TIME_NONE;
if (granulepos == 0 || granulerate_n == 0 || granulerate_d == 0)
return 0;
if (granuleshift != 0) {
keyindex = granulepos >> granuleshift;
keyoffset = granulepos - (keyindex << granuleshift);
granulepos = keyindex + keyoffset;
}
/* GST_SECOND / granulerate_n / granulerate_d */
granulerate = gst_util_uint64_scale (GST_SECOND,
granulerate_n, granulerate_d);
/* granulepos * granulerate */
res = gst_util_uint64_scale (granulepos, granulerate, 1);
return res;
}
GValueArray *
gst_annodex_parse_headers (const gchar * headers)
{
GValueArray *array;
GValue val = { 0 };
gchar *header_name = NULL;
gchar *header_value = NULL;
gchar *line, *column, *space, *tmp;
gchar **lines;
gint i = 0;
array = g_value_array_new (0);
g_value_init (&val, G_TYPE_STRING);
lines = g_strsplit (headers, "\r\n", 0);
line = lines[i];
while (line != NULL && *line != '\0') {
if (line[0] == '\t' || line[0] == ' ') {
/* WSP: continuation line */
if (header_value == NULL)
/* continuation line without a previous value */
goto fail;
tmp = g_strjoin (" ", header_value, g_strstrip (line), NULL);
g_free (header_value);
header_value = tmp;
} else {
if (header_name) {
g_value_take_string (&val, header_name);
g_value_array_append (array, &val);
g_value_take_string (&val, header_value);
g_value_array_append (array, &val);
}
/* search the column starting from line[1] as an header name can't be
* empty */
column = g_strstr_len (line + 1, strlen (line) - 1, ":");
if (column == NULL)
/* bad syntax */
goto fail;
if (*(space = column + 1) != ' ')
/* bad syntax */
goto fail;
header_name = g_strndup (line, column - line);
header_value = g_strdup (space + 1);
}
line = lines[++i];
}
if (header_name) {
g_value_take_string (&val, header_name);
g_value_array_append (array, &val);
g_value_take_string (&val, header_value);
g_value_array_append (array, &val);
}
g_value_unset (&val);
g_strfreev (lines);
return array;
fail:
GST_WARNING ("could not parse annodex headers");
g_free (header_name);
g_free (header_value);
g_strfreev (lines);
g_value_array_free (array);
g_value_unset (&val);
return NULL;
}
static gboolean
plugin_init (GstPlugin * plugin)
{
gst_tag_register (GST_TAG_CMML_STREAM, GST_TAG_FLAG_META,
GST_TYPE_CMML_TAG_STREAM, "cmml-stream", "annodex CMML stream tag", NULL);
gst_tag_register (GST_TAG_CMML_HEAD, GST_TAG_FLAG_META,
GST_TYPE_CMML_TAG_HEAD, "cmml-head", "annodex CMML head tag", NULL);
gst_tag_register (GST_TAG_CMML_CLIP, GST_TAG_FLAG_META,
GST_TYPE_CMML_TAG_CLIP, "cmml-clip", "annodex CMML clip tag", NULL);
if (!gst_cmml_enc_plugin_init (plugin))
return FALSE;
if (!gst_cmml_dec_plugin_init (plugin))
return FALSE;
if (!gst_skel_dec_plugin_init (plugin))
return FALSE;
return TRUE;
}
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"annodex",
"annodex stream manipulation (info about annodex: http://www.annodex.net)",
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)

34
ext/annodex/gstannodex.h Normal file
View file

@ -0,0 +1,34 @@
/*
* gstannodex.h - GStreamer annodex utility functions
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#ifndef __GST_ANNODEX_H__
#define __GST_ANNODEX_H__
#include <gst/gst.h>
GstClockTime gst_annodex_granule_to_time (gint64 granulepos,
gint64 granulerate_n, gint64 granulerate_d, guint8 granuleshift);
gchar *gst_annodex_time_to_npt (GstClockTime time);
GValueArray *gst_annodex_parse_headers (const gchar * headers);
#endif /* __GST_ANNODEX_H__ */

677
ext/annodex/gstcmmldec.c Normal file
View file

@ -0,0 +1,677 @@
/*
* gstcmmldec.c - GStreamer annodex CMML decoder
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
/**
* SECTION:element-cmmldec
* @see_also: cmmlenc, oggdemux
*
* <refsect2>
* <para>
* Cmmldec extracts a CMML document from a CMML bitstream.<ulink
* url="http://www.annodex.net/TR/draft-pfeiffer-cmml-02.html">CMML</ulink> is
* an XML markup language for time-continuous data maintained by the <ulink
* url="http:/www.annodex.org/">Annodex Foundation</ulink>.
* </para>
* <title>Example pipeline</title>
* <programlisting>
* gst-launch -v filesrc location=annotated.ogg ! oggdemux ! cmmldec ! filesink location=annotations.cmml
* </programlisting>
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <glib.h>
#include "gstannodex.h"
#include "gstcmmltag.h"
#include "gstcmmldec.h"
#include "gstcmmlutils.h"
GST_DEBUG_CATEGORY (cmmldec);
#define GST_CAT_DEFAULT cmmldec
#define CMML_IDENT_HEADER_SIZE 29
enum
{
ARG_0,
GST_CMML_DEC_WAIT_CLIP_END
};
enum
{
LAST_SIGNAL
};
static GstElementDetails gst_cmml_dec_details = {
"cmmldec: Decodes CMML streams", "Codec/Decoder",
"Decodes CMML streams",
"Alessandro Decina <alessandro@nnva.org>",
};
static GstStaticPadTemplate gst_cmml_dec_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("text/xml")
);
static GstStaticPadTemplate gst_cmml_dec_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("text/x-cmml")
);
/* GstCmmlDec */
GST_BOILERPLATE (GstCmmlDec, gst_cmml_dec, GstElement, GST_TYPE_ELEMENT);
static void gst_cmml_dec_get_property (GObject * dec, guint property_id,
GValue * value, GParamSpec * pspec);
static void gst_cmml_dec_set_property (GObject * dec, guint property_id,
const GValue * value, GParamSpec * pspec);
static const GstQueryType *gst_cmml_dec_query_types (GstPad * pad);
static gboolean gst_cmml_dec_sink_query (GstPad * pad, GstQuery * query);
static gboolean gst_cmml_dec_sink_event (GstPad * pad, GstEvent * event);
static gboolean gst_cmml_dec_convert (GstPad * pad, GstFormat src_fmt,
gint64 src_val, GstFormat * dest_fmt, gint64 * dest_val);
static GstStateChangeReturn gst_cmml_dec_change_state (GstElement * element,
GstStateChange transition);
static GstFlowReturn gst_cmml_dec_chain (GstPad * pad, GstBuffer * buffer);
static GstCmmlPacketType gst_cmml_dec_parse_packet_type (GstCmmlDec * dec,
GstBuffer * buffer);
static void gst_cmml_dec_parse_ident_header (GstCmmlDec * dec, GstBuffer * buf);
static void gst_cmml_dec_parse_first_header (GstCmmlDec * dec, GstBuffer * buf);
static void gst_cmml_dec_parse_preamble (GstCmmlDec * dec,
guchar * preamble, guchar * cmml_root_element);
static void gst_cmml_dec_parse_xml (GstCmmlDec * dec,
guchar * data, guint size);
static void gst_cmml_dec_parse_head (GstCmmlDec * dec, GstCmmlTagHead * head);
static void gst_cmml_dec_parse_clip (GstCmmlDec * dec, GstCmmlTagClip * clip);
static GstFlowReturn gst_cmml_dec_new_buffer (GstCmmlDec * dec,
guchar * data, gint size, GstBuffer ** buffer);
static void gst_cmml_dec_push_clip (GstCmmlDec * dec, GstCmmlTagClip * clip);
static void gst_cmml_dec_send_clip_tag (GstCmmlDec * dec,
GstCmmlTagClip * clip);
static void
gst_cmml_dec_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 (&gst_cmml_dec_sink_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_cmml_dec_src_factory));
gst_element_class_set_details (element_class, &gst_cmml_dec_details);
}
static void
gst_cmml_dec_class_init (GstCmmlDecClass * dec_class)
{
GObjectClass *klass = G_OBJECT_CLASS (dec_class);
GST_ELEMENT_CLASS (klass)->change_state = gst_cmml_dec_change_state;
klass->set_property = gst_cmml_dec_set_property;
klass->get_property = gst_cmml_dec_get_property;
g_object_class_install_property (klass, GST_CMML_DEC_WAIT_CLIP_END,
g_param_spec_boolean ("wait-clip-end-time",
"Wait clip end time",
"Send a tag for a clip when the clip ends, setting its end-time. "
"Use when you need to know both clip's start-time and end-time.",
FALSE, G_PARAM_READWRITE));
}
static void
gst_cmml_dec_init (GstCmmlDec * dec, GstCmmlDecClass * klass)
{
dec->sinkpad =
gst_pad_new_from_static_template (&gst_cmml_dec_sink_factory, "sink");
gst_pad_set_chain_function (dec->sinkpad, gst_cmml_dec_chain);
gst_pad_set_query_type_function (dec->sinkpad, gst_cmml_dec_query_types);
gst_pad_set_query_function (dec->sinkpad, gst_cmml_dec_sink_query);
gst_pad_set_event_function (dec->sinkpad, gst_cmml_dec_sink_event);
gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
dec->srcpad =
gst_pad_new_from_static_template (&gst_cmml_dec_src_factory, "src");
gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
dec->wait_clip_end = FALSE;
}
static void
gst_cmml_dec_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstCmmlDec *dec = GST_CMML_DEC (object);
switch (property_id) {
case GST_CMML_DEC_WAIT_CLIP_END:
g_value_set_boolean (value, dec->wait_clip_end);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gst_cmml_dec_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstCmmlDec *dec = GST_CMML_DEC (object);
switch (property_id) {
case GST_CMML_DEC_WAIT_CLIP_END:
dec->wait_clip_end = g_value_get_boolean (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (dec, property_id, pspec);
}
}
static GstStateChangeReturn
gst_cmml_dec_change_state (GstElement * element, GstStateChange transition)
{
GstCmmlDec *dec = GST_CMML_DEC (element);
GstStateChangeReturn res;
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
dec->parser = gst_cmml_parser_new (GST_CMML_PARSER_DECODE);
dec->parser->user_data = dec;
dec->parser->preamble_callback =
(GstCmmlParserPreambleCallback) gst_cmml_dec_parse_preamble;
dec->parser->head_callback =
(GstCmmlParserHeadCallback) gst_cmml_dec_parse_head;
dec->parser->clip_callback =
(GstCmmlParserClipCallback) gst_cmml_dec_parse_clip;
dec->major = -1;
dec->minor = -1;
dec->granulerate_n = -1;
dec->granulerate_d = -1;
dec->granuleshift = 0;
dec->granulepos = 0;
dec->flow_return = GST_FLOW_OK;
dec->sent_root = FALSE;
dec->tracks = gst_cmml_track_list_new ();
break;
default:
break;
}
res = parent_class->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
gst_cmml_parser_free (dec->parser);
gst_cmml_track_list_destroy (dec->tracks);
break;
default:
break;
}
return res;
}
static const GstQueryType *
gst_cmml_dec_query_types (GstPad * pad)
{
static const GstQueryType query_types[] = {
GST_QUERY_CONVERT,
0
};
return query_types;
}
static gboolean
gst_cmml_dec_sink_query (GstPad * pad, GstQuery * query)
{
gboolean res = FALSE;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONVERT:
{
GstFormat src_fmt, dest_fmt;
gint64 src_val, dest_val;
gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
res = gst_cmml_dec_convert (pad, src_fmt, src_val, &dest_fmt, &dest_val);
if (res)
gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
break;
}
default:
break;
}
return res;
}
static gboolean
gst_cmml_dec_convert (GstPad * pad,
GstFormat src_fmt, gint64 src_val, GstFormat * dest_fmt, gint64 * dest_val)
{
GstCmmlDec *dec = GST_CMML_DEC (GST_PAD_PARENT (pad));
gboolean res = FALSE;
switch (src_fmt) {
case GST_FORMAT_DEFAULT:
switch (*dest_fmt) {
case GST_FORMAT_TIME:
{
*dest_val = gst_annodex_granule_to_time (src_val, dec->granulerate_n,
dec->granulerate_d, dec->granuleshift);
res = TRUE;
break;
}
default:
break;
}
break;
default:
break;
}
return res;
}
static gboolean
gst_cmml_dec_sink_event (GstPad * pad, GstEvent * event)
{
GstCmmlDec *dec = GST_CMML_DEC (GST_PAD_PARENT (pad));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_EOS:
{
GstBuffer *buffer;
GstCmmlTagClip *clip;
GList *clips, *walk;
GST_INFO_OBJECT (dec, "got EOS, flushing clips");
/* since we output a clip when the next one in the same track is found, on
* EOS we need to output the last clip (if any) of every track
*/
clips = gst_cmml_track_list_get_clips (dec->tracks);
for (walk = clips; walk; walk = g_list_next (walk)) {
clip = GST_CMML_TAG_CLIP (walk->data);
gst_cmml_dec_push_clip (dec, clip);
if (dec->wait_clip_end) {
clip->end_time = dec->timestamp;
gst_cmml_dec_send_clip_tag (dec, clip);
}
}
g_list_free (clips);
/* send the cmml end tag */
dec->flow_return = gst_cmml_dec_new_buffer (dec,
(guchar *) "</cmml>", strlen ("</cmml>"), &buffer);
if (dec->flow_return == GST_FLOW_OK)
dec->flow_return = gst_pad_push (dec->srcpad, buffer);
break;
}
default:
break;
}
return gst_pad_event_default (pad, event);
}
static GstFlowReturn
gst_cmml_dec_chain (GstPad * pad, GstBuffer * buffer)
{
GstCmmlDec *dec = GST_CMML_DEC (GST_PAD_PARENT (pad));
GstCmmlPacketType packet;
if (GST_BUFFER_SIZE (buffer) == 0) {
/* the EOS page could be empty */
dec->flow_return = GST_FLOW_OK;
goto done;
}
dec->granulepos = GST_BUFFER_OFFSET_END (buffer);
dec->timestamp = gst_annodex_granule_to_time (dec->granulepos,
dec->granulerate_n, dec->granulerate_d, dec->granuleshift);
/* identify the packet type */
packet = gst_cmml_dec_parse_packet_type (dec, buffer);
/* handle the packet. the handler will set dec->flow_return */
switch (packet) {
case GST_CMML_PACKET_IDENT_HEADER:
gst_cmml_dec_parse_ident_header (dec, buffer);
break;
case GST_CMML_PACKET_FIRST_HEADER:
gst_cmml_dec_parse_first_header (dec, buffer);
break;
case GST_CMML_PACKET_SECOND_HEADER:
case GST_CMML_PACKET_CLIP:
gst_cmml_dec_parse_xml (dec,
GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
break;
case GST_CMML_PACKET_UNKNOWN:
default:
GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("unknown packet type"));
dec->flow_return = GST_FLOW_ERROR;
}
done:
gst_buffer_unref (buffer);
return dec->flow_return;
}
/* finds the packet type of the buffer
*/
static GstCmmlPacketType
gst_cmml_dec_parse_packet_type (GstCmmlDec * dec, GstBuffer * buffer)
{
GstCmmlPacketType packet_type = GST_CMML_PACKET_UNKNOWN;
gchar *data = (gchar *) GST_BUFFER_DATA (buffer);
guint size = GST_BUFFER_SIZE (buffer);
if (size >= 8 && !memcmp (data, "CMML\0\0\0\0", 8)) {
packet_type = GST_CMML_PACKET_IDENT_HEADER;
} else if (size >= 5) {
if (!strncmp (data, "<?xml", 5))
packet_type = GST_CMML_PACKET_FIRST_HEADER;
else if (!strncmp (data, "<head", 5))
packet_type = GST_CMML_PACKET_SECOND_HEADER;
else if (!strncmp (data, "<clip", 5))
packet_type = GST_CMML_PACKET_CLIP;
}
return packet_type;
}
/* creates a new buffer and sets caps and timestamp on it
*/
static GstFlowReturn
gst_cmml_dec_new_buffer (GstCmmlDec * dec,
guchar * data, gint size, GstBuffer ** buffer)
{
GstFlowReturn res;
res = gst_pad_alloc_buffer (dec->srcpad, GST_BUFFER_OFFSET_NONE,
size, gst_static_pad_template_get_caps (&gst_cmml_dec_src_factory),
buffer);
if (res == GST_FLOW_OK) {
if (data)
memcpy (GST_BUFFER_DATA (*buffer), data, size);
GST_BUFFER_TIMESTAMP (*buffer) = dec->timestamp;
}
return res;
}
/* parses the first CMML packet (the ident header)
*/
static void
gst_cmml_dec_parse_ident_header (GstCmmlDec * dec, GstBuffer * buffer)
{
guint8 *data = GST_BUFFER_DATA (buffer);
/* the ident header has a fixed length */
if (GST_BUFFER_SIZE (buffer) != CMML_IDENT_HEADER_SIZE) {
GST_ELEMENT_ERROR (dec, STREAM, DECODE,
(NULL), ("wrong ident header size: %d", GST_BUFFER_SIZE (buffer)));
dec->flow_return = GST_FLOW_ERROR;
return;
}
data += 8;
dec->major = GST_READ_UINT16_LE (data);
data += 2;
dec->minor = GST_READ_UINT16_LE (data);
data += 2;
dec->granulerate_n = GST_READ_UINT64_LE (data);
data += 8;
dec->granulerate_d = GST_READ_UINT64_LE (data);
data += 8;
dec->granuleshift = GST_READ_UINT8 (data);
GST_INFO_OBJECT (dec, "bitstream initialized "
"(major: %" G_GINT16_FORMAT " minor: %" G_GINT16_FORMAT
" granulerate_n: %" G_GINT64_FORMAT " granulerate_d: %" G_GINT64_FORMAT
" granuleshift: %d)",
dec->major, dec->minor,
dec->granulerate_n, dec->granulerate_d, dec->granuleshift);
dec->flow_return = GST_FLOW_OK;
}
/* parses the first secondary header.
* the first secondary header contains the xml version, the doctype and the
* optional "cmml" processing instruction.
*/
static void
gst_cmml_dec_parse_first_header (GstCmmlDec * dec, GstBuffer * buffer)
{
gst_cmml_dec_parse_xml (dec,
GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer));
/* if there is a processing instruction, gst_cmml_dec_parse_preamble
* will be triggered. Otherwise we need to call it manually.
*/
if (!GST_FLOW_IS_FATAL (dec->flow_return) && !dec->sent_root) {
gst_cmml_dec_parse_preamble (dec, GST_BUFFER_DATA (buffer),
(guchar *) "<cmml>");
}
}
/* feeds data into the cmml parser.
*/
static void
gst_cmml_dec_parse_xml (GstCmmlDec * dec, guchar * data, guint size)
{
GError *err = NULL;
if (!gst_cmml_parser_parse_chunk (dec->parser, (gchar *) data, size, &err)) {
GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), (err->message));
g_error_free (err);
dec->flow_return = GST_FLOW_ERROR;
}
}
static void
gst_cmml_dec_parse_preamble (GstCmmlDec * dec, guchar * preamble,
guchar * root_element)
{
GstBuffer *buffer;
guchar *encoded_preamble;
encoded_preamble = (guchar *) g_strconcat ((gchar *) preamble,
(gchar *) root_element, NULL);
/* send the root element to the internal parser */
gst_cmml_dec_parse_xml (dec, root_element, strlen ((gchar *) root_element));
/* push the root element */
dec->flow_return = gst_cmml_dec_new_buffer (dec,
encoded_preamble, strlen ((gchar *) encoded_preamble), &buffer);
if (dec->flow_return != GST_FLOW_OK)
goto done;
dec->flow_return = gst_pad_push (dec->srcpad, buffer);
if (!GST_FLOW_IS_FATAL (dec->flow_return)) {
GST_INFO_OBJECT (dec, "preamble parsed");
dec->sent_root = TRUE;
}
done:
g_free (encoded_preamble);
return;
}
/* outputs the cmml head element and send TITLE and CMML_HEAD tags.
* This callback is registered with dec->parser. It is called when the
* head element is parsed.
*/
static void
gst_cmml_dec_parse_head (GstCmmlDec * dec, GstCmmlTagHead * head)
{
GstTagList *tags;
GValue str_val = { 0 }, title_val = {
0};
guchar *head_str;
GstBuffer *buffer;
GST_DEBUG_OBJECT (dec, "found CMML head (title: %s base: %s)",
head->title, head->base);
/* create the GST_TAG_TITLE tag */
g_value_init (&str_val, G_TYPE_STRING);
g_value_init (&title_val, gst_tag_get_type (GST_TAG_TITLE));
g_value_set_string (&str_val, (gchar *) head->title);
g_value_transform (&str_val, &title_val);
tags = gst_tag_list_new ();
gst_tag_list_add_values (tags, GST_TAG_MERGE_APPEND,
GST_TAG_TITLE, &title_val, NULL);
gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_CMML_HEAD, head, NULL);
gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, tags);
g_value_unset (&str_val);
g_value_unset (&title_val);
head_str = gst_cmml_parser_tag_head_to_string (dec->parser, head);
dec->flow_return = gst_cmml_dec_new_buffer (dec,
head_str, strlen ((gchar *) head_str), &buffer);
g_free (head_str);
if (dec->flow_return == GST_FLOW_OK)
dec->flow_return = gst_pad_push (dec->srcpad, buffer);
}
/* send a TAG_MESSAGE event for a clip */
static void
gst_cmml_dec_send_clip_tag (GstCmmlDec * dec, GstCmmlTagClip * clip)
{
GstTagList *tags;
GST_DEBUG_OBJECT (dec, "sending clip tag %s", clip->id);
tags = gst_tag_list_new ();
gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_CMML_CLIP, clip, NULL);
gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, tags);
}
/* push the string representation of a clip */
static void
gst_cmml_dec_push_clip (GstCmmlDec * dec, GstCmmlTagClip * clip)
{
GstBuffer *buffer;
guchar *clip_str;
GST_DEBUG_OBJECT (dec, "pushing clip %s", clip->id);
clip_str = gst_cmml_parser_tag_clip_to_string (dec->parser, clip);
dec->flow_return = gst_cmml_dec_new_buffer (dec,
clip_str, strlen ((gchar *) clip_str), &buffer);
if (dec->flow_return == GST_FLOW_OK)
dec->flow_return = gst_pad_push (dec->srcpad, buffer);
g_free (clip_str);
}
/* decode a clip tag
* this callback is registered with dec->parser. It is called whenever a
* clip is parsed.
*/
static void
gst_cmml_dec_parse_clip (GstCmmlDec * dec, GstCmmlTagClip * clip)
{
GstCmmlTagClip *prev_clip;
dec->flow_return = GST_FLOW_OK;
if (clip->empty)
GST_INFO_OBJECT (dec, "parsing empty clip");
else
GST_INFO_OBJECT (dec, "parsing clip (id: %s)", clip->id);
clip->start_time = dec->timestamp;
if (clip->start_time == GST_CLOCK_TIME_NONE) {
GST_ELEMENT_ERROR (dec, STREAM, DECODE,
(NULL), ("invalid clip start time"));
dec->flow_return = GST_FLOW_ERROR;
return;
}
/* get the last clip in the current track */
prev_clip = gst_cmml_track_list_get_track_last_clip (dec->tracks,
(gchar *) clip->track);
if (prev_clip) {
/* output the previous clip */
if (clip->empty)
/* the current clip marks the end of the previous one */
prev_clip->end_time = clip->start_time;
gst_cmml_dec_push_clip (dec, prev_clip);
}
if (dec->wait_clip_end) {
/* now it's time to send the tag for the previous clip */
if (prev_clip) {
prev_clip->end_time = clip->start_time;
gst_cmml_dec_send_clip_tag (dec, prev_clip);
}
} else if (!clip->empty) {
/* send the tag for the current clip */
gst_cmml_dec_send_clip_tag (dec, clip);
}
if (prev_clip)
gst_cmml_track_list_del_clip (dec->tracks, prev_clip);
if (!clip->empty)
if (!gst_cmml_track_list_has_clip (dec->tracks, clip))
gst_cmml_track_list_add_clip (dec->tracks, clip);
}
gboolean
gst_cmml_dec_plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "cmmldec", GST_RANK_PRIMARY,
GST_TYPE_CMML_DEC))
return FALSE;
GST_DEBUG_CATEGORY_INIT (cmmldec, "cmmldec", 0,
"annodex CMML decoding element");
return TRUE;
}

96
ext/annodex/gstcmmldec.h Normal file
View file

@ -0,0 +1,96 @@
/*
* gstcmmldec.h - GStreamer annodex CMML decoder
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#ifndef __GST_CMML_DEC_H__
#define __GST_CMML_DEC_H__
#include <gst/gst.h>
#include <gst/gstformat.h>
#include <gst/controller/gstcontroller.h>
#include "gstcmmlparser.h"
/* GstCmmlDec */
#define GST_TYPE_CMML_DEC (gst_cmml_dec_get_type())
#define GST_CMML_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_CMML_DEC, GstCmmlDec))
#define GST_CMML_DEC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_CMML_DEC, GstCmmlDec))
#define GST_IS_CMML_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_CMML_DEC))
#define GST_IS_CMML_DEC_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_CMML_DEC))
#define GST_CMML_DEC_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_CMML_DEC, GstCmmlDecClass))
typedef struct _GstCmmlDec GstCmmlDec;
typedef struct _GstCmmlDecClass GstCmmlDecClass;
typedef enum _GstCmmlPacketType GstCmmlPacketType;
enum _GstCmmlPacketType
{
GST_CMML_PACKET_UNKNOWN,
GST_CMML_PACKET_IDENT_HEADER,
GST_CMML_PACKET_FIRST_HEADER,
GST_CMML_PACKET_SECOND_HEADER,
GST_CMML_PACKET_CLIP
};
struct _GstCmmlDec
{
GstElement element;
/* element part */
GstPad *sinkpad;
GstPad *srcpad;
/* bitstream part */
gint16 major; /* bitstream version major */
gint16 minor; /* bitstream version minor */
gint64 granulerate_n; /* bitrstream granulerate numerator */
gint64 granulerate_d; /* bitstream granulerate denominator */
gint8 granuleshift; /* bitstreamgranuleshift */
gint64 granulepos; /* bitstream granule position */
GstClockTime timestamp; /* timestamp of the last buffer */
/* decoder part */
GstCmmlParser *parser; /* cmml parser */
gboolean sent_root;
GstFlowReturn flow_return; /* _chain return value */
gboolean wait_clip_end; /* when TRUE, the GST_TAG_MESSAGE for a
* clip is sent when the next clip (or EOS)
* is found, so that the clip end-time is
* known. This is useful for pre-extracting
* the clips.
*/
GHashTable *tracks;
};
struct _GstCmmlDecClass
{
GstElementClass parent_class;
};
gboolean gst_cmml_dec_plugin_init (GstPlugin * plugin);
#endif /* __GST_CMML_DEC_H__ */

624
ext/annodex/gstcmmlenc.c Normal file
View file

@ -0,0 +1,624 @@
/*
* gstcmmlenc.c - GStreamer CMML encoder
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
/**
* SECTION:element-cmmlenc
* @see_also: cmmldec, oggmux
*
* <refsect2>
* <para> Cmmlenc encodes a CMML document into a CMML stream. <ulink
* url="http://www.annodex.net/TR/draft-pfeiffer-cmml-02.html">CMML</ulink> is
* an XML markup language for time-continuous data maintained by the <ulink
* url="http:/www.annodex.org/">Annodex Foundation</ulink>.
* </para>
* <title>Example pipeline</title>
* <programlisting>
* gst-launch -v filesrc location=annotations.cmml ! cmmlenc ! oggmux name=mux ! filesink location=annotated.ogg
* </programlisting>
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include "gstcmmlenc.h"
#include "gstannodex.h"
GST_DEBUG_CATEGORY (cmmlenc);
#define GST_CAT_DEFAULT cmmlenc
#define CMML_IDENT_HEADER_SIZE 29
enum
{
ARG_0,
GST_CMML_ENC_GRANULERATE_N,
GST_CMML_ENC_GRANULERATE_D,
GST_CMML_ENC_GRANULESHIFT
};
enum
{
LAST_SIGNAL
};
static GstElementDetails gst_cmml_enc_details = {
"cmmlenc: Encodes CMML streams", "Codec/Encoder",
"Encodes CMML streams",
"Alessandro Decina <alessandro@nnva.org>",
};
static GstStaticPadTemplate gst_cmml_enc_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("text/x-cmml")
);
static GstStaticPadTemplate gst_cmml_enc_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("text/xml")
);
GST_BOILERPLATE (GstCmmlEnc, gst_cmml_enc, GstElement, GST_TYPE_ELEMENT);
static void gst_cmml_enc_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec);
static void gst_cmml_enc_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec);
static gboolean gst_cmml_enc_sink_event (GstPad * pad, GstEvent * event);
static GstStateChangeReturn gst_cmml_enc_change_state (GstElement * element,
GstStateChange transition);
static GstFlowReturn gst_cmml_enc_chain (GstPad * pad, GstBuffer * buffer);
static void gst_cmml_enc_parse_preamble (GstCmmlEnc * enc,
guchar * preamble, guchar * processing_instruction);
static void gst_cmml_enc_parse_end_tag (GstCmmlEnc * enc);
static void gst_cmml_enc_parse_tag_head (GstCmmlEnc * enc,
GstCmmlTagHead * head);
static void gst_cmml_enc_parse_tag_clip (GstCmmlEnc * enc,
GstCmmlTagClip * tag);
static GstFlowReturn gst_cmml_enc_new_buffer (GstCmmlEnc * enc,
guchar * data, gint size, GstBuffer ** buffer);
#if 0
static void gst_cmml_enc_flush_clips (GstCmmlEnc * enc);
#endif
static GstFlowReturn gst_cmml_enc_push_clip (GstCmmlEnc * enc,
GstCmmlTagClip * clip, GstClockTime prev_clip_time);
static GstFlowReturn gst_cmml_enc_push (GstCmmlEnc * enc, GstBuffer * buffer);
static void
gst_cmml_enc_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 (&gst_cmml_enc_sink_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_cmml_enc_src_factory));
gst_element_class_set_details (element_class, &gst_cmml_enc_details);
}
static void
gst_cmml_enc_class_init (GstCmmlEncClass * enc_class)
{
GObjectClass *klass = G_OBJECT_CLASS (enc_class);
klass->get_property = gst_cmml_enc_get_property;
klass->set_property = gst_cmml_enc_set_property;
g_object_class_install_property (klass, GST_CMML_ENC_GRANULERATE_N,
g_param_spec_int64 ("granule-rate-numerator",
"Granulerate numerator",
"Granulerate numerator",
0, G_MAXINT64, 1, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_ENC_GRANULERATE_D,
g_param_spec_int64 ("granule-rate-denominator",
"Granulerate denominator",
"Granulerate denominator",
0, G_MAXINT64, 1000, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_ENC_GRANULESHIFT,
g_param_spec_uchar ("granule-shift",
"Granuleshift",
"The number of lower bits to use for partitioning a granule position",
0, G_MAXUINT8, 32, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
GST_ELEMENT_CLASS (klass)->change_state = gst_cmml_enc_change_state;
}
static void
gst_cmml_enc_init (GstCmmlEnc * enc, GstCmmlEncClass * klass)
{
enc->sinkpad =
gst_pad_new_from_static_template (&gst_cmml_enc_sink_factory, "sink");
gst_pad_set_chain_function (enc->sinkpad, gst_cmml_enc_chain);
gst_pad_set_event_function (enc->sinkpad, gst_cmml_enc_sink_event);
gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
enc->srcpad =
gst_pad_new_from_static_template (&gst_cmml_enc_src_factory, "src");
gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
enc->major = 3;
enc->minor = 0;
}
static void
gst_cmml_enc_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstCmmlEnc *enc = GST_CMML_ENC (object);
switch (property_id) {
case GST_CMML_ENC_GRANULERATE_N:
/* XXX: may need to flush clips */
enc->granulerate_n = g_value_get_int64 (value);
break;
case GST_CMML_ENC_GRANULERATE_D:
enc->granulerate_d = g_value_get_int64 (value);
break;
case GST_CMML_ENC_GRANULESHIFT:
enc->granuleshift = g_value_get_uchar (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gst_cmml_enc_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstCmmlEnc *enc = GST_CMML_ENC (object);
switch (property_id) {
case GST_CMML_ENC_GRANULERATE_N:
g_value_set_int64 (value, enc->granulerate_n);
break;
case GST_CMML_ENC_GRANULERATE_D:
g_value_set_int64 (value, enc->granulerate_d);
break;
case GST_CMML_ENC_GRANULESHIFT:
g_value_set_uchar (value, enc->granuleshift);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static GstStateChangeReturn
gst_cmml_enc_change_state (GstElement * element, GstStateChange transition)
{
GstCmmlEnc *enc = GST_CMML_ENC (element);
GstStateChangeReturn res;
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
enc->parser = gst_cmml_parser_new (GST_CMML_PARSER_ENCODE);
enc->parser->user_data = enc;
enc->parser->preamble_callback =
(GstCmmlParserPreambleCallback) gst_cmml_enc_parse_preamble;
enc->parser->head_callback =
(GstCmmlParserHeadCallback) gst_cmml_enc_parse_tag_head;
enc->parser->clip_callback =
(GstCmmlParserClipCallback) gst_cmml_enc_parse_tag_clip;
enc->parser->cmml_end_callback =
(GstCmmlParserCmmlEndCallback) gst_cmml_enc_parse_end_tag;
enc->tracks = gst_cmml_track_list_new ();
enc->sent_headers = FALSE;
enc->sent_eos = FALSE;
enc->flow_return = GST_FLOW_OK;
break;
default:
break;
}
res = parent_class->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
{
gst_cmml_track_list_destroy (enc->tracks);
g_free (enc->preamble);
gst_cmml_parser_free (enc->parser);
break;
}
default:
break;
}
return res;
}
static gboolean
gst_cmml_enc_sink_event (GstPad * pad, GstEvent * event)
{
GstCmmlEnc *enc = GST_CMML_ENC (GST_PAD_PARENT (pad));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_EOS:
{
if (!enc->sent_eos)
gst_cmml_enc_parse_end_tag (enc);
break;
}
default:
break;
}
return gst_pad_event_default (pad, event);
}
static GstFlowReturn
gst_cmml_enc_new_buffer (GstCmmlEnc * enc,
guchar * data, gint size, GstBuffer ** buffer)
{
GstFlowReturn res;
res = gst_pad_alloc_buffer (enc->srcpad, GST_BUFFER_OFFSET_NONE, size,
NULL, buffer);
if (res == GST_FLOW_OK) {
if (data)
memcpy (GST_BUFFER_DATA (*buffer), data, size);
} else {
GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
(NULL), ("alloc function returned error %s", gst_flow_get_name (res)));
}
return res;
}
static GstCaps *
gst_cmml_enc_set_header_on_caps (GstCaps * caps,
GstBuffer * ident, GstBuffer * preamble, GstBuffer * head)
{
GValue array = { 0 };
GValue value = { 0 };
GstStructure *structure;
caps = gst_caps_make_writable (caps);
structure = gst_caps_get_structure (caps, 0);
g_value_init (&array, GST_TYPE_ARRAY);
g_value_init (&value, GST_TYPE_BUFFER);
gst_value_set_buffer (&value, ident);
gst_value_array_append_value (&array, &value);
gst_value_set_buffer (&value, preamble);
gst_value_array_append_value (&array, &value);
gst_value_set_buffer (&value, head);
gst_value_array_append_value (&array, &value);
GST_BUFFER_FLAG_SET (ident, GST_BUFFER_FLAG_IN_CAPS);
GST_BUFFER_FLAG_SET (preamble, GST_BUFFER_FLAG_IN_CAPS);
GST_BUFFER_FLAG_SET (head, GST_BUFFER_FLAG_IN_CAPS);
gst_structure_set_value (structure, "streamheader", &array);
g_value_unset (&value);
g_value_unset (&array);
return caps;
}
/* create a CMML ident header buffer
*/
static GstFlowReturn
gst_cmml_enc_new_ident_header (GstCmmlEnc * enc, GstBuffer ** buffer)
{
guint8 ident_header[CMML_IDENT_HEADER_SIZE];
guint8 *wptr = ident_header;
memcpy (wptr, "CMML\0\0\0\0", 8);
wptr += 8;
GST_WRITE_UINT16_LE (wptr, enc->major);
wptr += 2;
GST_WRITE_UINT16_LE (wptr, enc->minor);
wptr += 2;
GST_WRITE_UINT64_LE (wptr, enc->granulerate_n);
wptr += 8;
GST_WRITE_UINT64_LE (wptr, enc->granulerate_d);
wptr += 8;
*wptr = enc->granuleshift;
return gst_cmml_enc_new_buffer (enc,
(guchar *) & ident_header, CMML_IDENT_HEADER_SIZE, buffer);
}
/* parse the CMML preamble */
static void
gst_cmml_enc_parse_preamble (GstCmmlEnc * enc,
guchar * preamble, guchar * processing_instruction)
{
GST_INFO_OBJECT (enc, "parsing preamble");
/* save the preamble: it will be pushed when the head tag is found */
enc->preamble = (guchar *) g_strconcat ((gchar *) preamble,
(gchar *) processing_instruction, NULL);
}
/* parse the CMML end tag */
static void
gst_cmml_enc_parse_end_tag (GstCmmlEnc * enc)
{
GstBuffer *buffer;
GST_INFO_OBJECT (enc, "parsing end tag");
#if 0
if (!enc->streaming)
gst_cmml_enc_flush_clips (enc);
if (GST_FLOW_IS_FATAL (enc->flow_return))
goto done;
#endif
/* push an empty buffer to signal EOS */
enc->flow_return = gst_cmml_enc_new_buffer (enc, NULL, 0, &buffer);
if (enc->flow_return == GST_FLOW_OK) {
/* set granulepos 0 on EOS */
GST_BUFFER_OFFSET_END (buffer) = 0;
enc->flow_return = gst_cmml_enc_push (enc, buffer);
enc->sent_eos = TRUE;
}
return;
}
/* encode the CMML head tag and push the CMML headers
*/
static void
gst_cmml_enc_parse_tag_head (GstCmmlEnc * enc, GstCmmlTagHead * head)
{
GList *headers = NULL;
GList *walk;
guchar *head_string;
GstCaps *caps;
GstBuffer *ident_buf, *preamble_buf, *head_buf;
GstBuffer *buffer;
if (enc->preamble == NULL)
goto flow_unexpected;
GST_INFO_OBJECT (enc, "parsing head tag");
enc->flow_return = gst_cmml_enc_new_ident_header (enc, &ident_buf);
if (enc->flow_return != GST_FLOW_OK)
goto alloc_error;
headers = g_list_append (headers, ident_buf);
enc->flow_return = gst_cmml_enc_new_buffer (enc,
enc->preamble, strlen ((gchar *) enc->preamble), &preamble_buf);
if (enc->flow_return != GST_FLOW_OK)
goto alloc_error;
headers = g_list_append (headers, preamble_buf);
head_string = gst_cmml_parser_tag_head_to_string (enc->parser, head);
enc->flow_return = gst_cmml_enc_new_buffer (enc,
head_string, strlen ((gchar *) head_string), &head_buf);
g_free (head_string);
if (enc->flow_return != GST_FLOW_OK)
goto alloc_error;
headers = g_list_append (headers, head_buf);
caps = gst_pad_get_caps (enc->srcpad);
/* this call unrefs caps and creates new caps */
caps = gst_cmml_enc_set_header_on_caps (caps,
ident_buf, preamble_buf, head_buf);
for (walk = headers; walk; walk = walk->next) {
buffer = GST_BUFFER (walk->data);
/* set granulepos 0 on headers */
GST_BUFFER_OFFSET_END (buffer) = 0;
gst_buffer_set_caps (buffer, caps);
enc->flow_return = gst_cmml_enc_push (enc, buffer);
if (GST_FLOW_IS_FATAL (enc->flow_return))
goto push_error;
}
gst_caps_unref (caps);
g_list_free (headers);
enc->sent_headers = TRUE;
return;
flow_unexpected:
GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
(NULL), ("got head tag before preamble"));
enc->flow_return = GST_FLOW_UNEXPECTED;
return;
push_error:
gst_caps_unref (caps);
/* fallthrough */
alloc_error:
for (walk = headers; walk; walk = walk->next)
gst_buffer_unref (GST_BUFFER (walk->data));
g_list_free (headers);
return;
}
#if 0
static void
gst_cmml_enc_flush_clips (GstCmmlEnc * enc)
{
GList *clips, *walk;
clips = gst_cmml_track_list_get_clips (enc->tracks);
for (walk = clips; walk; walk = g_list_next (walk)) {
enc->flow_return = gst_cmml_enc_push_clip (enc,
GST_CMML_TAG_CLIP (walk->data));
if (!GST_FLOW_IS_FATAL (enc->flow_return))
break;
}
}
#endif
/* encode a CMML clip tag
* remove the start and end attributes (GstCmmlParser does this itself) and
* push the tag with the timestamp of its start attribute. If the tag has the
* end attribute, create a new empty clip and encode it.
*/
static void
gst_cmml_enc_parse_tag_clip (GstCmmlEnc * enc, GstCmmlTagClip * clip)
{
GstCmmlTagClip *prev_clip;
GstClockTime prev_clip_time = GST_CLOCK_TIME_NONE;
/* this can happen if there's a programming error (eg user forgets to set
* the start-time property) or if one of the gst_cmml_clock_time_from_*
* overflows in the GstCmmlParser code */
if (clip->start_time == GST_CLOCK_TIME_NONE) {
GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
(NULL), ("invalid start time for clip (%s)", clip->id));
enc->flow_return = GST_FLOW_ERROR;
return;
}
prev_clip = gst_cmml_track_list_get_track_last_clip (enc->tracks,
(gchar *) clip->track);
if (prev_clip) {
prev_clip_time = prev_clip->start_time;
gst_cmml_track_list_del_clip (enc->tracks, prev_clip);
}
/* add the current clip to the tracklist */
gst_cmml_track_list_add_clip (enc->tracks, clip);
enc->flow_return = gst_cmml_enc_push_clip (enc, clip, prev_clip_time);
}
static GstFlowReturn
gst_cmml_enc_push_clip (GstCmmlEnc * enc, GstCmmlTagClip * clip,
GstClockTime prev_clip_time)
{
GstFlowReturn res;
GstBuffer *buffer;
gchar *clip_string;
gint64 granulepos;
if (prev_clip_time != GST_CLOCK_TIME_NONE &&
prev_clip_time > clip->start_time) {
GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
(NULL), ("previous clip start time > current clip (%s) start time",
clip->id));
return GST_FLOW_ERROR;
}
/* encode the clip */
clip_string =
(gchar *) gst_cmml_parser_tag_clip_to_string (enc->parser, clip);
res = gst_cmml_enc_new_buffer (enc,
(guchar *) clip_string, strlen (clip_string), &buffer);
g_free (clip_string);
if (res != GST_FLOW_OK)
goto done;
GST_INFO_OBJECT (enc, "encoding clip"
"(start-time: %" GST_TIME_FORMAT " end-time: %" GST_TIME_FORMAT,
GST_TIME_ARGS (clip->start_time), GST_TIME_ARGS (clip->end_time));
/* set the granulepos */
granulepos = gst_cmml_clock_time_to_granule (prev_clip_time, clip->start_time,
enc->granulerate_n, enc->granulerate_d, enc->granuleshift);
GST_BUFFER_OFFSET_END (buffer) = granulepos;
GST_BUFFER_TIMESTAMP (buffer) = clip->start_time;
res = gst_cmml_enc_push (enc, buffer);
if (GST_FLOW_IS_FATAL (res))
goto done;
if (clip->end_time != GST_CLOCK_TIME_NONE) {
/* create a new empty clip for the same cmml "track" starting at
* "end_time"
*/
GObject *end_clip = g_object_new (GST_TYPE_CMML_TAG_CLIP,
"start-time", clip->end_time, "track", clip->track, NULL);
/* encode the empty end clip */
gst_cmml_enc_push_clip (enc, GST_CMML_TAG_CLIP (end_clip),
clip->start_time);
g_object_unref (end_clip);
}
done:
return res;
}
static GstFlowReturn
gst_cmml_enc_push (GstCmmlEnc * enc, GstBuffer * buffer)
{
GstFlowReturn res;
/* FIXME: hack to make oggmux flush every cmml tag in its own page */
GST_BUFFER_DURATION (buffer) = G_MAXINT64;
res = gst_pad_push (enc->srcpad, buffer);
if (GST_FLOW_IS_FATAL (res))
GST_ELEMENT_ERROR (enc, STREAM, ENCODE,
(NULL), ("could not push the buffer: %s", gst_flow_get_name (res)));
return res;
}
static GstFlowReturn
gst_cmml_enc_chain (GstPad * pad, GstBuffer * buffer)
{
GError *err = NULL;
GstCmmlEnc *enc = GST_CMML_ENC (GST_PAD_PARENT (pad));
/* the CMML handlers registered with enc->parser will override this when
* encoding/pushing the buffers downstream
*/
enc->flow_return = GST_FLOW_OK;
if (!gst_cmml_parser_parse_chunk (enc->parser,
(gchar *) GST_BUFFER_DATA (buffer), GST_BUFFER_SIZE (buffer), &err)) {
GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL), (err->message));
g_error_free (err);
enc->flow_return = GST_FLOW_ERROR;
}
gst_buffer_unref (buffer);
return enc->flow_return;
}
gboolean
gst_cmml_enc_plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "cmmlenc", GST_RANK_NONE,
GST_TYPE_CMML_ENC))
return FALSE;
GST_DEBUG_CATEGORY_INIT (cmmlenc, "cmmlenc", 0,
"annodex cmml decoding element");
return TRUE;
}

77
ext/annodex/gstcmmlenc.h Normal file
View file

@ -0,0 +1,77 @@
/*
* gstcmmlenc.h - GStreamer CMML encoder
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Encina <alessandro@nnva.org>
*
* 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.
*/
#ifndef __GST_CMML_ENC_H__
#define __GST_CMML_ENC_H__
#define GST_TYPE_CMML_ENC (gst_cmml_enc_get_type())
#define GST_CMML_ENC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_CMML_ENC, GstCmmlEnc))
#define GST_CMML_ENC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_CMML_ENC, GstCmmlEnc))
#define GST_IS_CMML_ENC(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_CMML_ENC))
#define GST_IS_CMML_ENC_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_CMML_ENC))
#define GST_CMML_ENC_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_CMML_ENC, GstCmmlEncClass))
#include <glib.h>
#include <gst/gst.h>
#include "gstcmmlparser.h"
#include "gstcmmlutils.h"
typedef struct _GstCmmlEnc GstCmmlEnc;
typedef struct _GstCmmlEncClass GstCmmlEncClass;
struct _GstCmmlEnc
{
GstElement element;
GstPad *sinkpad;
GstPad *srcpad;
gint16 major;
gint16 minor;
gint64 granulerate_n;
gint64 granulerate_d;
gint8 granuleshift;
GstCmmlParser *parser;
gboolean streaming;
GHashTable *tracks;
GstFlowReturn flow_return;
guchar *preamble;
gboolean sent_headers;
gboolean sent_eos;
};
struct _GstCmmlEncClass
{
GstElementClass parent_class;
};
gboolean gst_cmml_enc_plugin_init (GstPlugin * plugin);
#endif /* __GST_CMML_ENC_H__ */

617
ext/annodex/gstcmmlparser.c Normal file
View file

@ -0,0 +1,617 @@
/*
* gstcmmlparser.c - GStreamer CMML document parser
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#include <string.h>
#include <gst/gst.h>
#include "gstcmmlparser.h"
#include "gstannodex.h"
#include "gstcmmlutils.h"
static xmlNodePtr gst_cmml_parser_new_node (GstCmmlParser * parser,
const gchar * name, ...);
static void
gst_cmml_parser_parse_start_element_ns (xmlParserCtxt * ctxt,
const xmlChar * name, const xmlChar * prefix, const xmlChar * URI,
int nb_preferences, const xmlChar ** namespaces,
int nb_attributes, int nb_defaulted, const xmlChar ** attributes);
static void gst_cmml_parser_parse_end_element_ns (xmlParserCtxt * ctxt,
const xmlChar * name, const xmlChar * prefix, const xmlChar * URI);
static void gst_cmml_parser_parse_processing_instruction (xmlParserCtxtPtr ctxt,
const xmlChar * target, const xmlChar * data);
static void gst_cmml_parser_meta_to_string (GstCmmlParser * parser,
xmlNodePtr parent, GValueArray * meta);
/* create a new CMML parser
*/
GstCmmlParser *
gst_cmml_parser_new (GstCmmlParserMode mode)
{
GstCmmlParser *parser = g_malloc (sizeof (GstCmmlParser));
parser->mode = mode;
parser->context = xmlCreatePushParserCtxt (NULL, NULL,
NULL, 0, "cmml-bitstream");
xmlCtxtUseOptions (parser->context, XML_PARSE_NONET | XML_PARSE_NOERROR);
parser->context->_private = parser;
parser->context->sax->startElementNs =
(startElementNsSAX2Func) gst_cmml_parser_parse_start_element_ns;
parser->context->sax->endElementNs =
(endElementNsSAX2Func) gst_cmml_parser_parse_end_element_ns;
parser->context->sax->processingInstruction = (processingInstructionSAXFunc)
gst_cmml_parser_parse_processing_instruction;
parser->preamble_callback = NULL;
parser->cmml_end_callback = NULL;
parser->stream_callback = NULL;
parser->head_callback = NULL;
parser->clip_callback = NULL;
parser->user_data = NULL;
return parser;
}
/* free a CMML parser instance
*/
void
gst_cmml_parser_free (GstCmmlParser * parser)
{
if (parser) {
xmlFreeDoc (parser->context->myDoc);
xmlFreeParserCtxt (parser->context);
g_free (parser);
}
}
/* parse an xml chunk
*
* returns false if the xml is invalid
*/
gboolean
gst_cmml_parser_parse_chunk (GstCmmlParser * parser,
const gchar * data, guint size, GError ** err)
{
gint xmlres;
xmlres = xmlParseChunk (parser->context, data, size, 0);
if (xmlres != XML_ERR_OK) {
xmlErrorPtr xml_error = xmlCtxtGetLastError (parser->context);
g_set_error (err,
GST_LIBRARY_ERROR, GST_LIBRARY_ERROR_FAILED, xml_error->message);
return FALSE;
}
return TRUE;
}
/* convert an xmlNodePtr to a string
*/
guchar *
gst_cmml_parser_node_to_string (GstCmmlParser * parser, xmlNodePtr node)
{
xmlBufferPtr xml_buffer;
xmlDocPtr doc;
guchar *str;
if (parser)
doc = parser->context->myDoc;
else
doc = NULL;
xml_buffer = xmlBufferCreate ();
xmlNodeDump (xml_buffer, doc, node, 0, 0);
str = xmlStrndup (xml_buffer->content, xml_buffer->use);
xmlBufferFree (xml_buffer);
return str;
}
guchar *
gst_cmml_parser_tag_stream_to_string (GstCmmlParser * parser,
GstCmmlTagStream * stream)
{
xmlNodePtr node;
xmlNodePtr import;
guchar *ret;
node = gst_cmml_parser_new_node (parser, "stream", NULL);
if (stream->timebase)
xmlSetProp (node, (xmlChar *) "timebase", stream->timebase);
if (stream->utc)
xmlSetProp (node, (xmlChar *) "utc", stream->utc);
if (stream->imports) {
gint i;
GValue *val;
for (i = 0; i < stream->imports->n_values; ++i) {
val = g_value_array_get_nth (stream->imports, i);
import = gst_cmml_parser_new_node (parser, "import",
"src", g_value_get_string (val), NULL);
xmlAddChild (node, import);
}
}
ret = gst_cmml_parser_node_to_string (parser, node);
xmlUnlinkNode (node);
xmlFreeNode (node);
return ret;
}
/* convert a GstCmmlTagHead to its string representation
*/
guchar *
gst_cmml_parser_tag_head_to_string (GstCmmlParser * parser,
GstCmmlTagHead * head)
{
xmlNodePtr node;
xmlNodePtr tmp;
guchar *ret;
node = gst_cmml_parser_new_node (parser, "head", NULL);
if (head->title) {
tmp = gst_cmml_parser_new_node (parser, "title", NULL);
xmlNodeSetContent (tmp, head->title);
xmlAddChild (node, tmp);
}
if (head->base) {
tmp = gst_cmml_parser_new_node (parser, "base", "uri", head->base, NULL);
xmlAddChild (node, tmp);
}
if (head->meta)
gst_cmml_parser_meta_to_string (parser, node, head->meta);
ret = gst_cmml_parser_node_to_string (parser, node);
xmlUnlinkNode (node);
xmlFreeNode (node);
return ret;
}
/* convert a GstCmmlTagClip to its string representation
*/
guchar *
gst_cmml_parser_tag_clip_to_string (GstCmmlParser * parser,
GstCmmlTagClip * clip)
{
xmlNodePtr node;
xmlNodePtr tmp;
guchar *ret;
node = gst_cmml_parser_new_node (parser, "clip",
"id", clip->id, "track", clip->track, NULL);
/* add the anchor element */
if (clip->anchor_href) {
tmp = gst_cmml_parser_new_node (parser, "a",
"href", clip->anchor_href, NULL);
if (clip->anchor_text)
xmlNodeSetContent (tmp, clip->anchor_text);
xmlAddChild (node, tmp);
}
/* add the img element */
if (clip->img_src) {
tmp = gst_cmml_parser_new_node (parser, "img",
"src", clip->img_src, "alt", clip->img_alt, NULL);
xmlAddChild (node, tmp);
}
/* add the desc element */
if (clip->desc_text) {
tmp = gst_cmml_parser_new_node (parser, "desc", NULL);
xmlNodeSetContent (tmp, clip->desc_text);
xmlAddChild (node, tmp);
}
/* add the meta elements */
if (clip->meta)
gst_cmml_parser_meta_to_string (parser, node, clip->meta);
if (parser->mode == GST_CMML_PARSER_DECODE) {
gchar *time_str;
time_str = gst_cmml_clock_time_to_npt (clip->start_time);
if (time_str == NULL)
goto fail;
xmlSetProp (node, (xmlChar *) "start", (xmlChar *) time_str);
g_free (time_str);
if (clip->end_time != GST_CLOCK_TIME_NONE) {
time_str = gst_cmml_clock_time_to_npt (clip->end_time);
if (time_str == NULL)
goto fail;
xmlSetProp (node, (xmlChar *) "end", (xmlChar *) time_str);
g_free (time_str);
}
}
ret = gst_cmml_parser_node_to_string (parser, node);
xmlUnlinkNode (node);
xmlFreeNode (node);
return ret;
fail:
xmlUnlinkNode (node);
xmlFreeNode (node);
return NULL;
}
guchar *
gst_cmml_parser_tag_object_to_string (GstCmmlParser * parser, GObject * tag)
{
guchar *tag_string = NULL;
GType tag_type = G_OBJECT_TYPE (tag);
if (tag_type == GST_TYPE_CMML_TAG_STREAM)
tag_string = gst_cmml_parser_tag_stream_to_string (parser,
GST_CMML_TAG_STREAM (tag));
else if (tag_type == GST_TYPE_CMML_TAG_HEAD)
tag_string = gst_cmml_parser_tag_head_to_string (parser,
GST_CMML_TAG_HEAD (tag));
else if (tag_type == GST_TYPE_CMML_TAG_CLIP)
tag_string = gst_cmml_parser_tag_clip_to_string (parser,
GST_CMML_TAG_CLIP (tag));
else
g_warning ("could not convert object to cmml");
return tag_string;
}
/*** private section ***/
/* create a new node
*
* helper to create a node and set its attributes
*/
static xmlNodePtr
gst_cmml_parser_new_node (GstCmmlParser * parser, const gchar * name, ...)
{
va_list args;
xmlNodePtr node;
xmlChar *prop_name, *prop_value;
node = xmlNewNode (NULL, (xmlChar *) name);
va_start (args, name);
prop_name = va_arg (args, xmlChar *);
while (prop_name != NULL) {
prop_value = va_arg (args, xmlChar *);
if (prop_value != NULL)
xmlSetProp (node, prop_name, prop_value);
prop_name = va_arg (args, xmlChar *);
}
va_end (args);
return node;
}
/* get the last node of the stream
*
* returns the last node at depth 1 (if any) or the root node
*/
static xmlNodePtr
gst_cmml_parser_get_last_element (GstCmmlParser * parser)
{
xmlNodePtr node;
node = xmlDocGetRootElement (parser->context->myDoc);
if (!node) {
g_warning ("no last cmml element");
return NULL;
}
if (node->children)
node = xmlGetLastChild (node);
return node;
}
static void
gst_cmml_parser_parse_preamble (GstCmmlParser * parser,
const guchar * attributes)
{
gchar *preamble;
gchar *element;
const gchar *version;
const gchar *encoding;
const gchar *standalone;
xmlDocPtr doc;
doc = parser->context->myDoc;
version = doc->version ? (gchar *) doc->version : "1.0";
encoding = doc->encoding ? (gchar *) doc->encoding : "UTF-8";
standalone = doc->standalone ? "yes" : "no";
preamble = g_strdup_printf ("<?xml version=\"%s\""
" encoding=\"%s\" standalone=\"%s\"?>\n"
"<!DOCTYPE cmml SYSTEM \"cmml.dtd\">\n", version, encoding, standalone);
if (attributes == NULL)
attributes = (guchar *) "";
if (parser->mode == GST_CMML_PARSER_ENCODE)
element = g_strdup_printf ("<?cmml %s?>", attributes);
else
element = g_strdup_printf ("<cmml %s>", attributes);
parser->preamble_callback (parser->user_data,
(guchar *) preamble, (guchar *) element);
g_free (preamble);
g_free (element);
}
/* parse the cmml stream tag */
static void
gst_cmml_parser_parse_stream (GstCmmlParser * parser, xmlNodePtr stream)
{
GstCmmlTagStream *stream_tag;
GValue str_val = { 0 };
xmlNodePtr walk;
guchar *timebase;
g_value_init (&str_val, G_TYPE_STRING);
/* read the timebase and utc attributes */
timebase = xmlGetProp (stream, (xmlChar *) "timebase");
if (timebase == NULL)
timebase = (guchar *) g_strdup ("0");
stream_tag = g_object_new (GST_TYPE_CMML_TAG_STREAM,
"timebase", timebase, NULL);
g_free (timebase);
stream_tag->utc = xmlGetProp (stream, (xmlChar *) "utc");
/* walk the children nodes */
for (walk = stream->children; walk; walk = walk->next) {
/* for every import tag add its src attribute to stream_tag->imports */
if (!xmlStrcmp (walk->name, (xmlChar *) "import")) {
g_value_take_string (&str_val,
(gchar *) xmlGetProp (walk, (xmlChar *) "src"));
if (stream_tag->imports == NULL)
stream_tag->imports = g_value_array_new (0);
g_value_array_append (stream_tag->imports, &str_val);
}
}
g_value_unset (&str_val);
parser->stream_callback (parser->user_data, stream_tag);
g_object_unref (stream_tag);
}
/* parse the cmml head tag */
static void
gst_cmml_parser_parse_head (GstCmmlParser * parser, xmlNodePtr head)
{
GstCmmlTagHead *head_tag;
xmlNodePtr walk;
GValue str_val = { 0 };
head_tag = g_object_new (GST_TYPE_CMML_TAG_HEAD, NULL);
g_value_init (&str_val, G_TYPE_STRING);
/* Parse the content of the node and setup the GST_TAG_CMML_HEAD tag.
* Create a GST_TAG_TITLE when we find the title element.
*/
for (walk = head->children; walk; walk = walk->next) {
if (!xmlStrcmp (walk->name, (xmlChar *) "title")) {
head_tag->title = xmlNodeGetContent (walk);
} else if (!xmlStrcmp (walk->name, (xmlChar *) "base")) {
head_tag->base = xmlGetProp (walk, (xmlChar *) "uri");
} else if (!xmlStrcmp (walk->name, (xmlChar *) "meta")) {
if (head_tag->meta == NULL)
head_tag->meta = g_value_array_new (0);
/* add a pair name, content to the meta value array */
g_value_take_string (&str_val,
(gchar *) xmlGetProp (walk, (xmlChar *) "name"));
g_value_array_append (head_tag->meta, &str_val);
g_value_take_string (&str_val,
(gchar *) xmlGetProp (walk, (xmlChar *) "content"));
g_value_array_append (head_tag->meta, &str_val);
}
}
g_value_unset (&str_val);
parser->head_callback (parser->user_data, head_tag);
g_object_unref (head_tag);
}
/* parse a cmml clip tag */
static void
gst_cmml_parser_parse_clip (GstCmmlParser * parser, xmlNodePtr clip)
{
GstCmmlTagClip *clip_tag;
GValue str_val = { 0 };
guchar *id, *track, *start, *end;
xmlNodePtr walk;
GstClockTime start_time = GST_CLOCK_TIME_NONE;
GstClockTime end_time = GST_CLOCK_TIME_NONE;
g_value_init (&str_val, G_TYPE_STRING);
id = xmlGetProp (clip, (xmlChar *) "id");
track = xmlGetProp (clip, (xmlChar *) "track");
start = xmlGetProp (clip, (xmlChar *) "start");
end = xmlGetProp (clip, (xmlChar *) "end");
if (start) {
if (!strncmp ((gchar *) start, "smpte", 5))
start_time = gst_cmml_clock_time_from_smpte ((gchar *) start);
else
start_time = gst_cmml_clock_time_from_npt ((gchar *) start);
}
if (end) {
if (!strncmp ((gchar *) end, "smpte", 5))
start_time = gst_cmml_clock_time_from_smpte ((gchar *) end);
else
end_time = gst_cmml_clock_time_from_npt ((gchar *) end);
}
if (track == NULL)
track = (guchar *) g_strdup ("default");
clip_tag = g_object_new (GST_TYPE_CMML_TAG_CLIP,
"id", id,
"track", track, "start-time", start_time, "end-time", end_time, NULL);
g_free (id);
g_free (track);
g_free (start);
g_free (end);
/* parse the children */
for (walk = clip->children; walk; walk = walk->next) {
/* the clip is not empty */
clip_tag->empty = FALSE;
if (!xmlStrcmp (walk->name, (xmlChar *) "a")) {
clip_tag->anchor_href = xmlGetProp (walk, (xmlChar *) "href");
clip_tag->anchor_text = xmlNodeGetContent (walk);
} else if (!xmlStrcmp (walk->name, (xmlChar *) "img")) {
clip_tag->img_src = xmlGetProp (walk, (xmlChar *) "src");
clip_tag->img_alt = xmlGetProp (walk, (xmlChar *) "alt");
} else if (!xmlStrcmp (walk->name, (xmlChar *) "desc")) {
clip_tag->desc_text = xmlNodeGetContent (walk);
} else if (!xmlStrcmp (walk->name, (xmlChar *) "meta")) {
if (clip_tag->meta == NULL)
clip_tag->meta = g_value_array_new (0);
/* add a pair name, content to the meta value array */
g_value_take_string (&str_val,
(char *) xmlGetProp (walk, (xmlChar *) "name"));
g_value_array_append (clip_tag->meta, &str_val);
g_value_take_string (&str_val,
(char *) xmlGetProp (walk, (xmlChar *) "content"));
g_value_array_append (clip_tag->meta, &str_val);
}
}
g_value_unset (&str_val);
parser->clip_callback (parser->user_data, clip_tag);
g_object_unref (clip_tag);
}
void
gst_cmml_parser_meta_to_string (GstCmmlParser * parser,
xmlNodePtr parent, GValueArray * array)
{
gint i;
xmlNodePtr node;
GValue *name, *content;
for (i = 0; i < array->n_values - 1; i += 2) {
name = g_value_array_get_nth (array, i);
content = g_value_array_get_nth (array, i + 1);
node = gst_cmml_parser_new_node (parser, "meta",
"name", g_value_get_string (name),
"content", g_value_get_string (content), NULL);
xmlAddChild (parent, node);
}
}
/* sax handler called when an element start tag is found
* this is used to parse the cmml start tag
*/
static void
gst_cmml_parser_parse_start_element_ns (xmlParserCtxt * ctxt,
const xmlChar * name, const xmlChar * prefix, const xmlChar * URI,
int nb_preferences, const xmlChar ** namespaces,
int nb_attributes, int nb_defaulted, const xmlChar ** attributes)
{
GstCmmlParser *parser = (GstCmmlParser *) ctxt->_private;
xmlSAX2StartElementNs (ctxt, name, prefix, URI, nb_preferences, namespaces,
nb_attributes, nb_defaulted, attributes);
if (parser->mode == GST_CMML_PARSER_ENCODE)
if (!xmlStrcmp (name, (xmlChar *) "cmml"))
if (parser->preamble_callback)
/* FIXME: parse attributes */
gst_cmml_parser_parse_preamble (parser, NULL);
}
/* sax processing instruction handler
* used to parse the cmml processing instruction
*/
static void
gst_cmml_parser_parse_processing_instruction (xmlParserCtxtPtr ctxt,
const xmlChar * target, const xmlChar * data)
{
GstCmmlParser *parser = (GstCmmlParser *) ctxt->_private;
xmlSAX2ProcessingInstruction (ctxt, target, data);
if (parser->mode == GST_CMML_PARSER_DECODE)
if (!xmlStrcmp (target, (xmlChar *) "cmml"))
if (parser->preamble_callback)
gst_cmml_parser_parse_preamble (parser, data);
}
/* sax handler called when an xml end tag is found
* used to parse the stream, head and clip nodes
*/
static void
gst_cmml_parser_parse_end_element_ns (xmlParserCtxt * ctxt,
const xmlChar * name, const xmlChar * prefix, const xmlChar * URI)
{
xmlNodePtr node;
GstCmmlParser *parser = (GstCmmlParser *) ctxt->_private;
xmlSAX2EndElementNs (ctxt, name, prefix, URI);
if (!xmlStrcmp (name, (xmlChar *) "clip")) {
if (parser->clip_callback) {
node = gst_cmml_parser_get_last_element (parser);
gst_cmml_parser_parse_clip (parser, node);
}
} else if (!xmlStrcmp (name, (xmlChar *) "cmml")) {
if (parser->cmml_end_callback)
parser->cmml_end_callback (parser->user_data);
} else if (!xmlStrcmp (name, (xmlChar *) "stream")) {
if (parser->stream_callback) {
node = gst_cmml_parser_get_last_element (parser);
gst_cmml_parser_parse_stream (parser, node);
}
} else if (!xmlStrcmp (name, (xmlChar *) "head")) {
if (parser->head_callback) {
node = gst_cmml_parser_get_last_element (parser);
gst_cmml_parser_parse_head (parser, node);
}
}
}

View file

@ -0,0 +1,90 @@
/*
* gstcmmlparser.h - GStreamer CMML document parser
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#ifndef __GST_CMML_PARSER_H__
#define __GST_CMML_PARSER_H__
#include <libxml/parser.h>
#include <glib.h>
#include "gstcmmltag.h"
typedef struct _GstCmmlParser GstCmmlParser;
typedef enum _GstCmmlParserMode GstCmmlParserMode;
typedef void (*GstCmmlParserPreambleCallback) (void *user_data,
const guchar * xml_preamble, const guchar * cmml_attrs);
typedef void (*GstCmmlParserCmmlEndCallback) (void *user_data);
typedef void (*GstCmmlParserStreamCallback) (void *user_data,
GstCmmlTagStream * stream);
typedef void (*GstCmmlParserHeadCallback) (void *user_data,
GstCmmlTagHead * head);
typedef void (*GstCmmlParserClipCallback) (void *user_data,
GstCmmlTagClip * clip);
enum _GstCmmlParserMode
{
GST_CMML_PARSER_ENCODE,
GST_CMML_PARSER_DECODE
};
struct _GstCmmlParser
{
GstCmmlParserMode mode;
xmlParserCtxtPtr context;
const gchar *preamble;
guint preamble_size;
void *user_data;
GstCmmlParserPreambleCallback preamble_callback;
GstCmmlParserStreamCallback stream_callback;
GstCmmlParserCmmlEndCallback cmml_end_callback;
GstCmmlParserHeadCallback head_callback;
GstCmmlParserClipCallback clip_callback;
};
GstCmmlParser *gst_cmml_parser_new (GstCmmlParserMode mode);
void gst_cmml_parser_free (GstCmmlParser * parser);
gboolean gst_cmml_parser_parse_chunk (GstCmmlParser * parser,
const gchar * data, guint size, GError ** error);
guchar *gst_cmml_parser_tag_stream_to_string (GstCmmlParser * parser,
GstCmmlTagStream * stream);
guchar *gst_cmml_parser_tag_head_to_string (GstCmmlParser * parser,
GstCmmlTagHead * head);
guchar *gst_cmml_parser_tag_clip_to_string (GstCmmlParser * parser,
GstCmmlTagClip * clip);
guchar *gst_cmml_parser_tag_object_to_string (GstCmmlParser * parser,
GObject * tag);
#endif /* __GST_CMML_PARSER_H__ */

565
ext/annodex/gstcmmltag.c Normal file
View file

@ -0,0 +1,565 @@
/*
* gstcmmltags.c - GStreamer CMML tag support
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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 <string.h>
#include "gstcmmlparser.h"
#include "gstcmmltag.h"
#include "gstannodex.h"
enum
{
ARG_0,
GST_CMML_TAG_STREAM_TIMEBASE,
GST_CMML_TAG_STREAM_UTC,
GST_CMML_TAG_STREAM_IMPORTS,
GST_CMML_TAG_HEAD_TITLE,
GST_CMML_TAG_HEAD_BASE,
GST_CMML_TAG_HEAD_META,
GST_CMML_TAG_CLIP_EMPTY,
GST_CMML_TAG_CLIP_ID,
GST_CMML_TAG_CLIP_TRACK,
GST_CMML_TAG_CLIP_START_TIME,
GST_CMML_TAG_CLIP_END_TIME,
GST_CMML_TAG_CLIP_ANCHOR_HREF,
GST_CMML_TAG_CLIP_ANCHOR_TEXT,
GST_CMML_TAG_CLIP_IMG_SRC,
GST_CMML_TAG_CLIP_IMG_ALT,
GST_CMML_TAG_CLIP_DESC_TEXT,
GST_CMML_TAG_CLIP_META,
};
G_DEFINE_TYPE (GstCmmlTagStream, gst_cmml_tag_stream, G_TYPE_OBJECT);
static void gst_cmml_tag_stream_finalize (GObject * object);
static void gst_cmml_tag_stream_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_stream_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_stream_value_from_string_value (const GValue * src,
GValue * dest);
G_DEFINE_TYPE (GstCmmlTagHead, gst_cmml_tag_head, G_TYPE_OBJECT);
static void gst_cmml_tag_head_finalize (GObject * object);
static void gst_cmml_tag_head_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_head_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_head_value_from_string_value (const GValue * src,
GValue * dest);
G_DEFINE_TYPE (GstCmmlTagClip, gst_cmml_tag_clip, G_TYPE_OBJECT);
static void gst_cmml_tag_clip_finalize (GObject * object);
static void gst_cmml_tag_clip_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_clip_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec);
static void gst_cmml_tag_clip_value_from_string_value (const GValue * src,
GValue * dest);
static void set_object_on_value (GObject * object, GValue * dest);
static const gchar *default_preamble =
"<?xml version=\"1.0\" standalone=\"yes\"?>";
/* Stream tag */
static void
gst_cmml_tag_stream_class_init (GstCmmlTagStreamClass * stream_class)
{
GObjectClass *klass = G_OBJECT_CLASS (stream_class);
klass->set_property = gst_cmml_tag_stream_set_property;
klass->get_property = gst_cmml_tag_stream_get_property;
klass->finalize = gst_cmml_tag_stream_finalize;
g_object_class_install_property (klass, GST_CMML_TAG_STREAM_TIMEBASE,
g_param_spec_string ("base-time",
"Base time",
"Playback time (in seconds) of the first data packet",
"0", G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_STREAM_UTC,
g_param_spec_string ("calendar-base-time",
"Calendar base time",
"Date and wall-clock time (expressed as UTC time in the format "
"YYYYMMDDTHHMMSS.sssZ) associated with the base-time",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_STREAM_IMPORTS,
g_param_spec_value_array ("input-streams",
"Input streams",
"List of input streams that compose this bitstream",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_CMML_TAG_STREAM,
gst_cmml_tag_stream_value_from_string_value);
}
static void
gst_cmml_tag_stream_init (GstCmmlTagStream * stream)
{
}
static void
gst_cmml_tag_stream_finalize (GObject * object)
{
GstCmmlTagStream *stream = GST_CMML_TAG_STREAM (object);
g_free (stream->timebase);
g_free (stream->utc);
if (stream->imports)
g_value_array_free (stream->imports);
if (G_OBJECT_CLASS (gst_cmml_tag_stream_parent_class)->finalize)
G_OBJECT_CLASS (gst_cmml_tag_stream_parent_class)->finalize (object);
}
static void
gst_cmml_tag_stream_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstCmmlTagStream *stream = GST_CMML_TAG_STREAM (object);
switch (property_id) {
case GST_CMML_TAG_STREAM_TIMEBASE:
g_free (stream->timebase);
stream->timebase = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_STREAM_UTC:
g_free (stream->utc);
stream->utc = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_STREAM_IMPORTS:
{
GValueArray *va = g_value_get_boxed (value);
if (stream->imports)
g_value_array_free (stream->imports);
stream->imports = va != NULL ? g_value_array_copy (va) : NULL;
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gst_cmml_tag_stream_value_from_string_value (const GValue * src, GValue * dest)
{
GstCmmlParser *parser;
const gchar *str;
guint size;
parser = gst_cmml_parser_new (GST_CMML_PARSER_DECODE);
parser->user_data = dest;
parser->stream_callback = (GstCmmlParserStreamCallback) set_object_on_value;
gst_cmml_parser_parse_chunk (parser,
default_preamble, strlen (default_preamble), NULL);
str = g_value_get_string (src);
size = strlen (str);
gst_cmml_parser_parse_chunk (parser, str, size, NULL);
gst_cmml_parser_free (parser);
}
static void
gst_cmml_tag_stream_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstCmmlTagStream *stream = GST_CMML_TAG_STREAM (object);
switch (property_id) {
case GST_CMML_TAG_STREAM_TIMEBASE:
g_value_set_string (value, (gchar *) stream->timebase);
break;
case GST_CMML_TAG_STREAM_UTC:
g_value_set_string (value, (gchar *) stream->utc);
break;
case GST_CMML_TAG_STREAM_IMPORTS:
g_value_set_boxed (value, stream->imports);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
/* Head tag */
static void
gst_cmml_tag_head_class_init (GstCmmlTagHeadClass * head_class)
{
GObjectClass *klass = G_OBJECT_CLASS (head_class);
klass->set_property = gst_cmml_tag_head_set_property;
klass->get_property = gst_cmml_tag_head_get_property;
klass->finalize = gst_cmml_tag_head_finalize;
g_object_class_install_property (klass, GST_CMML_TAG_HEAD_TITLE,
g_param_spec_string ("title",
"Title",
"Title of the bitstream",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_HEAD_BASE,
g_param_spec_string ("base-uri",
"Base URI",
"Base URI of the bitstream. All relative URIs are relative to this",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_HEAD_META,
g_param_spec_value_array ("meta",
"Meta annotations",
"Meta annotations for the complete Annodex bitstream",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_CMML_TAG_HEAD,
gst_cmml_tag_head_value_from_string_value);
}
static void
gst_cmml_tag_head_init (GstCmmlTagHead * head)
{
}
static void
gst_cmml_tag_head_finalize (GObject * object)
{
GstCmmlTagHead *head = GST_CMML_TAG_HEAD (object);
g_free (head->title);
g_free (head->base);
if (head->meta)
g_value_array_free (head->meta);
if (G_OBJECT_CLASS (gst_cmml_tag_head_parent_class)->finalize)
G_OBJECT_CLASS (gst_cmml_tag_head_parent_class)->finalize (object);
}
static void
gst_cmml_tag_head_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstCmmlTagHead *head = GST_CMML_TAG_HEAD (object);
switch (property_id) {
case GST_CMML_TAG_HEAD_TITLE:
g_free (head->title);
head->title = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_HEAD_BASE:
g_free (head->base);
head->base = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_HEAD_META:
{
GValueArray *va = g_value_get_boxed (value);
if (head->meta)
g_value_array_free (head->meta);
head->meta = va != NULL ? g_value_array_copy (va) : NULL;
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gst_cmml_tag_head_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstCmmlTagHead *head = GST_CMML_TAG_HEAD (object);
switch (property_id) {
case GST_CMML_TAG_HEAD_TITLE:
g_value_set_string (value, (gchar *) head->title);
break;
case GST_CMML_TAG_HEAD_BASE:
g_value_set_string (value, (gchar *) head->base);
break;
case GST_CMML_TAG_HEAD_META:
g_value_set_boxed (value, head->meta);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gst_cmml_tag_head_value_from_string_value (const GValue * src, GValue * dest)
{
GstCmmlParser *parser;
const gchar *str;
guint size;
parser = gst_cmml_parser_new (GST_CMML_PARSER_DECODE);
parser->user_data = dest;
parser->head_callback = (GstCmmlParserHeadCallback) set_object_on_value;
gst_cmml_parser_parse_chunk (parser,
default_preamble, strlen (default_preamble), NULL);
str = g_value_get_string (src);
size = strlen (str);
gst_cmml_parser_parse_chunk (parser, str, size, NULL);
gst_cmml_parser_free (parser);
}
/* Clip tag */
static void
gst_cmml_tag_clip_class_init (GstCmmlTagClipClass * clip_class)
{
GObjectClass *klass = G_OBJECT_CLASS (clip_class);
klass->set_property = gst_cmml_tag_clip_set_property;
klass->get_property = gst_cmml_tag_clip_get_property;
klass->finalize = gst_cmml_tag_clip_finalize;
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_EMPTY,
g_param_spec_boolean ("empty",
"Empty clip flag",
"An empty clip only marks the end of the previous clip",
TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_ID,
g_param_spec_string ("id",
"Clip id",
"Id of the clip", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_TRACK,
g_param_spec_string ("track",
"Track number",
"The track this clip belongs to",
"default", G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_START_TIME,
g_param_spec_uint64 ("start-time",
"Start time",
"The start time (in seconds) of the clip",
0, G_MAXUINT64, GST_CLOCK_TIME_NONE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_END_TIME,
g_param_spec_uint64 ("end-time",
"End time",
"The end time (in seconds) of the clip (only set if extract-mode=true)",
0, G_MAXUINT64, GST_CLOCK_TIME_NONE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_ANCHOR_HREF,
g_param_spec_string ("anchor-uri",
"Anchor URI",
"The location of a Web resource closely connected to the clip",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_ANCHOR_TEXT,
g_param_spec_string ("anchor-text",
"Anchor text",
"A short description of the resource pointed by anchor-uri",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_IMG_SRC,
g_param_spec_string ("img-uri",
"Image URI",
"The URI of a representative image for the clip",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_IMG_ALT,
g_param_spec_string ("img-alt",
"Image alternative text",
"Alternative text to be displayed instead of the image "
"specified in img-uri", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_DESC_TEXT,
g_param_spec_string ("description",
"Description",
"A textual description of the content of the clip",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_CMML_TAG_CLIP_META,
g_param_spec_value_array ("meta",
"Meta annotations",
"Meta annotations for the clip",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_value_register_transform_func (G_TYPE_STRING, GST_TYPE_CMML_TAG_CLIP,
gst_cmml_tag_clip_value_from_string_value);
}
static void
gst_cmml_tag_clip_init (GstCmmlTagClip * clip)
{
}
static void
gst_cmml_tag_clip_finalize (GObject * object)
{
GstCmmlTagClip *clip = GST_CMML_TAG_CLIP (object);
g_free (clip->id);
g_free (clip->track);
g_free (clip->anchor_href);
g_free (clip->anchor_text);
g_free (clip->img_src);
g_free (clip->img_alt);
g_free (clip->desc_text);
if (clip->meta)
g_value_array_free (clip->meta);
if (G_OBJECT_CLASS (gst_cmml_tag_clip_parent_class)->finalize)
G_OBJECT_CLASS (gst_cmml_tag_clip_parent_class)->finalize (object);
}
static void
gst_cmml_tag_clip_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstCmmlTagClip *clip = GST_CMML_TAG_CLIP (object);
switch (property_id) {
case GST_CMML_TAG_CLIP_EMPTY:
clip->empty = g_value_get_boolean (value);
break;
case GST_CMML_TAG_CLIP_ID:
g_free (clip->id);
clip->id = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_CLIP_TRACK:
g_free (clip->track);
clip->track = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_CLIP_START_TIME:
clip->start_time = g_value_get_uint64 (value);
break;
case GST_CMML_TAG_CLIP_END_TIME:
clip->end_time = g_value_get_uint64 (value);
break;
case GST_CMML_TAG_CLIP_ANCHOR_HREF:
g_free (clip->anchor_href);
clip->anchor_href = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_CLIP_ANCHOR_TEXT:
g_free (clip->anchor_text);
clip->anchor_text = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_CLIP_IMG_SRC:
g_free (clip->img_src);
clip->img_src = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_CLIP_IMG_ALT:
g_free (clip->img_alt);
clip->img_alt = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_CLIP_DESC_TEXT:
g_free (clip->desc_text);
clip->desc_text = (guchar *) g_value_dup_string (value);
break;
case GST_CMML_TAG_CLIP_META:
{
GValueArray *va = (GValueArray *) g_value_get_boxed (value);
if (clip->meta)
g_value_array_free (clip->meta);
clip->meta = va != NULL ? g_value_array_copy (va) : NULL;
break;
}
}
}
static void
gst_cmml_tag_clip_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstCmmlTagClip *clip = GST_CMML_TAG_CLIP (object);
switch (property_id) {
case GST_CMML_TAG_CLIP_EMPTY:
g_value_set_boolean (value, clip->empty);
break;
case GST_CMML_TAG_CLIP_ID:
g_value_set_string (value, (gchar *) clip->id);
break;
case GST_CMML_TAG_CLIP_TRACK:
g_value_set_string (value, (gchar *) clip->track);
break;
case GST_CMML_TAG_CLIP_START_TIME:
g_value_set_uint64 (value, clip->start_time);
break;
case GST_CMML_TAG_CLIP_END_TIME:
g_value_set_uint64 (value, clip->end_time);
break;
case GST_CMML_TAG_CLIP_ANCHOR_HREF:
g_value_set_string (value, (gchar *) clip->anchor_href);
break;
case GST_CMML_TAG_CLIP_ANCHOR_TEXT:
g_value_set_string (value, (gchar *) clip->anchor_text);
break;
case GST_CMML_TAG_CLIP_IMG_SRC:
g_value_set_string (value, (gchar *) clip->img_src);
break;
case GST_CMML_TAG_CLIP_IMG_ALT:
g_value_set_string (value, (gchar *) clip->img_alt);
break;
case GST_CMML_TAG_CLIP_DESC_TEXT:
g_value_set_string (value, (gchar *) clip->desc_text);
break;
case GST_CMML_TAG_CLIP_META:
g_value_set_boxed (value, clip->meta);
break;
}
}
static void
gst_cmml_tag_clip_value_from_string_value (const GValue * src, GValue * dest)
{
GstCmmlParser *parser;
const gchar *str;
guint size;
parser = gst_cmml_parser_new (GST_CMML_PARSER_DECODE);
parser->user_data = dest;
parser->clip_callback = (GstCmmlParserClipCallback) set_object_on_value;
gst_cmml_parser_parse_chunk (parser, default_preamble,
strlen (default_preamble), NULL);
str = g_value_get_string (src);
size = strlen (str);
gst_cmml_parser_parse_chunk (parser, str, size, NULL);
gst_cmml_parser_free (parser);
}
static void
set_object_on_value (GObject * tag, GValue * dest)
{
g_value_take_object (dest, tag);
}

131
ext/annodex/gstcmmltag.h Normal file
View file

@ -0,0 +1,131 @@
/*
* gstcmmltag.h - GStreamer annodex CMML tag support
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#ifndef __GST_CMML_TAG_H__
#define __GST_CMML_TAG_H__
#include <gst/gst.h>
/* GstCmmlTagStream */
#define GST_TYPE_CMML_TAG_STREAM (gst_cmml_tag_stream_get_type ())
#define GST_CMML_TAG_STREAM(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), \
GST_TYPE_CMML_TAG_STREAM, GstCmmlTagStream))
#define GST_CMML_TAG_STREAM_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_CMML_TAG_STREAM, GstCmmlTagStream))
#define GST_CMML_TAG_STREAM_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), \
GST_TYPE_CMML_TAG_STREAM, GstCmmlTagStreamClass))
/* GstCmmlTagHead */
#define GST_TYPE_CMML_TAG_HEAD (gst_cmml_tag_head_get_type ())
#define GST_CMML_TAG_HEAD(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_CMML_TAG_HEAD, GstCmmlTagHead))
#define GST_CMML_TAG_HEAD_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_CMML_TAG_HEAD, GstCmmlTagHead))
#define GST_CMML_TAG_HEAD_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), \
GST_TYPE_CMML_TAG_HEAD, GstCmmlTagHeadClass))
/* GstCmmlTagClip */
#define GST_TYPE_CMML_TAG_CLIP (gst_cmml_tag_clip_get_type ())
#define GST_CMML_TAG_CLIP(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_CMML_TAG_CLIP, GstCmmlTagClip))
#define GST_CMML_TAG_CLIP_CLASS(obj) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_CMML_TAG_CLIP, GstCmmlTagClip))
#define GST_CMML_TAG_CLIP_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_CMML_TAG_CLIP, GstCmmlTagClipClass))
typedef struct _GstCmmlTagStream GstCmmlTagStream;
typedef struct _GstCmmlTagStreamClass GstCmmlTagStreamClass;
typedef struct _GstCmmlTagHead GstCmmlTagHead;
typedef struct _GstCmmlTagHeadClass GstCmmlTagHeadClass;
typedef struct _GstCmmlTagClip GstCmmlTagClip;
typedef struct _GstCmmlTagClipClass GstCmmlTagClipClass;
struct _GstCmmlTagStream {
GObject object;
guchar *timebase;
guchar *utc;
GValueArray *imports;
};
struct _GstCmmlTagStreamClass {
GObjectClass parent_class;
};
struct _GstCmmlTagHead {
GObject object;
guchar *title; /* title of the media */
guchar *base;
GValueArray *meta; /* metadata attached to the media.
* The elements are positioned in key-value
* pairs ie (key, content, key2, content2,
* ...)
*/
};
struct _GstCmmlTagHeadClass {
GObjectClass parent_class;
};
struct _GstCmmlTagClip {
GObject object;
gboolean empty; /* empty flag. An empty clip marks the
* end of the previous clip.
*/
guchar *id; /* clip id */
guchar *track; /* clip track */
GstClockTime start_time; /* clip start time */
GstClockTime end_time; /* clip end time */
guchar *anchor_href; /* anchor href URI */
guchar *anchor_text; /* anchor text */
guchar *img_src; /* image URI */
guchar *img_alt; /* image alternative text */
guchar *desc_text; /* clip description */
GValueArray *meta; /* metadata attached to the clip
* The elements are positioned in key-value
* pairs ie (key, content, key2, content2,
* ...)
*/
};
struct _GstCmmlTagClipClass {
GObjectClass parent_class;
};
GType gst_cmml_tag_stream_get_type (void);
GType gst_cmml_tag_head_get_type (void);
GType gst_cmml_tag_clip_get_type (void);
#endif /* __GST_CMML_TAG_H__ */

376
ext/annodex/gstcmmlutils.c Normal file
View file

@ -0,0 +1,376 @@
/*
* gstcmmlutils.c - GStreamer CMML utility functions
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#include "gstcmmlutils.h"
#include <math.h>
#include <string.h>
typedef struct
{
GList *clips;
gpointer user_data;
} GstCmmlTrack;
GstClockTime
gst_cmml_clock_time_from_npt (const gchar * time)
{
GstClockTime res;
gint fields;
gint hours = 0;
gint minutes = 0;
gint seconds = 0;
gint mseconds = 0;
GstClockTime hours_t = 0, seconds_t = 0;
if (!strncmp (time, "npt:", 4))
time += 4;
/* parse npt-hhmmss */
fields = sscanf (time, "%d:%d:%d.%d", &hours, &minutes, &seconds, &mseconds);
if (fields == 4) {
if (hours < 0 || (guint) minutes > 59 || (guint) seconds > 59)
goto bad_input;
hours_t = gst_util_uint64_scale (hours, GST_SECOND * 3600, 1);
if (hours_t == G_MAXUINT64)
goto overflow;
seconds_t = seconds * GST_SECOND;
} else {
/* parse npt-sec */
hours_t = 0;
minutes = 0;
fields = sscanf (time, "%d.%d", &seconds, &mseconds);
if (seconds < 0)
goto bad_input;
seconds_t = gst_util_uint64_scale (seconds, GST_SECOND, 1);
if (seconds == G_MAXUINT64)
goto overflow;
}
if ((guint) mseconds > 999)
goto bad_input;
res = (minutes * 60) * GST_SECOND + mseconds * GST_MSECOND;
if (G_MAXUINT64 - hours_t - seconds_t < res)
goto overflow;
res += hours_t + seconds_t;
return res;
bad_input:
overflow:
return GST_CLOCK_TIME_NONE;
}
GstClockTime
gst_cmml_clock_time_from_smpte (const gchar * time)
{
GstClockTime res;
GstClockTime hours_t;
gint hours, minutes, seconds;
gdouble framerate;
gfloat frames;
gint fields;
if (!strncmp (time, "smpte-24:", 9)) {
framerate = 24.0;
time += 9;
} else if (!strncmp (time, "smpte-24-drop:", 14)) {
framerate = 23.976;
time += 14;
} else if (!strncmp (time, "smpte-25:", 9)) {
framerate = 25.0;
time += 9;
} else if (!strncmp (time, "smpte-30:", 9)) {
framerate = 30.0;
time += 9;
} else if (!strncmp (time, "smpte-30-drop:", 14)) {
framerate = 29.976;
time += 14;
} else if (!strncmp (time, "smpte-50:", 9)) {
framerate = 50.0;
time += 9;
} else if (!strncmp (time, "smpte-60:", 9)) {
framerate = 60.0;
time += 9;
} else if (!strncmp (time, "smpte-60-drop:", 14)) {
framerate = 59.94;
time += 14;
} else {
return GST_CLOCK_TIME_NONE;
}
fields = sscanf (time, "%d:%d:%d:%f", &hours, &minutes, &seconds, &frames);
if (fields == 4) {
if (hours < 0 || (guint) minutes > 59 || (guint) seconds > 59 ||
frames < 0 || frames > ceil (framerate)) {
res = GST_CLOCK_TIME_NONE;
} else {
hours_t = gst_util_uint64_scale (hours, GST_SECOND * 3600, 1);
if (hours_t == G_MAXUINT64)
goto overflow;
res = ((minutes * 60) + seconds + (frames / framerate))
* GST_SECOND;
if (G_MAXUINT64 - hours_t < res)
goto overflow;
res = hours_t + res;
}
} else {
res = GST_CLOCK_TIME_NONE;
}
return res;
overflow:
return GST_CLOCK_TIME_NONE;
}
gchar *
gst_cmml_clock_time_to_npt (const GstClockTime time)
{
guint seconds, hours, minutes, mseconds;
gchar *res;
g_return_val_if_fail (time != GST_CLOCK_TIME_NONE, NULL);
hours = time / (GST_SECOND * 3600);
minutes = (time / ((GST_SECOND * 60)) % 60);
seconds = (time / GST_SECOND) % 60;
mseconds = (time % GST_SECOND) / GST_MSECOND;
if (mseconds < 100)
mseconds *= 10;
res = g_strdup_printf ("%u:%02u:%02u.%03u",
hours, minutes, seconds, mseconds);
return res;
}
gint64
gst_cmml_clock_time_to_granule (GstClockTime prev_time,
GstClockTime current_time, gint64 granulerate_n, gint64 granulerate_d,
guint8 granuleshift)
{
gint64 keyindex, keyoffset, granulepos;
gint64 granulerate;
if (prev_time == GST_CLOCK_TIME_NONE)
prev_time = 0;
if (prev_time > current_time)
return -1;
granulerate = gst_util_uint64_scale (GST_SECOND,
granulerate_n, granulerate_d);
keyindex = prev_time / granulerate << granuleshift;
keyoffset = (current_time - prev_time) / granulerate;
granulepos = keyindex + keyoffset;
return granulepos;
}
/* track list */
GHashTable *
gst_cmml_track_list_new ()
{
return g_hash_table_new (g_str_hash, g_str_equal);
}
static gboolean
gst_cmml_track_list_destroy_track (gchar * key,
GstCmmlTrack * track, gpointer user_data)
{
GList *walk;
for (walk = track->clips; walk; walk = g_list_next (walk))
g_object_unref (G_OBJECT (walk->data));
g_free (key);
g_list_free (track->clips);
g_free (track);
return TRUE;
}
void
gst_cmml_track_list_destroy (GHashTable * tracks)
{
g_hash_table_foreach_remove (tracks,
(GHRFunc) gst_cmml_track_list_destroy_track, NULL);
g_hash_table_destroy (tracks);
}
static gint
gst_cmml_track_list_compare_clips (GstCmmlTagClip * a, GstCmmlTagClip * b)
{
if (a->start_time < b->start_time)
return -1;
return 1;
}
void
gst_cmml_track_list_add_clip (GHashTable * tracks, GstCmmlTagClip * clip)
{
GstCmmlTrack *track = NULL;
gchar *track_name = NULL;
void *key = NULL, *value = NULL;
/* find clip's track */
g_hash_table_lookup_extended (tracks, clip->track, &key, &value);
track_name = (gchar *) key;
track = (GstCmmlTrack *) track;
if (track_name == NULL)
/* it doesn't exist yet: create its key */
track_name = g_strdup ((gchar *) clip->track);
if (track == NULL)
track = g_new0 (GstCmmlTrack, 1);
/* add clip to the tracklist */
track->clips = g_list_insert_sorted (track->clips, g_object_ref (clip),
(GCompareFunc) gst_cmml_track_list_compare_clips);
/* reset the head every time as it could change */
g_hash_table_insert (tracks, track_name, track);
}
gboolean
gst_cmml_track_list_del_clip (GHashTable * tracks, GstCmmlTagClip * clip)
{
GstCmmlTrack *track;
GList *link;
gboolean res = FALSE;
track = g_hash_table_lookup (tracks, clip->track);
if (track) {
link = g_list_find (track->clips, clip);
if (link) {
g_object_unref (G_OBJECT (link->data));
track->clips = g_list_remove_link (track->clips, link);
res = TRUE;
}
}
return res;
}
gboolean
gst_cmml_track_list_has_clip (GHashTable * tracks, GstCmmlTagClip * clip)
{
GstCmmlTrack *track;
GList *walk;
GstCmmlTagClip *tmp;
gchar *clip_id = (gchar *) clip->id;
gboolean res = FALSE;
track = g_hash_table_lookup (tracks, clip_id);
if (track) {
for (walk = track->clips; walk; walk = g_list_next (walk)) {
tmp = GST_CMML_TAG_CLIP (walk->data);
if (!strcmp ((gchar *) tmp->id, clip_id)) {
res = TRUE;
break;
}
}
}
return res;
}
static gboolean
gst_cmml_track_list_merge_track (gchar * track_name,
GstCmmlTrack * track, GList ** list)
{
GList *walk;
GstCmmlTagClip *cur;
for (walk = track->clips; walk; walk = g_list_next (walk)) {
cur = GST_CMML_TAG_CLIP (walk->data);
*list = g_list_insert_sorted (*list, cur,
(GCompareFunc) gst_cmml_track_list_compare_clips);
}
return TRUE;
}
GList *
gst_cmml_track_list_get_track_clips (GHashTable * tracks,
const gchar * track_name)
{
GstCmmlTrack *track;
track = g_hash_table_lookup (tracks, track_name);
return track ? track->clips : NULL;
}
GList *
gst_cmml_track_list_get_clips (GHashTable * tracks)
{
GList *list = NULL;
g_hash_table_foreach (tracks,
(GHFunc) gst_cmml_track_list_merge_track, &list);
return list;
}
GstCmmlTagClip *
gst_cmml_track_list_get_track_last_clip (GHashTable * tracks,
const gchar * track_name)
{
GstCmmlTrack *track;
GList *res = NULL;
track = g_hash_table_lookup (tracks, track_name);
if (track && track->clips)
res = g_list_last (track->clips);
return res ? GST_CMML_TAG_CLIP (res->data) : NULL;
}
void
gst_cmml_track_list_set_data (GHashTable * tracks,
const gchar * track_name, gpointer data)
{
GstCmmlTrack *track;
track = g_hash_table_lookup (tracks, track_name);
if (track)
track->user_data = data;
}
gpointer
gst_cmml_track_get_data (GHashTable * tracks, const gchar * track_name)
{
GstCmmlTrack *track;
track = g_hash_table_lookup (tracks, track_name);
return track ? track->user_data : NULL;
}

View file

@ -0,0 +1,53 @@
/*
* gstcmmlutils.h - GStreamer CMML utility functions
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#ifndef __GST_CMML_CLOCK_TIME_H__
#define __GST_CMML_CLOCK_TIME_H__
#include <gst/gst.h>
#include "gstcmmltag.h"
/* time utils */
GstClockTime gst_cmml_clock_time_from_npt (const gchar * time);
GstClockTime gst_cmml_clock_time_from_smpte (const gchar * time);
gchar * gst_cmml_clock_time_to_npt (const GstClockTime time);
gint64 gst_cmml_clock_time_to_granule (GstClockTime prev_time,
GstClockTime current_time, gint64 granulerate_n, gint64 granulerate_d,
guint8 granuleshift);
/* tracklist */
GHashTable * gst_cmml_track_list_new (void);
void gst_cmml_track_list_destroy (GHashTable * tracks);
void gst_cmml_track_list_add_clip (GHashTable * tracks, GstCmmlTagClip * clip);
gboolean gst_cmml_track_list_del_clip (GHashTable * tracks,
GstCmmlTagClip * clip);
gboolean gst_cmml_track_list_has_clip (GHashTable * tracks,
GstCmmlTagClip * clip);
GstCmmlTagClip * gst_cmml_track_list_get_track_last_clip (GHashTable * tracks,
const gchar * track_name);
GList * gst_cmml_track_list_get_track_clips (GHashTable * tracks,
const gchar * track_name);
GList * gst_cmml_track_list_get_clips (GHashTable * tracks);
void gst_cmml_track_list_set_track_data (GHashTable * tracks, gpointer data);
gpointer gst_cmml_track_list_get_track_data (GHashTable * tracks);
#endif /* __GST_CMML_CLOCK_TIME_H__ */

380
ext/annodex/gstskeldec.c Normal file
View file

@ -0,0 +1,380 @@
/*
* gstskeldec.c - GStreamer annodex skeleton decoder
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#include <string.h>
#include "gstskeldec.h"
#include "gstskeltag.h"
#include "gstannodex.h"
GST_DEBUG_CATEGORY (skeldec);
#define GST_CAT_DEFAULT skeldec
#define FISHEAD_SIZE 64
#define FISBONE_MIN_SIZE 52
enum
{
GST_SKEL_TAG_FISHEAD_UTC_LEN = 19
};
enum
{
LAST_SIGNAL
};
static GstElementDetails gst_skel_dec_details = {
"skeldec: Annodex skeleton stream decoder",
"Codec/Decoder",
"Decodes ogg skeleton streams",
"Alessandro Decina <alessandro@nnva.org>",
};
static GstStaticPadTemplate gst_skel_dec_src_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-ogg-skeleton, parsed=(boolean)TRUE")
);
static GstStaticPadTemplate gst_skel_dec_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-ogg-skeleton, parsed=(boolean)FALSE")
);
/* GstSkelDec prototypes */
GST_BOILERPLATE (GstSkelDec, gst_skel_dec, GstElement, GST_TYPE_ELEMENT);
static gboolean gst_skel_dec_sink_query (GstPad * pad, GstQuery * query);
static GstStateChangeReturn gst_skel_dec_change_state (GstElement * element,
GstStateChange transition);
static GstFlowReturn gst_skel_dec_chain (GstPad * pad, GstBuffer * buffer);
static GstFlowReturn gst_skel_dec_parse_fishead (GstSkelDec * dec,
GstBuffer * buffer);
static GstFlowReturn gst_skel_dec_parse_fisbone (GstSkelDec * dec,
GstBuffer * buffer);
/* GstSkelDec code */
static void
gst_skel_dec_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 (&gst_skel_dec_sink_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_skel_dec_src_factory));
gst_element_class_set_details (element_class, &gst_skel_dec_details);
}
static void
gst_skel_dec_class_init (GstSkelDecClass * klass)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
element_class->change_state = gst_skel_dec_change_state;
}
static void
gst_skel_dec_init (GstSkelDec * dec, GstSkelDecClass * klass)
{
dec->sinkpad =
gst_pad_new_from_static_template (&gst_skel_dec_sink_factory, "sink");
gst_pad_set_query_function (dec->sinkpad, gst_skel_dec_sink_query);
gst_pad_set_chain_function (dec->sinkpad, gst_skel_dec_chain);
gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad);
dec->srcpad =
gst_pad_new_from_static_template (&gst_skel_dec_src_factory, "src");
gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad);
}
static gboolean
gst_skel_dec_sink_query (GstPad * pad, GstQuery * query)
{
gboolean res = FALSE;
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_CONVERT:
{
GstFormat src_format, dest_format;
gint64 src_val, dest_val;
gst_query_parse_convert (query, &src_format, &src_val,
&dest_format, &dest_val);
if (dest_format == GST_FORMAT_TIME) {
dest_val = GST_CLOCK_TIME_NONE;
res = TRUE;
}
if (res) {
gst_query_set_convert (query, src_format, src_val,
dest_format, dest_val);
}
break;
}
default:
break;
}
return res;
}
static GstStateChangeReturn
gst_skel_dec_change_state (GstElement * element, GstStateChange transition)
{
GstSkelDec *dec = GST_SKEL_DEC (element);
GstStateChangeReturn res;
/* handle the upward state changes */
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
dec->major = 0;
dec->minor = 0;
break;
default:
break;
}
res = parent_class->change_state (element, transition);
/* no need to handle downward state changes */
return res;
}
static GstFlowReturn
gst_skel_dec_chain (GstPad * pad, GstBuffer * buffer)
{
GstSkelDec *dec = GST_SKEL_DEC (GST_PAD_PARENT (pad));
GstFlowReturn ret;
gint size = GST_BUFFER_SIZE (buffer);
guint8 *data = GST_BUFFER_DATA (buffer);
if (GST_BUFFER_SIZE (buffer) == 0) {
/* the skeleton EOS has no packet data */
return GST_FLOW_OK;
}
if (size >= GST_SKEL_OGG_FISHEAD_SIZE && !memcmp (data, "fishead\0", 8)) {
ret = gst_skel_dec_parse_fishead (dec, buffer);
} else if (size >= 8 && !memcmp (data, "fisbone\0", 8)) {
ret = gst_skel_dec_parse_fisbone (dec, buffer);
} else {
/* maybe it would be better to ignore the packet */
GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("unknown packet type"));
ret = GST_FLOW_UNEXPECTED;
}
return ret;
}
static GstFlowReturn
gst_skel_dec_parse_fishead (GstSkelDec * dec, GstBuffer * buffer)
{
GstTagList *tags;
guint8 *data = GST_BUFFER_DATA (buffer);
GstSkelTagFishead *fishead;
if (GST_BUFFER_SIZE (buffer) != FISHEAD_SIZE)
goto wrong_size;
fishead = g_object_new (GST_TYPE_SKEL_TAG_FISHEAD, NULL);
data += 8;
fishead->major = GST_READ_UINT16_LE (data);
data += 2;
fishead->minor = GST_READ_UINT16_LE (data);
data += 2;
fishead->prestime_n = (gint64) GST_READ_UINT64_LE (data);
data += 8;
fishead->prestime_d = (gint64) GST_READ_UINT64_LE (data);
data += 8;
fishead->basetime_n = (gint64) GST_READ_UINT64_LE (data);
data += 8;
fishead->basetime_d = (gint64) GST_READ_UINT64_LE (data);
data += 8;
fishead->utc = g_strndup ((gchar *) data, GST_SKEL_TAG_FISHEAD_UTC_LEN);
GST_INFO_OBJECT (dec, "fishead parsed ("
"major: %" G_GUINT16_FORMAT " minor: %" G_GUINT16_FORMAT
" prestime_n: %" G_GINT64_FORMAT " prestime_d: %" G_GINT64_FORMAT
" basetime_n: %" G_GINT64_FORMAT " basetime_d: %" G_GINT64_FORMAT
" utc: %s)", fishead->major, fishead->minor, fishead->prestime_n,
fishead->prestime_d, fishead->basetime_n, fishead->basetime_d,
fishead->utc);
/* send the TAG_MESSAGE */
tags = gst_tag_list_new ();
gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
GST_TAG_SKELETON_FISHEAD, fishead, NULL);
gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, tags);
g_object_unref (fishead);
/* forward the fishead */
gst_buffer_set_caps (buffer,
gst_static_pad_template_get_caps (&gst_skel_dec_src_factory));
return gst_pad_push (dec->srcpad, buffer);
wrong_size:
GST_ELEMENT_ERROR (dec, STREAM, DECODE,
(NULL), ("wrong fishead packet size: %d", GST_BUFFER_SIZE (buffer)));
gst_buffer_unref (buffer);
return GST_FLOW_ERROR;
}
static GstFlowReturn
gst_skel_dec_parse_fisbone (GstSkelDec * dec, GstBuffer * buffer)
{
GstSkelTagFisbone *fisbone;
guint8 *data;
gchar *headers;
gchar **tokens;
const gchar *content_type;
GstTagList *tags;
GValue *val;
if (GST_BUFFER_SIZE (buffer) < FISBONE_MIN_SIZE)
goto wrong_size;
data = GST_BUFFER_DATA (buffer);
fisbone = g_object_new (GST_TYPE_SKEL_TAG_FISBONE, NULL);
data += 8;
fisbone->hdr_offset = GST_READ_UINT32_LE (data);
data += 4;
fisbone->serialno = GST_READ_UINT32_LE (data);
data += 4;
fisbone->hdr_num = GST_READ_UINT32_LE (data);
data += 4;
fisbone->granulerate_n = GST_READ_UINT64_LE (data);
data += 8;
fisbone->granulerate_d = GST_READ_UINT64_LE (data);
data += 8;
fisbone->start_granule = GST_READ_UINT64_LE (data);
data += 8;
fisbone->preroll = GST_READ_UINT32_LE (data);
data += 4;
fisbone->granuleshift = GST_READ_UINT8 (data);
data += 1;
data += 3; /* padding */
/* 8 = strlen ("fishead\0") */
headers = g_strndup ((gchar *) data,
GST_BUFFER_SIZE (buffer) - 8 - fisbone->hdr_offset);
fisbone->headers = gst_annodex_parse_headers (headers);
g_free (headers);
if (!fisbone->headers)
goto bad_headers;
/* check for the mandatory Content-Type header: it MUST be the first
* header */
if (fisbone->headers->n_values < 2 ||
(val = g_value_array_get_nth (fisbone->headers, 0)) == NULL ||
strcmp (g_value_get_string (val), "Content-Type"))
goto no_content_type;
/* get the Content-Type value */
val = g_value_array_get_nth (fisbone->headers, 1);
content_type = g_value_get_string (val);
/* Content-Type must not be empty */
if (*content_type == '\0')
goto bad_content_type;
/* split the content-type field into "content_type; encoding" */
tokens = g_strsplit (content_type, ";", 2);
fisbone->content_type = g_strdup (g_strstrip (tokens[0]));
fisbone->encoding = tokens[1] == NULL ? NULL :
g_strdup (g_strstrip (tokens[1]));
g_strfreev (tokens);
GST_INFO_OBJECT (dec, "fisbone parsed ("
"serialno %" G_GUINT32_FORMAT " granulerate_n: %" G_GINT64_FORMAT
" granulerate_d: %" G_GINT64_FORMAT " start_granule: %" G_GINT64_FORMAT
" preroll: %" G_GUINT32_FORMAT " granuleshift: %d"
" content-type: %s)",
fisbone->serialno, fisbone->granulerate_n, fisbone->granulerate_d,
fisbone->start_granule, fisbone->preroll, fisbone->granuleshift,
fisbone->content_type);
/* send the tag message */
tags = gst_tag_list_new ();
gst_tag_list_add (tags, GST_TAG_MERGE_APPEND,
GST_TAG_SKELETON_FISBONE, fisbone, NULL);
gst_element_found_tags_for_pad (GST_ELEMENT (dec), dec->srcpad, tags);
g_object_unref (fisbone);
gst_buffer_set_caps (buffer,
gst_static_pad_template_get_caps (&gst_skel_dec_src_factory));
return gst_pad_push (dec->srcpad, buffer);
wrong_size:
GST_ELEMENT_ERROR (dec, STREAM, DECODE,
(NULL), ("wrong fisbone size (%d)", GST_BUFFER_SIZE (buffer)));
gst_buffer_unref (buffer);
return GST_FLOW_ERROR;
bad_headers:
GST_ELEMENT_ERROR (dec, STREAM, DECODE, (NULL), ("bad fisbone headers"));
gst_buffer_unref (buffer);
g_object_unref (fisbone);
return GST_FLOW_ERROR;
no_content_type:
bad_content_type:
GST_ELEMENT_ERROR (dec, STREAM, DECODE,
(NULL), ("missing or bad fisbone content-type"));
gst_buffer_unref (buffer);
g_object_unref (fisbone);
return GST_FLOW_ERROR;
}
gboolean
gst_skel_dec_plugin_init (GstPlugin * plugin)
{
if (!gst_element_register (plugin, "skeldec", GST_RANK_PRIMARY,
gst_skel_dec_get_type ()))
return FALSE;
gst_tag_register (GST_TAG_SKELETON_FISHEAD, GST_TAG_FLAG_META,
GST_TYPE_SKEL_TAG_FISHEAD, "skeleton-fishead",
"annodex skeleton fishead tag", NULL);
gst_tag_register (GST_TAG_SKELETON_FISBONE, GST_TAG_FLAG_META,
GST_TYPE_SKEL_TAG_FISBONE, "skeleton-fisbone",
"annodex skeleton fisbone tag", NULL);
GST_DEBUG_CATEGORY_INIT (skeldec, "skeldec", 0,
"annodex skeleton decoding element");
return TRUE;
}

62
ext/annodex/gstskeldec.h Normal file
View file

@ -0,0 +1,62 @@
/*
* gstskeldec.h - GStreamer annodex skeleton decoder
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#ifndef __GST_SKEL_DEC_H__
#define __GST_SKEL_DEC_H__
#include <gst/gst.h>
/* GstSkelDec */
#define GST_TYPE_SKEL_DEC (gst_skel_dec_get_type ())
#define GST_SKEL_DEC(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_SKEL_DEC, GstSkelDec))
#define GST_SKEL_DEC_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_SKEL_DEC, GstSkelDec))
#define GST_SKEL_DEC_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_SKEL_DEC, GstSkelDecClass))
typedef struct _GstSkelDec GstSkelDec;
typedef struct _GstSkelDecClass GstSkelDecClass;
#define GST_SKEL_OGG_FISHEAD_SIZE 64
#define UTC_LEN 20
struct _GstSkelDec
{
GstElement element;
GstPad *sinkpad;
GstPad *srcpad;
gint16 major; /* skeleton version major */
gint16 minor; /* skeleton version minor */
};
struct _GstSkelDecClass
{
GstElementClass parent_class;
};
GType gst_skel_dec_get_type (void);
gboolean gst_skel_dec_plugin_init (GstPlugin * plugin);
#endif /* __GST_SKEL_DEC_H__ */

371
ext/annodex/gstskeltag.c Normal file
View file

@ -0,0 +1,371 @@
/*
* gstskeltag.c - GStreamer annodex skeleton tags
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#include "gstskeltag.h"
enum
{
ARG_0,
GST_SKEL_TAG_FISHEAD_MAJOR,
GST_SKEL_TAG_FISHEAD_MINOR,
GST_SKEL_TAG_FISHEAD_PRESTIME_N,
GST_SKEL_TAG_FISHEAD_PRESTIME_D,
GST_SKEL_TAG_FISHEAD_BASETIME_N,
GST_SKEL_TAG_FISHEAD_BASETIME_D,
GST_SKEL_TAG_FISHEAD_UTC,
GST_SKEL_TAG_FISBONE_SERIALNO,
GST_SKEL_TAG_FISBONE_GRANULERATE_N,
GST_SKEL_TAG_FISBONE_GRANULERATE_D,
GST_SKEL_TAG_FISBONE_START_GRANULE,
GST_SKEL_TAG_FISBONE_PREROLL,
GST_SKEL_TAG_FISBONE_GRANULESHIFT,
GST_SKEL_TAG_FISBONE_HEADERS,
GST_SKEL_TAG_FISBONE_CONTENT_TYPE,
GST_SKEL_TAG_FISBONE_ENCODING,
};
/* GstSkelTagFishead prototypes */
G_DEFINE_TYPE (GstSkelTagFishead, gst_skel_tag_fishead, G_TYPE_OBJECT);
static void gst_skel_tag_fishead_finalize (GObject * object);
static void gst_skel_tag_fishead_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_skel_tag_fishead_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
/* GstSkelTagFisbone prototypes */
G_DEFINE_TYPE (GstSkelTagFisbone, gst_skel_tag_fisbone, G_TYPE_OBJECT);
static void gst_skel_tag_fisbone_finalize (GObject * object);
static void gst_skel_tag_fisbone_set_property (GObject * object,
guint property_id, const GValue * value, GParamSpec * pspec);
static void gst_skel_tag_fisbone_get_property (GObject * object,
guint property_id, GValue * value, GParamSpec * pspec);
/* GstSkelTagFishead */
static void
gst_skel_tag_fishead_class_init (GstSkelTagFisheadClass * fishead_class)
{
GObjectClass *klass = G_OBJECT_CLASS (fishead_class);
klass->set_property = gst_skel_tag_fishead_set_property;
klass->get_property = gst_skel_tag_fishead_get_property;
klass->finalize = gst_skel_tag_fishead_finalize;
g_object_class_install_property (klass, GST_SKEL_TAG_FISHEAD_MAJOR,
g_param_spec_int ("version-major",
"Major version number",
"Major number of the skeleton bitstream",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISHEAD_MINOR,
g_param_spec_int ("version-minor",
"Minor version number",
"Minor number of the skeleton bitstream",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISHEAD_PRESTIME_N,
g_param_spec_int64 ("presentation-time-numerator",
"Presentation time numerator",
"Stream presentation time numerator",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISHEAD_PRESTIME_D,
g_param_spec_int64 ("presentation-time-denominator",
"Presentation time denominator",
"Stream presentation time denominator",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISHEAD_BASETIME_N,
g_param_spec_int64 ("base-time-numerator",
"Basetime numerator",
"Stream base time numerator",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISHEAD_BASETIME_D,
g_param_spec_int64 ("base-time-denominator",
"Base time denominator",
"Stream base time denominator",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISHEAD_UTC,
g_param_spec_string ("calendar-base-time",
"Calendar base time",
"Date and all-clock time (expressed as UTC in the format "
"YYYYMMDDTHHMMSS.sssZ) associated with the base time",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
}
static void
gst_skel_tag_fishead_init (GstSkelTagFishead * fishead)
{
}
static void
gst_skel_tag_fishead_finalize (GObject * object)
{
GObjectClass *parent_class =
G_OBJECT_CLASS (gst_skel_tag_fishead_parent_class);
GstSkelTagFishead *fishead = GST_SKEL_TAG_FISHEAD (object);
g_free (fishead->utc);
GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}
static void
gst_skel_tag_fishead_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstSkelTagFishead *fishead = GST_SKEL_TAG_FISHEAD (object);
switch (property_id) {
case GST_SKEL_TAG_FISHEAD_MAJOR:
fishead->major = g_value_get_int (value);
break;
case GST_SKEL_TAG_FISHEAD_MINOR:
fishead->minor = g_value_get_int (value);
break;
case GST_SKEL_TAG_FISHEAD_PRESTIME_N:
fishead->prestime_n = g_value_get_int64 (value);
break;
case GST_SKEL_TAG_FISHEAD_PRESTIME_D:
fishead->prestime_d = g_value_get_int64 (value);
break;
case GST_SKEL_TAG_FISHEAD_BASETIME_N:
fishead->basetime_n = g_value_get_int64 (value);
break;
case GST_SKEL_TAG_FISHEAD_BASETIME_D:
fishead->basetime_d = g_value_get_int64 (value);
break;
case GST_SKEL_TAG_FISHEAD_UTC:
g_free (fishead->utc);
fishead->utc = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gst_skel_tag_fishead_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstSkelTagFishead *fishead = GST_SKEL_TAG_FISHEAD (object);
switch (property_id) {
case GST_SKEL_TAG_FISHEAD_MAJOR:
g_value_set_int (value, fishead->major);
break;
case GST_SKEL_TAG_FISHEAD_MINOR:
g_value_set_int (value, fishead->minor);
break;
case GST_SKEL_TAG_FISHEAD_BASETIME_N:
g_value_set_int64 (value, fishead->basetime_n);
break;
case GST_SKEL_TAG_FISHEAD_BASETIME_D:
g_value_set_int64 (value, fishead->basetime_d);
break;
case GST_SKEL_TAG_FISHEAD_PRESTIME_N:
g_value_set_int64 (value, fishead->prestime_n);
break;
case GST_SKEL_TAG_FISHEAD_PRESTIME_D:
g_value_set_int64 (value, fishead->prestime_d);
break;
case GST_SKEL_TAG_FISHEAD_UTC:
g_value_set_string (value, fishead->utc);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
/* GstSkelTagFisbone code */
static void
gst_skel_tag_fisbone_class_init (GstSkelTagFisboneClass * fisbone_class)
{
GObjectClass *klass = G_OBJECT_CLASS (fisbone_class);
klass->set_property = gst_skel_tag_fisbone_set_property;
klass->get_property = gst_skel_tag_fisbone_get_property;
klass->finalize = gst_skel_tag_fisbone_finalize;
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_SERIALNO,
g_param_spec_uint ("serial-number",
"Serial number",
"Serial number of the logical bitstream",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_GRANULERATE_N,
g_param_spec_int64 ("granule-rate-numerator",
"Granulerate numerator",
"Granulerate numerator",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_GRANULERATE_D,
g_param_spec_int64 ("granule-rate-denominator",
"Granulerate denominator",
"Granulerate denominator",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_START_GRANULE,
g_param_spec_int64 ("granule-start",
"Start granule",
"The granule number with which this logical bitstream starts",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_PREROLL,
g_param_spec_uint64 ("preroll",
"The number of packets to preroll",
"The number of packets to preroll to decode a packet correctly",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_GRANULESHIFT,
g_param_spec_uint ("granule-shift",
"Granuleshift",
"The number of lower bits to use for partitioning a granule position",
0, 0, 0, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_HEADERS,
g_param_spec_boxed ("headers",
"Message header fields",
"RFC2822 header fields describing a logical bitstream",
G_TYPE_VALUE_ARRAY, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_CONTENT_TYPE,
g_param_spec_string ("content-type",
"Content type",
"Bitstream content type",
NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (klass, GST_SKEL_TAG_FISBONE_ENCODING,
g_param_spec_string ("encoding",
"Encoding",
"Bitstream encoding", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
}
static void
gst_skel_tag_fisbone_init (GstSkelTagFisbone * fisbone)
{
}
static void
gst_skel_tag_fisbone_finalize (GObject * object)
{
GObjectClass *parent_class =
G_OBJECT_CLASS (gst_skel_tag_fishead_parent_class);
GstSkelTagFisbone *fisbone = GST_SKEL_TAG_FISBONE (object);
g_free (fisbone->content_type);
g_free (fisbone->encoding);
g_value_array_free (fisbone->headers);
GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}
static void
gst_skel_tag_fisbone_set_property (GObject * object, guint property_id,
const GValue * value, GParamSpec * pspec)
{
GstSkelTagFisbone *fisbone = GST_SKEL_TAG_FISBONE (object);
switch (property_id) {
case GST_SKEL_TAG_FISBONE_SERIALNO:
fisbone->serialno = g_value_get_uint (value);
break;
case GST_SKEL_TAG_FISBONE_GRANULERATE_N:
fisbone->granulerate_n = g_value_get_int64 (value);
break;
case GST_SKEL_TAG_FISBONE_GRANULERATE_D:
fisbone->granulerate_d = g_value_get_int64 (value);
break;
case GST_SKEL_TAG_FISBONE_START_GRANULE:
fisbone->start_granule = g_value_get_int64 (value);
break;
case GST_SKEL_TAG_FISBONE_PREROLL:
fisbone->preroll = g_value_get_uint64 (value);
break;
case GST_SKEL_TAG_FISBONE_GRANULESHIFT:
fisbone->granuleshift = g_value_get_uint (value);
break;
case GST_SKEL_TAG_FISBONE_HEADERS:
{
GValueArray *va;
va = g_value_get_boxed (value);
if (fisbone->headers)
g_value_array_free (fisbone->headers);
fisbone->headers = va != NULL ? g_value_array_copy (va) : NULL;
break;
}
case GST_SKEL_TAG_FISBONE_CONTENT_TYPE:
g_free (fisbone->content_type);
fisbone->content_type = g_value_dup_string (value);
break;
case GST_SKEL_TAG_FISBONE_ENCODING:
g_free (fisbone->encoding);
fisbone->encoding = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}
static void
gst_skel_tag_fisbone_get_property (GObject * object, guint property_id,
GValue * value, GParamSpec * pspec)
{
GstSkelTagFisbone *fisbone = GST_SKEL_TAG_FISBONE (object);
switch (property_id) {
case GST_SKEL_TAG_FISBONE_SERIALNO:
g_value_set_uint (value, fisbone->serialno);
break;
case GST_SKEL_TAG_FISBONE_GRANULERATE_N:
g_value_set_int64 (value, fisbone->granulerate_n);
break;
case GST_SKEL_TAG_FISBONE_GRANULERATE_D:
g_value_set_int64 (value, fisbone->granulerate_d);
break;
case GST_SKEL_TAG_FISBONE_START_GRANULE:
g_value_set_int64 (value, fisbone->start_granule);
break;
case GST_SKEL_TAG_FISBONE_GRANULESHIFT:
g_value_set_uint (value, fisbone->granuleshift);
break;
case GST_SKEL_TAG_FISBONE_PREROLL:
g_value_set_uint64 (value, fisbone->preroll);
break;
case GST_SKEL_TAG_FISBONE_HEADERS:
g_value_set_boxed (value, fisbone->headers);
break;
case GST_SKEL_TAG_FISBONE_CONTENT_TYPE:
g_value_set_string (value, fisbone->content_type);
break;
case GST_SKEL_TAG_FISBONE_ENCODING:
g_value_set_string (value, fisbone->encoding);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
}

101
ext/annodex/gstskeltag.h Normal file
View file

@ -0,0 +1,101 @@
/*
* gstskeltag.h - GStreamer annodex skeleton tags
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#ifndef __GST_SKEL_TAG_H__
#define __GST_SKEL_TAG_H__
#include <gst/gst.h>
/* GstSkelTagFishead */
#define GST_TYPE_SKEL_TAG_FISHEAD (gst_skel_tag_fishead_get_type ())
#define GST_SKEL_TAG_FISHEAD(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_SKEL_TAG_FISHEAD, \
GstSkelTagFishead))
#define GST_SKEL_TAG_FISHEAD_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_SKEL_TAG_FISHEAD, \
GstSkelTagFishead))
#define GST_SKEL_TAG_FISHEAD_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_SKEL_TAG_FISHEAD, \
GstSkelTagFisheadClass))
/* GstSkelTagFisbone */
#define GST_TYPE_SKEL_TAG_FISBONE (gst_skel_tag_fisbone_get_type ())
#define GST_SKEL_TAG_FISBONE(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_SKEL_TAG_FISBONE, \
GstSkelTagFisbone))
#define GST_SKEL_TAG_FISBONE_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_SKEL_TAG_FISBONE, \
GstSkelTagFisbone))
#define GST_SKEL_TAG_FISBONE_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), GST_TYPE_SKEL_TAG_FISBONE, \
GstSkelTagFisboneClass))
typedef struct _GstSkelTagFishead GstSkelTagFishead;
typedef struct _GstSkelTagFisheadClass GstSkelTagFisheadClass;
typedef struct _GstSkelTagFisbone GstSkelTagFisbone;
typedef struct _GstSkelTagFisboneClass GstSkelTagFisboneClass;
struct _GstSkelTagFishead {
GObject object;
guint16 major;
guint16 minor;
gint64 prestime_n;
gint64 prestime_d;
gint64 basetime_n;
gint64 basetime_d;
gchar *utc;
};
struct _GstSkelTagFisheadClass {
GObjectClass parent_class;
};
struct _GstSkelTagFisbone {
GObject object;
guint32 hdr_offset;
guint32 serialno;
guint32 hdr_num;
gint64 granulerate_n;
gint64 granulerate_d;
gint64 start_granule;
guint32 preroll;
guint8 granuleshift;
gchar *content_type;
gchar *encoding;
GValueArray *headers;
};
struct _GstSkelTagFisboneClass {
GObjectClass parent_class;
};
GType gst_skel_tag_fishead_get_type (void);
GType gst_skel_tag_fisbone_get_type (void);
#endif /* __GST_SKEL_TAG_H__ */

View file

@ -19,7 +19,10 @@ TESTS = $(check_PROGRAMS)
check_PROGRAMS = \
elements/level \
elements/matroskamux
elements/matroskamux \
elements/cmmldec \
elements/cmmlenc \
elements/skeldec
# these tests don't even pass

View file

@ -0,0 +1,409 @@
/*
* cmmldec.c - GStreamer CMML decoder test suite
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#include <gst/check/gstcheck.h>
#define SINK_CAPS "text/xml"
#define SRC_CAPS "text/x-cmml"
#define IDENT_HEADER \
"CMML\x00\x00\x00\x00"\
"\x03\x00\x00\x00"\
"\x01\x00\x00\x00\x00\x00\x00\x00"\
"\xe8\x03\x00\x00\x00\x00\x00\x00"\
"\x20"
#define XML_PREAMBLE \
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"\
"<!DOCTYPE cmml SYSTEM \"cmml.dtd\">\n"\
#define PREAMBLE \
XML_PREAMBLE "<?cmml?>"
#define PREAMBLE_DECODED \
XML_PREAMBLE "<cmml >"
#define HEAD_TAG \
"<head>"\
"<title>The Research Hunter</title>"\
"<meta name=\"DC.audience\" content=\"General\"/>"\
"<meta name=\"DC.author\" content=\"CSIRO Publishing\"/>"\
"<meta name=\"DC.format\" content=\"video\"/>"\
"<meta name=\"DC.language\" content=\"English\"/>"\
"<meta name=\"DC.publisher\" content=\"CSIRO Australia\"/>"\
"</head>"
#define HEAD_TAG_DECODED HEAD_TAG
#define CLIP_TEMPLATE \
"<clip id=\"%s\" track=\"%s\">"\
"<a href=\"http://www.csiro.au/\">http://www.csiro.au</a>"\
"<img src=\"images/index1.jpg\"/>"\
"<desc>Welcome to CSIRO</desc>"\
"<meta name=\"test\" content=\"test content\"/>"\
"</clip>"
#define CLIP_TEMPLATE_DECODED \
"<clip id=\"%s\" track=\"%s\" start=\"%s\">"\
"<a href=\"http://www.csiro.au/\">http://www.csiro.au</a>"\
"<img src=\"images/index1.jpg\"/>"\
"<desc>Welcome to CSIRO</desc>"\
"<meta name=\"test\" content=\"test content\"/>"\
"</clip>"
#define END_TAG \
"</cmml>"
GList *buffers;
GList *current_buf = NULL;
gint64 granulerate;
guint8 granuleshift;
GstPad *srcpad, *sinkpad;
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (SINK_CAPS)
);
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (SRC_CAPS)
);
static GstBuffer *
buffer_new (const gchar * buffer_data, guint size)
{
GstBuffer *buffer;
GstCaps *caps;
buffer = gst_buffer_new_and_alloc (size);
memcpy (GST_BUFFER_DATA (buffer), buffer_data, size);
caps = gst_caps_from_string (SRC_CAPS);
gst_buffer_set_caps (buffer, caps);
gst_caps_unref (caps);
return buffer;
}
static void
buffer_unref (void *buffer, void *user_data)
{
ASSERT_OBJECT_REFCOUNT (buffer, "buf", 1);
gst_buffer_unref (GST_BUFFER (buffer));
}
GstElement *
setup_cmmldec ()
{
GstElement *cmmldec;
GstBus *bus;
GST_DEBUG ("setup_cmmldec");
cmmldec = gst_check_setup_element ("cmmldec");
srcpad = gst_check_setup_src_pad (cmmldec, &srctemplate, NULL);
sinkpad = gst_check_setup_sink_pad (cmmldec, &sinktemplate, NULL);
bus = gst_bus_new ();
gst_element_set_bus (cmmldec, bus);
fail_unless (gst_element_set_state (cmmldec,
GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
"could not set to playing");
granulerate = GST_SECOND / 1000;
granuleshift = 32;
buffers = NULL;
return cmmldec;
}
static void
cleanup_cmmldec (GstElement * cmmldec)
{
GstBus *bus;
g_list_foreach (buffers, buffer_unref, NULL);
g_list_free (buffers);
bus = GST_ELEMENT_BUS (cmmldec);
gst_bus_set_flushing (bus, TRUE);
gst_object_unref (bus);
GST_DEBUG ("cleanup_cmmldec");
gst_check_teardown_src_pad (cmmldec);
gst_check_teardown_sink_pad (cmmldec);
gst_check_teardown_element (cmmldec);
}
static void
check_output_buffer_is_equal (const gchar * name,
const gchar * data, gint refcount)
{
GstBuffer *buffer = GST_BUFFER (current_buf->data);
ASSERT_OBJECT_REFCOUNT (buffer, name, refcount);
fail_unless (memcmp (GST_BUFFER_DATA (buffer), data,
GST_BUFFER_SIZE (buffer)) == 0,
"'%s' (%s) is not equal to (%s)", name, GST_BUFFER_DATA (buffer), data);
}
static void
push_data (const gchar * name,
const gchar * data, gint size, gint64 granulepos,
GstFlowReturn expected_return)
{
GstBuffer *buffer;
GstFlowReturn res;
buffer = buffer_new (data, size);
GST_BUFFER_OFFSET_END (buffer) = granulepos;
res = gst_pad_push (srcpad, buffer);
fail_unless (res == expected_return,
"pushing %s returned %d not %d", name, res, expected_return);
}
static void
check_headers ()
{
/* push the ident header */
push_data ("ident-header", IDENT_HEADER, 29, 0, GST_FLOW_OK);
/* push the cmml start tag */
push_data ("preamble", PREAMBLE, strlen (PREAMBLE), 0, GST_FLOW_OK);
/* push the head tag */
push_data ("head", HEAD_TAG, strlen (HEAD_TAG), 0, GST_FLOW_OK);
current_buf = buffers;
fail_unless_equals_int (g_list_length (current_buf), 2);
/* check the preamble */
check_output_buffer_is_equal ("cmml-preamble-buffer", PREAMBLE_DECODED, 1);
/* check the decoded head tag */
current_buf = current_buf->next;
check_output_buffer_is_equal ("head-tag-buffer", HEAD_TAG_DECODED, 1);
}
static void
push_clip (const gchar * name, const gchar * track, GstClockTime prev,
GstClockTime start, GstClockTime end, GstFlowReturn expected_return)
{
gchar *clip;
gint64 keyindex, keyoffset, granulepos;
if (track == NULL)
track = "default";
keyindex = prev / granulerate << granuleshift;
keyoffset = (start - prev) / granulerate;
granulepos = keyindex + keyoffset;
clip = g_strdup_printf (CLIP_TEMPLATE, name, track);
push_data (name, clip, strlen (clip), granulepos, expected_return);
g_free (clip);
}
static void
check_clip (const gchar * name, const gchar * track,
const gchar * start, const gchar * end)
{
gchar *decoded_clip;
if (track == NULL)
track = "default";
current_buf = current_buf->next;
fail_unless (g_list_length (current_buf));
decoded_clip = g_strdup_printf (CLIP_TEMPLATE_DECODED, name, track, start);
check_output_buffer_is_equal (name, decoded_clip, 1);
g_free (decoded_clip);
}
static void
check_end ()
{
current_buf = current_buf->next;
check_output_buffer_is_equal ("cmml-end-tag", END_TAG, 1);
}
GST_START_TEST (test_dec)
{
GstElement *cmmldec;
cmmldec = setup_cmmldec ();
check_headers ();
push_clip ("clip-1", "default",
0, 1 * GST_SECOND + 234 * GST_MSECOND, 0, GST_FLOW_OK);
push_clip ("clip-2", "othertrack",
0, 4 * GST_SECOND + 321 * GST_MSECOND, 0, GST_FLOW_OK);
push_clip ("clip-3", "default",
1 * GST_SECOND + 234 * GST_MSECOND,
((100 * 3600) + (59 * 60) + 59) * GST_SECOND + 678 * GST_MSECOND, 0,
GST_FLOW_OK);
/* send EOS to flush clip-2 and clip-3 */
gst_pad_send_event (GST_PAD_PEER (srcpad), gst_event_new_eos ());
printf ("Check1\n");
check_clip ("clip-1", "default", "0:00:01.234", NULL);
printf ("Check2\n");
check_clip ("clip-2", "othertrack", "0:00:04.321", NULL);
printf ("Check3\n");
check_clip ("clip-3", "default", "100:59:59.678", NULL);
check_end ();
cleanup_cmmldec (cmmldec);
}
GST_END_TEST;
GST_START_TEST (test_tags)
{
GstElement *cmmldec;
GstBus *bus;
GstMessage *message;
GstTagList *tags;
const GValue *tag_val;
GObject *tag;
gchar *title, *base;
gboolean empty;
gchar *id, *track;
gint64 start_time, end_time;
gchar *anchor_href, *anchor_text;
gchar *img_src, *img_alt;
gchar *desc;
GValueArray *meta;
cmmldec = setup_cmmldec ();
bus = gst_element_get_bus (cmmldec);
check_headers ();
/* read the GstCmmlTagHead tag */
message = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
fail_unless (message != NULL);
gst_message_parse_tag (message, &tags);
fail_unless (tags != NULL);
tag_val = gst_tag_list_get_value_index (tags, GST_TAG_CMML_HEAD, 0);
fail_unless (tag_val != NULL);
tag = g_value_get_object (tag_val);
fail_unless (tags != NULL);
g_object_get (tag, "title", &title, "base-uri", &base, "meta", &meta, NULL);
fail_unless_equals_string ("The Research Hunter", title);
fail_unless (base == NULL);
fail_unless (meta != NULL);
fail_unless_equals_int (meta->n_values, 10);
gst_message_unref (message);
gst_tag_list_free (tags);
g_free (title);
g_free (base);
g_value_array_free (meta);
push_clip ("clip-1", "default",
0, 1 * GST_SECOND + 234 * GST_MSECOND, 0, GST_FLOW_OK);
/* read the GstCmmlTagClip */
message = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
fail_unless (message != NULL);
gst_message_parse_tag (message, &tags);
fail_unless (tags != NULL);
tag_val = gst_tag_list_get_value_index (tags, GST_TAG_CMML_CLIP, 0);
fail_unless (tag_val != NULL);
tag = g_value_get_object (tag_val);
fail_unless (tag != NULL);
g_object_get (tag, "id", &id, "empty", &empty, "track", &track,
"start-time", &start_time, "end-time", &end_time,
"anchor-uri", &anchor_href, "anchor-text", &anchor_text,
"img-uri", &img_src, "img-alt", &img_alt,
"description", &desc, "meta", &meta, NULL);
fail_unless (empty == FALSE);
fail_unless_equals_string (id, "clip-1");
fail_unless_equals_string (track, "default");
fail_unless_equals_int (start_time, 1 * GST_SECOND + 234 * GST_MSECOND);
fail_unless_equals_uint64 (end_time, GST_CLOCK_TIME_NONE);
fail_unless_equals_string (anchor_href, "http://www.csiro.au/");
fail_unless_equals_string (anchor_text, "http://www.csiro.au");
fail_unless_equals_string (img_src, "images/index1.jpg");
fail_unless (img_alt == NULL);
fail_unless_equals_string (desc, "Welcome to CSIRO");
fail_unless (meta != NULL);
fail_unless_equals_int (meta->n_values, 2);
g_free (id);
g_free (track);
g_free (anchor_href);
g_free (anchor_text);
g_free (img_src);
g_free (img_alt);
g_free (desc);
g_value_array_free (meta);
gst_tag_list_free (tags);
gst_message_unref (message);
gst_object_unref (bus);
cleanup_cmmldec (cmmldec);
}
GST_END_TEST;
Suite *
cmmldec_suite ()
{
Suite *s = suite_create ("cmmldec");
TCase *tc_chain = tcase_create ("general");
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, test_dec);
tcase_add_test (tc_chain, test_tags);
return s;
}
int
main (int argc, char **argv)
{
int nf;
Suite *s = cmmldec_suite ();
SRunner *sr = srunner_create (s);
gst_check_init (&argc, &argv);
srunner_run_all (sr, CK_NORMAL);
nf = srunner_ntests_failed (sr);
srunner_free (sr);
return nf;
}

View file

@ -0,0 +1,361 @@
/*
* cmmlenc.c - GStreamer CMML decoder test suite
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#include <gst/check/gstcheck.h>
#define SINK_CAPS "text/x-cmml"
#define SRC_CAPS "text/xml"
#define IDENT_HEADER \
"CMML\x00\x00\x00\x00"\
"\x03\x00\x00\x00"\
"\x01\x00\x00\x00\x00\x00\x00\x00"\
"\xe8\x03\x00\x00\x00\x00\x00\x00"\
"\x20"
#define XML_PREAMBLE \
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"\
"<!DOCTYPE cmml SYSTEM \"cmml.dtd\">\n"
#define START_TAG \
"<cmml>"
#define PROCESSING_INSTRUCTION \
"<?cmml ?>"
#define PREAMBLE \
XML_PREAMBLE START_TAG
#define PREAMBLE_ENCODED \
XML_PREAMBLE PROCESSING_INSTRUCTION
#define STREAM_TAG \
"<stream timebase=\"10\">"\
"<import src=\"test.ogg\"/>"\
"<import src=\"test1.ogg\"/>"\
"</stream>"
#define STREAM_TAG_ENCODED STREAM_TAG
#define HEAD_TAG \
"<head>"\
"<title>The Research Hunter</title>"\
"<meta name=\"DC.audience\" content=\"General\"/>"\
"<meta name=\"DC.author\" content=\"CSIRO Publishing\"/>"\
"<meta name=\"DC.format\" content=\"video\"/>"\
"<meta name=\"DC.language\" content=\"English\"/>"\
"<meta name=\"DC.publisher\" content=\"CSIRO Australia\"/>"\
"</head>"
#define HEAD_TAG_ENCODED HEAD_TAG
#define END_TAG \
"</cmml>"
#define CLIP_TEMPLATE \
"<clip id=\"%s\" track=\"%s\" start=\"%s\">"\
"<a href=\"http://www.annodex.org/\">http://www.annodex.org</a>"\
"<img src=\"images/index.jpg\"/>"\
"<desc>Annodex Foundation</desc>"\
"<meta name=\"test\" content=\"test content\"/>"\
"</clip>"
#define CLIP_TEMPLATE_ENCODED \
"<clip id=\"%s\" track=\"%s\">"\
"<a href=\"http://www.annodex.org/\">http://www.annodex.org</a>"\
"<img src=\"images/index.jpg\"/>"\
"<desc>Annodex Foundation</desc>"\
"<meta name=\"test\" content=\"test content\"/>"\
"</clip>"
GList *buffers;
GList *current_buf = NULL;
guint64 granulerate;
guint8 granuleshift;
GstPad *srcpad, *sinkpad;
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (SINK_CAPS)
);
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (SRC_CAPS)
);
GstBuffer *
buffer_new (const gchar * buffer_data, guint size)
{
GstBuffer *buffer;
GstCaps *caps;
buffer = gst_buffer_new_and_alloc (size);
memcpy (GST_BUFFER_DATA (buffer), buffer_data, size);
caps = gst_caps_from_string (SRC_CAPS);
gst_buffer_set_caps (buffer, caps);
gst_caps_unref (caps);
return buffer;
}
static void
buffer_unref (void *buffer, void *user_data)
{
gst_buffer_unref (GST_BUFFER (buffer));
}
GstElement *
setup_cmmlenc ()
{
GstElement *cmmlenc;
GstBus *bus;
guint64 granulerate_n, granulerate_d;
GST_DEBUG ("setup_cmmlenc");
cmmlenc = gst_check_setup_element ("cmmlenc");
srcpad = gst_check_setup_src_pad (cmmlenc, &srctemplate, NULL);
sinkpad = gst_check_setup_sink_pad (cmmlenc, &sinktemplate, NULL);
bus = gst_bus_new ();
gst_element_set_bus (cmmlenc, bus);
fail_unless (gst_element_set_state (cmmlenc,
GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE,
"could not set to playing");
g_object_get (cmmlenc, "granule-rate-numerator", &granulerate_n,
"granule-rate-denominator", &granulerate_d,
"granule-shift", &granuleshift, NULL);
granulerate = GST_SECOND * granulerate_n / granulerate_d;
buffers = NULL;
return cmmlenc;
}
static void
cleanup_cmmlenc (GstElement * cmmlenc)
{
GstBus *bus;
/* free encoded buffers */
g_list_foreach (buffers, buffer_unref, NULL);
g_list_free (buffers);
buffers = NULL;
bus = GST_ELEMENT_BUS (cmmlenc);
gst_bus_set_flushing (bus, TRUE);
gst_object_unref (bus);
GST_DEBUG ("cleanup_cmmlenc");
gst_check_teardown_src_pad (cmmlenc);
gst_check_teardown_sink_pad (cmmlenc);
gst_check_teardown_element (cmmlenc);
}
static void
check_output_buffer_is_equal (const gchar * name,
const gchar * data, gint refcount)
{
GstBuffer *buffer = GST_BUFFER (current_buf->data);
ASSERT_OBJECT_REFCOUNT (buffer, name, refcount);
fail_unless (memcmp (GST_BUFFER_DATA (buffer), data,
GST_BUFFER_SIZE (buffer)) == 0,
"'%s' (%s) is not equal to (%s)", name, GST_BUFFER_DATA (buffer), data);
}
static void
push_data (const gchar * name,
const gchar * data, gint size, GstFlowReturn expected_return)
{
GstBuffer *buffer;
GstFlowReturn res;
buffer = buffer_new (data, size);
res = gst_pad_push (srcpad, buffer);
fail_unless (res == expected_return,
"pushing %s returned %d not %d", name, res, expected_return);
}
static void
check_headers ()
{
/* push the cmml start tag */
push_data ("preamble", PREAMBLE, strlen (PREAMBLE), GST_FLOW_OK);
/* push the stream tag */
push_data ("stream", STREAM_TAG, strlen (STREAM_TAG), GST_FLOW_OK);
/* push the head tag */
push_data ("head", HEAD_TAG, strlen (HEAD_TAG), GST_FLOW_OK);
/* should output the cmml ident header and the cmml start tag transformed
* into a processing instruction */
current_buf = buffers;
fail_unless_equals_int (g_list_length (current_buf), 3);
/* check the ident header */
check_output_buffer_is_equal ("cmml-ident-buffer", IDENT_HEADER, 2);
/* check the cmml processing instruction */
current_buf = current_buf->next;
check_output_buffer_is_equal ("cmml-preamble-buffer", PREAMBLE_ENCODED, 2);
/* check the encoded head tag */
current_buf = current_buf->next;
check_output_buffer_is_equal ("head-tag-buffer", HEAD_TAG_ENCODED, 2);
}
static void
push_clip (const gchar * name, const gchar * track,
const gchar * start, const gchar * end, GstFlowReturn expected_return)
{
gchar *clip;
if (track == NULL)
track = "default";
clip = g_strdup_printf (CLIP_TEMPLATE, name, track, start, end);
push_data (name, clip, strlen (clip), expected_return);
g_free (clip);
}
static void
check_clip (const gchar * name, const gchar * track,
GstClockTime start, GstClockTime prev)
{
gchar *encoded_clip;
GstBuffer *buffer;
gint64 keyindex, keyoffset, granulepos;
if (track == NULL)
track = "default";
current_buf = current_buf->next;
fail_unless (g_list_length (current_buf));
encoded_clip = g_strdup_printf (CLIP_TEMPLATE_ENCODED, name, track);
check_output_buffer_is_equal (name, encoded_clip, 1);
g_free (encoded_clip);
buffer = GST_BUFFER (current_buf->data);
granulepos = GST_BUFFER_OFFSET_END (GST_BUFFER (buffer));
keyindex = granulepos >> granuleshift;
keyoffset = granulepos - (keyindex << granuleshift);
fail_unless_equals_uint64 (keyindex * granulerate, prev);
fail_unless_equals_uint64 ((keyindex + keyoffset) * granulerate, start);
}
static void
push_end ()
{
push_data ("end", END_TAG, strlen (END_TAG), GST_FLOW_OK);
}
static void
check_end ()
{
/* should output the EOS page */
current_buf = current_buf->next;
fail_unless_equals_int (g_list_length (current_buf), 1);
check_output_buffer_is_equal ("cmml-eos-buffer", NULL, 1);
}
GST_START_TEST (test_enc)
{
GstElement *cmmlenc;
cmmlenc = setup_cmmlenc ();
check_headers ();
push_clip ("clip-1", "default", "1.234", NULL, GST_FLOW_OK);
check_clip ("clip-1", "default", 1234 * granulerate, 0);
push_clip ("clip-2", NULL, "5.678", NULL, GST_FLOW_OK);
check_clip ("clip-2", "default", 5678 * granulerate, 1234 * granulerate);
push_clip ("clip-3", "othertrack", "9.123", NULL, GST_FLOW_OK);
check_clip ("clip-3", "othertrack", 9123 * granulerate, 0);
push_end ();
check_end ();
cleanup_cmmlenc (cmmlenc);
}
GST_END_TEST;
GST_START_TEST (test_bad_start_time)
{
GstElement *cmmlenc;
cmmlenc = setup_cmmlenc ();
check_headers ();
push_clip ("clip-1", "default", "1.234", NULL, GST_FLOW_OK);
check_clip ("clip-1", "default", 1234 * granulerate, 0);
push_clip ("clip-bad", "default", "1.1000", NULL, GST_FLOW_ERROR);
push_clip ("clip-2", NULL, "5.678", NULL, GST_FLOW_OK);
check_clip ("clip-2", "default", 5678 * granulerate, 1234 * granulerate);
push_clip ("clip-3", "othertrack", "9.123", NULL, GST_FLOW_OK);
check_clip ("clip-3", "othertrack", 9123 * granulerate, 0);
push_end ();
check_end ();
cleanup_cmmlenc (cmmlenc);
}
GST_END_TEST static Suite *
cmmlenc_suite ()
{
Suite *s = suite_create ("cmmlenc");
TCase *tc_chain = tcase_create ("general");
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, test_enc);
tcase_add_test (tc_chain, test_bad_start_time);
return s;
}
int
main (int argc, char **argv)
{
int nf;
Suite *s = cmmlenc_suite ();
SRunner *sr = srunner_create (s);
gst_check_init (&argc, &argv);
srunner_run_all (sr, CK_NORMAL);
nf = srunner_ntests_failed (sr);
srunner_free (sr);
return nf;
}

View file

@ -0,0 +1,258 @@
/*
* skeldec.c - GStreamer annodex skeleton decoder test suite
* Copyright (C) 2005 Alessandro Decina
*
* Authors:
* Alessandro Decina <alessandro@nnva.org>
*
* 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.
*/
#include <gst/check/gstcheck.h>
GList *buffers;
GstPad *srcpad, *sinkpad;
#define SKELETON_CAPS "application/x-ogg-skeleton"
#define SKELETON_FISHEAD \
"fishead\0"\
"\x03\0\0\0"\
"\x39\x30\0\0\0\0\0\0"\
"\x39\x30\0\0\0\0\0\0"\
"\x39\x30\0\0\0\0\0\0"\
"\x39\x30\0\0\0\0\0\0"\
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
#define SKELETON_FISHEAD_SIZE 64
#define SKELETON_FISBONE \
"fisbone\0"\
"\x2c\0\0\0"\
"\x39\x30\0\0"\
"\x39\x30\0\0"\
"\x39\x30\0\0\0\0\0\0"\
"\x39\x30\0\0\0\0\0\0"\
"\x39\x30\0\0\0\0\0\0"\
"\x39\x30\0\0"\
"\x20"\
"\0\0\0"\
"Content-Type: application/ogg; UTF-8\r\n"
#define SKELETON_FISBONE_SIZE 90
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS (SKELETON_CAPS)
);
static GstElement *
setup_skeldec ()
{
GstElement *skeldec;
GST_DEBUG ("setup_skeldec");
skeldec = gst_check_setup_element ("skeldec");
srcpad = gst_check_setup_src_pad (skeldec, &srctemplate, NULL);
sinkpad = gst_check_setup_sink_pad (skeldec, &sinktemplate, NULL);
return skeldec;
}
static void
cleanup_skeldec (GstElement * skeldec)
{
GST_DEBUG ("cleanup_skeldec");
gst_check_teardown_src_pad (skeldec);
gst_check_teardown_sink_pad (skeldec);
gst_check_teardown_element (skeldec);
}
static void
skel_buffer_unref (void *buf, void *user_data)
{
GstBuffer *buffer = GST_BUFFER (buf);
ASSERT_OBJECT_REFCOUNT (buffer, "skel-buffer", 1);
gst_buffer_unref (buffer);
}
static GstBuffer *
skel_buffer_new (gchar * buffer_data, guint size)
{
GstBuffer *buffer;
GstCaps *caps;
buffer = gst_buffer_new_and_alloc (size);
memcpy (GST_BUFFER_DATA (buffer), buffer_data, size);
caps = gst_caps_from_string (SKELETON_CAPS);
gst_buffer_set_caps (buffer, caps);
gst_caps_unref (caps);
return buffer;
}
GST_START_TEST (test_dec)
{
GstElement *skeldec;
GstBus *bus;
GstBuffer *inbuffer;
GstMessage *message;
GstTagList *tags;
const GValue *tag_val, *val;
GObject *tag;
gint major, minor;
gint64 prestime_n, prestime_d;
gint64 basetime_n, basetime_d;
guint serial_number;
gint64 granule_rate_n, granule_rate_d;
gint64 granule_start;
guint64 preroll;
guint granule_shift;
GValueArray *headers;
gchar *content_type;
gchar *encoding;
skeldec = setup_skeldec ();
bus = gst_bus_new ();
gst_element_set_bus (skeldec, bus);
fail_unless (gst_element_set_state (skeldec,
GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
"could not set to playing");
/* test the fishead */
inbuffer = skel_buffer_new (SKELETON_FISHEAD, SKELETON_FISHEAD_SIZE);
fail_unless_equals_int (gst_pad_push (srcpad, inbuffer), GST_FLOW_OK);
message = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
fail_unless (GST_MESSAGE_SRC (message) == GST_OBJECT (skeldec));
gst_message_parse_tag (message, &tags);
fail_unless (tags != NULL);
fail_unless_equals_int (gst_tag_list_get_tag_size (tags,
GST_TAG_SKELETON_FISHEAD), 1);
tag_val = gst_tag_list_get_value_index (tags, GST_TAG_SKELETON_FISHEAD, 0);
fail_unless (tag_val != NULL);
tag = g_value_get_object (tag_val);
fail_unless (tag != NULL);
g_object_get (tag,
"version-major", &major, "version-minor", &minor,
"presentation-time-numerator", &prestime_n,
"presentation-time-denominator", &prestime_d,
"base-time-numerator", &basetime_n,
"base-time-denominator", &basetime_d, NULL);
fail_unless_equals_int (major, 3);
fail_unless_equals_int (minor, 0);
fail_unless_equals_int (prestime_n, 12345);
fail_unless_equals_int (prestime_d, 12345);
fail_unless_equals_int (basetime_n, 12345);
fail_unless_equals_int (basetime_d, 12345);
gst_tag_list_free (tags);
gst_message_unref (message);
/* test the fisbone */
inbuffer = skel_buffer_new (SKELETON_FISBONE, SKELETON_FISBONE_SIZE);
fail_unless_equals_int (gst_pad_push (srcpad, inbuffer), GST_FLOW_OK);
message = gst_bus_poll (bus, GST_MESSAGE_TAG, -1);
fail_unless (GST_MESSAGE_SRC (message) == GST_OBJECT (skeldec));
gst_message_parse_tag (message, &tags);
fail_unless (tags != NULL);
fail_unless_equals_int (gst_tag_list_get_tag_size (tags,
GST_TAG_SKELETON_FISBONE), 1);
tag_val = gst_tag_list_get_value_index (tags, GST_TAG_SKELETON_FISBONE, 0);
fail_unless (tag_val != NULL);
tag = g_value_get_object (tag_val);
fail_unless (tag != NULL);
g_object_get (tag, "serial-number", &serial_number,
"granule-rate-numerator", &granule_rate_n,
"granule-rate-denominator", &granule_rate_d,
"granule-start", &granule_start,
"granule-shift", &granule_shift,
"preroll", &preroll,
"headers", &headers,
"content-type", &content_type, "encoding", &encoding, NULL);
fail_unless_equals_int (serial_number, 12345);
fail_unless_equals_int (granule_rate_n, 12345);
fail_unless_equals_int (granule_rate_d, 12345);
fail_unless_equals_int (granule_start, 12345);
fail_unless_equals_int (preroll, 12345);
fail_unless_equals_int (granule_shift, 32);
fail_unless_equals_int (headers->n_values, 2);
fail_unless_equals_string (content_type, "application/ogg");
fail_unless_equals_string (encoding, "UTF-8");
g_value_array_free (headers);
g_free (content_type);
g_free (encoding);
gst_tag_list_free (tags);
gst_message_unref (message);
gst_bus_set_flushing (bus, TRUE);
gst_object_unref (bus);
g_list_foreach (buffers, skel_buffer_unref, NULL);
cleanup_skeldec (skeldec);
}
GST_END_TEST;
static Suite *
skeldec_suite ()
{
Suite *s = suite_create ("skeldec");
TCase *tc_chain = tcase_create ("general");
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, test_dec);
return s;
}
int
main (int argc, char **argv)
{
int nf;
Suite *s = skeldec_suite ();
SRunner *sr = srunner_create (s);
gst_check_init (&argc, &argv);
srunner_run_all (sr, CK_NORMAL);
nf = srunner_ntests_failed (sr);
srunner_free (sr);
return nf;
}