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