2 * Copyright (C) <2009> 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/rtp/gstrtpbuffer.h>
28 #include "gstrtpceltpay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpceltpay_debug);
31 #define GST_CAT_DEFAULT (rtpceltpay_debug)
33 static GstStaticPadTemplate gst_rtp_celt_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_STATIC_CAPS ("audio/x-celt, "
38 "rate = (int) [ 32000, 64000 ], "
39 "channels = (int) [1, 2], " "frame-size = (int) [ 64, 512 ]")
42 static GstStaticPadTemplate gst_rtp_celt_pay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
46 GST_STATIC_CAPS ("application/x-rtp, "
47 "media = (string) \"audio\", "
48 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
49 "clock-rate = (int) [ 32000, 48000 ], "
50 "encoding-name = (string) \"CELT\"")
53 static void gst_rtp_celt_pay_finalize (GObject * object);
55 static GstStateChangeReturn gst_rtp_celt_pay_change_state (GstElement *
56 element, GstStateChange transition);
58 static gboolean gst_rtp_celt_pay_setcaps (GstRTPBasePayload * payload,
60 static GstCaps *gst_rtp_celt_pay_getcaps (GstRTPBasePayload * payload,
61 GstPad * pad, GstCaps * filter);
62 static GstFlowReturn gst_rtp_celt_pay_handle_buffer (GstRTPBasePayload *
63 payload, GstBuffer * buffer);
65 #define gst_rtp_celt_pay_parent_class parent_class
66 G_DEFINE_TYPE (GstRtpCELTPay, gst_rtp_celt_pay, GST_TYPE_RTP_BASE_PAYLOAD);
69 gst_rtp_celt_pay_class_init (GstRtpCELTPayClass * klass)
71 GObjectClass *gobject_class;
72 GstElementClass *gstelement_class;
73 GstRTPBasePayloadClass *gstrtpbasepayload_class;
75 GST_DEBUG_CATEGORY_INIT (rtpceltpay_debug, "rtpceltpay", 0,
76 "CELT RTP Payloader");
78 gobject_class = (GObjectClass *) klass;
79 gstelement_class = (GstElementClass *) klass;
80 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
82 gobject_class->finalize = gst_rtp_celt_pay_finalize;
84 gstelement_class->change_state = gst_rtp_celt_pay_change_state;
86 gst_element_class_add_pad_template (gstelement_class,
87 gst_static_pad_template_get (&gst_rtp_celt_pay_sink_template));
88 gst_element_class_add_pad_template (gstelement_class,
89 gst_static_pad_template_get (&gst_rtp_celt_pay_src_template));
91 gst_element_class_set_details_simple (gstelement_class, "RTP CELT payloader",
92 "Codec/Payloader/Network/RTP",
93 "Payload-encodes CELT audio into a RTP packet",
94 "Wim Taymans <wim.taymans@gmail.com>");
96 gstrtpbasepayload_class->set_caps = gst_rtp_celt_pay_setcaps;
97 gstrtpbasepayload_class->get_caps = gst_rtp_celt_pay_getcaps;
98 gstrtpbasepayload_class->handle_buffer = gst_rtp_celt_pay_handle_buffer;
102 gst_rtp_celt_pay_init (GstRtpCELTPay * rtpceltpay)
104 rtpceltpay->queue = g_queue_new ();
108 gst_rtp_celt_pay_finalize (GObject * object)
110 GstRtpCELTPay *rtpceltpay;
112 rtpceltpay = GST_RTP_CELT_PAY (object);
114 g_queue_free (rtpceltpay->queue);
116 G_OBJECT_CLASS (parent_class)->finalize (object);
120 gst_rtp_celt_pay_clear_queued (GstRtpCELTPay * rtpceltpay)
124 while ((buf = g_queue_pop_head (rtpceltpay->queue)))
125 gst_buffer_unref (buf);
127 rtpceltpay->bytes = 0;
128 rtpceltpay->sbytes = 0;
129 rtpceltpay->qduration = 0;
133 gst_rtp_celt_pay_add_queued (GstRtpCELTPay * rtpceltpay, GstBuffer * buffer,
134 guint ssize, guint size, GstClockTime duration)
136 g_queue_push_tail (rtpceltpay->queue, buffer);
137 rtpceltpay->sbytes += ssize;
138 rtpceltpay->bytes += size;
139 /* only add durations when we have a valid previous duration */
140 if (rtpceltpay->qduration != -1) {
142 /* only add valid durations */
143 rtpceltpay->qduration += duration;
145 /* if we add a buffer without valid duration, our total queued duration
147 rtpceltpay->qduration = -1;
152 gst_rtp_celt_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
154 /* don't configure yet, we wait for the ident packet */
160 gst_rtp_celt_pay_getcaps (GstRTPBasePayload * payload, GstPad * pad,
163 GstCaps *otherpadcaps;
167 otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
168 caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
171 if (!gst_caps_is_empty (otherpadcaps)) {
172 GstStructure *ps = gst_caps_get_structure (otherpadcaps, 0);
173 GstStructure *s = gst_caps_get_structure (caps, 0);
174 gint clock_rate = 0, frame_size = 0, channels = 1;
176 if (gst_structure_get_int (ps, "clock-rate", &clock_rate)) {
177 gst_structure_fixate_field_nearest_int (s, "rate", clock_rate);
180 if ((params = gst_structure_get_string (ps, "frame-size")))
181 frame_size = atoi (params);
183 gst_structure_set (s, "frame-size", G_TYPE_INT, frame_size, NULL);
185 if ((params = gst_structure_get_string (ps, "encoding-params"))) {
186 channels = atoi (params);
187 gst_structure_fixate_field_nearest_int (s, "channels", channels);
190 GST_DEBUG_OBJECT (payload, "clock-rate=%d frame-size=%d channels=%d",
191 clock_rate, frame_size, channels);
193 gst_caps_unref (otherpadcaps);
200 gst_rtp_celt_pay_parse_ident (GstRtpCELTPay * rtpceltpay,
201 const guint8 * data, guint size)
203 guint32 version, header_size, rate, nb_channels, frame_size, overlap;
204 guint32 bytes_per_packet;
205 GstRTPBasePayload *payload;
209 /* we need the header string (8), the version string (20), the version
210 * and the header length. */
214 if (!g_str_has_prefix ((const gchar *) data, "CELT "))
217 /* skip header and version string */
220 version = GST_READ_UINT32_LE (data);
221 GST_DEBUG_OBJECT (rtpceltpay, "version %08x", version);
229 header_size = GST_READ_UINT32_LE (data);
230 if (header_size < 56)
231 goto header_too_small;
233 if (size < header_size)
234 goto payload_too_small;
237 rate = GST_READ_UINT32_LE (data);
239 nb_channels = GST_READ_UINT32_LE (data);
241 frame_size = GST_READ_UINT32_LE (data);
243 overlap = GST_READ_UINT32_LE (data);
245 bytes_per_packet = GST_READ_UINT32_LE (data);
247 GST_DEBUG_OBJECT (rtpceltpay, "rate %d, nb_channels %d, frame_size %d",
248 rate, nb_channels, frame_size);
249 GST_DEBUG_OBJECT (rtpceltpay, "overlap %d, bytes_per_packet %d",
250 overlap, bytes_per_packet);
252 payload = GST_RTP_BASE_PAYLOAD (rtpceltpay);
254 gst_rtp_base_payload_set_options (payload, "audio", FALSE, "CELT", rate);
255 cstr = g_strdup_printf ("%d", nb_channels);
256 fsstr = g_strdup_printf ("%d", frame_size);
257 res = gst_rtp_base_payload_set_outcaps (payload, "encoding-params",
258 G_TYPE_STRING, cstr, "frame-size", G_TYPE_STRING, fsstr, NULL);
267 GST_DEBUG_OBJECT (rtpceltpay,
268 "ident packet too small, need at least 32 bytes");
273 GST_DEBUG_OBJECT (rtpceltpay,
274 "ident packet does not start with \"CELT \"");
280 GST_DEBUG_OBJECT (rtpceltpay, "can only handle version 1, have version %d",
287 GST_DEBUG_OBJECT (rtpceltpay,
288 "header size too small, need at least 80 bytes, " "got only %d",
294 GST_DEBUG_OBJECT (rtpceltpay,
295 "payload too small, need at least %d bytes, got only %d", header_size,
302 gst_rtp_celt_pay_flush_queued (GstRtpCELTPay * rtpceltpay)
305 GstBuffer *buf, *outbuf;
306 guint8 *payload, *spayload;
308 GstClockTime duration;
309 GstRTPBuffer rtp = { NULL, };
311 payload_len = rtpceltpay->bytes + rtpceltpay->sbytes;
312 duration = rtpceltpay->qduration;
314 GST_DEBUG_OBJECT (rtpceltpay, "flushing out %u, duration %" GST_TIME_FORMAT,
315 payload_len, GST_TIME_ARGS (rtpceltpay->qduration));
317 /* get a big enough packet for the sizes + payloads */
318 outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
320 GST_BUFFER_DURATION (outbuf) = duration;
322 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
324 /* point to the payload for size headers and data */
325 spayload = gst_rtp_buffer_get_payload (&rtp);
326 payload = spayload + rtpceltpay->sbytes;
328 while ((buf = g_queue_pop_head (rtpceltpay->queue))) {
331 /* copy first timestamp to output */
332 if (GST_BUFFER_TIMESTAMP (outbuf) == -1)
333 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
335 /* write the size to the header */
336 size = gst_buffer_get_size (buf);
337 while (size > 0xff) {
344 size = gst_buffer_get_size (buf);
345 gst_buffer_extract (buf, 0, payload, size);
348 gst_buffer_unref (buf);
350 gst_rtp_buffer_unmap (&rtp);
352 /* we consumed it all */
353 rtpceltpay->bytes = 0;
354 rtpceltpay->sbytes = 0;
355 rtpceltpay->qduration = 0;
357 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpceltpay), outbuf);
363 gst_rtp_celt_pay_handle_buffer (GstRTPBasePayload * basepayload,
367 GstRtpCELTPay *rtpceltpay;
370 GstClockTime duration, packet_dur;
371 guint i, ssize, packet_len;
373 rtpceltpay = GST_RTP_CELT_PAY (basepayload);
377 gst_buffer_map (buffer, &map, GST_MAP_READ);
379 switch (rtpceltpay->packet) {
381 /* ident packet. We need to parse the headers to construct the RTP
383 if (!gst_rtp_celt_pay_parse_ident (rtpceltpay, map.data, map.size))
388 /* comment packet, we ignore it */
391 /* other packets go in the payload */
394 gst_buffer_unmap (buffer, &map);
396 duration = GST_BUFFER_DURATION (buffer);
398 GST_LOG_OBJECT (rtpceltpay,
399 "got buffer of duration %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
400 GST_TIME_ARGS (duration), map.size);
402 /* calculate the size of the size field and the payload */
404 for (i = map.size; i > 0xff; i -= 0xff)
407 GST_DEBUG_OBJECT (rtpceltpay, "bytes for size %u", ssize);
409 /* calculate what the new size and duration would be of the packet */
410 payload_len = ssize + map.size + rtpceltpay->bytes + rtpceltpay->sbytes;
411 if (rtpceltpay->qduration != -1 && duration != -1)
412 packet_dur = rtpceltpay->qduration + duration;
416 packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
418 if (gst_rtp_base_payload_is_filled (basepayload, packet_len, packet_dur)) {
419 /* size or duration would overflow the packet, flush the queued data */
420 ret = gst_rtp_celt_pay_flush_queued (rtpceltpay);
423 /* queue the packet */
424 gst_rtp_celt_pay_add_queued (rtpceltpay, buffer, ssize, map.size, duration);
427 rtpceltpay->packet++;
434 gst_buffer_unmap (buffer, &map);
439 GST_ELEMENT_ERROR (rtpceltpay, STREAM, DECODE, (NULL),
440 ("Error parsing first identification packet."));
441 gst_buffer_unmap (buffer, &map);
442 return GST_FLOW_ERROR;
446 static GstStateChangeReturn
447 gst_rtp_celt_pay_change_state (GstElement * element, GstStateChange transition)
449 GstRtpCELTPay *rtpceltpay;
450 GstStateChangeReturn ret;
452 rtpceltpay = GST_RTP_CELT_PAY (element);
454 switch (transition) {
455 case GST_STATE_CHANGE_NULL_TO_READY:
457 case GST_STATE_CHANGE_READY_TO_PAUSED:
458 rtpceltpay->packet = 0;
464 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
466 switch (transition) {
467 case GST_STATE_CHANGE_PAUSED_TO_READY:
468 gst_rtp_celt_pay_clear_queued (rtpceltpay);
470 case GST_STATE_CHANGE_READY_TO_NULL:
479 gst_rtp_celt_pay_plugin_init (GstPlugin * plugin)
481 return gst_element_register (plugin, "rtpceltpay",
482 GST_RANK_SECONDARY, GST_TYPE_RTP_CELT_PAY);