gst/rtp/gstrtpg729pay.*: Replace G729 payloader with an improved version. Fixes #532409.

Original commit message from CVS:
Patch by: Olivier Crete <tester at tester dot ca>
* gst/rtp/gstrtpg729pay.c: (gst_rtp_g729_pay_base_init),
(gst_rtp_g729_pay_class_init), (gst_rtp_g729_pay_init),
(gst_rtp_g729_pay_set_caps), (gst_rtp_g729_pay_handle_buffer):
* gst/rtp/gstrtpg729pay.h:
Replace G729 payloader with an improved version. Fixes #532409.
This commit is contained in:
Olivier Crete 2008-11-11 17:29:03 +00:00 committed by Wim Taymans
parent 21edbcc566
commit 774f238b96
3 changed files with 242 additions and 74 deletions

View file

@ -1,3 +1,13 @@
2008-11-11 Wim Taymans <wim.taymans@collabora.co.uk>
Patch by: Olivier Crete <tester at tester dot ca>
* gst/rtp/gstrtpg729pay.c: (gst_rtp_g729_pay_base_init),
(gst_rtp_g729_pay_class_init), (gst_rtp_g729_pay_init),
(gst_rtp_g729_pay_set_caps), (gst_rtp_g729_pay_handle_buffer):
* gst/rtp/gstrtpg729pay.h:
Replace G729 payloader with an improved version. Fixes #532409.
2008-11-11 Wim Taymans <wim.taymans@collabora.co.uk> 2008-11-11 Wim Taymans <wim.taymans@collabora.co.uk>
* gst/rtsp/gstrtspsrc.c: (gst_rtspsrc_create_transports_string), * gst/rtsp/gstrtspsrc.c: (gst_rtspsrc_create_transports_string),

View file

