1 /*
2  * Opus Depayloader 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 #include <stdlib.h>
28 #include <gst/rtp/gstrtpbuffer.h>
29 #include <gst/audio/audio.h>
30 #include "gstrtpopusdepay.h"
31 #include "gstrtputils.h"
32 
33 GST_DEBUG_CATEGORY_STATIC (rtpopusdepay_debug);
34 #define GST_CAT_DEFAULT (rtpopusdepay_debug)
35 
36 static GstStaticPadTemplate gst_rtp_opus_depay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("application/x-rtp, "
41         "media = (string) \"audio\", "
42         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ","
43         "clock-rate = (int) 48000, "
44         "encoding-name = (string) { \"OPUS\", \"X-GST-OPUS-DRAFT-SPITTKA-00\" }")
45     );
46 
47 static GstStaticPadTemplate gst_rtp_opus_depay_src_template =
48 GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS ("audio/x-opus, channel-mapping-family = (int) 0")
52     );
53 
54 static GstBuffer *gst_rtp_opus_depay_process (GstRTPBaseDepayload * depayload,
55     GstRTPBuffer * rtp_buffer);
56 static gboolean gst_rtp_opus_depay_setcaps (GstRTPBaseDepayload * depayload,
57     GstCaps * caps);
58 
59 G_DEFINE_TYPE (GstRTPOpusDepay, gst_rtp_opus_depay,
60     GST_TYPE_RTP_BASE_DEPAYLOAD);
61 
62 static void
gst_rtp_opus_depay_class_init(GstRTPOpusDepayClass * klass)63 gst_rtp_opus_depay_class_init (GstRTPOpusDepayClass * klass)
64 {
65   GstRTPBaseDepayloadClass *gstbasertpdepayload_class;
66   GstElementClass *element_class;
67 
68   element_class = GST_ELEMENT_CLASS (klass);
69   gstbasertpdepayload_class = (GstRTPBaseDepayloadClass *) klass;
70 
71   gst_element_class_add_static_pad_template (element_class,
72       &gst_rtp_opus_depay_src_template);
73   gst_element_class_add_static_pad_template (element_class,
74       &gst_rtp_opus_depay_sink_template);
75   gst_element_class_set_static_metadata (element_class,
76       "RTP Opus packet depayloader", "Codec/Depayloader/Network/RTP",
77       "Extracts Opus audio from RTP packets",
78       "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
79 
80   gstbasertpdepayload_class->process_rtp_packet = gst_rtp_opus_depay_process;
81   gstbasertpdepayload_class->set_caps = gst_rtp_opus_depay_setcaps;
82 
83   GST_DEBUG_CATEGORY_INIT (rtpopusdepay_debug, "rtpopusdepay", 0,
84       "Opus RTP Depayloader");
85 }
86 
87 static void
gst_rtp_opus_depay_init(GstRTPOpusDepay * rtpopusdepay)88 gst_rtp_opus_depay_init (GstRTPOpusDepay * rtpopusdepay)
89 {
90 
91 }
92 
93 static gboolean
gst_rtp_opus_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)94 gst_rtp_opus_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
95 {
96   GstCaps *srccaps;
97   GstStructure *s;
98   gboolean ret;
99   const gchar *sprop_stereo, *sprop_maxcapturerate;
100 
101   srccaps =
102       gst_caps_new_simple ("audio/x-opus", "channel-mapping-family", G_TYPE_INT,
103       0, NULL);
104 
105   s = gst_caps_get_structure (caps, 0);
106   if ((sprop_stereo = gst_structure_get_string (s, "sprop-stereo"))) {
107     if (strcmp (sprop_stereo, "0") == 0)
108       gst_caps_set_simple (srccaps, "channels", G_TYPE_INT, 1, NULL);
109     else if (strcmp (sprop_stereo, "1") == 0)
110       gst_caps_set_simple (srccaps, "channels", G_TYPE_INT, 2, NULL);
111     else
112       GST_WARNING_OBJECT (depayload, "Unknown sprop-stereo value '%s'",
113           sprop_stereo);
114   }
115 
116   if ((sprop_maxcapturerate =
117           gst_structure_get_string (s, "sprop-maxcapturerate"))) {
118     gulong rate;
119     gchar *tailptr;
120 
121     rate = strtoul (sprop_maxcapturerate, &tailptr, 10);
122     if (rate > INT_MAX || *tailptr != '\0') {
123       GST_WARNING_OBJECT (depayload,
124           "Failed to parse sprop-maxcapturerate value '%s'",
125           sprop_maxcapturerate);
126     } else {
127       gst_caps_set_simple (srccaps, "rate", G_TYPE_INT, rate, NULL);
128     }
129   }
130 
131   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
132 
133   GST_DEBUG_OBJECT (depayload,
134       "set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
135   gst_caps_unref (srccaps);
136 
137   depayload->clock_rate = 48000;
138 
139   return ret;
140 }
141 
142 static GstBuffer *
gst_rtp_opus_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp_buffer)143 gst_rtp_opus_depay_process (GstRTPBaseDepayload * depayload,
144     GstRTPBuffer * rtp_buffer)
145 {
146   GstBuffer *outbuf;
147 
148   outbuf = gst_rtp_buffer_get_payload_buffer (rtp_buffer);
149 
150   gst_rtp_drop_non_audio_meta (depayload, outbuf);
151 
152   return outbuf;
153 }
154 
155 gboolean
gst_rtp_opus_depay_plugin_init(GstPlugin * plugin)156 gst_rtp_opus_depay_plugin_init (GstPlugin * plugin)
157 {
158   return gst_element_register (plugin, "rtpopusdepay",
159       GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_DEPAY);
160 }
161