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 "gstrtpelements.h"
30 #include "gstrtpceltpay.h"
31 #include "gstrtputils.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpceltpay_debug);
34 #define GST_CAT_DEFAULT (rtpceltpay_debug)
36 static GstStaticPadTemplate gst_rtp_celt_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_STATIC_CAPS ("audio/x-celt, "
41 "rate = (int) [ 32000, 64000 ], "
42 "channels = (int) [1, 2], " "frame-size = (int) [ 64, 512 ]")
45 static GstStaticPadTemplate gst_rtp_celt_pay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
49 GST_STATIC_CAPS ("application/x-rtp, "
50 "media = (string) \"audio\", "
51 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52 "clock-rate = (int) [ 32000, 48000 ], "
53 "encoding-name = (string) \"CELT\"")
56 static void gst_rtp_celt_pay_finalize (GObject * object);
58 static GstStateChangeReturn gst_rtp_celt_pay_change_state (GstElement *
59 element, GstStateChange transition);
61 static gboolean gst_rtp_celt_pay_setcaps (GstRTPBasePayload * payload,
63 static GstCaps *gst_rtp_celt_pay_getcaps (GstRTPBasePayload * payload,
64 GstPad * pad, GstCaps * filter);
65 static GstFlowReturn gst_rtp_celt_pay_handle_buffer (GstRTPBasePayload *
66 payload, GstBuffer * buffer);
68 #define gst_rtp_celt_pay_parent_class parent_class
69 G_DEFINE_TYPE (GstRtpCELTPay, gst_rtp_celt_pay, GST_TYPE_RTP_BASE_PAYLOAD);
70 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpceltpay, "rtpceltpay",
71 GST_RANK_SECONDARY, GST_TYPE_RTP_CELT_PAY, rtp_element_init (plugin));
74 gst_rtp_celt_pay_class_init (GstRtpCELTPayClass * klass)
76 GObjectClass *gobject_class;
77 GstElementClass *gstelement_class;
78 GstRTPBasePayloadClass *gstrtpbasepayload_class;
80 GST_DEBUG_CATEGORY_INIT (rtpceltpay_debug, "rtpceltpay", 0,
81 "CELT RTP Payloader");
83 gobject_class = (GObjectClass *) klass;
84 gstelement_class = (GstElementClass *) klass;
85 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
87 gobject_class->finalize = gst_rtp_celt_pay_finalize;
89 gstelement_class->change_state = gst_rtp_celt_pay_change_state;
91 gst_element_class_add_static_pad_template (gstelement_class,
92 &gst_rtp_celt_pay_sink_template);
93 gst_element_class_add_static_pad_template (gstelement_class,
94 &gst_rtp_celt_pay_src_template);
96 gst_element_class_set_static_metadata (gstelement_class, "RTP CELT payloader",
97 "Codec/Payloader/Network/RTP",
98 "Payload-encodes CELT audio into a RTP packet",
99 "Wim Taymans <wim.taymans@gmail.com>");
101 gstrtpbasepayload_class->set_caps = gst_rtp_celt_pay_setcaps;
102 gstrtpbasepayload_class->get_caps = gst_rtp_celt_pay_getcaps;
103 gstrtpbasepayload_class->handle_buffer = gst_rtp_celt_pay_handle_buffer;
107 gst_rtp_celt_pay_init (GstRtpCELTPay * rtpceltpay)
109 rtpceltpay->queue = g_queue_new ();
113 gst_rtp_celt_pay_finalize (GObject * object)
115 GstRtpCELTPay *rtpceltpay;
117 rtpceltpay = GST_RTP_CELT_PAY (object);
119 g_queue_free (rtpceltpay->queue);
121 G_OBJECT_CLASS (parent_class)->finalize (object);
125 gst_rtp_celt_pay_clear_queued (GstRtpCELTPay * rtpceltpay)
129 while ((buf = g_queue_pop_head (rtpceltpay->queue)))
130 gst_buffer_unref (buf);
132 rtpceltpay->bytes = 0;
133 rtpceltpay->sbytes = 0;
134 rtpceltpay->qduration = 0;
138 gst_rtp_celt_pay_add_queued (GstRtpCELTPay * rtpceltpay, GstBuffer * buffer,
139 guint ssize, guint size, GstClockTime duration)
141 g_queue_push_tail (rtpceltpay->queue, buffer);
142 rtpceltpay->sbytes += ssize;
143 rtpceltpay->bytes += size;
144 /* only add durations when we have a valid previous duration */
145 if (rtpceltpay->qduration != -1) {
147 /* only add valid durations */
148 rtpceltpay->qduration += duration;
150 /* if we add a buffer without valid duration, our total queued duration
152 rtpceltpay->qduration = -1;
157 gst_rtp_celt_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
159 /* don't configure yet, we wait for the ident packet */
165 gst_rtp_celt_pay_getcaps (GstRTPBasePayload * payload, GstPad * pad,
168 GstCaps *otherpadcaps;
172 caps = gst_pad_get_pad_template_caps (pad);
174 otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
176 if (!gst_caps_is_empty (otherpadcaps)) {
179 gint clock_rate = 0, frame_size = 0, channels = 1;
181 caps = gst_caps_make_writable (caps);
183 ps = gst_caps_get_structure (otherpadcaps, 0);
184 s = gst_caps_get_structure (caps, 0);
186 if (gst_structure_get_int (ps, "clock-rate", &clock_rate)) {
187 gst_structure_fixate_field_nearest_int (s, "rate", clock_rate);
190 if ((params = gst_structure_get_string (ps, "frame-size")))
191 frame_size = atoi (params);
193 gst_structure_set (s, "frame-size", G_TYPE_INT, frame_size, NULL);
195 if ((params = gst_structure_get_string (ps, "encoding-params"))) {
196 channels = atoi (params);
197 gst_structure_fixate_field_nearest_int (s, "channels", channels);
200 GST_DEBUG_OBJECT (payload, "clock-rate=%d frame-size=%d channels=%d",
201 clock_rate, frame_size, channels);
203 gst_caps_unref (otherpadcaps);
209 GST_DEBUG_OBJECT (payload, "Intersect %" GST_PTR_FORMAT " and filter %"
210 GST_PTR_FORMAT, caps, filter);
211 tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
212 gst_caps_unref (caps);
220 gst_rtp_celt_pay_parse_ident (GstRtpCELTPay * rtpceltpay,
221 const guint8 * data, guint size)
223 guint32 version, header_size, rate, nb_channels, frame_size, overlap;
224 guint32 bytes_per_packet;
225 GstRTPBasePayload *payload;
229 /* we need the header string (8), the version string (20), the version
230 * and the header length. */
234 if (!g_str_has_prefix ((const gchar *) data, "CELT "))
237 /* skip header and version string */
240 version = GST_READ_UINT32_LE (data);
241 GST_DEBUG_OBJECT (rtpceltpay, "version %08x", version);
249 header_size = GST_READ_UINT32_LE (data);
250 if (header_size < 56)
251 goto header_too_small;
253 if (size < header_size)
254 goto payload_too_small;
257 rate = GST_READ_UINT32_LE (data);
259 nb_channels = GST_READ_UINT32_LE (data);
261 frame_size = GST_READ_UINT32_LE (data);
263 overlap = GST_READ_UINT32_LE (data);
265 bytes_per_packet = GST_READ_UINT32_LE (data);
267 GST_DEBUG_OBJECT (rtpceltpay, "rate %d, nb_channels %d, frame_size %d",
268 rate, nb_channels, frame_size);
269 GST_DEBUG_OBJECT (rtpceltpay, "overlap %d, bytes_per_packet %d",
270 overlap, bytes_per_packet);
272 payload = GST_RTP_BASE_PAYLOAD (rtpceltpay);
274 gst_rtp_base_payload_set_options (payload, "audio", FALSE, "CELT", rate);
275 cstr = g_strdup_printf ("%d", nb_channels);
276 fsstr = g_strdup_printf ("%d", frame_size);
277 res = gst_rtp_base_payload_set_outcaps (payload, "encoding-params",
278 G_TYPE_STRING, cstr, "frame-size", G_TYPE_STRING, fsstr, NULL);
287 GST_DEBUG_OBJECT (rtpceltpay,
288 "ident packet too small, need at least 32 bytes");
293 GST_DEBUG_OBJECT (rtpceltpay,
294 "ident packet does not start with \"CELT \"");
300 GST_DEBUG_OBJECT (rtpceltpay, "can only handle version 1, have version %d",
307 GST_DEBUG_OBJECT (rtpceltpay,
308 "header size too small, need at least 80 bytes, " "got only %d",
314 GST_DEBUG_OBJECT (rtpceltpay,
315 "payload too small, need at least %d bytes, got only %d", header_size,
322 gst_rtp_celt_pay_flush_queued (GstRtpCELTPay * rtpceltpay)
325 GstBuffer *buf, *outbuf;
326 guint8 *payload, *spayload;
328 GstClockTime duration;
329 GstRTPBuffer rtp = { NULL, };
331 payload_len = rtpceltpay->bytes + rtpceltpay->sbytes;
332 duration = rtpceltpay->qduration;
334 GST_DEBUG_OBJECT (rtpceltpay, "flushing out %u, duration %" GST_TIME_FORMAT,
335 payload_len, GST_TIME_ARGS (rtpceltpay->qduration));
337 /* get a big enough packet for the sizes + payloads */
339 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
340 (rtpceltpay), payload_len, 0, 0);
342 GST_BUFFER_DURATION (outbuf) = duration;
344 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
346 /* point to the payload for size headers and data */
347 spayload = gst_rtp_buffer_get_payload (&rtp);
348 payload = spayload + rtpceltpay->sbytes;
350 while ((buf = g_queue_pop_head (rtpceltpay->queue))) {
353 /* copy first timestamp to output */
354 if (GST_BUFFER_PTS (outbuf) == -1)
355 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buf);
357 /* write the size to the header */
358 size = gst_buffer_get_size (buf);
359 while (size > 0xff) {
366 size = gst_buffer_get_size (buf);
367 gst_buffer_extract (buf, 0, payload, size);
370 gst_rtp_copy_audio_meta (rtpceltpay, outbuf, buf);
372 gst_buffer_unref (buf);
374 gst_rtp_buffer_unmap (&rtp);
376 /* we consumed it all */
377 rtpceltpay->bytes = 0;
378 rtpceltpay->sbytes = 0;
379 rtpceltpay->qduration = 0;
381 ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpceltpay), outbuf);
387 gst_rtp_celt_pay_handle_buffer (GstRTPBasePayload * basepayload,
391 GstRtpCELTPay *rtpceltpay;
394 GstClockTime duration, packet_dur;
395 guint i, ssize, packet_len;
397 rtpceltpay = GST_RTP_CELT_PAY (basepayload);
401 gst_buffer_map (buffer, &map, GST_MAP_READ);
403 switch (rtpceltpay->packet) {
405 /* ident packet. We need to parse the headers to construct the RTP
407 if (!gst_rtp_celt_pay_parse_ident (rtpceltpay, map.data, map.size))
412 /* comment packet, we ignore it */
415 /* other packets go in the payload */
418 gst_buffer_unmap (buffer, &map);
420 duration = GST_BUFFER_DURATION (buffer);
422 GST_LOG_OBJECT (rtpceltpay,
423 "got buffer of duration %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
424 GST_TIME_ARGS (duration), map.size);
426 /* calculate the size of the size field and the payload */
428 for (i = map.size; i > 0xff; i -= 0xff)
431 GST_DEBUG_OBJECT (rtpceltpay, "bytes for size %u", ssize);
433 /* calculate what the new size and duration would be of the packet */
434 payload_len = ssize + map.size + rtpceltpay->bytes + rtpceltpay->sbytes;
435 if (rtpceltpay->qduration != -1 && duration != -1)
436 packet_dur = rtpceltpay->qduration + duration;
440 packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
442 if (gst_rtp_base_payload_is_filled (basepayload, packet_len, packet_dur)) {
443 /* size or duration would overflow the packet, flush the queued data */
444 ret = gst_rtp_celt_pay_flush_queued (rtpceltpay);
447 /* queue the packet */
448 gst_rtp_celt_pay_add_queued (rtpceltpay, buffer, ssize, map.size, duration);
451 rtpceltpay->packet++;
458 gst_buffer_unmap (buffer, &map);
463 GST_ELEMENT_ERROR (rtpceltpay, STREAM, DECODE, (NULL),
464 ("Error parsing first identification packet."));
465 gst_buffer_unmap (buffer, &map);
466 return GST_FLOW_ERROR;
470 static GstStateChangeReturn
471 gst_rtp_celt_pay_change_state (GstElement * element, GstStateChange transition)
473 GstRtpCELTPay *rtpceltpay;
474 GstStateChangeReturn ret;
476 rtpceltpay = GST_RTP_CELT_PAY (element);
478 switch (transition) {
479 case GST_STATE_CHANGE_NULL_TO_READY:
481 case GST_STATE_CHANGE_READY_TO_PAUSED:
482 rtpceltpay->packet = 0;
488 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
490 switch (transition) {
491 case GST_STATE_CHANGE_PAUSED_TO_READY:
492 gst_rtp_celt_pay_clear_queued (rtpceltpay);
494 case GST_STATE_CHANGE_READY_TO_NULL: