jpegpay: turn some errors into warnings
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-rtpL16depay
22  * @see_also: rtpL16pay
23  *
24  * Extract raw audio from RTP packets according to RFC 3551.
25  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3551.txt
26  *
27  * <refsect2>
28  * <title>Example pipeline</title>
29  * |[
30  * gst-launch udpsrc caps='application/x-rtp, media=(string)audio, clock-rate=(int)44100, encoding-name=(string)L16, encoding-params=(string)1, channels=(int)1, payload=(int)96' ! rtpL16depay ! pulsesink
31  * ]| This example pipeline will depayload an RTP raw audio stream. Refer to
32  * the rtpL16pay example to create the RTP stream.
33  * </refsect2>
34  *
35  * Last reviewed on 2013-04-25 (1.1.0)
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <string.h>
43 #include <stdlib.h>
44
45 #include <gst/audio/audio.h>
46
47 #include "gstrtpL16depay.h"
48 #include "gstrtpchannels.h"
49
50 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
51 #define GST_CAT_DEFAULT (rtpL16depay_debug)
52
53 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
54 GST_STATIC_PAD_TEMPLATE ("src",
55     GST_PAD_SRC,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("audio/x-raw, "
58         "format = (string) S16BE, "
59         "layout = (string) interleaved, "
60         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
61     );
62
63 static GstStaticPadTemplate gst_rtp_L16_depay_sink_template =
64     GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("application/x-rtp, "
68         "media = (string) \"audio\", " "clock-rate = (int) [ 1, MAX ], "
69         /* "channels = (int) [1, MAX]"  */
70         /* "emphasis = (string) ANY" */
71         /* "channel-order = (string) ANY" */
72         "encoding-name = (string) \"L16\";"
73         "application/x-rtp, "
74         "media = (string) \"audio\", "
75         "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
76         GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) [ 1, MAX ]"
77         /* "channels = (int) [1, MAX]" */
78         /* "emphasis = (string) ANY" */
79         /* "channel-order = (string) ANY" */
80     )
81     );
82
83 #define gst_rtp_L16_depay_parent_class parent_class
84 G_DEFINE_TYPE (GstRtpL16Depay, gst_rtp_L16_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
85
86 static gboolean gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload,
87     GstCaps * caps);
88 static GstBuffer *gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload,
89     GstBuffer * buf);
90
91 static void
92 gst_rtp_L16_depay_class_init (GstRtpL16DepayClass * klass)
93 {
94   GstElementClass *gstelement_class;
95   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
96
97   gstelement_class = (GstElementClass *) klass;
98   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
99
100   gstrtpbasedepayload_class->set_caps = gst_rtp_L16_depay_setcaps;
101   gstrtpbasedepayload_class->process = gst_rtp_L16_depay_process;
102
103   gst_element_class_add_pad_template (gstelement_class,
104       gst_static_pad_template_get (&gst_rtp_L16_depay_src_template));
105   gst_element_class_add_pad_template (gstelement_class,
106       gst_static_pad_template_get (&gst_rtp_L16_depay_sink_template));
107
108   gst_element_class_set_static_metadata (gstelement_class,
109       "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
110       "Extracts raw audio from RTP packets",
111       "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>");
112
113   GST_DEBUG_CATEGORY_INIT (rtpL16depay_debug, "rtpL16depay", 0,
114       "Raw Audio RTP Depayloader");
115 }
116
117 static void
118 gst_rtp_L16_depay_init (GstRtpL16Depay * rtpL16depay)
119 {
120 }
121
122 static gint
123 gst_rtp_L16_depay_parse_int (GstStructure * structure, const gchar * field,
124     gint def)
125 {
126   const gchar *str;
127   gint res;
128
129   if ((str = gst_structure_get_string (structure, field)))
130     return atoi (str);
131
132   if (gst_structure_get_int (structure, field, &res))
133     return res;
134
135   return def;
136 }
137
138 static gboolean
139 gst_rtp_L16_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
140 {
141   GstStructure *structure;
142   GstRtpL16Depay *rtpL16depay;
143   gint clock_rate, payload;
144   gint channels;
145   GstCaps *srccaps;
146   gboolean res;
147   const gchar *channel_order;
148   const GstRTPChannelOrder *order;
149   GstAudioInfo *info;
150
151   rtpL16depay = GST_RTP_L16_DEPAY (depayload);
152
153   structure = gst_caps_get_structure (caps, 0);
154
155   payload = 96;
156   gst_structure_get_int (structure, "payload", &payload);
157   switch (payload) {
158     case GST_RTP_PAYLOAD_L16_STEREO:
159       channels = 2;
160       clock_rate = 44100;
161       break;
162     case GST_RTP_PAYLOAD_L16_MONO:
163       channels = 1;
164       clock_rate = 44100;
165       break;
166     default:
167       /* no fixed mapping, we need clock-rate */
168       channels = 0;
169       clock_rate = 0;
170       break;
171   }
172
173   /* caps can overwrite defaults */
174   clock_rate =
175       gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
176   if (clock_rate == 0)
177     goto no_clockrate;
178
179   channels =
180       gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
181   if (channels == 0) {
182     channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
183     if (channels == 0) {
184       /* channels defaults to 1 otherwise */
185       channels = 1;
186     }
187   }
188
189   depayload->clock_rate = clock_rate;
190
191   info = &rtpL16depay->info;
192   gst_audio_info_init (info);
193   info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_S16BE);
194   info->rate = clock_rate;
195   info->channels = channels;
196   info->bpf = (info->finfo->width / 8) * channels;
197
198   /* add channel positions */
199   channel_order = gst_structure_get_string (structure, "channel-order");
200
201   order = gst_rtp_channels_get_by_order (channels, channel_order);
202   rtpL16depay->order = order;
203   if (order) {
204     memcpy (info->position, order->pos,
205         sizeof (GstAudioChannelPosition) * channels);
206     gst_audio_channel_positions_to_valid_order (info->position, info->channels);
207   } else {
208     GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
209         (NULL), ("Unknown channel order '%s' for %d channels",
210             GST_STR_NULL (channel_order), channels));
211     /* create default NONE layout */
212     gst_rtp_channels_create_default (channels, info->position);
213   }
214
215   srccaps = gst_audio_info_to_caps (info);
216   res = gst_pad_set_caps (depayload->srcpad, srccaps);
217   gst_caps_unref (srccaps);
218
219   return res;
220
221   /* ERRORS */
222 no_clockrate:
223   {
224     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
225     return FALSE;
226   }
227 }
228
229 static GstBuffer *
230 gst_rtp_L16_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
231 {
232   GstRtpL16Depay *rtpL16depay;
233   GstBuffer *outbuf;
234   gint payload_len;
235   gboolean marker;
236   GstRTPBuffer rtp = { NULL };
237
238   rtpL16depay = GST_RTP_L16_DEPAY (depayload);
239
240   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
241   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
242
243   if (payload_len <= 0)
244     goto empty_packet;
245
246   GST_DEBUG_OBJECT (rtpL16depay, "got payload of %d bytes", payload_len);
247
248   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
249   marker = gst_rtp_buffer_get_marker (&rtp);
250
251   if (marker) {
252     /* mark talk spurt with RESYNC */
253     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
254   }
255
256   outbuf = gst_buffer_make_writable (outbuf);
257   if (rtpL16depay->order &&
258       !gst_audio_buffer_reorder_channels (outbuf,
259           rtpL16depay->info.finfo->format, rtpL16depay->info.channels,
260           rtpL16depay->info.position, rtpL16depay->order->pos)) {
261     goto reorder_failed;
262   }
263
264   gst_rtp_buffer_unmap (&rtp);
265
266   return outbuf;
267
268   /* ERRORS */
269 empty_packet:
270   {
271     GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
272         ("Empty Payload."), (NULL));
273     gst_rtp_buffer_unmap (&rtp);
274     return NULL;
275   }
276 reorder_failed:
277   {
278     GST_ELEMENT_ERROR (rtpL16depay, STREAM, DECODE,
279         ("Channel reordering failed."), (NULL));
280     gst_rtp_buffer_unmap (&rtp);
281     return NULL;
282   }
283 }
284
285 gboolean
286 gst_rtp_L16_depay_plugin_init (GstPlugin * plugin)
287 {
288   return gst_element_register (plugin, "rtpL16depay",
289       GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY);
290 }