rtpopuspay: Set the number of channels to 2 as per RFC draft
[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 GstFlowReturn gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload *
58     payload, GstBuffer * buffer);
59
60 G_DEFINE_TYPE (GstRtpOPUSPay, gst_rtp_opus_pay, GST_TYPE_RTP_BASE_PAYLOAD);
61
62 static void
63 gst_rtp_opus_pay_class_init (GstRtpOPUSPayClass * klass)
64 {
65   GstRTPBasePayloadClass *gstbasertppayload_class;
66   GstElementClass *element_class;
67
68   gstbasertppayload_class = (GstRTPBasePayloadClass *) klass;
69   element_class = GST_ELEMENT_CLASS (klass);
70
71   gstbasertppayload_class->set_caps = gst_rtp_opus_pay_setcaps;
72   gstbasertppayload_class->handle_buffer = gst_rtp_opus_pay_handle_buffer;
73
74   gst_element_class_add_pad_template (element_class,
75       gst_static_pad_template_get (&gst_rtp_opus_pay_src_template));
76   gst_element_class_add_pad_template (element_class,
77       gst_static_pad_template_get (&gst_rtp_opus_pay_sink_template));
78
79   gst_element_class_set_static_metadata (element_class,
80       "RTP Opus payloader",
81       "Codec/Payloader/Network/RTP",
82       "Puts Opus audio in RTP packets",
83       "Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>");
84
85   GST_DEBUG_CATEGORY_INIT (rtpopuspay_debug, "rtpopuspay", 0,
86       "Opus RTP Payloader");
87 }
88
89 static void
90 gst_rtp_opus_pay_init (GstRtpOPUSPay * rtpopuspay)
91 {
92 }
93
94 static gboolean
95 gst_rtp_opus_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
96 {
97   gboolean res;
98   GstCaps *src_caps;
99   GstStructure *s;
100   char *encoding_name;
101   gint channels, rate;
102   const char *sprop_stereo = NULL;
103   char *sprop_maxcapturerate = NULL;
104
105   src_caps = gst_pad_get_allowed_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload));
106   if (src_caps) {
107     src_caps = gst_caps_make_writable (src_caps);
108     src_caps = gst_caps_truncate (src_caps);
109     s = gst_caps_get_structure (src_caps, 0);
110     gst_structure_fixate_field_string (s, "encoding-name", "OPUS");
111     encoding_name = g_strdup (gst_structure_get_string (s, "encoding-name"));
112     gst_caps_unref (src_caps);
113   } else {
114     encoding_name = g_strdup ("X-GST-OPUS-DRAFT-SPITTKA-00");
115   }
116
117   s = gst_caps_get_structure (caps, 0);
118   if (gst_structure_get_int (s, "channels", &channels)) {
119     if (channels > 2) {
120       GST_ERROR_OBJECT (payload, "More than 2 channels are not supported yet");
121       return FALSE;
122     } else if (channels == 2) {
123       sprop_stereo = "1";
124     } else {
125       sprop_stereo = "0";
126     }
127   }
128
129   if (gst_structure_get_int (s, "rate", &rate)) {
130     sprop_maxcapturerate = g_strdup_printf ("%d", rate);
131   }
132
133   gst_rtp_base_payload_set_options (payload, "audio", FALSE,
134       encoding_name, 48000);
135   g_free (encoding_name);
136
137   if (sprop_maxcapturerate && sprop_stereo) {
138     res =
139         gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
140         G_TYPE_STRING, sprop_maxcapturerate, "sprop-stereo", G_TYPE_STRING,
141         sprop_stereo, NULL);
142   } else if (sprop_maxcapturerate) {
143     res =
144         gst_rtp_base_payload_set_outcaps (payload, "sprop-maxcapturerate",
145         G_TYPE_STRING, sprop_maxcapturerate, NULL);
146   } else if (sprop_stereo) {
147     res =
148         gst_rtp_base_payload_set_outcaps (payload, "sprop-stereo",
149         G_TYPE_STRING, sprop_stereo, NULL);
150   } else {
151     res = gst_rtp_base_payload_set_outcaps (payload, NULL);
152   }
153
154   g_free (sprop_maxcapturerate);
155
156   return res;
157 }
158
159 static GstFlowReturn
160 gst_rtp_opus_pay_handle_buffer (GstRTPBasePayload * basepayload,
161     GstBuffer * buffer)
162 {
163   GstBuffer *outbuf;
164   GstClockTime pts, dts, duration;
165
166   pts = GST_BUFFER_PTS (buffer);
167   dts = GST_BUFFER_DTS (buffer);
168   duration = GST_BUFFER_DURATION (buffer);
169
170   outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
171   outbuf = gst_buffer_append (outbuf, buffer);
172
173   GST_BUFFER_PTS (outbuf) = pts;
174   GST_BUFFER_DTS (outbuf) = dts;
175   GST_BUFFER_DURATION (outbuf) = duration;
176
177   /* Push out */
178   return gst_rtp_base_payload_push (basepayload, outbuf);
179 }