AmAudio.h

Go to the documentation of this file.
00001 /*
00002  * $Id: AmAudio.h 819 2008-03-19 13:28:06Z sayer $
00003  *
00004  * Copyright (C) 2002-2003 Fhg Fokus
00005  *
00006  * This file is part of sems, a free SIP media server.
00007  *
00008  * sems is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * sems is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00028 #ifndef _AmAudio_h_
00029 #define _AmAudio_h_
00030 
00031 #include "AmThread.h"
00032 #include "amci/amci.h"
00033 #include "AmEventQueue.h"
00034 
00035 #include <stdio.h>
00036 
00037 #include <memory>
00038 using std::auto_ptr;
00039 #include <string>
00040 using std::string;
00041 #include <map>
00042 
00043 #ifdef USE_LIBSAMPLERATE 
00044 #include <samplerate.h>
00045 #endif
00046 
00047 #define PCM16_B2S(b) ((b) >> 1)
00048 #define PCM16_S2B(s) ((s) << 1)
00049 
00050 class SdpPayload;
00051 class CodecContainer;
00052 
00054 class AmAudioEvent: public AmEvent
00055 {
00056 public:
00057   enum EventType {
00058         
00059     noAudio, // Audio class has nothing to play and/or record anymore
00060 
00061     // Audio input & output have been cleared: 
00062     // !!! sent only from AmSession !!!
00063     cleared  
00064   };
00065 
00066   AmAudioEvent(int id):AmEvent(id){}
00067 };
00068 
00069 
00075 class DblBuffer
00076 {
00078   unsigned char samples[AUDIO_BUFFER_SIZE * 2];
00080   int active_buf;
00081 
00082 public:
00084   DblBuffer();
00086   operator unsigned char*();
00088   unsigned char* back_buffer();
00090   void swap();
00091 };
00092 
00093 class AmAudio;
00094 
00105 class AmAudioFormat
00106 {
00107 public:
00109   int channels;
00111   int rate;
00112   /* frame length in ms (frame based codecs) - unused */
00113   int frame_length;
00114   /* frame size in samples */
00115   int frame_size;
00116   /* encoded frame size in bytes - unused */
00117   int frame_encoded_size;
00118 
00119   string sdp_format_parameters;
00120     
00121   AmAudioFormat();
00122   virtual ~AmAudioFormat();
00123 
00125   virtual amci_codec_t*    getCodec();
00126   void resetCodec();
00127 
00129   long             getHCodec();
00130   long             getHCodecNoInit() { return h_codec; } // do not initialize
00131 
00132   unsigned int calcBytesToRead(unsigned int needed_samples) const;
00133   unsigned int bytes2samples(unsigned int) const;
00134 
00136   bool operator == (const AmAudioFormat& r) const;
00138   bool operator != (const AmAudioFormat& r) const;
00139 
00140 protected:
00141   virtual int getCodecId()=0;
00142 
00144   amci_codec_t*   codec;
00146   long            h_codec;
00147 
00149   void destroyCodec();
00151   void initCodec();
00152 
00153 private:
00154   void operator = (const AmAudioFormat& r);
00155 };
00156 
00158 class AmAudioSimpleFormat: public AmAudioFormat
00159 {
00160   int codec_id;
00161 
00162 protected:
00163   virtual int getCodecId() { return codec_id; }
00164 
00165 public:
00166   AmAudioSimpleFormat(int codec_id);
00167 };
00168 
00169 
00171 class AmAudioRtpFormat: public AmAudioFormat
00172 {
00173   vector<SdpPayload *> m_payloads;
00174   int m_currentPayload;
00175   amci_payload_t *m_currentPayloadP;
00176   std::map<int, SdpPayload *> m_sdpPayloadByPayload;
00177   std::map<int, amci_payload_t *> m_payloadPByPayload;
00178   std::map<int, CodecContainer *> m_codecContainerByPayload;
00179 
00180 protected:
00181   virtual int getCodecId();
00182 
00183 public:
00189   AmAudioRtpFormat(const vector<SdpPayload *>& payloads);
00190   ~AmAudioRtpFormat();
00191 
00195   int setCurrentPayload(int payload);
00196 
00197   int getCurrentPayload() { return m_currentPayload; };
00198 };
00199 
00207 class AmAudio
00208 {
00209 private:
00210   int rec_time; // in samples
00211   int max_rec_time;
00212 
00213 #ifdef USE_LIBSAMPLERATE 
00214   SRC_STATE* resample_state;
00215   float resample_in[PCM16_B2S(AUDIO_BUFFER_SIZE)*2];
00216   float resample_out[PCM16_B2S(AUDIO_BUFFER_SIZE)];
00217   size_t resample_buf_samples;
00218 #endif
00219 
00220 protected:
00222   DblBuffer samples;
00223   
00225   auto_ptr<AmAudioFormat> fmt;
00226 
00227   AmAudio();
00228   AmAudio(AmAudioFormat *);
00229 
00230 
00232   virtual int read(unsigned int user_ts, unsigned int size) = 0;
00234   virtual int write(unsigned int user_ts, unsigned int size) = 0;
00235 
00241   void stereo2mono(unsigned char* out_buf,unsigned char* in_buf,unsigned int& size);
00242 
00249   int decode(unsigned int size);
00256   int encode(unsigned int size);
00257 
00262   unsigned int downMix(unsigned int size);
00263 
00267   unsigned int calcBytesToRead(unsigned int needed_samples) const;
00268 
00272   unsigned int bytes2samples(unsigned int bytes) const;
00273 
00274 public:
00275   //bool begin_talk;
00276 
00277   virtual ~AmAudio();
00278 
00280   virtual void close();
00281 
00289   virtual int get(unsigned int user_ts, unsigned char* buffer, unsigned int nb_samples);
00290 
00298   virtual int put(unsigned int user_ts, unsigned char* buffer, unsigned int size);
00299   
00300   unsigned int getFrameSize();
00301 
00302   void setRecordTime(unsigned int ms);
00303   int  incRecordTime(unsigned int samples);
00304 
00305   void setBufferedOutput(unsigned int buffer_size);
00306 
00307   void setFormat(AmAudioFormat* new_fmt);
00308 };
00309 
00310 
00311 #endif
00312 
00313 // Local Variables:
00314 // mode:C++
00315 // End:
00316 
00317 
00318 

Generated on Fri May 16 12:02:05 2008 for SEMS by  doxygen 1.5.1
Home |  Recent changes |  Search |  Glossary |  Sitemap |  Login