00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
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
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
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
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 }