liveMedia/WAVAudioFileSource.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2013 Live Networks, Inc.  All rights reserved.
00018 // A WAV audio file source
00019 // Implementation
00020 
00021 #include "WAVAudioFileSource.hh"
00022 #include "InputFile.hh"
00023 #include "GroupsockHelper.hh"
00024 
00026 
00027 WAVAudioFileSource*
00028 WAVAudioFileSource::createNew(UsageEnvironment& env, char const* fileName) {
00029   do {
00030     FILE* fid = OpenInputFile(env, fileName);
00031     if (fid == NULL) break;
00032 
00033     WAVAudioFileSource* newSource = new WAVAudioFileSource(env, fid);
00034     if (newSource != NULL && newSource->bitsPerSample() == 0) {
00035       // The WAV file header was apparently invalid.
00036       Medium::close(newSource);
00037       break;
00038     }
00039 
00040     newSource->fFileSize = (unsigned)GetFileSize(fileName, fid);
00041 
00042     return newSource;
00043   } while (0);
00044 
00045   return NULL;
00046 }
00047 
00048 unsigned WAVAudioFileSource::numPCMBytes() const {
00049   if (fFileSize < fWAVHeaderSize) return 0;
00050   return fFileSize - fWAVHeaderSize;
00051 }
00052 
00053 void WAVAudioFileSource::setScaleFactor(int scale) {
00054   if (!fFidIsSeekable) return; // we can't do 'trick play' operations on non-seekable files
00055 
00056   fScaleFactor = scale;
00057 
00058   if (fScaleFactor < 0 && TellFile64(fFid) > 0) {
00059     // Because we're reading backwards, seek back one sample, to ensure that
00060     // (i)  we start reading the last sample before the start point, and
00061     // (ii) we don't hit end-of-file on the first read.
00062     int bytesPerSample = (fNumChannels*fBitsPerSample)/8;
00063     if (bytesPerSample == 0) bytesPerSample = 1;
00064     SeekFile64(fFid, -bytesPerSample, SEEK_CUR);
00065   }
00066 }
00067 
00068 void WAVAudioFileSource::seekToPCMByte(unsigned byteNumber, unsigned numBytesToStream) {
00069   byteNumber += fWAVHeaderSize;
00070   if (byteNumber > fFileSize) byteNumber = fFileSize;
00071 
00072   SeekFile64(fFid, byteNumber, SEEK_SET);
00073 
00074   fNumBytesToStream = numBytesToStream;
00075   fLimitNumBytesToStream = fNumBytesToStream > 0;
00076 }
00077 
00078 unsigned char WAVAudioFileSource::getAudioFormat() {
00079   return fAudioFormat;
00080 }
00081 
00082 
00083 #define nextc fgetc(fid)
00084 
00085 static Boolean get4Bytes(FILE* fid, u_int32_t& result) { // little-endian
00086   int c0, c1, c2, c3;
00087   if ((c0 = nextc) == EOF || (c1 = nextc) == EOF ||
00088       (c2 = nextc) == EOF || (c3 = nextc) == EOF) return False;
00089   result = (c3<<24)|(c2<<16)|(c1<<8)|c0;
00090   return True;
00091 }
00092 
00093 static Boolean get2Bytes(FILE* fid, u_int16_t& result) {//little-endian
00094   int c0, c1;
00095   if ((c0 = nextc) == EOF || (c1 = nextc) == EOF) return False;
00096   result = (c1<<8)|c0;
00097   return True;
00098 }
00099 
00100 static Boolean skipBytes(FILE* fid, int num) {
00101   while (num-- > 0) {
00102     if (nextc == EOF) return False;
00103   }
00104   return True;
00105 }
00106 
00107 WAVAudioFileSource::WAVAudioFileSource(UsageEnvironment& env, FILE* fid)
00108   : AudioInputDevice(env, 0, 0, 0, 0)/* set the real parameters later */,
00109     fFid(fid), fFidIsSeekable(False), fLastPlayTime(0), fHaveStartedReading(False), fWAVHeaderSize(0), fFileSize(0),
00110     fScaleFactor(1), fLimitNumBytesToStream(False), fNumBytesToStream(0), fAudioFormat(WA_UNKNOWN) {
00111   // Check the WAV file header for validity.
00112   // Note: The following web pages contain info about the WAV format:
00113   // http://www.ringthis.com/dev/wave_format.htm
00114   // http://www.lightlink.com/tjweber/StripWav/Canon.html
00115   // http://www.onicos.com/staff/iz/formats/wav.html
00116 
00117   Boolean success = False; // until we learn otherwise
00118   do {
00119     // RIFF Chunk:
00120     if (nextc != 'R' || nextc != 'I' || nextc != 'F' || nextc != 'F') break;
00121     if (!skipBytes(fid, 4)) break;
00122     if (nextc != 'W' || nextc != 'A' || nextc != 'V' || nextc != 'E') break;
00123 
00124     // Skip over any chunk that's not a FORMAT ('fmt ') chunk:
00125     u_int32_t tmp;
00126     if (!get4Bytes(fid, tmp)) break;
00127     if (tmp != 0x20746d66/*'fmt ', little-endian*/) {
00128       // Skip this chunk:
00129       if (!get4Bytes(fid, tmp)) break;
00130       if (!skipBytes(fid, tmp)) break;
00131     }
00132 
00133     // FORMAT Chunk (the 4-byte header code has already been parsed):
00134     unsigned formatLength;
00135     if (!get4Bytes(fid, formatLength)) break;
00136     unsigned short audioFormat;
00137     if (!get2Bytes(fid, audioFormat)) break;
00138 
00139     fAudioFormat = (unsigned char)audioFormat;
00140     if (fAudioFormat != WA_PCM && fAudioFormat != WA_PCMA && fAudioFormat != WA_PCMU && fAudioFormat != WA_IMA_ADPCM) {
00141       // It's a format that we don't (yet) understand
00142       env.setResultMsg("Audio format is not one that we handle (PCM/PCMU/PCMA or IMA ADPCM)");
00143       break;
00144     }
00145     unsigned short numChannels;
00146     if (!get2Bytes(fid, numChannels)) break;
00147     fNumChannels = (unsigned char)numChannels;
00148     if (fNumChannels < 1 || fNumChannels > 2) { // invalid # channels
00149       char errMsg[100];
00150       sprintf(errMsg, "Bad # channels: %d", fNumChannels);
00151       env.setResultMsg(errMsg);
00152       break;
00153     }
00154     if (!get4Bytes(fid, fSamplingFrequency)) break;
00155     if (fSamplingFrequency == 0) {
00156       env.setResultMsg("Bad sampling frequency: 0");
00157       break;
00158     }
00159     if (!skipBytes(fid, 6)) break; // "nAvgBytesPerSec" (4 bytes) + "nBlockAlign" (2 bytes)
00160     unsigned short bitsPerSample;
00161     if (!get2Bytes(fid, bitsPerSample)) break;
00162     fBitsPerSample = (unsigned char)bitsPerSample;
00163     if (fBitsPerSample == 0) {
00164       env.setResultMsg("Bad bits-per-sample: 0");
00165       break;
00166     }
00167     if (!skipBytes(fid, formatLength - 16)) break;
00168 
00169     // FACT chunk (optional):
00170     int c = nextc;
00171     if (c == 'f') {
00172       if (nextc != 'a' || nextc != 'c' || nextc != 't') break;
00173       unsigned factLength;
00174       if (!get4Bytes(fid, factLength)) break;
00175       if (!skipBytes(fid, factLength)) break;
00176       c = nextc;
00177     }
00178 
00179     // DATA Chunk:
00180     if (c != 'd' || nextc != 'a' || nextc != 't' || nextc != 'a') break;
00181     if (!skipBytes(fid, 4)) break;
00182 
00183     // The header is good; the remaining data are the sample bytes.
00184     fWAVHeaderSize = (unsigned)TellFile64(fid);
00185     success = True;
00186   } while (0);
00187 
00188   if (!success) {
00189     env.setResultMsg("Bad WAV file format");
00190     // Set "fBitsPerSample" to zero, to indicate failure:
00191     fBitsPerSample = 0;
00192     return;
00193   }
00194 
00195   fPlayTimePerSample = 1e6/(double)fSamplingFrequency;
00196 
00197   // Although PCM is a sample-based format, we group samples into
00198   // 'frames' for efficient delivery to clients.  Set up our preferred
00199   // frame size to be close to 20 ms, if possible, but always no greater
00200   // than 1400 bytes (to ensure that it will fit in a single RTP packet)
00201   unsigned maxSamplesPerFrame = (1400*8)/(fNumChannels*fBitsPerSample);
00202   unsigned desiredSamplesPerFrame = (unsigned)(0.02*fSamplingFrequency);
00203   unsigned samplesPerFrame = desiredSamplesPerFrame < maxSamplesPerFrame ? desiredSamplesPerFrame : maxSamplesPerFrame;
00204   fPreferredFrameSize = (samplesPerFrame*fNumChannels*fBitsPerSample)/8;
00205 
00206   fFidIsSeekable = FileIsSeekable(fFid);
00207 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00208   // Now that we've finished reading the WAV header, all future reads (of audio samples) from the file will be asynchronous:
00209   makeSocketNonBlocking(fileno(fFid));
00210 #endif
00211 }
00212 
00213 WAVAudioFileSource::~WAVAudioFileSource() {
00214   if (fFid == NULL) return;
00215 
00216 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00217   envir().taskScheduler().turnOffBackgroundReadHandling(fileno(fFid));
00218 #endif
00219 
00220   CloseInputFile(fFid);
00221 }
00222 
00223 void WAVAudioFileSource::doGetNextFrame() {
00224   if (feof(fFid) || ferror(fFid) || (fLimitNumBytesToStream && fNumBytesToStream == 0)) {
00225     handleClosure(this);
00226     return;
00227   }
00228 
00229   fFrameSize = 0; // until it's set later
00230 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00231   doReadFromFile();
00232 #else
00233   if (!fHaveStartedReading) {
00234     // Await readable data from the file:
00235     envir().taskScheduler().turnOnBackgroundReadHandling(fileno(fFid),
00236                                                          (TaskScheduler::BackgroundHandlerProc*)&fileReadableHandler, this);
00237     fHaveStartedReading = True;
00238   }
00239 #endif
00240 }
00241 
00242 void WAVAudioFileSource::doStopGettingFrames() {
00243 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00244   envir().taskScheduler().turnOffBackgroundReadHandling(fileno(fFid));
00245   fHaveStartedReading = False;
00246 #endif
00247 }
00248 
00249 void WAVAudioFileSource::fileReadableHandler(WAVAudioFileSource* source, int /*mask*/) {
00250   if (!source->isCurrentlyAwaitingData()) {
00251     source->doStopGettingFrames(); // we're not ready for the data yet
00252     return;
00253   }
00254   source->doReadFromFile();
00255 }
00256 
00257 void WAVAudioFileSource::doReadFromFile() {
00258   // Try to read as many bytes as will fit in the buffer provided (or "fPreferredFrameSize" if less)
00259   if (fLimitNumBytesToStream && fNumBytesToStream < fMaxSize) {
00260     fMaxSize = fNumBytesToStream;
00261   }
00262   if (fPreferredFrameSize < fMaxSize) {
00263     fMaxSize = fPreferredFrameSize;
00264   }
00265   unsigned bytesPerSample = (fNumChannels*fBitsPerSample)/8;
00266   if (bytesPerSample == 0) bytesPerSample = 1; // because we can't read less than a byte at a time
00267 
00268   // For 'trick play', read one sample at a time; otherwise (normal case) read samples in bulk:
00269   unsigned bytesToRead = fScaleFactor == 1 ? fMaxSize - fMaxSize%bytesPerSample : bytesPerSample;
00270   unsigned numBytesRead;
00271   while (1) { // loop for 'trick play' only
00272 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00273     numBytesRead = fread(fTo, 1, bytesToRead, fFid);
00274 #else
00275     if (fFidIsSeekable) {
00276       numBytesRead = fread(fTo, 1, bytesToRead, fFid);
00277     } else {
00278       // For non-seekable files (e.g., pipes), call "read()" rather than "fread()", to ensure that the read doesn't block:
00279       numBytesRead = read(fileno(fFid), fTo, bytesToRead);
00280     }
00281 #endif
00282     if (numBytesRead == 0) {
00283      handleClosure(this);
00284       return;
00285     }
00286     fFrameSize += numBytesRead;
00287     fTo += numBytesRead;
00288     fMaxSize -= numBytesRead;
00289     fNumBytesToStream -= numBytesRead;
00290 
00291     // If we did an asynchronous read, and didn't read an integral number of samples, then we need to wait for another read:
00292 #ifndef READ_FROM_FILES_SYNCHRONOUSLY
00293     if (fFrameSize%bytesPerSample > 0) return;
00294 #endif
00295     
00296     // If we're doing 'trick play', then seek to the appropriate place for reading the next sample,
00297     // and keep reading until we fill the provided buffer:
00298     if (fScaleFactor != 1) {
00299       SeekFile64(fFid, (fScaleFactor-1)*bytesPerSample, SEEK_CUR);
00300       if (fMaxSize < bytesPerSample) break;
00301     } else {
00302       break; // from the loop (normal case)
00303     }
00304   }
00305 
00306   // Set the 'presentation time' and 'duration' of this frame:
00307   if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
00308     // This is the first frame, so use the current time:
00309     gettimeofday(&fPresentationTime, NULL);
00310   } else {
00311     // Increment by the play time of the previous data:
00312     unsigned uSeconds = fPresentationTime.tv_usec + fLastPlayTime;
00313     fPresentationTime.tv_sec += uSeconds/1000000;
00314     fPresentationTime.tv_usec = uSeconds%1000000;
00315   }
00316 
00317   // Remember the play time of this data:
00318   fDurationInMicroseconds = fLastPlayTime
00319     = (unsigned)((fPlayTimePerSample*fFrameSize)/bytesPerSample);
00320 
00321   // Inform the reader that he has data:
00322 #ifdef READ_FROM_FILES_SYNCHRONOUSLY
00323   // To avoid possible infinite recursion, we need to return to the event loop to do this:
00324   nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
00325                                 (TaskFunc*)FramedSource::afterGetting, this);
00326 #else
00327   // Because the file read was done from the event loop, we can call the
00328   // 'after getting' function directly, without risk of infinite recursion:
00329   FramedSource::afterGetting(this);
00330 #endif
00331 }
00332 
00333 Boolean WAVAudioFileSource::setInputPort(int /*portIndex*/) {
00334   return True;
00335 }
00336 
00337 double WAVAudioFileSource::getAverageLevel() const {
00338   return 0.0;//##### fix this later
00339 }

Generated on Mon Apr 29 13:28:03 2013 for live by  doxygen 1.5.2