gst/rtp/gstrtpg729pay.*: Replace G729 payloader with an improved version. Fixes ...
[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 /* TODO: fix gstrtpbuffer.h */
39 #undef GST_RTP_PAYLOAD_G729
40 #define GST_RTP_PAYLOAD_G729 18
41 #undef GST_RTP_PAYLOAD_G729_STRING
42 #define GST_RTP_PAYLOAD_G729_STRING "18"
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 (GstBaseRTPPayload * payload, GstCaps * caps);
51 static GstFlowReturn
52 gst_rtp_g729_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buf);
53
54
55 static const GstElementDetails gst_rtp_g729_pay_details =
56 GST_ELEMENT_DETAILS ("G729 RTP packet payloader",
57     "Codec/Payloader/Network",
58     "Packetize G729 audio into RTP packets",
59     "Olivier Crete <olivier.crete@collabora.co.uk>");
60
61 static GstStaticPadTemplate gst_rtp_g729_pay_sink_template =
62 GST_STATIC_PAD_TEMPLATE ("sink",
63     GST_PAD_SINK,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("audio/G729, "     /* according to RFC 3555 */
66         "channels = (int) 1, " "rate = (int) 8000")
67     );
68
69 static GstStaticPadTemplate gst_rtp_g729_pay_src_template =
70     GST_STATIC_PAD_TEMPLATE ("src",
71     GST_PAD_SRC,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS ("application/x-rtp, "
74         "media = (string) \"audio\", "
75         "payload = (int) " GST_RTP_PAYLOAD_G729_STRING ", "
76         "clock-rate = (int) 8000, "
77         "encoding-name = (string) \"G729\"; "
78         "application/x-rtp, "
79         "media = (string) \"audio\", "
80         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
81         "clock-rate = (int) 8000, " "encoding-name = (string) \"G729\"")
82     );
83
84 static void
85 gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass);
86
87 GST_BOILERPLATE (GstRTPG729Pay, gst_rtp_g729_pay, GstBaseRTPAudioPayload,
88     GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
89
90 static void
91 gst_rtp_g729_pay_base_init (gpointer klass)
92 {
93   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
94
95   gst_element_class_add_pad_template (element_class,
96       gst_static_pad_template_get (&gst_rtp_g729_pay_sink_template));
97   gst_element_class_add_pad_template (element_class,
98       gst_static_pad_template_get (&gst_rtp_g729_pay_src_template));
99   gst_element_class_set_details (element_class, &gst_rtp_g729_pay_details);
100 }
101
102 static void
103 gst_rtp_g729_pay_class_init (GstRTPG729PayClass * klass)
104 {
105   GstBaseRTPPayloadClass *payload_class = GST_BASE_RTP_PAYLOAD_CLASS (klass);
106
107   payload_class->set_caps = gst_rtp_g729_pay_set_caps;
108   payload_class->handle_buffer = gst_rtp_g729_pay_handle_buffer;
109 }
110
111 static void
112 gst_rtp_g729_pay_init (GstRTPG729Pay * pay, GstRTPG729PayClass * klass)
113 {
114   GstBaseRTPPayload *payload = GST_BASE_RTP_PAYLOAD (pay);
115   GstBaseRTPAudioPayload *audiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (pay);
116
117   payload->pt = GST_RTP_PAYLOAD_G729;
118   gst_basertppayload_set_options (payload, "audio", FALSE, "G729", 8000);
119
120   gst_base_rtp_audio_payload_set_frame_based (audiopayload);
121   gst_base_rtp_audio_payload_set_frame_options (audiopayload,
122       G729_FRAME_DURATION_MS, G729_FRAME_SIZE);
123
124 }
125
126 static gboolean
127 gst_rtp_g729_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
128 {
129   GstStructure *structure;
130   gint pt;
131
132   structure = gst_caps_get_structure (caps, 0);
133   if (!gst_structure_get_int (structure, "payload", &pt))
134     pt = GST_RTP_PAYLOAD_G729;
135
136   payload->pt = pt;
137   payload->dynamic = pt != GST_RTP_PAYLOAD_G729;
138
139   gst_basertppayload_set_outcaps (payload, NULL);
140
141   return TRUE;
142 }
143
144 static GstFlowReturn
145 gst_rtp_g729_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buf)
146 {
147   GstFlowReturn ret = GST_FLOW_OK;
148   GstBaseRTPAudioPayload *basertpaudiopayload =
149       GST_BASE_RTP_AUDIO_PAYLOAD (payload);
150   GstAdapter *adapter = NULL;
151   guint payload_len;
152   const guint8 *data = NULL;
153   guint available;
154   guint maxptime_octets = G_MAXUINT;
155   guint minptime_octets = 0;
156   guint min_payload_len;
157   guint max_payload_len;
158   gboolean use_adapter = FALSE;
159
160   available = GST_BUFFER_SIZE (buf);
161
162   if (available % G729_FRAME_SIZE != 0 &&
163       available % G729_FRAME_SIZE != G729B_CN_FRAME_SIZE)
164     goto invalid_size;
165
166   /* max number of bytes based on given ptime, has to be multiple of
167    * frame_duration */
168   if (payload->max_ptime != -1) {
169     guint ptime_ms = payload->max_ptime / 1000000;
170
171     maxptime_octets = G729_FRAME_SIZE *
172         (int) (ptime_ms / G729_FRAME_DURATION_MS);
173
174     if (maxptime_octets < G729_FRAME_SIZE) {
175       GST_WARNING_OBJECT (basertpaudiopayload, "Given ptime %d is smaller than"
176           " minimum %d ns, overwriting to minimum",
177           payload->max_ptime, G729_FRAME_DURATION_MS);
178       maxptime_octets = G729_FRAME_SIZE;
179     }
180   }
181
182   max_payload_len = MIN (
183       /* MTU max */
184       (int) (gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU
185               (basertpaudiopayload), 0, 0) / G729_FRAME_SIZE) * G729_FRAME_SIZE,
186       /* ptime max */
187       maxptime_octets);
188
189   /* min number of bytes based on a given ptime, has to be a multiple
190      of frame duration */
191   {
192     guint64 min_ptime;
193
194     g_object_get (G_OBJECT (payload), "min-ptime", &min_ptime, NULL);
195
196     min_ptime = min_ptime / 1000000;
197     minptime_octets = G729_FRAME_SIZE *
198         (int) (min_ptime / G729_FRAME_DURATION_MS);
199   }
200
201   min_payload_len = MAX (minptime_octets, G729_FRAME_SIZE);
202
203   if (min_payload_len > max_payload_len) {
204     min_payload_len = max_payload_len;
205   }
206
207   GST_DEBUG_OBJECT (basertpaudiopayload,
208       "Calculated min_payload_len %u and max_payload_len %u",
209       min_payload_len, max_payload_len);
210
211   adapter = gst_base_rtp_audio_payload_get_adapter (basertpaudiopayload);
212
213   if (adapter && gst_adapter_available (adapter)) {
214     /* If there is always data in the adapter, we have to use it */
215     gst_adapter_push (adapter, buf);
216     available = gst_adapter_available (adapter);
217     use_adapter = TRUE;
218   } else {
219     /* let's set the base timestamp */
220     basertpaudiopayload->base_ts = GST_BUFFER_TIMESTAMP (buf);
221
222     /* If buffer fits on an RTP packet, let's just push it through */
223     /* this will check against max_ptime and max_mtu */
224     if (GST_BUFFER_SIZE (buf) >= min_payload_len &&
225         GST_BUFFER_SIZE (buf) <= max_payload_len) {
226       ret = gst_base_rtp_audio_payload_push (basertpaudiopayload,
227           GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf),
228           GST_BUFFER_TIMESTAMP (buf));
229       gst_buffer_unref (buf);
230
231       return ret;
232     }
233
234     available = GST_BUFFER_SIZE (buf);
235     data = (guint8 *) GST_BUFFER_DATA (buf);
236   }
237
238   /* as long as we have full frames */
239   /* this loop will push all available buffers till the last frame */
240   while (available >= min_payload_len ||
241       available % G729_FRAME_SIZE == G729B_CN_FRAME_SIZE) {
242     guint num;
243
244     /* We send as much as we can */
245     if (available <= max_payload_len) {
246       payload_len = available;
247     } else {
248       payload_len = MIN (max_payload_len,
249           (available / G729_FRAME_SIZE) * G729_FRAME_SIZE);
250     }
251
252     if (use_adapter) {
253       data = gst_adapter_peek (adapter, payload_len);
254     }
255
256     ret = gst_base_rtp_audio_payload_push (basertpaudiopayload, data,
257         payload_len, basertpaudiopayload->base_ts);
258
259     num = payload_len / G729_FRAME_SIZE;
260     basertpaudiopayload->base_ts += G729_FRAME_DURATION * num;
261
262     if (use_adapter) {
263       gst_adapter_flush (adapter, payload_len);
264       available = gst_adapter_available (adapter);
265     } else {
266       available -= payload_len;
267       data += payload_len;
268     }
269   }
270
271   if (!use_adapter) {
272     if (available != 0 && adapter) {
273       GstBuffer *buf2;
274       buf2 = gst_buffer_create_sub (buf,
275           GST_BUFFER_SIZE (buf) - available, available);
276       gst_adapter_push (adapter, buf2);
277     } else {
278       gst_buffer_unref (buf);
279     }
280   }
281
282   if (adapter) {
283     g_object_unref (adapter);
284   }
285
286   return ret;
287
288   /* ERRORS */
289 invalid_size:
290   {
291     GST_ELEMENT_ERROR (payload, STREAM, WRONG_TYPE,
292         ("Invalid input buffer size"),
293         ("Invalid buffer size, should be a multiple of"
294             " G729_FRAME_SIZE(10) with an optional G729B_CN_FRAME_SIZE(2)"
295             " added to it, but it is %u", available));
296     gst_buffer_unref (buf);
297     return GST_FLOW_ERROR;
298   }
299 }
300
301 gboolean
302 gst_rtp_g729_pay_plugin_init (GstPlugin * plugin)
303 {
304   return gst_element_register (plugin, "rtpg729pay",
305       GST_RANK_NONE, GST_TYPE_RTP_G729_PAY);
306 }