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);
51 static GstStateChangeReturn
52 gst_rtp_g729_pay_change_state (GstElement * element, GstStateChange transition);
54 static GstStaticPadTemplate gst_rtp_g729_pay_sink_template =
55 GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_STATIC_CAPS ("audio/G729, " /* according to RFC 3555 */
59 "channels = (int) 1, " "rate = (int) 8000")
62 static GstStaticPadTemplate gst_rtp_g729_pay_src_template =
63 GST_STATIC_PAD_TEMPLATE ("src",
66 GST_STATIC_CAPS ("application/x-rtp, "
67 "media = (string) \"audio\", "
68 "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
69 "clock-rate = (int) 8000, "
70 "encoding-name = (string) \"G729\"; "
72 "media = (string) \"audio\", "
73 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
74 "clock-rate = (int) 8000, " "encoding-name = (string) \"G729\"")
77 GST_BOILERPLATE (GstRTPG729Pay, gst_rtp_g729_pay, GstBaseRTPPayload,
78 GST_TYPE_BASE_RTP_PAYLOAD);
81 gst_rtp_g729_pay_base_init (gpointer klass)
83 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
85 gst_element_class_add_pad_template (element_class,
86 gst_static_pad_template_get (&gst_rtp_g729_pay_sink_template));
87 gst_element_class_add_pad_template (element_class,
88 gst_static_pad_template_get (&gst_rtp_g729_pay_src_template));
89 gst_element_class_set_details_simple (element_class, "RTP G.729 payloader",
90 "Codec/Payloader/Network",
91 "Packetize G.729 audio into RTP packets",
92 "Olivier Crete <olivier.crete@collabora.co.uk>");
94 GST_DEBUG_CATEGORY_INIT (rtpg729pay_debug, "rtpg729pay", 0,
95 "G.729 RTP Payloader");
99 gst_rtp_g729_pay_finalize (GObject * object)
101 GstRTPG729Pay *pay = GST_RTP_G729_PAY (object);
103 g_object_unref (pay->adapter);
105 G_OBJECT_CLASS (parent_class)->finalize (object);
109 gst_rtp_g729_pay_class_init (GstRTPG729PayClass * klass)
111 GObjectClass *gobject_class = (GObjectClass *) klass;
112 GstElementClass *gstelement_class = (GstElementClass *) klass;
113 GstBaseRTPPayloadClass *payload_class = GST_BASE_RTP_PAYLOAD_CLASS (klass);
115 gobject_class->finalize = gst_rtp_g729_pay_finalize;
117 gstelement_class->change_state = gst_rtp_g729_pay_change_state;
119 payload_class->set_caps = gst_rtp_g729_pay_set_caps;
120 payload_class->handle_buffer = gst_rtp_g729_pay_handle_buffer;
124 gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass)
126 GstBaseRTPPayload *payload = GST_BASE_RTP_PAYLOAD (pay);
128 payload->pt = GST_RTP_PAYLOAD_G729;
129 gst_basertppayload_set_options (payload, "audio", FALSE, "G729", 8000);
131 pay->adapter = gst_adapter_new ();
135 gst_rtp_g729_pay_reset (GstRTPG729Pay * pay)
137 gst_adapter_clear (pay->adapter);
138 pay->discont = FALSE;
142 gst_rtp_g729_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
145 GstStructure *structure;
148 structure = gst_caps_get_structure (caps, 0);
149 if (!gst_structure_get_int (structure, "payload", &pt))
150 pt = GST_RTP_PAYLOAD_G729;
153 payload->dynamic = pt != GST_RTP_PAYLOAD_G729;
155 res = gst_basertppayload_set_outcaps (payload, NULL);
161 gst_rtp_g729_pay_push (GstRTPG729Pay * rtpg729pay,
162 const guint8 * data, guint payload_len, GstClockTime timestamp,
163 GstClockTime duration)
165 GstBaseRTPPayload *basepayload;
170 basepayload = GST_BASE_RTP_PAYLOAD (rtpg729pay);
172 GST_DEBUG_OBJECT (rtpg729pay, "Pushing %d bytes ts %" GST_TIME_FORMAT,
173 payload_len, GST_TIME_ARGS (timestamp));
175 /* create buffer to hold the payload */
176 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
179 payload = gst_rtp_buffer_get_payload (outbuf);
180 memcpy (payload, data, payload_len);
183 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
184 GST_BUFFER_DURATION (outbuf) = duration;
186 if (G_UNLIKELY (rtpg729pay->discont)) {
187 GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
188 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
189 gst_rtp_buffer_set_marker (outbuf, TRUE);
190 rtpg729pay->discont = FALSE;
193 ret = gst_basertppayload_push (basepayload, outbuf);
199 gst_rtp_g729_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buf)
201 GstFlowReturn ret = GST_FLOW_OK;
202 GstRTPG729Pay *rtpg729pay = GST_RTP_G729_PAY (payload);
203 GstAdapter *adapter = NULL;
206 guint maxptime_octets = G_MAXUINT;
207 guint minptime_octets = 0;
208 guint min_payload_len;
209 guint max_payload_len;
211 available = GST_BUFFER_SIZE (buf);
213 if (available % G729_FRAME_SIZE != 0 &&
214 available % G729_FRAME_SIZE != G729B_CN_FRAME_SIZE)
217 /* max number of bytes based on given ptime, has to be multiple of
219 if (payload->max_ptime != -1) {
220 guint ptime_ms = payload->max_ptime / GST_MSECOND;
222 maxptime_octets = G729_FRAME_SIZE *
223 (int) (ptime_ms / G729_FRAME_DURATION_MS);
225 if (maxptime_octets < G729_FRAME_SIZE) {
226 GST_WARNING_OBJECT (payload, "Given ptime %" G_GINT64_FORMAT
227 " is smaller than minimum %d ns, overwriting to minimum",
228 payload->max_ptime, G729_FRAME_DURATION_MS);
229 maxptime_octets = G729_FRAME_SIZE;
233 max_payload_len = MIN (
235 (int) (gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU
236 (payload), 0, 0) / G729_FRAME_SIZE)
241 /* min number of bytes based on a given ptime, has to be a multiple
244 guint64 min_ptime = payload->min_ptime;
246 min_ptime = min_ptime / GST_MSECOND;
247 minptime_octets = G729_FRAME_SIZE *
248 (int) (min_ptime / G729_FRAME_DURATION_MS);
251 min_payload_len = MAX (minptime_octets, G729_FRAME_SIZE);
253 if (min_payload_len > max_payload_len) {
254 min_payload_len = max_payload_len;
257 /* If the ptime is specified in the caps, tried to adhere to it exactly */
258 if (payload->abidata.ABI.ptime) {
259 guint64 ptime = payload->abidata.ABI.ptime / GST_MSECOND;
260 guint ptime_in_bytes = G729_FRAME_SIZE *
261 (guint) (ptime / G729_FRAME_DURATION_MS);
263 /* clip to computed min and max lengths */
264 ptime_in_bytes = MAX (min_payload_len, ptime_in_bytes);
265 ptime_in_bytes = MIN (max_payload_len, ptime_in_bytes);
267 min_payload_len = max_payload_len = ptime_in_bytes;
270 GST_LOG_OBJECT (payload,
271 "Calculated min_payload_len %u and max_payload_len %u",
272 min_payload_len, max_payload_len);
274 if (GST_BUFFER_IS_DISCONT (buf))
275 rtpg729pay->discont = TRUE;
277 adapter = rtpg729pay->adapter;
279 /* let's reset the base timestamp when the adapter is empty */
280 if (gst_adapter_available (adapter) == 0)
281 rtpg729pay->next_ts = GST_BUFFER_TIMESTAMP (buf);
283 if (gst_adapter_available (adapter) == 0 &&
284 GST_BUFFER_SIZE (buf) >= min_payload_len &&
285 GST_BUFFER_SIZE (buf) <= max_payload_len) {
286 ret = gst_rtp_g729_pay_push (rtpg729pay,
287 GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf),
288 GST_BUFFER_TIMESTAMP (buf), GST_BUFFER_DURATION (buf));
289 gst_buffer_unref (buf);
293 gst_adapter_push (adapter, buf);
295 available = gst_adapter_available (adapter);
296 /* as long as we have full frames */
297 /* this loop will push all available buffers till the last frame */
298 while (available >= min_payload_len ||
299 available % G729_FRAME_SIZE == G729B_CN_FRAME_SIZE) {
300 GstClockTime duration;
302 /* We send as much as we can */
303 if (available <= max_payload_len) {
304 payload_len = available;
306 payload_len = MIN (max_payload_len,
307 (available / G729_FRAME_SIZE) * G729_FRAME_SIZE);
310 duration = (payload_len / G729_FRAME_SIZE) * G729_FRAME_DURATION;
311 rtpg729pay->next_ts += duration;
313 ret = gst_rtp_g729_pay_push (rtpg729pay,
314 gst_adapter_take (adapter, payload_len), payload_len,
315 rtpg729pay->next_ts, duration);
317 available = gst_adapter_available (adapter);
325 GST_ELEMENT_ERROR (payload, STREAM, WRONG_TYPE,
326 ("Invalid input buffer size"),
327 ("Invalid buffer size, should be a multiple of"
328 " G729_FRAME_SIZE(10) with an optional G729B_CN_FRAME_SIZE(2)"
329 " added to it, but it is %u", available));
330 gst_buffer_unref (buf);
331 return GST_FLOW_ERROR;
335 static GstStateChangeReturn
336 gst_rtp_g729_pay_change_state (GstElement * element, GstStateChange transition)
338 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
340 /* handle upwards state changes here */
341 switch (transition) {
346 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
348 /* handle downwards state changes */
349 switch (transition) {
350 case GST_STATE_CHANGE_PAUSED_TO_READY:
351 gst_rtp_g729_pay_reset (GST_RTP_G729_PAY (element));
361 gst_rtp_g729_pay_plugin_init (GstPlugin * plugin)
363 return gst_element_register (plugin, "rtpg729pay",
364 GST_RANK_NONE, GST_TYPE_RTP_G729_PAY);