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 * @see_also: rtpL16depay
23 *
24 * Payload raw audio into RTP packets according to RFC 3551.
25 * For detailed information see: http://www.rfc-editor.org/rfc/rfc3551.txt
26 *
27 * <refsect2>
28 * <title>Example pipeline</title>
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 * </refsect2>
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 "gstrtpL16pay.h"
46 #include "gstrtpchannels.h"
47
48 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
49 #define GST_CAT_DEFAULT (rtpL16pay_debug)
50
51 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_PAD_SINK,
54 GST_PAD_ALWAYS,
55 GST_STATIC_CAPS ("audio/x-raw, "
56 "format = (string) S16BE, "
57 "layout = (string) interleaved, "
58 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
59 );
60
61 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
63 GST_PAD_SRC,
64 GST_PAD_ALWAYS,
65 GST_STATIC_CAPS ("application/x-rtp, "
66 "media = (string) \"audio\", "
67 "payload = (int) [ 96, 127 ], "
68 "clock-rate = (int) [ 1, MAX ], "
69 "encoding-name = (string) \"L16\", "
70 "channels = (int) [ 1, MAX ];"
71 "application/x-rtp, "
72 "media = (string) \"audio\", "
73 "encoding-name = (string) \"L16\", "
74 "payload = (int) " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
75 "clock-rate = (int) 44100;"
76 "application/x-rtp, "
77 "media = (string) \"audio\", "
78 "encoding-name = (string) \"L16\", "
79 "payload = (int) " GST_RTP_PAYLOAD_L16_MONO_STRING ", "
80 "clock-rate = (int) 44100")
81 );
82
83 static gboolean gst_rtp_L16_pay_setcaps (GstRTPBasePayload * basepayload,
84 GstCaps * caps);
85 static GstCaps *gst_rtp_L16_pay_getcaps (GstRTPBasePayload * rtppayload,
86 GstPad * pad, GstCaps * filter);
87 static GstFlowReturn
88 gst_rtp_L16_pay_handle_buffer (GstRTPBasePayload * basepayload,
89 GstBuffer * buffer);
90
91 #define gst_rtp_L16_pay_parent_class parent_class
92 G_DEFINE_TYPE (GstRtpL16Pay, gst_rtp_L16_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
93
94 static void
gst_rtp_L16_pay_class_init(GstRtpL16PayClass * klass)95 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
96 {
97 GstElementClass *gstelement_class;
98 GstRTPBasePayloadClass *gstrtpbasepayload_class;
99
100 gstelement_class = (GstElementClass *) klass;
101 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
102
103 gstrtpbasepayload_class->set_caps = gst_rtp_L16_pay_setcaps;
104 gstrtpbasepayload_class->get_caps = gst_rtp_L16_pay_getcaps;
105 gstrtpbasepayload_class->handle_buffer = gst_rtp_L16_pay_handle_buffer;
106
107 gst_element_class_add_static_pad_template (gstelement_class,
108 &gst_rtp_L16_pay_src_template);
109 gst_element_class_add_static_pad_template (gstelement_class,
110 &gst_rtp_L16_pay_sink_template);
111
112 gst_element_class_set_static_metadata (gstelement_class,
113 "RTP audio payloader", "Codec/Payloader/Network/RTP",
114 "Payload-encode Raw audio into RTP packets (RFC 3551)",
115 "Wim Taymans <wim.taymans@gmail.com>");
116
117 GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
118 "L16 RTP Payloader");
119 }
120
121 static void
gst_rtp_L16_pay_init(GstRtpL16Pay * rtpL16pay)122 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
123 {
124 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
125
126 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpL16pay);
127
128 /* tell rtpbaseaudiopayload that this is a sample based codec */
129 gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
130 }
131
132 static gboolean
gst_rtp_L16_pay_setcaps(GstRTPBasePayload * basepayload,GstCaps * caps)133 gst_rtp_L16_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
134 {
135 GstRtpL16Pay *rtpL16pay;
136 gboolean res;
137 gchar *params;
138 GstAudioInfo *info;
139 const GstRTPChannelOrder *order;
140 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
141
142 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
143 rtpL16pay = GST_RTP_L16_PAY (basepayload);
144
145 info = &rtpL16pay->info;
146 gst_audio_info_init (info);
147 if (!gst_audio_info_from_caps (info, caps))
148 goto invalid_caps;
149
150 order = gst_rtp_channels_get_by_pos (info->channels, info->position);
151 rtpL16pay->order = order;
152
153 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "L16",
154 info->rate);
155 params = g_strdup_printf ("%d", info->channels);
156
157 if (!order && info->channels > 2) {
158 GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
159 (NULL), ("Unknown channel order for %d channels", info->channels));
160 }
161
162 if (order && order->name) {
163 res = gst_rtp_base_payload_set_outcaps (basepayload,
164 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
165 info->channels, "channel-order", G_TYPE_STRING, order->name, NULL);
166 } else {
167 res = gst_rtp_base_payload_set_outcaps (basepayload,
168 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
169 info->channels, NULL);
170 }
171
172 g_free (params);
173
174 /* octet-per-sample is 2 * channels for L16 */
175 gst_rtp_base_audio_payload_set_sample_options (rtpbaseaudiopayload,
176 2 * info->channels);
177
178 return res;
179
180 /* ERRORS */
181 invalid_caps:
182 {
183 GST_DEBUG_OBJECT (rtpL16pay, "invalid caps");
184 return FALSE;
185 }
186 }
187
188 static GstCaps *
gst_rtp_L16_pay_getcaps(GstRTPBasePayload * rtppayload,GstPad * pad,GstCaps * filter)189 gst_rtp_L16_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
190 GstCaps * filter)
191 {
192 GstCaps *otherpadcaps;
193 GstCaps *caps;
194
195 caps = gst_pad_get_pad_template_caps (pad);
196
197 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
198 if (otherpadcaps) {
199 if (!gst_caps_is_empty (otherpadcaps)) {
200 GstStructure *structure;
201 gint channels;
202 gint pt;
203 gint rate;
204
205 structure = gst_caps_get_structure (otherpadcaps, 0);
206 caps = gst_caps_make_writable (caps);
207
208 if (gst_structure_get_int (structure, "channels", &channels)) {
209 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
210 } else if (gst_structure_get_int (structure, "payload", &pt)) {
211 if (pt == GST_RTP_PAYLOAD_L16_STEREO)
212 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
213 else if (pt == GST_RTP_PAYLOAD_L16_MONO)
214 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
215 }
216
217 if (gst_structure_get_int (structure, "clock-rate", &rate)) {
218 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
219 } else if (gst_structure_get_int (structure, "payload", &pt)) {
220 if (pt == GST_RTP_PAYLOAD_L16_STEREO || pt == GST_RTP_PAYLOAD_L16_MONO)
221 gst_caps_set_simple (caps, "rate", G_TYPE_INT, 44100, NULL);
222 }
223
224 }
225 gst_caps_unref (otherpadcaps);
226 }
227
228 if (filter) {
229 GstCaps *tcaps = caps;
230
231 caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
232 gst_caps_unref (tcaps);
233 }
234
235 return caps;
236 }
237
238 static GstFlowReturn
gst_rtp_L16_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)239 gst_rtp_L16_pay_handle_buffer (GstRTPBasePayload * basepayload,
240 GstBuffer * buffer)
241 {
242 GstRtpL16Pay *rtpL16pay;
243
244 rtpL16pay = GST_RTP_L16_PAY (basepayload);
245 buffer = gst_buffer_make_writable (buffer);
246
247 if (rtpL16pay->order &&
248 !gst_audio_buffer_reorder_channels (buffer, rtpL16pay->info.finfo->format,
249 rtpL16pay->info.channels, rtpL16pay->info.position,
250 rtpL16pay->order->pos)) {
251 return GST_FLOW_ERROR;
252 }
253
254 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (basepayload,
255 buffer);
256 }
257
258 gboolean
gst_rtp_L16_pay_plugin_init(GstPlugin * plugin)259 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
260 {
261 return gst_element_register (plugin, "rtpL16pay",
262 GST_RANK_SECONDARY, GST_TYPE_RTP_L16_PAY);
263 }
264