2 * Opus Depayloader Gst Element
4 * @author: Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>
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.
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.
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.
28 #include <gst/rtp/gstrtpbuffer.h>
29 #include <gst/audio/audio.h>
30 #include "gstrtpelements.h"
31 #include "gstrtpopusdepay.h"
32 #include "gstrtputils.h"
34 GST_DEBUG_CATEGORY_STATIC (rtpopusdepay_debug);
35 #define GST_CAT_DEFAULT (rtpopusdepay_debug)
37 static GstStaticPadTemplate gst_rtp_opus_depay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
41 GST_STATIC_CAPS ("application/x-rtp, "
42 "media = (string) \"audio\", "
43 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
44 "clock-rate = (int) 48000, "
45 "encoding-name = (string) { \"OPUS\", \"X-GST-OPUS-DRAFT-SPITTKA-00\", \"multiopus\" }")
48 static GstStaticPadTemplate gst_rtp_opus_depay_src_template =
49 GST_STATIC_PAD_TEMPLATE ("src",
52 GST_STATIC_CAPS ("audio/x-opus, channel-mapping-family = (int) [ 0, 1 ]")
55 static GstBuffer *gst_rtp_opus_depay_process (GstRTPBaseDepayload * depayload,
56 GstRTPBuffer * rtp_buffer);
57 static gboolean gst_rtp_opus_depay_setcaps (GstRTPBaseDepayload * depayload,
60 G_DEFINE_TYPE (GstRTPOpusDepay, gst_rtp_opus_depay,
61 GST_TYPE_RTP_BASE_DEPAYLOAD);
62 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpopusdepay, "rtpopusdepay",
63 GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_DEPAY, rtp_element_init (plugin));
66 gst_rtp_opus_depay_class_init (GstRTPOpusDepayClass * klass)
68 GstRTPBaseDepayloadClass *gstbasertpdepayload_class;
69 GstElementClass *element_class;
71 element_class = GST_ELEMENT_CLASS (klass);
72 gstbasertpdepayload_class = (GstRTPBaseDepayloadClass *) klass;
74 gst_element_class_add_static_pad_template (element_class,
75 &gst_rtp_opus_depay_src_template);
76 gst_element_class_add_static_pad_template (element_class,
77 &gst_rtp_opus_depay_sink_template);
78 gst_element_class_set_static_metadata (element_class,
79 "RTP Opus packet depayloader", "Codec/Depayloader/Network/RTP",
80 "Extracts Opus audio from RTP packets",
81 "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
83 gstbasertpdepayload_class->process_rtp_packet = gst_rtp_opus_depay_process;
84 gstbasertpdepayload_class->set_caps = gst_rtp_opus_depay_setcaps;
86 GST_DEBUG_CATEGORY_INIT (rtpopusdepay_debug, "rtpopusdepay", 0,
87 "Opus RTP Depayloader");
91 gst_rtp_opus_depay_init (GstRTPOpusDepay * rtpopusdepay)
97 gst_rtp_opus_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
102 const gchar *sprop_maxcapturerate;
104 srccaps = gst_caps_new_empty_simple ("audio/x-opus");
106 s = gst_caps_get_structure (caps, 0);
108 if (g_str_equal (gst_structure_get_string (s, "encoding-name"), "multiopus")) {
112 const gchar *encoding_params;
113 const gchar *num_streams;
114 const gchar *coupled_streams;
115 const gchar *channel_mapping;
118 if (!gst_structure_has_field_typed (s, "encoding-params", G_TYPE_STRING) ||
119 !gst_structure_has_field_typed (s, "num_streams", G_TYPE_STRING) ||
120 !gst_structure_has_field_typed (s, "coupled_streams", G_TYPE_STRING) ||
121 !gst_structure_has_field_typed (s, "channel_mapping", G_TYPE_STRING)) {
122 GST_WARNING_OBJECT (depayload, "Encoding name 'multiopus' requires "
123 "encoding-params, num_streams, coupled_streams and channel_mapping "
124 "as string fields in caps.");
128 gst_caps_set_simple (srccaps, "channel-mapping-family", G_TYPE_INT, 1,
131 encoding_params = gst_structure_get_string (s, "encoding-params");
132 channels = g_ascii_strtoull (encoding_params, &endptr, 10);
133 if (*endptr != '\0' || channels > 255) {
134 GST_WARNING_OBJECT (depayload, "Invalid encoding-params value '%s'",
138 gst_caps_set_simple (srccaps, "channels", G_TYPE_INT, channels, NULL);
140 num_streams = gst_structure_get_string (s, "num_streams");
141 stream_count = g_ascii_strtoull (num_streams, &endptr, 10);
142 if (*endptr != '\0' || stream_count > channels) {
143 GST_WARNING_OBJECT (depayload, "Invalid num_streams value '%s'",
147 gst_caps_set_simple (srccaps, "stream-count", G_TYPE_INT, stream_count,
150 coupled_streams = gst_structure_get_string (s, "coupled_streams");
151 coupled_count = g_ascii_strtoull (coupled_streams, &endptr, 10);
152 if (*endptr != '\0' || coupled_count > stream_count) {
153 GST_WARNING_OBJECT (depayload, "Invalid coupled_streams value '%s'",
157 gst_caps_set_simple (srccaps, "coupled-count", G_TYPE_INT, coupled_count,
160 channel_mapping = gst_structure_get_string (s, "channel_mapping");
164 GValue mapping = G_VALUE_INIT;
165 GValue v = G_VALUE_INIT;
167 split = g_strsplit (channel_mapping, ",", -1);
169 g_value_init (&mapping, GST_TYPE_ARRAY);
170 g_value_init (&v, G_TYPE_INT);
172 for (ptr = split; *ptr; ++ptr) {
173 gint channel = g_ascii_strtoull (*ptr, &endptr, 10);
174 if (*endptr != '\0' || channel > channels) {
175 GST_WARNING_OBJECT (depayload, "Invalid channel_mapping value '%s'",
177 g_value_unset (&mapping);
180 g_value_set_int (&v, channel);
181 gst_value_array_append_value (&mapping, &v);
187 if (G_IS_VALUE (&mapping)) {
188 gst_caps_set_value (srccaps, "channel-mapping", &mapping);
189 g_value_unset (&mapping);
195 const gchar *sprop_stereo;
197 gst_caps_set_simple (srccaps, "channel-mapping-family", G_TYPE_INT, 0,
200 if ((sprop_stereo = gst_structure_get_string (s, "sprop-stereo"))) {
201 if (strcmp (sprop_stereo, "0") == 0)
202 gst_caps_set_simple (srccaps, "channels", G_TYPE_INT, 1, NULL);
203 else if (strcmp (sprop_stereo, "1") == 0)
204 gst_caps_set_simple (srccaps, "channels", G_TYPE_INT, 2, NULL);
206 GST_WARNING_OBJECT (depayload, "Unknown sprop-stereo value '%s'",
209 /* sprop-stereo defaults to mono as per RFC 7587. */
210 gst_caps_set_simple (srccaps, "channels", G_TYPE_INT, 1, NULL);
214 if ((sprop_maxcapturerate =
215 gst_structure_get_string (s, "sprop-maxcapturerate"))) {
219 rate = strtoul (sprop_maxcapturerate, &tailptr, 10);
220 if (rate > INT_MAX || *tailptr != '\0') {
221 GST_WARNING_OBJECT (depayload,
222 "Failed to parse sprop-maxcapturerate value '%s'",
223 sprop_maxcapturerate);
225 gst_caps_set_simple (srccaps, "rate", G_TYPE_INT, rate, NULL);
229 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
231 GST_DEBUG_OBJECT (depayload,
232 "set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
233 gst_caps_unref (srccaps);
235 depayload->clock_rate = 48000;
240 gst_caps_unref (srccaps);
246 gst_rtp_opus_depay_process (GstRTPBaseDepayload * depayload,
247 GstRTPBuffer * rtp_buffer)
251 outbuf = gst_rtp_buffer_get_payload_buffer (rtp_buffer);
253 gst_rtp_drop_non_audio_meta (depayload, outbuf);