webrtc/nice: support consent-freshness RFC7675
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpL16pay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-rtpL16pay
22  * @title: rtpL16pay
23  * @see_also: rtpL16depay
24  *
25  * Payload raw audio into RTP packets according to RFC 3551.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3551.txt
27  *
28  * ## Example pipeline
29  * |[
30  * gst-launch-1.0 -v audiotestsrc ! audioconvert ! rtpL16pay ! udpsink
31  * ]| This example pipeline will payload raw audio. Refer to
32  * the rtpL16depay example to depayload and play the RTP stream.
33  *
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include <string.h>
41
42 #include <gst/audio/audio.h>
43 #include <gst/rtp/gstrtpbuffer.h>
44
45 #include "gstrtpelements.h"
46 #include "gstrtpL16pay.h"
47 #include "gstrtpchannels.h"
48
49 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
50 #define GST_CAT_DEFAULT (rtpL16pay_debug)
51
52 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("audio/x-raw, "
57         "format = (string) S16BE, "
58         "layout = (string) interleaved, "
59         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
60     );
61
62 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
63     GST_STATIC_PAD_TEMPLATE ("src",
64     GST_PAD_SRC,
65     GST_PAD_ALWAYS,
66     GST_STATIC_CAPS ("application/x-rtp, "
67         "media = (string) \"audio\", "
68         "payload = (int) [ 96, 127 ], "
69         "clock-rate = (int) [ 1, MAX ], "
70         "encoding-name = (string) \"L16\", "
71         "channels = (int) [ 1, MAX ];"
72         "application/x-rtp, "
73         "media = (string) \"audio\", "
74         "encoding-name = (string) \"L16\", "
75         "payload = (int) " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
76         "clock-rate = (int) 44100;"
77         "application/x-rtp, "
78         "media = (string) \"audio\", "
79         "encoding-name = (string) \"L16\", "
80         "payload = (int) " GST_RTP_PAYLOAD_L16_MONO_STRING ", "
81         "clock-rate = (int) 44100")
82     );
83
84 static gboolean gst_rtp_L16_pay_setcaps (GstRTPBasePayload * basepayload,
85     GstCaps * caps);
86 static GstCaps *gst_rtp_L16_pay_getcaps (GstRTPBasePayload * rtppayload,
87     GstPad * pad, GstCaps * filter);
88 static GstFlowReturn
89 gst_rtp_L16_pay_handle_buffer (GstRTPBasePayload * basepayload,
90     GstBuffer * buffer);
91
92 #define gst_rtp_L16_pay_parent_class parent_class
93 G_DEFINE_TYPE (GstRtpL16Pay, gst_rtp_L16_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
94 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpL16pay, "rtpL16pay",
95     GST_RANK_SECONDARY, GST_TYPE_RTP_L16_PAY, rtp_element_init (plugin));
96
97 static void
98 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
99 {
100   GstElementClass *gstelement_class;
101   GstRTPBasePayloadClass *gstrtpbasepayload_class;
102
103   gstelement_class = (GstElementClass *) klass;
104   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
105
106   gstrtpbasepayload_class->set_caps = gst_rtp_L16_pay_setcaps;
107   gstrtpbasepayload_class->get_caps = gst_rtp_L16_pay_getcaps;
108   gstrtpbasepayload_class->handle_buffer = gst_rtp_L16_pay_handle_buffer;
109
110   gst_element_class_add_static_pad_template (gstelement_class,
111       &gst_rtp_L16_pay_src_template);
112   gst_element_class_add_static_pad_template (gstelement_class,
113       &gst_rtp_L16_pay_sink_template);
114
115   gst_element_class_set_static_metadata (gstelement_class,
116       "RTP audio payloader", "Codec/Payloader/Network/RTP",
117       "Payload-encode Raw audio into RTP packets (RFC 3551)",
118       "Wim Taymans <wim.taymans@gmail.com>");
119
120   GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
121       "L16 RTP Payloader");
122 }
123
124 static void
125 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
126 {
127   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
128
129   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpL16pay);
130
131   /* tell rtpbaseaudiopayload that this is a sample based codec */
132   gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
133 }
134
135 static gboolean
136 gst_rtp_L16_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
137 {
138   GstRtpL16Pay *rtpL16pay;
139   gboolean res;
140   gchar *params;
141   GstAudioInfo *info;
142   const GstRTPChannelOrder *order;
143   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
144
145   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
146   rtpL16pay = GST_RTP_L16_PAY (basepayload);
147
148   info = &rtpL16pay->info;
149   gst_audio_info_init (info);
150   if (!gst_audio_info_from_caps (info, caps))
151     goto invalid_caps;
152
153   order = gst_rtp_channels_get_by_pos (info->channels, info->position);
154   rtpL16pay->order = order;
155
156   gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "L16",
157       info->rate);
158   params = g_strdup_printf ("%d", info->channels);
159
160   if (!order && info->channels > 2) {
161     GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
162         (NULL), ("Unknown channel order for %d channels", info->channels));
163   }
164
165   if (order && order->name) {
166     res = gst_rtp_base_payload_set_outcaps (basepayload,
167         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
168         info->channels, "channel-order", G_TYPE_STRING, order->name, NULL);
169   } else {
170     res = gst_rtp_base_payload_set_outcaps (basepayload,
171         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
172         info->channels, NULL);
173   }
174
175   g_free (params);
176
177   /* octet-per-sample is 2 * channels for L16 */
178   gst_rtp_base_audio_payload_set_sample_options (rtpbaseaudiopayload,
179       2 * info->channels);
180
181   return res;
182
183   /* ERRORS */
184 invalid_caps:
185   {
186     GST_DEBUG_OBJECT (rtpL16pay, "invalid caps");
187     return FALSE;
188   }
189 }
190
191 static GstCaps *
192 gst_rtp_L16_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
193     GstCaps * filter)
194 {
195   GstCaps *otherpadcaps;
196   GstCaps *caps;
197
198   caps = gst_pad_get_pad_template_caps (pad);
199
200   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
201   if (otherpadcaps) {
202     if (!gst_caps_is_empty (otherpadcaps)) {
203       GstStructure *structure;
204       gint channels;
205       gint pt;
206       gint rate;
207
208       structure = gst_caps_get_structure (otherpadcaps, 0);
209       caps = gst_caps_make_writable (caps);
210
211       if (gst_structure_get_int (structure, "channels", &channels)) {
212         gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
213       } else if (gst_structure_get_int (structure, "payload", &pt)) {
214         if (pt == GST_RTP_PAYLOAD_L16_STEREO)
215           gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
216         else if (pt == GST_RTP_PAYLOAD_L16_MONO)
217           gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
218       }
219
220       if (gst_structure_get_int (structure, "clock-rate", &rate)) {
221         gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
222       } else if (gst_structure_get_int (structure, "payload", &pt)) {
223         if (pt == GST_RTP_PAYLOAD_L16_STEREO || pt == GST_RTP_PAYLOAD_L16_MONO)
224           gst_caps_set_simple (caps, "rate", G_TYPE_INT, 44100, NULL);
225       }
226
227     }
228     gst_caps_unref (otherpadcaps);
229   }
230
231   if (filter) {
232     GstCaps *tcaps = caps;
233
234     caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
235     gst_caps_unref (tcaps);
236   }
237
238   return caps;
239 }
240
241 static GstFlowReturn
242 gst_rtp_L16_pay_handle_buffer (GstRTPBasePayload * basepayload,
243     GstBuffer * buffer)
244 {
245   GstRtpL16Pay *rtpL16pay;
246
247   rtpL16pay = GST_RTP_L16_PAY (basepayload);
248   buffer = gst_buffer_make_writable (buffer);
249
250   if (rtpL16pay->order &&
251       !gst_audio_buffer_reorder_channels (buffer, rtpL16pay->info.finfo->format,
252           rtpL16pay->info.channels, rtpL16pay->info.position,
253           rtpL16pay->order->pos)) {
254     return GST_FLOW_ERROR;
255   }
256
257   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (basepayload,
258       buffer);
259 }