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