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