rtpsbcpay: Fix runtime warnings of gstreamer plugin.
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpsbcpay.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2008  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Lesser General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2.1 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "gstrtpsbcpay.h"
29 #include <math.h>
30 #include <string.h>
31
32 #define RTP_SBC_PAYLOAD_HEADER_SIZE 1
33 #define DEFAULT_MIN_FRAMES 0
34 #define RTP_SBC_HEADER_TOTAL (12 + RTP_SBC_PAYLOAD_HEADER_SIZE)
35
36 #if __BYTE_ORDER == __LITTLE_ENDIAN
37
38 struct rtp_payload
39 {
40   guint8 frame_count:4;
41   guint8 rfa0:1;
42   guint8 is_last_fragment:1;
43   guint8 is_first_fragment:1;
44   guint8 is_fragmented:1;
45 } __attribute__ ((packed));
46
47 #elif __BYTE_ORDER == __BIG_ENDIAN
48
49 struct rtp_payload
50 {
51   guint8 is_fragmented:1;
52   guint8 is_first_fragment:1;
53   guint8 is_last_fragment:1;
54   guint8 rfa0:1;
55   guint8 frame_count:4;
56 } __attribute__ ((packed));
57
58 #else
59 #error "Unknown byte order"
60 #endif
61
62 enum
63 {
64   PROP_0,
65   PROP_MIN_FRAMES
66 };
67
68 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sbc_pay_debug);
69 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
70
71 GST_BOILERPLATE (GstRtpSBCPay, gst_rtp_sbc_pay, GstBaseRTPPayload,
72     GST_TYPE_BASE_RTP_PAYLOAD);
73
74 static const GstElementDetails gst_rtp_sbc_pay_details =
75 GST_ELEMENT_DETAILS ("RTP packet payloader",
76     "Codec/Payloader/Network",
77     "Payload SBC audio as RTP packets",
78     "Thiago Sousa Santos " "<thiagoss@lcc.ufcg.edu.br>");
79
80 static GstStaticPadTemplate gst_rtp_sbc_pay_sink_factory =
81 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
82     GST_STATIC_CAPS ("audio/x-sbc, "
83         "rate = (int) { 16000, 32000, 44100, 48000 }, "
84         "channels = (int) [ 1, 2 ], "
85         "mode = (string) { \"mono\", \"dual\", \"stereo\", \"joint\" }, "
86         "blocks = (int) { 4, 8, 12, 16 }, "
87         "subbands = (int) { 4, 8 }, "
88         "allocation = (string) { \"snr\", \"loudness\" }, "
89         "bitpool = (int) [ 2, 64 ]")
90     );
91
92 static GstStaticPadTemplate gst_rtp_sbc_pay_src_factory =
93 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
94     GST_STATIC_CAPS ("application/x-rtp, "
95         "media = (string) \"audio\","
96         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
97         "clock-rate = (int) { 16000, 32000, 44100, 48000 },"
98         "encoding-name = (string) \"SBC\"")
99     );
100
101 static void gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
102     const GValue * value, GParamSpec * pspec);
103 static void gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
104     GValue * value, GParamSpec * pspec);
105
106 static gint
107 gst_rtp_sbc_pay_get_frame_len (gint subbands, gint channels,
108     gint blocks, gint bitpool, const gchar * channel_mode)
109 {
110   gint len;
111   gint join;
112
113   len = 4 + (4 * subbands * channels) / 8;
114
115   if (strcmp (channel_mode, "mono") == 0 || strcmp (channel_mode, "dual") == 0)
116     len += ((blocks * channels * bitpool) + 7) / 8;
117   else {
118     join = strcmp (channel_mode, "joint") == 0 ? 1 : 0;
119     len += ((join * subbands + blocks * bitpool) + 7) / 8;
120   }
121
122   return len;
123 }
124
125 static gboolean
126 gst_rtp_sbc_pay_set_caps (GstBaseRTPPayload * payload, GstCaps * caps)
127 {
128   GstRtpSBCPay *sbcpay;
129   gint rate, subbands, channels, blocks, bitpool;
130   gint frame_len;
131   const gchar *channel_mode;
132   GstStructure *structure;
133
134   sbcpay = GST_RTP_SBC_PAY (payload);
135
136   structure = gst_caps_get_structure (caps, 0);
137   if (!gst_structure_get_int (structure, "rate", &rate))
138     return FALSE;
139   if (!gst_structure_get_int (structure, "channels", &channels))
140     return FALSE;
141   if (!gst_structure_get_int (structure, "blocks", &blocks))
142     return FALSE;
143   if (!gst_structure_get_int (structure, "bitpool", &bitpool))
144     return FALSE;
145   if (!gst_structure_get_int (structure, "subbands", &subbands))
146     return FALSE;
147
148   channel_mode = gst_structure_get_string (structure, "mode");
149   if (!channel_mode)
150     return FALSE;
151
152   frame_len = gst_rtp_sbc_pay_get_frame_len (subbands, channels, blocks,
153       bitpool, channel_mode);
154
155   sbcpay->frame_length = frame_len;
156
157   gst_basertppayload_set_options (payload, "audio", TRUE, "SBC", rate);
158
159   GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
160
161   return gst_basertppayload_set_outcaps (payload, NULL);
162 }
163
164 static GstFlowReturn
165 gst_rtp_sbc_pay_flush_buffers (GstRtpSBCPay * sbcpay)
166 {
167   guint available;
168   guint max_payload;
169   GstBuffer *outbuf;
170   guint8 *payload_data;
171   guint8 *data;
172   guint frame_count;
173   guint payload_length;
174   struct rtp_payload *payload;
175
176   if (sbcpay->frame_length == 0) {
177     GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
178     return GST_FLOW_ERROR;
179   }
180
181   available = gst_adapter_available (sbcpay->adapter);
182
183   max_payload =
184       gst_rtp_buffer_calc_payload_len (GST_BASE_RTP_PAYLOAD_MTU (sbcpay) -
185       RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
186
187   max_payload = MIN (max_payload, available);
188   frame_count = max_payload / sbcpay->frame_length;
189   payload_length = frame_count * sbcpay->frame_length;
190
191   outbuf = gst_rtp_buffer_new_allocate (payload_length +
192       RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
193
194   gst_rtp_buffer_set_payload_type (outbuf, GST_BASE_RTP_PAYLOAD_PT (sbcpay));
195
196   payload_data = gst_rtp_buffer_get_payload (outbuf);
197   payload = (struct rtp_payload *) payload_data;
198   memset (payload, 0, sizeof (struct rtp_payload));
199   payload->frame_count = frame_count;
200
201   data = gst_adapter_take (sbcpay->adapter, payload_length);
202   memcpy (payload_data + RTP_SBC_PAYLOAD_HEADER_SIZE, data, payload_length);
203   g_free (data);
204
205   GST_BUFFER_TIMESTAMP (outbuf) = sbcpay->timestamp;
206   GST_DEBUG_OBJECT (sbcpay, "Pushing %d bytes", payload_length);
207
208   return gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (sbcpay), outbuf);
209 }
210
211 static GstFlowReturn
212 gst_rtp_sbc_pay_handle_buffer (GstBaseRTPPayload * payload, GstBuffer * buffer)
213 {
214   GstRtpSBCPay *sbcpay;
215   guint available;
216
217   /* FIXME check for negotiation */
218
219   sbcpay = GST_RTP_SBC_PAY (payload);
220   sbcpay->timestamp = GST_BUFFER_TIMESTAMP (buffer);
221
222   gst_adapter_push (sbcpay->adapter, gst_buffer_copy (buffer));
223
224   available = gst_adapter_available (sbcpay->adapter);
225   if (available + RTP_SBC_HEADER_TOTAL >=
226       GST_BASE_RTP_PAYLOAD_MTU (sbcpay) ||
227       (sbcpay->min_frames != -1 && available >
228           (sbcpay->min_frames * sbcpay->frame_length)))
229     return gst_rtp_sbc_pay_flush_buffers (sbcpay);
230
231   return GST_FLOW_OK;
232 }
233
234 static gboolean
235 gst_rtp_sbc_pay_handle_event (GstPad * pad, GstEvent * event)
236 {
237   GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (GST_PAD_PARENT (pad));
238
239   switch (GST_EVENT_TYPE (event)) {
240     case GST_EVENT_EOS:
241       gst_rtp_sbc_pay_flush_buffers (sbcpay);
242       break;
243     default:
244       break;
245   }
246
247   return FALSE;
248 }
249
250 static void
251 gst_rtp_sbc_pay_base_init (gpointer g_class)
252 {
253   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
254
255   gst_element_class_add_pad_template (element_class,
256       gst_static_pad_template_get (&gst_rtp_sbc_pay_sink_factory));
257   gst_element_class_add_pad_template (element_class,
258       gst_static_pad_template_get (&gst_rtp_sbc_pay_src_factory));
259
260   gst_element_class_set_details (element_class, &gst_rtp_sbc_pay_details);
261 }
262
263 static void
264 gst_rtp_sbc_pay_finalize (GObject * object)
265 {
266   GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
267   g_object_unref (sbcpay->adapter);
268
269   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
270 }
271
272 static void
273 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
274 {
275   GObjectClass *gobject_class;
276   GstBaseRTPPayloadClass *payload_class = GST_BASE_RTP_PAYLOAD_CLASS (klass);
277
278   gobject_class = G_OBJECT_CLASS (klass);
279   parent_class = g_type_class_peek_parent (klass);
280
281   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_finalize);
282   gobject_class->set_property =
283       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_property);
284   gobject_class->get_property =
285       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_get_property);
286
287   payload_class->set_caps = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_caps);
288   payload_class->handle_buffer =
289       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
290   payload_class->handle_event =
291       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_event);
292
293   /* properties */
294   g_object_class_install_property (G_OBJECT_CLASS (klass),
295       PROP_MIN_FRAMES,
296       g_param_spec_int ("min-frames", "minimum frame number",
297           "Minimum quantity of frames to send in one packet "
298           "(-1 for maximum allowed by the mtu)",
299           -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
300
301   GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
302       "RTP SBC payloader");
303 }
304
305 static void
306 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
307     const GValue * value, GParamSpec * pspec)
308 {
309   GstRtpSBCPay *sbcpay;
310
311   sbcpay = GST_RTP_SBC_PAY (object);
312
313   switch (prop_id) {
314     case PROP_MIN_FRAMES:
315       sbcpay->min_frames = g_value_get_int (value);
316       break;
317     default:
318       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319       break;
320   }
321 }
322
323 static void
324 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
325     GValue * value, GParamSpec * pspec)
326 {
327   GstRtpSBCPay *sbcpay;
328
329   sbcpay = GST_RTP_SBC_PAY (object);
330
331   switch (prop_id) {
332     case PROP_MIN_FRAMES:
333       g_value_set_int (value, sbcpay->min_frames);
334       break;
335     default:
336       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337       break;
338   }
339 }
340
341 static void
342 gst_rtp_sbc_pay_init (GstRtpSBCPay * self, GstRtpSBCPayClass * klass)
343 {
344   self->adapter = gst_adapter_new ();
345   self->frame_length = 0;
346   self->timestamp = 0;
347
348   self->min_frames = DEFAULT_MIN_FRAMES;
349 }
350
351 gboolean
352 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
353 {
354   return gst_element_register (plugin, "rtpsbcpay",
355       GST_RANK_NONE, GST_TYPE_RTP_SBC_PAY);
356 }