2007-02-18 13:24:26 +00:00
|
|
|
/* GStreamer
|
2008-11-25 18:03:02 +00:00
|
|
|
* Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
|
2007-02-18 13:24:26 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2007-02-18 13:24:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gst/rtp/gstrtpbuffer.h>
|
|
|
|
|
|
|
|
#include "gstrtpmp2tpay.h"
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_rtp_mp2t_pay_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/mpegts,"
|
|
|
|
"packetsize=(int)188," "systemstream=(boolean)true")
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_rtp_mp2t_pay_src_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("application/x-rtp, "
|
|
|
|
"media = (string) \"video\", "
|
|
|
|
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
|
2011-08-31 16:45:15 +00:00
|
|
|
"clock-rate = (int) 90000, " "encoding-name = (string) \"MP2T\"")
|
2007-02-18 13:24:26 +00:00
|
|
|
);
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
static gboolean gst_rtp_mp2t_pay_setcaps (GstRTPBasePayload * payload,
|
2007-02-18 13:24:26 +00:00
|
|
|
GstCaps * caps);
|
2011-11-11 11:25:01 +00:00
|
|
|
static GstFlowReturn gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload *
|
2007-02-18 13:24:26 +00:00
|
|
|
payload, GstBuffer * buffer);
|
2007-11-15 18:19:19 +00:00
|
|
|
static GstFlowReturn gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay);
|
|
|
|
static void gst_rtp_mp2t_pay_finalize (GObject * object);
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2011-07-10 19:50:19 +00:00
|
|
|
#define gst_rtp_mp2t_pay_parent_class parent_class
|
2011-11-11 11:25:01 +00:00
|
|
|
G_DEFINE_TYPE (GstRTPMP2TPay, gst_rtp_mp2t_pay, GST_TYPE_RTP_BASE_PAYLOAD);
|
2007-02-18 13:24:26 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtp_mp2t_pay_class_init (GstRTPMP2TPayClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2011-07-10 19:50:19 +00:00
|
|
|
GstElementClass *gstelement_class;
|
2011-11-11 11:25:01 +00:00
|
|
|
GstRTPBasePayloadClass *gstrtpbasepayload_class;
|
2007-02-18 13:24:26 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2011-07-10 19:50:19 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2011-11-11 11:25:01 +00:00
|
|
|
gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2007-11-15 18:19:19 +00:00
|
|
|
gobject_class->finalize = gst_rtp_mp2t_pay_finalize;
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
gstrtpbasepayload_class->set_caps = gst_rtp_mp2t_pay_setcaps;
|
|
|
|
gstrtpbasepayload_class->handle_buffer = gst_rtp_mp2t_pay_handle_buffer;
|
2011-07-10 19:50:19 +00:00
|
|
|
|
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&gst_rtp_mp2t_pay_sink_template));
|
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&gst_rtp_mp2t_pay_src_template));
|
2012-04-09 23:51:41 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2011-07-10 19:50:19 +00:00
|
|
|
"RTP MPEG2 Transport Stream payloader", "Codec/Payloader/Network/RTP",
|
|
|
|
"Payload-encodes MPEG2 TS into RTP packets (RFC 2250)",
|
|
|
|
"Wim Taymans <wim.taymans@gmail.com>");
|
2007-02-18 13:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-07-10 19:50:19 +00:00
|
|
|
gst_rtp_mp2t_pay_init (GstRTPMP2TPay * rtpmp2tpay)
|
2007-02-18 13:24:26 +00:00
|
|
|
{
|
2011-11-11 11:25:01 +00:00
|
|
|
GST_RTP_BASE_PAYLOAD (rtpmp2tpay)->clock_rate = 90000;
|
|
|
|
GST_RTP_BASE_PAYLOAD_PT (rtpmp2tpay) = GST_RTP_PAYLOAD_MP2T;
|
2007-11-15 18:19:19 +00:00
|
|
|
|
|
|
|
rtpmp2tpay->adapter = gst_adapter_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtp_mp2t_pay_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstRTPMP2TPay *rtpmp2tpay;
|
|
|
|
|
|
|
|
rtpmp2tpay = GST_RTP_MP2T_PAY (object);
|
|
|
|
|
|
|
|
g_object_unref (rtpmp2tpay->adapter);
|
|
|
|
rtpmp2tpay->adapter = NULL;
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2007-02-18 13:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_mp2t_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
|
2007-02-18 13:24:26 +00:00
|
|
|
{
|
2010-02-04 14:59:25 +00:00
|
|
|
gboolean res;
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_base_payload_set_options (payload, "video", TRUE, "MP2T", 90000);
|
|
|
|
res = gst_rtp_base_payload_set_outcaps (payload, NULL);
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2010-02-04 14:59:25 +00:00
|
|
|
return res;
|
2007-02-18 13:24:26 +00:00
|
|
|
}
|
|
|
|
|
2007-11-15 18:19:19 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay)
|
|
|
|
{
|
2012-05-18 10:53:44 +00:00
|
|
|
guint avail, mtu;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2007-11-15 18:19:19 +00:00
|
|
|
GstBuffer *outbuf;
|
|
|
|
|
|
|
|
avail = gst_adapter_available (rtpmp2tpay->adapter);
|
|
|
|
|
2012-05-18 10:53:44 +00:00
|
|
|
mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp2tpay);
|
|
|
|
|
|
|
|
while (avail > 0 && (ret == GST_FLOW_OK)) {
|
|
|
|
guint towrite;
|
|
|
|
guint payload_len;
|
|
|
|
guint packet_len;
|
2013-07-16 19:37:49 +00:00
|
|
|
GstBuffer *paybuf;
|
2012-05-18 10:53:44 +00:00
|
|
|
|
|
|
|
/* this will be the total length of the packet */
|
|
|
|
packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
|
|
|
|
|
|
|
|
/* fill one MTU or all available bytes */
|
|
|
|
towrite = MIN (packet_len, mtu);
|
|
|
|
|
|
|
|
/* this is the payload length */
|
|
|
|
payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
|
|
|
|
payload_len -= payload_len % 188;
|
|
|
|
|
|
|
|
/* need whole packets */
|
|
|
|
if (!payload_len)
|
|
|
|
break;
|
2007-11-15 18:19:19 +00:00
|
|
|
|
2012-05-18 10:53:44 +00:00
|
|
|
/* create buffer to hold the payload */
|
2013-07-16 19:37:49 +00:00
|
|
|
outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
|
2007-11-15 18:19:19 +00:00
|
|
|
|
2012-05-18 10:53:44 +00:00
|
|
|
/* get payload */
|
2013-07-16 19:37:49 +00:00
|
|
|
paybuf = gst_adapter_take_buffer_fast (rtpmp2tpay->adapter, payload_len);
|
|
|
|
outbuf = gst_buffer_append (outbuf, paybuf);
|
2012-05-18 10:53:44 +00:00
|
|
|
avail -= payload_len;
|
2007-11-15 18:19:19 +00:00
|
|
|
|
2012-05-18 10:53:44 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = rtpmp2tpay->first_ts;
|
|
|
|
GST_BUFFER_DURATION (outbuf) = rtpmp2tpay->duration;
|
2007-11-15 18:19:19 +00:00
|
|
|
|
2012-05-31 21:39:25 +00:00
|
|
|
GST_DEBUG_OBJECT (rtpmp2tpay, "pushing buffer of size %u",
|
|
|
|
(guint) gst_buffer_get_size (outbuf));
|
2012-05-18 10:53:44 +00:00
|
|
|
|
|
|
|
ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp2tpay), outbuf);
|
|
|
|
}
|
2007-11-15 18:19:19 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-18 13:24:26 +00:00
|
|
|
static GstFlowReturn
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload * basepayload,
|
2007-02-18 13:24:26 +00:00
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstRTPMP2TPay *rtpmp2tpay;
|
2007-11-15 18:19:19 +00:00
|
|
|
guint size, avail, packet_len;
|
2007-09-19 16:24:09 +00:00
|
|
|
GstClockTime timestamp, duration;
|
2007-02-18 13:24:26 +00:00
|
|
|
GstFlowReturn ret;
|
|
|
|
|
|
|
|
rtpmp2tpay = GST_RTP_MP2T_PAY (basepayload);
|
|
|
|
|
2011-07-10 19:50:19 +00:00
|
|
|
size = gst_buffer_get_size (buffer);
|
2007-02-18 13:24:26 +00:00
|
|
|
timestamp = GST_BUFFER_TIMESTAMP (buffer);
|
2007-09-19 16:24:09 +00:00
|
|
|
duration = GST_BUFFER_DURATION (buffer);
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2012-05-18 10:53:44 +00:00
|
|
|
again:
|
2007-11-15 18:19:19 +00:00
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
avail = gst_adapter_available (rtpmp2tpay->adapter);
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2007-11-15 18:19:19 +00:00
|
|
|
/* Initialize new RTP payload */
|
|
|
|
if (avail == 0) {
|
|
|
|
rtpmp2tpay->first_ts = timestamp;
|
|
|
|
rtpmp2tpay->duration = duration;
|
|
|
|
}
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2012-05-18 10:53:44 +00:00
|
|
|
/* get packet length of previous data and this new data */
|
|
|
|
packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2012-05-18 10:53:44 +00:00
|
|
|
/* if this buffer is going to overflow the packet, flush what we have,
|
|
|
|
* or if upstream is handing us several packets, to keep latency low */
|
|
|
|
if (!size || gst_rtp_base_payload_is_filled (basepayload,
|
2007-11-15 18:19:19 +00:00
|
|
|
packet_len, rtpmp2tpay->duration + duration)) {
|
|
|
|
ret = gst_rtp_mp2t_pay_flush (rtpmp2tpay);
|
|
|
|
rtpmp2tpay->first_ts = timestamp;
|
|
|
|
rtpmp2tpay->duration = duration;
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2007-11-15 18:19:19 +00:00
|
|
|
/* keep filling the payload */
|
|
|
|
} else {
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (duration))
|
|
|
|
rtpmp2tpay->duration += duration;
|
|
|
|
}
|
2007-02-18 13:24:26 +00:00
|
|
|
|
2007-11-15 18:19:19 +00:00
|
|
|
/* copy buffer to adapter */
|
2012-05-18 10:53:44 +00:00
|
|
|
if (buffer) {
|
|
|
|
gst_adapter_push (rtpmp2tpay->adapter, buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size >= (188 * 2)) {
|
|
|
|
size = 0;
|
|
|
|
goto again;
|
|
|
|
}
|
2007-02-18 13:24:26 +00:00
|
|
|
|
|
|
|
return ret;
|
2007-11-15 18:19:19 +00:00
|
|
|
|
2007-02-18 13:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_rtp_mp2t_pay_plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
return gst_element_register (plugin, "rtpmp2tpay",
|
2010-12-21 15:49:28 +00:00
|
|
|
GST_RANK_SECONDARY, GST_TYPE_RTP_MP2T_PAY);
|
2007-02-18 13:24:26 +00:00
|
|
|
}
|