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-rtpL24pay
22 * @title: rtpL24pay
23 * @see_also: rtpL24depay
24 *
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
27 *
28 * ## Example pipeline
29 * |[
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.
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 "gstrtpL24pay.h"
47 #include "gstrtpchannels.h"
48
49 GST_DEBUG_CATEGORY_STATIC (rtpL24pay_debug);
50 #define GST_CAT_DEFAULT (rtpL24pay_debug)
51
52 static GstStaticPadTemplate gst_rtp_L24_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) S24BE, "
58 "layout = (string) interleaved, "
59 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
60 );
61
62 static GstStaticPadTemplate gst_rtp_L24_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) \"L24\", " "channels = (int) [ 1, MAX ];")
71 );
72
73 static gboolean gst_rtp_L24_pay_setcaps (GstRTPBasePayload * basepayload,
74 GstCaps * caps);
75 static GstCaps *gst_rtp_L24_pay_getcaps (GstRTPBasePayload * rtppayload,
76 GstPad * pad, GstCaps * filter);
77 static GstFlowReturn
78 gst_rtp_L24_pay_handle_buffer (GstRTPBasePayload * basepayload,
79 GstBuffer * buffer);
80
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));
85
86 static void
gst_rtp_L24_pay_class_init(GstRtpL24PayClass * klass)87 gst_rtp_L24_pay_class_init (GstRtpL24PayClass * klass)
88 {
89 GstElementClass *gstelement_class;
90 GstRTPBasePayloadClass *gstrtpbasepayload_class;
91
92 gstelement_class = (GstElementClass *) klass;
93 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
94
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;
98
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);
103
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>");
109
110 GST_DEBUG_CATEGORY_INIT (rtpL24pay_debug, "rtpL24pay", 0,
111 "L24 RTP Payloader");
112 }
113
114 static void
gst_rtp_L24_pay_init(GstRtpL24Pay * rtpL24pay)115 gst_rtp_L24_pay_init (GstRtpL24Pay * rtpL24pay)
116 {
117 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
118
119 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpL24pay);
120
121 /* tell rtpbaseaudiopayload that this is a sample based codec */
122 gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
123 }
124
125 static gboolean
gst_rtp_L24_pay_setcaps(GstRTPBasePayload * basepayload,GstCaps * caps)126 gst_rtp_L24_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
127 {
128 GstRtpL24Pay *rtpL24pay;
129 gboolean res;
130 gchar *params;
131 GstAudioInfo *info;
132 const GstRTPChannelOrder *order;
133 GstRTPBaseAudioPayload *rtpbaseaudiopayload;
134
135 rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
136 rtpL24pay = GST_RTP_L24_PAY (basepayload);
137
138 info = &rtpL24pay->info;
139 gst_audio_info_init (info);
140 if (!gst_audio_info_from_caps (info, caps))
141 goto invalid_caps;
142
143 order = gst_rtp_channels_get_by_pos (info->channels, info->position);
144 rtpL24pay->order = order;
145
146 gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "L24",
147 info->rate);
148 params = g_strdup_printf ("%d", info->channels);
149
150 if (!order && info->channels > 2) {
151 GST_ELEMENT_WARNING (rtpL24pay, STREAM, DECODE,
152 (NULL), ("Unknown channel order for %d channels", info->channels));
153 }
154
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);
159 } else {
160 res = gst_rtp_base_payload_set_outcaps (basepayload,
161 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
162 info->channels, NULL);
163 }
164
165 g_free (params);
166
167 /* octet-per-sample is 3 * channels for L24 */
168 gst_rtp_base_audio_payload_set_sample_options (rtpbaseaudiopayload,
169 3 * info->channels);
170
171 return res;
172
173 /* ERRORS */
174 invalid_caps:
175 {
176 GST_DEBUG_OBJECT (rtpL24pay, "invalid caps");
177 return FALSE;
178 }
179 }
180
181 static GstCaps *
gst_rtp_L24_pay_getcaps(GstRTPBasePayload * rtppayload,GstPad * pad,GstCaps * filter)182 gst_rtp_L24_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
183 GstCaps * filter)
184 {
185 GstCaps *otherpadcaps;
186 GstCaps *caps;
187
188 caps = gst_pad_get_pad_template_caps (pad);
189
190 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
191 if (otherpadcaps) {
192 if (!gst_caps_is_empty (otherpadcaps)) {
193 GstStructure *structure;
194 gint channels;
195 gint rate;
196
197 structure = gst_caps_get_structure (otherpadcaps, 0);
198 caps = gst_caps_make_writable (caps);
199
200 if (gst_structure_get_int (structure, "channels", &channels)) {
201 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
202 }
203
204 if (gst_structure_get_int (structure, "clock-rate", &rate)) {
205 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
206 }
207
208 }
209 gst_caps_unref (otherpadcaps);
210 }
211
212 if (filter) {
213 GstCaps *tcaps = caps;
214
215 caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
216 gst_caps_unref (tcaps);
217 }
218
219 return caps;
220 }
221
222 static GstFlowReturn
gst_rtp_L24_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)223 gst_rtp_L24_pay_handle_buffer (GstRTPBasePayload * basepayload,
224 GstBuffer * buffer)
225 {
226 GstRtpL24Pay *rtpL24pay;
227
228 rtpL24pay = GST_RTP_L24_PAY (basepayload);
229 buffer = gst_buffer_make_writable (buffer);
230
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;
236 }
237
238 return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (basepayload,
239 buffer);
240 }
241