rtpg729pay: avoid basertppayload perfect-rtptime mode
[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 (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_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>");
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 }
140
141 static gboolean
142 gst_rtp_g729_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
143 {
144   gboolean res;
145   GstStructure *structure;
146   gint pt;
147
148   structure = gst_caps_get_structure (caps, 0);
149   if (!gst_structure_get_int (structure, "payload", &pt))
150     pt = GST_RTP_PAYLOAD_G729;
151
152   payload->pt = pt;
153   payload->dynamic = pt != GST_RTP_PAYLOAD_G729;
154
155   res = gst_basertppayload_set_outcaps (payload, NULL);
156
157   return res;
158 }
159
160 static GstFlowReturn
161 gst_rtp_g729_pay_push (GstRTPG729Pay * rtpg729pay,
162     const guint8 * data, guint payload_len, GstClockTime timestamp,
163     GstClockTime duration)
164 {
165   GstBaseRTPPayload *basepayload;
166   GstBuffer *outbuf;
167   guint8 *payload;
168   GstFlowReturn ret;
169
170   basepayload = GST_BASE_RTP_PAYLOAD (rtpg729pay);
171
172   GST_DEBUG_OBJECT (rtpg729pay, "Pushing %d bytes ts %" GST_TIME_FORMAT,
173       payload_len, GST_TIME_ARGS (timestamp));
174
175   /* create buffer to hold the payload */
176   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
177
178   /* copy payload */
179   payload = gst_rtp_buffer_get_payload (outbuf);
180   memcpy (payload, data, payload_len);
181
182   /* set metadata */
183   GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
184   GST_BUFFER_DURATION (outbuf) = duration;
185
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;
191   }
192
193   ret = gst_basertppayload_push (basepayload, outbuf);
194
195   return ret;
196 }
197
198 static GstFlowReturn
199 gst_rtp_g729_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buf)
200 {
201   GstFlowReturn ret = GST_FLOW_OK;
202   GstRTPG729Pay *rtpg729pay = GST_RTP_G729_PAY (payload);
203   GstAdapter *adapter = NULL;
204   guint payload_len;
205   guint available;
206   guint maxptime_octets = G_MAXUINT;
207   guint minptime_octets = 0;
208   guint min_payload_len;
209   guint max_payload_len;
210
211   available = GST_BUFFER_SIZE (buf);
212
213   if (available % G729_FRAME_SIZE != 0 &&
214       available % G729_FRAME_SIZE != G729B_CN_FRAME_SIZE)
215     goto invalid_size;
216
217   /* max number of bytes based on given ptime, has to be multiple of
218    * frame_duration */
219   if (payload->max_ptime != -1) {
220     guint ptime_ms = payload->max_ptime / GST_MSECOND;
221
222     maxptime_octets = G729_FRAME_SIZE *
223         (int) (ptime_ms / G729_FRAME_DURATION_MS);
224
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;
230     }
231   }
232
233   max_payload_len = MIN (
234       /* MTU max */
235       (int) (gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU
236               (payload), 0, 0) / G729_FRAME_SIZE)
237       * G729_FRAME_SIZE,
238       /* ptime max */
239       maxptime_octets);
240
241   /* min number of bytes based on a given ptime, has to be a multiple
242      of frame duration */
243   {
244     guint64 min_ptime = payload->min_ptime;
245
246     min_ptime = min_ptime / GST_MSECOND;
247     minptime_octets = G729_FRAME_SIZE *
248         (int) (min_ptime / G729_FRAME_DURATION_MS);
249   }
250
251   min_payload_len = MAX (minptime_octets, G729_FRAME_SIZE);
252
253   if (min_payload_len > max_payload_len) {
254     min_payload_len = max_payload_len;
255   }
256
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);
262
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);
266
267     min_payload_len = max_payload_len = ptime_in_bytes;
268   }
269
270   GST_LOG_OBJECT (payload,
271       "Calculated min_payload_len %u and max_payload_len %u",
272       min_payload_len, max_payload_len);
273
274   if (GST_BUFFER_IS_DISCONT (buf))
275     rtpg729pay->discont = TRUE;
276
277   adapter = rtpg729pay->adapter;
278
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);
282
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);
290     return ret;
291   }
292
293   gst_adapter_push (adapter, buf);
294
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;
301
302     /* We send as much as we can */
303     if (available <= max_payload_len) {
304       payload_len = available;
305     } else {
306       payload_len = MIN (max_payload_len,
307           (available / G729_FRAME_SIZE) * G729_FRAME_SIZE);
308     }
309
310     duration = (payload_len / G729_FRAME_SIZE) * G729_FRAME_DURATION;
311     rtpg729pay->next_ts += duration;
312
313     ret = gst_rtp_g729_pay_push (rtpg729pay,
314         gst_adapter_take (adapter, payload_len), payload_len,
315         rtpg729pay->next_ts, duration);
316
317     available = gst_adapter_available (adapter);
318   }
319
320   return ret;
321
322   /* ERRORS */
323 invalid_size:
324   {
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;
332   }
333 }
334
335 static GstStateChangeReturn
336 gst_rtp_g729_pay_change_state (GstElement * element, GstStateChange transition)
337 {
338   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
339
340   /* handle upwards state changes here */
341   switch (transition) {
342     default:
343       break;
344   }
345
346   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
347
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));
352       break;
353     default:
354       break;
355   }
356
357   return ret;
358 }
359
360 gboolean
361 gst_rtp_g729_pay_plugin_init (GstPlugin * plugin)
362 {
363   return gst_element_register (plugin, "rtpg729pay",
364       GST_RANK_NONE, GST_TYPE_RTP_G729_PAY);
365 }