rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL8depay.c
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 "gstrtpL8depay.h"
46 #include "gstrtpchannels.h"
47
48 GST_DEBUG_CATEGORY_STATIC (rtpL8depay_debug);
49 #define GST_CAT_DEFAULT (rtpL8depay_debug)
50
51 static GstStaticPadTemplate gst_rtp_L8_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) U8, "
57         "layout = (string) interleaved, "
58         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
59     );
60
61 static GstStaticPadTemplate gst_rtp_L8_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) L8;")
71     );
72
73 #define gst_rtp_L8_depay_parent_class parent_class
74 G_DEFINE_TYPE (GstRtpL8Depay, gst_rtp_L8_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
75
76 static gboolean gst_rtp_L8_depay_setcaps (GstRTPBaseDepayload * depayload,
77     GstCaps * caps);
78 static GstBuffer *gst_rtp_L8_depay_process (GstRTPBaseDepayload * depayload,
79     GstBuffer * buf);
80
81 static void
82 gst_rtp_L8_depay_class_init (GstRtpL8DepayClass * klass)
83 {
84   GstElementClass *gstelement_class;
85   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
86
87   gstelement_class = (GstElementClass *) klass;
88   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
89
90   gstrtpbasedepayload_class->set_caps = gst_rtp_L8_depay_setcaps;
91   gstrtpbasedepayload_class->process = gst_rtp_L8_depay_process;
92
93   gst_element_class_add_pad_template (gstelement_class,
94       gst_static_pad_template_get (&gst_rtp_L8_depay_src_template));
95   gst_element_class_add_pad_template (gstelement_class,
96       gst_static_pad_template_get (&gst_rtp_L8_depay_sink_template));
97
98   gst_element_class_set_static_metadata (gstelement_class,
99       "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
100       "Extracts raw audio from RTP packets",
101       "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>, "
102       "GE Intelligent Platforms Embedded Systems, Inc.");
103
104   GST_DEBUG_CATEGORY_INIT (rtpL8depay_debug, "rtpL8depay", 0,
105       "Raw Audio RTP Depayloader");
106 }
107
108 static void
109 gst_rtp_L8_depay_init (GstRtpL8Depay * rtpL8depay)
110 {
111 }
112
113 static gint
114 gst_rtp_L8_depay_parse_int (GstStructure * structure, const gchar * field,
115     gint def)
116 {
117   const gchar *str;
118   gint res;
119
120   if ((str = gst_structure_get_string (structure, field)))
121     return atoi (str);
122
123   if (gst_structure_get_int (structure, field, &res))
124     return res;
125
126   return def;
127 }
128
129 static gboolean
130 gst_rtp_L8_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
131 {
132   GstStructure *structure;
133   GstRtpL8Depay *rtpL8depay;
134   gint clock_rate;
135   gint channels;
136   GstCaps *srccaps;
137   gboolean res;
138   const gchar *channel_order;
139   const GstRTPChannelOrder *order;
140   GstAudioInfo *info;
141
142   rtpL8depay = GST_RTP_L8_DEPAY (depayload);
143
144   structure = gst_caps_get_structure (caps, 0);
145
146   /* no fixed mapping, we need clock-rate */
147   channels = 0;
148   clock_rate = 0;
149
150   /* caps can overwrite defaults */
151   clock_rate = gst_rtp_L8_depay_parse_int (structure, "clock-rate", clock_rate);
152   if (clock_rate == 0)
153     goto no_clockrate;
154
155   channels =
156       gst_rtp_L8_depay_parse_int (structure, "encoding-params", channels);
157   if (channels == 0) {
158     channels = gst_rtp_L8_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 = &rtpL8depay->info;
168   gst_audio_info_init (info);
169   info->finfo = gst_audio_format_get_info (GST_AUDIO_FORMAT_U8);
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   rtpL8depay->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 (rtpL8depay, 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     info->flags |= GST_AUDIO_FLAG_UNPOSITIONED;
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 *
207 gst_rtp_L8_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
208 {
209   GstRtpL8Depay *rtpL8depay;
210   GstBuffer *outbuf;
211   gint payload_len;
212   gboolean marker;
213   GstRTPBuffer rtp = { NULL };
214
215   rtpL8depay = GST_RTP_L8_DEPAY (depayload);
216
217   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
218   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
219
220   if (payload_len <= 0)
221     goto empty_packet;
222
223   GST_DEBUG_OBJECT (rtpL8depay, "got payload of %d bytes", payload_len);
224
225   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
226   marker = gst_rtp_buffer_get_marker (&rtp);
227
228   if (marker) {
229     /* mark talk spurt with RESYNC */
230     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
231   }
232
233   outbuf = gst_buffer_make_writable (outbuf);
234   if (rtpL8depay->order &&
235       !gst_audio_buffer_reorder_channels (outbuf,
236           rtpL8depay->info.finfo->format, rtpL8depay->info.channels,
237           rtpL8depay->info.position, rtpL8depay->order->pos)) {
238     goto reorder_failed;
239   }
240
241   gst_rtp_buffer_unmap (&rtp);
242
243   return outbuf;
244
245   /* ERRORS */
246 empty_packet:
247   {
248     GST_ELEMENT_WARNING (rtpL8depay, STREAM, DECODE,
249         ("Empty Payload."), (NULL));
250     gst_rtp_buffer_unmap (&rtp);
251     return NULL;
252   }
253 reorder_failed:
254   {
255     GST_ELEMENT_ERROR (rtpL8depay, STREAM, DECODE,
256         ("Channel reordering failed."), (NULL));
257     gst_rtp_buffer_unmap (&rtp);
258     return NULL;
259   }
260 }
261
262 gboolean
263 gst_rtp_L8_depay_plugin_init (GstPlugin * plugin)
264 {
265   return gst_element_register (plugin, "rtpL8depay",
266       GST_RANK_SECONDARY, GST_TYPE_RTP_L8_DEPAY);
267 }