gst/rtp/: Add mappings for multichannel support. Does not completely just work becaus...
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL16pay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim@fluendo.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/audio/audio.h>
27 #include <gst/audio/multichannel.h>
28 #include <gst/rtp/gstrtpbuffer.h>
29
30 #include "gstrtpL16pay.h"
31 #include "gstrtpchannels.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
34 #define GST_CAT_DEFAULT (rtpL16pay_debug)
35
36 /* elementfactory information */
37 static const GstElementDetails gst_rtp_L16_pay_details =
38 GST_ELEMENT_DETAILS ("RTP packet payloader",
39     "Codec/Payloader/Network",
40     "Payload-encode Raw audio into RTP packets (RFC 3551)",
41     "Wim Taymans <wim@fluendo.com>");
42
43 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("audio/x-raw-int, "
48         "endianness = (int) BIG_ENDIAN, "
49         "signed = (boolean) true, "
50         "width = (int) 16, "
51         "depth = (int) 16, "
52         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
53     );
54
55 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
56     GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp, "
60         "media = (string) \"audio\", "
61         "payload = (int) [ 96, 127 ], "
62         "clock-rate = (int) [ 1, MAX ], "
63         "encoding-name = (string) \"L16\", "
64         "channels = (int) [ 1, MAX ];"
65         "application/x-rtp, "
66         "media = (string) \"audio\", "
67         "encoding-name = (string) \"L16\", "
68         "payload = (int) " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
69         "clock-rate = (int) 44100;"
70         "application/x-rtp, "
71         "media = (string) \"audio\", "
72         "encoding-name = (string) \"L16\", "
73         "payload = (int) " GST_RTP_PAYLOAD_L16_MONO_STRING ", "
74         "clock-rate = (int) 44100")
75     );
76
77 static void gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass);
78 static void gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass);
79 static void gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay);
80 static void gst_rtp_L16_pay_finalize (GObject * object);
81
82 static gboolean gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload,
83     GstCaps * caps);
84 static GstFlowReturn gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * pad,
85     GstBuffer * buffer);
86 static GstCaps *gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload,
87     GstPad * pad);
88
89 static GstBaseRTPPayloadClass *parent_class = NULL;
90
91 static GType
92 gst_rtp_L16_pay_get_type (void)
93 {
94   static GType rtpL16pay_type = 0;
95
96   if (!rtpL16pay_type) {
97     static const GTypeInfo rtpL16pay_info = {
98       sizeof (GstRtpL16PayClass),
99       (GBaseInitFunc) gst_rtp_L16_pay_base_init,
100       NULL,
101       (GClassInitFunc) gst_rtp_L16_pay_class_init,
102       NULL,
103       NULL,
104       sizeof (GstRtpL16Pay),
105       0,
106       (GInstanceInitFunc) gst_rtp_L16_pay_init,
107     };
108
109     rtpL16pay_type =
110         g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpL16Pay",
111         &rtpL16pay_info, 0);
112   }
113   return rtpL16pay_type;
114 }
115
116 static void
117 gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass)
118 {
119   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
120
121   gst_element_class_add_pad_template (element_class,
122       gst_static_pad_template_get (&gst_rtp_L16_pay_src_template));
123   gst_element_class_add_pad_template (element_class,
124       gst_static_pad_template_get (&gst_rtp_L16_pay_sink_template));
125
126   gst_element_class_set_details (element_class, &gst_rtp_L16_pay_details);
127 }
128
129 static void
130 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
131 {
132   GObjectClass *gobject_class;
133   GstElementClass *gstelement_class;
134   GstBaseRTPPayloadClass *gstbasertppayload_class;
135
136   gobject_class = (GObjectClass *) klass;
137   gstelement_class = (GstElementClass *) klass;
138   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
139
140   parent_class = g_type_class_peek_parent (klass);
141
142   gobject_class->finalize = gst_rtp_L16_pay_finalize;
143
144   gstbasertppayload_class->set_caps = gst_rtp_L16_pay_setcaps;
145   gstbasertppayload_class->get_caps = gst_rtp_L16_pay_getcaps;
146   gstbasertppayload_class->handle_buffer = gst_rtp_L16_pay_handle_buffer;
147
148   GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
149       "L16 RTP Payloader");
150 }
151
152 static void
153 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
154 {
155   rtpL16pay->adapter = gst_adapter_new ();
156 }
157
158 static void
159 gst_rtp_L16_pay_finalize (GObject * object)
160 {
161   GstRtpL16Pay *rtpL16pay;
162
163   rtpL16pay = GST_RTP_L16_PAY (object);
164
165   g_object_unref (rtpL16pay->adapter);
166   rtpL16pay->adapter = NULL;
167
168   G_OBJECT_CLASS (parent_class)->finalize (object);
169 }
170
171 static gboolean
172 gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
173 {
174   GstRtpL16Pay *rtpL16pay;
175   GstStructure *structure;
176   gint channels, rate;
177   gboolean res;
178   gchar *params;
179   GstAudioChannelPosition *pos;
180   const GstRTPChannelOrder *order;
181
182   rtpL16pay = GST_RTP_L16_PAY (basepayload);
183
184   structure = gst_caps_get_structure (caps, 0);
185
186   /* first parse input caps */
187   if (!gst_structure_get_int (structure, "rate", &rate))
188     goto no_rate;
189
190   if (!gst_structure_get_int (structure, "channels", &channels))
191     goto no_channels;
192
193   /* get the channel order */
194   pos = gst_audio_get_channel_positions (structure);
195   if (pos)
196     order = gst_rtp_channels_get_by_pos (channels, pos);
197   else
198     order = NULL;
199
200   gst_basertppayload_set_options (basepayload, "audio", TRUE, "L16", rate);
201   params = g_strdup_printf ("%d", channels);
202
203   if (!order && channels > 2) {
204     GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
205         (NULL), ("Unknown channel order for %d channels", channels));
206   }
207
208   if (order && order->name) {
209     res = gst_basertppayload_set_outcaps (basepayload,
210         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
211         channels, "channel-order", G_TYPE_STRING, order->name, NULL);
212   } else {
213     res = gst_basertppayload_set_outcaps (basepayload,
214         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
215         channels, NULL);
216   }
217
218   g_free (params);
219   g_free (pos);
220
221   rtpL16pay->rate = rate;
222   rtpL16pay->channels = channels;
223
224   return res;
225
226   /* ERRORS */
227 no_rate:
228   {
229     GST_DEBUG_OBJECT (rtpL16pay, "no rate given");
230     return FALSE;
231   }
232 no_channels:
233   {
234     GST_DEBUG_OBJECT (rtpL16pay, "no channels given");
235     return FALSE;
236   }
237 }
238
239 static GstFlowReturn
240 gst_rtp_L16_pay_flush (GstRtpL16Pay * rtpL16pay, guint len)
241 {
242   GstBuffer *outbuf;
243   guint8 *payload;
244   GstFlowReturn ret;
245   guint samples;
246   GstClockTime duration;
247
248   /* now alloc output buffer */
249   outbuf = gst_rtp_buffer_new_allocate (len, 0, 0);
250
251   /* get payload, this is now writable */
252   payload = gst_rtp_buffer_get_payload (outbuf);
253
254   /* copy and flush data out of adapter into the RTP payload */
255   gst_adapter_copy (rtpL16pay->adapter, payload, 0, len);
256   gst_adapter_flush (rtpL16pay->adapter, len);
257
258   samples = len / (2 * rtpL16pay->channels);
259   duration = gst_util_uint64_scale_int (samples, GST_SECOND, rtpL16pay->rate);
260
261   GST_BUFFER_TIMESTAMP (outbuf) = rtpL16pay->first_ts;
262   GST_BUFFER_DURATION (outbuf) = duration;
263
264   /* increase count (in ts) of data pushed to basertppayload */
265   if (GST_CLOCK_TIME_IS_VALID (rtpL16pay->first_ts))
266     rtpL16pay->first_ts += duration;
267
268   ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpL16pay), outbuf);
269
270   return ret;
271 }
272
273 static GstFlowReturn
274 gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * basepayload,
275     GstBuffer * buffer)
276 {
277   GstRtpL16Pay *rtpL16pay;
278   GstFlowReturn ret = GST_FLOW_OK;
279   guint payload_len;
280   GstClockTime timestamp;
281   guint mtu, avail;
282
283   rtpL16pay = GST_RTP_L16_PAY (basepayload);
284   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpL16pay);
285
286   timestamp = GST_BUFFER_TIMESTAMP (buffer);
287
288   if (GST_BUFFER_IS_DISCONT (buffer))
289     gst_adapter_clear (rtpL16pay->adapter);
290
291   avail = gst_adapter_available (rtpL16pay->adapter);
292   if (avail == 0) {
293     rtpL16pay->first_ts = timestamp;
294   }
295
296   /* push buffer in adapter */
297   gst_adapter_push (rtpL16pay->adapter, buffer);
298
299   /* get payload len for MTU */
300   payload_len = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
301
302   /* flush complete MTU while we have enough data in the adapter */
303   while (avail >= payload_len) {
304     /* flush payload_len bytes */
305     ret = gst_rtp_L16_pay_flush (rtpL16pay, payload_len);
306     if (ret != GST_FLOW_OK)
307       break;
308
309     avail = gst_adapter_available (rtpL16pay->adapter);
310   }
311   return ret;
312 }
313
314 static GstCaps *
315 gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
316 {
317   GstCaps *otherpadcaps;
318   GstCaps *caps;
319
320   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
321   caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
322
323   if (otherpadcaps) {
324     if (!gst_caps_is_empty (otherpadcaps)) {
325       GstStructure *structure;
326       gint channels;
327       gint pt;
328       gint rate;
329
330       structure = gst_caps_get_structure (otherpadcaps, 0);
331
332       if (gst_structure_get_int (structure, "channels", &channels)) {
333         gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
334       } else if (gst_structure_get_int (structure, "payload", &pt)) {
335         if (pt == 10)
336           gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
337         else if (pt == 11)
338           gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
339       }
340
341       if (gst_structure_get_int (structure, "clock-rate", &rate)) {
342         gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
343       } else if (gst_structure_get_int (structure, "payload", &pt)) {
344         if (pt == 10 || pt == 11)
345           gst_caps_set_simple (caps, "rate", G_TYPE_INT, 44100, NULL);
346       }
347
348     }
349     gst_caps_unref (otherpadcaps);
350   }
351
352   return caps;
353 }
354
355 gboolean
356 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
357 {
358   return gst_element_register (plugin, "rtpL16pay",
359       GST_RANK_NONE, GST_TYPE_RTP_L16_PAY);
360 }