2010-06-01 23:45:06 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
|
|
|
|
* 2000 Wim Taymans <wtay@chello.be>
|
|
|
|
* 2002 Kristian Rietveld <kris@gtk.org>
|
|
|
|
* 2002,2003 Colin Walters <walters@gnu.org>
|
2010-06-04 20:04:53 +00:00
|
|
|
* 2001,2010 Bastien Nocera <hadess@hadess.net>
|
|
|
|
* 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
2010-06-01 23:45:06 +00:00
|
|
|
*
|
|
|
|
* rtmpsrc.c:
|
|
|
|
*
|
|
|
|
* 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
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-06-01 23:45:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:element-rtmpsrc
|
2017-03-08 18:01:13 +00:00
|
|
|
* @title: rtmpsrc
|
2010-06-01 23:45:06 +00:00
|
|
|
*
|
|
|
|
* This plugin reads data from a local or remote location specified
|
|
|
|
* by an URI. This location can be specified using any protocol supported by
|
2010-06-04 20:04:53 +00:00
|
|
|
* the RTMP library, i.e. rtmp, rtmpt, rtmps, rtmpe, rtmfp, rtmpte and rtmpts.
|
2010-06-01 23:45:06 +00:00
|
|
|
*
|
2017-03-08 18:01:13 +00:00
|
|
|
* ## Example launch lines
|
2010-06-01 23:45:06 +00:00
|
|
|
* |[
|
2015-12-14 02:09:46 +00:00
|
|
|
* gst-launch-1.0 -v rtmpsrc location=rtmp://somehost/someurl ! fakesink
|
2010-06-04 20:04:53 +00:00
|
|
|
* ]| Open an RTMP location and pass its content to fakesink.
|
2017-03-08 18:01:13 +00:00
|
|
|
*
|
2010-06-01 23:45:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib/gi18n-lib.h>
|
|
|
|
|
|
|
|
#include "gstrtmpsrc.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2011-10-06 15:43:19 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
|
|
|
|
2010-06-01 23:45:06 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (rtmpsrc_debug);
|
|
|
|
#define GST_CAT_DEFAULT rtmpsrc_debug
|
|
|
|
|
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2010-06-04 20:04:53 +00:00
|
|
|
PROP_0,
|
2016-03-27 01:11:45 +00:00
|
|
|
PROP_LOCATION,
|
|
|
|
PROP_TIMEOUT
|
2012-02-09 00:09:36 +00:00
|
|
|
#if 0
|
|
|
|
PROP_SWF_URL,
|
2010-06-04 20:36:16 +00:00
|
|
|
PROP_PAGE_URL
|
2012-02-09 00:09:36 +00:00
|
|
|
#endif
|
2010-06-01 23:45:06 +00:00
|
|
|
};
|
|
|
|
|
2012-02-09 00:09:36 +00:00
|
|
|
#define DEFAULT_LOCATION NULL
|
2016-03-27 01:11:45 +00:00
|
|
|
#define DEFAULT_TIMEOUT 120
|
2012-02-09 00:09:36 +00:00
|
|
|
|
2010-06-01 23:45:06 +00:00
|
|
|
static void gst_rtmp_src_uri_handler_init (gpointer g_iface,
|
|
|
|
gpointer iface_data);
|
|
|
|
|
|
|
|
static void gst_rtmp_src_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_rtmp_src_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2010-06-04 20:04:53 +00:00
|
|
|
static void gst_rtmp_src_finalize (GObject * object);
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2017-08-07 11:25:26 +00:00
|
|
|
static gboolean gst_rtmp_src_connect (GstRTMPSrc * src);
|
2013-04-02 02:49:00 +00:00
|
|
|
static gboolean gst_rtmp_src_unlock (GstBaseSrc * src);
|
2010-06-01 23:45:06 +00:00
|
|
|
static gboolean gst_rtmp_src_stop (GstBaseSrc * src);
|
|
|
|
static gboolean gst_rtmp_src_start (GstBaseSrc * src);
|
|
|
|
static gboolean gst_rtmp_src_is_seekable (GstBaseSrc * src);
|
2010-06-06 13:24:23 +00:00
|
|
|
static gboolean gst_rtmp_src_prepare_seek_segment (GstBaseSrc * src,
|
|
|
|
GstEvent * event, GstSegment * segment);
|
|
|
|
static gboolean gst_rtmp_src_do_seek (GstBaseSrc * src, GstSegment * segment);
|
2010-06-04 20:04:53 +00:00
|
|
|
static GstFlowReturn gst_rtmp_src_create (GstPushSrc * pushsrc,
|
|
|
|
GstBuffer ** buffer);
|
2010-06-01 23:45:06 +00:00
|
|
|
static gboolean gst_rtmp_src_query (GstBaseSrc * src, GstQuery * query);
|
|
|
|
|
2011-10-08 09:40:25 +00:00
|
|
|
#define gst_rtmp_src_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstRTMPSrc, gst_rtmp_src, GST_TYPE_PUSH_SRC,
|
|
|
|
G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
|
|
|
|
gst_rtmp_src_uri_handler_init));
|
2010-06-01 23:45:06 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtmp_src_class_init (GstRTMPSrcClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2011-10-08 09:40:25 +00:00
|
|
|
GstElementClass *gstelement_class;
|
2010-06-01 23:45:06 +00:00
|
|
|
GstBaseSrcClass *gstbasesrc_class;
|
2010-06-04 20:04:53 +00:00
|
|
|
GstPushSrcClass *gstpushsrc_class;
|
2010-06-01 23:45:06 +00:00
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
2011-10-08 09:40:25 +00:00
|
|
|
gstelement_class = GST_ELEMENT_CLASS (klass);
|
2010-06-01 23:45:06 +00:00
|
|
|
gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
|
2010-06-04 20:04:53 +00:00
|
|
|
gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
|
2010-06-01 23:45:06 +00:00
|
|
|
|
|
|
|
gobject_class->finalize = gst_rtmp_src_finalize;
|
|
|
|
gobject_class->set_property = gst_rtmp_src_set_property;
|
|
|
|
gobject_class->get_property = gst_rtmp_src_get_property;
|
|
|
|
|
|
|
|
/* properties */
|
2012-02-09 00:09:36 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_LOCATION,
|
|
|
|
g_param_spec_string ("location", "RTMP Location",
|
|
|
|
"Location of the RTMP url to read",
|
|
|
|
DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2016-03-27 01:11:45 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_TIMEOUT,
|
|
|
|
g_param_spec_int ("timeout", "RTMP Timeout",
|
|
|
|
"Time without receiving any data from the server to wait before to timeout the session",
|
|
|
|
0, G_MAXINT,
|
|
|
|
DEFAULT_TIMEOUT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
|
2011-10-08 09:40:25 +00:00
|
|
|
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2011-10-08 09:40:25 +00:00
|
|
|
"RTMP Source",
|
|
|
|
"Source/File",
|
|
|
|
"Read RTMP streams",
|
|
|
|
"Bastien Nocera <hadess@hadess.net>, "
|
|
|
|
"Sebastian Dröge <sebastian.droege@collabora.co.uk>");
|
|
|
|
|
2010-06-01 23:45:06 +00:00
|
|
|
gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_rtmp_src_start);
|
|
|
|
gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_rtmp_src_stop);
|
2013-04-02 02:49:00 +00:00
|
|
|
gstbasesrc_class->unlock = GST_DEBUG_FUNCPTR (gst_rtmp_src_unlock);
|
2010-06-01 23:45:06 +00:00
|
|
|
gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_rtmp_src_is_seekable);
|
2010-06-06 13:24:23 +00:00
|
|
|
gstbasesrc_class->prepare_seek_segment =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_rtmp_src_prepare_seek_segment);
|
|
|
|
gstbasesrc_class->do_seek = GST_DEBUG_FUNCPTR (gst_rtmp_src_do_seek);
|
2010-06-04 20:04:53 +00:00
|
|
|
gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_rtmp_src_create);
|
2010-06-01 23:45:06 +00:00
|
|
|
gstbasesrc_class->query = GST_DEBUG_FUNCPTR (gst_rtmp_src_query);
|
2011-10-08 09:40:25 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (rtmpsrc_debug, "rtmpsrc", 0, "RTMP Source");
|
2010-06-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-08 09:40:25 +00:00
|
|
|
gst_rtmp_src_init (GstRTMPSrc * rtmpsrc)
|
2010-06-01 23:45:06 +00:00
|
|
|
{
|
2011-10-06 15:43:19 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
WSADATA wsa_data;
|
|
|
|
|
|
|
|
if (WSAStartup (MAKEWORD (2, 2), &wsa_data) != 0) {
|
|
|
|
GST_ERROR_OBJECT (rtmpsrc, "WSAStartup failed: 0x%08x", WSAGetLastError ());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-06-06 11:57:06 +00:00
|
|
|
rtmpsrc->cur_offset = 0;
|
|
|
|
rtmpsrc->last_timestamp = 0;
|
2016-03-27 01:11:45 +00:00
|
|
|
rtmpsrc->timeout = DEFAULT_TIMEOUT;
|
2010-06-06 13:24:23 +00:00
|
|
|
|
|
|
|
gst_base_src_set_format (GST_BASE_SRC (rtmpsrc), GST_FORMAT_TIME);
|
2010-06-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtmp_src_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstRTMPSrc *rtmpsrc = GST_RTMP_SRC (object);
|
|
|
|
|
|
|
|
g_free (rtmpsrc->uri);
|
|
|
|
rtmpsrc->uri = NULL;
|
|
|
|
|
2011-10-06 15:43:19 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
WSACleanup ();
|
|
|
|
#endif
|
|
|
|
|
2010-06-01 23:45:06 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* URI interface support.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static GstURIType
|
2011-10-08 09:40:25 +00:00
|
|
|
gst_rtmp_src_uri_get_type (GType type)
|
2010-06-01 23:45:06 +00:00
|
|
|
{
|
|
|
|
return GST_URI_SRC;
|
|
|
|
}
|
|
|
|
|
2011-11-13 23:55:56 +00:00
|
|
|
static const gchar *const *
|
2011-10-08 09:40:25 +00:00
|
|
|
gst_rtmp_src_uri_get_protocols (GType type)
|
2010-06-01 23:45:06 +00:00
|
|
|
{
|
2011-11-13 23:55:56 +00:00
|
|
|
static const gchar *protocols[] =
|
|
|
|
{ "rtmp", "rtmpt", "rtmps", "rtmpe", "rtmfp", "rtmpte", "rtmpts", NULL };
|
|
|
|
|
2010-06-01 23:45:06 +00:00
|
|
|
return protocols;
|
|
|
|
}
|
|
|
|
|
2011-11-13 23:55:56 +00:00
|
|
|
static gchar *
|
2010-06-01 23:45:06 +00:00
|
|
|
gst_rtmp_src_uri_get_uri (GstURIHandler * handler)
|
|
|
|
{
|
|
|
|
GstRTMPSrc *src = GST_RTMP_SRC (handler);
|
|
|
|
|
2011-11-13 23:55:56 +00:00
|
|
|
/* FIXME: make thread-safe */
|
|
|
|
return g_strdup (src->uri);
|
2010-06-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-13 23:55:56 +00:00
|
|
|
gst_rtmp_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
|
|
|
|
GError ** error)
|
2010-06-01 23:45:06 +00:00
|
|
|
{
|
|
|
|
GstRTMPSrc *src = GST_RTMP_SRC (handler);
|
|
|
|
|
2011-11-13 23:55:56 +00:00
|
|
|
if (GST_STATE (src) >= GST_STATE_PAUSED) {
|
|
|
|
g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
|
|
|
|
"Changing the URI on rtmpsrc when it is running is not supported");
|
2010-06-01 23:45:06 +00:00
|
|
|
return FALSE;
|
2011-11-13 23:55:56 +00:00
|
|
|
}
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2010-06-04 20:04:53 +00:00
|
|
|
g_free (src->uri);
|
|
|
|
src->uri = NULL;
|
|
|
|
|
|
|
|
if (uri != NULL) {
|
2010-06-06 06:30:09 +00:00
|
|
|
int protocol;
|
|
|
|
AVal host;
|
|
|
|
unsigned int port;
|
|
|
|
AVal playpath, app;
|
2010-06-04 20:04:53 +00:00
|
|
|
|
2010-06-23 19:46:42 +00:00
|
|
|
if (!RTMP_ParseURL (uri, &protocol, &host, &port, &playpath, &app) ||
|
|
|
|
!host.av_len || !playpath.av_len) {
|
2010-06-06 06:30:09 +00:00
|
|
|
GST_ERROR_OBJECT (src, "Failed to parse URI %s", uri);
|
2011-11-13 23:55:56 +00:00
|
|
|
g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
|
|
|
|
"Could not parse RTMP URI");
|
2015-10-01 09:18:52 +00:00
|
|
|
/* FIXME: we should not be freeing RTMP internals to avoid leaking */
|
|
|
|
free (playpath.av_val);
|
2010-06-04 20:04:53 +00:00
|
|
|
return FALSE;
|
2010-06-04 20:36:16 +00:00
|
|
|
}
|
2015-10-01 09:18:52 +00:00
|
|
|
free (playpath.av_val);
|
2010-06-04 20:36:16 +00:00
|
|
|
src->uri = g_strdup (uri);
|
2010-06-04 20:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (src, "Changed URI to %s", GST_STR_NULL (uri));
|
2010-06-01 23:45:06 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtmp_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
|
|
|
|
{
|
|
|
|
GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
|
|
|
|
|
|
|
|
iface->get_type = gst_rtmp_src_uri_get_type;
|
|
|
|
iface->get_protocols = gst_rtmp_src_uri_get_protocols;
|
|
|
|
iface->get_uri = gst_rtmp_src_uri_get_uri;
|
|
|
|
iface->set_uri = gst_rtmp_src_uri_set_uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtmp_src_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstRTMPSrc *src;
|
|
|
|
|
|
|
|
src = GST_RTMP_SRC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2010-06-04 20:04:53 +00:00
|
|
|
case PROP_LOCATION:{
|
|
|
|
gst_rtmp_src_uri_set_uri (GST_URI_HANDLER (src),
|
2011-11-13 23:55:56 +00:00
|
|
|
g_value_get_string (value), NULL);
|
2010-06-01 23:45:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-03-27 01:11:45 +00:00
|
|
|
case PROP_TIMEOUT:{
|
|
|
|
src->timeout = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
}
|
2010-06-01 23:45:06 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtmp_src_get_property (GObject * object, guint prop_id, GValue * value,
|
|
|
|
GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstRTMPSrc *src;
|
|
|
|
|
|
|
|
src = GST_RTMP_SRC (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2010-06-04 20:04:53 +00:00
|
|
|
case PROP_LOCATION:
|
2010-06-01 23:45:06 +00:00
|
|
|
g_value_set_string (value, src->uri);
|
|
|
|
break;
|
2016-03-27 01:11:45 +00:00
|
|
|
case PROP_TIMEOUT:
|
|
|
|
g_value_set_int (value, src->timeout);
|
|
|
|
break;
|
2010-06-01 23:45:06 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a new buffer from src->reqoffset, takes care of events
|
|
|
|
* and seeking and such.
|
|
|
|
*/
|
|
|
|
static GstFlowReturn
|
2010-06-04 20:04:53 +00:00
|
|
|
gst_rtmp_src_create (GstPushSrc * pushsrc, GstBuffer ** buffer)
|
2010-06-01 23:45:06 +00:00
|
|
|
{
|
|
|
|
GstRTMPSrc *src;
|
|
|
|
GstBuffer *buf;
|
2012-01-25 17:49:58 +00:00
|
|
|
GstMapInfo map;
|
|
|
|
guint8 *data;
|
2010-06-01 23:45:06 +00:00
|
|
|
guint todo;
|
2011-10-08 09:40:25 +00:00
|
|
|
gsize bsize;
|
2010-06-04 20:04:53 +00:00
|
|
|
int size;
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2010-06-04 20:04:53 +00:00
|
|
|
src = GST_RTMP_SRC (pushsrc);
|
2010-06-01 23:45:06 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (src->rtmp != NULL, GST_FLOW_ERROR);
|
|
|
|
|
2017-08-07 11:25:26 +00:00
|
|
|
if (!RTMP_IsConnected (src->rtmp)) {
|
|
|
|
GST_DEBUG_OBJECT (src, "reconnecting");
|
|
|
|
if (!gst_rtmp_src_connect (src))
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-06-04 20:04:53 +00:00
|
|
|
size = GST_BASE_SRC_CAST (pushsrc)->blocksize;
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2010-06-04 20:04:53 +00:00
|
|
|
GST_DEBUG ("reading from %" G_GUINT64_FORMAT
|
2010-06-06 11:57:06 +00:00
|
|
|
", size %u", src->cur_offset, size);
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2012-03-15 12:37:36 +00:00
|
|
|
buf = gst_buffer_new_allocate (NULL, size, NULL);
|
2010-06-04 20:04:53 +00:00
|
|
|
if (G_UNLIKELY (buf == NULL)) {
|
2010-06-01 23:45:06 +00:00
|
|
|
GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", size);
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-05-15 12:16:55 +00:00
|
|
|
todo = size;
|
2012-01-25 17:49:58 +00:00
|
|
|
gst_buffer_map (buf, &map, GST_MAP_WRITE);
|
|
|
|
data = map.data;
|
2016-05-15 12:16:55 +00:00
|
|
|
bsize = 0;
|
2010-06-01 23:45:06 +00:00
|
|
|
|
|
|
|
while (todo > 0) {
|
2016-05-15 12:16:55 +00:00
|
|
|
int read = RTMP_Read (src->rtmp, (char *) data, todo);
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2016-05-15 12:16:55 +00:00
|
|
|
if (G_UNLIKELY (read == 0 && todo == size))
|
2010-06-01 23:45:06 +00:00
|
|
|
goto eos;
|
2016-05-15 12:16:55 +00:00
|
|
|
|
|
|
|
if (G_UNLIKELY (read == 0))
|
2010-06-06 13:24:23 +00:00
|
|
|
break;
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2010-06-23 20:18:53 +00:00
|
|
|
if (G_UNLIKELY (read < 0))
|
2010-06-01 23:45:06 +00:00
|
|
|
goto read_failed;
|
|
|
|
|
|
|
|
if (read < todo) {
|
2011-10-08 09:40:25 +00:00
|
|
|
data += read;
|
2010-06-01 23:45:06 +00:00
|
|
|
todo -= read;
|
2011-10-08 09:40:25 +00:00
|
|
|
bsize += read;
|
2010-06-01 23:45:06 +00:00
|
|
|
} else {
|
2011-10-08 09:40:25 +00:00
|
|
|
bsize += todo;
|
2012-06-20 22:52:13 +00:00
|
|
|
todo = 0;
|
2010-06-01 23:45:06 +00:00
|
|
|
}
|
2010-07-30 21:59:10 +00:00
|
|
|
GST_LOG (" got size %d", read);
|
2010-06-01 23:45:06 +00:00
|
|
|
}
|
2012-01-25 17:49:58 +00:00
|
|
|
gst_buffer_unmap (buf, &map);
|
|
|
|
gst_buffer_resize (buf, 0, bsize);
|
2010-06-06 11:57:06 +00:00
|
|
|
|
2010-06-06 13:24:23 +00:00
|
|
|
if (src->discont) {
|
|
|
|
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
|
|
|
|
src->discont = FALSE;
|
|
|
|
}
|
|
|
|
|
2010-06-06 11:57:06 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buf) = src->last_timestamp;
|
|
|
|
GST_BUFFER_OFFSET (buf) = src->cur_offset;
|
2010-06-06 13:29:34 +00:00
|
|
|
|
2010-06-06 11:57:06 +00:00
|
|
|
src->cur_offset += size;
|
2010-06-09 18:49:10 +00:00
|
|
|
if (src->last_timestamp == GST_CLOCK_TIME_NONE)
|
|
|
|
src->last_timestamp = src->rtmp->m_mediaStamp * GST_MSECOND;
|
|
|
|
else
|
|
|
|
src->last_timestamp =
|
|
|
|
MAX (src->last_timestamp, src->rtmp->m_mediaStamp * GST_MSECOND);
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2010-06-06 13:24:23 +00:00
|
|
|
GST_LOG_OBJECT (src, "Created buffer of size %u at %" G_GINT64_FORMAT
|
|
|
|
" with timestamp %" GST_TIME_FORMAT, size, GST_BUFFER_OFFSET (buf),
|
2010-06-06 13:29:34 +00:00
|
|
|
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
|
2010-06-06 13:24:23 +00:00
|
|
|
|
|
|
|
|
2010-06-01 23:45:06 +00:00
|
|
|
/* we're done, return the buffer */
|
|
|
|
*buffer = buf;
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
read_failed:
|
|
|
|
{
|
|
|
|
gst_buffer_unref (buf);
|
2010-06-06 13:24:23 +00:00
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), ("Failed to read data"));
|
2010-06-01 23:45:06 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
eos:
|
|
|
|
{
|
|
|
|
gst_buffer_unref (buf);
|
2014-04-11 18:38:32 +00:00
|
|
|
if (src->cur_offset == 0) {
|
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
|
|
|
|
("Failed to read any data from stream, check your URL"));
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
} else {
|
|
|
|
GST_DEBUG_OBJECT (src, "Reading data gave EOS");
|
|
|
|
return GST_FLOW_EOS;
|
|
|
|
}
|
2010-06-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_rtmp_src_query (GstBaseSrc * basesrc, GstQuery * query)
|
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
GstRTMPSrc *src = GST_RTMP_SRC (basesrc);
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_URI:
|
|
|
|
gst_query_set_uri (query, src->uri);
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
2010-06-06 11:57:06 +00:00
|
|
|
case GST_QUERY_POSITION:{
|
|
|
|
GstFormat format;
|
|
|
|
|
|
|
|
gst_query_parse_position (query, &format, NULL);
|
|
|
|
if (format == GST_FORMAT_TIME) {
|
2014-06-05 06:29:50 +00:00
|
|
|
gst_query_set_position (query, format, src->last_timestamp);
|
2010-06-06 11:57:06 +00:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-06-04 20:04:53 +00:00
|
|
|
case GST_QUERY_DURATION:{
|
|
|
|
GstFormat format;
|
|
|
|
gdouble duration;
|
|
|
|
|
|
|
|
gst_query_parse_duration (query, &format, NULL);
|
|
|
|
if (format == GST_FORMAT_TIME && src->rtmp) {
|
|
|
|
duration = RTMP_GetDuration (src->rtmp);
|
|
|
|
if (duration != 0.0) {
|
|
|
|
gst_query_set_duration (query, format, duration * GST_SECOND);
|
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-06-27 10:24:49 +00:00
|
|
|
case GST_QUERY_SCHEDULING:{
|
|
|
|
gst_query_set_scheduling (query,
|
2015-09-28 10:29:29 +00:00
|
|
|
GST_SCHEDULING_FLAG_SEQUENTIAL |
|
|
|
|
GST_SCHEDULING_FLAG_BANDWIDTH_LIMITED, 1, -1, 0);
|
2014-06-27 10:24:49 +00:00
|
|
|
gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2010-06-01 23:45:06 +00:00
|
|
|
default:
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
ret = GST_BASE_SRC_CLASS (parent_class)->query (basesrc, query);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-06-04 20:04:53 +00:00
|
|
|
|
2010-06-01 23:45:06 +00:00
|
|
|
static gboolean
|
|
|
|
gst_rtmp_src_is_seekable (GstBaseSrc * basesrc)
|
|
|
|
{
|
|
|
|
GstRTMPSrc *src;
|
|
|
|
|
|
|
|
src = GST_RTMP_SRC (basesrc);
|
|
|
|
|
2010-06-06 13:24:23 +00:00
|
|
|
return src->seekable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_rtmp_src_prepare_seek_segment (GstBaseSrc * basesrc, GstEvent * event,
|
|
|
|
GstSegment * segment)
|
|
|
|
{
|
|
|
|
GstRTMPSrc *src;
|
|
|
|
GstSeekType cur_type, stop_type;
|
|
|
|
gint64 cur, stop;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstFormat format;
|
|
|
|
gdouble rate;
|
|
|
|
|
|
|
|
src = GST_RTMP_SRC (basesrc);
|
|
|
|
|
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags,
|
|
|
|
&cur_type, &cur, &stop_type, &stop);
|
|
|
|
|
|
|
|
if (!src->seekable) {
|
|
|
|
GST_LOG_OBJECT (src, "Not a seekable stream");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!src->rtmp) {
|
|
|
|
GST_LOG_OBJECT (src, "Not connected yet");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME) {
|
|
|
|
GST_LOG_OBJECT (src, "Seeking only supported in TIME format");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop_type != GST_SEEK_TYPE_NONE) {
|
|
|
|
GST_LOG_OBJECT (src, "Setting a stop position is not supported");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_segment_init (segment, GST_FORMAT_TIME);
|
2011-10-08 09:40:25 +00:00
|
|
|
gst_segment_do_seek (segment, rate, format, flags, cur_type, cur, stop_type,
|
2010-06-06 13:24:23 +00:00
|
|
|
stop, NULL);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_rtmp_src_do_seek (GstBaseSrc * basesrc, GstSegment * segment)
|
|
|
|
{
|
|
|
|
GstRTMPSrc *src;
|
|
|
|
|
|
|
|
src = GST_RTMP_SRC (basesrc);
|
|
|
|
|
|
|
|
if (segment->format != GST_FORMAT_TIME) {
|
|
|
|
GST_LOG_OBJECT (src, "Only time based seeks are supported");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!src->rtmp) {
|
|
|
|
GST_LOG_OBJECT (src, "Not connected yet");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initial seek */
|
|
|
|
if (src->cur_offset == 0 && segment->start == 0)
|
2017-08-07 11:25:26 +00:00
|
|
|
goto success;
|
2010-06-06 13:24:23 +00:00
|
|
|
|
2012-12-01 16:05:38 +00:00
|
|
|
if (!src->seekable) {
|
|
|
|
GST_LOG_OBJECT (src, "Not a seekable stream");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-08-07 11:25:26 +00:00
|
|
|
/* If we have just disconnected in unlock(), we need to re-connect
|
|
|
|
* and also let librtmp read some data before sending a seek,
|
|
|
|
* otherwise it will stall. Calling create() does both. */
|
|
|
|
if (!RTMP_IsConnected (src->rtmp)) {
|
|
|
|
GstBuffer *buffer = NULL;
|
|
|
|
gst_rtmp_src_create (GST_PUSH_SRC (basesrc), &buffer);
|
|
|
|
gst_buffer_replace (&buffer, NULL);
|
|
|
|
}
|
|
|
|
|
2010-06-09 18:49:10 +00:00
|
|
|
src->last_timestamp = GST_CLOCK_TIME_NONE;
|
2010-06-06 13:24:23 +00:00
|
|
|
if (!RTMP_SendSeek (src->rtmp, segment->start / GST_MSECOND)) {
|
|
|
|
GST_ERROR_OBJECT (src, "Seeking failed");
|
|
|
|
src->seekable = FALSE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-08-07 11:25:26 +00:00
|
|
|
success:
|
|
|
|
/* This is set here so that the call to create() above doesn't clear it */
|
|
|
|
src->discont = TRUE;
|
|
|
|
|
2010-06-06 13:24:23 +00:00
|
|
|
GST_DEBUG_OBJECT (src, "Seek to %" GST_TIME_FORMAT " successfull",
|
|
|
|
GST_TIME_ARGS (segment->start));
|
|
|
|
|
|
|
|
return TRUE;
|
2010-06-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 11:25:26 +00:00
|
|
|
static gboolean
|
|
|
|
gst_rtmp_src_connect (GstRTMPSrc * src)
|
|
|
|
{
|
|
|
|
RTMP_Init (src->rtmp);
|
|
|
|
src->rtmp->Link.timeout = src->timeout;
|
|
|
|
if (!RTMP_SetupURL (src->rtmp, src->uri)) {
|
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
|
|
|
|
("Failed to setup URL '%s'", src->uri));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
src->seekable = !(src->rtmp->Link.lFlags & RTMP_LF_LIVE);
|
|
|
|
GST_INFO_OBJECT (src, "seekable %d", src->seekable);
|
|
|
|
|
|
|
|
/* open if required */
|
|
|
|
if (!RTMP_IsConnected (src->rtmp)) {
|
|
|
|
if (!RTMP_Connect (src->rtmp, NULL)) {
|
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
|
|
|
|
("Could not connect to RTMP stream \"%s\" for reading", src->uri));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-04 20:04:53 +00:00
|
|
|
/* open the file, do stuff necessary to go to PAUSED state */
|
2010-06-01 23:45:06 +00:00
|
|
|
static gboolean
|
2010-06-04 20:04:53 +00:00
|
|
|
gst_rtmp_src_start (GstBaseSrc * basesrc)
|
2010-06-01 23:45:06 +00:00
|
|
|
{
|
|
|
|
GstRTMPSrc *src;
|
|
|
|
|
|
|
|
src = GST_RTMP_SRC (basesrc);
|
|
|
|
|
2010-06-04 20:04:53 +00:00
|
|
|
if (!src->uri) {
|
|
|
|
GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), ("No filename given"));
|
2010-06-01 23:45:06 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-06-06 11:57:06 +00:00
|
|
|
src->cur_offset = 0;
|
2010-06-06 13:24:23 +00:00
|
|
|
src->last_timestamp = 0;
|
|
|
|
src->discont = TRUE;
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2010-06-06 06:30:09 +00:00
|
|
|
src->rtmp = RTMP_Alloc ();
|
2015-12-29 22:35:14 +00:00
|
|
|
if (!src->rtmp) {
|
|
|
|
GST_ERROR_OBJECT (src, "Could not allocate librtmp's RTMP context");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-08-07 11:25:26 +00:00
|
|
|
if (!gst_rtmp_src_connect (src))
|
2015-12-29 22:35:14 +00:00
|
|
|
goto error;
|
2010-06-01 23:45:06 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2015-12-29 22:35:14 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (src->rtmp) {
|
|
|
|
RTMP_Free (src->rtmp);
|
|
|
|
src->rtmp = NULL;
|
|
|
|
}
|
|
|
|
return FALSE;
|
2010-06-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2013-04-02 02:49:00 +00:00
|
|
|
static gboolean
|
|
|
|
gst_rtmp_src_unlock (GstBaseSrc * basesrc)
|
|
|
|
{
|
|
|
|
GstRTMPSrc *rtmpsrc = GST_RTMP_SRC (basesrc);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (rtmpsrc, "unlock");
|
|
|
|
|
|
|
|
/* This closes the socket, which means that any pending socket calls
|
|
|
|
* error out. */
|
|
|
|
if (rtmpsrc->rtmp) {
|
|
|
|
RTMP_Close (rtmpsrc->rtmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-01 23:45:06 +00:00
|
|
|
static gboolean
|
2010-06-04 20:04:53 +00:00
|
|
|
gst_rtmp_src_stop (GstBaseSrc * basesrc)
|
2010-06-01 23:45:06 +00:00
|
|
|
{
|
|
|
|
GstRTMPSrc *src;
|
|
|
|
|
|
|
|
src = GST_RTMP_SRC (basesrc);
|
|
|
|
|
2010-06-06 06:30:09 +00:00
|
|
|
if (src->rtmp) {
|
|
|
|
RTMP_Free (src->rtmp);
|
|
|
|
src->rtmp = NULL;
|
|
|
|
}
|
2010-06-01 23:45:06 +00:00
|
|
|
|
2010-06-06 11:57:06 +00:00
|
|
|
src->cur_offset = 0;
|
2010-06-06 13:24:23 +00:00
|
|
|
src->last_timestamp = 0;
|
|
|
|
src->discont = TRUE;
|
2010-06-01 23:45:06 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|