2 * Copyright (C) <2007> Wim Taymans <wim@fluendo.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 packet payloader",
39 "Codec/Payloader/Network",
40 "Payload-encode Raw audio into RTP packets (RFC 3551)",
41 "Wim Taymans <wim@fluendo.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 GstElementClass *gstelement_class;
134 GstBaseRTPPayloadClass *gstbasertppayload_class;
136 gobject_class = (GObjectClass *) klass;
137 gstelement_class = (GstElementClass *) klass;
138 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
140 parent_class = g_type_class_peek_parent (klass);
142 gobject_class->finalize = gst_rtp_L16_pay_finalize;
144 gstbasertppayload_class->set_caps = gst_rtp_L16_pay_setcaps;
145 gstbasertppayload_class->get_caps = gst_rtp_L16_pay_getcaps;
146 gstbasertppayload_class->handle_buffer = gst_rtp_L16_pay_handle_buffer;
148 GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
149 "L16 RTP Payloader");
153 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
155 rtpL16pay->adapter = gst_adapter_new ();
159 gst_rtp_L16_pay_finalize (GObject * object)
161 GstRtpL16Pay *rtpL16pay;
163 rtpL16pay = GST_RTP_L16_PAY (object);
165 g_object_unref (rtpL16pay->adapter);
166 rtpL16pay->adapter = NULL;
168 G_OBJECT_CLASS (parent_class)->finalize (object);
172 gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
174 GstRtpL16Pay *rtpL16pay;
175 GstStructure *structure;
179 GstAudioChannelPosition *pos;
180 const GstRTPChannelOrder *order;
182 rtpL16pay = GST_RTP_L16_PAY (basepayload);
184 structure = gst_caps_get_structure (caps, 0);
186 /* first parse input caps */
187 if (!gst_structure_get_int (structure, "rate", &rate))
190 if (!gst_structure_get_int (structure, "channels", &channels))
193 /* get the channel order */
194 pos = gst_audio_get_channel_positions (structure);
196 order = gst_rtp_channels_get_by_pos (channels, pos);
200 gst_basertppayload_set_options (basepayload, "audio", TRUE, "L16", rate);
201 params = g_strdup_printf ("%d", channels);
203 if (!order && channels > 2) {
204 GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
205 (NULL), ("Unknown channel order for %d channels", channels));
208 if (order && order->name) {
209 res = gst_basertppayload_set_outcaps (basepayload,
210 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
211 channels, "channel-order", G_TYPE_STRING, order->name, NULL);
213 res = gst_basertppayload_set_outcaps (basepayload,
214 "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
221 rtpL16pay->rate = rate;
222 rtpL16pay->channels = channels;
229 GST_DEBUG_OBJECT (rtpL16pay, "no rate given");
234 GST_DEBUG_OBJECT (rtpL16pay, "no channels given");
240 gst_rtp_L16_pay_flush (GstRtpL16Pay * rtpL16pay, guint len)
246 GstClockTime duration;
248 /* calculate the amount of samples and round down the length */
249 samples = len / (2 * rtpL16pay->channels);
250 len = samples * (2 * rtpL16pay->channels);
252 /* now alloc output buffer */
253 outbuf = gst_rtp_buffer_new_allocate (len, 0, 0);
255 /* get payload, this is now writable */
256 payload = gst_rtp_buffer_get_payload (outbuf);
258 /* copy and flush data out of adapter into the RTP payload */
259 gst_adapter_copy (rtpL16pay->adapter, payload, 0, len);
260 gst_adapter_flush (rtpL16pay->adapter, len);
262 duration = gst_util_uint64_scale_int (samples, GST_SECOND, rtpL16pay->rate);
264 GST_BUFFER_TIMESTAMP (outbuf) = rtpL16pay->first_ts;
265 GST_BUFFER_DURATION (outbuf) = duration;
267 /* increase count (in ts) of data pushed to basertppayload */
268 if (GST_CLOCK_TIME_IS_VALID (rtpL16pay->first_ts))
269 rtpL16pay->first_ts += duration;
271 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpL16pay), outbuf);
277 gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * basepayload,
280 GstRtpL16Pay *rtpL16pay;
281 GstFlowReturn ret = GST_FLOW_OK;
283 GstClockTime timestamp;
286 rtpL16pay = GST_RTP_L16_PAY (basepayload);
287 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpL16pay);
289 timestamp = GST_BUFFER_TIMESTAMP (buffer);
291 if (GST_BUFFER_IS_DISCONT (buffer))
292 gst_adapter_clear (rtpL16pay->adapter);
294 avail = gst_adapter_available (rtpL16pay->adapter);
296 rtpL16pay->first_ts = timestamp;
299 /* push buffer in adapter */
300 gst_adapter_push (rtpL16pay->adapter, buffer);
302 /* get payload len for MTU */
303 payload_len = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
305 /* flush complete MTU while we have enough data in the adapter */
306 while (avail >= payload_len) {
307 /* flush payload_len bytes */
308 ret = gst_rtp_L16_pay_flush (rtpL16pay, payload_len);
309 if (ret != GST_FLOW_OK)
312 avail = gst_adapter_available (rtpL16pay->adapter);
318 gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
320 GstCaps *otherpadcaps;
323 otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
324 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
327 if (!gst_caps_is_empty (otherpadcaps)) {
328 GstStructure *structure;
333 structure = gst_caps_get_structure (otherpadcaps, 0);
335 if (gst_structure_get_int (structure, "channels", &channels)) {
336 gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
337 } else if (gst_structure_get_int (structure, "payload", &pt)) {
339 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
341 gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
344 if (gst_structure_get_int (structure, "clock-rate", &rate)) {
345 gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
346 } else if (gst_structure_get_int (structure, "payload", &pt)) {
347 if (pt == 10 || pt == 11)
348 gst_caps_set_simple (caps, "rate", G_TYPE_INT, 44100, NULL);
352 gst_caps_unref (otherpadcaps);
358 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
360 return gst_element_register (plugin, "rtpL16pay",
361 GST_RANK_NONE, GST_TYPE_RTP_L16_PAY);