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 /** @file AmAudio.h */
28 #ifndef _AmAudio_h_
29 #define _AmAudio_h_
30
31 #include "AmThread.h"
32 #include "amci/amci.h"
33 #include "amci/codecs.h"
34 #include "AmEventQueue.h"
35
36 #include <stdio.h>
37
38 #include <memory>
39 using std::unique_ptr;
40 #include <string>
41 using std::string;
42 #include <map>
43
44 #ifdef USE_LIBSAMPLERATE
45 #include <samplerate.h>
46 #endif
47
48 #ifdef USE_INTERNAL_RESAMPLER
49 #include "resample/resample.h"
50 #endif
51
52 #define PCM16_B2S(b) ((b) >> 1)
53 #define PCM16_S2B(s) ((s) << 1)
AmAudioFormat(int codec_id,unsigned int rate)54
55 //#define SYSTEM_SAMPLERATE 8000 // fixme: sr per session
56 #ifndef SYSTEM_SAMPLECLOCK_RATE
57 #define SYSTEM_SAMPLECLOCK_RATE 32000
58 #endif
59
60 // Wallclock definitions:
61 //
62 // The wallclock is defined such that:
63 // - it is the highest clock rate is the system
64 // - any supported sample rate must be smaller
65 // - the difference between scaled down timers
66 // is always consistent with respect to overflows.
67 // - supported sample rates are multiples of 100
68 // (44100 is supported, 44110 is not)
69 #define WALLCLOCK_RATE 102400LL
70 //
71 // Wallclock overflow mask
72 #define WALLCLOCK_MASK 0xFFFFFFFFFFFFLL // 48 bit mask
73 //
74 // Wallclock increments
75 #define WC_INC_MS 10LL /* 10 ms */
76 #define WC_INC ((WALLCLOCK_RATE*WC_INC_MS)/1000LL)
77
78 struct SdpPayload;
79 struct CodecContainer;
80 struct Payload;
81
82 /** \brief Audio Event */
bytes2samples(unsigned int bytes) const83 class AmAudioEvent: public AmEvent
84 {
85 public:
86 enum EventType {
87
88 noAudio, // Audio class has nothing to play and/or record anymore
89
90 // Audio input & output have been cleared:
91 // !!! sent only from AmSession !!!
92 cleared
93 };
94
95 AmAudioEvent(int id):AmEvent(id){}
96 virtual ~AmAudioEvent() { }
97 };
98
99
operator !=(const AmAudioFormat & r) const100 /**
101 * \brief double buffer with back and front
102 * Implements double buffering.
103 */
104
105 class DblBuffer
106 {
107 /** Buffer. */
108 unsigned char samples[AUDIO_BUFFER_SIZE * 2];
109 /** 0 for first buffer, 1 for the second. */
110 int active_buf;
111
112 public:
113 /** Constructs a double buffer. */
114 DblBuffer();
115 /** Returns a pointer to the current front buffer. */
116 operator unsigned char*();
117 /** Returns a pointer to the current back buffer. */
118 unsigned char* back_buffer();
119 /** swaps front and back buffer. */
120 void swap();
121 };
destroyCodec()122
123 class AmAudio;
124
125 /**
126 * \brief Audio format structure.
127 * Holds a description of the format.
128 * @todo Create two child class:
129 * <ul>
130 * <li>file based format
resetCodec()131 * <li>payload based format
132 * </ul>
133 */
134
135 class AmAudioFormat
136 {
137 public:
138 /** Number of channels. */
139 int channels;
140
141 string sdp_format_parameters;
142 const char* sdp_format_parameters_out;
143
144 AmAudioFormat(int codec_id = CODEC_PCM16,
145 unsigned int rate = SYSTEM_SAMPLECLOCK_RATE);
146
147 virtual ~AmAudioFormat();
148
149 /** @return The format's codec pointer. */
150 virtual amci_codec_t* getCodec();
151 void resetCodec();
152
153 /** return the sampling rate */
154 unsigned int getRate() { return rate; }
155
156 /** set the sampling rate */
157 void setRate(unsigned int sample_rate);
158
159 /** @return Handler returned by the codec's init function.*/
160 long getHCodec();
161 long getHCodecNoInit() { return h_codec; } // do not initialize
162
163 unsigned int calcBytesToRead(unsigned int needed_samples) const;
164 unsigned int bytes2samples(unsigned int) const;
165
166 /** @return true if same format. */
167 bool operator == (const AmAudioFormat& r) const;
168 /** @return false if same format. */
169 bool operator != (const AmAudioFormat& r) const;
170
171 protected:
172 /** Codec id: @see amci/codecs.h */
173 int codec_id;
174
175 /** Sampling rate. */
176 unsigned int rate;
177
178 /** ==0 if not yet initialized. */
179 amci_codec_t* codec;
180 /** ==0 if not yet initialized. */
181 long h_codec;
182
183 /** Calls amci_codec_t::destroy() */
184 void destroyCodec();
185 /** Calls amci_codec_t::init() */
186 virtual void initCodec();
187
188 private:
189 void operator = (const AmAudioFormat& r);
190 };
191
192 /**
193 * \brief keeps the resampling state for one direction (input or output)
194 */
195 class AmResamplingState
196 {
197 public:
198 virtual unsigned int resample(unsigned char* samples, unsigned int size, double ratio) = 0;
199 virtual ~AmResamplingState() {}
200 };
201
202 #ifdef USE_LIBSAMPLERATE
203 class AmLibSamplerateResamplingState: public AmResamplingState
204 {
205 private:
206 SRC_STATE* resample_state;
207 float resample_in[PCM16_B2S(AUDIO_BUFFER_SIZE)*2];
208 float resample_out[PCM16_B2S(AUDIO_BUFFER_SIZE)];
209 size_t resample_buf_samples;
210 size_t resample_out_buf_samples;
211 public:
212 AmLibSamplerateResamplingState();
213 virtual ~AmLibSamplerateResamplingState();
214
215 virtual unsigned int resample(unsigned char* samples, unsigned int size, double ratio);
216 };
217 #endif
218
219 #ifdef USE_INTERNAL_RESAMPLER
220 class AmInternalResamplerState: public AmResamplingState
221 {
222 private:
223 Resample *rstate;
224
225 public:
226 AmInternalResamplerState();
AmInternalResamplerState()227 virtual ~AmInternalResamplerState();
228
229 virtual unsigned int resample(unsigned char* samples, unsigned int size, double ratio);
230 };
231 #endif
232
~AmInternalResamplerState()233 /**
234 * \brief base for classes that input or output audio.
235 *
236 * AmAudio binds a format and converts the samples if needed.
237 * <br>Internal Format: PCM signed 16 bit (mono | stereo).
238 */
239
240 class AmAudio
241 : public AmObject
242 {
243 private:
244 int rec_time; // in samples
245 int max_rec_time;
246
247 public:
248 enum ResamplingImplementationType {
249 LIBSAMPLERATE,
250 INTERNAL_RESAMPLER,
251 UNAVAILABLE
252 };
253
254 protected:
255 /** Sample buffer. */
256 DblBuffer samples;
257
258 /** Audio format. @see AmAudioFormat */
259 unique_ptr<AmAudioFormat> fmt;
260
261 /** Resampling states. @see AmResamplingState */
262 unique_ptr<AmResamplingState> input_resampling_state;
263 unique_ptr<AmResamplingState> output_resampling_state;
264
265 AmAudio();
266 AmAudio(AmAudioFormat *);
267
268 /** Gets 'size' bytes directly from stream (Read,Pull). */
269 virtual int read(unsigned int user_ts, unsigned int size) = 0;
270 /** Puts 'size' bytes directly from stream (Write,Push). */
271 virtual int write(unsigned int user_ts, unsigned int size) = 0;
272
273 /**
274 * Converts a buffer from stereo to mono.
275 * @param size [in,out] size in bytes
276 * <ul><li>Before call is size = input size</li><li>After the call is size = output size</li></ul>
277 */
278 void stereo2mono(unsigned char* out_buf,unsigned char* in_buf,unsigned int& size);
279
280 /**
281 * Converts from the input format to the internal format.
282 * <ul><li>input = front buffer</li><li>output = back buffer</li></ul>
283 * @param size [in] size in bytes
284 * @return new size in bytes
285 */
286 int decode(unsigned int size);
287 /**
288 * Converts from the internal format to the output format.
289 * <ul><li>input = front buffer</li><li>output = back buffer</li></ul>
290 * @param size [in] size in bytes
291 * @return new size in bytes
292 */
293 int encode(unsigned int size);
294
295 /**
296 * Converts to mono depending on the format.
297 * @return new size in bytes
298 */
299 unsigned int downMix(unsigned int size);
300
301 /**
302 * Resamples from the given input sample rate to the given output sample rate
303 * using the input resampling state. The input resampling state is created if
304 * it does not exist.
305 *
306 */
307 unsigned int resampleInput(unsigned char *buffer, unsigned int size, int input_sample_rate, int output_sample_rate);
308
309 /**
310 * Resamples from the given input sample rate to the given output sample rate
311 * using the output resampling state. The output resampling state is created if
312 * it does not exist.
313 *
314 */
315 unsigned int resampleOutput(unsigned char *buffer, unsigned int size, int input_sample_rate, int output_sample_rate);
316
317 /**
318 * Resamples from the given input sample rate to the given output sample rate using
319 * the given resampling state.
320 * <ul><li>input = front buffer</li><li>output = back buffer</li></ul>
321 * @param rstate resampling state to be used
322 * @param size [in] size in bytes
323 * @param output_sample_rate desired output sample rate
324 * @return new size in bytes
325 */
326 unsigned int resample(AmResamplingState& rstate, unsigned char *buffer, unsigned int size, int input_sample_rate, int output_sample_rate);
327
328 /**
329 * Get the number of bytes to read from encoded, depending on the format.
330 */
331 unsigned int calcBytesToRead(unsigned int needed_samples) const;
332
333 /**
334 * Convert the size from bytes to samples, depending on the format.
335 */
336 unsigned int bytes2samples(unsigned int bytes) const;
337
338 /**
339 * Scale a system timestamp down dependent on the sample rate.
340 */
341 unsigned int scaleSystemTS(unsigned long long system_ts);
342
343 public:
344 /** Destructor */
345 virtual ~AmAudio();
346
347 /** Closes the audio pipe. */
348 virtual void close();
349
350 /**
351 * Get some samples from input stream.
352 * @warning For packet based payloads / file formats, use:
353 * <pre> nb_sample = input buffer size / sample size of the reference format
354 * </pre> whereby the format with/from which the codec works is the reference one.
355 * @return # bytes read, else -1 if error (0 is OK)
356 */
357 virtual int get(unsigned long long system_ts, unsigned char* buffer,
358 int output_sample_rate, unsigned int nb_samples);
359
360 /**
361 * Put some samples to the output stream.
362 * @warning For packet based payloads / file formats, use:
363 * <pre> nb_sample = input buffer size / sample size of the reference format
364 * </pre> whereby the format with/from which the codec works is the reference one.
365 * @return # bytes written, else -1 if error (0 is OK)
366 */
367 virtual int put(unsigned long long system_ts, unsigned char* buffer,
368 int input_sample_rate, unsigned int size);
369
370 int getSampleRate();
371
372 void setRecordTime(unsigned int ms);
373 int incRecordTime(unsigned int samples);
374
375 void setBufferedOutput(unsigned int buffer_size);
376
377 void setFormat(AmAudioFormat* new_fmt);
378 };
379
380
381 #endif
382
383 // Local Variables:
384 // mode:C++
385 // End:
386
387
encode(unsigned int size)388
389