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