rendered paste body/* GStreamer
* Copyright (C) <2007> Wouter Cloetens <wouter@mind.be>
* Loosely based on neonhttpsrc:
* Copyright (C) <2005> Edgard Lima <edgard.lima@indt.org.br>
* Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
* Copyright (C) <2006> Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
*
* 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <libsoup/soup.h>
#include "gstsouphttpsrc.h"
GST_DEBUG_CATEGORY_STATIC (souphttpsrc_debug);
#define GST_CAT_DEFAULT souphttpsrc_debug
static const GstElementDetails gst_souphttp_src_details =
GST_ELEMENT_DETAILS ("HTTP client source",
"Source/Network",
"Receive data as a client over the network via HTTP using SOUP",
"Wouter Cloetens <wouter@mind.be>");
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY);
enum
{
PROP_0,
PROP_LOCATION,
};
static void gst_souphttp_src_dispose (GObject * gobject);
static void gst_souphttp_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_souphttp_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static GstFlowReturn gst_souphttp_src_create (GstPushSrc * psrc,
GstBuffer ** outbuf);
static gboolean gst_souphttp_src_start (GstBaseSrc * bsrc);
static gboolean gst_souphttp_src_stop (GstBaseSrc * bsrc);
static gboolean gst_souphttp_src_get_size (GstBaseSrc * bsrc, guint64 * size);
static gboolean gst_souphttp_src_is_seekable (GstBaseSrc * bsrc);
static gboolean gst_souphttp_src_do_seek (GstBaseSrc * bsrc,
GstSegment * segment);
static gboolean gst_souphttp_src_set_location (GstSouphttpSrc * src,
const gchar * uri);
static void soup_got_headers (SoupMessage *msg, GstSouphttpSrc *src);
static void soup_got_chunk (SoupMessage *msg, GstSouphttpSrc *src);
static void soup_response (SoupMessage *msg, gpointer user_data);
static void
_do_init (GType type)
{
GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
"SOUP HTTP src");
}
GST_BOILERPLATE_FULL (GstSouphttpSrc, gst_souphttp_src, GstPushSrc,
GST_TYPE_PUSH_SRC, _do_init);
static void
gst_souphttp_src_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 (&srctemplate));
gst_element_class_set_details (element_class, &gst_souphttp_src_details);
}
static void
gst_souphttp_src_class_init (GstSouphttpSrcClass * klass)
{
GObjectClass *gobject_class;
GstBaseSrcClass *gstbasesrc_class;
GstPushSrcClass *gstpushsrc_class;
gobject_class = (GObjectClass *) klass;
gstbasesrc_class = (GstBaseSrcClass *) klass;
gstpushsrc_class = (GstPushSrcClass *) klass;
gobject_class->set_property = gst_souphttp_src_set_property;
gobject_class->get_property = gst_souphttp_src_get_property;
gobject_class->dispose = gst_souphttp_src_dispose;
g_object_class_install_property
(gobject_class, PROP_LOCATION,
g_param_spec_string ("location", "Location",
"Location to read from", "", G_PARAM_READWRITE));
gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_souphttp_src_start);
gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_souphttp_src_stop);
// gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_souphttp_src_get_size);
// gstbasesrc_class->is_seekable =
// GST_DEBUG_FUNCPTR (gst_souphttp_src_is_seekable);
// gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_souphttp_src_do_seek);
gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_souphttp_src_create);
GST_DEBUG_CATEGORY_INIT (souphttpsrc_debug, "souphttpsrc", 0,
"SOUP HTTP Client Source");
}
static void
gst_souphttp_src_init (GstSouphttpSrc * src, GstSouphttpSrcClass * g_class)
{
src->location = NULL;
src->loop = NULL;
src->session = NULL;
src->msg = NULL;
}
static void
gst_souphttp_src_dispose (GObject * gobject)
{
GstSouphttpSrc *src = GST_SOUPHTTP_SRC (gobject);
if (src->location) {
g_free (src->location);
}
if (src->session) {
soup_session_abort (src->session); /* This unrefs the message. */
g_object_unref (src->session);
src->session = NULL;
src->msg = NULL;
}
if (src->loop) {
g_main_loop_unref (src->loop);
src->loop = NULL;
}
G_OBJECT_CLASS (parent_class)->dispose (gobject);
}
static void
gst_souphttp_src_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstSouphttpSrc *src = GST_SOUPHTTP_SRC (object);
switch (prop_id) {
case PROP_LOCATION:
{
const gchar *location;
location = g_value_get_string (value);
if (location == NULL) {
GST_WARNING ("location property cannot be NULL");
goto done;
}
if (!gst_souphttp_src_set_location (src, location)) {
GST_WARNING ("badly formatted location");
goto done;
}
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
done:
return;
}
static void
gst_souphttp_src_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstSouphttpSrc *souphttpsrc = GST_SOUPHTTP_SRC (object);
switch (prop_id) {
case PROP_LOCATION:
g_value_set_string (value, souphttpsrc->location);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static GstFlowReturn
gst_souphttp_src_create (GstPushSrc * psrc, GstBuffer ** outbuf)
{
GstSouphttpSrc *src;
src = GST_SOUPHTTP_SRC (psrc);
src->ret = GST_FLOW_CUSTOM_ERROR;
src->outbuf = outbuf;
do {
if (!src->session) {
GST_DEBUG_OBJECT (src, "EOS reached");
return GST_FLOW_UNEXPECTED;
}
switch (src->msg->status_code)
{
case SOUP_MESSAGE_STATUS_IDLE:
GST_DEBUG_OBJECT (src, "create(): status: IDLE");
soup_session_queue_message (src->session, src->msg,
soup_response, src);
break;
case SOUP_MESSAGE_STATUS_QUEUED:
GST_DEBUG_OBJECT (src, "create(): status: QUEUED");
break;
case SOUP_MESSAGE_STATUS_CONNECTING:
GST_DEBUG_OBJECT (src, "create(): status: CONNECTING");
break;
case SOUP_MESSAGE_STATUS_RUNNING:
GST_DEBUG_OBJECT (src, "create(): status: RUNNING");
break;
case SOUP_MESSAGE_STATUS_FINISHED:
GST_DEBUG_OBJECT (src, "create(): status: FINISHED");
if (src->session) {
soup_session_abort (src->session); /* This unrefs the message. */
g_object_unref (src->session);
src->session = NULL;
src->msg = NULL;
}
return GST_FLOW_UNEXPECTED;
}
g_main_loop_run (src->loop);
} while (src->ret == GST_FLOW_CUSTOM_ERROR);
GST_DEBUG_OBJECT (src, "create() returns %d", src->ret);
switch (src->msg->status_code)
{
case SOUP_MESSAGE_STATUS_IDLE:
GST_DEBUG_OBJECT (src, "status: IDLE");
break;
case SOUP_MESSAGE_STATUS_QUEUED:
GST_DEBUG_OBJECT (src, "status: QUEUED");
break;
case SOUP_MESSAGE_STATUS_CONNECTING:
GST_DEBUG_OBJECT (src, "status: CONNECTING");
break;
case SOUP_MESSAGE_STATUS_RUNNING:
GST_DEBUG_OBJECT (src, "status: RUNNING");
break;
case SOUP_MESSAGE_STATUS_FINISHED:
GST_DEBUG_OBJECT (src, "status: FINISHED");
break;
}
return src->ret;
}
/* create a socket for connecting to remote server */
static gboolean
gst_souphttp_src_start (GstBaseSrc * bsrc)
{
GstSouphttpSrc *src = GST_SOUPHTTP_SRC (bsrc);
gpointer *p;
if (!src->location) {
GST_ELEMENT_ERROR (src, LIBRARY, INIT,
(NULL), ("Missing location property"));
return FALSE;
}
src->loop = g_main_loop_new (NULL, TRUE);
if (!src->loop)
{
GST_ELEMENT_ERROR (src, LIBRARY, INIT,
(NULL), ("Failed to start GMainLoop"));
return FALSE;
}
src->session = g_object_new (SOUP_TYPE_SESSION_ASYNC,
SOUP_SESSION_ASYNC_CONTEXT, src->loop,
NULL);
// src->session = soup_session_async_new_with_options (
// SOUP_SESSION_ASYNC_CONTEXT, src->loop,
// NULL);
g_object_get(G_OBJECT(src->session), SOUP_SESSION_ASYNC_CONTEXT, &p, NULL);
GST_DEBUG_OBJECT (src, "new session, async_context = %p, should be mainloop = %p",
p, src->loop);
if (!src->session)
{
GST_ELEMENT_ERROR (src, LIBRARY, INIT,
(NULL), ("Failed to create async session"));
return FALSE;
}
src->msg = soup_message_new (SOUP_METHOD_GET, src->location);
if (!src->msg) {
GST_ELEMENT_ERROR (src, LIBRARY, INIT,
(NULL), ("Error parsing URL"));
return FALSE;
}
g_signal_connect (src->msg, "got_headers",
G_CALLBACK (soup_got_headers), src);
g_signal_connect (src->msg, "got_chunk",
G_CALLBACK (soup_got_chunk), src);
soup_message_set_flags (src->msg, SOUP_MESSAGE_OVERWRITE_CHUNKS);
return TRUE;
}
/* close the socket and associated resources
* used both to recover from errors and go to NULL state */
static gboolean
gst_souphttp_src_stop (GstBaseSrc * bsrc)
{
GstSouphttpSrc *src;
src = GST_SOUPHTTP_SRC (bsrc);
GST_DEBUG_OBJECT (src, "stop()");
if (src->session) {
soup_session_abort (src->session); /* This unrefs the message. */
g_object_unref (src->session);
src->session = NULL;
src->msg = NULL;
}
return TRUE;
}
static gboolean
gst_souphttp_src_get_size (GstBaseSrc * bsrc, guint64 * size)
{
GST_DEBUG_OBJECT (bsrc, "get_size()");
return FALSE;
}
static gboolean
gst_souphttp_src_is_seekable (GstBaseSrc * bsrc)
{
GST_DEBUG_OBJECT (bsrc, "is_seekable()");
return FALSE;
}
static gboolean
gst_souphttp_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
{
GST_DEBUG_OBJECT (bsrc, "do_seek()");
return FALSE;
}
static gboolean
gst_souphttp_src_set_location (GstSouphttpSrc * src, const gchar * uri)
{
if (src->location) {
g_free (src->location);
src->location = NULL;
}
src->location = g_strdup(uri);
return TRUE;
}
/* DNS lookup complete, socket connected, request sent, headers received. */
static void
soup_got_headers (SoupMessage *msg, GstSouphttpSrc *src)
{
GST_DEBUG_OBJECT (src, "got headers");
g_main_loop_quit(src->loop);
}
static void
soup_got_chunk (SoupMessage *msg, GstSouphttpSrc *src)
{
GstBaseSrc *basesrc;
basesrc = GST_BASE_SRC_CAST (src);
GST_DEBUG_OBJECT (src, "got chunk of %d bytes", msg->response.length);
/* Create the buffer. */
src->ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (basesrc),
basesrc->segment.last_stop, msg->response.length,
GST_PAD_CAPS (GST_BASE_SRC_PAD (basesrc)), src->outbuf);
memcpy(GST_BUFFER_DATA (*src->outbuf), msg->response.body, msg->response.length);
g_main_loop_quit(src->loop);
}
static void
soup_response (SoupMessage *msg, gpointer user_data)
{
GstSouphttpSrc *src = (GstSouphttpSrc *)user_data;
GST_DEBUG_OBJECT (src, "got response %d: %s", msg->status_code, msg->reason_phrase);
g_main_loop_quit(src->loop);
}
/* entry point to initialize the plug-in
* initialize the plug-in itself
* register the element factories and pad templates
* register the features
*/
static gboolean
plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "souphttpsrc", GST_RANK_NONE,
GST_TYPE_SOUPHTTP_SRC);
}
/* this is the structure that gst-register looks for
* so keep the name plugin_desc, or you cannot get your plug-in registered */
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
GST_VERSION_MINOR,
"soup",
"libsoup http client src",
plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")