Use new gst_element_class_set_static_metadata()
[platform/upstream/gstreamer.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
29 #include "gstrtpL16depay.h"
30 #include "gstrtpchannels.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
33 #define GST_CAT_DEFAULT (rtpL16depay_debug)
34
35 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("audio/x-raw, "
40         "format = (string) S16BE, "
41         "layout = (string) interleaved, "
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_static_metadata (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   GstAudioInfo *info;
135
136   rtpL16depay = GST_RTP_L16_DEPAY (depayload);
137
138   structure = gst_caps_get_structure (caps, 0);
139
140   payload = 96;
141   gst_structure_get_int (structure, "payload", &payload);
142   switch (payload) {
143     case GST_RTP_PAYLOAD_L16_STEREO:
144       channels = 2;
145       clock_rate = 44100;
146       break;
147     case GST_RTP_PAYLOAD_L16_MONO:
148       channels = 1;
149       clock_rate = 44100;
150       break;
151     default:
152       /* no fixed mapping, we need clock-rate */
153       channels = 0;
154       clock_rate = 0;
155       break;
156   }
157
158   /* caps can overwrite defaults */
159   clock_rate =
160       gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
161   if (clock_rate == 0)
162     goto no_clockrate;
163
164   channels =
165       gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
166   if (channels == 0) {
167     channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
168     if (channels == 0) {
169       /* channels defaults to 1 otherwise */
170       channels = 1;
171     }
172   }
173
174   depayload->clock_rate = clock_rate;
175
176   info = &rtpL16depay->info;
177   gst_audio_info_init (info);
178   info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_S16BE);
179   info->rate = clock_rate;
180   info->channels = channels;
181   info->bpf = (info->finfo->width / 8) * channels;
182
183   /* add channel positions */
184   channel_order = gst_structure_get_string (structure, "channel-order");
185
186   order = gst_rtp_channels_get_by_order (channels, channel_order);
187   rtpL16depay->order = order;
188   if (order) {
189     memcpy (info->position, order->pos,
190         sizeof (GstAudioChannelPosition) * channels);
191     gst_audio_channel_positions_to_valid_order (info->position, info->channels);
192   } else {
193     GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
194         (NULL), ("Unknown channel order '%s' for %d channels",
195             GST_STR_NULL (channel_order), channels));
196     /* create default NONE layout */
197     gst_rtp_channels_create_default (channels, info->position);
198   }
199
200   srccaps = gst_audio_info_to_caps (info);
201   res = gst_pad_set_caps (depayload->srcpad, srccaps);
202   gst_caps_unref (srccaps);
203
204   return res;
205
206   /* ERRORS */
207 no_clockrate:
208   {
209     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
210     return FALSE;
211   }
212 }
213
214 static GstBuffer *
215 gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
216 {
217   GstRtpL16Depay *rtpL16depay;
218   GstBuffer *outbuf;
219   gint payload_len;
220   gboolean marker;
221   GstRTPBuffer rtp = { NULL };
222
223   rtpL16depay = GST_RTP_L16_DEPAY (depayload);
224
225   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
226   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
227
228   if (payload_len <= 0)
229     goto empty_packet;
230
231   GST_DEBUG_OBJECT (rtpL16depay, "got payload of %d bytes", payload_len);
232
233   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
234   marker = gst_rtp_buffer_get_marker (&rtp);
235
236   if (marker) {
237     /* mark talk spurt with DISCONT */
238     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
239   }
240
241   outbuf = gst_buffer_make_writable (outbuf);
242   if (rtpL16depay->order &&
243       !gst_audio_buffer_reorder_channels (outbuf,
244           rtpL16depay->info.finfo->format, rtpL16depay->info.channels,
245           rtpL16depay->info.position, rtpL16depay->order->pos)) {
246     goto reorder_failed;
247   }
248
249   gst_rtp_buffer_unmap (&rtp);
250
251   return outbuf;
252
253   /* ERRORS */
254 empty_packet:
255   {
256     GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
257         ("Empty Payload."), (NULL));
258     gst_rtp_buffer_unmap (&rtp);
259     return NULL;
260   }
261 reorder_failed:
262   {
263     GST_ELEMENT_ERROR (rtpL16depay, STREAM, DECODE,
264         ("Channel reordering failed."), (NULL));
265     gst_rtp_buffer_unmap (&rtp);
266     return NULL;
267   }
268 }
269
270 gboolean
271 gst_rtp_L16_depay_plugin_init (GstPlugin * plugin)
272 {
273   return gst_element_register (plugin, "rtpL16depay",
274       GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY);
275 }