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.
23 * SECTION:element-rtpopuspay
26 * rtpopuspay encapsulates Opus-encoded audio data into RTP packets following
27 * the payload format described in RFC 7587.
29 * In addition to the RFC, which assumes only mono and stereo payload,
30 * the element supports multichannel Opus audio streams using a non-standardized
31 * SDP config and "multiopus" codec developed by Google for libwebrtc. When the
32 * input data have more than 2 channels, rtpopuspay will add extra fields to
33 * output caps that can be used to generate SDP in the syntax understood by
34 * libwebrtc. For example in the case of 5.1 audio:
37 * a=rtpmap:96 multiopus/48000/6
38 * a=fmtp:96 num_streams=4;coupled_streams=2;channel_mapping=0,4,1,2,3,5
41 * See https://webrtc-review.googlesource.com/c/src/+/129768 for more details on
42 * multichannel Opus in libwebrtc.
51 #include <gst/rtp/gstrtpbuffer.h>
52 #include <gst/audio/audio.h>
54 #include "gstrtpelements.h"
55 #include "gstrtpopuspay.h"
56 #include "gstrtputils.h"
58 GST_DEBUG_CATEGORY_STATIC (rtpopuspay_debug);
59 #define GST_CAT_DEFAULT (rtpopuspay_debug)
62 static GstStaticPadTemplate gst_rtp_opus_pay_sink_template =
63 GST_STATIC_PAD_TEMPLATE ("sink",
66 GST_STATIC_CAPS ("audio/x-opus, channel-mapping-family = (int) 0;"
67 "audio/x-opus, channel-mapping-family = (int) 0, channels = (int) [1, 2];"
68 "audio/x-opus, channel-mapping-family = (int) 1, channels = (int) [3, 255]")
71 static GstStaticPadTemplate gst_rtp_opus_pay_src_template =
72 GST_STATIC_PAD_TEMPLATE ("src",
75 GST_STATIC_CAPS ("application/x-rtp, "
76 "media = (string) \"audio\", "
77 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
78 "clock-rate = (int) 48000, "
79 "encoding-name = (string) { \"OPUS\", \"X-GST-OPUS-DRAFT-SPITTKA-00\", \"multiopus\" }")
82 static gboolean gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload,
84 static GstCaps *gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
85 GstPad * pad, GstCaps * filter);
86 static GstFlowReturn gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload *
87 payload, GstBuffer * buffer);
89 G_DEFINE_TYPE (GstRtpOPUSPay, gst_rtp_opus_pay, GST_TYPE_RTP_BASE_PAYLOAD);
90 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpopuspay, "rtpopuspay",
91 GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_PAY, rtp_element_init (plugin));
94 gst_rtp_opus_pay_class_init (GstRtpOPUSPayClass * klass)
96 GstRTPBasePayloadClass *gstbasertppayload_class;
97 GstElementClass *element_class;
99 gstbasertppayload_class = (GstRTPBasePayloadClass *) klass;
100 element_class = GST_ELEMENT_CLASS (klass);
102 gstbasertppayload_class->set_caps = gst_rtp_opus_pay_setcaps;
103 gstbasertppayload_class->get_caps = gst_rtp_opus_pay_getcaps;
104 gstbasertppayload_class->handle_buffer = gst_rtp_opus_pay_handle_buffer;
106 gst_element_class_add_static_pad_template (element_class,
107 &gst_rtp_opus_pay_src_template);
108 gst_element_class_add_static_pad_template (element_class,
109 &gst_rtp_opus_pay_sink_template);
111 gst_element_class_set_static_metadata (element_class,
112 "RTP Opus payloader",
113 "Codec/Payloader/Network/RTP",
114 "Puts Opus audio in RTP packets",
115 "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
117 GST_DEBUG_CATEGORY_INIT (rtpopuspay_debug, "rtpopuspay", 0,
118 "Opus RTP Payloader");
122 gst_rtp_opus_pay_init (GstRtpOPUSPay * rtpopuspay)
127 gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
131 GstStructure *s, *outcaps;
132 const char *encoding_name = "OPUS";
135 gchar *encoding_params;
137 outcaps = gst_structure_new_empty ("unused");
139 src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
144 s = gst_caps_get_structure (src_caps, 0);
146 if (gst_structure_has_field (s, "encoding-name")) {
147 GValue default_value = G_VALUE_INIT;
149 g_value_init (&default_value, G_TYPE_STRING);
150 g_value_set_static_string (&default_value, encoding_name);
152 value = gst_structure_get_value (s, "encoding-name");
153 if (!gst_value_can_intersect (&default_value, value))
154 encoding_name = "X-GST-OPUS-DRAFT-SPITTKA-00";
156 gst_caps_unref (src_caps);
159 s = gst_caps_get_structure (caps, 0);
160 if (gst_structure_get_int (s, "channels", &channels)) {
162 /* Implies channel-mapping-family = 1. */
164 gint stream_count, coupled_count;
165 const GValue *channel_mapping_array;
167 /* libwebrtc only supports "multiopus" when channels > 2. Mono and stereo
168 * sound must always be payloaded according to RFC 7587. */
169 encoding_name = "multiopus";
171 if (gst_structure_get_int (s, "stream-count", &stream_count)) {
172 char *num_streams = g_strdup_printf ("%d", stream_count);
173 gst_structure_set (outcaps, "num_streams", G_TYPE_STRING, num_streams,
175 g_free (num_streams);
177 if (gst_structure_get_int (s, "coupled-count", &coupled_count)) {
178 char *coupled_streams = g_strdup_printf ("%d", coupled_count);
179 gst_structure_set (outcaps, "coupled_streams", G_TYPE_STRING,
180 coupled_streams, NULL);
181 g_free (coupled_streams);
184 channel_mapping_array = gst_structure_get_value (s, "channel-mapping");
185 if (GST_VALUE_HOLDS_ARRAY (channel_mapping_array)) {
186 GString *str = g_string_new (NULL);
189 for (i = 0; i < gst_value_array_get_size (channel_mapping_array); ++i) {
191 g_string_append_c (str, ',');
193 g_string_append_printf (str, "%d",
194 g_value_get_int (gst_value_array_get_value (channel_mapping_array,
198 gst_structure_set (outcaps, "channel_mapping", G_TYPE_STRING, str->str,
201 g_string_free (str, TRUE);
204 gst_structure_set (outcaps, "sprop-stereo", G_TYPE_STRING,
205 (channels == 2) ? "1" : "0", NULL);
206 /* RFC 7587 requires the number of channels always be 2. */
211 encoding_params = g_strdup_printf ("%d", channels);
212 gst_structure_set (outcaps, "encoding-params", G_TYPE_STRING,
213 encoding_params, NULL);
214 g_free (encoding_params);
216 if (gst_structure_get_int (s, "rate", &rate)) {
217 gchar *sprop_maxcapturerate = g_strdup_printf ("%d", rate);
219 gst_structure_set (outcaps, "sprop-maxcapturerate", G_TYPE_STRING,
220 sprop_maxcapturerate, NULL);
222 g_free (sprop_maxcapturerate);
225 gst_rtp_base_payload_set_options (payload, "audio", FALSE,
226 encoding_name, 48000);
228 res = gst_rtp_base_payload_set_outcaps_structure (payload, outcaps);
230 gst_structure_free (outcaps);
236 gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload * basepayload,
240 GstClockTime pts, dts, duration;
242 pts = GST_BUFFER_PTS (buffer);
243 dts = GST_BUFFER_DTS (buffer);
244 duration = GST_BUFFER_DURATION (buffer);
246 outbuf = gst_rtp_base_payload_allocate_output_buffer (basepayload, 0, 0, 0);
248 gst_rtp_copy_audio_meta (basepayload, outbuf, buffer);
250 outbuf = gst_buffer_append (outbuf, buffer);
252 GST_BUFFER_PTS (outbuf) = pts;
253 GST_BUFFER_DTS (outbuf) = dts;
254 GST_BUFFER_DURATION (outbuf) = duration;
257 return gst_rtp_base_payload_push (basepayload, outbuf);
261 gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
262 GstPad * pad, GstCaps * filter)
264 GstCaps *caps, *peercaps, *tcaps;
268 if (pad == GST_RTP_BASE_PAYLOAD_SRCPAD (payload))
270 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
271 (payload, pad, filter);
273 tcaps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
274 peercaps = gst_pad_peer_query_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload),
276 gst_caps_unref (tcaps);
279 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
280 (payload, pad, filter);
282 if (gst_caps_is_empty (peercaps))
285 caps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SINKPAD (payload));
287 s = gst_caps_get_structure (peercaps, 0);
288 stereo = gst_structure_get_string (s, "stereo");
289 if (stereo != NULL) {
290 caps = gst_caps_make_writable (caps);
292 if (!strcmp (stereo, "1")) {
293 GstCaps *caps2 = gst_caps_copy (caps);
295 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
296 gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 1, NULL);
297 caps = gst_caps_merge (caps, caps2);
298 } else if (!strcmp (stereo, "0")) {
299 GstCaps *caps2 = gst_caps_copy (caps);
301 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
302 gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 2, NULL);
303 caps = gst_caps_merge (caps, caps2);
306 gst_caps_unref (peercaps);
309 GstCaps *tmp = gst_caps_intersect_full (caps, filter,
310 GST_CAPS_INTERSECT_FIRST);
311 gst_caps_unref (caps);
315 GST_DEBUG_OBJECT (payload, "Returning caps: %" GST_PTR_FORMAT, caps);