1 /*
2 * Opus Payloader Gst Element
3 *
4 * @author: Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <string.h>
27
28 #include <gst/rtp/gstrtpbuffer.h>
29 #include <gst/audio/audio.h>
30
31 #include "gstrtpopuspay.h"
32 #include "gstrtputils.h"
33
34 GST_DEBUG_CATEGORY_STATIC (rtpopuspay_debug);
35 #define GST_CAT_DEFAULT (rtpopuspay_debug)
36
37
38 static GstStaticPadTemplate gst_rtp_opus_pay_sink_template =
39 GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_PAD_SINK,
41 GST_PAD_ALWAYS,
42 GST_STATIC_CAPS
43 ("audio/x-opus, channels = (int) [1, 2], channel-mapping-family = (int) 0")
44 );
45
46 static GstStaticPadTemplate gst_rtp_opus_pay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_PAD_SRC,
49 GST_PAD_ALWAYS,
50 GST_STATIC_CAPS ("application/x-rtp, "
51 "media = (string) \"audio\", "
52 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53 "clock-rate = (int) 48000, "
54 "encoding-params = (string) \"2\", "
55 "encoding-name = (string) { \"OPUS\", \"X-GST-OPUS-DRAFT-SPITTKA-00\" }")
56 );
57
58 static gboolean gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload,
59 GstCaps * caps);
60 static GstCaps *gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
61 GstPad * pad, GstCaps * filter);
62 static GstFlowReturn gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload *
63 payload, GstBuffer * buffer);
64
65 G_DEFINE_TYPE (GstRtpOPUSPay, gst_rtp_opus_pay, GST_TYPE_RTP_BASE_PAYLOAD);
66
67 static void
gst_rtp_opus_pay_class_init(GstRtpOPUSPayClass * klass)68 gst_rtp_opus_pay_class_init (GstRtpOPUSPayClass * klass)
69 {
70 GstRTPBasePayloadClass *gstbasertppayload_class;
71 GstElementClass *element_class;
72
73 gstbasertppayload_class = (GstRTPBasePayloadClass *) klass;
74 element_class = GST_ELEMENT_CLASS (klass);
75
76 gstbasertppayload_class->set_caps = gst_rtp_opus_pay_setcaps;
77 gstbasertppayload_class->get_caps = gst_rtp_opus_pay_getcaps;
78 gstbasertppayload_class->handle_buffer = gst_rtp_opus_pay_handle_buffer;
79
80 gst_element_class_add_static_pad_template (element_class,
81 &gst_rtp_opus_pay_src_template);
82 gst_element_class_add_static_pad_template (element_class,
83 &gst_rtp_opus_pay_sink_template);
84
85 gst_element_class_set_static_metadata (element_class,
86 "RTP Opus payloader",
87 "Codec/Payloader/Network/RTP",
88 "Puts Opus audio in RTP packets",
89 "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
90
91 GST_DEBUG_CATEGORY_INIT (rtpopuspay_debug, "rtpopuspay", 0,
92 "Opus RTP Payloader");
93 }
94
95 static void
gst_rtp_opus_pay_init(GstRtpOPUSPay * rtpopuspay)96 gst_rtp_opus_pay_init (GstRtpOPUSPay * rtpopuspay)
97 {
98 }
99
100 static gboolean
gst_rtp_opus_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)101 gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
102 {
103 gboolean res;
104 GstCaps *src_caps;
105 GstStructure *s;
106 const char *encoding_name = "OPUS";
107 gint channels, rate;
108 const char *sprop_stereo = NULL;
109 char *sprop_maxcapturerate = NULL;
110
111 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
112 if (src_caps) {
113 GstStructure *s;
114 const GValue *value;
115
116 s = gst_caps_get_structure (src_caps, 0);
117
118 if (gst_structure_has_field (s, "encoding-name")) {
119 GValue default_value = G_VALUE_INIT;
120
121 g_value_init (&default_value, G_TYPE_STRING);
122 g_value_set_static_string (&default_value, encoding_name);
123
124 value = gst_structure_get_value (s, "encoding-name");
125 if (!gst_value_can_intersect (&default_value, value))
126 encoding_name = "X-GST-OPUS-DRAFT-SPITTKA-00";
127 }
128 gst_caps_unref (src_caps);
129 }
130
131 s = gst_caps_get_structure (caps, 0);
132 if (gst_structure_get_int (s, "channels", &channels)) {
133 if (channels > 2) {
134 GST_ERROR_OBJECT (payload,
135 "More than 2 channels with channel-mapping-family=0 is invalid");
136 return FALSE;
137 } else if (channels == 2) {
138 sprop_stereo = "1";
139 } else {
140 sprop_stereo = "0";
141 }
142 }
143
144 if (gst_structure_get_int (s, "rate", &rate)) {
145 sprop_maxcapturerate = g_strdup_printf ("%d", rate);
146 }
147
148 gst_rtp_base_payload_set_options (payload, "audio", FALSE,
149 encoding_name, 48000);
150
151 if (sprop_maxcapturerate && sprop_stereo) {
152 res =
153 gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
154 G_TYPE_STRING, sprop_maxcapturerate, "sprop-stereo", G_TYPE_STRING,
155 sprop_stereo, NULL);
156 } else if (sprop_maxcapturerate) {
157 res =
158 gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
159 G_TYPE_STRING, sprop_maxcapturerate, NULL);
160 } else if (sprop_stereo) {
161 res =
162 gst_rtp_base_payload_set_outcaps (payload, "sprop-stereo",
163 G_TYPE_STRING, sprop_stereo, NULL);
164 } else {
165 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
166 }
167
168 g_free (sprop_maxcapturerate);
169
170 return res;
171 }
172
173 static GstFlowReturn
gst_rtp_opus_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)174 gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload * basepayload,
175 GstBuffer * buffer)
176 {
177 GstBuffer *outbuf;
178 GstClockTime pts, dts, duration;
179
180 pts = GST_BUFFER_PTS (buffer);
181 dts = GST_BUFFER_DTS (buffer);
182 duration = GST_BUFFER_DURATION (buffer);
183
184 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
185
186 gst_rtp_copy_audio_meta (basepayload, outbuf, buffer);
187
188 outbuf = gst_buffer_append (outbuf, buffer);
189
190 GST_BUFFER_PTS (outbuf) = pts;
191 GST_BUFFER_DTS (outbuf) = dts;
192 GST_BUFFER_DURATION (outbuf) = duration;
193
194 /* Push out */
195 return gst_rtp_base_payload_push (basepayload, outbuf);
196 }
197
198 static GstCaps *
gst_rtp_opus_pay_getcaps(GstRTPBasePayload * payload,GstPad * pad,GstCaps * filter)199 gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
200 GstPad * pad, GstCaps * filter)
201 {
202 GstCaps *caps, *peercaps, *tcaps;
203 GstStructure *s;
204 const gchar *stereo;
205
206 if (pad == GST_RTP_BASE_PAYLOAD_SRCPAD (payload))
207 return
208 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
209 (payload, pad, filter);
210
211 tcaps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
212 peercaps = gst_pad_peer_query_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload),
213 tcaps);
214 gst_caps_unref (tcaps);
215 if (!peercaps)
216 return
217 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
218 (payload, pad, filter);
219
220 if (gst_caps_is_empty (peercaps))
221 return peercaps;
222
223 caps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SINKPAD (payload));
224
225 s = gst_caps_get_structure (peercaps, 0);
226 stereo = gst_structure_get_string (s, "stereo");
227 if (stereo != NULL) {
228 caps = gst_caps_make_writable (caps);
229
230 if (!strcmp (stereo, "1")) {
231 GstCaps *caps2 = gst_caps_copy (caps);
232
233 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
234 gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 1, NULL);
235 caps = gst_caps_merge (caps, caps2);
236 } else if (!strcmp (stereo, "0")) {
237 GstCaps *caps2 = gst_caps_copy (caps);
238
239 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
240 gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 2, NULL);
241 caps = gst_caps_merge (caps, caps2);
242 }
243 }
244 gst_caps_unref (peercaps);
245
246 if (filter) {
247 GstCaps *tmp = gst_caps_intersect_full (caps, filter,
248 GST_CAPS_INTERSECT_FIRST);
249 gst_caps_unref (caps);
250 caps = tmp;
251 }
252
253 GST_DEBUG_OBJECT (payload, "Returning caps: %" GST_PTR_FORMAT, caps);
254 return caps;
255 }
256
257 gboolean
gst_rtp_opus_pay_plugin_init(GstPlugin * plugin)258 gst_rtp_opus_pay_plugin_init (GstPlugin * plugin)
259 {
260 return gst_element_register (plugin, "rtpopuspay",
261 GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_PAY);
262 }
263