diff options
author | Andreas Huber <andih@google.com> | 2009-11-16 15:34:01 -0800 |
---|---|---|
committer | Andreas Huber <andih@google.com> | 2009-11-17 10:21:03 -0800 |
commit | 03475f5af25e4aa32d46d41dd2ec756853d8dd9d (patch) | |
tree | aa3a35c429a4307d2383931b3dd2e72fe55277f2 /media/libstagefright/FileSource.cpp | |
parent | 057eacf4578ac6b90ee18c524670e01445bf2732 (diff) | |
download | frameworks_av-03475f5af25e4aa32d46d41dd2ec756853d8dd9d.zip frameworks_av-03475f5af25e4aa32d46d41dd2ec756853d8dd9d.tar.gz frameworks_av-03475f5af25e4aa32d46d41dd2ec756853d8dd9d.tar.bz2 |
Update FileSource to also accept a file descriptor and a range.
Diffstat (limited to 'media/libstagefright/FileSource.cpp')
-rw-r--r-- | media/libstagefright/FileSource.cpp | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/media/libstagefright/FileSource.cpp b/media/libstagefright/FileSource.cpp index f318ee3..130239e 100644 --- a/media/libstagefright/FileSource.cpp +++ b/media/libstagefright/FileSource.cpp @@ -20,7 +20,17 @@ namespace android { FileSource::FileSource(const char *filename) - : mFile(fopen(filename, "rb")) { + : mFile(fopen(filename, "rb")), + mOffset(0), + mLength(-1) { +} + +FileSource::FileSource(int fd, int64_t offset, int64_t length) + : mFile(fdopen(fd, "rb")), + mOffset(offset), + mLength(length) { + CHECK(offset >= 0); + CHECK(length >= 0); } FileSource::~FileSource() { @@ -37,12 +47,33 @@ status_t FileSource::initCheck() const { ssize_t FileSource::readAt(off_t offset, void *data, size_t size) { Mutex::Autolock autoLock(mLock); - int err = fseeko(mFile, offset, SEEK_SET); + if (mLength >= 0) { + if (offset >= mLength) { + return 0; // read beyond EOF. + } + int64_t numAvailable = mLength - offset; + if ((int64_t)size > numAvailable) { + size = numAvailable; + } + } + + int err = fseeko(mFile, offset + mOffset, SEEK_SET); CHECK(err != -1); - ssize_t result = fread(data, 1, size, mFile); + return fread(data, 1, size, mFile); +} + +status_t FileSource::getSize(off_t *size) { + if (mLength >= 0) { + *size = mLength; + + return OK; + } + + fseek(mFile, SEEK_END, 0); + *size = ftello(mFile); - return result; + return OK; } } // namespace android |