MP3FromPCM.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) Massimo Cora' 2006 <maxcvs@email.it>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #include "MP3FromPCM.hh"
00021 
00022 #include <cv.h>
00023 #include <highgui.h>
00024 
00025 
00026 
00027 
00028 MP3FromPCM* MP3FromPCM::createNew( UsageEnvironment& env, FramedSource *audio_source ) 
00029 {
00030         return new MP3FromPCM( env, audio_source );
00031 }
00032 
00033 
00034 MP3FromPCM::~MP3FromPCM ()
00035 {
00036         
00037         if ( _audio_encoder )
00038                 delete _audio_encoder;
00039         _audio_encoder = NULL;
00040 
00041         if ( _input_buffer )
00042                 delete _input_buffer;
00043         _input_buffer = NULL;
00044 
00045         _initialized = false;
00046 }
00047 
00048 MP3FromPCM::MP3FromPCM( UsageEnvironment& env, FramedSource *audio_source ) : 
00049                                         FramedFilter( env, audio_source ),
00050                                         _audio_source( audio_source ),
00051                                         _input_buffer( NULL ),
00052                                         _input_buffer_size ( 0 )
00053 {
00054         _initialized = false;
00055         _audio_encoder = new OStreamAudioEncoder( MAX_AUDIO_PACKET_SIZE * 4, OSTREAM_ENCODING_MP3 );
00056 
00057         if ( _audio_encoder != NULL )
00058                 _initialized = true;
00059 }
00060 
00061 // Redefined virtual functions:
00062 void MP3FromPCM::doGetNextFrame() 
00063 {
00064         unsigned int bytes_to_read = AUDIO_BLOCK_SIZE;
00065 
00066         if ( bytes_to_read > _input_buffer_size ) {
00067         delete[] _input_buffer; 
00068                 _input_buffer = new unsigned char[bytes_to_read];
00069         _input_buffer_size = bytes_to_read;
00070         }
00071 
00072         // be sure to give a clean buffer where to store the
00073         memset( _input_buffer, 0, _input_buffer_size );
00074 
00075         // Arrange to read samples into the input buffer:
00076         _audio_source->getNextFrame( _input_buffer, bytes_to_read,
00077                                                         afterGettingFrame, this, FramedSource::handleClosure, 
00078                                                                 this);
00079 }
00080 
00081 void MP3FromPCM::afterGettingFrame(void* clientData, unsigned frameSize,
00082                                         unsigned numTruncatedBytes, struct timeval presentationTime,                                    
00083                                         unsigned durationInMicroseconds) 
00084 {
00085         MP3FromPCM* klass = (MP3FromPCM*)clientData;
00086         klass->afterGettingFrame1( frameSize, numTruncatedBytes,
00087                                                                 presentationTime, durationInMicroseconds );
00088 }
00089 
00090 void MP3FromPCM::afterGettingFrame1( unsigned frameSize, unsigned numTruncatedBytes,
00091                                                 struct timeval presentationTime,  unsigned durationInMicroseconds)
00092 {
00093 
00094         int encoded_frame_size;
00095         const unsigned char* encoded_frame;
00096 
00097         // encode the buffer
00098         encoded_frame = _audio_encoder->encode_audio_frame( _input_buffer, 
00099                                                                                 _input_buffer_size, &encoded_frame_size );
00100 
00101         if ( (unsigned int) encoded_frame_size > fMaxSize ) {
00102                 DEBUG_PRINT( "error: if ( encoded_frame_size > fMaxSize )\n");
00103         }
00104 
00105         memcpy( fTo, encoded_frame, encoded_frame_size );
00106 
00107         // Complete delivery to the client:
00108         fFrameSize = encoded_frame_size;
00109         fNumTruncatedBytes = numTruncatedBytes;
00110         fPresentationTime = presentationTime;
00111         fDurationInMicroseconds = durationInMicroseconds;
00112         afterGetting(this);
00113 }
00114 
00115 
00116 char const* MP3FromPCM::MIMEtype() const {
00117         return "audio/MPEG";
00118 }

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