rtpvorbisdepay: remove dead code
[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
161   gst_rtp_base_payload_set_options (payload, "audio", TRUE, "SBC", rate);
162
163   GST_DEBUG_OBJECT (payload, "calculated frame length: %d ", frame_len);
164
165   return gst_rtp_base_payload_set_outcaps (payload, NULL);
166 }
167
168 static GstFlowReturn
169 gst_rtp_sbc_pay_flush_buffers (GstRtpSBCPay * sbcpay)
170 {
171   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
172   guint available;
173   guint max_payload;
174   GstBuffer *outbuf, *paybuf;
175   guint8 *payload_data;
176   guint frame_count;
177   guint payload_length;
178   struct rtp_payload *payload;
179
180   if (sbcpay->frame_length == 0) {
181     GST_ERROR_OBJECT (sbcpay, "Frame length is 0");
182     return GST_FLOW_ERROR;
183   }
184
185   available = gst_adapter_available (sbcpay->adapter);
186
187   max_payload =
188       gst_rtp_buffer_calc_payload_len (GST_RTP_BASE_PAYLOAD_MTU (sbcpay) -
189       RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
190
191   max_payload = MIN (max_payload, available);
192   frame_count = max_payload / sbcpay->frame_length;
193   payload_length = frame_count * sbcpay->frame_length;
194   if (payload_length == 0)      /* Nothing to send */
195     return GST_FLOW_OK;
196
197   outbuf = gst_rtp_buffer_new_allocate (RTP_SBC_PAYLOAD_HEADER_SIZE, 0, 0);
198
199   /* get payload */
200   gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
201
202   gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_BASE_PAYLOAD_PT (sbcpay));
203
204   /* write header and copy data into payload */
205   payload_data = gst_rtp_buffer_get_payload (&rtp);
206   payload = (struct rtp_payload *) payload_data;
207   memset (payload, 0, sizeof (struct rtp_payload));
208   payload->frame_count = frame_count;
209
210   gst_rtp_buffer_unmap (&rtp);
211
212   paybuf = gst_adapter_take_buffer_fast (sbcpay->adapter, payload_length);
213   gst_rtp_copy_meta (GST_ELEMENT_CAST (sbcpay), outbuf, paybuf,
214       g_quark_from_static_string (GST_META_TAG_AUDIO_STR));
215   outbuf = gst_buffer_append (outbuf, paybuf);
216
217   /* FIXME: what about duration? */
218   GST_BUFFER_PTS (outbuf) = sbcpay->timestamp;
219   GST_DEBUG_OBJECT (sbcpay, "Pushing %d bytes", payload_length);
220
221   return gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (sbcpay), outbuf);
222 }
223
224 static GstFlowReturn
225 gst_rtp_sbc_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
226 {
227   GstRtpSBCPay *sbcpay;
228   guint available;
229
230   /* FIXME check for negotiation */
231
232   sbcpay = GST_RTP_SBC_PAY (payload);
233   sbcpay->timestamp = GST_BUFFER_PTS (buffer);
234
235   gst_adapter_push (sbcpay->adapter, buffer);
236
237   available = gst_adapter_available (sbcpay->adapter);
238   if (available + RTP_SBC_HEADER_TOTAL >=
239       GST_RTP_BASE_PAYLOAD_MTU (sbcpay) ||
240       (available > (sbcpay->min_frames * sbcpay->frame_length)))
241     return gst_rtp_sbc_pay_flush_buffers (sbcpay);
242
243   return GST_FLOW_OK;
244 }
245
246 static gboolean
247 gst_rtp_sbc_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
248 {
249   GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (payload);
250
251   switch (GST_EVENT_TYPE (event)) {
252     case GST_EVENT_EOS:
253       gst_rtp_sbc_pay_flush_buffers (sbcpay);
254       break;
255     default:
256       break;
257   }
258
259   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
260 }
261
262 static void
263 gst_rtp_sbc_pay_finalize (GObject * object)
264 {
265   GstRtpSBCPay *sbcpay = GST_RTP_SBC_PAY (object);
266
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   GstRTPBasePayloadClass *payload_class = GST_RTP_BASE_PAYLOAD_CLASS (klass);
276   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
277   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
278
279   gobject_class->finalize = gst_rtp_sbc_pay_finalize;
280   gobject_class->set_property = gst_rtp_sbc_pay_set_property;
281   gobject_class->get_property = gst_rtp_sbc_pay_get_property;
282
283   payload_class->set_caps = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_set_caps);
284   payload_class->handle_buffer =
285       GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_handle_buffer);
286   payload_class->sink_event = GST_DEBUG_FUNCPTR (gst_rtp_sbc_pay_sink_event);
287
288   /* properties */
289   g_object_class_install_property (G_OBJECT_CLASS (klass),
290       PROP_MIN_FRAMES,
291       g_param_spec_int ("min-frames", "minimum frame number",
292           "Minimum quantity of frames to send in one packet "
293           "(-1 for maximum allowed by the mtu)",
294           -1, G_MAXINT, DEFAULT_MIN_FRAMES, G_PARAM_READWRITE));
295
296   gst_element_class_add_static_pad_template (element_class,
297       &gst_rtp_sbc_pay_sink_factory);
298   gst_element_class_add_static_pad_template (element_class,
299       &gst_rtp_sbc_pay_src_factory);
300
301   gst_element_class_set_static_metadata (element_class, "RTP packet payloader",
302       "Codec/Payloader/Network", "Payload SBC audio as RTP packets",
303       "Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>");
304
305   GST_DEBUG_CATEGORY_INIT (gst_rtp_sbc_pay_debug, "rtpsbcpay", 0,
306       "RTP SBC payloader");
307 }
308
309 static void
310 gst_rtp_sbc_pay_set_property (GObject * object, guint prop_id,
311     const GValue * value, GParamSpec * pspec)
312 {
313   GstRtpSBCPay *sbcpay;
314
315   sbcpay = GST_RTP_SBC_PAY (object);
316
317   switch (prop_id) {
318     case PROP_MIN_FRAMES:
319       sbcpay->min_frames = g_value_get_int (value);
320       break;
321     default:
322       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323       break;
324   }
325 }
326
327 static void
328 gst_rtp_sbc_pay_get_property (GObject * object, guint prop_id,
329     GValue * value, GParamSpec * pspec)
330 {
331   GstRtpSBCPay *sbcpay;
332
333   sbcpay = GST_RTP_SBC_PAY (object);
334
335   switch (prop_id) {
336     case PROP_MIN_FRAMES:
337       g_value_set_int (value, sbcpay->min_frames);
338       break;
339     default:
340       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341       break;
342   }
343 }
344
345 static void
346 gst_rtp_sbc_pay_init (GstRtpSBCPay * self)
347 {
348   self->adapter = gst_adapter_new ();
349   self->frame_length = 0;
350   self->timestamp = 0;
351
352   self->min_frames = DEFAULT_MIN_FRAMES;
353 }
354
355 gboolean
356 gst_rtp_sbc_pay_plugin_init (GstPlugin * plugin)
357 {
358   return gst_element_register (plugin, "rtpsbcpay", GST_RANK_NONE,
359       GST_TYPE_RTP_SBC_PAY);
360 }