• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  * Copyright (C) <2015> GE Intelligent Platforms Embedded Systems, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  * SECTION:element-rtpL8depay
23  * @see_also: rtpL8pay
24  *
25  * Extract raw audio from RTP packets according to RFC 3551.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3551.txt
27  *
28  * ## Example pipeline
29  *
30  * |[
31  * gst-launch udpsrc caps='application/x-rtp, media=(string)audio, clock-rate=(int)44100, encoding-name=(string)L8, encoding-params=(string)1, channels=(int)1, payload=(int)96' ! rtpL8depay ! pulsesink
32  * ]| This example pipeline will depayload an RTP raw audio stream. Refer to
33  * the rtpL8pay example to create the RTP stream.
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 "gstrtpelements.h"
46 #include "gstrtpL8depay.h"
47 #include "gstrtpchannels.h"
48 
49 GST_DEBUG_CATEGORY_STATIC (rtpL8depay_debug);
50 #define GST_CAT_DEFAULT (rtpL8depay_debug)
51 
52 static GstStaticPadTemplate gst_rtp_L8_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) U8, "
58         "layout = (string) interleaved, "
59         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
60     );
61 
62 static GstStaticPadTemplate gst_rtp_L8_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         /* "channels = (int) [1, MAX]"  */
69         /* "emphasis = (string) ANY" */
70         /* "channel-order = (string) ANY" */
71         "encoding-name = (string) L8;")
72     );
73 
74 #define gst_rtp_L8_depay_parent_class parent_class
75 G_DEFINE_TYPE (GstRtpL8Depay, gst_rtp_L8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
76 
77 
78 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpL8depay, "rtpL8depay",
79     GST_RANK_SECONDARY, GST_TYPE_RTP_L8_DEPAY, rtp_element_init (plugin));
80 
81 static gboolean gst_rtp_L8_depay_setcaps (GstRTPBaseDepayload * depayload,
82     GstCaps * caps);
83 static GstBuffer *gst_rtp_L8_depay_process (GstRTPBaseDepayload * depayload,
84     GstBuffer * buf);
85 
86 static void
gst_rtp_L8_depay_class_init(GstRtpL8DepayClass * klass)87 gst_rtp_L8_depay_class_init (GstRtpL8DepayClass * klass)
88 {
89   GstElementClass *gstelement_class;
90   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
91 
92   gstelement_class = (GstElementClass *) klass;
93   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
94 
95   gstrtpbasedepayload_class->set_caps = gst_rtp_L8_depay_setcaps;
96   gstrtpbasedepayload_class->process = gst_rtp_L8_depay_process;
97 
98   gst_element_class_add_pad_template (gstelement_class,
99       gst_static_pad_template_get (&gst_rtp_L8_depay_src_template));
100   gst_element_class_add_pad_template (gstelement_class,
101       gst_static_pad_template_get (&gst_rtp_L8_depay_sink_template));
102 
103   gst_element_class_set_static_metadata (gstelement_class,
104       "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
105       "Extracts raw audio from RTP packets",
106       "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>, "
107       "GE Intelligent Platforms Embedded Systems, Inc.");
108 
109   GST_DEBUG_CATEGORY_INIT (rtpL8depay_debug, "rtpL8depay", 0,
110       "Raw Audio RTP Depayloader");
111 }
112 
113 static void
gst_rtp_L8_depay_init(GstRtpL8Depay * rtpL8depay)114 gst_rtp_L8_depay_init (GstRtpL8Depay * rtpL8depay)
115 {
116 }
117 
118 static gint
gst_rtp_L8_depay_parse_int(GstStructure * structure,const gchar * field,gint def)119 gst_rtp_L8_depay_parse_int (GstStructure * structure, const gchar * field,
120     gint def)
121 {
122   const gchar *str;
123   gint res;
124 
125   if ((str = gst_structure_get_string (structure, field)))
126     return atoi (str);
127 
128   if (gst_structure_get_int (structure, field, &res))
129     return res;
130 
131   return def;
132 }
133 
134 static gboolean
gst_rtp_L8_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)135 gst_rtp_L8_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
136 {
137   GstStructure *structure;
138   GstRtpL8Depay *rtpL8depay;
139   gint clock_rate;
140   gint channels;
141   GstCaps *srccaps;
142   gboolean res;
143   const gchar *channel_order;
144   const GstRTPChannelOrder *order;
145   GstAudioInfo *info;
146 
147   rtpL8depay = GST_RTP_L8_DEPAY (depayload);
148 
149   structure = gst_caps_get_structure (caps, 0);
150 
151   /* no fixed mapping, we need clock-rate */
152   channels = 0;
153   clock_rate = 0;
154 
155   /* caps can overwrite defaults */
156   clock_rate = gst_rtp_L8_depay_parse_int (structure, "clock-rate", clock_rate);
157   if (clock_rate == 0)
158     goto no_clockrate;
159 
160   channels =
161       gst_rtp_L8_depay_parse_int (structure, "encoding-params", channels);
162   if (channels == 0) {
163     channels = gst_rtp_L8_depay_parse_int (structure, "channels", channels);
164     if (channels == 0) {
165       /* channels defaults to 1 otherwise */
166       channels = 1;
167     }
168   }
169 
170   depayload->clock_rate = clock_rate;
171 
172   info = &rtpL8depay->info;
173   gst_audio_info_init (info);
174   info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_U8);
175   info->rate = clock_rate;
176   info->channels = channels;
177   info->bpf = (info->finfo->width / 8) * channels;
178 
179   /* add channel positions */
180   channel_order = gst_structure_get_string (structure, "channel-order");
181 
182   order = gst_rtp_channels_get_by_order (channels, channel_order);
183   rtpL8depay->order = order;
184   if (order) {
185     memcpy (info->position, order->pos,
186         sizeof (GstAudioChannelPosition) * channels);
187     gst_audio_channel_positions_to_valid_order (info->position, info->channels);
188   } else {
189     GST_ELEMENT_WARNING (rtpL8depay, STREAM, DECODE,
190         (NULL), ("Unknown channel order '%s' for %d channels",
191             GST_STR_NULL (channel_order), channels));
192     /* create default NONE layout */
193     gst_rtp_channels_create_default (channels, info->position);
194     info->flags |= GST_AUDIO_FLAG_UNPOSITIONED;
195   }
196 
197   srccaps = gst_audio_info_to_caps (info);
198   res = gst_pad_set_caps (depayload->srcpad, srccaps);
199   gst_caps_unref (srccaps);
200 
201   return res;
202 
203   /* ERRORS */
204 no_clockrate:
205   {
206     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
207     return FALSE;
208   }
209 }
210 
211 static GstBuffer *
gst_rtp_L8_depay_process(GstRTPBaseDepayload * depayload,GstBuffer * buf)212 gst_rtp_L8_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
213 {
214   GstRtpL8Depay *rtpL8depay;
215   GstBuffer *outbuf;
216   gint payload_len;
217   gboolean marker;
218   GstRTPBuffer rtp = { NULL };
219 
220   rtpL8depay = GST_RTP_L8_DEPAY (depayload);
221 
222   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
223   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
224 
225   if (payload_len <= 0)
226     goto empty_packet;
227 
228   GST_DEBUG_OBJECT (rtpL8depay, "got payload of %d bytes", payload_len);
229 
230   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
231   marker = gst_rtp_buffer_get_marker (&rtp);
232 
233   if (marker) {
234     /* mark talk spurt with RESYNC */
235     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
236   }
237 
238   outbuf = gst_buffer_make_writable (outbuf);
239   if (rtpL8depay->order &&
240       !gst_audio_buffer_reorder_channels (outbuf,
241           rtpL8depay->info.finfo->format, rtpL8depay->info.channels,
242           rtpL8depay->info.position, rtpL8depay->order->pos)) {
243     goto reorder_failed;
244   }
245 
246   gst_rtp_buffer_unmap (&rtp);
247 
248   return outbuf;
249 
250   /* ERRORS */
251 empty_packet:
252   {
253     GST_ELEMENT_WARNING (rtpL8depay, STREAM, DECODE,
254         ("Empty Payload."), (NULL));
255     gst_rtp_buffer_unmap (&rtp);
256     return NULL;
257   }
258 reorder_failed:
259   {
260     GST_ELEMENT_ERROR (rtpL8depay, STREAM, DECODE,
261         ("Channel reordering failed."), (NULL));
262     gst_rtp_buffer_unmap (&rtp);
263     return NULL;
264   }
265 }
266