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