1 /* GStreamer
2 * Copyright (C) <2007> Nokia Corporation
3 * Copyright (C) <2007> Collabora Ltd
4 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /*
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
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <string.h>
33 #include <gst/rtp/gstrtpbuffer.h>
34 #include <gst/base/gstadapter.h>
35 #include <gst/audio/audio.h>
36
37 #include "gstrtpg729pay.h"
38 #include "gstrtputils.h"
39
40 GST_DEBUG_CATEGORY_STATIC (rtpg729pay_debug);
41 #define GST_CAT_DEFAULT (rtpg729pay_debug)
42
43 #define G729_FRAME_SIZE 10
44 #define G729B_CN_FRAME_SIZE 2
45 #define G729_FRAME_DURATION (10 * GST_MSECOND)
46 #define G729_FRAME_DURATION_MS (10)
47
48 static gboolean
49 gst_rtp_g729_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps);
50 static GstFlowReturn
51 gst_rtp_g729_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buf);
52
53 static GstStateChangeReturn
54 gst_rtp_g729_pay_change_state (GstElement * element, GstStateChange transition);
55
56 static GstStaticPadTemplate gst_rtp_g729_pay_sink_template =
57 GST_STATIC_PAD_TEMPLATE ("sink",
58 GST_PAD_SINK,
59 GST_PAD_ALWAYS,
60 GST_STATIC_CAPS ("audio/G729, " /* according to RFC 3555 */
61 "channels = (int) 1, " "rate = (int) 8000")
62 );
63
64 static GstStaticPadTemplate gst_rtp_g729_pay_src_template =
65 GST_STATIC_PAD_TEMPLATE ("src",
66 GST_PAD_SRC,
67 GST_PAD_ALWAYS,
68 GST_STATIC_CAPS ("application/x-rtp, "
69 "media = (string) \"audio\", "
70 "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
71 "clock-rate = (int) 8000, "
72 "encoding-name = (string) \"G729\"; "
73 "application/x-rtp, "
74 "media = (string) \"audio\", "
75 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
76 "clock-rate = (int) 8000, " "encoding-name = (string) \"G729\"")
77 );
78
79 #define gst_rtp_g729_pay_parent_class parent_class
80 G_DEFINE_TYPE (GstRTPG729Pay, gst_rtp_g729_pay, GST_TYPE_RTP_BASE_PAYLOAD);
81
82 static void
gst_rtp_g729_pay_finalize(GObject * object)83 gst_rtp_g729_pay_finalize (GObject * object)
84 {
85 GstRTPG729Pay *pay = GST_RTP_G729_PAY (object);
86
87 g_object_unref (pay->adapter);
88
89 G_OBJECT_CLASS (parent_class)->finalize (object);
90 }
91
92 static void
gst_rtp_g729_pay_class_init(GstRTPG729PayClass * klass)93 gst_rtp_g729_pay_class_init (GstRTPG729PayClass * klass)
94 {
95 GObjectClass *gobject_class = (GObjectClass *) klass;
96 GstElementClass *gstelement_class = (GstElementClass *) klass;
97 GstRTPBasePayloadClass *payload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
98
99 GST_DEBUG_CATEGORY_INIT (rtpg729pay_debug, "rtpg729pay", 0,
100 "G.729 RTP Payloader");
101
102 gobject_class->finalize = gst_rtp_g729_pay_finalize;
103
104 gstelement_class->change_state = gst_rtp_g729_pay_change_state;
105
106 gst_element_class_add_static_pad_template (gstelement_class,
107 &gst_rtp_g729_pay_sink_template);
108 gst_element_class_add_static_pad_template (gstelement_class,
109 &gst_rtp_g729_pay_src_template);
110
111 gst_element_class_set_static_metadata (gstelement_class,
112 "RTP G.729 payloader", "Codec/Payloader/Network/RTP",
113 "Packetize G.729 audio into RTP packets",
114 "Olivier Crete <olivier.crete@collabora.co.uk>");
115
116 payload_class->set_caps = gst_rtp_g729_pay_set_caps;
117 payload_class->handle_buffer = gst_rtp_g729_pay_handle_buffer;
118 }
119
120 static void
gst_rtp_g729_pay_init(GstRTPG729Pay * pay)121 gst_rtp_g729_pay_init (GstRTPG729Pay * pay)
122 {
123 GstRTPBasePayload *payload = GST_RTP_BASE_PAYLOAD (pay);
124
125 payload->pt = GST_RTP_PAYLOAD_G729;
126
127 pay->adapter = gst_adapter_new ();
128 }
129
130 static void
gst_rtp_g729_pay_reset(GstRTPG729Pay * pay)131 gst_rtp_g729_pay_reset (GstRTPG729Pay * pay)
132 {
133 gst_adapter_clear (pay->adapter);
134 pay->discont = FALSE;
135 pay->next_rtp_time = 0;
136 pay->first_ts = GST_CLOCK_TIME_NONE;
137 pay->first_rtp_time = 0;
138 }
139
140 static gboolean
gst_rtp_g729_pay_set_caps(GstRTPBasePayload * payload,GstCaps * caps)141 gst_rtp_g729_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
142 {
143 gboolean res;
144
145 gst_rtp_base_payload_set_options (payload, "audio",
146 payload->pt != GST_RTP_PAYLOAD_G729, "G729", 8000);
147
148 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
149
150 return res;
151 }
152
153 static GstFlowReturn
gst_rtp_g729_pay_push(GstRTPG729Pay * rtpg729pay,GstBuffer * buf)154 gst_rtp_g729_pay_push (GstRTPG729Pay * rtpg729pay, GstBuffer * buf)
155 {
156 GstRTPBasePayload *basepayload;
157 GstClockTime duration;
158 guint frames;
159 GstBuffer *outbuf;
160 GstFlowReturn ret;
161 GstRTPBuffer rtp = { NULL };
162 guint payload_len = gst_buffer_get_size (buf);
163
164 basepayload = GST_RTP_BASE_PAYLOAD (rtpg729pay);
165
166 GST_DEBUG_OBJECT (rtpg729pay, "Pushing %d bytes ts %" GST_TIME_FORMAT,
167 payload_len, GST_TIME_ARGS (rtpg729pay->next_ts));
168
169 /* create buffer to hold the payload */
170 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
171
172 gst_rtp_buffer_map (outbuf, GST_MAP_READWRITE, &rtp);
173
174 /* set metadata */
175 frames =
176 (payload_len / G729_FRAME_SIZE) + ((payload_len % G729_FRAME_SIZE) >> 1);
177 duration = frames * G729_FRAME_DURATION;
178 GST_BUFFER_PTS (outbuf) = rtpg729pay->next_ts;
179 GST_BUFFER_DURATION (outbuf) = duration;
180 GST_BUFFER_OFFSET (outbuf) = rtpg729pay->next_rtp_time;
181 rtpg729pay->next_ts += duration;
182 rtpg729pay->next_rtp_time += frames * 80;
183
184 if (G_UNLIKELY (rtpg729pay->discont)) {
185 GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
186 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
187 gst_rtp_buffer_set_marker (&rtp, TRUE);
188 rtpg729pay->discont = FALSE;
189 }
190 gst_rtp_buffer_unmap (&rtp);
191
192 /* append payload */
193 gst_rtp_copy_audio_meta (basepayload, outbuf, buf);
194 outbuf = gst_buffer_append (outbuf, buf);
195
196 ret = gst_rtp_base_payload_push (basepayload, outbuf);
197
198 return ret;
199 }
200
201 static void
gst_rtp_g729_pay_recalc_rtp_time(GstRTPG729Pay * rtpg729pay,GstClockTime time)202 gst_rtp_g729_pay_recalc_rtp_time (GstRTPG729Pay * rtpg729pay, GstClockTime time)
203 {
204 if (GST_CLOCK_TIME_IS_VALID (rtpg729pay->first_ts)
205 && GST_CLOCK_TIME_IS_VALID (time) && time >= rtpg729pay->first_ts) {
206 GstClockTime diff;
207 guint32 rtpdiff;
208
209 diff = time - rtpg729pay->first_ts;
210 rtpdiff = (diff / GST_MSECOND) * 8;
211 rtpg729pay->next_rtp_time = rtpg729pay->first_rtp_time + rtpdiff;
212 GST_DEBUG_OBJECT (rtpg729pay,
213 "elapsed time %" GST_TIME_FORMAT ", rtp %" G_GUINT32_FORMAT ", "
214 "new offset %" G_GUINT32_FORMAT, GST_TIME_ARGS (diff), rtpdiff,
215 rtpg729pay->next_rtp_time);
216 }
217 }
218
219 static GstFlowReturn
gst_rtp_g729_pay_handle_buffer(GstRTPBasePayload * payload,GstBuffer * buf)220 gst_rtp_g729_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buf)
221 {
222 GstFlowReturn ret = GST_FLOW_OK;
223 GstRTPG729Pay *rtpg729pay = GST_RTP_G729_PAY (payload);
224 GstAdapter *adapter = NULL;
225 guint payload_len;
226 guint available;
227 guint maxptime_octets = G_MAXUINT;
228 guint minptime_octets = 0;
229 guint min_payload_len;
230 guint max_payload_len;
231 gsize size;
232 GstClockTime timestamp;
233
234 size = gst_buffer_get_size (buf);
235
236 if (size % G729_FRAME_SIZE != 0 &&
237 size % G729_FRAME_SIZE != G729B_CN_FRAME_SIZE)
238 goto invalid_size;
239
240 /* max number of bytes based on given ptime, has to be multiple of
241 * frame_duration */
242 if (payload->max_ptime != -1) {
243 guint ptime_ms = payload->max_ptime / GST_MSECOND;
244
245 maxptime_octets = G729_FRAME_SIZE *
246 (int) (ptime_ms / G729_FRAME_DURATION_MS);
247
248 if (maxptime_octets < G729_FRAME_SIZE) {
249 GST_WARNING_OBJECT (payload, "Given ptime %" G_GINT64_FORMAT
250 " is smaller than minimum %d ns, overwriting to minimum",
251 payload->max_ptime, G729_FRAME_DURATION_MS);
252 maxptime_octets = G729_FRAME_SIZE;
253 }
254 }
255
256 max_payload_len = MIN (
257 /* MTU max */
258 (int) (gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU
259 (payload), 0, 0) / G729_FRAME_SIZE)
260 * G729_FRAME_SIZE,
261 /* ptime max */
262 maxptime_octets);
263
264 /* min number of bytes based on a given ptime, has to be a multiple
265 of frame duration */
266 {
267 guint64 min_ptime = payload->min_ptime;
268
269 min_ptime = min_ptime / GST_MSECOND;
270 minptime_octets = G729_FRAME_SIZE *
271 (int) (min_ptime / G729_FRAME_DURATION_MS);
272 }
273
274 min_payload_len = MAX (minptime_octets, G729_FRAME_SIZE);
275
276 if (min_payload_len > max_payload_len) {
277 min_payload_len = max_payload_len;
278 }
279
280 /* If the ptime is specified in the caps, tried to adhere to it exactly */
281 if (payload->ptime) {
282 guint64 ptime = payload->ptime / GST_MSECOND;
283 guint ptime_in_bytes = G729_FRAME_SIZE *
284 (guint) (ptime / G729_FRAME_DURATION_MS);
285
286 /* clip to computed min and max lengths */
287 ptime_in_bytes = MAX (min_payload_len, ptime_in_bytes);
288 ptime_in_bytes = MIN (max_payload_len, ptime_in_bytes);
289
290 min_payload_len = max_payload_len = ptime_in_bytes;
291 }
292
293 GST_LOG_OBJECT (payload,
294 "Calculated min_payload_len %u and max_payload_len %u",
295 min_payload_len, max_payload_len);
296
297 adapter = rtpg729pay->adapter;
298 available = gst_adapter_available (adapter);
299
300 timestamp = GST_BUFFER_PTS (buf);
301
302 /* resync rtp time on discont or a discontinuous cn packet */
303 if (GST_BUFFER_IS_DISCONT (buf)) {
304 /* flush remainder */
305 if (available > 0) {
306 gst_rtp_g729_pay_push (rtpg729pay,
307 gst_adapter_take_buffer_fast (adapter, available));
308 available = 0;
309 }
310 rtpg729pay->discont = TRUE;
311 gst_rtp_g729_pay_recalc_rtp_time (rtpg729pay, timestamp);
312 }
313
314 if (size < G729_FRAME_SIZE)
315 gst_rtp_g729_pay_recalc_rtp_time (rtpg729pay, timestamp);
316
317 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rtpg729pay->first_ts))) {
318 rtpg729pay->first_ts = timestamp;
319 rtpg729pay->first_rtp_time = rtpg729pay->next_rtp_time;
320 }
321
322 /* let's reset the base timestamp when the adapter is empty */
323 if (available == 0)
324 rtpg729pay->next_ts = timestamp;
325
326 if (available == 0 && size >= min_payload_len && size <= max_payload_len) {
327 ret = gst_rtp_g729_pay_push (rtpg729pay, buf);
328 return ret;
329 }
330
331 gst_adapter_push (adapter, buf);
332 available = gst_adapter_available (adapter);
333
334 /* as long as we have full frames */
335 /* this loop will push all available buffers till the last frame */
336 while (available >= min_payload_len ||
337 available % G729_FRAME_SIZE == G729B_CN_FRAME_SIZE) {
338 /* We send as much as we can */
339 if (available <= max_payload_len) {
340 payload_len = available;
341 } else {
342 payload_len = MIN (max_payload_len,
343 (available / G729_FRAME_SIZE) * G729_FRAME_SIZE);
344 }
345
346 ret = gst_rtp_g729_pay_push (rtpg729pay,
347 gst_adapter_take_buffer_fast (adapter, payload_len));
348 available -= payload_len;
349 }
350
351 return ret;
352
353 /* ERRORS */
354 invalid_size:
355 {
356 GST_ELEMENT_ERROR (payload, STREAM, WRONG_TYPE,
357 ("Invalid input buffer size"),
358 ("Invalid buffer size, should be a multiple of"
359 " G729_FRAME_SIZE(10) with an optional G729B_CN_FRAME_SIZE(2)"
360 " added to it, but it is %" G_GSIZE_FORMAT, size));
361 gst_buffer_unref (buf);
362 return GST_FLOW_ERROR;
363 }
364 }
365
366 static GstStateChangeReturn
gst_rtp_g729_pay_change_state(GstElement * element,GstStateChange transition)367 gst_rtp_g729_pay_change_state (GstElement * element, GstStateChange transition)
368 {
369 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
370
371 /* handle upwards state changes here */
372 switch (transition) {
373 default:
374 break;
375 }
376
377 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
378
379 /* handle downwards state changes */
380 switch (transition) {
381 case GST_STATE_CHANGE_PAUSED_TO_READY:
382 gst_rtp_g729_pay_reset (GST_RTP_G729_PAY (element));
383 break;
384 default:
385 break;
386 }
387
388 return ret;
389 }
390
391 gboolean
gst_rtp_g729_pay_plugin_init(GstPlugin * plugin)392 gst_rtp_g729_pay_plugin_init (GstPlugin * plugin)
393 {
394 return gst_element_register (plugin, "rtpg729pay",
395 GST_RANK_SECONDARY, GST_TYPE_RTP_G729_PAY);
396 }
397