2010-12-12 04:10:01 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-12-12 04:10:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gst/rtp/gstrtpbuffer.h>
|
|
|
|
|
|
|
|
#include "gstrtpgstpay.h"
|
|
|
|
|
2012-10-31 16:17:48 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_rtp_pay_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_rtp_pay_debug
|
|
|
|
|
2010-12-12 04:10:01 +00:00
|
|
|
/*
|
|
|
|
* 0 1 2 3
|
|
|
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2012-11-01 17:40:31 +00:00
|
|
|
* |C| CV |D|0|0|0| ETYPE | MBZ |
|
2010-12-12 04:10:01 +00:00
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Frag_offset |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*
|
2012-11-01 17:40:31 +00:00
|
|
|
* C: caps inlined flag
|
2010-12-12 04:10:01 +00:00
|
|
|
* When C set, first part of payload contains caps definition. Caps definition
|
2010-12-22 10:41:59 +00:00
|
|
|
* starts with variable-length length prefix and then a string of that length.
|
2010-12-12 04:10:01 +00:00
|
|
|
* the length is encoded in big endian 7 bit chunks, the top 1 bit of a byte
|
|
|
|
* is the continuation marker and the 7 next bits the data. A continuation
|
2012-11-01 17:40:31 +00:00
|
|
|
* marker of 1 means that the next byte contains more data.
|
2010-12-12 04:10:01 +00:00
|
|
|
*
|
|
|
|
* CV: caps version, 0 = caps from SDP, 1 - 7 inlined caps
|
|
|
|
* D: delta unit buffer
|
2012-11-01 17:40:31 +00:00
|
|
|
* ETYPE: type of event. Payload contains the event, prefixed with a
|
|
|
|
* variable length field.
|
|
|
|
* 0 = NO event
|
|
|
|
* 1 = GST_EVENT_TAG
|
|
|
|
* 2 = GST_EVENT_CUSTOM_DOWNSTREAM
|
|
|
|
* 3 = GST_EVENT_CUSTOM_BOTH
|
2013-07-25 21:56:38 +00:00
|
|
|
* 4 = GST_EVENT_STREAM_START
|
2010-12-12 04:10:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_rtp_gst_pay_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_rtp_gst_pay_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("application/x-rtp, "
|
|
|
|
"media = (string) \"application\", "
|
|
|
|
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
|
|
|
|
"clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
|
|
|
|
);
|
|
|
|
|
2013-07-26 01:12:05 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2015-04-24 16:01:10 +00:00
|
|
|
PROP_CONFIG_INTERVAL
|
2013-07-26 01:12:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DEFAULT_CONFIG_INTERVAL 0
|
|
|
|
|
|
|
|
static void gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
2012-11-01 10:52:25 +00:00
|
|
|
static void gst_rtp_gst_pay_finalize (GObject * obj);
|
2013-08-21 08:03:52 +00:00
|
|
|
static GstStateChangeReturn gst_rtp_gst_pay_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
2012-11-01 10:52:25 +00:00
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
static gboolean gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload,
|
2010-12-12 04:10:01 +00:00
|
|
|
GstCaps * caps);
|
2011-11-11 11:25:01 +00:00
|
|
|
static GstFlowReturn gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * payload,
|
2010-12-12 04:10:01 +00:00
|
|
|
GstBuffer * buffer);
|
2012-11-01 17:40:31 +00:00
|
|
|
static gboolean gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload,
|
|
|
|
GstEvent * event);
|
2010-12-12 04:10:01 +00:00
|
|
|
|
2011-04-25 11:16:58 +00:00
|
|
|
#define gst_rtp_gst_pay_parent_class parent_class
|
2011-11-11 11:25:01 +00:00
|
|
|
G_DEFINE_TYPE (GstRtpGSTPay, gst_rtp_gst_pay, GST_TYPE_RTP_BASE_PAYLOAD);
|
2010-12-12 04:10:01 +00:00
|
|
|
|
2011-04-25 11:16:58 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_gst_pay_class_init (GstRtpGSTPayClass * klass)
|
2010-12-12 04:10:01 +00:00
|
|
|
{
|
2012-11-01 10:52:25 +00:00
|
|
|
GObjectClass *gobject_class;
|
2011-04-25 11:16:58 +00:00
|
|
|
GstElementClass *gstelement_class;
|
2011-11-11 11:25:01 +00:00
|
|
|
GstRTPBasePayloadClass *gstrtpbasepayload_class;
|
2010-12-12 04:10:01 +00:00
|
|
|
|
2012-11-01 10:52:25 +00:00
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-04-25 11:16:58 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2011-11-11 11:25:01 +00:00
|
|
|
gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
|
2011-04-25 11:16:58 +00:00
|
|
|
|
2013-07-26 01:12:05 +00:00
|
|
|
gobject_class->set_property = gst_rtp_gst_pay_set_property;
|
|
|
|
gobject_class->get_property = gst_rtp_gst_pay_get_property;
|
2012-11-01 10:52:25 +00:00
|
|
|
gobject_class->finalize = gst_rtp_gst_pay_finalize;
|
|
|
|
|
2013-07-26 01:12:05 +00:00
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass),
|
|
|
|
PROP_CONFIG_INTERVAL,
|
|
|
|
g_param_spec_uint ("config-interval",
|
|
|
|
"Caps/Tags Send Interval",
|
|
|
|
"Interval for sending caps and TAG events in seconds (0 = disabled)",
|
|
|
|
0, 3600, DEFAULT_CONFIG_INTERVAL,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
|
|
|
|
);
|
|
|
|
|
2013-08-21 08:03:52 +00:00
|
|
|
gstelement_class->change_state = gst_rtp_gst_pay_change_state;
|
|
|
|
|
2011-04-25 11:16:58 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2010-12-12 04:10:01 +00:00
|
|
|
gst_static_pad_template_get (&gst_rtp_gst_pay_src_template));
|
2011-04-25 11:16:58 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2010-12-12 04:10:01 +00:00
|
|
|
gst_static_pad_template_get (&gst_rtp_gst_pay_sink_template));
|
|
|
|
|
2012-04-09 23:51:41 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2010-12-22 10:41:59 +00:00
|
|
|
"RTP GStreamer payloader", "Codec/Payloader/Network/RTP",
|
2010-12-12 04:10:01 +00:00
|
|
|
"Payload GStreamer buffers as RTP packets",
|
|
|
|
"Wim Taymans <wim.taymans@gmail.com>");
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
gstrtpbasepayload_class->set_caps = gst_rtp_gst_pay_setcaps;
|
|
|
|
gstrtpbasepayload_class->handle_buffer = gst_rtp_gst_pay_handle_buffer;
|
2012-11-01 17:40:31 +00:00
|
|
|
gstrtpbasepayload_class->sink_event = gst_rtp_gst_pay_sink_event;
|
2012-10-31 16:17:48 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_rtp_pay_debug, "rtpgstpay", 0,
|
|
|
|
"rtpgstpay element");
|
2010-12-12 04:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-25 11:16:58 +00:00
|
|
|
gst_rtp_gst_pay_init (GstRtpGSTPay * rtpgstpay)
|
2010-12-12 04:10:01 +00:00
|
|
|
{
|
2012-11-01 15:54:58 +00:00
|
|
|
rtpgstpay->adapter = gst_adapter_new ();
|
2013-07-26 00:54:50 +00:00
|
|
|
rtpgstpay->pending_buffers = NULL;
|
2012-11-01 17:40:31 +00:00
|
|
|
gst_rtp_base_payload_set_options (GST_RTP_BASE_PAYLOAD (rtpgstpay),
|
|
|
|
"application", TRUE, "X-GST", 90000);
|
2013-07-26 01:12:05 +00:00
|
|
|
rtpgstpay->last_config = GST_CLOCK_TIME_NONE;
|
|
|
|
rtpgstpay->taglist = NULL;
|
|
|
|
rtpgstpay->config_interval = DEFAULT_CONFIG_INTERVAL;
|
2010-12-12 04:10:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 07:55:20 +00:00
|
|
|
static void
|
2013-08-21 08:03:52 +00:00
|
|
|
gst_rtp_gst_pay_reset (GstRtpGSTPay * rtpgstpay, gboolean full)
|
2013-08-21 07:55:20 +00:00
|
|
|
{
|
|
|
|
rtpgstpay->last_config = GST_CLOCK_TIME_NONE;
|
|
|
|
gst_adapter_clear (rtpgstpay->adapter);
|
|
|
|
rtpgstpay->flags &= 0x70;
|
|
|
|
rtpgstpay->etype = 0;
|
|
|
|
if (rtpgstpay->pending_buffers)
|
|
|
|
g_list_free_full (rtpgstpay->pending_buffers,
|
|
|
|
(GDestroyNotify) gst_buffer_list_unref);
|
|
|
|
rtpgstpay->pending_buffers = NULL;
|
2013-08-21 08:03:52 +00:00
|
|
|
if (full) {
|
|
|
|
if (rtpgstpay->taglist)
|
|
|
|
gst_tag_list_unref (rtpgstpay->taglist);
|
|
|
|
rtpgstpay->taglist = NULL;
|
|
|
|
if (rtpgstpay->stream_id)
|
|
|
|
g_free (rtpgstpay->stream_id);
|
|
|
|
rtpgstpay->stream_id = NULL;
|
|
|
|
rtpgstpay->current_CV = 0;
|
|
|
|
rtpgstpay->next_CV = 0;
|
|
|
|
}
|
2013-08-21 07:55:20 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 10:52:25 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_gst_pay_finalize (GObject * obj)
|
|
|
|
{
|
|
|
|
GstRtpGSTPay *rtpgstpay;
|
|
|
|
|
|
|
|
rtpgstpay = GST_RTP_GST_PAY (obj);
|
|
|
|
|
2013-08-21 08:03:52 +00:00
|
|
|
gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
|
2013-08-21 07:55:20 +00:00
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
g_object_unref (rtpgstpay->adapter);
|
2012-11-01 10:52:25 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (obj);
|
|
|
|
}
|
|
|
|
|
2013-07-26 01:12:05 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_gst_pay_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstRtpGSTPay *rtpgstpay;
|
|
|
|
|
|
|
|
rtpgstpay = GST_RTP_GST_PAY (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_CONFIG_INTERVAL:
|
|
|
|
rtpgstpay->config_interval = g_value_get_uint (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtp_gst_pay_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstRtpGSTPay *rtpgstpay;
|
|
|
|
|
|
|
|
rtpgstpay = GST_RTP_GST_PAY (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_CONFIG_INTERVAL:
|
|
|
|
g_value_set_uint (value, rtpgstpay->config_interval);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 08:03:52 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_rtp_gst_pay_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstRtpGSTPay *rtpgstpay;
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
rtpgstpay = GST_RTP_GST_PAY (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
gst_rtp_gst_pay_reset (rtpgstpay, TRUE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-18 07:10:03 +00:00
|
|
|
#define RTP_HEADER_LEN 12
|
2013-08-21 08:03:52 +00:00
|
|
|
|
2013-07-26 00:54:50 +00:00
|
|
|
static gboolean
|
|
|
|
gst_rtp_gst_pay_create_from_adapter (GstRtpGSTPay * rtpgstpay,
|
|
|
|
GstClockTime timestamp)
|
2010-12-12 04:10:01 +00:00
|
|
|
{
|
2014-06-18 07:10:03 +00:00
|
|
|
guint avail, mtu;
|
2012-11-01 15:54:58 +00:00
|
|
|
guint frag_offset;
|
2013-04-09 14:52:21 +00:00
|
|
|
GstBufferList *list;
|
2012-11-01 10:52:25 +00:00
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
avail = gst_adapter_available (rtpgstpay->adapter);
|
2013-04-09 14:52:21 +00:00
|
|
|
if (avail == 0)
|
2013-07-26 00:54:50 +00:00
|
|
|
return FALSE;
|
2013-04-09 14:52:21 +00:00
|
|
|
|
2014-06-18 07:10:03 +00:00
|
|
|
mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpgstpay);
|
|
|
|
|
|
|
|
list = gst_buffer_list_new_sized ((avail / (mtu - (RTP_HEADER_LEN + 8))) + 1);
|
2013-07-26 00:54:50 +00:00
|
|
|
frag_offset = 0;
|
2010-12-12 04:10:01 +00:00
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
while (avail) {
|
2010-12-12 04:10:01 +00:00
|
|
|
guint towrite;
|
|
|
|
guint8 *payload;
|
|
|
|
guint payload_len;
|
|
|
|
guint packet_len;
|
2012-11-01 15:54:58 +00:00
|
|
|
GstBuffer *outbuf;
|
2011-04-18 08:54:43 +00:00
|
|
|
GstRTPBuffer rtp = { NULL };
|
2013-04-09 14:52:21 +00:00
|
|
|
GstBuffer *paybuf;
|
2010-12-12 04:10:01 +00:00
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
|
2010-12-12 04:10:01 +00:00
|
|
|
/* this will be the total lenght of the packet */
|
2012-11-01 15:54:58 +00:00
|
|
|
packet_len = gst_rtp_buffer_calc_packet_len (8 + avail, 0, 0);
|
2010-12-12 04:10:01 +00:00
|
|
|
|
|
|
|
/* fill one MTU or all available bytes */
|
2014-06-18 07:10:03 +00:00
|
|
|
towrite = MIN (packet_len, mtu);
|
2010-12-12 04:10:01 +00:00
|
|
|
|
|
|
|
/* this is the payload length */
|
|
|
|
payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
|
|
|
|
|
2013-04-09 14:52:21 +00:00
|
|
|
/* create buffer to hold the header */
|
|
|
|
outbuf = gst_rtp_buffer_new_allocate (8, 0, 0);
|
2011-04-18 08:54:43 +00:00
|
|
|
|
|
|
|
gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
|
|
|
|
payload = gst_rtp_buffer_get_payload (&rtp);
|
2010-12-12 04:10:01 +00:00
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
GST_DEBUG_OBJECT (rtpgstpay, "new packet len %u, frag %u", packet_len,
|
2012-11-01 10:52:25 +00:00
|
|
|
frag_offset);
|
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
/*
|
|
|
|
* 0 1 2 3
|
|
|
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2012-11-01 17:40:31 +00:00
|
|
|
* |C| CV |D|0|0|0| ETYPE | MBZ |
|
2012-11-01 15:54:58 +00:00
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Frag_offset |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
payload[0] = rtpgstpay->flags;
|
2012-11-01 17:40:31 +00:00
|
|
|
payload[1] = rtpgstpay->etype;
|
|
|
|
payload[2] = payload[3] = 0;
|
2010-12-12 04:10:01 +00:00
|
|
|
payload[4] = frag_offset >> 24;
|
|
|
|
payload[5] = frag_offset >> 16;
|
|
|
|
payload[6] = frag_offset >> 8;
|
|
|
|
payload[7] = frag_offset & 0xff;
|
|
|
|
|
|
|
|
payload += 8;
|
|
|
|
payload_len -= 8;
|
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
frag_offset += payload_len;
|
|
|
|
avail -= payload_len;
|
|
|
|
|
|
|
|
if (avail == 0)
|
2011-04-18 08:54:43 +00:00
|
|
|
gst_rtp_buffer_set_marker (&rtp, TRUE);
|
|
|
|
|
|
|
|
gst_rtp_buffer_unmap (&rtp);
|
2010-12-12 04:10:01 +00:00
|
|
|
|
2013-04-09 14:52:21 +00:00
|
|
|
/* create a new buf to hold the payload */
|
|
|
|
GST_DEBUG_OBJECT (rtpgstpay, "take %u bytes from adapter", payload_len);
|
2013-07-16 19:37:49 +00:00
|
|
|
paybuf = gst_adapter_take_buffer_fast (rtpgstpay->adapter, payload_len);
|
2013-04-09 14:52:21 +00:00
|
|
|
|
|
|
|
/* create a new group to hold the rtp header and the payload */
|
|
|
|
gst_buffer_append (outbuf, paybuf);
|
|
|
|
|
2015-06-10 12:33:50 +00:00
|
|
|
GST_BUFFER_PTS (outbuf) = timestamp;
|
2010-12-12 04:10:01 +00:00
|
|
|
|
2013-04-09 14:52:21 +00:00
|
|
|
/* and add to list */
|
|
|
|
gst_buffer_list_insert (list, -1, outbuf);
|
2012-11-01 15:54:58 +00:00
|
|
|
}
|
2013-04-09 14:52:21 +00:00
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
rtpgstpay->flags &= 0x70;
|
2012-11-01 17:40:31 +00:00
|
|
|
rtpgstpay->etype = 0;
|
2013-07-26 00:54:50 +00:00
|
|
|
rtpgstpay->pending_buffers = g_list_append (rtpgstpay->pending_buffers, list);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_rtp_gst_pay_flush (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2013-09-30 10:24:32 +00:00
|
|
|
GList *iter;
|
2013-07-26 00:54:50 +00:00
|
|
|
|
|
|
|
gst_rtp_gst_pay_create_from_adapter (rtpgstpay, timestamp);
|
2013-09-30 10:24:32 +00:00
|
|
|
|
|
|
|
iter = rtpgstpay->pending_buffers;
|
|
|
|
while (iter) {
|
2013-07-26 00:54:50 +00:00
|
|
|
GstBufferList *list = iter->data;
|
|
|
|
|
2013-09-30 10:24:32 +00:00
|
|
|
rtpgstpay->pending_buffers = iter =
|
|
|
|
g_list_delete_link (rtpgstpay->pending_buffers, iter);
|
2013-07-26 00:54:50 +00:00
|
|
|
|
|
|
|
/* push the whole buffer list at once */
|
|
|
|
ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpgstpay),
|
|
|
|
list);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
break;
|
|
|
|
}
|
2012-11-01 15:54:58 +00:00
|
|
|
|
2013-04-09 14:52:21 +00:00
|
|
|
return ret;
|
2012-11-01 15:54:58 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 17:40:31 +00:00
|
|
|
static GstBuffer *
|
|
|
|
make_data_buffer (GstRtpGSTPay * rtpgstpay, gchar * data, guint size)
|
|
|
|
{
|
|
|
|
guint plen;
|
|
|
|
guint8 *ptr;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
GstMapInfo map;
|
|
|
|
|
|
|
|
/* calculate length */
|
|
|
|
plen = 1;
|
|
|
|
while (size >> (7 * plen))
|
|
|
|
plen++;
|
|
|
|
|
|
|
|
outbuf = gst_buffer_new_allocate (NULL, plen + size, NULL);
|
|
|
|
|
|
|
|
gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
|
|
|
|
ptr = map.data;
|
|
|
|
|
|
|
|
/* write length */
|
|
|
|
while (plen) {
|
|
|
|
plen--;
|
|
|
|
*ptr++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
|
|
|
|
}
|
|
|
|
/* copy data */
|
|
|
|
memcpy (ptr, data, size);
|
|
|
|
gst_buffer_unmap (outbuf, &map);
|
|
|
|
|
|
|
|
return outbuf;
|
|
|
|
}
|
|
|
|
|
2013-07-26 01:10:10 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_gst_pay_send_caps (GstRtpGSTPay * rtpgstpay, guint8 cv, GstCaps * caps)
|
|
|
|
{
|
|
|
|
gchar *capsstr;
|
|
|
|
guint capslen;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
|
2013-11-04 14:46:22 +00:00
|
|
|
if (rtpgstpay->flags & (1 << 7))
|
|
|
|
return;
|
|
|
|
|
2013-07-26 01:10:10 +00:00
|
|
|
capsstr = gst_caps_to_string (caps);
|
|
|
|
capslen = strlen (capsstr);
|
|
|
|
/* for 0 byte */
|
|
|
|
capslen++;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (rtpgstpay, "sending caps=%s", capsstr);
|
|
|
|
|
|
|
|
/* make a data buffer of it */
|
|
|
|
outbuf = make_data_buffer (rtpgstpay, capsstr, capslen);
|
|
|
|
g_free (capsstr);
|
|
|
|
|
|
|
|
/* store in adapter, we don't flush yet, buffer might follow */
|
|
|
|
rtpgstpay->flags = (1 << 7) | (cv << 4);
|
|
|
|
gst_adapter_push (rtpgstpay->adapter, outbuf);
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
static gboolean
|
|
|
|
gst_rtp_gst_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstRtpGSTPay *rtpgstpay;
|
|
|
|
gboolean res;
|
|
|
|
gchar *capsstr, *capsenc, *capsver;
|
2012-11-01 17:40:31 +00:00
|
|
|
guint capslen;
|
2012-11-01 15:54:58 +00:00
|
|
|
|
|
|
|
rtpgstpay = GST_RTP_GST_PAY (payload);
|
|
|
|
|
|
|
|
capsstr = gst_caps_to_string (caps);
|
|
|
|
capslen = strlen (capsstr);
|
|
|
|
|
|
|
|
/* encode without 0 byte */
|
|
|
|
capsenc = g_base64_encode ((guchar *) capsstr, capslen);
|
|
|
|
GST_DEBUG_OBJECT (payload, "caps=%s, caps(base64)=%s", capsstr, capsenc);
|
|
|
|
g_free (capsstr);
|
|
|
|
|
2013-07-26 01:10:10 +00:00
|
|
|
/* Send the new caps */
|
|
|
|
rtpgstpay->current_CV = rtpgstpay->next_CV;
|
2012-11-01 15:54:58 +00:00
|
|
|
rtpgstpay->next_CV = (rtpgstpay->next_CV + 1) & 0x7;
|
2013-07-26 01:10:10 +00:00
|
|
|
gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
|
2012-11-01 15:54:58 +00:00
|
|
|
|
|
|
|
/* make caps for SDP */
|
|
|
|
capsver = g_strdup_printf ("%d", rtpgstpay->current_CV);
|
|
|
|
res =
|
|
|
|
gst_rtp_base_payload_set_outcaps (payload, "caps", G_TYPE_STRING, capsenc,
|
|
|
|
"capsversion", G_TYPE_STRING, capsver, NULL);
|
|
|
|
g_free (capsenc);
|
|
|
|
g_free (capsver);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-07-26 01:03:34 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_gst_pay_send_event (GstRtpGSTPay * rtpgstpay, guint etype,
|
|
|
|
GstEvent * event)
|
|
|
|
{
|
|
|
|
const GstStructure *s;
|
|
|
|
gchar *estr;
|
|
|
|
guint elen;
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
|
|
|
|
/* Create the standalone caps packet if an inlined caps was pending */
|
|
|
|
gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
|
|
|
|
|
|
|
|
s = gst_event_get_structure (event);
|
|
|
|
|
|
|
|
estr = gst_structure_to_string (s);
|
|
|
|
elen = strlen (estr);
|
2014-11-20 12:14:14 +00:00
|
|
|
/* for 0 byte */
|
|
|
|
elen++;
|
2013-07-26 01:03:34 +00:00
|
|
|
outbuf = make_data_buffer (rtpgstpay, estr, elen);
|
|
|
|
GST_DEBUG_OBJECT (rtpgstpay, "sending event=%s", estr);
|
|
|
|
g_free (estr);
|
|
|
|
|
|
|
|
rtpgstpay->etype = etype;
|
|
|
|
gst_adapter_push (rtpgstpay->adapter, outbuf);
|
|
|
|
/* Create the event packet now to avoid conflict with data/caps packets */
|
|
|
|
gst_rtp_gst_pay_create_from_adapter (rtpgstpay, GST_CLOCK_TIME_NONE);
|
|
|
|
}
|
|
|
|
|
2012-11-01 17:40:31 +00:00
|
|
|
static gboolean
|
|
|
|
gst_rtp_gst_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
|
|
|
|
{
|
2012-11-01 18:42:39 +00:00
|
|
|
gboolean ret;
|
2012-11-01 17:40:31 +00:00
|
|
|
GstRtpGSTPay *rtpgstpay;
|
2013-08-21 07:55:20 +00:00
|
|
|
guint etype = 0;
|
2012-11-01 17:40:31 +00:00
|
|
|
|
|
|
|
rtpgstpay = GST_RTP_GST_PAY (payload);
|
|
|
|
|
2013-05-13 08:43:32 +00:00
|
|
|
ret =
|
|
|
|
GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload,
|
|
|
|
gst_event_ref (event));
|
|
|
|
|
2012-11-01 17:40:31 +00:00
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2013-08-21 07:55:20 +00:00
|
|
|
case GST_EVENT_FLUSH_STOP:
|
2013-08-21 08:03:52 +00:00
|
|
|
gst_rtp_gst_pay_reset (rtpgstpay, FALSE);
|
2013-08-21 07:55:20 +00:00
|
|
|
break;
|
2013-07-26 01:12:05 +00:00
|
|
|
case GST_EVENT_TAG:{
|
|
|
|
GstTagList *tags;
|
|
|
|
|
|
|
|
gst_event_parse_tag (event, &tags);
|
|
|
|
|
|
|
|
if (gst_tag_list_get_scope (tags) == GST_TAG_SCOPE_STREAM) {
|
|
|
|
GstTagList *old;
|
|
|
|
|
2013-08-21 08:52:59 +00:00
|
|
|
GST_DEBUG_OBJECT (rtpgstpay, "storing stream tags %" GST_PTR_FORMAT,
|
|
|
|
tags);
|
|
|
|
if ((old = rtpgstpay->taglist))
|
2013-07-26 01:12:05 +00:00
|
|
|
gst_tag_list_unref (old);
|
2013-08-21 08:52:59 +00:00
|
|
|
rtpgstpay->taglist = gst_tag_list_ref (tags);
|
2013-07-26 01:12:05 +00:00
|
|
|
}
|
2012-11-01 18:42:39 +00:00
|
|
|
etype = 1;
|
2012-11-01 17:40:31 +00:00
|
|
|
break;
|
2013-07-26 01:12:05 +00:00
|
|
|
}
|
2012-11-01 17:40:31 +00:00
|
|
|
case GST_EVENT_CUSTOM_DOWNSTREAM:
|
2012-11-01 18:42:39 +00:00
|
|
|
etype = 2;
|
2012-11-01 17:40:31 +00:00
|
|
|
break;
|
|
|
|
case GST_EVENT_CUSTOM_BOTH:
|
2013-07-25 21:52:16 +00:00
|
|
|
etype = 3;
|
2012-11-01 17:40:31 +00:00
|
|
|
break;
|
2013-08-07 13:47:35 +00:00
|
|
|
case GST_EVENT_STREAM_START:{
|
|
|
|
const gchar *stream_id = NULL;
|
|
|
|
|
2013-07-26 01:12:05 +00:00
|
|
|
if (rtpgstpay->taglist)
|
|
|
|
gst_tag_list_unref (rtpgstpay->taglist);
|
|
|
|
rtpgstpay->taglist = NULL;
|
2013-08-07 13:47:35 +00:00
|
|
|
|
|
|
|
gst_event_parse_stream_start (event, &stream_id);
|
|
|
|
if (stream_id) {
|
|
|
|
if (rtpgstpay->stream_id)
|
|
|
|
g_free (rtpgstpay->stream_id);
|
|
|
|
rtpgstpay->stream_id = g_strdup (stream_id);
|
|
|
|
}
|
|
|
|
etype = 4;
|
2013-07-25 21:56:38 +00:00
|
|
|
break;
|
2013-08-07 13:47:35 +00:00
|
|
|
}
|
2012-11-01 17:40:31 +00:00
|
|
|
default:
|
2012-11-01 18:42:39 +00:00
|
|
|
GST_LOG_OBJECT (rtpgstpay, "no event for %s",
|
|
|
|
GST_EVENT_TYPE_NAME (event));
|
2012-11-01 17:40:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-11-01 18:42:39 +00:00
|
|
|
if (etype) {
|
|
|
|
GST_DEBUG_OBJECT (rtpgstpay, "make event type %d for %s",
|
|
|
|
etype, GST_EVENT_TYPE_NAME (event));
|
2013-07-26 01:03:34 +00:00
|
|
|
gst_rtp_gst_pay_send_event (rtpgstpay, etype, event);
|
|
|
|
/* Do not send stream-start right away since caps/new-segment were not yet
|
|
|
|
sent, so our data would be considered invalid */
|
|
|
|
if (etype != 4) {
|
|
|
|
/* flush the adapter immediately */
|
|
|
|
gst_rtp_gst_pay_flush (rtpgstpay, GST_CLOCK_TIME_NONE);
|
|
|
|
}
|
2012-11-01 17:40:31 +00:00
|
|
|
}
|
|
|
|
|
2013-05-13 08:43:32 +00:00
|
|
|
gst_event_unref (event);
|
2013-05-03 21:43:26 +00:00
|
|
|
|
2012-11-01 18:42:39 +00:00
|
|
|
return ret;
|
2012-11-01 17:40:31 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 01:12:05 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_gst_pay_send_config (GstRtpGSTPay * rtpgstpay, GstClockTime timestamp)
|
|
|
|
{
|
|
|
|
GstPad *pad = GST_RTP_BASE_PAYLOAD_SINKPAD (rtpgstpay);
|
|
|
|
GstCaps *caps = NULL;
|
|
|
|
GstEvent *tag = NULL;
|
2013-08-07 13:47:35 +00:00
|
|
|
GstEvent *stream_start = NULL;
|
2013-07-26 01:12:05 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (rtpgstpay, "time to send config");
|
|
|
|
/* Send tags */
|
|
|
|
if (rtpgstpay->taglist && !gst_tag_list_is_empty (rtpgstpay->taglist))
|
|
|
|
tag = gst_event_new_tag (gst_tag_list_ref (rtpgstpay->taglist));
|
|
|
|
if (tag) {
|
2013-08-07 13:47:35 +00:00
|
|
|
/* Send start-stream to clear tags */
|
|
|
|
if (rtpgstpay->stream_id)
|
|
|
|
stream_start = gst_event_new_stream_start (rtpgstpay->stream_id);
|
|
|
|
if (stream_start) {
|
|
|
|
gst_rtp_gst_pay_send_event (rtpgstpay, 4, stream_start);
|
|
|
|
gst_event_unref (stream_start);
|
|
|
|
}
|
2013-07-26 01:12:05 +00:00
|
|
|
gst_rtp_gst_pay_send_event (rtpgstpay, 1, tag);
|
|
|
|
gst_event_unref (tag);
|
|
|
|
}
|
|
|
|
/* send caps */
|
|
|
|
caps = gst_pad_get_current_caps (pad);
|
|
|
|
if (caps) {
|
|
|
|
gst_rtp_gst_pay_send_caps (rtpgstpay, rtpgstpay->current_CV, caps);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
}
|
|
|
|
rtpgstpay->last_config = timestamp;
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_rtp_gst_pay_handle_buffer (GstRTPBasePayload * basepayload,
|
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret;
|
|
|
|
GstRtpGSTPay *rtpgstpay;
|
|
|
|
GstClockTime timestamp;
|
|
|
|
|
|
|
|
rtpgstpay = GST_RTP_GST_PAY (basepayload);
|
|
|
|
|
2015-06-10 12:33:50 +00:00
|
|
|
timestamp = GST_BUFFER_PTS (buffer);
|
2012-11-01 15:54:58 +00:00
|
|
|
|
2013-07-26 01:12:05 +00:00
|
|
|
/* check if we need to send the caps and taglist now */
|
|
|
|
if (rtpgstpay->config_interval > 0) {
|
|
|
|
GST_DEBUG_OBJECT (rtpgstpay,
|
2013-08-21 07:39:30 +00:00
|
|
|
"timestamp %" GST_TIME_FORMAT ", last config %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpgstpay->last_config));
|
|
|
|
|
|
|
|
if (timestamp != GST_CLOCK_TIME_NONE &&
|
2013-07-26 01:12:05 +00:00
|
|
|
rtpgstpay->last_config != GST_CLOCK_TIME_NONE) {
|
|
|
|
guint64 diff;
|
|
|
|
|
|
|
|
/* calculate diff between last SPS/PPS in milliseconds */
|
2013-08-21 07:39:30 +00:00
|
|
|
if (timestamp > rtpgstpay->last_config)
|
|
|
|
diff = timestamp - rtpgstpay->last_config;
|
2013-07-26 01:12:05 +00:00
|
|
|
else
|
|
|
|
diff = 0;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (rtpgstpay,
|
|
|
|
"interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
|
|
|
|
|
|
|
|
/* bigger than interval, queue SPS/PPS */
|
|
|
|
if (GST_TIME_AS_SECONDS (diff) >= rtpgstpay->config_interval)
|
2013-08-21 07:39:30 +00:00
|
|
|
gst_rtp_gst_pay_send_config (rtpgstpay, timestamp);
|
2013-07-26 01:12:05 +00:00
|
|
|
} else {
|
2013-08-21 07:39:30 +00:00
|
|
|
gst_rtp_gst_pay_send_config (rtpgstpay, timestamp);
|
2013-07-26 01:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:54:58 +00:00
|
|
|
/* caps always from SDP for now */
|
|
|
|
if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
|
|
|
|
rtpgstpay->flags |= (1 << 3);
|
|
|
|
|
|
|
|
gst_adapter_push (rtpgstpay->adapter, buffer);
|
|
|
|
ret = gst_rtp_gst_pay_flush (rtpgstpay, timestamp);
|
2010-12-12 04:10:01 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_rtp_gst_pay_plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
return gst_element_register (plugin, "rtpgstpay",
|
|
|
|
GST_RANK_NONE, GST_TYPE_RTP_GST_PAY);
|
|
|
|
}
|