Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL16depay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.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 #include <stdlib.h>
26
27 #include <gst/audio/audio.h>
28 #include <gst/audio/multichannel.h>
29
30 #include "gstrtpL16depay.h"
31 #include "gstrtpchannels.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
34 #define GST_CAT_DEFAULT (rtpL16depay_debug)
35
36 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src",
38     GST_PAD_SRC,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("audio/x-raw, "
41         "format = (string) S16_BE, "
42         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
43     );
44
45 static GstStaticPadTemplate gst_rtp_L16_depay_sink_template =
46     GST_STATIC_PAD_TEMPLATE ("sink",
47     GST_PAD_SINK,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp, "
50         "media = (string) \"audio\", "
51         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52         "clock-rate = (int) [ 1, MAX ], "
53         /* "channels = (int) [1, MAX]"  */
54         /* "emphasis = (string) ANY" */
55         /* "channel-order = (string) ANY" */
56         "encoding-name = (string) \"L16\";"
57         "application/x-rtp, "
58         "media = (string) \"audio\", "
59         "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
60         GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) [ 1, MAX ]"
61         /* "channels = (int) [1, MAX]" */
62         /* "emphasis = (string) ANY" */
63         /* "channel-order = (string) ANY" */
64     )
65     );
66
67 #define gst_rtp_L16_depay_parent_class parent_class
68 G_DEFINE_TYPE (GstRtpL16Depay, gst_rtp_L16_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
69
70 static gboolean gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload,
71     GstCaps * caps);
72 static GstBuffer *gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload,
73     GstBuffer * buf);
74
75 static void
76 gst_rtp_L16_depay_class_init (GstRtpL16DepayClass * klass)
77 {
78   GstElementClass *gstelement_class;
79   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
80
81   gstelement_class = (GstElementClass *) klass;
82   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
83
84   gstrtpbasedepayload_class->set_caps = gst_rtp_L16_depay_setcaps;
85   gstrtpbasedepayload_class->process = gst_rtp_L16_depay_process;
86
87   gst_element_class_add_pad_template (gstelement_class,
88       gst_static_pad_template_get (&gst_rtp_L16_depay_src_template));
89   gst_element_class_add_pad_template (gstelement_class,
90       gst_static_pad_template_get (&gst_rtp_L16_depay_sink_template));
91
92   gst_element_class_set_details_simple (gstelement_class,
93       "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
94       "Extracts raw audio from RTP packets",
95       "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>");
96
97   GST_DEBUG_CATEGORY_INIT (rtpL16depay_debug, "rtpL16depay", 0,
98       "Raw Audio RTP Depayloader");
99 }
100
101 static void
102 gst_rtp_L16_depay_init (GstRtpL16Depay * rtpL16depay)
103 {
104   /* needed because of GST_BOILERPLATE */
105 }
106
107 static gint
108 gst_rtp_L16_depay_parse_int (GstStructure * structure, const gchar * field,
109     gint def)
110 {
111   const gchar *str;
112   gint res;
113
114   if ((str = gst_structure_get_string (structure, field)))
115     return atoi (str);
116
117   if (gst_structure_get_int (structure, field, &res))
118     return res;
119
120   return def;
121 }
122
123 static gboolean
124 gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
125 {
126   GstStructure *structure;
127   GstRtpL16Depay *rtpL16depay;
128   gint clock_rate, payload;
129   gint channels;
130   GstCaps *srccaps;
131   gboolean res;
132   const gchar *channel_order;
133   const GstRTPChannelOrder *order;
134
135   rtpL16depay = GST_RTP_L16_DEPAY (depayload);
136
137   structure = gst_caps_get_structure (caps, 0);
138
139   payload = 96;
140   gst_structure_get_int (structure, "payload", &payload);
141   switch (payload) {
142     case GST_RTP_PAYLOAD_L16_STEREO:
143       channels = 2;
144       clock_rate = 44100;
145       break;
146     case GST_RTP_PAYLOAD_L16_MONO:
147       channels = 1;
148       clock_rate = 44100;
149       break;
150     default:
151       /* no fixed mapping, we need clock-rate */
152       channels = 0;
153       clock_rate = 0;
154       break;
155   }
156
157   /* caps can overwrite defaults */
158   clock_rate =
159       gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
160   if (clock_rate == 0)
161     goto no_clockrate;
162
163   channels =
164       gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
165   if (channels == 0) {
166     channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
167     if (channels == 0) {
168       /* channels defaults to 1 otherwise */
169       channels = 1;
170     }
171   }
172
173   depayload->clock_rate = clock_rate;
174   rtpL16depay->rate = clock_rate;
175   rtpL16depay->channels = channels;
176
177   srccaps = gst_caps_new_simple ("audio/x-raw",
178       "format", G_TYPE_STRING, "S16_BE",
179       "rate", G_TYPE_INT, clock_rate, "channels", G_TYPE_INT, channels, NULL);
180
181   /* add channel positions */
182   channel_order = gst_structure_get_string (structure, "channel-order");
183
184   order = gst_rtp_channels_get_by_order (channels, channel_order);
185   if (order) {
186     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0),
187         order->pos);
188   } else {
189     GstAudioChannelPosition *pos;
190
191     GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
192         (NULL), ("Unknown channel order '%s' for %d channels",
193             GST_STR_NULL (channel_order), channels));
194     /* create default NONE layout */
195     pos = gst_rtp_channels_create_default (channels);
196     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0), pos);
197     g_free (pos);
198   }
199
200   res = gst_pad_set_caps (depayload->srcpad, srccaps);
201   gst_caps_unref (srccaps);
202
203   return res;
204
205   /* ERRORS */
206 no_clockrate:
207   {
208     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
209     return FALSE;
210   }
211 }
212
213 static GstBuffer *
214 gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
215 {
216   GstRtpL16Depay *rtpL16depay;
217   GstBuffer *outbuf;
218   gint payload_len;
219   gboolean marker;
220   GstRTPBuffer rtp = { NULL };
221
222   rtpL16depay = GST_RTP_L16_DEPAY (depayload);
223
224   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
225   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
226
227   if (payload_len <= 0)
228     goto empty_packet;
229
230   GST_DEBUG_OBJECT (rtpL16depay, "got payload of %d bytes", payload_len);
231
232   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
233   marker = gst_rtp_buffer_get_marker (&rtp);
234
235   if (marker) {
236     /* mark talk spurt with DISCONT */
237     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
238   }
239
240   gst_rtp_buffer_unmap (&rtp);
241
242   return outbuf;
243
244   /* ERRORS */
245 empty_packet:
246   {
247     GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
248         ("Empty Payload."), (NULL));
249     gst_rtp_buffer_unmap (&rtp);
250     return NULL;
251   }
252 }
253
254 gboolean
255 gst_rtp_L16_depay_plugin_init (GstPlugin * plugin)
256 {
257   return gst_element_register (plugin, "rtpL16depay",
258       GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY);
259 }