Use new gst_element_class_set_static_metadata()
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpceltpay.c
1 /* GStreamer
2  * Copyright (C) <2009> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpceltpay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpceltpay_debug);
31 #define GST_CAT_DEFAULT (rtpceltpay_debug)
32
33 static GstStaticPadTemplate gst_rtp_celt_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("audio/x-celt, "
38         "rate = (int) [ 32000, 64000 ], "
39         "channels = (int) [1, 2], " "frame-size = (int) [ 64, 512 ]")
40     );
41
42 static GstStaticPadTemplate gst_rtp_celt_pay_src_template =
43 GST_STATIC_PAD_TEMPLATE ("src",
44     GST_PAD_SRC,
45     GST_PAD_ALWAYS,
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\"")
51     );
52
53 static void gst_rtp_celt_pay_finalize (GObject * object);
54
55 static GstStateChangeReturn gst_rtp_celt_pay_change_state (GstElement *
56     element, GstStateChange transition);
57
58 static gboolean gst_rtp_celt_pay_setcaps (GstRTPBasePayload * payload,
59     GstCaps * caps);
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);
64
65 #define gst_rtp_celt_pay_parent_class parent_class
66 G_DEFINE_TYPE (GstRtpCELTPay, gst_rtp_celt_pay, GST_TYPE_RTP_BASE_PAYLOAD);
67
68 static void
69 gst_rtp_celt_pay_class_init (GstRtpCELTPayClass * klass)
70 {
71   GObjectClass *gobject_class;
72   GstElementClass *gstelement_class;
73   GstRTPBasePayloadClass *gstrtpbasepayload_class;
74
75   GST_DEBUG_CATEGORY_INIT (rtpceltpay_debug, "rtpceltpay", 0,
76       "CELT RTP Payloader");
77
78   gobject_class = (GObjectClass *) klass;
79   gstelement_class = (GstElementClass *) klass;
80   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
81
82   gobject_class->finalize = gst_rtp_celt_pay_finalize;
83
84   gstelement_class->change_state = gst_rtp_celt_pay_change_state;
85
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));
90
91   gst_element_class_set_static_metadata (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>");
95
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;
99 }
100
101 static void
102 gst_rtp_celt_pay_init (GstRtpCELTPay * rtpceltpay)
103 {
104   rtpceltpay->queue = g_queue_new ();
105 }
106
107 static void
108 gst_rtp_celt_pay_finalize (GObject * object)
109 {
110   GstRtpCELTPay *rtpceltpay;
111
112   rtpceltpay = GST_RTP_CELT_PAY (object);
113
114   g_queue_free (rtpceltpay->queue);
115
116   G_OBJECT_CLASS (parent_class)->finalize (object);
117 }
118
119 static void
120 gst_rtp_celt_pay_clear_queued (GstRtpCELTPay * rtpceltpay)
121 {
122   GstBuffer *buf;
123
124   while ((buf = g_queue_pop_head (rtpceltpay->queue)))
125     gst_buffer_unref (buf);
126
127   rtpceltpay->bytes = 0;
128   rtpceltpay->sbytes = 0;
129   rtpceltpay->qduration = 0;
130 }
131
132 static void
133 gst_rtp_celt_pay_add_queued (GstRtpCELTPay * rtpceltpay, GstBuffer * buffer,
134     guint ssize, guint size, GstClockTime duration)
135 {
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) {
141     if (duration != -1)
142       /* only add valid durations */
143       rtpceltpay->qduration += duration;
144     else
145       /* if we add a buffer without valid duration, our total queued duration
146        * becomes unknown */
147       rtpceltpay->qduration = -1;
148   }
149 }
150
151 static gboolean
152 gst_rtp_celt_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
153 {
154   /* don't configure yet, we wait for the ident packet */
155   return TRUE;
156 }
157
158
159 static GstCaps *
160 gst_rtp_celt_pay_getcaps (GstRTPBasePayload * payload, GstPad * pad,
161     GstCaps * filter)
162 {
163   GstCaps *otherpadcaps;
164   GstCaps *caps;
165   const gchar *params;
166
167   caps = gst_pad_get_pad_template_caps (pad);
168
169   otherpadcaps = gst_pad_get_allowed_caps (payload->srcpad);
170   if (otherpadcaps) {
171     if (!gst_caps_is_empty (otherpadcaps)) {
172       GstStructure *ps;
173       GstStructure *s;
174       gint clock_rate = 0, frame_size = 0, channels = 1;
175
176       caps = gst_caps_make_writable (caps);
177
178       ps = gst_caps_get_structure (otherpadcaps, 0);
179       s = gst_caps_get_structure (caps, 0);
180
181       if (gst_structure_get_int (ps, "clock-rate", &clock_rate)) {
182         gst_structure_fixate_field_nearest_int (s, "rate", clock_rate);
183       }
184
185       if ((params = gst_structure_get_string (ps, "frame-size")))
186         frame_size = atoi (params);
187       if (frame_size)
188         gst_structure_set (s, "frame-size", G_TYPE_INT, frame_size, NULL);
189
190       if ((params = gst_structure_get_string (ps, "encoding-params"))) {
191         channels = atoi (params);
192         gst_structure_fixate_field_nearest_int (s, "channels", channels);
193       }
194
195       GST_DEBUG_OBJECT (payload, "clock-rate=%d frame-size=%d channels=%d",
196           clock_rate, frame_size, channels);
197     }
198     gst_caps_unref (otherpadcaps);
199   }
200
201   return caps;
202 }
203
204 static gboolean
205 gst_rtp_celt_pay_parse_ident (GstRtpCELTPay * rtpceltpay,
206     const guint8 * data, guint size)
207 {
208   guint32 version, header_size, rate, nb_channels, frame_size, overlap;
209   guint32 bytes_per_packet;
210   GstRTPBasePayload *payload;
211   gchar *cstr, *fsstr;
212   gboolean res;
213
214   /* we need the header string (8), the version string (20), the version
215    * and the header length. */
216   if (size < 36)
217     goto too_small;
218
219   if (!g_str_has_prefix ((const gchar *) data, "CELT    "))
220     goto wrong_header;
221
222   /* skip header and version string */
223   data += 28;
224
225   version = GST_READ_UINT32_LE (data);
226   GST_DEBUG_OBJECT (rtpceltpay, "version %08x", version);
227 #if 0
228   if (version != 1)
229     goto wrong_version;
230 #endif
231
232   data += 4;
233   /* ensure sizes */
234   header_size = GST_READ_UINT32_LE (data);
235   if (header_size < 56)
236     goto header_too_small;
237
238   if (size < header_size)
239     goto payload_too_small;
240
241   data += 4;
242   rate = GST_READ_UINT32_LE (data);
243   data += 4;
244   nb_channels = GST_READ_UINT32_LE (data);
245   data += 4;
246   frame_size = GST_READ_UINT32_LE (data);
247   data += 4;
248   overlap = GST_READ_UINT32_LE (data);
249   data += 4;
250   bytes_per_packet = GST_READ_UINT32_LE (data);
251
252   GST_DEBUG_OBJECT (rtpceltpay, "rate %d, nb_channels %d, frame_size %d",
253       rate, nb_channels, frame_size);
254   GST_DEBUG_OBJECT (rtpceltpay, "overlap %d, bytes_per_packet %d",
255       overlap, bytes_per_packet);
256
257   payload = GST_RTP_BASE_PAYLOAD (rtpceltpay);
258
259   gst_rtp_base_payload_set_options (payload, "audio", FALSE, "CELT", rate);
260   cstr = g_strdup_printf ("%d", nb_channels);
261   fsstr = g_strdup_printf ("%d", frame_size);
262   res = gst_rtp_base_payload_set_outcaps (payload, "encoding-params",
263       G_TYPE_STRING, cstr, "frame-size", G_TYPE_STRING, fsstr, NULL);
264   g_free (cstr);
265   g_free (fsstr);
266
267   return res;
268
269   /* ERRORS */
270 too_small:
271   {
272     GST_DEBUG_OBJECT (rtpceltpay,
273         "ident packet too small, need at least 32 bytes");
274     return FALSE;
275   }
276 wrong_header:
277   {
278     GST_DEBUG_OBJECT (rtpceltpay,
279         "ident packet does not start with \"CELT    \"");
280     return FALSE;
281   }
282 #if 0
283 wrong_version:
284   {
285     GST_DEBUG_OBJECT (rtpceltpay, "can only handle version 1, have version %d",
286         version);
287     return FALSE;
288   }
289 #endif
290 header_too_small:
291   {
292     GST_DEBUG_OBJECT (rtpceltpay,
293         "header size too small, need at least 80 bytes, " "got only %d",
294         header_size);
295     return FALSE;
296   }
297 payload_too_small:
298   {
299     GST_DEBUG_OBJECT (rtpceltpay,
300         "payload too small, need at least %d bytes, got only %d", header_size,
301         size);
302     return FALSE;
303   }
304 }
305
306 static GstFlowReturn
307 gst_rtp_celt_pay_flush_queued (GstRtpCELTPay * rtpceltpay)
308 {
309   GstFlowReturn ret;
310   GstBuffer *buf, *outbuf;
311   guint8 *payload, *spayload;
312   guint payload_len;
313   GstClockTime duration;
314   GstRTPBuffer rtp = { NULL, };
315
316   payload_len = rtpceltpay->bytes + rtpceltpay->sbytes;
317   duration = rtpceltpay->qduration;
318
319   GST_DEBUG_OBJECT (rtpceltpay, "flushing out %u, duration %" GST_TIME_FORMAT,
320       payload_len, GST_TIME_ARGS (rtpceltpay->qduration));
321
322   /* get a big enough packet for the sizes + payloads */
323   outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
324
325   GST_BUFFER_DURATION (outbuf) = duration;
326
327   gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
328
329   /* point to the payload for size headers and data */
330   spayload = gst_rtp_buffer_get_payload (&rtp);
331   payload = spayload + rtpceltpay->sbytes;
332
333   while ((buf = g_queue_pop_head (rtpceltpay->queue))) {
334     guint size;
335
336     /* copy first timestamp to output */
337     if (GST_BUFFER_TIMESTAMP (outbuf) == -1)
338       GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf);
339
340     /* write the size to the header */
341     size = gst_buffer_get_size (buf);
342     while (size > 0xff) {
343       *spayload++ = 0xff;
344       size -= 0xff;
345     }
346     *spayload++ = size;
347
348     /* copy payload */
349     size = gst_buffer_get_size (buf);
350     gst_buffer_extract (buf, 0, payload, size);
351     payload += size;
352
353     gst_buffer_unref (buf);
354   }
355   gst_rtp_buffer_unmap (&rtp);
356
357   /* we consumed it all */
358   rtpceltpay->bytes = 0;
359   rtpceltpay->sbytes = 0;
360   rtpceltpay->qduration = 0;
361
362   ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpceltpay), outbuf);
363
364   return ret;
365 }
366
367 static GstFlowReturn
368 gst_rtp_celt_pay_handle_buffer (GstRTPBasePayload * basepayload,
369     GstBuffer * buffer)
370 {
371   GstFlowReturn ret;
372   GstRtpCELTPay *rtpceltpay;
373   gsize payload_len;
374   GstMapInfo map;
375   GstClockTime duration, packet_dur;
376   guint i, ssize, packet_len;
377
378   rtpceltpay = GST_RTP_CELT_PAY (basepayload);
379
380   ret = GST_FLOW_OK;
381
382   gst_buffer_map (buffer, &map, GST_MAP_READ);
383
384   switch (rtpceltpay->packet) {
385     case 0:
386       /* ident packet. We need to parse the headers to construct the RTP
387        * properties. */
388       if (!gst_rtp_celt_pay_parse_ident (rtpceltpay, map.data, map.size))
389         goto parse_error;
390
391       goto cleanup;
392     case 1:
393       /* comment packet, we ignore it */
394       goto cleanup;
395     default:
396       /* other packets go in the payload */
397       break;
398   }
399   gst_buffer_unmap (buffer, &map);
400
401   duration = GST_BUFFER_DURATION (buffer);
402
403   GST_LOG_OBJECT (rtpceltpay,
404       "got buffer of duration %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
405       GST_TIME_ARGS (duration), map.size);
406
407   /* calculate the size of the size field and the payload */
408   ssize = 1;
409   for (i = map.size; i > 0xff; i -= 0xff)
410     ssize++;
411
412   GST_DEBUG_OBJECT (rtpceltpay, "bytes for size %u", ssize);
413
414   /* calculate what the new size and duration would be of the packet */
415   payload_len = ssize + map.size + rtpceltpay->bytes + rtpceltpay->sbytes;
416   if (rtpceltpay->qduration != -1 && duration != -1)
417     packet_dur = rtpceltpay->qduration + duration;
418   else
419     packet_dur = 0;
420
421   packet_len = gst_rtp_buffer_calc_packet_len (payload_len, 0, 0);
422
423   if (gst_rtp_base_payload_is_filled (basepayload, packet_len, packet_dur)) {
424     /* size or duration would overflow the packet, flush the queued data */
425     ret = gst_rtp_celt_pay_flush_queued (rtpceltpay);
426   }
427
428   /* queue the packet */
429   gst_rtp_celt_pay_add_queued (rtpceltpay, buffer, ssize, map.size, duration);
430
431 done:
432   rtpceltpay->packet++;
433
434   return ret;
435
436   /* ERRORS */
437 cleanup:
438   {
439     gst_buffer_unmap (buffer, &map);
440     goto done;
441   }
442 parse_error:
443   {
444     GST_ELEMENT_ERROR (rtpceltpay, STREAM, DECODE, (NULL),
445         ("Error parsing first identification packet."));
446     gst_buffer_unmap (buffer, &map);
447     return GST_FLOW_ERROR;
448   }
449 }
450
451 static GstStateChangeReturn
452 gst_rtp_celt_pay_change_state (GstElement * element, GstStateChange transition)
453 {
454   GstRtpCELTPay *rtpceltpay;
455   GstStateChangeReturn ret;
456
457   rtpceltpay = GST_RTP_CELT_PAY (element);
458
459   switch (transition) {
460     case GST_STATE_CHANGE_NULL_TO_READY:
461       break;
462     case GST_STATE_CHANGE_READY_TO_PAUSED:
463       rtpceltpay->packet = 0;
464       break;
465     default:
466       break;
467   }
468
469   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
470
471   switch (transition) {
472     case GST_STATE_CHANGE_PAUSED_TO_READY:
473       gst_rtp_celt_pay_clear_queued (rtpceltpay);
474       break;
475     case GST_STATE_CHANGE_READY_TO_NULL:
476       break;
477     default:
478       break;
479   }
480   return ret;
481 }
482
483 gboolean
484 gst_rtp_celt_pay_plugin_init (GstPlugin * plugin)
485 {
486   return gst_element_register (plugin, "rtpceltpay",
487       GST_RANK_SECONDARY, GST_TYPE_RTP_CELT_PAY);
488 }