2 * Opus Payloader 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>
31 #include "gstrtpopuspay.h"
32 #include "gstrtputils.h"
34 GST_DEBUG_CATEGORY_STATIC (rtpopuspay_debug);
35 #define GST_CAT_DEFAULT (rtpopuspay_debug)
38 static GstStaticPadTemplate gst_rtp_opus_pay_sink_template =
39 GST_STATIC_PAD_TEMPLATE ("sink",
42 GST_STATIC_CAPS ("audio/x-opus, channel-mapping-family = (int) 0")
45 static GstStaticPadTemplate gst_rtp_opus_pay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("application/x-rtp, "
50 "media = (string) \"audio\", "
51 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52 "clock-rate = (int) 48000, "
53 "encoding-params = (string) \"2\", "
54 "encoding-name = (string) { \"OPUS\", \"X-GST-OPUS-DRAFT-SPITTKA-00\" }")
57 static gboolean gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload,
59 static GstCaps *gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
60 GstPad * pad, GstCaps * filter);
61 static GstFlowReturn gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload *
62 payload, GstBuffer * buffer);
64 G_DEFINE_TYPE (GstRtpOPUSPay, gst_rtp_opus_pay, GST_TYPE_RTP_BASE_PAYLOAD);
67 gst_rtp_opus_pay_class_init (GstRtpOPUSPayClass * klass)
69 GstRTPBasePayloadClass *gstbasertppayload_class;
70 GstElementClass *element_class;
72 gstbasertppayload_class = (GstRTPBasePayloadClass *) klass;
73 element_class = GST_ELEMENT_CLASS (klass);
75 gstbasertppayload_class->set_caps = gst_rtp_opus_pay_setcaps;
76 gstbasertppayload_class->get_caps = gst_rtp_opus_pay_getcaps;
77 gstbasertppayload_class->handle_buffer = gst_rtp_opus_pay_handle_buffer;
79 gst_element_class_add_static_pad_template (element_class,
80 &gst_rtp_opus_pay_src_template);
81 gst_element_class_add_static_pad_template (element_class,
82 &gst_rtp_opus_pay_sink_template);
84 gst_element_class_set_static_metadata (element_class,
86 "Codec/Payloader/Network/RTP",
87 "Puts Opus audio in RTP packets",
88 "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
90 GST_DEBUG_CATEGORY_INIT (rtpopuspay_debug, "rtpopuspay", 0,
91 "Opus RTP Payloader");
95 gst_rtp_opus_pay_init (GstRtpOPUSPay * rtpopuspay)
100 gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
105 const char *encoding_name = "OPUS";
107 const char *sprop_stereo = NULL;
108 char *sprop_maxcapturerate = NULL;
110 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
115 s = gst_caps_get_structure (src_caps, 0);
117 if (gst_structure_has_field (s, "encoding-name")) {
118 GValue default_value = G_VALUE_INIT;
120 g_value_init (&default_value, G_TYPE_STRING);
121 g_value_set_static_string (&default_value, encoding_name);
123 value = gst_structure_get_value (s, "encoding-name");
124 if (!gst_value_can_intersect (&default_value, value))
125 encoding_name = "X-GST-OPUS-DRAFT-SPITTKA-00";
127 gst_caps_unref (src_caps);
130 s = gst_caps_get_structure (caps, 0);
131 if (gst_structure_get_int (s, "channels", &channels)) {
133 GST_ERROR_OBJECT (payload,
134 "More than 2 channels with channel-mapping-family=0 is invalid");
136 } else if (channels == 2) {
143 if (gst_structure_get_int (s, "rate", &rate)) {
144 sprop_maxcapturerate = g_strdup_printf ("%d", rate);
147 gst_rtp_base_payload_set_options (payload, "audio", FALSE,
148 encoding_name, 48000);
150 if (sprop_maxcapturerate && sprop_stereo) {
152 gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
153 G_TYPE_STRING, sprop_maxcapturerate, "sprop-stereo", G_TYPE_STRING,
155 } else if (sprop_maxcapturerate) {
157 gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
158 G_TYPE_STRING, sprop_maxcapturerate, NULL);
159 } else if (sprop_stereo) {
161 gst_rtp_base_payload_set_outcaps (payload, "sprop-stereo",
162 G_TYPE_STRING, sprop_stereo, NULL);
164 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
167 g_free (sprop_maxcapturerate);
173 gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload * basepayload,
177 GstClockTime pts, dts, duration;
179 pts = GST_BUFFER_PTS (buffer);
180 dts = GST_BUFFER_DTS (buffer);
181 duration = GST_BUFFER_DURATION (buffer);
183 outbuf = gst_rtp_base_payload_allocate_output_buffer (basepayload, 0, 0, 0);
185 gst_rtp_copy_audio_meta (basepayload, outbuf, buffer);
187 outbuf = gst_buffer_append (outbuf, buffer);
189 GST_BUFFER_PTS (outbuf) = pts;
190 GST_BUFFER_DTS (outbuf) = dts;
191 GST_BUFFER_DURATION (outbuf) = duration;
194 return gst_rtp_base_payload_push (basepayload, outbuf);
198 gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
199 GstPad * pad, GstCaps * filter)
201 GstCaps *caps, *peercaps, *tcaps;
205 if (pad == GST_RTP_BASE_PAYLOAD_SRCPAD (payload))
207 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
208 (payload, pad, filter);
210 tcaps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
211 peercaps = gst_pad_peer_query_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload),
213 gst_caps_unref (tcaps);
216 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
217 (payload, pad, filter);
219 if (gst_caps_is_empty (peercaps))
222 caps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SINKPAD (payload));
224 s = gst_caps_get_structure (peercaps, 0);
225 stereo = gst_structure_get_string (s, "stereo");
226 if (stereo != NULL) {
227 caps = gst_caps_make_writable (caps);
229 if (!strcmp (stereo, "1")) {
230 GstCaps *caps2 = gst_caps_copy (caps);
232 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
233 gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 1, NULL);
234 caps = gst_caps_merge (caps, caps2);
235 } else if (!strcmp (stereo, "0")) {
236 GstCaps *caps2 = gst_caps_copy (caps);
238 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
239 gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 2, NULL);
240 caps = gst_caps_merge (caps, caps2);
243 gst_caps_unref (peercaps);
246 GstCaps *tmp = gst_caps_intersect_full (caps, filter,
247 GST_CAPS_INTERSECT_FIRST);
248 gst_caps_unref (caps);
252 GST_DEBUG_OBJECT (payload, "Returning caps: %" GST_PTR_FORMAT, caps);
257 gst_rtp_opus_pay_plugin_init (GstPlugin * plugin)
259 return gst_element_register (plugin, "rtpopuspay",
260 GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_PAY);