2008-05-13 08:35:55 +00:00
|
|
|
/* GStreamer
|
2008-11-11 17:29:03 +00:00
|
|
|
* Copyright (C) <2007> Nokia Corporation
|
|
|
|
* Copyright (C) <2007> Collabora Ltd
|
|
|
|
* @author: Olivier Crete <olivier.crete@collabora.co.uk>
|
2008-05-13 08:35:55 +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
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
/*
|
|
|
|
* This payloader assumes that the data will ALWAYS come as zero or more
|
|
|
|
* 10 bytes frame of audio followed by 0 or 1 2 byte frame of silence.
|
|
|
|
* Any other buffer format won't work
|
|
|
|
*/
|
|
|
|
|
2008-05-13 08:35:55 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2008-11-11 17:29:03 +00:00
|
|
|
#include <config.h>
|
2008-05-13 08:35:55 +00:00
|
|
|
#endif
|
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
#include <string.h>
|
2008-05-13 08:35:55 +00:00
|
|
|
#include <gst/rtp/gstrtpbuffer.h>
|
2008-11-11 17:29:03 +00:00
|
|
|
#include <gst/base/gstadapter.h>
|
|
|
|
|
|
|
|
#include "gstrtpg729pay.h"
|
|
|
|
|
2009-09-16 17:57:05 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (rtpg729pay_debug);
|
|
|
|
#define GST_CAT_DEFAULT (rtpg729pay_debug)
|
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
#define G729_FRAME_SIZE 10
|
|
|
|
#define G729B_CN_FRAME_SIZE 2
|
|
|
|
#define G729_FRAME_DURATION (10 * GST_MSECOND)
|
|
|
|
#define G729_FRAME_DURATION_MS (10)
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_rtp_g729_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps);
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_rtp_g729_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buf);
|
2008-05-13 08:35:55 +00:00
|
|
|
|
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
static const GstElementDetails gst_rtp_g729_pay_details =
|
2008-11-25 18:03:02 +00:00
|
|
|
GST_ELEMENT_DETAILS ("RTP G.729 payloader",
|
2008-11-11 17:29:03 +00:00
|
|
|
"Codec/Payloader/Network",
|
2008-11-25 18:03:02 +00:00
|
|
|
"Packetize G.729 audio into RTP packets",
|
2008-11-11 17:29:03 +00:00
|
|
|
"Olivier Crete <olivier.crete@collabora.co.uk>");
|
2008-05-13 08:35:55 +00:00
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
static GstStaticPadTemplate gst_rtp_g729_pay_sink_template =
|
2008-05-13 08:35:55 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2008-11-11 17:29:03 +00:00
|
|
|
GST_STATIC_CAPS ("audio/G729, " /* according to RFC 3555 */
|
|
|
|
"channels = (int) 1, " "rate = (int) 8000")
|
2008-05-13 08:35:55 +00:00
|
|
|
);
|
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
static GstStaticPadTemplate gst_rtp_g729_pay_src_template =
|
2008-05-13 08:35:55 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("application/x-rtp, "
|
|
|
|
"media = (string) \"audio\", "
|
2008-11-11 17:29:03 +00:00
|
|
|
"payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
|
|
|
|
"clock-rate = (int) 8000, "
|
|
|
|
"encoding-name = (string) \"G729\"; "
|
2008-05-13 08:35:55 +00:00
|
|
|
"application/x-rtp, "
|
|
|
|
"media = (string) \"audio\", "
|
2008-11-11 17:29:03 +00:00
|
|
|
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
|
|
|
|
"clock-rate = (int) 8000, " "encoding-name = (string) \"G729\"")
|
2008-05-13 08:35:55 +00:00
|
|
|
);
|
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
static void
|
|
|
|
gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass);
|
2008-05-13 08:35:55 +00:00
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
GST_BOILERPLATE (GstRTPG729Pay, gst_rtp_g729_pay, GstBaseRTPAudioPayload,
|
2008-05-13 08:35:55 +00:00
|
|
|
GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
|
|
|
|
|
|
|
|
static void
|
2008-11-11 17:29:03 +00:00
|
|
|
gst_rtp_g729_pay_base_init (gpointer klass)
|
2008-05-13 08:35:55 +00:00
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
2008-11-11 17:29:03 +00:00
|
|
|
gst_static_pad_template_get (&gst_rtp_g729_pay_sink_template));
|
2008-05-13 08:35:55 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
2008-11-11 17:29:03 +00:00
|
|
|
gst_static_pad_template_get (&gst_rtp_g729_pay_src_template));
|
|
|
|
gst_element_class_set_details (element_class, &gst_rtp_g729_pay_details);
|
2009-09-16 17:57:05 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (rtpg729pay_debug, "rtpg729pay", 0,
|
|
|
|
"G.729 RTP Payloader");
|
2008-05-13 08:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-11-11 17:29:03 +00:00
|
|
|
gst_rtp_g729_pay_class_init (GstRTPG729PayClass * klass)
|
2008-05-13 08:35:55 +00:00
|
|
|
{
|
2008-11-11 17:29:03 +00:00
|
|
|
GstBaseRTPPayloadClass *payload_class = GST_BASE_RTP_PAYLOAD_CLASS (klass);
|
2008-05-13 08:35:55 +00:00
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
payload_class->set_caps = gst_rtp_g729_pay_set_caps;
|
|
|
|
payload_class->handle_buffer = gst_rtp_g729_pay_handle_buffer;
|
2008-05-13 08:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-11-11 17:29:03 +00:00
|
|
|
gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass)
|
2008-05-13 08:35:55 +00:00
|
|
|
{
|
2008-11-11 17:29:03 +00:00
|
|
|
GstBaseRTPPayload *payload = GST_BASE_RTP_PAYLOAD (pay);
|
|
|
|
GstBaseRTPAudioPayload *audiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (pay);
|
2008-05-13 08:35:55 +00:00
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
payload->pt = GST_RTP_PAYLOAD_G729;
|
|
|
|
gst_basertppayload_set_options (payload, "audio", FALSE, "G729", 8000);
|
2008-05-13 08:35:55 +00:00
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
gst_base_rtp_audio_payload_set_frame_based (audiopayload);
|
|
|
|
gst_base_rtp_audio_payload_set_frame_options (audiopayload,
|
|
|
|
G729_FRAME_DURATION_MS, G729_FRAME_SIZE);
|
2008-05-13 08:35:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2008-11-11 17:29:03 +00:00
|
|
|
gst_rtp_g729_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
|
2008-05-13 08:35:55 +00:00
|
|
|
{
|
2008-11-11 17:33:00 +00:00
|
|
|
gboolean res;
|
2008-05-13 08:35:55 +00:00
|
|
|
GstStructure *structure;
|
2008-11-11 17:29:03 +00:00
|
|
|
gint pt;
|
2008-05-13 08:35:55 +00:00
|
|
|
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
2008-11-11 17:29:03 +00:00
|
|
|
if (!gst_structure_get_int (structure, "payload", &pt))
|
|
|
|
pt = GST_RTP_PAYLOAD_G729;
|
|
|
|
|
|
|
|
payload->pt = pt;
|
|
|
|
payload->dynamic = pt != GST_RTP_PAYLOAD_G729;
|
|
|
|
|
2008-11-11 17:33:00 +00:00
|
|
|
res = gst_basertppayload_set_outcaps (payload, NULL);
|
2008-11-11 17:29:03 +00:00
|
|
|
|
2008-11-11 17:33:00 +00:00
|
|
|
return res;
|
2008-11-11 17:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_rtp_g729_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstBaseRTPAudioPayload *basertpaudiopayload =
|
|
|
|
GST_BASE_RTP_AUDIO_PAYLOAD (payload);
|
|
|
|
GstAdapter *adapter = NULL;
|
|
|
|
guint payload_len;
|
|
|
|
guint available;
|
|
|
|
guint maxptime_octets = G_MAXUINT;
|
|
|
|
guint minptime_octets = 0;
|
|
|
|
guint min_payload_len;
|
|
|
|
guint max_payload_len;
|
|
|
|
|
|
|
|
available = GST_BUFFER_SIZE (buf);
|
2008-05-13 08:35:55 +00:00
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
if (available % G729_FRAME_SIZE != 0 &&
|
|
|
|
available % G729_FRAME_SIZE != G729B_CN_FRAME_SIZE)
|
|
|
|
goto invalid_size;
|
2008-05-13 08:35:55 +00:00
|
|
|
|
2008-11-11 17:29:03 +00:00
|
|
|
/* max number of bytes based on given ptime, has to be multiple of
|
|
|
|
* frame_duration */
|
|
|
|
if (payload->max_ptime != -1) {
|
2010-01-19 02:15:51 +00:00
|
|
|
guint ptime_ms = payload->max_ptime / GST_MSECOND;
|
2008-11-11 17:29:03 +00:00
|
|
|
|
|
|
|
maxptime_octets = G729_FRAME_SIZE *
|
|
|
|
(int) (ptime_ms / G729_FRAME_DURATION_MS);
|
|
|
|
|
|
|
|
if (maxptime_octets < G729_FRAME_SIZE) {
|
2008-11-12 16:37:06 +00:00
|
|
|
GST_WARNING_OBJECT (basertpaudiopayload, "Given ptime %" G_GINT64_FORMAT
|
|
|
|
" is smaller than minimum %d ns, overwriting to minimum",
|
2008-11-11 17:29:03 +00:00
|
|
|
payload->max_ptime, G729_FRAME_DURATION_MS);
|
|
|
|
maxptime_octets = G729_FRAME_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
max_payload_len = MIN (
|
|
|
|
/* MTU max */
|
|
|
|
(int) (gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU
|
|
|
|
(basertpaudiopayload), 0, 0) / G729_FRAME_SIZE) * G729_FRAME_SIZE,
|
|
|
|
/* ptime max */
|
|
|
|
maxptime_octets);
|
|
|
|
|
|
|
|
/* min number of bytes based on a given ptime, has to be a multiple
|
|
|
|
of frame duration */
|
|
|
|
{
|
2009-12-31 21:27:30 +00:00
|
|
|
guint64 min_ptime = payload->min_ptime;
|
2008-11-11 17:29:03 +00:00
|
|
|
|
2010-01-19 02:15:51 +00:00
|
|
|
min_ptime = min_ptime / GST_MSECOND;
|
2008-11-11 17:29:03 +00:00
|
|
|
minptime_octets = G729_FRAME_SIZE *
|
|
|
|
(int) (min_ptime / G729_FRAME_DURATION_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
min_payload_len = MAX (minptime_octets, G729_FRAME_SIZE);
|
|
|
|
|
|
|
|
if (min_payload_len > max_payload_len) {
|
|
|
|
min_payload_len = max_payload_len;
|
|
|
|
}
|
|
|
|
|
2009-12-31 21:27:30 +00:00
|
|
|
/* If the ptime is specified in the caps, tried to adhere to it exactly */
|
|
|
|
if (payload->abidata.ABI.ptime) {
|
2010-01-19 02:15:51 +00:00
|
|
|
guint64 ptime = payload->abidata.ABI.ptime / GST_MSECOND;
|
2009-12-31 21:27:30 +00:00
|
|
|
guint ptime_in_bytes = G729_FRAME_SIZE *
|
2010-01-19 02:15:51 +00:00
|
|
|
(guint) (ptime / G729_FRAME_DURATION_MS);
|
2009-12-31 21:27:30 +00:00
|
|
|
|
|
|
|
/* clip to computed min and max lengths */
|
|
|
|
ptime_in_bytes = MAX (min_payload_len, ptime_in_bytes);
|
|
|
|
ptime_in_bytes = MIN (max_payload_len, ptime_in_bytes);
|
|
|
|
|
|
|
|
min_payload_len = max_payload_len = ptime_in_bytes;
|
|
|
|
}
|
|
|
|
|
2009-09-16 18:23:24 +00:00
|
|
|
GST_LOG_OBJECT (basertpaudiopayload,
|
2008-11-11 17:29:03 +00:00
|
|
|
"Calculated min_payload_len %u and max_payload_len %u",
|
|
|
|
min_payload_len, max_payload_len);
|
|
|
|
|
|
|
|
adapter = gst_base_rtp_audio_payload_get_adapter (basertpaudiopayload);
|
|
|
|
|
|
|
|
|
2009-12-31 21:52:30 +00:00
|
|
|
/* let's reset the base timestamp when the adapter is empty */
|
|
|
|
if (gst_adapter_available (adapter) == 0)
|
|
|
|
basertpaudiopayload->base_ts = GST_BUFFER_TIMESTAMP (buf);
|
2008-11-11 17:29:03 +00:00
|
|
|
|
2009-12-31 21:52:30 +00:00
|
|
|
if (gst_adapter_available (adapter) == 0 &&
|
|
|
|
GST_BUFFER_SIZE (buf) >= min_payload_len &&
|
|
|
|
GST_BUFFER_SIZE (buf) <= max_payload_len) {
|
|
|
|
ret = gst_base_rtp_audio_payload_push (basertpaudiopayload,
|
|
|
|
GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf),
|
|
|
|
GST_BUFFER_TIMESTAMP (buf));
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
g_object_unref (adapter);
|
|
|
|
return ret;
|
2008-11-11 17:29:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-31 21:52:30 +00:00
|
|
|
gst_adapter_push (adapter, buf);
|
|
|
|
|
|
|
|
available = gst_adapter_available (adapter);
|
2008-11-11 17:29:03 +00:00
|
|
|
/* as long as we have full frames */
|
|
|
|
/* this loop will push all available buffers till the last frame */
|
|
|
|
while (available >= min_payload_len ||
|
|
|
|
available % G729_FRAME_SIZE == G729B_CN_FRAME_SIZE) {
|
|
|
|
guint num;
|
|
|
|
|
|
|
|
/* We send as much as we can */
|
|
|
|
if (available <= max_payload_len) {
|
|
|
|
payload_len = available;
|
|
|
|
} else {
|
|
|
|
payload_len = MIN (max_payload_len,
|
|
|
|
(available / G729_FRAME_SIZE) * G729_FRAME_SIZE);
|
|
|
|
}
|
|
|
|
|
2009-12-31 21:52:30 +00:00
|
|
|
ret = gst_base_rtp_audio_payload_flush (basertpaudiopayload, payload_len,
|
|
|
|
basertpaudiopayload->base_ts);
|
2008-11-11 17:29:03 +00:00
|
|
|
|
|
|
|
num = payload_len / G729_FRAME_SIZE;
|
|
|
|
basertpaudiopayload->base_ts += G729_FRAME_DURATION * num;
|
|
|
|
|
2009-12-31 21:52:30 +00:00
|
|
|
available = gst_adapter_available (adapter);
|
2008-11-11 17:29:03 +00:00
|
|
|
}
|
|
|
|
|
2009-12-31 21:52:30 +00:00
|
|
|
g_object_unref (adapter);
|
2008-05-13 08:35:55 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* ERRORS */
|
2008-11-11 17:29:03 +00:00
|
|
|
invalid_size:
|
2008-05-13 08:35:55 +00:00
|
|
|
{
|
2008-11-11 17:29:03 +00:00
|
|
|
GST_ELEMENT_ERROR (payload, STREAM, WRONG_TYPE,
|
|
|
|
("Invalid input buffer size"),
|
|
|
|
("Invalid buffer size, should be a multiple of"
|
|
|
|
" G729_FRAME_SIZE(10) with an optional G729B_CN_FRAME_SIZE(2)"
|
|
|
|
" added to it, but it is %u", available));
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return GST_FLOW_ERROR;
|
2008-05-13 08:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_rtp_g729_pay_plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
return gst_element_register (plugin, "rtpg729pay",
|
|
|
|
GST_RANK_NONE, GST_TYPE_RTP_G729_PAY);
|
|
|
|
}
|