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-rtpL24pay
23 * @see_also: rtpL24depay
25 * Payload raw 24-bit audio into RTP packets according to RFC 3190, section 4.
26 * For detailed information see: http://www.rfc-editor.org/rfc/rfc3190.txt
30 * gst-launch-1.0 -v audiotestsrc ! audioconvert ! rtpL24pay ! udpsink
31 * ]| This example pipeline will payload raw audio. Refer to
32 * the rtpL24depay example to depayload and play the RTP stream.
42 #include <gst/audio/audio.h>
43 #include <gst/rtp/gstrtpbuffer.h>
45 #include "gstrtpelements.h"
46 #include "gstrtpL24pay.h"
47 #include "gstrtpchannels.h"
49 GST_DEBUG_CATEGORY_STATIC (rtpL24pay_debug);
50 #define GST_CAT_DEFAULT (rtpL24pay_debug)
52 static GstStaticPadTemplate gst_rtp_L24_pay_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS ("audio/x-raw, "
57 "format = (string) S24BE, "
58 "layout = (string) interleaved, "
59 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
62 static GstStaticPadTemplate gst_rtp_L24_pay_src_template =
63 GST_STATIC_PAD_TEMPLATE ("src",
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) \"L24\", " "channels = (int) [ 1, MAX ];")
73 static gboolean gst_rtp_L24_pay_setcaps (GstRTPBasePayload * basepayload,
75 static GstCaps *gst_rtp_L24_pay_getcaps (GstRTPBasePayload * rtppayload,
76 GstPad * pad, GstCaps * filter);
78 gst_rtp_L24_pay_handle_buffer (GstRTPBasePayload * basepayload,
81 #define gst_rtp_L24_pay_parent_class parent_class
82 G_DEFINE_TYPE (GstRtpL24Pay, gst_rtp_L24_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
83 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpL24pay, "rtpL24pay",
84 GST_RANK_SECONDARY, GST_TYPE_RTP_L24_PAY, rtp_element_init (plugin));
87 gst_rtp_L24_pay_class_init (GstRtpL24PayClass * klass)
89 GstElementClass *gstelement_class;
90 GstRTPBasePayloadClass *gstrtpbasepayload_class;
92 gstelement_class = (GstElementClass *) klass;
93 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
95 gstrtpbasepayload_class->set_caps = gst_rtp_L24_pay_setcaps;
96 gstrtpbasepayload_class->get_caps = gst_rtp_L24_pay_getcaps;
97 gstrtpbasepayload_class->handle_buffer = gst_rtp_L24_pay_handle_buffer;
99 gst_element_class_add_static_pad_template (gstelement_class,
100 &gst_rtp_L24_pay_src_template);
101 gst_element_class_add_static_pad_template (gstelement_class,
102 &gst_rtp_L24_pay_sink_template);
104 gst_element_class_set_static_metadata (gstelement_class,
105 "RTP audio payloader", "Codec/Payloader/Network/RTP",
106 "Payload-encode Raw 24-bit audio into RTP packets (RFC 3190)",
107 "Wim Taymans <wim.taymans@gmail.com>,"
108 "David Holroyd <dave@badgers-in-foil.co.uk>");
110 GST_DEBUG_CATEGORY_INIT (rtpL24pay_debug, "rtpL24pay", 0,
111 "L24 RTP Payloader");
115 gst_rtp_L24_pay_init (GstRtpL24Pay * rtpL24pay)
117 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
119 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpL24pay);
121 /* tell rtpbaseaudiopayload that this is a sample based codec */
122 gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
126 gst_rtp_L24_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
128 GstRtpL24Pay *rtpL24pay;
132 const GstRTPChannelOrder *order;
133 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
135 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
136 rtpL24pay = GST_RTP_L24_PAY (basepayload);
138 info = &rtpL24pay->info;
139 gst_audio_info_init (info);
140 if (!gst_audio_info_from_caps (info, caps))
143 order = gst_rtp_channels_get_by_pos (info->channels, info->position);
144 rtpL24pay->order = order;
146 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "L24",
148 params = g_strdup_printf ("%d", info->channels);
150 if (!order && info->channels > 2) {
151 GST_ELEMENT_WARNING (rtpL24pay, STREAM, DECODE,
152 (NULL), ("Unknown channel order for %d channels", info->channels));
155 if (order && order->name) {
156 res = gst_rtp_base_payload_set_outcaps (basepayload,
157 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
158 info->channels, "channel-order", G_TYPE_STRING, order->name, NULL);
160 res = gst_rtp_base_payload_set_outcaps (basepayload,
161 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
162 info->channels, NULL);
167 /* octet-per-sample is 3 * channels for L24 */
168 gst_rtp_base_audio_payload_set_sample_options (rtpbaseaudiopayload,
176 GST_DEBUG_OBJECT (rtpL24pay, "invalid caps");
182 gst_rtp_L24_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
185 GstCaps *otherpadcaps;
188 caps = gst_pad_get_pad_template_caps (pad);
190 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
192 if (!gst_caps_is_empty (otherpadcaps)) {
193 GstStructure *structure;
197 structure = gst_caps_get_structure (otherpadcaps, 0);
198 caps = gst_caps_make_writable (caps);
200 if (gst_structure_get_int (structure, "channels", &channels)) {
201 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
204 if (gst_structure_get_int (structure, "clock-rate", &rate)) {
205 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
209 gst_caps_unref (otherpadcaps);
213 GstCaps *tcaps = caps;
215 caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
216 gst_caps_unref (tcaps);
223 gst_rtp_L24_pay_handle_buffer (GstRTPBasePayload * basepayload,
226 GstRtpL24Pay *rtpL24pay;
228 rtpL24pay = GST_RTP_L24_PAY (basepayload);
229 buffer = gst_buffer_make_writable (buffer);
231 if (rtpL24pay->order &&
232 !gst_audio_buffer_reorder_channels (buffer, rtpL24pay->info.finfo->format,
233 rtpL24pay->info.channels, rtpL24pay->info.position,
234 rtpL24pay->order->pos)) {
235 return GST_FLOW_ERROR;
238 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (basepayload,