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"
33 GST_DEBUG_CATEGORY_STATIC (rtpopuspay_debug);
34 #define GST_CAT_DEFAULT (rtpopuspay_debug)
37 static GstStaticPadTemplate gst_rtp_opus_pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
42 ("audio/x-opus, channels = (int) [1, 2], 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)
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));
112 src_caps = gst_caps_make_writable (src_caps);
113 src_caps = gst_caps_truncate (src_caps);
114 s = gst_caps_get_structure (src_caps, 0);
115 gst_structure_fixate_field_string (s, "encoding-name", "OPUS");
116 encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
117 gst_caps_unref (src_caps);
119 encoding_name = g_strdup ("X-GST-OPUS-DRAFT-SPITTKA-00");
122 s = gst_caps_get_structure (caps, 0);
123 if (gst_structure_get_int (s, "channels", &channels)) {
125 GST_ERROR_OBJECT (payload,
126 "More than 2 channels with channel-mapping-family=0 is invalid");
128 } else if (channels == 2) {
135 if (gst_structure_get_int (s, "rate", &rate)) {
136 sprop_maxcapturerate = g_strdup_printf ("%d", rate);
139 gst_rtp_base_payload_set_options (payload, "audio", FALSE,
140 encoding_name, 48000);
141 g_free (encoding_name);
143 if (sprop_maxcapturerate && sprop_stereo) {
145 gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
146 G_TYPE_STRING, sprop_maxcapturerate, "sprop-stereo", G_TYPE_STRING,
148 } else if (sprop_maxcapturerate) {
150 gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
151 G_TYPE_STRING, sprop_maxcapturerate, NULL);
152 } else if (sprop_stereo) {
154 gst_rtp_base_payload_set_outcaps (payload, "sprop-stereo",
155 G_TYPE_STRING, sprop_stereo, NULL);
157 res = gst_rtp_base_payload_set_outcaps (payload, NULL);
160 g_free (sprop_maxcapturerate);
172 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
174 CopyMetaData *data = user_data;
175 GstRtpOPUSPay *pay = data->pay;
176 GstBuffer *outbuf = data->outbuf;
177 const GstMetaInfo *info = (*meta)->info;
178 const gchar *const *tags = gst_meta_api_type_get_tags (info->api);
180 if (!tags || (g_strv_length ((gchar **) tags) == 1
181 && gst_meta_api_type_has_tag (info->api,
182 g_quark_from_string (GST_META_TAG_AUDIO_STR)))) {
183 GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
184 GST_DEBUG_OBJECT (pay, "copy metadata %s", g_type_name (info->api));
185 /* simply copy then */
186 info->transform_func (outbuf, *meta, inbuf,
187 _gst_meta_transform_copy, ©_data);
189 GST_DEBUG_OBJECT (pay, "not copying metadata %s", g_type_name (info->api));
196 gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload * basepayload,
200 GstClockTime pts, dts, duration;
203 pts = GST_BUFFER_PTS (buffer);
204 dts = GST_BUFFER_DTS (buffer);
205 duration = GST_BUFFER_DURATION (buffer);
207 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
208 data.pay = GST_RTP_OPUS_PAY (basepayload);
209 data.outbuf = outbuf;
210 gst_buffer_foreach_meta (buffer, foreach_metadata, &data);
211 outbuf = gst_buffer_append (outbuf, buffer);
213 GST_BUFFER_PTS (outbuf) = pts;
214 GST_BUFFER_DTS (outbuf) = dts;
215 GST_BUFFER_DURATION (outbuf) = duration;
218 return gst_rtp_base_payload_push (basepayload, outbuf);
222 gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
223 GstPad * pad, GstCaps * filter)
225 GstCaps *caps, *peercaps, *tcaps;
229 if (pad == GST_RTP_BASE_PAYLOAD_SRCPAD (payload))
231 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
232 (payload, pad, filter);
234 tcaps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
235 peercaps = gst_pad_peer_query_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload),
237 gst_caps_unref (tcaps);
240 GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
241 (payload, pad, filter);
243 if (gst_caps_is_empty (peercaps))
246 caps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SINKPAD (payload));
248 s = gst_caps_get_structure (peercaps, 0);
249 stereo = gst_structure_get_string (s, "stereo");
250 if (stereo != NULL) {
251 caps = gst_caps_make_writable (caps);
253 if (!strcmp (stereo, "1")) {
254 GstCaps *caps2 = gst_caps_copy (caps);
256 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
257 gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 1, NULL);
258 caps = gst_caps_merge (caps, caps2);
259 } else if (!strcmp (stereo, "0")) {
260 GstCaps *caps2 = gst_caps_copy (caps);
262 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
263 gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 2, NULL);
264 caps = gst_caps_merge (caps, caps2);
267 gst_caps_unref (peercaps);
270 GstCaps *tmp = gst_caps_intersect_full (caps, filter,
271 GST_CAPS_INTERSECT_FIRST);
272 gst_caps_unref (caps);
276 GST_DEBUG_OBJECT (payload, "Returning caps: %" GST_PTR_FORMAT, caps);
281 gst_rtp_opus_pay_plugin_init (GstPlugin * plugin)
283 return gst_element_register (plugin, "rtpopuspay",
284 GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_PAY);