Boolean FileIsSeekable(FILE *fid) { if (SeekFile64(fid, 1, SEEK_CUR) < 0) { return False; } SeekFile64(fid, -1, SEEK_CUR); // seek back to where we were return True; }
void AVIFileSink::setWord(unsigned filePosn, unsigned size) { do { if (SeekFile64(fOutFid, filePosn, SEEK_SET) < 0) break; addWord(size); if (SeekFile64(fOutFid, 0, SEEK_END) < 0) break; // go back to where we were return; } while (0); // One of the SeekFile64()s failed, probable because we're not a seekable file envir() << "AVIFileSink::setWord(): SeekFile64 failed (err " << envir().getErrno() << ")\n"; }
void ByteStreamFileSource::seekToByteAbsolute(u_int64_t byteNumber, u_int64_t numBytesToStream) { SeekFile64(fFid, (int64_t)byteNumber, SEEK_SET); fNumBytesToStream = numBytesToStream; fLimitNumBytesToStream = fNumBytesToStream > 0; }
Boolean MPEG2TransportStreamIndexFile::seekToIndexRecord(unsigned long indexRecordNumber) { if (!openFid()) return False; if (indexRecordNumber == fCurrentIndexRecordNum) return True; // we're already there if (SeekFile64(fFid, (int64_t)(indexRecordNumber*INDEX_RECORD_SIZE), SEEK_SET) != 0) return False; fCurrentIndexRecordNum = indexRecordNumber; return True; }
ByteStreamFileSource::ByteStreamFileSource(UsageEnvironment& env, FILE* fid, unsigned preferredFrameSize, unsigned playTimePerFrame) : FramedFileSource(env, fid), fFileSize(0), fPreferredFrameSize(preferredFrameSize), fPlayTimePerFrame(playTimePerFrame), fLastPlayTime(0), fHaveStartedReading(False), fLimitNumBytesToStream(False), fNumBytesToStream(0) { #ifndef READ_FROM_FILES_SYNCHRONOUSLY makeSocketNonBlocking(fileno(fFid)); #endif // Test whether the file is seekable if (SeekFile64(fFid, 1, SEEK_CUR) >= 0) { fFidIsSeekable = True; SeekFile64(fFid, -1, SEEK_CUR); } else { fFidIsSeekable = False; } }
void WAVAudioFileSource::seekToPCMByte(unsigned byteNumber, unsigned numBytesToStream) { byteNumber += fWAVHeaderSize; if (byteNumber > fFileSize) byteNumber = fFileSize; SeekFile64(fFid, byteNumber, SEEK_SET); fNumBytesToStream = numBytesToStream; fLimitNumBytesToStream = fNumBytesToStream > 0; }
void WAVAudioFileSource::setScaleFactor(int scale) { if (!fFidIsSeekable) return; // we can't do 'trick play' operations on non-seekable files fScaleFactor = scale; if (fScaleFactor < 0 && TellFile64(fFid) > 0) { // Because we're reading backwards, seek back one sample, to ensure that // (i) we start reading the last sample before the start point, and // (ii) we don't hit end-of-file on the first read. int bytesPerSample = (fNumChannels*fBitsPerSample)/8; if (bytesPerSample == 0) bytesPerSample = 1; SeekFile64(fFid, -bytesPerSample, SEEK_CUR); } }
u_int64_t GetFileSize(char const* fileName, FILE* fid) { u_int64_t fileSize = 0; // by default if (fid != stdin) { #if !defined(_WIN32_WCE) if (fileName == NULL) { #endif if (fid != NULL && SeekFile64(fid, 0, SEEK_END) >= 0) { fileSize = (u_int64_t)TellFile64(fid); if (fileSize == (u_int64_t)-1) fileSize = 0; // TellFile64() failed SeekFile64(fid, 0, SEEK_SET); } #if !defined(_WIN32_WCE) } else { struct stat sb; if (stat(fileName, &sb) == 0) { fileSize = sb.st_size; } } #endif } return fileSize; }
void ByteStreamFileSource::seekToByteRelative(int64_t offset) { SeekFile64(fFid, offset, SEEK_CUR); }
void ByteStreamFileSource::seekToByteAbsolute(u_int64_t byteNumber) { SeekFile64(fFid, (int64_t)byteNumber, SEEK_SET); }
void ByteStreamFileSource::seekToEnd() { SeekFile64(fFid, 0, SEEK_END); }
void WAVAudioFileSource::seekToPCMByte(unsigned byteNumber) { byteNumber += fWAVHeaderSize; if (byteNumber > fFileSize) byteNumber = fFileSize; SeekFile64(fFid, byteNumber, SEEK_SET); }
void WAVAudioFileSource::doReadFromFile() { // Try to read as many bytes as will fit in the buffer provided (or "fPreferredFrameSize" if less) if (fLimitNumBytesToStream && fNumBytesToStream < fMaxSize) { fMaxSize = fNumBytesToStream; } if (fPreferredFrameSize < fMaxSize) { fMaxSize = fPreferredFrameSize; } unsigned bytesPerSample = (fNumChannels*fBitsPerSample)/8; if (bytesPerSample == 0) bytesPerSample = 1; // because we can't read less than a byte at a time // For 'trick play', read one sample at a time; otherwise (normal case) read samples in bulk: unsigned bytesToRead = fScaleFactor == 1 ? fMaxSize - fMaxSize%bytesPerSample : bytesPerSample; unsigned numBytesRead; while (1) { // loop for 'trick play' only #ifdef READ_FROM_FILES_SYNCHRONOUSLY numBytesRead = fread(fTo, 1, bytesToRead, fFid); #else if (fFidIsSeekable) { numBytesRead = fread(fTo, 1, bytesToRead, fFid); } else { // For non-seekable files (e.g., pipes), call "read()" rather than "fread()", to ensure that the read doesn't block: numBytesRead = read(fileno(fFid), fTo, bytesToRead); } #endif if (numBytesRead == 0) { handleClosure(); return; } fFrameSize += numBytesRead; fTo += numBytesRead; fMaxSize -= numBytesRead; fNumBytesToStream -= numBytesRead; // If we did an asynchronous read, and didn't read an integral number of samples, then we need to wait for another read: #ifndef READ_FROM_FILES_SYNCHRONOUSLY if (fFrameSize%bytesPerSample > 0) return; #endif // If we're doing 'trick play', then seek to the appropriate place for reading the next sample, // and keep reading until we fill the provided buffer: if (fScaleFactor != 1) { SeekFile64(fFid, (fScaleFactor-1)*bytesPerSample, SEEK_CUR); if (fMaxSize < bytesPerSample) break; } else { break; // from the loop (normal case) } } // Set the 'presentation time' and 'duration' of this frame: if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) { // This is the first frame, so use the current time: gettimeofday(&fPresentationTime, NULL); } else { // Increment by the play time of the previous data: unsigned uSeconds = fPresentationTime.tv_usec + fLastPlayTime; fPresentationTime.tv_sec += uSeconds/1000000; fPresentationTime.tv_usec = uSeconds%1000000; } // Remember the play time of this data: fDurationInMicroseconds = fLastPlayTime = (unsigned)((fPlayTimePerSample*fFrameSize)/bytesPerSample); // Inform the reader that he has data: #ifdef READ_FROM_FILES_SYNCHRONOUSLY // To avoid possible infinite recursion, we need to return to the event loop to do this: nextTask() = envir().taskScheduler().scheduleDelayedTask(0, (TaskFunc*)FramedSource::afterGetting, this); #else // Because the file read was done from the event loop, we can call the // 'after getting' function directly, without risk of infinite recursion: FramedSource::afterGetting(this); #endif }
void ByteStreamFileSource::seekToByteRelative(int64_t offset, u_int64_t numBytesToStream) { SeekFile64(fFid, offset, SEEK_CUR); fNumBytesToStream = numBytesToStream; fLimitNumBytesToStream = fNumBytesToStream > 0; }
void MP3StreamState::seekWithinFile(unsigned seekByteNumber) { if (fFidIsReallyASocket) return; // it's not seekable SeekFile64(fFid, seekByteNumber, SEEK_SET); }