rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpceltdepay.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 <string.h>
25 #include <stdlib.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpceltdepay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpceltdepay_debug);
31 #define GST_CAT_DEFAULT (rtpceltdepay_debug)
32
33 /* RtpCELTDepay signals and args */
34
35 #define DEFAULT_FRAMESIZE       480
36 #define DEFAULT_CHANNELS        1
37 #define DEFAULT_CLOCKRATE       32000
38
39 enum
40 {
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum
46 {
47   ARG_0
48 };
49
50 static GstStaticPadTemplate gst_rtp_celt_depay_sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-rtp, "
55         "media = (string) \"audio\", "
56         "clock-rate =  (int) [32000, 48000], "
57         "encoding-name = (string) \"CELT\"")
58     );
59
60 static GstStaticPadTemplate gst_rtp_celt_depay_src_template =
61 GST_STATIC_PAD_TEMPLATE ("src",
62     GST_PAD_SRC,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("audio/x-celt")
65     );
66
67 static GstBuffer *gst_rtp_celt_depay_process (GstRTPBaseDepayload * depayload,
68     GstBuffer * buf);
69 static gboolean gst_rtp_celt_depay_setcaps (GstRTPBaseDepayload * depayload,
70     GstCaps * caps);
71
72 #define gst_rtp_celt_depay_parent_class parent_class
73 G_DEFINE_TYPE (GstRtpCELTDepay, gst_rtp_celt_depay,
74     GST_TYPE_RTP_BASE_DEPAYLOAD);
75
76 static void
77 gst_rtp_celt_depay_class_init (GstRtpCELTDepayClass * klass)
78 {
79   GstElementClass *gstelement_class;
80   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
81
82   GST_DEBUG_CATEGORY_INIT (rtpceltdepay_debug, "rtpceltdepay", 0,
83       "CELT RTP Depayloader");
84
85   gstelement_class = (GstElementClass *) klass;
86   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
87
88   gst_element_class_add_pad_template (gstelement_class,
89       gst_static_pad_template_get (&gst_rtp_celt_depay_src_template));
90   gst_element_class_add_pad_template (gstelement_class,
91       gst_static_pad_template_get (&gst_rtp_celt_depay_sink_template));
92
93   gst_element_class_set_static_metadata (gstelement_class,
94       "RTP CELT depayloader", "Codec/Depayloader/Network/RTP",
95       "Extracts CELT audio from RTP packets",
96       "Wim Taymans <wim.taymans@gmail.com>");
97
98   gstrtpbasedepayload_class->process = gst_rtp_celt_depay_process;
99   gstrtpbasedepayload_class->set_caps = gst_rtp_celt_depay_setcaps;
100 }
101
102 static void
103 gst_rtp_celt_depay_init (GstRtpCELTDepay * rtpceltdepay)
104 {
105 }
106
107 /* len 4 bytes LE,
108  * vendor string (len bytes),
109  * user_len 4 (0) bytes LE
110  */
111 static const gchar gst_rtp_celt_comment[] =
112     "\045\0\0\0Depayloaded with GStreamer celtdepay\0\0\0\0";
113
114 static gboolean
115 gst_rtp_celt_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
116 {
117   GstStructure *structure;
118   GstRtpCELTDepay *rtpceltdepay;
119   gint clock_rate, nb_channels = 0, frame_size = 0;
120   GstBuffer *buf;
121   GstMapInfo map;
122   guint8 *ptr;
123   const gchar *params;
124   GstCaps *srccaps;
125   gboolean res;
126
127   rtpceltdepay = GST_RTP_CELT_DEPAY (depayload);
128
129   structure = gst_caps_get_structure (caps, 0);
130
131   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
132     goto no_clockrate;
133   depayload->clock_rate = clock_rate;
134
135   if ((params = gst_structure_get_string (structure, "encoding-params")))
136     nb_channels = atoi (params);
137   if (!nb_channels)
138     nb_channels = DEFAULT_CHANNELS;
139
140   if ((params = gst_structure_get_string (structure, "frame-size")))
141     frame_size = atoi (params);
142   if (!frame_size)
143     frame_size = DEFAULT_FRAMESIZE;
144   rtpceltdepay->frame_size = frame_size;
145
146   GST_DEBUG_OBJECT (depayload, "clock-rate=%d channels=%d frame-size=%d",
147       clock_rate, nb_channels, frame_size);
148
149   /* construct minimal header and comment packet for the decoder */
150   buf = gst_buffer_new_and_alloc (60);
151   gst_buffer_map (buf, &map, GST_MAP_WRITE);
152   ptr = map.data;
153   memcpy (ptr, "CELT    ", 8);
154   ptr += 8;
155   memcpy (ptr, "1.1.12", 7);
156   ptr += 20;
157   GST_WRITE_UINT32_LE (ptr, 0x80000006);        /* version */
158   ptr += 4;
159   GST_WRITE_UINT32_LE (ptr, 56);        /* header_size */
160   ptr += 4;
161   GST_WRITE_UINT32_LE (ptr, clock_rate);        /* rate */
162   ptr += 4;
163   GST_WRITE_UINT32_LE (ptr, nb_channels);       /* channels */
164   ptr += 4;
165   GST_WRITE_UINT32_LE (ptr, frame_size);        /* frame-size */
166   ptr += 4;
167   GST_WRITE_UINT32_LE (ptr, -1);        /* overlap */
168   ptr += 4;
169   GST_WRITE_UINT32_LE (ptr, -1);        /* bytes_per_packet */
170   ptr += 4;
171   GST_WRITE_UINT32_LE (ptr, 0); /* extra headers */
172   gst_buffer_unmap (buf, &map);
173
174   srccaps = gst_caps_new_empty_simple ("audio/x-celt");
175   res = gst_pad_set_caps (depayload->srcpad, srccaps);
176   gst_caps_unref (srccaps);
177
178   gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpceltdepay), buf);
179
180   buf = gst_buffer_new_and_alloc (sizeof (gst_rtp_celt_comment));
181   gst_buffer_fill (buf, 0, gst_rtp_celt_comment, sizeof (gst_rtp_celt_comment));
182
183   gst_rtp_base_depayload_push (GST_RTP_BASE_DEPAYLOAD (rtpceltdepay), buf);
184
185   return res;
186
187   /* ERRORS */
188 no_clockrate:
189   {
190     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
191     return FALSE;
192   }
193 }
194
195 static GstBuffer *
196 gst_rtp_celt_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
197 {
198   GstBuffer *outbuf = NULL;
199   guint8 *payload;
200   guint offset, pos, payload_len, total_size, size;
201   guint8 s;
202   gint clock_rate = 0, frame_size = 0;
203   GstClockTime framesize_ns = 0, timestamp;
204   guint n = 0;
205   GstRtpCELTDepay *rtpceltdepay;
206   GstRTPBuffer rtp = { NULL, };
207
208   rtpceltdepay = GST_RTP_CELT_DEPAY (depayload);
209   clock_rate = depayload->clock_rate;
210   frame_size = rtpceltdepay->frame_size;
211   framesize_ns = gst_util_uint64_scale_int (frame_size, GST_SECOND, clock_rate);
212
213   timestamp = GST_BUFFER_TIMESTAMP (buf);
214
215   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
216
217   GST_LOG_OBJECT (depayload,
218       "got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
219       gst_buffer_get_size (buf), gst_rtp_buffer_get_marker (&rtp),
220       gst_rtp_buffer_get_timestamp (&rtp), gst_rtp_buffer_get_seq (&rtp));
221
222   GST_LOG_OBJECT (depayload, "got clock-rate=%d, frame_size=%d, "
223       "_ns=%" GST_TIME_FORMAT ", timestamp=%" GST_TIME_FORMAT, clock_rate,
224       frame_size, GST_TIME_ARGS (framesize_ns), GST_TIME_ARGS (timestamp));
225
226   payload = gst_rtp_buffer_get_payload (&rtp);
227   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
228
229   /* first count how many bytes are consumed by the size headers and make offset
230    * point to the first data byte */
231   total_size = 0;
232   offset = 0;
233   while (total_size < payload_len) {
234     do {
235       s = payload[offset++];
236       total_size += s + 1;
237     } while (s == 0xff);
238   }
239
240   /* offset is now pointing to the payload */
241   total_size = 0;
242   pos = 0;
243   while (total_size < payload_len) {
244     n++;
245     size = 0;
246     do {
247       s = payload[pos++];
248       size += s;
249       total_size += s + 1;
250     } while (s == 0xff);
251
252     outbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, offset, size);
253     offset += size;
254
255     if (frame_size != -1 && clock_rate != -1) {
256       GST_BUFFER_TIMESTAMP (outbuf) = timestamp + framesize_ns * n;
257       GST_BUFFER_DURATION (outbuf) = framesize_ns;
258     }
259     GST_LOG_OBJECT (depayload, "push timestamp=%"
260         GST_TIME_FORMAT ", duration=%" GST_TIME_FORMAT,
261         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)),
262         GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)));
263
264     gst_rtp_base_depayload_push (depayload, outbuf);
265   }
266   gst_rtp_buffer_unmap (&rtp);
267
268   return NULL;
269 }
270
271 gboolean
272 gst_rtp_celt_depay_plugin_init (GstPlugin * plugin)
273 {
274   return gst_element_register (plugin, "rtpceltdepay",
275       GST_RANK_SECONDARY, GST_TYPE_RTP_CELT_DEPAY);
276 }