rtpsbcpay: Update copyright information
[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   gst_adapter_push (sbcpay->adapter, gst_buffer_copy (buffer));
221
222   sbcpay->timestamp = GST_BUFFER_TIMESTAMP (buffer);
223   available = gst_adapter_available (sbcpay->adapter);
224   if (available + RTP_SBC_HEADER_TOTAL >=
225       GST_BASE_RTP_PAYLOAD_MTU (sbcpay) ||
226       (sbcpay->min_frames != -1 && available >
227           (sbcpay->min_frames * sbcpay->frame_length)))
228     return gst_rtp_sbc_pay_flush_buffers (sbcpay);
229
230   return GST_FLOW_OK;
231 }
232
233 static gboolean
234 gst_rtp_sbc_pay_handle_event (GstPad * pad, GstEvent * event)
235 {
236   GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (GST_PAD_PARENT (pad));
237
238   switch (GST_EVENT_TYPE (event)) {
239     case GST_EVENT_EOS:
240       gst_rtp_sbc_pay_flush_buffers (sbcpay);
241       break;
242     default:
243       break;
244   }
245
246   return FALSE;
247 }
248
249 static void
250 gst_rtp_sbc_pay_base_init (gpointer g_class)
251 {
252   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
253
254   gst_element_class_add_pad_template (element_class,
255       gst_static_pad_template_get (&gst_rtp_sbc_pay_sink_factory));
256   gst_element_class_add_pad_template (element_class,
257       gst_static_pad_template_get (&gst_rtp_sbc_pay_src_factory));
258
259   gst_element_class_set_details (element_class, &gst_rtp_sbc_pay_details);
260 }
261
262 static void
263 gst_rtp_sbc_pay_finalize (GObject * object)
264 {
265   GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
266   g_object_unref (sbcpay->adapter);
267
268   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
269 }
270
271 static void
272 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
273 {
274   GObjectClass *gobject_class;
275   GstBaseRTPPayloadClass *payload_class = GST_BASE_RTP_PAYLOAD_CLASS (klass);
276
277   gobject_class = G_OBJECT_CLASS (klass);
278   parent_class = g_type_class_peek_parent (klass);
279
280   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_finalize);
281   gobject_class->set_property =
282       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_property);
283   gobject_class->get_property =
284       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_get_property);
285
286   payload_class->set_caps = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_caps);
287   payload_class->handle_buffer =
288       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
289   payload_class->handle_event =
290       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_event);
291
292   /* properties */
293   g_object_class_install_property (G_OBJECT_CLASS (klass),
294       PROP_MIN_FRAMES,
295       g_param_spec_int ("min-frames", "minimum frame number",
296           "Minimum quantity of frames to send in one packet "
297           "(-1 for maximum allowed by the mtu)",
298           -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
299
300   GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
301       "RTP SBC payloader");
302 }
303
304 static void
305 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
306     const GValue * value, GParamSpec * pspec)
307 {
308   GstRtpSBCPay *sbcpay;
309
310   sbcpay = GST_RTP_SBC_PAY (object);
311
312   switch (prop_id) {
313     case PROP_MIN_FRAMES:
314       sbcpay->min_frames = g_value_get_int (value);
315       break;
316     default:
317       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318       break;
319   }
320 }
321
322 static void
323 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
324     GValue * value, GParamSpec * pspec)
325 {
326   GstRtpSBCPay *sbcpay;
327
328   sbcpay = GST_RTP_SBC_PAY (object);
329
330   switch (prop_id) {
331     case PROP_MIN_FRAMES:
332       g_value_set_int (value, sbcpay->min_frames);
333       break;
334     default:
335       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
336       break;
337   }
338 }
339
340 static void
341 gst_rtp_sbc_pay_init (GstRtpSBCPay * self, GstRtpSBCPayClass * klass)
342 {
343   self->adapter = gst_adapter_new ();
344   self->frame_length = 0;
345   self->timestamp = 0;
346
347   self->min_frames = DEFAULT_MIN_FRAMES;
348 }
349
350 gboolean
351 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
352 {
353   return gst_element_register (plugin, "rtpsbcpay",
354       GST_RANK_NONE, GST_TYPE_RTP_SBC_PAY);
355 }