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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/audio/audio.h>
29 #include "gstrtpceltpay.h"
30 #include "gstrtputils.h"
32 GST_DEBUG_CATEGORY_STATIC (rtpceltpay_debug);
33 #define GST_CAT_DEFAULT (rtpceltpay_debug)
35 static GstStaticPadTemplate gst_rtp_celt_pay_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("audio/x-celt, "
40 "rate = (int) [ 32000, 64000 ], "
41 "channels = (int) [1, 2], " "frame-size = (int) [ 64, 512 ]")
44 static GstStaticPadTemplate gst_rtp_celt_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "media = (string) \"audio\", "
50 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51 "clock-rate = (int) [ 32000, 48000 ], "
52 "encoding-name = (string) \"CELT\"")
55 static void gst_rtp_celt_pay_finalize (GObject * object);
57 static GstStateChangeReturn gst_rtp_celt_pay_change_state (GstElement *
58 element, GstStateChange transition);
60 static gboolean gst_rtp_celt_pay_setcaps (GstRTPBasePayload * payload,
62 static GstCaps *gst_rtp_celt_pay_getcaps (GstRTPBasePayload * payload,
63 GstPad * pad, GstCaps * filter);
64 static GstFlowReturn gst_rtp_celt_pay_handle_buffer (GstRTPBasePayload *
65 payload, GstBuffer * buffer);
67 #define gst_rtp_celt_pay_parent_class parent_class
68 G_DEFINE_TYPE (GstRtpCELTPay, gst_rtp_celt_pay, GST_TYPE_RTP_BASE_PAYLOAD);
71 gst_rtp_celt_pay_class_init (GstRtpCELTPayClass * klass)
73 GObjectClass *gobject_class;
74 GstElementClass *gstelement_class;
75 GstRTPBasePayloadClass *gstrtpbasepayload_class;
77 GST_DEBUG_CATEGORY_INIT (rtpceltpay_debug, "rtpceltpay", 0,
78 "CELT RTP Payloader");
80 gobject_class = (GObjectClass *) klass;
81 gstelement_class = (GstElementClass *) klass;
82 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
84 gobject_class->finalize = gst_rtp_celt_pay_finalize;
86 gstelement_class->change_state = gst_rtp_celt_pay_change_state;
88 gst_element_class_add_static_pad_template (gstelement_class,
89 &gst_rtp_celt_pay_sink_template);
90 gst_element_class_add_static_pad_template (gstelement_class,
91 &gst_rtp_celt_pay_src_template);
93 gst_element_class_set_static_metadata (gstelement_class, "RTP CELT payloader",
94 "Codec/Payloader/Network/RTP",
95 "Payload-encodes CELT audio into a RTP packet",
96 "Wim Taymans <wim.taymans@gmail.com>");
98 gstrtpbasepayload_class->set_caps = gst_rtp_celt_pay_setcaps;
99 gstrtpbasepayload_class->get_caps = gst_rtp_celt_pay_getcaps;
100 gstrtpbasepayload_class->handle_buffer = gst_rtp_celt_pay_handle_buffer;
104 gst_rtp_celt_pay_init (GstRtpCELTPay * rtpceltpay)
106 rtpceltpay->queue = g_queue_new ();
110 gst_rtp_celt_pay_finalize (GObject * object)
112 GstRtpCELTPay *rtpceltpay;
114 rtpceltpay = GST_RTP_CELT_PAY (object);
116 g_queue_free (rtpceltpay->queue);
118 G_OBJECT_CLASS (parent_class)->finalize (object);
122 gst_rtp_celt_pay_clear_queued (GstRtpCELTPay * rtpceltpay)
126 while ((buf = g_queue_pop_head (rtpceltpay->queue)))
127 gst_buffer_unref (buf);
129 rtpceltpay->bytes = 0;
130 rtpceltpay->sbytes = 0;
131 rtpceltpay->qduration = 0;
135 gst_rtp_celt_pay_add_queued (GstRtpCELTPay * rtpceltpay, GstBuffer * buffer,
136 guint ssize, guint size, GstClockTime duration)
138 g_queue_push_tail (rtpceltpay->queue, buffer);
139 rtpceltpay->sbytes += ssize;
140 rtpceltpay->bytes += size;
141 /* only add durations when we have a valid previous duration */
142 if (rtpceltpay->qduration != -1) {
144 /* only add valid durations */
145 rtpceltpay->qduration += duration;
147 /* if we add a buffer without valid duration, our total queued duration
149 rtpceltpay->qduration = -1;
154 gst_rtp_celt_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
156 /* don't configure yet, we wait for the ident packet */
162 gst_rtp_celt_pay_getcaps (GstRTPBasePayload * payload, GstPad * pad,
165 GstCaps *otherpadcaps;
169 caps = gst_pad_get_pad_template_caps (pad);
171 otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
173 if (!gst_caps_is_empty (otherpadcaps)) {
176 gint clock_rate = 0, frame_size = 0, channels = 1;
178 caps = gst_caps_make_writable (caps);
180 ps = gst_caps_get_structure (otherpadcaps, 0);
181 s = gst_caps_get_structure (caps, 0);
183 if (gst_structure_get_int (ps, "clock-rate", &clock_rate)) {
184 gst_structure_fixate_field_nearest_int (s, "rate", clock_rate);
187 if ((params = gst_structure_get_string (ps, "frame-size")))
188 frame_size = atoi (params);
190 gst_structure_set (s, "frame-size", G_TYPE_INT, frame_size, NULL);
192 if ((params = gst_structure_get_string (ps, "encoding-params"))) {
193 channels = atoi (params);
194 gst_structure_fixate_field_nearest_int (s, "channels", channels);
197 GST_DEBUG_OBJECT (payload, "clock-rate=%d frame-size=%d channels=%d",
198 clock_rate, frame_size, channels);
200 gst_caps_unref (otherpadcaps);
206 GST_DEBUG_OBJECT (payload, "Intersect %" GST_PTR_FORMAT " and filter %"
207 GST_PTR_FORMAT, caps, filter);
208 tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
209 gst_caps_unref (caps);
217 gst_rtp_celt_pay_parse_ident (GstRtpCELTPay * rtpceltpay,
218 const guint8 * data, guint size)
220 guint32 version, header_size, rate, nb_channels, frame_size, overlap;
221 guint32 bytes_per_packet;
222 GstRTPBasePayload *payload;
226 /* we need the header string (8), the version string (20), the version
227 * and the header length. */
231 if (!g_str_has_prefix ((const gchar *) data, "CELT "))
234 /* skip header and version string */
237 version = GST_READ_UINT32_LE (data);
238 GST_DEBUG_OBJECT (rtpceltpay, "version %08x", version);
246 header_size = GST_READ_UINT32_LE (data);
247 if (header_size < 56)
248 goto header_too_small;
250 if (size < header_size)
251 goto payload_too_small;
254 rate = GST_READ_UINT32_LE (data);
256 nb_channels = GST_READ_UINT32_LE (data);
258 frame_size = GST_READ_UINT32_LE (data);
260 overlap = GST_READ_UINT32_LE (data);
262 bytes_per_packet = GST_READ_UINT32_LE (data);
264 GST_DEBUG_OBJECT (rtpceltpay, "rate %d, nb_channels %d, frame_size %d",
265 rate, nb_channels, frame_size);
266 GST_DEBUG_OBJECT (rtpceltpay, "overlap %d, bytes_per_packet %d",
267 overlap, bytes_per_packet);
269 payload = GST_RTP_BASE_PAYLOAD (rtpceltpay);
271 gst_rtp_base_payload_set_options (payload, "audio", FALSE, "CELT", rate);
272 cstr = g_strdup_printf ("%d", nb_channels);
273 fsstr = g_strdup_printf ("%d", frame_size);
274 res = gst_rtp_base_payload_set_outcaps (payload, "encoding-params",
275 G_TYPE_STRING, cstr, "frame-size", G_TYPE_STRING, fsstr, NULL);
284 GST_DEBUG_OBJECT (rtpceltpay,
285 "ident packet too small, need at least 32 bytes");
290 GST_DEBUG_OBJECT (rtpceltpay,
291 "ident packet does not start with \"CELT \"");
297 GST_DEBUG_OBJECT (rtpceltpay, "can only handle version 1, have version %d",
304 GST_DEBUG_OBJECT (rtpceltpay,
305 "header size too small, need at least 80 bytes, " "got only %d",
311 GST_DEBUG_OBJECT (rtpceltpay,
312 "payload too small, need at least %d bytes, got only %d", header_size,
319 gst_rtp_celt_pay_flush_queued (GstRtpCELTPay * rtpceltpay)
322 GstBuffer *buf, *outbuf;
323 guint8 *payload, *spayload;
325 GstClockTime duration;
326 GstRTPBuffer rtp = { NULL, };
328 payload_len = rtpceltpay->bytes + rtpceltpay->sbytes;
329 duration = rtpceltpay->qduration;
331 GST_DEBUG_OBJECT (rtpceltpay, "flushing out %u, duration %" GST_TIME_FORMAT,
332 payload_len, GST_TIME_ARGS (rtpceltpay->qduration));
334 /* get a big enough packet for the sizes + payloads */
336 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
337 (rtpceltpay), payload_len, 0, 0);
339 GST_BUFFER_DURATION (outbuf) = duration;
341 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
343 /* point to the payload for size headers and data */
344 spayload = gst_rtp_buffer_get_payload (&rtp);
345 payload = spayload + rtpceltpay->sbytes;
347 while ((buf = g_queue_pop_head (rtpceltpay->queue))) {
350 /* copy first timestamp to output */
351 if (GST_BUFFER_PTS (outbuf) == -1)
352 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buf);
354 /* write the size to the header */
355 size = gst_buffer_get_size (buf);
356 while (size > 0xff) {
363 size = gst_buffer_get_size (buf);
364 gst_buffer_extract (buf, 0, payload, size);
367 gst_rtp_copy_audio_meta (rtpceltpay, outbuf, buf);
369 gst_buffer_unref (buf);
371 gst_rtp_buffer_unmap (&rtp);
373 /* we consumed it all */
374 rtpceltpay->bytes = 0;
375 rtpceltpay->sbytes = 0;
376 rtpceltpay->qduration = 0;
378 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpceltpay), outbuf);
384 gst_rtp_celt_pay_handle_buffer (GstRTPBasePayload * basepayload,
388 GstRtpCELTPay *rtpceltpay;
391 GstClockTime duration, packet_dur;
392 guint i, ssize, packet_len;
394 rtpceltpay = GST_RTP_CELT_PAY (basepayload);
398 gst_buffer_map (buffer, &map, GST_MAP_READ);
400 switch (rtpceltpay->packet) {
402 /* ident packet. We need to parse the headers to construct the RTP
404 if (!gst_rtp_celt_pay_parse_ident (rtpceltpay, map.data, map.size))
409 /* comment packet, we ignore it */
412 /* other packets go in the payload */
415 gst_buffer_unmap (buffer, &map);
417 duration = GST_BUFFER_DURATION (buffer);
419 GST_LOG_OBJECT (rtpceltpay,
420 "got buffer of duration %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
421 GST_TIME_ARGS (duration), map.size);
423 /* calculate the size of the size field and the payload */
425 for (i = map.size; i > 0xff; i -= 0xff)
428 GST_DEBUG_OBJECT (rtpceltpay, "bytes for size %u", ssize);
430 /* calculate what the new size and duration would be of the packet */
431 payload_len = ssize + map.size + rtpceltpay->bytes + rtpceltpay->sbytes;
432 if (rtpceltpay->qduration != -1 && duration != -1)
433 packet_dur = rtpceltpay->qduration + duration;
437 packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
439 if (gst_rtp_base_payload_is_filled (basepayload, packet_len, packet_dur)) {
440 /* size or duration would overflow the packet, flush the queued data */
441 ret = gst_rtp_celt_pay_flush_queued (rtpceltpay);
444 /* queue the packet */
445 gst_rtp_celt_pay_add_queued (rtpceltpay, buffer, ssize, map.size, duration);
448 rtpceltpay->packet++;
455 gst_buffer_unmap (buffer, &map);
460 GST_ELEMENT_ERROR (rtpceltpay, STREAM, DECODE, (NULL),
461 ("Error parsing first identification packet."));
462 gst_buffer_unmap (buffer, &map);
463 return GST_FLOW_ERROR;
467 static GstStateChangeReturn
468 gst_rtp_celt_pay_change_state (GstElement * element, GstStateChange transition)
470 GstRtpCELTPay *rtpceltpay;
471 GstStateChangeReturn ret;
473 rtpceltpay = GST_RTP_CELT_PAY (element);
475 switch (transition) {
476 case GST_STATE_CHANGE_NULL_TO_READY:
478 case GST_STATE_CHANGE_READY_TO_PAUSED:
479 rtpceltpay->packet = 0;
485 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
487 switch (transition) {
488 case GST_STATE_CHANGE_PAUSED_TO_READY:
489 gst_rtp_celt_pay_clear_queued (rtpceltpay);
491 case GST_STATE_CHANGE_READY_TO_NULL:
500 gst_rtp_celt_pay_plugin_init (GstPlugin * plugin)
502 return gst_element_register (plugin, "rtpceltpay",
503 GST_RANK_SECONDARY, GST_TYPE_RTP_CELT_PAY);