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