2 * Copyright (C) <2007> Nokia Corporation
3 * Copyright (C) <2007> Collabora Ltd
4 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * This payloader assumes that the data will ALWAYS come as zero or more
24 * 10 bytes frame of audio followed by 0 or 1 2 byte frame of silence.
25 * Any other buffer format won't work
33 #include <gst/rtp/gstrtpbuffer.h>
34 #include <gst/base/gstadapter.h>
36 #include "gstrtpg729pay.h"
38 GST_DEBUG_CATEGORY_STATIC (rtpg729pay_debug);
39 #define GST_CAT_DEFAULT (rtpg729pay_debug)
41 #define G729_FRAME_SIZE 10
42 #define G729B_CN_FRAME_SIZE 2
43 #define G729_FRAME_DURATION (10 * GST_MSECOND)
44 #define G729_FRAME_DURATION_MS (10)
47 gst_rtp_g729_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps);
49 gst_rtp_g729_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buf);
52 static const GstElementDetails gst_rtp_g729_pay_details =
53 GST_ELEMENT_DETAILS ("RTP G.729 payloader",
54 "Codec/Payloader/Network",
55 "Packetize G.729 audio into RTP packets",
56 "Olivier Crete <olivier.crete@collabora.co.uk>");
58 static GstStaticPadTemplate gst_rtp_g729_pay_sink_template =
59 GST_STATIC_PAD_TEMPLATE ("sink",
62 GST_STATIC_CAPS ("audio/G729, " /* according to RFC 3555 */
63 "channels = (int) 1, " "rate = (int) 8000")
66 static GstStaticPadTemplate gst_rtp_g729_pay_src_template =
67 GST_STATIC_PAD_TEMPLATE ("src",
70 GST_STATIC_CAPS ("application/x-rtp, "
71 "media = (string) \"audio\", "
72 "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
73 "clock-rate = (int) 8000, "
74 "encoding-name = (string) \"G729\"; "
76 "media = (string) \"audio\", "
77 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
78 "clock-rate = (int) 8000, " "encoding-name = (string) \"G729\"")
82 gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass);
84 GST_BOILERPLATE (GstRTPG729Pay, gst_rtp_g729_pay, GstBaseRTPAudioPayload,
85 GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
88 gst_rtp_g729_pay_base_init (gpointer klass)
90 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
92 gst_element_class_add_pad_template (element_class,
93 gst_static_pad_template_get (&gst_rtp_g729_pay_sink_template));
94 gst_element_class_add_pad_template (element_class,
95 gst_static_pad_template_get (&gst_rtp_g729_pay_src_template));
96 gst_element_class_set_details (element_class, &gst_rtp_g729_pay_details);
98 GST_DEBUG_CATEGORY_INIT (rtpg729pay_debug, "rtpg729pay", 0,
99 "G.729 RTP Payloader");
103 gst_rtp_g729_pay_class_init (GstRTPG729PayClass * klass)
105 GstBaseRTPPayloadClass *payload_class = GST_BASE_RTP_PAYLOAD_CLASS (klass);
107 payload_class->set_caps = gst_rtp_g729_pay_set_caps;
108 payload_class->handle_buffer = gst_rtp_g729_pay_handle_buffer;
112 gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass)
114 GstBaseRTPPayload *payload = GST_BASE_RTP_PAYLOAD (pay);
115 GstBaseRTPAudioPayload *audiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (pay);
117 payload->pt = GST_RTP_PAYLOAD_G729;
118 gst_basertppayload_set_options (payload, "audio", FALSE, "G729", 8000);
120 gst_base_rtp_audio_payload_set_frame_based (audiopayload);
121 gst_base_rtp_audio_payload_set_frame_options (audiopayload,
122 G729_FRAME_DURATION_MS, G729_FRAME_SIZE);
127 gst_rtp_g729_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
130 GstStructure *structure;
133 structure = gst_caps_get_structure (caps, 0);
134 if (!gst_structure_get_int (structure, "payload", &pt))
135 pt = GST_RTP_PAYLOAD_G729;
138 payload->dynamic = pt != GST_RTP_PAYLOAD_G729;
140 res = gst_basertppayload_set_outcaps (payload, NULL);
146 gst_rtp_g729_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buf)
148 GstFlowReturn ret = GST_FLOW_OK;
149 GstBaseRTPAudioPayload *basertpaudiopayload =
150 GST_BASE_RTP_AUDIO_PAYLOAD (payload);
151 GstAdapter *adapter = NULL;
153 const guint8 *data = NULL;
155 guint maxptime_octets = G_MAXUINT;
156 guint minptime_octets = 0;
157 guint min_payload_len;
158 guint max_payload_len;
159 gboolean use_adapter = FALSE;
161 available = GST_BUFFER_SIZE (buf);
163 if (available % G729_FRAME_SIZE != 0 &&
164 available % G729_FRAME_SIZE != G729B_CN_FRAME_SIZE)
167 /* max number of bytes based on given ptime, has to be multiple of
169 if (payload->max_ptime != -1) {
170 guint ptime_ms = payload->max_ptime / 1000000;
172 maxptime_octets = G729_FRAME_SIZE *
173 (int) (ptime_ms / G729_FRAME_DURATION_MS);
175 if (maxptime_octets < G729_FRAME_SIZE) {
176 GST_WARNING_OBJECT (basertpaudiopayload, "Given ptime %" G_GINT64_FORMAT
177 " is smaller than minimum %d ns, overwriting to minimum",
178 payload->max_ptime, G729_FRAME_DURATION_MS);
179 maxptime_octets = G729_FRAME_SIZE;
183 max_payload_len = MIN (
185 (int) (gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU
186 (basertpaudiopayload), 0, 0) / G729_FRAME_SIZE) * G729_FRAME_SIZE,
190 /* min number of bytes based on a given ptime, has to be a multiple
193 guint64 min_ptime = payload->min_ptime;
195 min_ptime = min_ptime / 1000000;
196 minptime_octets = G729_FRAME_SIZE *
197 (int) (min_ptime / G729_FRAME_DURATION_MS);
200 min_payload_len = MAX (minptime_octets, G729_FRAME_SIZE);
202 if (min_payload_len > max_payload_len) {
203 min_payload_len = max_payload_len;
206 /* If the ptime is specified in the caps, tried to adhere to it exactly */
207 if (payload->abidata.ABI.ptime) {
208 guint ptime_in_bytes = G729_FRAME_SIZE *
209 (guint) (payload->abidata.ABI.ptime / G729_FRAME_DURATION_MS);
211 /* clip to computed min and max lengths */
212 ptime_in_bytes = MAX (min_payload_len, ptime_in_bytes);
213 ptime_in_bytes = MIN (max_payload_len, ptime_in_bytes);
215 min_payload_len = max_payload_len = ptime_in_bytes;
218 GST_LOG_OBJECT (basertpaudiopayload,
219 "Calculated min_payload_len %u and max_payload_len %u",
220 min_payload_len, max_payload_len);
222 adapter = gst_base_rtp_audio_payload_get_adapter (basertpaudiopayload);
224 if (adapter && gst_adapter_available (adapter)) {
225 /* If there is always data in the adapter, we have to use it */
226 gst_adapter_push (adapter, buf);
227 available = gst_adapter_available (adapter);
230 /* let's set the base timestamp */
231 basertpaudiopayload->base_ts = GST_BUFFER_TIMESTAMP (buf);
233 /* If buffer fits on an RTP packet, let's just push it through */
234 /* this will check against max_ptime and max_mtu */
235 if (GST_BUFFER_SIZE (buf) >= min_payload_len &&
236 GST_BUFFER_SIZE (buf) <= max_payload_len) {
237 ret = gst_base_rtp_audio_payload_push (basertpaudiopayload,
238 GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf),
239 GST_BUFFER_TIMESTAMP (buf));
240 gst_buffer_unref (buf);
241 g_object_unref (adapter);
245 available = GST_BUFFER_SIZE (buf);
246 data = (guint8 *) GST_BUFFER_DATA (buf);
249 /* as long as we have full frames */
250 /* this loop will push all available buffers till the last frame */
251 while (available >= min_payload_len ||
252 available % G729_FRAME_SIZE == G729B_CN_FRAME_SIZE) {
255 /* We send as much as we can */
256 if (available <= max_payload_len) {
257 payload_len = available;
259 payload_len = MIN (max_payload_len,
260 (available / G729_FRAME_SIZE) * G729_FRAME_SIZE);
264 data = gst_adapter_peek (adapter, payload_len);
267 ret = gst_base_rtp_audio_payload_push (basertpaudiopayload, data,
268 payload_len, basertpaudiopayload->base_ts);
270 num = payload_len / G729_FRAME_SIZE;
271 basertpaudiopayload->base_ts += G729_FRAME_DURATION * num;
274 gst_adapter_flush (adapter, payload_len);
275 available = gst_adapter_available (adapter);
277 available -= payload_len;
283 if (available != 0 && adapter) {
285 buf2 = gst_buffer_create_sub (buf,
286 GST_BUFFER_SIZE (buf) - available, available);
287 gst_adapter_push (adapter, buf2);
289 gst_buffer_unref (buf);
293 g_object_unref (adapter);
301 GST_ELEMENT_ERROR (payload, STREAM, WRONG_TYPE,
302 ("Invalid input buffer size"),
303 ("Invalid buffer size, should be a multiple of"
304 " G729_FRAME_SIZE(10) with an optional G729B_CN_FRAME_SIZE(2)"
305 " added to it, but it is %u", available));
306 gst_buffer_unref (buf);
307 return GST_FLOW_ERROR;
312 gst_rtp_g729_pay_plugin_init (GstPlugin * plugin)
314 return gst_element_register (plugin, "rtpg729pay",
315 GST_RANK_NONE, GST_TYPE_RTP_G729_PAY);