rtp: Update codes based on 1.18.4
[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  * @title: rtpL24depay
23  * @see_also: rtpL24pay
24  *
25  * Extract raw audio from RTP packets according to RFC 3190, section 4.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3190.txt
27  *
28  * ## Example pipeline
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  *
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
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
107 gst_rtp_L24_depay_init (GstRtpL24Depay * rtpL24depay)
108 {
109 }
110
111 static gint
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
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     info->flags |= GST_AUDIO_FLAG_UNPOSITIONED;
191   }
192
193   srccaps = gst_audio_info_to_caps (info);
194   res = gst_pad_set_caps (depayload->srcpad, srccaps);
195   gst_caps_unref (srccaps);
196
197   return res;
198
199   /* ERRORS */
200 no_clockrate:
201   {
202     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
203     return FALSE;
204   }
205 }
206
207 static GstBuffer *
208 gst_rtp_L24_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
209 {
210   GstRtpL24Depay *rtpL24depay;
211   GstBuffer *outbuf;
212   gint payload_len;
213   gboolean marker;
214
215   rtpL24depay = GST_RTP_L24_DEPAY (depayload);
216
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 (outbuf) {
234     gst_rtp_drop_non_audio_meta (rtpL24depay, outbuf);
235   }
236   if (rtpL24depay->order &&
237       !gst_audio_buffer_reorder_channels (outbuf,
238           rtpL24depay->info.finfo->format, rtpL24depay->info.channels,
239           rtpL24depay->info.position, rtpL24depay->order->pos)) {
240     goto reorder_failed;
241   }
242
243   return outbuf;
244
245   /* ERRORS */
246 empty_packet:
247   {
248     GST_ELEMENT_WARNING (rtpL24depay, STREAM, DECODE,
249         ("Empty Payload."), (NULL));
250     return NULL;
251   }
252 reorder_failed:
253   {
254     GST_ELEMENT_ERROR (rtpL24depay, STREAM, DECODE,
255         ("Channel reordering failed."), (NULL));
256     return NULL;
257   }
258 }
259
260 gboolean
261 gst_rtp_L24_depay_plugin_init (GstPlugin * plugin)
262 {
263   return gst_element_register (plugin, "rtpL24depay",
264       GST_RANK_SECONDARY, GST_TYPE_RTP_L24_DEPAY);
265 }