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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <gst/audio/audio.h>
27 #include <gst/audio/multichannel.h>
28 #include <gst/rtp/gstrtpbuffer.h>
30 #include "gstrtpL16pay.h"
31 #include "gstrtpchannels.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
34 #define GST_CAT_DEFAULT (rtpL16pay_debug)
36 /* elementfactory information */
37 static const GstElementDetails gst_rtp_L16_pay_details =
38 GST_ELEMENT_DETAILS ("RTP audio payloader",
39 "Codec/Payloader/Network",
40 "Payload-encode Raw audio into RTP packets (RFC 3551)",
41 "Wim Taymans <wim.taymans@gmail.com>");
43 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_STATIC_CAPS ("audio/x-raw-int, "
48 "endianness = (int) BIG_ENDIAN, "
49 "signed = (boolean) true, "
52 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
55 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
56 GST_STATIC_PAD_TEMPLATE ("src",
59 GST_STATIC_CAPS ("application/x-rtp, "
60 "media = (string) \"audio\", "
61 "payload = (int) [ 96, 127 ], "
62 "clock-rate = (int) [ 1, MAX ], "
63 "encoding-name = (string) \"L16\", "
64 "channels = (int) [ 1, MAX ];"
66 "media = (string) \"audio\", "
67 "encoding-name = (string) \"L16\", "
68 "payload = (int) " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
69 "clock-rate = (int) 44100;"
71 "media = (string) \"audio\", "
72 "encoding-name = (string) \"L16\", "
73 "payload = (int) " GST_RTP_PAYLOAD_L16_MONO_STRING ", "
74 "clock-rate = (int) 44100")
77 static void gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass);
78 static void gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass);
79 static void gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay);
80 static void gst_rtp_L16_pay_finalize (GObject * object);
82 static gboolean gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload,
84 static GstFlowReturn gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * pad,
86 static GstCaps *gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload,
89 static GstBaseRTPPayloadClass *parent_class = NULL;
92 gst_rtp_L16_pay_get_type (void)
94 static GType rtpL16pay_type = 0;
96 if (!rtpL16pay_type) {
97 static const GTypeInfo rtpL16pay_info = {
98 sizeof (GstRtpL16PayClass),
99 (GBaseInitFunc) gst_rtp_L16_pay_base_init,
101 (GClassInitFunc) gst_rtp_L16_pay_class_init,
104 sizeof (GstRtpL16Pay),
106 (GInstanceInitFunc) gst_rtp_L16_pay_init,
110 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpL16Pay",
113 return rtpL16pay_type;
117 gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass)
119 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
121 gst_element_class_add_pad_template (element_class,
122 gst_static_pad_template_get (&gst_rtp_L16_pay_src_template));
123 gst_element_class_add_pad_template (element_class,
124 gst_static_pad_template_get (&gst_rtp_L16_pay_sink_template));
126 gst_element_class_set_details (element_class, &gst_rtp_L16_pay_details);
130 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
132 GObjectClass *gobject_class;
133 GstBaseRTPPayloadClass *gstbasertppayload_class;
135 gobject_class = (GObjectClass *) klass;
136 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
138 parent_class = g_type_class_peek_parent (klass);
140 gobject_class->finalize = gst_rtp_L16_pay_finalize;
142 gstbasertppayload_class->set_caps = gst_rtp_L16_pay_setcaps;
143 gstbasertppayload_class->get_caps = gst_rtp_L16_pay_getcaps;
144 gstbasertppayload_class->handle_buffer = gst_rtp_L16_pay_handle_buffer;
146 GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
147 "L16 RTP Payloader");
151 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
153 rtpL16pay->adapter = gst_adapter_new ();
157 gst_rtp_L16_pay_finalize (GObject * object)
159 GstRtpL16Pay *rtpL16pay;
161 rtpL16pay = GST_RTP_L16_PAY (object);
163 g_object_unref (rtpL16pay->adapter);
164 rtpL16pay->adapter = NULL;
166 G_OBJECT_CLASS (parent_class)->finalize (object);
170 gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
172 GstRtpL16Pay *rtpL16pay;
173 GstStructure *structure;
177 GstAudioChannelPosition *pos;
178 const GstRTPChannelOrder *order;
180 rtpL16pay = GST_RTP_L16_PAY (basepayload);
182 structure = gst_caps_get_structure (caps, 0);
184 /* first parse input caps */
185 if (!gst_structure_get_int (structure, "rate", &rate))
188 if (!gst_structure_get_int (structure, "channels", &channels))
191 /* get the channel order */
192 pos = gst_audio_get_channel_positions (structure);
194 order = gst_rtp_channels_get_by_pos (channels, pos);
198 gst_basertppayload_set_options (basepayload, "audio", TRUE, "L16", rate);
199 params = g_strdup_printf ("%d", channels);
201 if (!order && channels > 2) {
202 GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
203 (NULL), ("Unknown channel order for %d channels", channels));
206 if (order && order->name) {
207 res = gst_basertppayload_set_outcaps (basepayload,
208 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
209 channels, "channel-order", G_TYPE_STRING, order->name, NULL);
211 res = gst_basertppayload_set_outcaps (basepayload,
212 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
219 rtpL16pay->rate = rate;
220 rtpL16pay->channels = channels;
227 GST_DEBUG_OBJECT (rtpL16pay, "no rate given");
232 GST_DEBUG_OBJECT (rtpL16pay, "no channels given");
238 gst_rtp_L16_pay_flush (GstRtpL16Pay * rtpL16pay, guint len)
244 GstClockTime duration;
246 /* calculate the amount of samples and round down the length */
247 samples = len / (2 * rtpL16pay->channels);
248 len = samples * (2 * rtpL16pay->channels);
250 /* now alloc output buffer */
251 outbuf = gst_rtp_buffer_new_allocate (len, 0, 0);
253 /* get payload, this is now writable */
254 payload = gst_rtp_buffer_get_payload (outbuf);
256 /* copy and flush data out of adapter into the RTP payload */
257 gst_adapter_copy (rtpL16pay->adapter, payload, 0, len);
258 gst_adapter_flush (rtpL16pay->adapter, len);
260 duration = gst_util_uint64_scale_int (samples, GST_SECOND, rtpL16pay->rate);
262 GST_BUFFER_TIMESTAMP (outbuf) = rtpL16pay->first_ts;
263 GST_BUFFER_DURATION (outbuf) = duration;
265 /* increase count (in ts) of data pushed to basertppayload */
266 if (GST_CLOCK_TIME_IS_VALID (rtpL16pay->first_ts))
267 rtpL16pay->first_ts += duration;
269 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpL16pay), outbuf);
275 gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * basepayload,
278 GstRtpL16Pay *rtpL16pay;
279 GstFlowReturn ret = GST_FLOW_OK;
281 GstClockTime timestamp;
284 rtpL16pay = GST_RTP_L16_PAY (basepayload);
285 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpL16pay);
287 timestamp = GST_BUFFER_TIMESTAMP (buffer);
289 if (GST_BUFFER_IS_DISCONT (buffer))
290 gst_adapter_clear (rtpL16pay->adapter);
292 avail = gst_adapter_available (rtpL16pay->adapter);
294 rtpL16pay->first_ts = timestamp;
297 /* push buffer in adapter */
298 gst_adapter_push (rtpL16pay->adapter, buffer);
300 /* get payload len for MTU */
301 payload_len = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
303 /* flush complete MTU while we have enough data in the adapter */
304 while (avail >= payload_len) {
305 /* flush payload_len bytes */
306 ret = gst_rtp_L16_pay_flush (rtpL16pay, payload_len);
307 if (ret != GST_FLOW_OK)
310 avail = gst_adapter_available (rtpL16pay->adapter);
316 gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
318 GstCaps *otherpadcaps;
321 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
322 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
325 if (!gst_caps_is_empty (otherpadcaps)) {
326 GstStructure *structure;
331 structure = gst_caps_get_structure (otherpadcaps, 0);
333 if (gst_structure_get_int (structure, "channels", &channels)) {
334 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
335 } else if (gst_structure_get_int (structure, "payload", &pt)) {
337 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
339 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
342 if (gst_structure_get_int (structure, "clock-rate", &rate)) {
343 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
344 } else if (gst_structure_get_int (structure, "payload", &pt)) {
345 if (pt == 10 || pt == 11)
346 gst_caps_set_simple (caps, "rate", G_TYPE_INT, 44100, NULL);
350 gst_caps_unref (otherpadcaps);
356 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
358 return gst_element_register (plugin, "rtpL16pay",
359 GST_RANK_NONE, GST_TYPE_RTP_L16_PAY);