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