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 "gstrtpopusdepay.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpopusdepay_debug);
33 #define GST_CAT_DEFAULT (rtpopusdepay_debug)
35 static GstStaticPadTemplate gst_rtp_opus_depay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("application/x-rtp, "
40 "media = (string) \"audio\", "
41 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
42 "clock-rate = (int) 48000, "
43 "encoding-name = (string) { \"OPUS\", \"X-GST-OPUS-DRAFT-SPITTKA-00\" }")
46 static GstStaticPadTemplate gst_rtp_opus_depay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
50 GST_STATIC_CAPS ("audio/x-opus, channel-mapping-family = (int) 0")
53 static GstBuffer *gst_rtp_opus_depay_process (GstRTPBaseDepayload * depayload,
55 static gboolean gst_rtp_opus_depay_setcaps (GstRTPBaseDepayload * depayload,
58 G_DEFINE_TYPE (GstRTPOpusDepay, gst_rtp_opus_depay,
59 GST_TYPE_RTP_BASE_DEPAYLOAD);
62 gst_rtp_opus_depay_class_init (GstRTPOpusDepayClass * klass)
64 GstRTPBaseDepayloadClass *gstbasertpdepayload_class;
65 GstElementClass *element_class;
67 element_class = GST_ELEMENT_CLASS (klass);
68 gstbasertpdepayload_class = (GstRTPBaseDepayloadClass *) klass;
70 gst_element_class_add_static_pad_template (element_class,
71 &gst_rtp_opus_depay_src_template);
72 gst_element_class_add_static_pad_template (element_class,
73 &gst_rtp_opus_depay_sink_template);
74 gst_element_class_set_static_metadata (element_class,
75 "RTP Opus packet depayloader", "Codec/Depayloader/Network/RTP",
76 "Extracts Opus audio from RTP packets",
77 "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
79 gstbasertpdepayload_class->process = gst_rtp_opus_depay_process;
80 gstbasertpdepayload_class->set_caps = gst_rtp_opus_depay_setcaps;
82 GST_DEBUG_CATEGORY_INIT (rtpopusdepay_debug, "rtpopusdepay", 0,
83 "Opus RTP Depayloader");
87 gst_rtp_opus_depay_init (GstRTPOpusDepay * rtpopusdepay)
93 gst_rtp_opus_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
98 const gchar *sprop_stereo, *sprop_maxcapturerate;
101 gst_caps_new_simple ("audio/x-opus", "channel-mapping-family", G_TYPE_INT,
104 s = gst_caps_get_structure (caps, 0);
105 if ((sprop_stereo = gst_structure_get_string (s, "sprop-stereo"))) {
106 if (strcmp (sprop_stereo, "0") == 0)
107 gst_caps_set_simple (srccaps, "channels", G_TYPE_INT, 1, NULL);
108 else if (strcmp (sprop_stereo, "1") == 0)
109 gst_caps_set_simple (srccaps, "channels", G_TYPE_INT, 2, NULL);
111 GST_WARNING_OBJECT (depayload, "Unknown sprop-stereo value '%s'",
115 if ((sprop_maxcapturerate =
116 gst_structure_get_string (s, "sprop-maxcapturerate"))) {
120 rate = strtoul (sprop_maxcapturerate, &tailptr, 10);
121 if (rate > INT_MAX || *tailptr != '\0') {
122 GST_WARNING_OBJECT (depayload,
123 "Failed to parse sprop-maxcapturerate value '%s'",
124 sprop_maxcapturerate);
126 gst_caps_set_simple (srccaps, "rate", G_TYPE_INT, rate, NULL);
130 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
132 GST_DEBUG_OBJECT (depayload,
133 "set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
134 gst_caps_unref (srccaps);
136 depayload->clock_rate = 48000;
142 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
144 GstRTPOpusDepay *depay = user_data;
145 const GstMetaInfo *info = (*meta)->info;
146 const gchar *const *tags = gst_meta_api_type_get_tags (info->api);
148 if (!tags || (g_strv_length ((gchar **) tags) == 1
149 && gst_meta_api_type_has_tag (info->api,
150 g_quark_from_string (GST_META_TAG_AUDIO_STR)))) {
151 GST_DEBUG_OBJECT (depay, "keeping metadata %s", g_type_name (info->api));
153 GST_DEBUG_OBJECT (depay, "dropping metadata %s", g_type_name (info->api));
161 gst_rtp_opus_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
164 GstRTPBuffer rtpbuf = { NULL, };
166 gst_rtp_buffer_map (buf, GST_MAP_READ, &rtpbuf);
167 outbuf = gst_rtp_buffer_get_payload_buffer (&rtpbuf);
168 gst_rtp_buffer_unmap (&rtpbuf);
170 outbuf = gst_buffer_make_writable (outbuf);
171 /* Filter away all metas that are not sensible to copy */
172 gst_buffer_foreach_meta (outbuf, foreach_metadata, depayload);
178 gst_rtp_opus_depay_plugin_init (GstPlugin * plugin)
180 return gst_element_register (plugin, "rtpopusdepay",
181 GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_DEPAY);