rtp: Update codes based on 1.18.4
[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 <gst/audio/audio.h>
27 #include "gstrtpsbcpay.h"
28 #include <math.h>
29 #include <string.h>
30 #include "gstrtputils.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 /* BEGIN: Packing for rtp_payload */
37 #ifdef _MSC_VER
38 #pragma pack(push, 1)
39 #endif
40
41 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
42 /* FIXME: this seems all a bit over the top for a single byte.. */
43 struct rtp_payload
44 {
45   guint8 frame_count:4;
46   guint8 rfa0:1;
47   guint8 is_last_fragment:1;
48   guint8 is_first_fragment:1;
49   guint8 is_fragmented:1;
50 }
51 #elif G_BYTE_ORDER == G_BIG_ENDIAN
52 struct rtp_payload
53 {
54   guint8 is_fragmented:1;
55   guint8 is_first_fragment:1;
56   guint8 is_last_fragment:1;
57   guint8 rfa0:1;
58   guint8 frame_count:4;
59 }
60 #else
61 #error "Unknown byte order"
62 #endif
63
64 #ifdef _MSC_VER
65 ;
66 #pragma pack(pop)
67 #else
68 __attribute__ ((packed));
69 #endif
70 /* END: Packing for rtp_payload */
71
72 enum
73 {
74   PROP_0,
75   PROP_MIN_FRAMES
76 };
77
78 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sbc_pay_debug);
79 #define GST_CAT_DEFAULT gst_rtp_sbc_pay_debug
80
81 #define parent_class gst_rtp_sbc_pay_parent_class
82 G_DEFINE_TYPE (GstRtpSBCPay, gst_rtp_sbc_pay, GST_TYPE_RTP_BASE_PAYLOAD);
83
84 static GstStaticPadTemplate gst_rtp_sbc_pay_sink_factory =
85 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
86     GST_STATIC_CAPS ("audio/x-sbc, "
87         "rate = (int) { 16000, 32000, 44100, 48000 }, "
88         "channels = (int) [ 1, 2 ], "
89         "channel-mode = (string) { mono, dual, stereo, joint }, "
90         "blocks = (int) { 4, 8, 12, 16 }, "
91         "subbands = (int) { 4, 8 }, "
92         "allocation-method = (string) { snr, loudness }, "
93         "bitpool = (int) [ 2, 64 ]")
94     );
95
96 static GstStaticPadTemplate gst_rtp_sbc_pay_src_factory =
97 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
98     GST_STATIC_CAPS ("application/x-rtp, "
99         "media = (string) audio,"
100         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
101         "clock-rate = (int) { 16000, 32000, 44100, 48000 },"
102         "encoding-name = (string) SBC")
103     );
104
105 static void gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
106     const GValue * value, GParamSpec * pspec);
107 static void gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
108     GValue * value, GParamSpec * pspec);
109
110 static gint
111 gst_rtp_sbc_pay_get_frame_len (gint subbands, gint channels,
112     gint blocks, gint bitpool, const gchar * channel_mode)
113 {
114   gint len;
115   gint join;
116
117   len = 4 + (4 * subbands * channels) / 8;
118
119   if (strcmp (channel_mode, "mono") == 0 || strcmp (channel_mode, "dual") == 0)
120     len += ((blocks * channels * bitpool) + 7) / 8;
121   else {
122     join = strcmp (channel_mode, "joint") == 0 ? 1 : 0;
123     len += ((join * subbands + blocks * bitpool) + 7) / 8;
124   }
125
126   return len;
127 }
128
129 static gboolean
130 gst_rtp_sbc_pay_set_caps (GstRTPBasePayload * payload, GstCaps * caps)
131 {
132   GstRtpSBCPay *sbcpay;
133   gint rate, subbands, channels, blocks, bitpool;
134   gint frame_len;
135   const gchar *channel_mode;
136   GstStructure *structure;
137
138   sbcpay = GST_RTP_SBC_PAY (payload);
139
140   structure = gst_caps_get_structure (caps, 0);
141   if (!gst_structure_get_int (structure, "rate", &rate))
142     return FALSE;
143   if (!gst_structure_get_int (structure, "channels", &channels))
144     return FALSE;
145   if (!gst_structure_get_int (structure, "blocks", &blocks))
146     return FALSE;
147   if (!gst_structure_get_int (structure, "bitpool", &bitpool))
148     return FALSE;
149   if (!gst_structure_get_int (structure, "subbands", &subbands))
150     return FALSE;
151
152   channel_mode = gst_structure_get_string (structure, "channel-mode");
153   if (!channel_mode)
154     return FALSE;
155
156   frame_len = gst_rtp_sbc_pay_get_frame_len (subbands, channels, blocks,
157       bitpool, channel_mode);
158
159   sbcpay->frame_length = frame_len;
160   sbcpay->frame_duration = ((blocks * subbands) * GST_SECOND) / rate;
161   sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
162
163   gst_rtp_base_payload_set_options (payload, "audio", TRUE, "SBC", rate);
164
165   GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
166
167   return gst_rtp_base_payload_set_outcaps (payload, NULL);
168 }
169
170 static GstFlowReturn
171 gst_rtp_sbc_pay_flush_buffers (GstRtpSBCPay * sbcpay)
172 {
173   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
174   guint available;
175   guint max_payload;
176   GstBuffer *outbuf, *paybuf;
177   guint8 *payload_data;
178   guint frame_count;
179   guint payload_length;
180   struct rtp_payload *payload;
181   GstFlowReturn res;
182
183   if (sbcpay->frame_length == 0) {
184     GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
185     return GST_FLOW_ERROR;
186   }
187
188   do {
189     available = gst_adapter_available (sbcpay->adapter);
190
191     max_payload =
192         gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (sbcpay) -
193         RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
194
195     max_payload = MIN (max_payload, available);
196     frame_count = max_payload / sbcpay->frame_length;
197     payload_length = frame_count * sbcpay->frame_length;
198     if (payload_length == 0)    /* Nothing to send */
199       return GST_FLOW_OK;
200
201     outbuf =
202         gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
203         (sbcpay), RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
204
205     /* get payload */
206     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
207
208     gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_BASE_PAYLOAD_PT (sbcpay));
209
210     /* write header and copy data into payload */
211     payload_data = gst_rtp_buffer_get_payload (&rtp);
212     payload = (struct rtp_payload *) payload_data;
213     memset (payload, 0, sizeof (struct rtp_payload));
214     payload->frame_count = frame_count;
215
216     gst_rtp_buffer_unmap (&rtp);
217
218     paybuf = gst_adapter_take_buffer_fast (sbcpay->adapter, payload_length);
219     gst_rtp_copy_audio_meta (sbcpay, outbuf, paybuf);
220     outbuf = gst_buffer_append (outbuf, paybuf);
221
222     GST_BUFFER_PTS (outbuf) = sbcpay->last_timestamp;
223     GST_BUFFER_DURATION (outbuf) = frame_count * sbcpay->frame_duration;
224     GST_DEBUG_OBJECT (sbcpay, "Pushing %d bytes: %" GST_TIME_FORMAT,
225         payload_length, GST_TIME_ARGS (GST_BUFFER_PTS (outbuf)));
226
227     sbcpay->last_timestamp += frame_count * sbcpay->frame_duration;
228
229     res = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (sbcpay), outbuf);
230
231     /* try to send another RTP buffer if available data exceeds MTU size */
232   } while (res == GST_FLOW_OK);
233
234   return res;
235 }
236
237 static GstFlowReturn
238 gst_rtp_sbc_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
239 {
240   GstRtpSBCPay *sbcpay;
241   guint available;
242
243   /* FIXME check for negotiation */
244
245   sbcpay = GST_RTP_SBC_PAY (payload);
246
247   if (GST_BUFFER_IS_DISCONT (buffer)) {
248     /* Try to flush whatever's left */
249     gst_rtp_sbc_pay_flush_buffers (sbcpay);
250     /* Drop the rest */
251     gst_adapter_flush (sbcpay->adapter,
252         gst_adapter_available (sbcpay->adapter));
253     /* Reset timestamps */
254     sbcpay->last_timestamp = GST_CLOCK_TIME_NONE;
255   }
256
257   if (sbcpay->last_timestamp == GST_CLOCK_TIME_NONE)
258     sbcpay->last_timestamp = GST_BUFFER_PTS (buffer);
259
260   gst_adapter_push (sbcpay->adapter, buffer);
261
262   available = gst_adapter_available (sbcpay->adapter);
263   if (available + RTP_SBC_HEADER_TOTAL >=
264       GST_RTP_BASE_PAYLOAD_MTU (sbcpay) ||
265       (available > (sbcpay->min_frames * sbcpay->frame_length)))
266     return gst_rtp_sbc_pay_flush_buffers (sbcpay);
267
268   return GST_FLOW_OK;
269 }
270
271 static gboolean
272 gst_rtp_sbc_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
273 {
274   GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (payload);
275
276   switch (GST_EVENT_TYPE (event)) {
277     case GST_EVENT_EOS:
278       gst_rtp_sbc_pay_flush_buffers (sbcpay);
279       break;
280     default:
281       break;
282   }
283
284   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
285 }
286
287 static void
288 gst_rtp_sbc_pay_finalize (GObject * object)
289 {
290   GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
291
292   g_object_unref (sbcpay->adapter);
293
294   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
295 }
296
297 static void
298 gst_rtp_sbc_pay_class_init (GstRtpSBCPayClass * klass)
299 {
300   GstRTPBasePayloadClass *payload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
301   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
302   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
303
304   gobject_class->finalize = gst_rtp_sbc_pay_finalize;
305   gobject_class->set_property = gst_rtp_sbc_pay_set_property;
306   gobject_class->get_property = gst_rtp_sbc_pay_get_property;
307
308   payload_class->set_caps = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_caps);
309   payload_class->handle_buffer =
310       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
311   payload_class->sink_event = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_sink_event);
312
313   /* properties */
314   g_object_class_install_property (G_OBJECT_CLASS (klass),
315       PROP_MIN_FRAMES,
316       g_param_spec_int ("min-frames", "minimum frame number",
317           "Minimum quantity of frames to send in one packet "
318           "(-1 for maximum allowed by the mtu)",
319           -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
320
321   gst_element_class_add_static_pad_template (element_class,
322       &gst_rtp_sbc_pay_sink_factory);
323   gst_element_class_add_static_pad_template (element_class,
324       &gst_rtp_sbc_pay_src_factory);
325
326   gst_element_class_set_static_metadata (element_class, "RTP packet payloader",
327       "Codec/Payloader/Network", "Payload SBC audio as RTP packets",
328       "Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>");
329
330   GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
331       "RTP SBC payloader");
332 }
333
334 static void
335 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
336     const GValue * value, GParamSpec * pspec)
337 {
338   GstRtpSBCPay *sbcpay;
339
340   sbcpay = GST_RTP_SBC_PAY (object);
341
342   switch (prop_id) {
343     case PROP_MIN_FRAMES:
344       sbcpay->min_frames = g_value_get_int (value);
345       break;
346     default:
347       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
348       break;
349   }
350 }
351
352 static void
353 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
354     GValue * value, GParamSpec * pspec)
355 {
356   GstRtpSBCPay *sbcpay;
357
358   sbcpay = GST_RTP_SBC_PAY (object);
359
360   switch (prop_id) {
361     case PROP_MIN_FRAMES:
362       g_value_set_int (value, sbcpay->min_frames);
363       break;
364     default:
365       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366       break;
367   }
368 }
369
370 static void
371 gst_rtp_sbc_pay_init (GstRtpSBCPay * self)
372 {
373   self->adapter = gst_adapter_new ();
374   self->frame_length = 0;
375   self->last_timestamp = GST_CLOCK_TIME_NONE;
376
377   self->min_frames = DEFAULT_MIN_FRAMES;
378 }
379
380 gboolean
381 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
382 {
383   return gst_element_register (plugin, "rtpsbcpay", GST_RANK_NONE,
384       GST_TYPE_RTP_SBC_PAY);
385 }