2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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.
21 * SECTION:element-rtpL16pay
22 * @see_also: rtpL16depay
24 * Payload raw audio into RTP packets according to RFC 3551.
25 * For detailed information see: http://www.rfc-editor.org/rfc/rfc3551.txt
28 * <title>Example pipeline</title>
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.
42 #include <gst/audio/audio.h>
43 #include <gst/rtp/gstrtpbuffer.h>
45 #include "gstrtpL16pay.h"
46 #include "gstrtpchannels.h"
48 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
49 #define GST_CAT_DEFAULT (rtpL16pay_debug)
51 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS ("audio/x-raw, "
56 "format = (string) S16BE, "
57 "layout = (string) interleaved, "
58 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
61 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
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 ];"
72 "media = (string) \"audio\", "
73 "encoding-name = (string) \"L16\", "
74 "payload = (int) " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
75 "clock-rate = (int) 44100;"
77 "media = (string) \"audio\", "
78 "encoding-name = (string) \"L16\", "
79 "payload = (int) " GST_RTP_PAYLOAD_L16_MONO_STRING ", "
80 "clock-rate = (int) 44100")
83 static gboolean gst_rtp_L16_pay_setcaps (GstRTPBasePayload * basepayload,
85 static GstCaps *gst_rtp_L16_pay_getcaps (GstRTPBasePayload * rtppayload,
86 GstPad * pad, GstCaps * filter);
88 gst_rtp_L16_pay_handle_buffer (GstRTPBasePayload * basepayload,
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);
95 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
97 GstElementClass *gstelement_class;
98 GstRTPBasePayloadClass *gstrtpbasepayload_class;
100 gstelement_class = (GstElementClass *) klass;
101 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
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;
107 gst_element_class_add_pad_template (gstelement_class,
108 gst_static_pad_template_get (&gst_rtp_L16_pay_src_template));
109 gst_element_class_add_pad_template (gstelement_class,
110 gst_static_pad_template_get (&gst_rtp_L16_pay_sink_template));
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>");
117 GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
118 "L16 RTP Payloader");
122 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
124 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
126 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpL16pay);
128 /* tell rtpbaseaudiopayload that this is a sample based codec */
129 gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
133 gst_rtp_L16_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
135 GstRtpL16Pay *rtpL16pay;
139 const GstRTPChannelOrder *order;
140 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
142 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
143 rtpL16pay = GST_RTP_L16_PAY (basepayload);
145 info = &rtpL16pay->info;
146 gst_audio_info_init (info);
147 if (!gst_audio_info_from_caps (info, caps))
150 order = gst_rtp_channels_get_by_pos (info->channels, info->position);
151 rtpL16pay->order = order;
153 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "L16",
155 params = g_strdup_printf ("%d", info->channels);
157 if (!order && info->channels > 2) {
158 GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
159 (NULL), ("Unknown channel order for %d channels", info->channels));
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);
167 res = gst_rtp_base_payload_set_outcaps (basepayload,
168 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
169 info->channels, NULL);
174 /* octet-per-sample is 2 * channels for L16 */
175 gst_rtp_base_audio_payload_set_sample_options (rtpbaseaudiopayload,
183 GST_DEBUG_OBJECT (rtpL16pay, "invalid caps");
189 gst_rtp_L16_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
192 GstCaps *otherpadcaps;
195 caps = gst_pad_get_pad_template_caps (pad);
197 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
199 if (!gst_caps_is_empty (otherpadcaps)) {
200 GstStructure *structure;
205 structure = gst_caps_get_structure (otherpadcaps, 0);
206 caps = gst_caps_make_writable (caps);
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);
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);
225 gst_caps_unref (otherpadcaps);
229 GstCaps *tcaps = caps;
231 caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
232 gst_caps_unref (tcaps);
239 gst_rtp_L16_pay_handle_buffer (GstRTPBasePayload * basepayload,
242 GstRtpL16Pay *rtpL16pay;
244 rtpL16pay = GST_RTP_L16_PAY (basepayload);
245 buffer = gst_buffer_make_writable (buffer);
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;
254 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (basepayload,
259 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
261 return gst_element_register (plugin, "rtpL16pay",
262 GST_RANK_SECONDARY, GST_TYPE_RTP_L16_PAY);