rtsp-server: fix memory leak
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpisacpay.c
1 /* GStreamer
2  * Copyright (C) 2020 Collabora Ltd.
3  *  Author: Guillaume Desmottes <guillaume.desmottes@collabora.com>, Collabora Ltd.
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-rtpisacpay
23  * @title: rtpisacpay
24  * @short_description: iSAC RTP Payloader
25  *
26  * Since: 1.20
27  *
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <gst/rtp/gstrtpbuffer.h>
35
36 #include "gstrtpelements.h"
37 #include "gstrtpisacpay.h"
38 #include "gstrtputils.h"
39
40 GST_DEBUG_CATEGORY_STATIC (rtpisacpay_debug);
41 #define GST_CAT_DEFAULT (rtpisacpay_debug)
42
43 static GstStaticPadTemplate gst_rtp_isac_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("audio/isac, "
48         "rate = (int) { 16000, 32000 }, " "channels = (int) 1")
49     );
50
51 static GstStaticPadTemplate gst_rtp_isac_pay_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("application/x-rtp, "
56         "media = (string) \"audio\", "
57         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
58         "clock-rate =  (int) { 16000, 32000 }, "
59         "encoding-name = (string) \"ISAC\", "
60         "encoding-params = (string) \"1\"")
61     );
62
63 struct _GstRtpIsacPay
64 {
65   /*< private > */
66   GstRTPBasePayload parent;
67 };
68
69 #define gst_rtp_isac_pay_parent_class parent_class
70 G_DEFINE_TYPE (GstRtpIsacPay, gst_rtp_isac_pay, GST_TYPE_RTP_BASE_PAYLOAD);
71 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpisacpay, "rtpisacpay",
72     GST_RANK_SECONDARY, GST_TYPE_RTP_ISAC_PAY, rtp_element_init (plugin));
73
74 static GstCaps *
75 gst_rtp_isac_pay_getcaps (GstRTPBasePayload * payload, GstPad * pad,
76     GstCaps * filter)
77 {
78   GstCaps *otherpadcaps;
79   GstCaps *caps;
80
81   otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
82   caps = gst_pad_get_pad_template_caps (pad);
83
84   if (otherpadcaps) {
85     if (!gst_caps_is_empty (otherpadcaps)) {
86       GstStructure *ps;
87       GstStructure *s;
88       const GValue *v;
89
90       ps = gst_caps_get_structure (otherpadcaps, 0);
91       caps = gst_caps_make_writable (caps);
92       s = gst_caps_get_structure (caps, 0);
93
94       v = gst_structure_get_value (ps, "clock-rate");
95       if (v)
96         gst_structure_set_value (s, "rate", v);
97     }
98     gst_caps_unref (otherpadcaps);
99   }
100
101   if (filter) {
102     GstCaps *tcaps = caps;
103
104     caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
105     gst_caps_unref (tcaps);
106   }
107
108   GST_DEBUG_OBJECT (payload, "%" GST_PTR_FORMAT, caps);
109
110   return caps;
111 }
112
113 static gboolean
114 gst_rtp_isac_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
115 {
116   GstStructure *s;
117   gint rate;
118
119   GST_DEBUG_OBJECT (payload, "%" GST_PTR_FORMAT, caps);
120
121   s = gst_caps_get_structure (caps, 0);
122   if (!gst_structure_get_int (s, "rate", &rate)) {
123     GST_ERROR_OBJECT (payload, "Missing 'rate' in caps");
124     return FALSE;
125   }
126
127   gst_rtp_base_payload_set_options (payload, "audio", TRUE, "ISAC", rate);
128
129   return gst_rtp_base_payload_set_outcaps (payload, NULL);
130 }
131
132 static GstFlowReturn
133 gst_rtp_isac_pay_handle_buffer (GstRTPBasePayload * basepayload,
134     GstBuffer * buffer)
135 {
136   GstBuffer *outbuf;
137   GstClockTime pts, dts, duration;
138
139   pts = GST_BUFFER_PTS (buffer);
140   dts = GST_BUFFER_DTS (buffer);
141   duration = GST_BUFFER_DURATION (buffer);
142
143   outbuf = gst_rtp_base_payload_allocate_output_buffer (basepayload, 0, 0, 0);
144
145   gst_rtp_copy_audio_meta (basepayload, outbuf, buffer);
146
147   outbuf = gst_buffer_append (outbuf, buffer);
148
149   GST_BUFFER_PTS (outbuf) = pts;
150   GST_BUFFER_DTS (outbuf) = dts;
151   GST_BUFFER_DURATION (outbuf) = duration;
152
153   return gst_rtp_base_payload_push (basepayload, outbuf);
154 }
155
156 static void
157 gst_rtp_isac_pay_class_init (GstRtpIsacPayClass * klass)
158 {
159   GstElementClass *gstelement_class = (GstElementClass *) klass;
160   GstRTPBasePayloadClass *payload_class = (GstRTPBasePayloadClass *) klass;
161
162   payload_class->get_caps = gst_rtp_isac_pay_getcaps;
163   payload_class->set_caps = gst_rtp_isac_pay_setcaps;
164   payload_class->handle_buffer = gst_rtp_isac_pay_handle_buffer;
165
166   gst_element_class_add_static_pad_template (gstelement_class,
167       &gst_rtp_isac_pay_sink_template);
168   gst_element_class_add_static_pad_template (gstelement_class,
169       &gst_rtp_isac_pay_src_template);
170
171   gst_element_class_set_static_metadata (gstelement_class,
172       "RTP iSAC payloader", "Codec/Payloader/Network/RTP",
173       "Payload-encodes iSAC audio into a RTP packet",
174       "Guillaume Desmottes <guillaume.desmottes@collabora.com>");
175
176   GST_DEBUG_CATEGORY_INIT (rtpisacpay_debug, "rtpisacpay", 0,
177       "iSAC RTP Payloader");
178 }
179
180 static void
181 gst_rtp_isac_pay_init (GstRtpIsacPay * rtpisacpay)
182 {
183 }