rtpvorbisdepay: remove dead code
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpopusdepay.c
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
32 GST_DEBUG_CATEGORY_STATIC (rtpopusdepay_debug);
33 #define GST_CAT_DEFAULT (rtpopusdepay_debug)
34
35 static GstStaticPadTemplate gst_rtp_opus_depay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
37     GST_PAD_SINK,
38     GST_PAD_ALWAYS,
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\" }")
44     );
45
46 static GstStaticPadTemplate gst_rtp_opus_depay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("audio/x-opus, channel-mapping-family = (int) 0")
51     );
52
53 static GstBuffer *gst_rtp_opus_depay_process (GstRTPBaseDepayload * depayload,
54     GstBuffer * buf);
55 static gboolean gst_rtp_opus_depay_setcaps (GstRTPBaseDepayload * depayload,
56     GstCaps * caps);
57
58 G_DEFINE_TYPE (GstRTPOpusDepay, gst_rtp_opus_depay,
59     GST_TYPE_RTP_BASE_DEPAYLOAD);
60
61 static void
62 gst_rtp_opus_depay_class_init (GstRTPOpusDepayClass * klass)
63 {
64   GstRTPBaseDepayloadClass *gstbasertpdepayload_class;
65   GstElementClass *element_class;
66
67   element_class = GST_ELEMENT_CLASS (klass);
68   gstbasertpdepayload_class = (GstRTPBaseDepayloadClass *) klass;
69
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>");
78
79   gstbasertpdepayload_class->process = gst_rtp_opus_depay_process;
80   gstbasertpdepayload_class->set_caps = gst_rtp_opus_depay_setcaps;
81
82   GST_DEBUG_CATEGORY_INIT (rtpopusdepay_debug, "rtpopusdepay", 0,
83       "Opus RTP Depayloader");
84 }
85
86 static void
87 gst_rtp_opus_depay_init (GstRTPOpusDepay * rtpopusdepay)
88 {
89
90 }
91
92 static gboolean
93 gst_rtp_opus_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
94 {
95   GstCaps *srccaps;
96   GstStructure *s;
97   gboolean ret;
98   const gchar *sprop_stereo, *sprop_maxcapturerate;
99
100   srccaps =
101       gst_caps_new_simple ("audio/x-opus", "channel-mapping-family", G_TYPE_INT,
102       0, NULL);
103
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);
110     else
111       GST_WARNING_OBJECT (depayload, "Unknown sprop-stereo value '%s'",
112           sprop_stereo);
113   }
114
115   if ((sprop_maxcapturerate =
116           gst_structure_get_string (s, "sprop-maxcapturerate"))) {
117     gulong rate;
118     gchar *tailptr;
119
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);
125     } else {
126       gst_caps_set_simple (srccaps, "rate", G_TYPE_INT, rate, NULL);
127     }
128   }
129
130   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
131
132   GST_DEBUG_OBJECT (depayload,
133       "set caps on source: %" GST_PTR_FORMAT " (ret=%d)", srccaps, ret);
134   gst_caps_unref (srccaps);
135
136   depayload->clock_rate = 48000;
137
138   return ret;
139 }
140
141 static gboolean
142 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
143 {
144   GstRTPOpusDepay *depay = user_data;
145   const GstMetaInfo *info = (*meta)->info;
146   const gchar *const *tags = gst_meta_api_type_get_tags (info->api);
147
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));
152   } else {
153     GST_DEBUG_OBJECT (depay, "dropping metadata %s", g_type_name (info->api));
154     *meta = NULL;
155   }
156
157   return TRUE;
158 }
159
160 static GstBuffer *
161 gst_rtp_opus_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
162 {
163   GstBuffer *outbuf;
164   GstRTPBuffer rtpbuf = { NULL, };
165
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);
169
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);
173
174   return outbuf;
175 }
176
177 gboolean
178 gst_rtp_opus_depay_plugin_init (GstPlugin * plugin)
179 {
180   return gst_element_register (plugin, "rtpopusdepay",
181       GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_DEPAY);
182 }