• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Siren Encoder Gst Element
3  *
4  *   @author: Youness Alaoui <kakaroto@kakaroto.homelinux.net>
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 /**
23  * SECTION:element-sirenenc
24  * @title: sirenenc
25  *
26  * This encodes audio buffers into the Siren 16 codec (a 16khz extension of
27  * G.722.1) that is meant to be compatible with the Microsoft Windows Live
28  * Messenger(tm) implementation.
29  *
30  * Ref: http://www.polycom.com/company/about_us/technology/siren_g7221/index.html
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include "gstsirenenc.h"
38 
39 #include <string.h>
40 
41 GST_DEBUG_CATEGORY (sirenenc_debug);
42 #define GST_CAT_DEFAULT (sirenenc_debug)
43 
44 #define FRAME_DURATION  (20 * GST_MSECOND)
45 
46 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("audio/x-siren, " "dct-length = (int) 320"));
50 
51 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
52     GST_PAD_SINK,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("audio/x-raw, format = (string) \"S16LE\", "
55         "rate = (int) 16000, " "channels = (int) 1"));
56 
57 static gboolean gst_siren_enc_start (GstAudioEncoder * enc);
58 static gboolean gst_siren_enc_stop (GstAudioEncoder * enc);
59 static gboolean gst_siren_enc_set_format (GstAudioEncoder * enc,
60     GstAudioInfo * info);
61 static GstFlowReturn gst_siren_enc_handle_frame (GstAudioEncoder * enc,
62     GstBuffer * in_buf);
63 
64 G_DEFINE_TYPE (GstSirenEnc, gst_siren_enc, GST_TYPE_AUDIO_ENCODER);
65 GST_ELEMENT_REGISTER_DEFINE (sirenenc, "sirenenc",
66     GST_RANK_MARGINAL, GST_TYPE_SIREN_ENC);
67 
68 static void
gst_siren_enc_class_init(GstSirenEncClass * klass)69 gst_siren_enc_class_init (GstSirenEncClass * klass)
70 {
71   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
72   GstAudioEncoderClass *base_class = GST_AUDIO_ENCODER_CLASS (klass);
73 
74   GST_DEBUG_CATEGORY_INIT (sirenenc_debug, "sirenenc", 0, "sirenenc");
75 
76   gst_element_class_add_static_pad_template (element_class, &srctemplate);
77   gst_element_class_add_static_pad_template (element_class, &sinktemplate);
78 
79   gst_element_class_set_static_metadata (element_class, "Siren Encoder element",
80       "Codec/Encoder/Audio ",
81       "Encode 16bit PCM streams into the Siren7 codec",
82       "Youness Alaoui <kakaroto@kakaroto.homelinux.net>");
83 
84   base_class->start = GST_DEBUG_FUNCPTR (gst_siren_enc_start);
85   base_class->stop = GST_DEBUG_FUNCPTR (gst_siren_enc_stop);
86   base_class->set_format = GST_DEBUG_FUNCPTR (gst_siren_enc_set_format);
87   base_class->handle_frame = GST_DEBUG_FUNCPTR (gst_siren_enc_handle_frame);
88 
89   GST_DEBUG ("Class Init done");
90 }
91 
92 static void
gst_siren_enc_init(GstSirenEnc * enc)93 gst_siren_enc_init (GstSirenEnc * enc)
94 {
95   GST_PAD_SET_ACCEPT_TEMPLATE (GST_AUDIO_ENCODER_SINK_PAD (enc));
96 }
97 
98 static gboolean
gst_siren_enc_start(GstAudioEncoder * enc)99 gst_siren_enc_start (GstAudioEncoder * enc)
100 {
101   GstSirenEnc *senc = GST_SIREN_ENC (enc);
102 
103   GST_DEBUG_OBJECT (enc, "start");
104 
105   senc->encoder = Siren7_NewEncoder (16000);
106 
107   return TRUE;
108 }
109 
110 static gboolean
gst_siren_enc_stop(GstAudioEncoder * enc)111 gst_siren_enc_stop (GstAudioEncoder * enc)
112 {
113   GstSirenEnc *senc = GST_SIREN_ENC (enc);
114 
115   GST_DEBUG_OBJECT (senc, "stop");
116 
117   Siren7_CloseEncoder (senc->encoder);
118 
119   return TRUE;
120 }
121 
122 static gboolean
gst_siren_enc_set_format(GstAudioEncoder * benc,GstAudioInfo * info)123 gst_siren_enc_set_format (GstAudioEncoder * benc, GstAudioInfo * info)
124 {
125   gboolean res;
126   GstCaps *outcaps;
127 
128   outcaps = gst_static_pad_template_get_caps (&srctemplate);
129   res = gst_audio_encoder_set_output_format (benc, outcaps);
130   gst_caps_unref (outcaps);
131 
132   /* report needs to base class */
133   gst_audio_encoder_set_frame_samples_min (benc, 320);
134   gst_audio_encoder_set_frame_samples_max (benc, 320);
135   /* no remainder or flushing please */
136   gst_audio_encoder_set_hard_min (benc, TRUE);
137   gst_audio_encoder_set_drainable (benc, FALSE);
138 
139   return res;
140 }
141 
142 static GstFlowReturn
gst_siren_enc_handle_frame(GstAudioEncoder * benc,GstBuffer * buf)143 gst_siren_enc_handle_frame (GstAudioEncoder * benc, GstBuffer * buf)
144 {
145   GstSirenEnc *enc;
146   GstFlowReturn ret = GST_FLOW_OK;
147   GstBuffer *out_buf;
148   guint8 *in_data, *out_data;
149   guint i, size, num_frames;
150   gint out_size;
151 #ifndef GST_DISABLE_GST_DEBUG
152   gint in_size;
153 #endif
154   gint encode_ret;
155   GstMapInfo inmap, outmap;
156 
157   g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
158 
159   enc = GST_SIREN_ENC (benc);
160 
161   size = gst_buffer_get_size (buf);
162 
163   GST_LOG_OBJECT (enc, "Received buffer of size %d", size);
164 
165   g_return_val_if_fail (size > 0, GST_FLOW_ERROR);
166   g_return_val_if_fail (size % 640 == 0, GST_FLOW_ERROR);
167 
168   /* we need to process 640 input bytes to produce 40 output bytes */
169   /* calculate the amount of frames we will handle */
170   num_frames = size / 640;
171 
172   /* this is the input/output size */
173 #ifndef GST_DISABLE_GST_DEBUG
174   in_size = num_frames * 640;
175 #endif
176   out_size = num_frames * 40;
177 
178   GST_LOG_OBJECT (enc, "we have %u frames, %u in, %u out", num_frames, in_size,
179       out_size);
180 
181   /* get a buffer */
182   out_buf = gst_audio_encoder_allocate_output_buffer (benc, out_size);
183   if (out_buf == NULL)
184     goto alloc_failed;
185 
186   /* get the input data for all the frames */
187   gst_buffer_map (buf, &inmap, GST_MAP_READ);
188   gst_buffer_map (out_buf, &outmap, GST_MAP_READ);
189   in_data = inmap.data;
190   out_data = outmap.data;
191 
192   for (i = 0; i < num_frames; i++) {
193     GST_LOG_OBJECT (enc, "Encoding frame %u/%u", i, num_frames);
194 
195     /* encode 640 input bytes to 40 output bytes */
196     encode_ret = Siren7_EncodeFrame (enc->encoder, in_data, out_data);
197     if (encode_ret != 0)
198       goto encode_error;
199 
200     /* move to next frame */
201     out_data += 40;
202     in_data += 640;
203   }
204 
205   gst_buffer_unmap (buf, &inmap);
206   gst_buffer_unmap (out_buf, &outmap);
207 
208   GST_LOG_OBJECT (enc, "Finished encoding");
209 
210   /* we encode all we get, pass it along */
211   ret = gst_audio_encoder_finish_frame (benc, out_buf, -1);
212 
213 done:
214   return ret;
215 
216   /* ERRORS */
217 alloc_failed:
218   {
219     GST_DEBUG_OBJECT (enc, "failed to pad_alloc buffer: %d (%s)", ret,
220         gst_flow_get_name (ret));
221     goto done;
222   }
223 encode_error:
224   {
225     GST_ELEMENT_ERROR (enc, STREAM, ENCODE, (NULL),
226         ("Error encoding frame: %d", encode_ret));
227     ret = GST_FLOW_ERROR;
228     gst_buffer_unref (out_buf);
229     goto done;
230   }
231 }
232