LinuxAudioInputDevice.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) Massimo Cora' 2006 <maxcvs@email.it>
00003  *  This class is a "subclass" of AudioInputDevice from LiveMedia.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 
00020 
00021 #include "LinuxAudioInputDevice.hh"
00022 
00023 
00025 
00026 AudioInputDevice*
00027 AudioInputDevice::createNew(UsageEnvironment& env, int inputPortNumber,
00028                             unsigned char bitsPerSample,
00029                             unsigned char numChannels,
00030                             unsigned samplingFrequency,
00031                             unsigned granularityInMS) 
00032 {
00033         return new LinuxAudioInputDevice( env, inputPortNumber,
00034                                   bitsPerSample, numChannels,
00035                                   samplingFrequency, granularityInMS );
00036 }
00037 
00038 AudioPortNames* AudioInputDevice::getPortNames() 
00039 {
00040         AudioPortNames* portNames = new AudioPortNames;
00041         portNames->numPorts = 1;
00042         portNames->portName = new char*[1];
00043   
00044   
00045         return portNames;
00046 }
00047 
00048 
00050 
00051 
00052 LinuxAudioInputDevice::LinuxAudioInputDevice(UsageEnvironment& env, int inputPortNumber,
00053                           unsigned char bitsPerSample,
00054                           unsigned char numChannels,
00055                           unsigned samplingFrequency,
00056                           unsigned granularityInMS  ) : 
00057                 AudioInputDevice( env, bitsPerSample, numChannels, samplingFrequency, granularityInMS )
00058 {
00059         DEBUG_PRINT ("hey from LinuxAudioInputDevice::LinuxAudioInputDevice\n");
00060 
00061         _initialized = false;
00062         
00063     // initialize libavcodec, and register all codecs and formats
00064         avcodec_init();
00065         avcodec_register_all();
00066     av_register_all();
00067 
00068         AVFormatParameters ap;
00069         _ap = new AVFormatParameters;
00070         
00071         memset( &ap, 0, sizeof(AVFormatParameters));
00072         ap.channels = 2;
00073         ap.sample_rate = 44100;
00074         
00075         // search a suitable codec for PCM data.
00076         _pcm_codec = avcodec_find_decoder( CODEC_ID_PCM_S16LE );
00077     if ( !_pcm_codec ) {
00078         DEBUG_PRINT( "codec not found\n" );
00079         return;
00080     }   
00081 
00082         _format_context = av_alloc_format_context ();
00083 
00084         _audio_grab_format = "audio_device";
00085     _input_fmt = av_find_input_format( _audio_grab_format );
00086     if ( !_input_fmt ) {
00087         DEBUG_PRINT( "_input_fmt == NULL \n" );
00088         return;
00089         }       
00090         
00091         ap.device = strdup("/dev/dsp");
00092         
00093 
00094         if ( av_open_input_file( &_format_context, "", _input_fmt, 0, &ap ) < 0 ) {
00095         DEBUG_PRINT( "Could not find audio grab device\n" );
00096         return;
00097         }
00098         
00099         if ( (_format_context->ctx_flags & AVFMTCTX_NOHEADER) && av_find_stream_info( _format_context ) < 0 ) {
00100         DEBUG_PRINT ( "Could not find video grab parameters\n" );
00101         return;
00102     }   
00103         
00104         
00105         AVCodecContext *codec_context = NULL;
00106         codec_context = _format_context->streams[0]->codec;
00107         
00114         
00115     // open it 
00116     if ( avcodec_open( codec_context, _pcm_codec ) < 0 ) {
00117         DEBUG_PRINT( "could not open codec\n" );
00118         return;
00119     }
00120         
00121         
00122         _initialized = true;
00123 }
00124 
00125 
00126 LinuxAudioInputDevice::~LinuxAudioInputDevice()
00127 {
00128 }
00129 
00130 
00131 void LinuxAudioInputDevice::doGetNextFrame() 
00132 {
00133         AVPacket pkt;
00134 
00135         if ( _initialized == false ) {
00136                 DEBUG_PRINT ("device not initialized \n");
00137                 return;
00138         }
00139         
00140         if ( av_read_frame( _format_context, &pkt ) < 0) {
00141                 DEBUG_PRINT( "error in getting packet......\n" );       
00142         }
00143         
00144         fFrameSize = pkt.size;
00145         
00146         if ( pkt.size > fMaxSize ) {
00147                 DEBUG_PRINT( "pkt.size > fMaxSize \n" );        
00148                 return;
00149         }
00150 
00151         DEBUG_PRINT ("read from mic pkt.size %d\n", pkt.size );
00152         memcpy( fTo, pkt.data, pkt.size );      
00153 }
00154 
00155 void LinuxAudioInputDevice::doStopGettingFrames() 
00156 {
00157         // Turn off the audio poller:
00158         envir().taskScheduler().unscheduleDelayedTask(nextTask()); 
00159         nextTask() = NULL;
00160 }
00161 
00162 
00163 Boolean LinuxAudioInputDevice::setInputPort(int port_index)
00164 {
00165         return true;
00166 }
00167 
00168 double LinuxAudioInputDevice::getAverageLevel() const 
00169 {
00170         return 0;       
00171 }

Generated on Tue Dec 26 10:32:38 2006 for Omnimeeting by  doxygen 1.4.7