Merge the tizen patch and fix build err based on 1.12.2
[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
42     ("audio/x-opus, channels = (int) [1, 2], channel-mapping-family = (int) 0")
43     );
44
45 static GstStaticPadTemplate gst_rtp_opus_pay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
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\" }")
55     );
56
57 static gboolean gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload,
58     GstCaps * caps);
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);
63
64 G_DEFINE_TYPE (GstRtpOPUSPay, gst_rtp_opus_pay, GST_TYPE_RTP_BASE_PAYLOAD);
65
66 static void
67 gst_rtp_opus_pay_class_init (GstRtpOPUSPayClass * klass)
68 {
69   GstRTPBasePayloadClass *gstbasertppayload_class;
70   GstElementClass *element_class;
71
72   gstbasertppayload_class = (GstRTPBasePayloadClass *) klass;
73   element_class = GST_ELEMENT_CLASS (klass);
74
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;
78
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);
83
84   gst_element_class_set_static_metadata (element_class,
85       "RTP Opus payloader",
86       "Codec/Payloader/Network/RTP",
87       "Puts Opus audio in RTP packets",
88       "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
89
90   GST_DEBUG_CATEGORY_INIT (rtpopuspay_debug, "rtpopuspay", 0,
91       "Opus RTP Payloader");
92 }
93
94 static void
95 gst_rtp_opus_pay_init (GstRtpOPUSPay * rtpopuspay)
96 {
97 }
98
99 static gboolean
100 gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
101 {
102   gboolean res;
103   GstCaps *src_caps;
104   GstStructure *s;
105   char *encoding_name;
106   gint channels, rate;
107   const char *sprop_stereo = NULL;
108   char *sprop_maxcapturerate = NULL;
109
110   src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
111   if (src_caps) {
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);
118   } else {
119     encoding_name = g_strdup ("X-GST-OPUS-DRAFT-SPITTKA-00");
120   }
121
122   s = gst_caps_get_structure (caps, 0);
123   if (gst_structure_get_int (s, "channels", &channels)) {
124     if (channels > 2) {
125       GST_ERROR_OBJECT (payload,
126           "More than 2 channels with channel-mapping-family=0 is invalid");
127       return FALSE;
128     } else if (channels == 2) {
129       sprop_stereo = "1";
130     } else {
131       sprop_stereo = "0";
132     }
133   }
134
135   if (gst_structure_get_int (s, "rate", &rate)) {
136     sprop_maxcapturerate = g_strdup_printf ("%d", rate);
137   }
138
139   gst_rtp_base_payload_set_options (payload, "audio", FALSE,
140       encoding_name, 48000);
141   g_free (encoding_name);
142
143   if (sprop_maxcapturerate && sprop_stereo) {
144     res =
145         gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
146         G_TYPE_STRING, sprop_maxcapturerate, "sprop-stereo", G_TYPE_STRING,
147         sprop_stereo, NULL);
148   } else if (sprop_maxcapturerate) {
149     res =
150         gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
151         G_TYPE_STRING, sprop_maxcapturerate, NULL);
152   } else if (sprop_stereo) {
153     res =
154         gst_rtp_base_payload_set_outcaps (payload, "sprop-stereo",
155         G_TYPE_STRING, sprop_stereo, NULL);
156   } else {
157     res = gst_rtp_base_payload_set_outcaps (payload, NULL);
158   }
159
160   g_free (sprop_maxcapturerate);
161
162   return res;
163 }
164
165 typedef struct
166 {
167   GstRtpOPUSPay *pay;
168   GstBuffer *outbuf;
169 } CopyMetaData;
170
171 static gboolean
172 foreach_metadata (GstBuffer * inbuf, GstMeta ** meta, gpointer user_data)
173 {
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);
179
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, &copy_data);
188   } else {
189     GST_DEBUG_OBJECT (pay, "not copying metadata %s", g_type_name (info->api));
190   }
191
192   return TRUE;
193 }
194
195 static GstFlowReturn
196 gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload * basepayload,
197     GstBuffer * buffer)
198 {
199   GstBuffer *outbuf;
200   GstClockTime pts, dts, duration;
201   CopyMetaData data;
202
203   pts = GST_BUFFER_PTS (buffer);
204   dts = GST_BUFFER_DTS (buffer);
205   duration = GST_BUFFER_DURATION (buffer);
206
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);
212
213   GST_BUFFER_PTS (outbuf) = pts;
214   GST_BUFFER_DTS (outbuf) = dts;
215   GST_BUFFER_DURATION (outbuf) = duration;
216
217   /* Push out */
218   return gst_rtp_base_payload_push (basepayload, outbuf);
219 }
220
221 static GstCaps *
222 gst_rtp_opus_pay_getcaps (GstRTPBasePayload * payload,
223     GstPad * pad, GstCaps * filter)
224 {
225   GstCaps *caps, *peercaps, *tcaps;
226   GstStructure *s;
227   const gchar *stereo;
228
229   if (pad == GST_RTP_BASE_PAYLOAD_SRCPAD (payload))
230     return
231         GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
232         (payload, pad, filter);
233
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),
236       tcaps);
237   gst_caps_unref (tcaps);
238   if (!peercaps)
239     return
240         GST_RTP_BASE_PAYLOAD_CLASS (gst_rtp_opus_pay_parent_class)->get_caps
241         (payload, pad, filter);
242
243   if (gst_caps_is_empty (peercaps))
244     return peercaps;
245
246   caps = gst_pad_get_pad_template_caps (GST_RTP_BASE_PAYLOAD_SINKPAD (payload));
247
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);
252
253     if (!strcmp (stereo, "1")) {
254       GstCaps *caps2 = gst_caps_copy (caps);
255
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);
261
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);
265     }
266   }
267   gst_caps_unref (peercaps);
268
269   if (filter) {
270     GstCaps *tmp = gst_caps_intersect_full (caps, filter,
271         GST_CAPS_INTERSECT_FIRST);
272     gst_caps_unref (caps);
273     caps = tmp;
274   }
275
276   GST_DEBUG_OBJECT (payload, "Returning caps: %" GST_PTR_FORMAT, caps);
277   return caps;
278 }
279
280 gboolean
281 gst_rtp_opus_pay_plugin_init (GstPlugin * plugin)
282 {
283   return gst_element_register (plugin, "rtpopuspay",
284       GST_RANK_PRIMARY, GST_TYPE_RTP_OPUS_PAY);
285 }