Merge branch 'tizen_gst_1.20.0' into tizen
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / 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., 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 "gstrtpelements.h"
38 #include "gstrtpg729pay.h"
39 #include "gstrtputils.h"
40
41 GST_DEBUG_CATEGORY_STATIC (rtpg729pay_debug);
42 #define GST_CAT_DEFAULT (rtpg729pay_debug)
43
44 #define G729_FRAME_SIZE 10
45 #define G729B_CN_FRAME_SIZE 2
46 #define G729_FRAME_DURATION (10 * GST_MSECOND)
47 #define G729_FRAME_DURATION_MS (10)
48
49 static gboolean
50 gst_rtp_g729_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps);
51 static GstFlowReturn
52 gst_rtp_g729_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buf);
53
54 static GstStateChangeReturn
55 gst_rtp_g729_pay_change_state (GstElement * element, GstStateChange transition);
56
57 static GstStaticPadTemplate gst_rtp_g729_pay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("audio/G729, "     /* according to RFC 3555 */
62         "channels = (int) 1, " "rate = (int) 8000")
63     );
64
65 static GstStaticPadTemplate gst_rtp_g729_pay_src_template =
66     GST_STATIC_PAD_TEMPLATE ("src",
67     GST_PAD_SRC,
68     GST_PAD_ALWAYS,
69     GST_STATIC_CAPS ("application/x-rtp, "
70         "media = (string) \"audio\", "
71         "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
72         "clock-rate = (int) 8000, "
73         "encoding-name = (string) \"G729\"; "
74         "application/x-rtp, "
75         "media = (string) \"audio\", "
76         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
77         "clock-rate = (int) 8000, " "encoding-name = (string) \"G729\"")
78     );
79
80 #define gst_rtp_g729_pay_parent_class parent_class
81 G_DEFINE_TYPE (GstRTPG729Pay, gst_rtp_g729_pay, GST_TYPE_RTP_BASE_PAYLOAD);
82 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpg729pay, "rtpg729pay",
83     GST_RANK_SECONDARY, GST_TYPE_RTP_G729_PAY, rtp_element_init (plugin));
84
85 static void
86 gst_rtp_g729_pay_finalize (GObject * object)
87 {
88   GstRTPG729Pay *pay = GST_RTP_G729_PAY (object);
89
90   g_object_unref (pay->adapter);
91
92   G_OBJECT_CLASS (parent_class)->finalize (object);
93 }
94
95 static void
96 gst_rtp_g729_pay_class_init (GstRTPG729PayClass * klass)
97 {
98   GObjectClass *gobject_class = (GObjectClass *) klass;
99   GstElementClass *gstelement_class = (GstElementClass *) klass;
100   GstRTPBasePayloadClass *payload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
101
102   GST_DEBUG_CATEGORY_INIT (rtpg729pay_debug, "rtpg729pay", 0,
103       "G.729 RTP Payloader");
104
105   gobject_class->finalize = gst_rtp_g729_pay_finalize;
106
107   gstelement_class->change_state = gst_rtp_g729_pay_change_state;
108
109   gst_element_class_add_static_pad_template (gstelement_class,
110       &gst_rtp_g729_pay_sink_template);
111   gst_element_class_add_static_pad_template (gstelement_class,
112       &gst_rtp_g729_pay_src_template);
113
114   gst_element_class_set_static_metadata (gstelement_class,
115       "RTP G.729 payloader", "Codec/Payloader/Network/RTP",
116       "Packetize G.729 audio into RTP packets",
117       "Olivier Crete <olivier.crete@collabora.co.uk>");
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)
125 {
126   GstRTPBasePayload *payload = GST_RTP_BASE_PAYLOAD (pay);
127
128   payload->pt = GST_RTP_PAYLOAD_G729;
129
130   pay->adapter = gst_adapter_new ();
131 }
132
133 static void
134 gst_rtp_g729_pay_reset (GstRTPG729Pay * pay)
135 {
136   gst_adapter_clear (pay->adapter);
137   pay->discont = FALSE;
138   pay->next_rtp_time = 0;
139   pay->first_ts = GST_CLOCK_TIME_NONE;
140   pay->first_rtp_time = 0;
141 }
142
143 static gboolean
144 gst_rtp_g729_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
145 {
146   gboolean res;
147
148   gst_rtp_base_payload_set_options (payload, "audio",
149       payload->pt != GST_RTP_PAYLOAD_G729, "G729", 8000);
150
151   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
152
153   return res;
154 }
155
156 static GstFlowReturn
157 gst_rtp_g729_pay_push (GstRTPG729Pay * rtpg729pay, GstBuffer * buf)
158 {
159   GstRTPBasePayload *basepayload;
160   GstClockTime duration;
161   guint frames;
162   GstBuffer *outbuf;
163   GstFlowReturn ret;
164   GstRTPBuffer rtp = { NULL };
165   guint payload_len = gst_buffer_get_size (buf);
166
167   basepayload = GST_RTP_BASE_PAYLOAD (rtpg729pay);
168
169   GST_DEBUG_OBJECT (rtpg729pay, "Pushing %d bytes ts %" GST_TIME_FORMAT,
170       payload_len, GST_TIME_ARGS (rtpg729pay->next_ts));
171
172   /* create buffer to hold the payload */
173   outbuf =
174       gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
175       (rtpg729pay), 0, 0, 0);
176
177   gst_rtp_buffer_map (outbuf, GST_MAP_READWRITE, &rtp);
178
179   /* set metadata */
180   frames =
181       (payload_len / G729_FRAME_SIZE) + ((payload_len % G729_FRAME_SIZE) >> 1);
182   duration = frames * G729_FRAME_DURATION;
183   GST_BUFFER_PTS (outbuf) = rtpg729pay->next_ts;
184   GST_BUFFER_DURATION (outbuf) = duration;
185   GST_BUFFER_OFFSET (outbuf) = rtpg729pay->next_rtp_time;
186   rtpg729pay->next_ts += duration;
187   rtpg729pay->next_rtp_time += frames * 80;
188
189   if (G_UNLIKELY (rtpg729pay->discont)) {
190     GST_DEBUG_OBJECT (basepayload, "discont, setting marker bit");
191     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
192     gst_rtp_buffer_set_marker (&rtp, TRUE);
193     rtpg729pay->discont = FALSE;
194   }
195   gst_rtp_buffer_unmap (&rtp);
196
197   /* append payload */
198   gst_rtp_copy_audio_meta (basepayload, outbuf, buf);
199   outbuf = gst_buffer_append (outbuf, buf);
200
201   ret = gst_rtp_base_payload_push (basepayload, outbuf);
202
203   return ret;
204 }
205
206 static void
207 gst_rtp_g729_pay_recalc_rtp_time (GstRTPG729Pay * rtpg729pay, GstClockTime time)
208 {
209   if (GST_CLOCK_TIME_IS_VALID (rtpg729pay->first_ts)
210       && GST_CLOCK_TIME_IS_VALID (time) && time >= rtpg729pay->first_ts) {
211     GstClockTime diff;
212     guint32 rtpdiff;
213
214     diff = time - rtpg729pay->first_ts;
215     rtpdiff = (diff / GST_MSECOND) * 8;
216     rtpg729pay->next_rtp_time = rtpg729pay->first_rtp_time + rtpdiff;
217     GST_DEBUG_OBJECT (rtpg729pay,
218         "elapsed time %" GST_TIME_FORMAT ", rtp %" G_GUINT32_FORMAT ", "
219         "new offset %" G_GUINT32_FORMAT, GST_TIME_ARGS (diff), rtpdiff,
220         rtpg729pay->next_rtp_time);
221   }
222 }
223
224 static GstFlowReturn
225 gst_rtp_g729_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buf)
226 {
227   GstFlowReturn ret = GST_FLOW_OK;
228   GstRTPG729Pay *rtpg729pay = GST_RTP_G729_PAY (payload);
229   GstAdapter *adapter = NULL;
230   guint payload_len;
231   guint available;
232   guint maxptime_octets = G_MAXUINT;
233   guint minptime_octets = 0;
234   guint min_payload_len;
235   guint max_payload_len;
236   gsize size;
237   GstClockTime timestamp;
238
239   size = gst_buffer_get_size (buf);
240
241   if (size % G729_FRAME_SIZE != 0 &&
242       size % 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_RTP_BASE_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->ptime) {
287     guint64 ptime = payload->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   timestamp = GST_BUFFER_PTS (buf);
306
307   /* resync rtp time on discont or a discontinuous cn packet */
308   if (GST_BUFFER_IS_DISCONT (buf)) {
309     /* flush remainder */
310     if (available > 0) {
311       gst_rtp_g729_pay_push (rtpg729pay,
312           gst_adapter_take_buffer_fast (adapter, available));
313       available = 0;
314     }
315     rtpg729pay->discont = TRUE;
316     gst_rtp_g729_pay_recalc_rtp_time (rtpg729pay, timestamp);
317   }
318
319   if (size < G729_FRAME_SIZE)
320     gst_rtp_g729_pay_recalc_rtp_time (rtpg729pay, timestamp);
321
322   if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rtpg729pay->first_ts))) {
323     rtpg729pay->first_ts = timestamp;
324     rtpg729pay->first_rtp_time = rtpg729pay->next_rtp_time;
325   }
326
327   /* let's reset the base timestamp when the adapter is empty */
328   if (available == 0)
329     rtpg729pay->next_ts = timestamp;
330
331   if (available == 0 && size >= min_payload_len && size <= max_payload_len) {
332     ret = gst_rtp_g729_pay_push (rtpg729pay, buf);
333     return ret;
334   }
335
336   gst_adapter_push (adapter, buf);
337   available = gst_adapter_available (adapter);
338
339   /* as long as we have full frames */
340   /* this loop will push all available buffers till the last frame */
341   while (available >= min_payload_len ||
342       available % G729_FRAME_SIZE == G729B_CN_FRAME_SIZE) {
343     /* We send as much as we can */
344     if (available <= max_payload_len) {
345       payload_len = available;
346     } else {
347       payload_len = MIN (max_payload_len,
348           (available / G729_FRAME_SIZE) * G729_FRAME_SIZE);
349     }
350
351     ret = gst_rtp_g729_pay_push (rtpg729pay,
352         gst_adapter_take_buffer_fast (adapter, payload_len));
353     available -= payload_len;
354   }
355
356   return ret;
357
358   /* ERRORS */
359 invalid_size:
360   {
361     GST_ELEMENT_ERROR (payload, STREAM, WRONG_TYPE,
362         ("Invalid input buffer size"),
363         ("Invalid buffer size, should be a multiple of"
364             " G729_FRAME_SIZE(10) with an optional G729B_CN_FRAME_SIZE(2)"
365             " added to it, but it is %" G_GSIZE_FORMAT, size));
366     gst_buffer_unref (buf);
367     return GST_FLOW_ERROR;
368   }
369 }
370
371 static GstStateChangeReturn
372 gst_rtp_g729_pay_change_state (GstElement * element, GstStateChange transition)
373 {
374   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
375
376   /* handle upwards state changes here */
377   switch (transition) {
378     default:
379       break;
380   }
381
382   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
383
384   /* handle downwards state changes */
385   switch (transition) {
386     case GST_STATE_CHANGE_PAUSED_TO_READY:
387       gst_rtp_g729_pay_reset (GST_RTP_G729_PAY (element));
388       break;
389     default:
390       break;
391   }
392
393   return ret;
394 }