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