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