1 /*
2  * Copyright (C) 2002-2003 Fhg Fokus
3  *
4  * This file is part of SEMS, a free SIP media server.
5  *
6  * SEMS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version. This program is released under
10  * the GPL with the additional exemption that compiling, linking,
11  * and/or using OpenSSL is allowed.
12  *
13  * For a license to use the SEMS software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * SEMS is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27 
28 #include "AmAudio.h"
29 #include "AmPlugIn.h"
30 #include "AmUtils.h"
31 #include "AmSdp.h"
32 #include "AmRtpStream.h"
33 #include "AmConfig.h"
34 #include "amci/codecs.h"
35 #include "log.h"
36 
37 #include <stdlib.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <errno.h>
41 
42 #include <typeinfo>
43 
44 /** \brief structure to hold loaded codec instances */
45 struct CodecContainer
46 {
47   amci_codec_t *codec;
AmObject()48   int frame_size;
~AmObject()49   int frame_length;
50   int frame_encoded_size;
51   long h_codec;
52 };
53 
54 AmAudioFormat::AmAudioFormat(int codec_id, unsigned int rate)
55   : channels(1),
56     sdp_format_parameters_out(NULL),
ArgBlobArgBlob57     codec_id(codec_id),
58     rate(rate),
59     codec(NULL)
60 {
61   codec = getCodec();
62 }
63 
64 AmAudioFormat::~AmAudioFormat()
65 {
66   destroyCodec();
67 }
68 
ArgBlobArgBlob69 void AmAudioFormat::setRate(unsigned int sample_rate)
70 {
71   rate = sample_rate;
72 }
73 
74 unsigned int AmAudioFormat::calcBytesToRead(unsigned int needed_samples) const
75 {
~ArgBlobArgBlob76   if (codec && codec->samples2bytes)
77     return codec->samples2bytes(h_codec, needed_samples) * channels; // FIXME: channels
78 
79   WARN("Cannot convert samples to bytes\n");
80   return needed_samples * channels;
81 }
82 
83 unsigned int AmAudioFormat::bytes2samples(unsigned int bytes) const
84 {
85   if (codec && codec->bytes2samples)
86     return codec->bytes2samples(h_codec, bytes) / channels;
87   WARN("Cannot convert bytes to samples\n");
88   return bytes / channels;
89 }
90 
91 bool AmAudioFormat::operator == (const AmAudioFormat& r) const
92 {
93   return ( codec && r.codec
94 	   && (r.codec->id == codec->id)
95 	   && (r.bytes2samples(1024) == bytes2samples(1024))
96 	   && (r.channels == channels)
97 	   && (r.rate == rate));
98 }
99 
100 bool AmAudioFormat::operator != (const AmAudioFormat& r) const
101 {
102   return !(this->operator == (r));
103 }
OutOfBoundsExceptionOutOfBoundsException104 
105 void AmAudioFormat::initCodec()
106 {
107   amci_codec_fmt_info_t* fmt_i = NULL;
108   sdp_format_parameters_out    = NULL; // reset
109 
110   if( codec && codec->init ) {
111     if ((h_codec = (*codec->init)(sdp_format_parameters.c_str(),
112 				  &sdp_format_parameters_out, &fmt_i)) == -1) {
113       ERROR("could not initialize codec %i\n",codec->id);
114     } else {
115       if (NULL != sdp_format_parameters_out) {
116 	DBG("negotiated fmt parameters '%s'\n", sdp_format_parameters_out);
117       }
118     }
119   }
120 }
121 
122 void AmAudioFormat::destroyCodec()
123 {
124   if( codec && codec->destroy ){
125     (*codec->destroy)(h_codec);
126     h_codec = 0;
127   }
128   codec = NULL;
129 }
130 
131 void AmAudioFormat::resetCodec() {
132   codec = NULL;
133   getCodec();
134 }
135 
AmArg()136 amci_codec_t* AmAudioFormat::getCodec()
137 {
138   if(!codec){
139     codec = AmPlugIn::instance()->codec(codec_id);
140     initCodec();
141   }
142 
143   return codec;
144 }
145 
146 long AmAudioFormat::getHCodec()
AmArg(const long int & v)147 {
148   if(!codec)
149     getCodec();
150   return h_codec;
151 }
AmArg(const long long int & v)152 
153 #ifdef USE_LIBSAMPLERATE
154 AmLibSamplerateResamplingState::AmLibSamplerateResamplingState()
155   : resample_state(NULL), resample_buf_samples(0), resample_out_buf_samples(0)
156 {
157 }
158 
159 AmLibSamplerateResamplingState::~AmLibSamplerateResamplingState()
160 {
161   if (NULL != resample_state) {
AmArg(const double & v)162     src_delete(resample_state);
163     resample_state=NULL;
164   }
165 }
166 
AmArg(const char * v)167 unsigned int AmLibSamplerateResamplingState::resample(unsigned char* samples, unsigned int s, double ratio)
168 {
169   DBG("resampling packet of size %d with ratio %f", s, ratio);
170   if (!resample_state) {
171     int src_error;
172     // for better quality but more CPU usage, use SRC_SINC_ converters
173     resample_state = src_new(SRC_LINEAR, 1, &src_error);
174     if (!resample_state) {
175       ERROR("samplerate initialization error: ");
176     }
177   }
178 
179   if (resample_state) {
180     if (resample_buf_samples + PCM16_B2S(s) > PCM16_B2S(AUDIO_BUFFER_SIZE) * 2) {
181       WARN("resample input buffer overflow! (%lu)\n", resample_buf_samples + PCM16_B2S(s));
182     } else if (resample_out_buf_samples + (PCM16_B2S(s) * ratio) + 20 > PCM16_B2S(AUDIO_BUFFER_SIZE)) {
183       WARN("resample: possible output buffer overflow! (%lu)\n", (resample_out_buf_samples + (size_t) ((PCM16_B2S(s) * ratio)) + 20));
184     } else {
185       signed short* samples_s = (signed short*)samples;
186       src_short_to_float_array(samples_s, &resample_in[resample_buf_samples], PCM16_B2S(s));
187       resample_buf_samples += PCM16_B2S(s);
188     }
189 
190     SRC_DATA src_data;
191     src_data.data_in = resample_in;
192     src_data.input_frames = resample_buf_samples;
193     src_data.data_out = &resample_out[resample_out_buf_samples];
194     src_data.output_frames = PCM16_B2S(AUDIO_BUFFER_SIZE);
195     src_data.src_ratio = ratio;
196     src_data.end_of_input = 0;
197 
198     int src_err = src_process(resample_state, &src_data);
199     if (src_err) {
200       DBG("resample error: '%s'\n", src_strerror(src_err));
201     }else {
202       signed short* samples_s = (signed short*)(unsigned char*)samples;
203       resample_out_buf_samples += src_data.output_frames_gen;
204       s *= ratio;
205       src_float_to_short_array(resample_out, samples_s, PCM16_B2S(s));
206       DBG("resample: output_frames_gen = %ld", src_data.output_frames_gen);
207 
208       if (resample_buf_samples !=  (unsigned int)src_data.input_frames_used) {
209 	memmove(resample_in, &resample_in[src_data.input_frames_used],
210 		(resample_buf_samples - src_data.input_frames_used) * sizeof(float));
211       }
212       resample_buf_samples = resample_buf_samples - src_data.input_frames_used;
213 
214       if (resample_out_buf_samples != s) {
215 	memmove(resample_out, &resample_out[PCM16_B2S(s)], (resample_out_buf_samples - PCM16_B2S(s)) * sizeof(float));
216       }
217       resample_out_buf_samples -= PCM16_B2S(s);
218     }
219   }
220 
221   DBG("resample: output size is %d", s);
222   return s;
223 }
224 #endif
225 
226 #ifdef USE_INTERNAL_RESAMPLER
227 AmInternalResamplerState::AmInternalResamplerState()
228   : rstate(NULL)
229 {
230   rstate = ResampleFactory::createResampleObj(true, 4.0, ResampleFactory::INTERPOL_SINC, ResampleFactory::SAMPLE_MONO);
231 }
232 
233 AmInternalResamplerState::~AmInternalResamplerState()
234 {
235   if (rstate != NULL)
236     ResampleFactory::destroyResampleObj(rstate);
237 }
238 
239 unsigned int AmInternalResamplerState::resample(unsigned char *samples, unsigned int s, double ratio)
240 {
241   if (rstate == NULL) {
242     ERROR("Uninitialized resampling state");
243     return s;
244   }
245 
246   //DBG("Resampling with ration %f", ratio);
247   //DBG("Putting %d samples in the buffer", PCM16_B2S(s));
248   rstate->put_samples((signed short *)samples, PCM16_B2S(s));
249   s = rstate->resample((signed short *)samples, ratio, PCM16_B2S(s) * ratio);
250   //DBG("Returning %d samples", s);
251   return PCM16_S2B(s);
252 }
253 #endif
254 
255 AmAudio::AmAudio()
256   : rec_time(0),
257     max_rec_time(-1),
258     fmt(new AmAudioFormat(CODEC_PCM16)),
259     input_resampling_state(nullptr),
260     output_resampling_state(nullptr)
261 {
262 }
setBorrowedPointer(AmObject * v)263 
264 AmAudio::AmAudio(AmAudioFormat *_fmt)
265   : rec_time(0),
266     max_rec_time(-1),
267     fmt(_fmt),
268     input_resampling_state(nullptr),
269     output_resampling_state(nullptr)
270 {
271 }
asBool()272 
273 AmAudio::~AmAudio()
274 {
275   close();
276 }
asBlob()277 
278 void AmAudio::setFormat(AmAudioFormat* new_fmt) {
279   fmt.reset(new_fmt);
280   fmt->resetCodec();
281 }
282 
283 void AmAudio::close()
284 {
285 }
286 
287 
288 // returns bytes read, else -1 if error (0 is OK)
289 int AmAudio::get(unsigned long long system_ts, unsigned char* buffer,
290 		 int output_sample_rate, unsigned int nb_samples)
291 {
292   int size = calcBytesToRead((int)((float)nb_samples * (float)getSampleRate()
293 				   / (float)output_sample_rate));
294 
295   unsigned int rd_ts = scaleSystemTS(system_ts);
296   //DBG("\tread(rd_ts = %10.u; size = %u)\n",rd_ts,size);
297   size = read(rd_ts,size);
298   if(size <= 0){
299     return size;
300   }
301 
302   size = decode(size);
303   if(size < 0) {
304     DBG("decode returned %i\n",size);
305     return -1;
306   }
307   size = downMix(size);
308 
309   size = resampleOutput((unsigned char*)samples, size,
310 			getSampleRate(), output_sample_rate);
311 
312   if(size>0)
313     memcpy(buffer,(unsigned char*)samples,size);
314 
315   return size;
316 }
317 
318 // returns bytes written, else -1 if error (0 is OK)
319 int AmAudio::put(unsigned long long system_ts, unsigned char* buffer,
320 		 int input_sample_rate, unsigned int size)
321 {
322   if(!size){
323     return 0;
324   }
325 
326   if(max_rec_time > -1 && rec_time >= max_rec_time)
327     return -1;
328 
329   memcpy((unsigned char*)samples,buffer,size);
330   size = resampleInput((unsigned char*)samples, size,
331 		       input_sample_rate, getSampleRate());
332 
333   int s = encode(size);
334   if(s>0){
335 
336     incRecordTime(bytes2samples(size));
337 
338     unsigned int wr_ts = scaleSystemTS(system_ts);
339     //DBG("write(wr_ts = %10.u; s = %u)\n",wr_ts,s);
340     return write(wr_ts,(unsigned int)s);
341   }
342   else{
343     return s;
344   }
345 }
346 
347 void AmAudio::stereo2mono(unsigned char* out_buf,unsigned char* in_buf,unsigned int& size)
348 {
349   short* in  = (short*)in_buf;
350   short* end = (short*)(in_buf + size);
351   short* out = (short*)out_buf;
352 
353   while(in != end){
354     *(out++) = (*in + *(in+1)) / 2;
355     in += 2;
356   }
357 
358   size /= 2;
359 }
360 
361 int AmAudio::decode(unsigned int size)
362 {
363   int s = size;
364 
365   if(!fmt.get()){
366     DBG("no fmt !\n");
367     return s;
368   }
369 
370   amci_codec_t* codec = fmt->getCodec();
371   long h_codec = fmt->getHCodec();
372 
373   if(!codec){
374     ERROR("audio format set, but no codec has been loaded\n");
375     return -1;
376   }
377 
378   if(codec->decode){
379     s = (*codec->decode)(samples.back_buffer(),samples,s,
380 			 fmt->channels,getSampleRate(),h_codec);
381     if(s<0) return s;
382     samples.swap();
383   }
384 
385   return s;
386 }
387 
388 int AmAudio::encode(unsigned int size)
389 {
390   int s = size;
391 
392   amci_codec_t* codec = fmt->getCodec();
393   long h_codec = fmt->getHCodec();
394 
395   assert(codec);
396   if(codec->encode){
397     s = (*codec->encode)(samples.back_buffer(),samples,(unsigned int) size,
398 			 fmt->channels,getSampleRate(),h_codec);
399     if(s<0) return s;
400     samples.swap();
401   }
402 
403   return s;
404 }
405 
406 unsigned int AmAudio::downMix(unsigned int size)
407 {
408   unsigned int s = size;
409   if(fmt->channels == 2){
410     stereo2mono(samples.back_buffer(),(unsigned char*)samples,s);
411     samples.swap();
412   }
413 
414   return s;
415 }
416 
417 unsigned int AmAudio::resampleInput(unsigned char* buffer, unsigned int s, int input_sample_rate, int output_sample_rate)
418 {
419   if ((input_sample_rate == output_sample_rate) && !input_resampling_state.get()) {
420     return s;
421   }
422 
423   if (!input_resampling_state.get()) {
424 #ifdef USE_INTERNAL_RESAMPLER
425     if (AmConfig::ResamplingImplementationType == AmAudio::INTERNAL_RESAMPLER) {
426       DBG("using internal resampler for input");
427       input_resampling_state.reset(new AmInternalResamplerState());
428     } else
429 #endif
430 #ifdef USE_LIBSAMPLERATE
431       if (AmConfig::ResamplingImplementationType == AmAudio::LIBSAMPLERATE) {
432 	input_resampling_state.reset(new AmLibSamplerateResamplingState());
433       } else
434 #endif
435 	{
436 	  return s;
437 	}
438   }
439 
440   return resample(*input_resampling_state, buffer, s, input_sample_rate, output_sample_rate);
441 }
442 
443 unsigned int AmAudio::resampleOutput(unsigned char* buffer, unsigned int s, int input_sample_rate, int output_sample_rate)
444 {
445   if ((input_sample_rate == output_sample_rate)
446       && !output_resampling_state.get()) {
447     return s;
448   }
449 
450   if (!output_resampling_state.get()) {
451 #ifdef USE_INTERNAL_RESAMPLER
452     if (AmConfig::ResamplingImplementationType == AmAudio::INTERNAL_RESAMPLER) {
453       DBG("using internal resampler for output");
454       output_resampling_state.reset(new AmInternalResamplerState());
455     } else
456 #endif
457 #ifdef USE_LIBSAMPLERATE
458       if (AmConfig::ResamplingImplementationType == AmAudio::LIBSAMPLERATE) {
459 	output_resampling_state.reset(new AmLibSamplerateResamplingState());
460       } else
461 #endif
462 	{
463 	  return s;
464 	}
465   }
466 
467   return resample(*output_resampling_state, buffer, s, input_sample_rate, output_sample_rate);
468 }
469 
470 unsigned int AmAudio::resample(AmResamplingState& rstate, unsigned char* buffer, unsigned int s, int input_sample_rate, int output_sample_rate)
471 {
472   return rstate.resample((unsigned char*) buffer, s, ((double) output_sample_rate) / ((double) input_sample_rate));
473 }
474 
475 int AmAudio::getSampleRate()
476 {
477   if (!fmt.get())
478     return 0;
479 
480   return fmt->getRate();
481 }
482 
483 unsigned int AmAudio::scaleSystemTS(unsigned long long system_ts)
484 {
485   // pre-division by 100 is important
486   // so that the first multiplication
487   // does not overflow the 64bit int
488   unsigned long long user_ts =
489     system_ts * ((unsigned long long)getSampleRate() / 100)
490     / (WALLCLOCK_RATE / 100);
491 
492   return (unsigned int)user_ts;
493 }
494 
495 unsigned int AmAudio::calcBytesToRead(unsigned int nb_samples) const
496 {
497   return fmt->calcBytesToRead(nb_samples);
498 }
499 
500 unsigned int AmAudio::bytes2samples(unsigned int bytes) const
501 {
502   return fmt->bytes2samples(bytes);
503 }
504 
505 void AmAudio::setRecordTime(unsigned int ms)
506 {
507   max_rec_time = (ms * (getSampleRate() / 100)) / 10;
508 }
509 
510 int AmAudio::incRecordTime(unsigned int samples)
511 {
512   return rec_time += samples;
513 }
514 
515 
516 DblBuffer::DblBuffer()
517   : active_buf(0)
518 {
519   memset(samples, 0, AUDIO_BUFFER_SIZE * 2);
520 }
521 
522 DblBuffer::operator unsigned char*()
523 {
524   return samples + (active_buf ? AUDIO_BUFFER_SIZE : 0);
525 }
526 
527 unsigned char* DblBuffer::back_buffer()
528 {
529   return samples + (active_buf ? 0 : AUDIO_BUFFER_SIZE);
530 }
531 
532 void DblBuffer::swap()
533 {
534   active_buf = !active_buf;
535 }
536