docs: update example pipelines in element docs
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL24depay.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-rtpL24depay
22  * @see_also: rtpL24pay
23  *
24  * Extract raw audio from RTP packets according to RFC 3190, section 4.
25  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3190.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)L24, encoding-params=(string)1, channels=(int)1, payload=(int)96' ! rtpL24depay ! pulsesink
31  * ]| This example pipeline will depayload an RTP raw audio stream. Refer to
32  * the rtpL24pay 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 "gstrtpL24depay.h"
46 #include "gstrtpchannels.h"
47
48 GST_DEBUG_CATEGORY_STATIC (rtpL24depay_debug);
49 #define GST_CAT_DEFAULT (rtpL24depay_debug)
50
51 static GstStaticPadTemplate gst_rtp_L24_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) S24BE, "
57         "layout = (string) interleaved, "
58         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
59     );
60
61 static GstStaticPadTemplate gst_rtp_L24_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         "encoding-name = (string) \"L24\"")
68     );
69
70 #define gst_rtp_L24_depay_parent_class parent_class
71 G_DEFINE_TYPE (GstRtpL24Depay, gst_rtp_L24_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
72
73 static gboolean gst_rtp_L24_depay_setcaps (GstRTPBaseDepayload * depayload,
74     GstCaps * caps);
75 static GstBuffer *gst_rtp_L24_depay_process (GstRTPBaseDepayload * depayload,
76     GstBuffer * buf);
77
78 static void
79 gst_rtp_L24_depay_class_init (GstRtpL24DepayClass * klass)
80 {
81   GstElementClass *gstelement_class;
82   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
83
84   gstelement_class = (GstElementClass *) klass;
85   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
86
87   gstrtpbasedepayload_class->set_caps = gst_rtp_L24_depay_setcaps;
88   gstrtpbasedepayload_class->process = gst_rtp_L24_depay_process;
89
90   gst_element_class_add_pad_template (gstelement_class,
91       gst_static_pad_template_get (&gst_rtp_L24_depay_src_template));
92   gst_element_class_add_pad_template (gstelement_class,
93       gst_static_pad_template_get (&gst_rtp_L24_depay_sink_template));
94
95   gst_element_class_set_static_metadata (gstelement_class,
96       "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
97       "Extracts raw 24-bit audio from RTP packets",
98       "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>,"
99       "David Holroyd <dave@badgers-in-foil.co.uk>");
100
101   GST_DEBUG_CATEGORY_INIT (rtpL24depay_debug, "rtpL24depay", 0,
102       "Raw Audio RTP Depayloader");
103 }
104
105 static void
106 gst_rtp_L24_depay_init (GstRtpL24Depay * rtpL24depay)
107 {
108 }
109
110 static gint
111 gst_rtp_L24_depay_parse_int (GstStructure * structure, const gchar * field,
112     gint def)
113 {
114   const gchar *str;
115   gint res;
116
117   if ((str = gst_structure_get_string (structure, field)))
118     return atoi (str);
119
120   if (gst_structure_get_int (structure, field, &res))
121     return res;
122
123   return def;
124 }
125
126 static gboolean
127 gst_rtp_L24_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
128 {
129   GstStructure *structure;
130   GstRtpL24Depay *rtpL24depay;
131   gint clock_rate, payload;
132   gint channels;
133   GstCaps *srccaps;
134   gboolean res;
135   const gchar *channel_order;
136   const GstRTPChannelOrder *order;
137   GstAudioInfo *info;
138
139   rtpL24depay = GST_RTP_L24_DEPAY (depayload);
140
141   structure = gst_caps_get_structure (caps, 0);
142
143   payload = 96;
144   gst_structure_get_int (structure, "payload", &payload);
145   /* no fixed mapping, we need clock-rate */
146   channels = 0;
147   clock_rate = 0;
148
149   /* caps can overwrite defaults */
150   clock_rate =
151       gst_rtp_L24_depay_parse_int (structure, "clock-rate", clock_rate);
152   if (clock_rate == 0)
153     goto no_clockrate;
154
155   channels =
156       gst_rtp_L24_depay_parse_int (structure, "encoding-params", channels);
157   if (channels == 0) {
158     channels = gst_rtp_L24_depay_parse_int (structure, "channels", channels);
159     if (channels == 0) {
160       /* channels defaults to 1 otherwise */
161       channels = 1;
162     }
163   }
164
165   depayload->clock_rate = clock_rate;
166
167   info = &rtpL24depay->info;
168   gst_audio_info_init (info);
169   info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_S24BE);
170   info->rate = clock_rate;
171   info->channels = channels;
172   info->bpf = (info->finfo->width / 8) * channels;
173
174   /* add channel positions */
175   channel_order = gst_structure_get_string (structure, "channel-order");
176
177   order = gst_rtp_channels_get_by_order (channels, channel_order);
178   rtpL24depay->order = order;
179   if (order) {
180     memcpy (info->position, order->pos,
181         sizeof (GstAudioChannelPosition) * channels);
182     gst_audio_channel_positions_to_valid_order (info->position, info->channels);
183   } else {
184     GST_ELEMENT_WARNING (rtpL24depay, STREAM, DECODE,
185         (NULL), ("Unknown channel order '%s' for %d channels",
186             GST_STR_NULL (channel_order), channels));
187     /* create default NONE layout */
188     gst_rtp_channels_create_default (channels, info->position);
189   }
190
191   srccaps = gst_audio_info_to_caps (info);
192   res = gst_pad_set_caps (depayload->srcpad, srccaps);
193   gst_caps_unref (srccaps);
194
195   return res;
196
197   /* ERRORS */
198 no_clockrate:
199   {
200     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
201     return FALSE;
202   }
203 }
204
205 static GstBuffer *
206 gst_rtp_L24_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
207 {
208   GstRtpL24Depay *rtpL24depay;
209   GstBuffer *outbuf;
210   gint payload_len;
211   gboolean marker;
212   GstRTPBuffer rtp = { NULL };
213
214   rtpL24depay = GST_RTP_L24_DEPAY (depayload);
215
216   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
217   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
218
219   if (payload_len <= 0)
220     goto empty_packet;
221
222   GST_DEBUG_OBJECT (rtpL24depay, "got payload of %d bytes", payload_len);
223
224   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
225   marker = gst_rtp_buffer_get_marker (&rtp);
226
227   if (marker) {
228     /* mark talk spurt with RESYNC */
229     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
230   }
231
232   outbuf = gst_buffer_make_writable (outbuf);
233   if (rtpL24depay->order &&
234       !gst_audio_buffer_reorder_channels (outbuf,
235           rtpL24depay->info.finfo->format, rtpL24depay->info.channels,
236           rtpL24depay->info.position, rtpL24depay->order->pos)) {
237     goto reorder_failed;
238   }
239
240   gst_rtp_buffer_unmap (&rtp);
241
242   return outbuf;
243
244   /* ERRORS */
245 empty_packet:
246   {
247     GST_ELEMENT_WARNING (rtpL24depay, STREAM, DECODE,
248         ("Empty Payload."), (NULL));
249     gst_rtp_buffer_unmap (&rtp);
250     return NULL;
251   }
252 reorder_failed:
253   {
254     GST_ELEMENT_ERROR (rtpL24depay, STREAM, DECODE,
255         ("Channel reordering failed."), (NULL));
256     gst_rtp_buffer_unmap (&rtp);
257     return NULL;
258   }
259 }
260
261 gboolean
262 gst_rtp_L24_depay_plugin_init (GstPlugin * plugin)
263 {
264   return gst_element_register (plugin, "rtpL24depay",
265       GST_RANK_SECONDARY, GST_TYPE_RTP_L24_DEPAY);
266 }