gstreamer/gst/rtp/gstrtpg723pay.c

318 lines
8.9 KiB
C
Raw Normal View History

2009-12-17 16:27:42 +00:00
/* GStreamer
* Copyright (C) <2007> Nokia Corporation
* Copyright (C) <2007> Collabora Ltd
* @author: Olivier Crete <olivier.crete@collabora.co.uk>
*
* 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
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <gst/rtp/gstrtpbuffer.h>
#include <gst/base/gstadapter.h>
#include "gstrtpg723pay.h"
#define GST_RTP_PAYLOAD_G723 4
#define GST_RTP_PAYLOAD_G723_STRING "4"
#define G723_FRAME_DURATION (30 * GST_MSECOND)
2011-11-11 11:25:01 +00:00
static gboolean gst_rtp_g723_pay_set_caps (GstRTPBasePayload * payload,
GstCaps * caps);
2011-11-11 11:25:01 +00:00
static GstFlowReturn gst_rtp_g723_pay_handle_buffer (GstRTPBasePayload *
payload, GstBuffer * buf);
2009-12-17 16:27:42 +00:00
static GstStaticPadTemplate gst_rtp_g723_pay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/G723, " /* according to RFC 3551 */
"channels = (int) 1, " "rate = (int) 8000")
);
static GstStaticPadTemplate gst_rtp_g723_pay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-rtp, "
"media = (string) \"audio\", "
"payload = (int) " GST_RTP_PAYLOAD_G723_STRING ", "
"clock-rate = (int) 8000, "
"encoding-name = (string) \"G723\"; "
"application/x-rtp, "
"media = (string) \"audio\", "
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
"clock-rate = (int) 8000, " "encoding-name = (string) \"G723\"")
);
static void gst_rtp_g723_pay_finalize (GObject * object);
2009-12-17 16:27:42 +00:00
static GstStateChangeReturn gst_rtp_g723_pay_change_state (GstElement * element,
GstStateChange transition);
2011-04-25 15:27:40 +00:00
#define gst_rtp_g723_pay_parent_class parent_class
2011-11-11 11:25:01 +00:00
G_DEFINE_TYPE (GstRTPG723Pay, gst_rtp_g723_pay, GST_TYPE_RTP_BASE_PAYLOAD);
2009-12-17 16:27:42 +00:00
static void
gst_rtp_g723_pay_class_init (GstRTPG723PayClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
2011-11-11 11:25:01 +00:00
GstRTPBasePayloadClass *payload_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
2011-11-11 11:25:01 +00:00
payload_class = (GstRTPBasePayloadClass *) klass;
gobject_class->finalize = gst_rtp_g723_pay_finalize;
gstelement_class->change_state = gst_rtp_g723_pay_change_state;
2009-12-17 16:27:42 +00:00
2011-04-25 15:27:40 +00:00
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_rtp_g723_pay_sink_template));
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_rtp_g723_pay_src_template));
gst_element_class_set_static_metadata (gstelement_class,
"RTP G.723 payloader", "Codec/Payloader/Network/RTP",
2011-04-25 15:27:40 +00:00
"Packetize G.723 audio into RTP packets",
"Wim Taymans <wim.taymans@gmail.com>");
2009-12-17 16:27:42 +00:00
payload_class->set_caps = gst_rtp_g723_pay_set_caps;
payload_class->handle_buffer = gst_rtp_g723_pay_handle_buffer;
}
static void
2011-04-25 15:27:40 +00:00
gst_rtp_g723_pay_init (GstRTPG723Pay * pay)
2009-12-17 16:27:42 +00:00
{
2011-11-11 11:25:01 +00:00
GstRTPBasePayload *payload = GST_RTP_BASE_PAYLOAD (pay);
pay->adapter = gst_adapter_new ();
2009-12-17 16:27:42 +00:00
payload->pt = GST_RTP_PAYLOAD_G723;
2011-11-11 11:25:01 +00:00
gst_rtp_base_payload_set_options (payload, "audio", FALSE, "G723", 8000);
}
2009-12-17 16:27:42 +00:00
static void
gst_rtp_g723_pay_finalize (GObject * object)
{
GstRTPG723Pay *pay;
pay = GST_RTP_G723_PAY (object);
2009-12-17 16:27:42 +00:00
g_object_unref (pay->adapter);
pay->adapter = NULL;
G_OBJECT_CLASS (parent_class)->finalize (object);
2009-12-17 16:27:42 +00:00
}
2009-12-17 16:27:42 +00:00
static gboolean
2011-11-11 11:25:01 +00:00
gst_rtp_g723_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
2009-12-17 16:27:42 +00:00
{
gboolean res;
GstStructure *structure;
gint pt;
structure = gst_caps_get_structure (caps, 0);
if (!gst_structure_get_int (structure, "payload", &pt))
pt = GST_RTP_PAYLOAD_G723;
payload->pt = pt;
payload->dynamic = pt != GST_RTP_PAYLOAD_G723;
2011-11-11 11:25:01 +00:00
res = gst_rtp_base_payload_set_outcaps (payload, NULL);
2009-12-17 16:27:42 +00:00
return res;
}
static GstFlowReturn
gst_rtp_g723_pay_flush (GstRTPG723Pay * pay)
2009-12-17 16:27:42 +00:00
{
GstBuffer *outbuf;
GstFlowReturn ret;
guint8 *payload;
guint avail;
2011-04-25 15:27:40 +00:00
GstRTPBuffer rtp = { NULL };
avail = gst_adapter_available (pay->adapter);
outbuf = gst_rtp_buffer_new_allocate (avail, 0, 0);
2011-04-25 15:27:40 +00:00
gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
payload = gst_rtp_buffer_get_payload (&rtp);
GST_BUFFER_TIMESTAMP (outbuf) = pay->timestamp;
GST_BUFFER_DURATION (outbuf) = pay->duration;
2009-12-17 16:27:42 +00:00
/* copy G723 data as payload */
gst_adapter_copy (pay->adapter, payload, 0, avail);
2009-12-17 16:27:42 +00:00
/* flush bytes from adapter */
gst_adapter_flush (pay->adapter, avail);
pay->timestamp = GST_CLOCK_TIME_NONE;
pay->duration = 0;
2009-12-17 16:27:42 +00:00
/* set discont and marker */
if (pay->discont) {
GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
2011-04-25 15:27:40 +00:00
gst_rtp_buffer_set_marker (&rtp, TRUE);
pay->discont = FALSE;
2009-12-17 16:27:42 +00:00
}
2011-04-25 15:27:40 +00:00
gst_rtp_buffer_unmap (&rtp);
2009-12-17 16:27:42 +00:00
2011-11-11 11:25:01 +00:00
ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (pay), outbuf);
2009-12-17 16:27:42 +00:00
return ret;
}
2009-12-17 16:27:42 +00:00
/* 00 high-rate speech (6.3 kb/s) 24
* 01 low-rate speech (5.3 kb/s) 20
* 10 SID frame 4
* 11 reserved 0 */
static const guint size_tab[4] = {
24, 20, 4, 0
};
2009-12-17 16:27:42 +00:00
static GstFlowReturn
2011-11-11 11:25:01 +00:00
gst_rtp_g723_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buf)
{
GstFlowReturn ret = GST_FLOW_OK;
2012-01-23 16:25:37 +00:00
GstMapInfo map;
guint8 HDR;
GstRTPG723Pay *pay;
GstClockTime packet_dur, timestamp;
guint payload_len, packet_len;
pay = GST_RTP_G723_PAY (payload);
2012-01-23 16:25:37 +00:00
gst_buffer_map (buf, &map, GST_MAP_READ);
timestamp = GST_BUFFER_TIMESTAMP (buf);
if (GST_BUFFER_IS_DISCONT (buf)) {
/* flush everything on discont */
gst_adapter_clear (pay->adapter);
pay->timestamp = GST_CLOCK_TIME_NONE;
pay->duration = 0;
pay->discont = TRUE;
2009-12-17 16:27:42 +00:00
}
/* should be one of these sizes */
2012-01-23 16:25:37 +00:00
if (map.size != 4 && map.size != 20 && map.size != 24)
goto invalid_size;
2009-12-17 16:27:42 +00:00
/* check size by looking at the header bits */
2012-01-23 16:25:37 +00:00
HDR = map.data[0] & 0x3;
if (size_tab[HDR] != map.size)
goto wrong_size;
2009-12-17 16:27:42 +00:00
/* calculate packet size and duration */
2012-01-23 16:25:37 +00:00
payload_len = gst_adapter_available (pay->adapter) + map.size;
packet_dur = pay->duration + G723_FRAME_DURATION;
packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
2009-12-17 16:27:42 +00:00
2011-11-11 11:25:01 +00:00
if (gst_rtp_base_payload_is_filled (payload, packet_len, packet_dur)) {
/* size or duration would overflow the packet, flush the queued data */
ret = gst_rtp_g723_pay_flush (pay);
2009-12-17 16:27:42 +00:00
}
/* update timestamp, we keep the timestamp for the first packet in the adapter
* but are able to calculate it from next packets. */
if (timestamp != GST_CLOCK_TIME_NONE && pay->timestamp == GST_CLOCK_TIME_NONE) {
if (timestamp > pay->duration)
pay->timestamp = timestamp - pay->duration;
else
pay->timestamp = 0;
2009-12-17 16:27:42 +00:00
}
2012-01-23 16:25:37 +00:00
gst_buffer_unmap (buf, &map);
2009-12-17 16:27:42 +00:00
/* add packet to the queue */
gst_adapter_push (pay->adapter, buf);
pay->duration = packet_dur;
/* check if we can flush now */
if (pay->duration >= payload->min_ptime) {
ret = gst_rtp_g723_pay_flush (pay);
2009-12-17 16:27:42 +00:00
}
return ret;
/* WARNINGS */
2009-12-17 16:27:42 +00:00
invalid_size:
{
GST_ELEMENT_WARNING (pay, STREAM, WRONG_TYPE,
2009-12-17 16:27:42 +00:00
("Invalid input buffer size"),
2012-01-23 16:25:37 +00:00
("Input size should be 4, 20 or 24, got %" G_GSIZE_FORMAT, map.size));
gst_buffer_unmap (buf, &map);
2009-12-17 16:27:42 +00:00
gst_buffer_unref (buf);
return GST_FLOW_OK;
2009-12-17 16:27:42 +00:00
}
wrong_size:
{
GST_ELEMENT_WARNING (pay, STREAM, WRONG_TYPE,
("Wrong input buffer size"),
("Expected input buffer size %u but got %" G_GSIZE_FORMAT,
2012-01-23 16:25:37 +00:00
size_tab[HDR], map.size));
gst_buffer_unmap (buf, &map);
gst_buffer_unref (buf);
return GST_FLOW_OK;
}
}
static GstStateChangeReturn
gst_rtp_g723_pay_change_state (GstElement * element, GstStateChange transition)
{
GstStateChangeReturn ret;
GstRTPG723Pay *pay;
pay = GST_RTP_G723_PAY (element);
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_adapter_clear (pay->adapter);
pay->timestamp = GST_CLOCK_TIME_NONE;
pay->duration = 0;
pay->discont = TRUE;
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
gst_adapter_clear (pay->adapter);
break;
default:
break;
}
return ret;
2009-12-17 16:27:42 +00:00
}
/*Plugin init functions*/
gboolean
gst_rtp_g723_pay_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "rtpg723pay", GST_RANK_SECONDARY,
2009-12-17 16:27:42 +00:00
gst_rtp_g723_pay_get_type ());
}