@ -1,4 +1,7 @@
/* GStreamer /* 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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public * modify it under the terms of the GNU Library General Public
@ -16,129 +19,282 @@
* Boston, MA 02111-1307, USA. * Boston, MA 02111-1307, USA.
*/ */
/*
* 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
*/
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include <config.h>
#endif #endif
#include "gstrtpg729pay.h" #include <string.h>
#include <gst/rtp/gstrtpbuffer.h> #include <gst/rtp/gstrtpbuffer.h>
#include <gst/base/gstadapter.h>
/* elementfactory information */ #include "gstrtpg729pay.h"
static GstElementDetails gst_rtpg729pay_details = {
"RTP Payloader for G729 Audio",
"Codec/Payloader/Network",
"Packetize G729 audio streams into RTP packets",
"Laurent Glayal <spglegle@yahoo.fr>"
};
GST_DEBUG_CATEGORY_STATIC (rtpg729pay_debug); /* TODO: fix gstrtpbuffer.h */
#define GST_CAT_DEFAULT (rtpg729pay_debug) #undef GST_RTP_PAYLOAD_G729
#define GST_RTP_PAYLOAD_G729 18
#undef GST_RTP_PAYLOAD_G729_STRING
#define GST_RTP_PAYLOAD_G729_STRING "18"
static GstStaticPadTemplate gst_rtpg729pay_sink_template = #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);
static const GstElementDetails gst_rtp_g729_pay_details =
GST_ELEMENT_DETAILS ("G729 RTP packet payloader",
"Codec/Payloader/Network",
"Packetize G729 audio into RTP packets",
"Olivier Crete <olivier.crete@collabora.co.uk>");
static GstStaticPadTemplate gst_rtp_g729_pay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink", GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK, GST_PAD_SINK,
GST_PAD_ALWAYS, GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/G729, channels=(int)1, rate=(int)8000") GST_STATIC_CAPS ("audio/G729, " /* according to RFC 3555 */
"channels = (int) 1, " "rate = (int) 8000")
); );
static GstStaticPadTemplate gst_rtpg729pay_src_template = static GstStaticPadTemplate gst_rtp_g729_pay_src_template =
GST_STATIC_PAD_TEMPLATE ("src", GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC, GST_PAD_SRC,
GST_PAD_ALWAYS, GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-rtp, " GST_STATIC_CAPS ("application/x-rtp, "
"media = (string) \"audio\", " "media = (string) \"audio\", "
"payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", " "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
"clock-rate = (int) 8000, " "encoding-name = (string) \"G729\";" "clock-rate = (int) 8000, "
"encoding-name = (string) \"G729\"; "
"application/x-rtp, " "application/x-rtp, "
"media = (string) \"audio\", " "media = (string) \"audio\", "
"payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", " "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
"clock-rate = (int) 8000") "clock-rate = (int) 8000, " "encoding-name = (string) \"G729\"")
); );
static gboolean gst_rtpg729pay_setcaps (GstBaseRTPPayload * payload, static void
GstCaps * caps); gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass);
GST_BOILERPLATE (GstRtpG729Pay, gst_rtpg729pay, GstBaseRTPAudioPayload, GST_BOILERPLATE (GstRTPG729Pay, gst_rtp_g729_pay, GstBaseRTPAudioPayload,
GST_TYPE_BASE_RTP_AUDIO_PAYLOAD); GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
static void static void
gst_rtpg729pay_base_init (gpointer klass) gst_rtp_g729_pay_base_init (gpointer klass)
{ {
GstElementClass *element_class = GST_ELEMENT_CLASS (klass); GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
gst_element_class_add_pad_template (element_class, gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtpg729pay_sink_template)); gst_static_pad_template_get (&gst_rtp_g729_pay_sink_template));
gst_element_class_add_pad_template (element_class, gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&gst_rtpg729pay_src_template)); gst_static_pad_template_get (&gst_rtp_g729_pay_src_template));
gst_element_class_set_details (element_class, &gst_rtpg729pay_details); gst_element_class_set_details (element_class, &gst_rtp_g729_pay_details);
} }
static void static void
gst_rtpg729pay_class_init (GstRtpG729PayClass * klass) gst_rtp_g729_pay_class_init (GstRTPG729PayClass * klass)
{ {
GObjectClass *gobject_class; GstBaseRTPPayloadClass *payload_class = GST_BASE_RTP_PAYLOAD_CLASS (klass);
GstElementClass *gstelement_class;
GstBaseRTPPayloadClass *gstbasertppayload_class;
gobject_class = (GObjectClass *) klass; payload_class->set_caps = gst_rtp_g729_pay_set_caps;
gstelement_class = (GstElementClass *) klass; payload_class->handle_buffer = gst_rtp_g729_pay_handle_buffer;
gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
parent_class = g_type_class_ref (GST_TYPE_BASE_RTP_PAYLOAD);
gstbasertppayload_class->set_caps = gst_rtpg729pay_setcaps;
GST_DEBUG_CATEGORY_INIT (rtpg729pay_debug, "rtpg729pay", 0,
"G729 audio RTP payloader");
} }
static void static void
gst_rtpg729pay_init (GstRtpG729Pay * rtpg729pay, GstRtpG729PayClass * klass) gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass)
{ {
GstBaseRTPPayload *basertppayload; GstBaseRTPPayload *payload = GST_BASE_RTP_PAYLOAD (pay);
GstBaseRTPAudioPayload *basertpaudiopayload; GstBaseRTPAudioPayload *audiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (pay);
basertppayload = GST_BASE_RTP_PAYLOAD (rtpg729pay); payload->pt = GST_RTP_PAYLOAD_G729;
basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpg729pay); gst_basertppayload_set_options (payload, "audio", FALSE, "G729", 8000);
/* we don't set the payload type, it should be set by the application using gst_base_rtp_audio_payload_set_frame_based (audiopayload);
* the pt property or the default 96 will be used */ gst_base_rtp_audio_payload_set_frame_options (audiopayload,
basertppayload->clock_rate = 8000; G729_FRAME_DURATION_MS, G729_FRAME_SIZE);
/* tell basertpaudiopayload that this is a frame based codec */
gst_base_rtp_audio_payload_set_frame_based (basertpaudiopayload);
gst_basertppayload_set_options (basertppayload, "audio", FALSE, "G729", 8000);
gst_base_rtp_audio_payload_set_frame_options (basertpaudiopayload, 10, 10);
} }
static gboolean static gboolean
gst_rtpg729pay_setcaps (GstBaseRTPPayload * basertppayload, GstCaps * caps) gst_rtp_g729_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
{ {
GstRtpG729Pay *rtpg729pay;
GstBaseRTPAudioPayload *basertpaudiopayload;
gboolean ret;
GstStructure *structure; GstStructure *structure;
const char *payload_name; gint pt;
rtpg729pay = GST_RTP_G729_PAY (basertppayload);
basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basertppayload);
structure = gst_caps_get_structure (caps, 0); structure = gst_caps_get_structure (caps, 0);
if (!gst_structure_get_int (structure, "payload", &pt))
pt = GST_RTP_PAYLOAD_G729;
payload_name = gst_structure_get_name (structure); payload->pt = pt;
if (g_strcasecmp ("audio/G729", payload_name) != 0) payload->dynamic = pt != GST_RTP_PAYLOAD_G729;
goto wrong_name;
ret = gst_basertppayload_set_outcaps (basertppayload, NULL); gst_basertppayload_set_outcaps (payload, NULL);
return TRUE;
}
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;
const guint8 *data = NULL;
guint available;
guint maxptime_octets = G_MAXUINT;
guint minptime_octets = 0;
guint min_payload_len;
guint max_payload_len;
gboolean use_adapter = FALSE;
available = GST_BUFFER_SIZE (buf);
if (available % G729_FRAME_SIZE != 0 &&
available % G729_FRAME_SIZE != G729B_CN_FRAME_SIZE)
goto invalid_size;
/* max number of bytes based on given ptime, has to be multiple of
* frame_duration */
if (payload->max_ptime != -1) {
guint ptime_ms = payload->max_ptime / 1000000;
maxptime_octets = G729_FRAME_SIZE *
(int) (ptime_ms / G729_FRAME_DURATION_MS);
if (maxptime_octets < G729_FRAME_SIZE) {
GST_WARNING_OBJECT (basertpaudiopayload, "Given ptime %d is smaller than"
" minimum %d ns, overwriting to minimum",
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 */
{
guint64 min_ptime;
g_object_get (G_OBJECT (payload), "min-ptime", &min_ptime, NULL);
min_ptime = min_ptime / 1000000;
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;
}
GST_DEBUG_OBJECT (basertpaudiopayload,
"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);
if (adapter && gst_adapter_available (adapter)) {
/* If there is always data in the adapter, we have to use it */
gst_adapter_push (adapter, buf);
available = gst_adapter_available (adapter);
use_adapter = TRUE;
} else {
/* let's set the base timestamp */
basertpaudiopayload->base_ts = GST_BUFFER_TIMESTAMP (buf);
/* If buffer fits on an RTP packet, let's just push it through */
/* this will check against max_ptime and max_mtu */
if (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);
return ret;
}
available = GST_BUFFER_SIZE (buf);
data = (guint8 *) GST_BUFFER_DATA (buf);
}
/* 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);
}
if (use_adapter) {
data = gst_adapter_peek (adapter, payload_len);
}
ret = gst_base_rtp_audio_payload_push (basertpaudiopayload, data,
payload_len, basertpaudiopayload->base_ts);
num = payload_len / G729_FRAME_SIZE;
basertpaudiopayload->base_ts += G729_FRAME_DURATION * num;
if (use_adapter) {
gst_adapter_flush (adapter, payload_len);
available = gst_adapter_available (adapter);
} else {
available -= payload_len;
data += payload_len;
}
}
if (!use_adapter) {
if (available != 0 && adapter) {
GstBuffer *buf2;
buf2 = gst_buffer_create_sub (buf,
GST_BUFFER_SIZE (buf) - available, available);
gst_adapter_push (adapter, buf2);
} else {
gst_buffer_unref (buf);
}
}
if (adapter) {
g_object_unref (adapter);
}
return ret; return ret;
/* ERRORS */ /* ERRORS */
wrong_name: invalid_size:
{ {
GST_ERROR_OBJECT (rtpg729pay, "wrong name, expected 'audio/G729', got '%s'", GST_ELEMENT_ERROR (payload, STREAM, WRONG_TYPE,
payload_name); ("Invalid input buffer size"),
return FALSE; ("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;
} }
} }

View file

@ -1,4 +1,6 @@
/* GStreamer /* GStreamer
* Copyright (C) <2007> Nokia Corporation
* Copyright (C) <2007> Collabora Ltd
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public * modify it under the terms of the GNU Library General Public
@ -25,25 +27,25 @@
G_BEGIN_DECLS G_BEGIN_DECLS
#define GST_TYPE_RTP_G729_PAY \ #define GST_TYPE_RTP_G729_PAY \
(gst_rtpg729pay_get_type()) (gst_rtp_g729_pay_get_type())
#define GST_RTP_G729_PAY(obj) \ #define GST_RTP_G729_PAY(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_G729_PAY,GstRtpG729Pay)) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_G729_PAY,GstRTPG729Pay))
#define GST_RTP_G729_PAY_CLASS(klass) \ #define GST_RTP_G729_PAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_G729_PAY,GstRtpG729PayClass)) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_G729_PAY,GstRTPG729PayClass))
#define GST_IS_RTP_G729_PAY(obj) \ #define GST_IS_RTP_G729_PAY(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_G729_PAY)) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_G729_PAY))
#define GST_IS_RTP_G729_PAY_CLASS(klass) \ #define GST_IS_RTP_G729_PAY_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_G729_PAY)) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_G729_PAY))
typedef struct _GstRtpG729Pay GstRtpG729Pay; typedef struct _GstRTPG729Pay GstRTPG729Pay;
typedef struct _GstRtpG729PayClass GstRtpG729PayClass; typedef struct _GstRTPG729PayClass GstRTPG729PayClass;
struct _GstRtpG729Pay struct _GstRTPG729Pay
{ {
GstBaseRTPAudioPayload audiopayload; GstBaseRTPAudioPayload audiopayload;
}; };
struct _GstRtpG729PayClass struct _GstRTPG729PayClass
{ {
GstBaseRTPAudioPayloadClass parent_class; GstBaseRTPAudioPayloadClass parent_class;
}; };