opus: Copy metadata in the (de)payloader, but only the relevant ones
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpopuspay.c
1 /*
2  * Opus Payloader 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
28 #include <gst/rtp/gstrtpbuffer.h>
29 #include <gst/audio/audio.h>
30
31 #include "gstrtpopuspay.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpopuspay_debug);
34 #define GST_CAT_DEFAULT (rtpopuspay_debug)
35
36
37 static GstStaticPadTemplate gst_rtp_opus_pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
39     GST_PAD_SINK,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("audio/x-opus, multistream = (boolean) FALSE")
42     );
43
44 static GstStaticPadTemplate gst_rtp_opus_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "media = (string) \"audio\", "
50         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51         "clock-rate = (int) 48000, "
52         "encoding-params = (string) \"2\", "
53         "encoding-name = (string) { \"OPUS\", \"X-GST-OPUS-DRAFT-SPITTKA-00\" }")
54     );
55
56 static gboolean gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload,
57     GstCaps * caps);
58 static GstCaps *gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
59     GstPad * pad, GstCaps * filter);
60 static GstFlowReturn gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload *
61     payload, GstBuffer * buffer);
62
63 G_DEFINE_TYPE (GstRtpOPUSPay, gst_rtp_opus_pay, GST_TYPE_RTP_BASE_PAYLOAD);
64
65 static void
66 gst_rtp_opus_pay_class_init (GstRtpOPUSPayClass * klass)
67 {
68   GstRTPBasePayloadClass *gstbasertppayload_class;
69   GstElementClass *element_class;
70
71   gstbasertppayload_class = (GstRTPBasePayloadClass *) klass;
72   element_class = GST_ELEMENT_CLASS (klass);
73
74   gstbasertppayload_class->set_caps = gst_rtp_opus_pay_setcaps;
75   gstbasertppayload_class->get_caps = gst_rtp_opus_pay_getcaps;
76   gstbasertppayload_class->handle_buffer = gst_rtp_opus_pay_handle_buffer;
77
78   gst_element_class_add_pad_template (element_class,
79       gst_static_pad_template_get (&gst_rtp_opus_pay_src_template));
80   gst_element_class_add_pad_template (element_class,
81       gst_static_pad_template_get (&gst_rtp_opus_pay_sink_template));
82
83   gst_element_class_set_static_metadata (element_class,
84       "RTP Opus payloader",
85       "Codec/Payloader/Network/RTP",
86       "Puts Opus audio in RTP packets",
87       "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
88
89   GST_DEBUG_CATEGORY_INIT (rtpopuspay_debug, "rtpopuspay", 0,
90       "Opus RTP Payloader");
91 }
92
93 static void
94 gst_rtp_opus_pay_init (GstRtpOPUSPay * rtpopuspay)
95 {
96 }
97
98 static gboolean
99 gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
100 {
101   gboolean res;
102   GstCaps *src_caps;
103   GstStructure *s;
104   char *encoding_name;
105   gint channels, rate;
106   const char *sprop_stereo = NULL;
107   char *sprop_maxcapturerate = NULL;
108
109   src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
110   if (src_caps) {
111     src_caps = gst_caps_make_writable (src_caps);
112     src_caps = gst_caps_truncate (src_caps);
113     s = gst_caps_get_structure (src_caps, 0);
114     gst_structure_fixate_field_string (s, "encoding-name", "OPUS");
115     encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
116     gst_caps_unref (src_caps);
117   } else {
118     encoding_name = g_strdup ("X-GST-OPUS-DRAFT-SPITTKA-00");
119   }
120
121   s = gst_caps_get_structure (caps, 0);
122   if (gst_structure_get_int (s, "channels", &channels)) {
123     if (channels > 2) {
124       GST_ERROR_OBJECT (payload,
125           "More than 2 channels with multistream=FALSE is invalid");
126       return FALSE;
127     } else if (channels == 2) {
128       sprop_stereo = "1";
129     } else {
130       sprop_stereo = "0";
131     }
132   }
133
134   if (gst_structure_get_int (s, "rate", &rate)) {
135     sprop_maxcapturerate = g_strdup_printf ("%d", rate);
136   }
137
138   gst_rtp_base_payload_set_options (payload, "audio", FALSE,
139       encoding_name, 48000);
140   g_free (encoding_name);
141
142   if (sprop_maxcapturerate && sprop_stereo) {
143     res =
144         gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
145         G_TYPE_STRING, sprop_maxcapturerate, "sprop-stereo", G_TYPE_STRING,
146         sprop_stereo, NULL);
147   } else if (sprop_maxcapturerate) {
148     res =
149         gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
150         G_TYPE_STRING, sprop_maxcapturerate, NULL);
151   } else if (sprop_stereo) {
152     res =
153         gst_rtp_base_payload_set_outcaps (payload, "sprop-stereo",
154         G_TYPE_STRING, sprop_stereo, NULL);
155   } else {
156     res = gst_rtp_base_payload_set_outcaps (payload, NULL);
157   }
158
159   g_free (sprop_maxcapturerate);
160
161   return res;
162 }
163
164 typedef struct
165 {
166   GstRtpOPUSPay *pay;
167   GstBuffer *outbuf;
168 } CopyMetaData;
169
170 static gboolean
171 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
172 {
173   CopyMetaData *data = user_data;
174   GstRtpOPUSPay *pay = data->pay;
175   GstBuffer *outbuf = data->outbuf;
176   const GstMetaInfo *info = (*meta)->info;
177   const gchar *const *tags = gst_meta_api_type_get_tags (info->api);
178
179   if (!tags || (g_strv_length ((gchar **) tags) == 1
180           && gst_meta_api_type_has_tag (info->api,
181               g_quark_from_string (GST_META_TAG_AUDIO_STR)))) {
182     GstMetaTransformCopy copy_data = { FALSE, 0, -1 };
183     GST_DEBUG_OBJECT (pay, "copy metadata %s", g_type_name (info->api));
184     /* simply copy then */
185     info->transform_func (outbuf, *meta, inbuf,
186         _gst_meta_transform_copy, &copy_data);
187   } else {
188     GST_DEBUG_OBJECT (pay, "not copying metadata %s", g_type_name (info->api));
189   }
190
191   return TRUE;
192 }
193
194 static GstFlowReturn
195 gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload * basepayload,
196     GstBuffer * buffer)
197 {
198   GstBuffer *outbuf;
199   GstClockTime pts, dts, duration;
200   CopyMetaData data;
201
202   pts = GST_BUFFER_PTS (buffer);
203   dts = GST_BUFFER_DTS (buffer);
204   duration = GST_BUFFER_DURATION (buffer);
205
206   outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
207   data.pay = GST_RTP_OPUS_PAY (basepayload);
208   data.outbuf = outbuf;
209   gst_buffer_foreach_meta (buffer, foreach_metadata, &data);
210   outbuf = gst_buffer_append (outbuf, buffer);
211
212   GST_BUFFER_PTS (outbuf) = pts;
213   GST_BUFFER_DTS (outbuf) = dts;
214   GST_BUFFER_DURATION (outbuf) = duration;
215
216   /* Push out */
217   return gst_rtp_base_payload_push (basepayload, outbuf);
218 }
219
220 static GstCaps *
221 gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
222     GstPad * pad, GstCaps * filter)
223 {
224   GstCaps *caps, *peercaps, *tcaps;
225   GstStructure *s;
226   const gchar *stereo;
227
228   if (pad == GST_RTP_BASE_PAYLOAD_SRCPAD (payload))
229     return
230         GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
231         (payload, pad, filter);
232
233   tcaps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
234   peercaps = gst_pad_peer_query_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload),
235       tcaps);
236   gst_caps_unref (tcaps);
237   if (!peercaps)
238     return
239         GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
240         (payload, pad, filter);
241
242   if (gst_caps_is_empty (peercaps))
243     return peercaps;
244
245   caps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SINKPAD (payload));
246
247   s = gst_caps_get_structure (peercaps, 0);
248   stereo = gst_structure_get_string (s, "stereo");
249   if (stereo != NULL) {
250     caps = gst_caps_make_writable (caps);
251
252     if (!strcmp (stereo, "1")) {
253       GstCaps *caps2 = gst_caps_copy (caps);
254
255       gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
256       gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 1, NULL);
257       caps = gst_caps_merge (caps, caps2);
258     } else if (!strcmp (stereo, "0")) {
259       GstCaps *caps2 = gst_caps_copy (caps);
260
261       gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
262       gst_caps_set_simple (caps2, "channels", G_TYPE_INT, 2, NULL);
263       caps = gst_caps_merge (caps, caps2);
264     }
265   }
266   gst_caps_unref (peercaps);
267
268   if (filter) {
269     GstCaps *tmp = gst_caps_intersect_full (caps, filter,
270         GST_CAPS_INTERSECT_FIRST);
271     gst_caps_unref (caps);
272     caps = tmp;
273   }
274
275   GST_DEBUG_OBJECT (payload, "Returning caps: %" GST_PTR_FORMAT, caps);
276   return caps;
277 }