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