rtsp-server: fix memory leak
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpL8pay.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-rtpL8pay
23  * @see_also: rtpL8depay
24  *
25  * Payload raw audio into 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 -v audiotestsrc ! audioconvert ! rtpL8pay ! udpsink
32  * ]| This example pipeline will payload raw audio. Refer to
33  * the rtpL8depay example to depayload and play the RTP stream.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include <string.h>
41
42 #include <gst/audio/audio.h>
43 #include <gst/rtp/gstrtpbuffer.h>
44
45 #include "gstrtpelements.h"
46 #include "gstrtpL8pay.h"
47 #include "gstrtpchannels.h"
48
49 GST_DEBUG_CATEGORY_STATIC (rtpL8pay_debug);
50 #define GST_CAT_DEFAULT (rtpL8pay_debug)
51
52 static GstStaticPadTemplate gst_rtp_L8_pay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
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_pay_src_template =
63     GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-rtp, "
67         "media = (string) audio, "
68         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
69         "clock-rate = (int) [ 1, MAX ], "
70         "encoding-name = (string) L8, " "channels = (int) [ 1, MAX ];")
71     );
72
73 static gboolean gst_rtp_L8_pay_setcaps (GstRTPBasePayload * basepayload,
74     GstCaps * caps);
75 static GstCaps *gst_rtp_L8_pay_getcaps (GstRTPBasePayload * rtppayload,
76     GstPad * pad, GstCaps * filter);
77 static GstFlowReturn
78 gst_rtp_L8_pay_handle_buffer (GstRTPBasePayload * basepayload,
79     GstBuffer * buffer);
80
81 #define gst_rtp_L8_pay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpL8Pay, gst_rtp_L8_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
83
84
85 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpL8pay, "rtpL8pay", GST_RANK_SECONDARY,
86     GST_TYPE_RTP_L8_PAY, rtp_element_init (plugin));
87
88 static void
89 gst_rtp_L8_pay_class_init (GstRtpL8PayClass * klass)
90 {
91   GstElementClass *gstelement_class;
92   GstRTPBasePayloadClass *gstrtpbasepayload_class;
93
94   gstelement_class = (GstElementClass *) klass;
95   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
96
97   gstrtpbasepayload_class->set_caps = gst_rtp_L8_pay_setcaps;
98   gstrtpbasepayload_class->get_caps = gst_rtp_L8_pay_getcaps;
99   gstrtpbasepayload_class->handle_buffer = gst_rtp_L8_pay_handle_buffer;
100
101   gst_element_class_add_pad_template (gstelement_class,
102       gst_static_pad_template_get (&gst_rtp_L8_pay_src_template));
103   gst_element_class_add_pad_template (gstelement_class,
104       gst_static_pad_template_get (&gst_rtp_L8_pay_sink_template));
105
106   gst_element_class_set_static_metadata (gstelement_class,
107       "RTP audio payloader", "Codec/Payloader/Network/RTP",
108       "Payload-encode Raw audio into RTP packets (RFC 3551)",
109       "Wim Taymans <wim.taymans@gmail.com>, "
110       "GE Intelligent Platforms Embedded Systems, Inc.");
111
112   GST_DEBUG_CATEGORY_INIT (rtpL8pay_debug, "rtpL8pay", 0, "L8 RTP Payloader");
113 }
114
115 static void
116 gst_rtp_L8_pay_init (GstRtpL8Pay * rtpL8pay)
117 {
118   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
119
120   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpL8pay);
121
122   /* tell rtpbaseaudiopayload that this is a sample based codec */
123   gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
124 }
125
126 static gboolean
127 gst_rtp_L8_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
128 {
129   GstRtpL8Pay *rtpL8pay;
130   gboolean res;
131   gchar *params;
132   GstAudioInfo *info;
133   const GstRTPChannelOrder *order;
134   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
135
136   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
137   rtpL8pay = GST_RTP_L8_PAY (basepayload);
138
139   info = &rtpL8pay->info;
140   gst_audio_info_init (info);
141   if (!gst_audio_info_from_caps (info, caps))
142     goto invalid_caps;
143
144   order = gst_rtp_channels_get_by_pos (info->channels, info->position);
145   rtpL8pay->order = order;
146
147   gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "L8",
148       info->rate);
149   params = g_strdup_printf ("%d", info->channels);
150
151   if (!order && info->channels > 2) {
152     GST_ELEMENT_WARNING (rtpL8pay, STREAM, DECODE,
153         (NULL), ("Unknown channel order for %d channels", info->channels));
154   }
155
156   if (order && order->name) {
157     res = gst_rtp_base_payload_set_outcaps (basepayload,
158         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
159         info->channels, "channel-order", G_TYPE_STRING, order->name, NULL);
160   } else {
161     res = gst_rtp_base_payload_set_outcaps (basepayload,
162         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
163         info->channels, NULL);
164   }
165
166   g_free (params);
167
168   /* octet-per-sample is # channels for L8 */
169   gst_rtp_base_audio_payload_set_sample_options (rtpbaseaudiopayload,
170       info->channels);
171
172   return res;
173
174   /* ERRORS */
175 invalid_caps:
176   {
177     GST_DEBUG_OBJECT (rtpL8pay, "invalid caps");
178     return FALSE;
179   }
180 }
181
182 static GstCaps *
183 gst_rtp_L8_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
184     GstCaps * filter)
185 {
186   GstCaps *otherpadcaps;
187   GstCaps *caps;
188
189   caps = gst_pad_get_pad_template_caps (pad);
190
191   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
192   if (otherpadcaps) {
193     if (!gst_caps_is_empty (otherpadcaps)) {
194       GstStructure *structure;
195       gint channels;
196       gint rate;
197
198       structure = gst_caps_get_structure (otherpadcaps, 0);
199       caps = gst_caps_make_writable (caps);
200
201       if (gst_structure_get_int (structure, "channels", &channels)) {
202         gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
203       }
204
205       if (gst_structure_get_int (structure, "clock-rate", &rate)) {
206         gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
207       }
208
209     }
210     gst_caps_unref (otherpadcaps);
211   }
212
213   if (filter) {
214     GstCaps *tcaps = caps;
215
216     caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
217     gst_caps_unref (tcaps);
218   }
219
220   return caps;
221 }
222
223 static GstFlowReturn
224 gst_rtp_L8_pay_handle_buffer (GstRTPBasePayload * basepayload,
225     GstBuffer * buffer)
226 {
227   GstRtpL8Pay *rtpL8pay;
228
229   rtpL8pay = GST_RTP_L8_PAY (basepayload);
230   buffer = gst_buffer_make_writable (buffer);
231
232   if (rtpL8pay->order &&
233       !gst_audio_buffer_reorder_channels (buffer, rtpL8pay->info.finfo->format,
234           rtpL8pay->info.channels, rtpL8pay->info.position,
235           rtpL8pay->order->pos)) {
236     return GST_FLOW_ERROR;
237   }
238
239   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (basepayload,
240       buffer);
241 }