2008-01-25 10:53:17 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2007> Thijs Vermeir <thijsvermeir@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.
|
2008-01-25 10:53:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gst/rtp/gstrtpbuffer.h>
|
|
|
|
|
|
|
|
#include "gstrtpmpvpay.h"
|
|
|
|
|
2010-10-12 13:02:42 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (rtpmpvpay_debug);
|
|
|
|
#define GST_CAT_DEFAULT (rtpmpvpay_debug)
|
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
static GstStaticPadTemplate gst_rtp_mpv_pay_sink_template =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/mpeg, "
|
|
|
|
"mpegversion = (int) 2, systemstream = (boolean) FALSE")
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate gst_rtp_mpv_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_MPV_STRING ", "
|
|
|
|
"clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\"")
|
|
|
|
);
|
|
|
|
|
2010-12-30 15:22:48 +00:00
|
|
|
static GstStateChangeReturn gst_rtp_mpv_pay_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
|
|
|
|
|
|
|
static void gst_rtp_mpv_pay_finalize (GObject * object);
|
|
|
|
|
|
|
|
static GstFlowReturn gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay);
|
2011-11-11 11:25:01 +00:00
|
|
|
static gboolean gst_rtp_mpv_pay_setcaps (GstRTPBasePayload * payload,
|
2008-01-25 10:53:17 +00:00
|
|
|
GstCaps * caps);
|
2011-11-11 11:25:01 +00:00
|
|
|
static GstFlowReturn gst_rtp_mpv_pay_handle_buffer (GstRTPBasePayload *
|
2008-01-25 10:53:17 +00:00
|
|
|
payload, GstBuffer * buffer);
|
2011-11-15 15:31:45 +00:00
|
|
|
static gboolean gst_rtp_mpv_pay_sink_event (GstRTPBasePayload * payload,
|
2011-06-13 11:25:49 +00:00
|
|
|
GstEvent * event);
|
2008-01-25 10:53:17 +00:00
|
|
|
|
2011-04-25 15:27:40 +00:00
|
|
|
#define gst_rtp_mpv_pay_parent_class parent_class
|
2011-11-11 11:25:01 +00:00
|
|
|
G_DEFINE_TYPE (GstRTPMPVPay, gst_rtp_mpv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
|
2008-01-25 10:53:17 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtp_mpv_pay_class_init (GstRTPMPVPayClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
2010-12-30 15:22:48 +00:00
|
|
|
GstElementClass *gstelement_class;
|
2011-11-11 11:25:01 +00:00
|
|
|
GstRTPBasePayloadClass *gstrtpbasepayload_class;
|
2008-01-25 10:53:17 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
2010-12-30 15:22:48 +00:00
|
|
|
gstelement_class = (GstElementClass *) klass;
|
2011-11-11 11:25:01 +00:00
|
|
|
gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
|
2008-01-25 10:53:17 +00:00
|
|
|
|
|
|
|
gobject_class->finalize = gst_rtp_mpv_pay_finalize;
|
|
|
|
|
2010-12-30 15:22:48 +00:00
|
|
|
gstelement_class->change_state = gst_rtp_mpv_pay_change_state;
|
|
|
|
|
2011-04-25 15:27:40 +00:00
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&gst_rtp_mpv_pay_sink_template));
|
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
|
|
|
gst_static_pad_template_get (&gst_rtp_mpv_pay_src_template));
|
|
|
|
|
2012-04-09 23:51:41 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2011-04-25 15:27:40 +00:00
|
|
|
"RTP MPEG2 ES video payloader", "Codec/Payloader/Network/RTP",
|
|
|
|
"Payload-encodes MPEG2 ES into RTP packets (RFC 2250)",
|
|
|
|
"Thijs Vermeir <thijsvermeir@gmail.com>");
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
gstrtpbasepayload_class->set_caps = gst_rtp_mpv_pay_setcaps;
|
|
|
|
gstrtpbasepayload_class->handle_buffer = gst_rtp_mpv_pay_handle_buffer;
|
2011-11-15 15:31:45 +00:00
|
|
|
gstrtpbasepayload_class->sink_event = gst_rtp_mpv_pay_sink_event;
|
2010-10-12 13:02:42 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (rtpmpvpay_debug, "rtpmpvpay", 0,
|
|
|
|
"MPEG2 ES Video RTP Payloader");
|
2008-01-25 10:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-25 15:27:40 +00:00
|
|
|
gst_rtp_mpv_pay_init (GstRTPMPVPay * rtpmpvpay)
|
2008-01-25 10:53:17 +00:00
|
|
|
{
|
2011-11-11 11:25:01 +00:00
|
|
|
GST_RTP_BASE_PAYLOAD (rtpmpvpay)->clock_rate = 90000;
|
|
|
|
GST_RTP_BASE_PAYLOAD_PT (rtpmpvpay) = GST_RTP_PAYLOAD_MPV;
|
2008-01-25 10:53:17 +00:00
|
|
|
|
|
|
|
rtpmpvpay->adapter = gst_adapter_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_rtp_mpv_pay_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstRTPMPVPay *rtpmpvpay;
|
|
|
|
|
|
|
|
rtpmpvpay = GST_RTP_MPV_PAY (object);
|
|
|
|
|
|
|
|
g_object_unref (rtpmpvpay->adapter);
|
|
|
|
rtpmpvpay->adapter = NULL;
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2010-12-30 15:22:48 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_mpv_pay_reset (GstRTPMPVPay * pay)
|
|
|
|
{
|
|
|
|
pay->first_ts = -1;
|
|
|
|
pay->duration = 0;
|
|
|
|
gst_adapter_clear (pay->adapter);
|
|
|
|
GST_DEBUG_OBJECT (pay, "reset depayloader");
|
|
|
|
}
|
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
static gboolean
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_mpv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
|
2008-01-25 10:53:17 +00:00
|
|
|
{
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_base_payload_set_options (payload, "video", FALSE, "MPV", 90000);
|
|
|
|
return gst_rtp_base_payload_set_outcaps (payload, NULL);
|
2008-01-25 10:53:17 +00:00
|
|
|
}
|
|
|
|
|
2010-12-30 15:22:48 +00:00
|
|
|
static gboolean
|
2011-11-15 15:31:45 +00:00
|
|
|
gst_rtp_mpv_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
|
2010-12-30 15:22:48 +00:00
|
|
|
{
|
2011-06-13 11:25:49 +00:00
|
|
|
gboolean ret;
|
2010-12-30 15:22:48 +00:00
|
|
|
GstRTPMPVPay *rtpmpvpay;
|
|
|
|
|
2011-06-13 11:25:49 +00:00
|
|
|
rtpmpvpay = GST_RTP_MPV_PAY (payload);
|
2010-12-30 15:22:48 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_EOS:
|
|
|
|
/* make sure we push the last packets in the adapter on EOS */
|
|
|
|
gst_rtp_mpv_pay_flush (rtpmpvpay);
|
|
|
|
break;
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
gst_rtp_mpv_pay_reset (rtpmpvpay);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-15 15:31:45 +00:00
|
|
|
ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
|
2010-12-30 15:22:48 +00:00
|
|
|
|
2011-06-13 11:25:49 +00:00
|
|
|
return ret;
|
2010-12-30 15:22:48 +00:00
|
|
|
}
|
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
static GstFlowReturn
|
2009-07-13 15:53:25 +00:00
|
|
|
gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay)
|
2008-01-25 10:53:17 +00:00
|
|
|
{
|
|
|
|
GstBuffer *outbuf;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
guint avail;
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
guint8 *payload;
|
|
|
|
|
|
|
|
avail = gst_adapter_available (rtpmpvpay->adapter);
|
|
|
|
|
2009-07-13 15:53:25 +00:00
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
|
|
|
|
while (avail > 0) {
|
|
|
|
guint towrite;
|
|
|
|
guint packet_len;
|
|
|
|
guint payload_len;
|
2011-04-25 15:27:40 +00:00
|
|
|
GstRTPBuffer rtp = { NULL };
|
2013-07-16 19:37:49 +00:00
|
|
|
GstBuffer *paybuf;
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2013-08-29 11:15:15 +00:00
|
|
|
packet_len = gst_rtp_buffer_calc_packet_len (avail + 4, 0, 0);
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpvpay));
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2013-08-29 11:15:15 +00:00
|
|
|
payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2013-07-16 19:37:49 +00:00
|
|
|
outbuf = gst_rtp_buffer_new_allocate (4, 0, 0);
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2013-08-29 11:15:15 +00:00
|
|
|
payload_len -= 4;
|
|
|
|
|
2011-04-25 15:27:40 +00:00
|
|
|
gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
|
|
|
|
|
|
|
|
payload = gst_rtp_buffer_get_payload (&rtp);
|
2009-07-13 15:53:25 +00:00
|
|
|
/* enable MPEG Video-specific header
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | MBZ |T| TR | |N|S|B|E| P | | BFC | | FFC |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* AN FBV FFV
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* fill in the MPEG Video-specific header
|
|
|
|
* data is set to 0x0 here
|
|
|
|
*/
|
|
|
|
memset (payload, 0x0, 4);
|
|
|
|
|
|
|
|
avail -= payload_len;
|
|
|
|
|
2011-04-25 15:27:40 +00:00
|
|
|
gst_rtp_buffer_set_marker (&rtp, avail == 0);
|
|
|
|
gst_rtp_buffer_unmap (&rtp);
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2013-07-16 19:37:49 +00:00
|
|
|
paybuf = gst_adapter_take_buffer_fast (rtpmpvpay->adapter, payload_len);
|
|
|
|
outbuf = gst_buffer_append (outbuf, paybuf);
|
|
|
|
|
2009-07-13 15:53:25 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (outbuf) = rtpmpvpay->first_ts;
|
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmpvpay), outbuf);
|
2008-01-25 10:53:17 +00:00
|
|
|
}
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-11-11 11:25:01 +00:00
|
|
|
gst_rtp_mpv_pay_handle_buffer (GstRTPBasePayload * basepayload,
|
2008-01-25 10:53:17 +00:00
|
|
|
GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GstRTPMPVPay *rtpmpvpay;
|
2009-04-18 16:11:00 +00:00
|
|
|
guint avail, packet_len;
|
2008-01-25 10:53:17 +00:00
|
|
|
GstClockTime timestamp, duration;
|
2009-07-13 15:53:25 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2008-01-25 10:53:17 +00:00
|
|
|
|
|
|
|
rtpmpvpay = GST_RTP_MPV_PAY (basepayload);
|
|
|
|
|
|
|
|
timestamp = GST_BUFFER_TIMESTAMP (buffer);
|
|
|
|
duration = GST_BUFFER_DURATION (buffer);
|
|
|
|
|
2010-12-30 15:22:48 +00:00
|
|
|
if (GST_BUFFER_IS_DISCONT (buffer)) {
|
|
|
|
GST_DEBUG_OBJECT (rtpmpvpay, "DISCONT");
|
|
|
|
gst_rtp_mpv_pay_reset (rtpmpvpay);
|
|
|
|
}
|
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
avail = gst_adapter_available (rtpmpvpay->adapter);
|
|
|
|
|
2009-07-13 15:53:25 +00:00
|
|
|
if (duration == -1)
|
|
|
|
duration = 0;
|
|
|
|
|
2010-10-12 13:02:42 +00:00
|
|
|
if (rtpmpvpay->first_ts == GST_CLOCK_TIME_NONE || avail == 0)
|
2008-01-25 10:53:17 +00:00
|
|
|
rtpmpvpay->first_ts = timestamp;
|
2010-10-12 13:02:42 +00:00
|
|
|
|
|
|
|
if (avail == 0) {
|
2008-01-25 10:53:17 +00:00
|
|
|
rtpmpvpay->duration = duration;
|
2010-10-12 13:02:42 +00:00
|
|
|
} else {
|
|
|
|
rtpmpvpay->duration += duration;
|
2008-01-25 10:53:17 +00:00
|
|
|
}
|
|
|
|
|
2010-10-12 13:02:42 +00:00
|
|
|
gst_adapter_push (rtpmpvpay->adapter, buffer);
|
|
|
|
avail = gst_adapter_available (rtpmpvpay->adapter);
|
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
/* get packet length of previous data and this new data,
|
|
|
|
* payload length includes a 4 byte MPEG video-specific header */
|
2009-07-13 15:53:25 +00:00
|
|
|
packet_len = gst_rtp_buffer_calc_packet_len (avail, 4, 0);
|
2010-10-12 13:02:42 +00:00
|
|
|
GST_LOG_OBJECT (rtpmpvpay, "available %d, rtp packet length %d", avail,
|
|
|
|
packet_len);
|
2008-01-25 10:53:17 +00:00
|
|
|
|
2011-11-11 11:25:01 +00:00
|
|
|
if (gst_rtp_base_payload_is_filled (basepayload,
|
2010-10-12 13:02:42 +00:00
|
|
|
packet_len, rtpmpvpay->duration)) {
|
2009-07-13 15:53:25 +00:00
|
|
|
ret = gst_rtp_mpv_pay_flush (rtpmpvpay);
|
2010-10-12 13:02:42 +00:00
|
|
|
} else {
|
2009-07-13 15:53:25 +00:00
|
|
|
rtpmpvpay->first_ts = timestamp;
|
2008-01-25 10:53:17 +00:00
|
|
|
}
|
2009-07-13 15:53:25 +00:00
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-30 15:22:48 +00:00
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_rtp_mpv_pay_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstRTPMPVPay *rtpmpvpay;
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
rtpmpvpay = GST_RTP_MPV_PAY (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
gst_rtp_mpv_pay_reset (rtpmpvpay);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
gst_rtp_mpv_pay_reset (rtpmpvpay);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-25 10:53:17 +00:00
|
|
|
gboolean
|
|
|
|
gst_rtp_mpv_pay_plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
return gst_element_register (plugin, "rtpmpvpay",
|
2010-12-21 15:49:28 +00:00
|
|
|
GST_RANK_SECONDARY, GST_TYPE_RTP_MPV_PAY);
|
2008-01-25 10:53:17 +00:00
|
|
|
}
|