2 * Siren Encoder Gst Element
4 * @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-sirenenc
25 * This encodes audio buffers into the Siren 16 codec (a 16khz extension of
26 * G.722.1) that is meant to be compatible with the Microsoft Windows Live
27 * Messenger(tm) implementation.
29 * Ref: http://www.polycom.com/company/about_us/technology/siren_g7221/index.html
36 #include "gstsirenenc.h"
40 GST_DEBUG_CATEGORY (sirenenc_debug);
41 #define GST_CAT_DEFAULT (sirenenc_debug)
43 #define FRAME_DURATION (20 * GST_MSECOND)
45 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
48 GST_STATIC_CAPS ("audio/x-siren, " "dct-length = (int) 320"));
50 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_STATIC_CAPS ("audio/x-raw-int, "
56 "endianness = (int) 1234, "
57 "signed = (boolean) true, "
58 "rate = (int) 16000, " "channels = (int) 1"));
60 /* signals and args */
74 static void gst_siren_enc_finalize (GObject * object);
76 static gboolean gst_siren_enc_sink_setcaps (GstPad * pad, GstCaps * caps);
77 static gboolean gst_siren_enc_sink_event (GstPad * pad, GstEvent * event);
79 static GstFlowReturn gst_siren_enc_chain (GstPad * pad, GstBuffer * buf);
80 static GstStateChangeReturn
81 gst_siren_change_state (GstElement * element, GstStateChange transition);
87 GST_DEBUG_CATEGORY_INIT (sirenenc_debug, "sirenenc", 0, "sirenenc");
90 GST_BOILERPLATE_FULL (GstSirenEnc, gst_siren_enc, GstElement,
91 GST_TYPE_ELEMENT, _do_init);
94 gst_siren_enc_base_init (gpointer klass)
96 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
98 gst_element_class_add_static_pad_template (element_class, &srctemplate);
99 gst_element_class_add_static_pad_template (element_class, &sinktemplate);
101 gst_element_class_set_details_simple (element_class, "Siren Encoder element",
102 "Codec/Encoder/Audio ",
103 "Encode 16bit PCM streams into the Siren7 codec",
104 "Youness Alaoui <kakaroto@kakaroto.homelinux.net>");
108 gst_siren_enc_class_init (GstSirenEncClass * klass)
110 GObjectClass *gobject_class;
111 GstElementClass *gstelement_class;
113 gobject_class = (GObjectClass *) klass;
114 gstelement_class = (GstElementClass *) klass;
116 GST_DEBUG ("Initializing Class");
118 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_siren_enc_finalize);
120 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_siren_change_state);
122 GST_DEBUG ("Class Init done");
126 gst_siren_enc_init (GstSirenEnc * enc, GstSirenEncClass * klass)
129 GST_DEBUG_OBJECT (enc, "Initializing");
130 enc->encoder = Siren7_NewEncoder (16000);
131 enc->adapter = gst_adapter_new ();
133 enc->sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
134 enc->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
136 gst_pad_set_setcaps_function (enc->sinkpad,
137 GST_DEBUG_FUNCPTR (gst_siren_enc_sink_setcaps));
138 gst_pad_set_event_function (enc->sinkpad,
139 GST_DEBUG_FUNCPTR (gst_siren_enc_sink_event));
140 gst_pad_set_chain_function (enc->sinkpad,
141 GST_DEBUG_FUNCPTR (gst_siren_enc_chain));
143 gst_element_add_pad (GST_ELEMENT (enc), enc->sinkpad);
144 gst_element_add_pad (GST_ELEMENT (enc), enc->srcpad);
146 GST_DEBUG_OBJECT (enc, "Init done");
150 gst_siren_enc_finalize (GObject * object)
152 GstSirenEnc *enc = GST_SIREN_ENC (object);
154 GST_DEBUG_OBJECT (object, "Disposing");
156 Siren7_CloseEncoder (enc->encoder);
157 g_object_unref (enc->adapter);
159 G_OBJECT_CLASS (parent_class)->finalize (object);
163 gst_siren_enc_sink_setcaps (GstPad * pad, GstCaps * caps)
169 enc = GST_SIREN_ENC (GST_PAD_PARENT (pad));
171 outcaps = gst_static_pad_template_get_caps (&srctemplate);
172 res = gst_pad_set_caps (enc->srcpad, outcaps);
173 gst_caps_unref (outcaps);
179 gst_siren_enc_sink_event (GstPad * pad, GstEvent * event)
184 enc = GST_SIREN_ENC (GST_PAD_PARENT (pad));
186 switch (GST_EVENT_TYPE (event)) {
188 gst_adapter_clear (enc->adapter);
189 res = gst_pad_push_event (enc->srcpad, event);
191 case GST_EVENT_FLUSH_STOP:
192 gst_adapter_clear (enc->adapter);
193 res = gst_pad_push_event (enc->srcpad, event);
196 res = gst_pad_push_event (enc->srcpad, event);
203 gst_siren_enc_chain (GstPad * pad, GstBuffer * buf)
206 GstFlowReturn ret = GST_FLOW_OK;
208 guint8 *in_data, *out_data;
209 guint8 *to_free = NULL;
210 guint i, size, num_frames;
211 gint out_size, in_size;
214 GstClockTime timestamp;
218 enc = GST_SIREN_ENC (GST_PAD_PARENT (pad));
220 discont = GST_BUFFER_IS_DISCONT (buf);
222 GST_DEBUG_OBJECT (enc, "received DISCONT, flush adapter");
223 gst_adapter_clear (enc->adapter);
227 gst_adapter_push (enc->adapter, buf);
229 size = gst_adapter_available (enc->adapter);
231 GST_LOG_OBJECT (enc, "Received buffer of size %d with adapter of size : %d",
232 GST_BUFFER_SIZE (buf), size);
234 /* we need to process 640 input bytes to produce 40 output bytes */
235 /* calculate the amount of frames we will handle */
236 num_frames = size / 640;
238 /* no frames, wait some more */
242 /* this is the input/output size */
243 in_size = num_frames * 640;
244 out_size = num_frames * 40;
246 GST_LOG_OBJECT (enc, "we have %u frames, %u in, %u out", num_frames, in_size,
249 /* set output caps when needed */
250 if ((outcaps = GST_PAD_CAPS (enc->srcpad)) == NULL) {
251 outcaps = gst_static_pad_template_get_caps (&srctemplate);
252 gst_pad_set_caps (enc->srcpad, outcaps);
253 gst_caps_unref (outcaps);
257 ret = gst_pad_alloc_buffer_and_set_caps (enc->srcpad, -1,
258 out_size, outcaps, &out_buf);
259 if (ret != GST_FLOW_OK)
262 /* get the timestamp for the output buffer */
263 timestamp = gst_adapter_prev_timestamp (enc->adapter, &distance);
265 /* add the amount of time taken by the distance */
267 timestamp += gst_util_uint64_scale_int (distance / 2, GST_SECOND, 16000);
270 "timestamp %" GST_TIME_FORMAT ", distance %" G_GUINT64_FORMAT,
271 GST_TIME_ARGS (timestamp), distance);
273 /* get the input data for all the frames */
274 to_free = in_data = gst_adapter_take (enc->adapter, in_size);
275 out_data = GST_BUFFER_DATA (out_buf);
277 for (i = 0; i < num_frames; i++) {
278 GST_LOG_OBJECT (enc, "Encoding frame %u/%u", i, num_frames);
280 /* encode 640 input bytes to 40 output bytes */
281 encode_ret = Siren7_EncodeFrame (enc->encoder, in_data, out_data);
285 /* move to next frame */
290 GST_LOG_OBJECT (enc, "Finished encoding");
294 GST_BUFFER_FLAG_SET (out_buf, GST_BUFFER_FLAG_DISCONT);
295 enc->discont = FALSE;
297 GST_BUFFER_TIMESTAMP (out_buf) = timestamp;
298 GST_BUFFER_DURATION (out_buf) = num_frames * FRAME_DURATION;
300 ret = gst_pad_push (enc->srcpad, out_buf);
311 GST_DEBUG_OBJECT (enc, "failed to pad_alloc buffer: %d (%s)", ret,
312 gst_flow_get_name (ret));
317 GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
318 ("Error encoding frame: %d", encode_ret));
319 ret = GST_FLOW_ERROR;
320 gst_buffer_unref (out_buf);
325 static GstStateChangeReturn
326 gst_siren_change_state (GstElement * element, GstStateChange transition)
328 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
329 GstSirenEnc *enc = GST_SIREN_ENC (element);
331 switch (transition) {
332 case GST_STATE_CHANGE_READY_TO_PAUSED:
333 enc->discont = FALSE;
339 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
341 switch (transition) {
342 case GST_STATE_CHANGE_PAUSED_TO_READY:
343 gst_adapter_clear (enc->adapter);
353 gst_siren_enc_plugin_init (GstPlugin * plugin)
355 return gst_element_register (plugin, "sirenenc",
356 GST_RANK_MARGINAL, GST_TYPE_SIREN_ENC);