Merge "Removed uncessary FILE structure pointer for I/O"

This commit is contained in:
James Dong
2010-11-19 16:49:10 -08:00
committed by Android (Google) Code Review
9 changed files with 76 additions and 62 deletions

View File

@ -44,7 +44,6 @@ protected:
virtual ~AMRWriter();
private:
FILE *mFile;
int mFd;
status_t mInitCheck;
sp<MediaSource> mSource;

View File

@ -46,7 +46,6 @@ protected:
virtual ~FileSource();
private:
FILE *mFile;
int mFd;
int64_t mOffset;
int64_t mLength;

View File

@ -61,8 +61,8 @@ protected:
private:
class Track;
FILE *mFile;
int mFd;
status_t mInitCheck;
bool mUse4ByteNalLength;
bool mUse32BitOffset;
bool mIsFileSizeLimitExplicitlyRequested;
@ -149,7 +149,7 @@ private:
off64_t addSample_l(MediaBuffer *buffer);
off64_t addLengthPrefixedSample_l(MediaBuffer *buffer);
inline size_t write(const void *ptr, size_t size, size_t nmemb, FILE* stream);
inline size_t write(const void *ptr, size_t size, size_t nmemb);
bool exceedsFileSizeLimit();
bool use32BitFileOffset() const;
bool exceedsFileDurationLimit();

View File

@ -864,7 +864,7 @@ status_t StagefrightRecorder::startAMRRecording() {
return UNKNOWN_ERROR;
}
mWriter = new AMRWriter(dup(mOutputFd));
mWriter = new AMRWriter(mOutputFd);
mWriter->addSource(audioEncoder);
if (mMaxFileDurationUs != 0) {
@ -912,7 +912,7 @@ status_t StagefrightRecorder::startRTPRecording() {
}
}
mWriter = new ARTPWriter(dup(mOutputFd));
mWriter = new ARTPWriter(mOutputFd);
mWriter->addSource(source);
mWriter->setListener(mListener);
@ -922,7 +922,7 @@ status_t StagefrightRecorder::startRTPRecording() {
status_t StagefrightRecorder::startMPEG2TSRecording() {
CHECK_EQ(mOutputFormat, OUTPUT_FORMAT_MPEG2TS);
sp<MediaWriter> writer = new MPEG2TSWriter(dup(mOutputFd));
sp<MediaWriter> writer = new MPEG2TSWriter(mOutputFd);
if (mAudioSource != AUDIO_SOURCE_LIST_END) {
if (mAudioEncoder != AUDIO_ENCODER_AAC) {
@ -1204,7 +1204,7 @@ status_t StagefrightRecorder::setupMPEG4Recording(
mediaWriter->clear();
*totalBitRate = 0;
status_t err = OK;
sp<MediaWriter> writer = new MPEG4Writer(dup(outputFd));
sp<MediaWriter> writer = new MPEG4Writer(outputFd);
// Add audio source first if it exists
if (!mCaptureTimeLapse && (mAudioSource != AUDIO_SOURCE_LIST_END)) {

View File

@ -24,22 +24,28 @@
#include <media/mediarecorder.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
namespace android {
AMRWriter::AMRWriter(const char *filename)
: mFile(fopen(filename, "wb")),
mFd(mFile == NULL? -1: fileno(mFile)),
mInitCheck(mFile != NULL ? OK : NO_INIT),
: mFd(-1),
mInitCheck(NO_INIT),
mStarted(false),
mPaused(false),
mResumed(false) {
mFd = open(filename, O_CREAT | O_LARGEFILE | O_TRUNC);
if (mFd >= 0) {
mInitCheck = OK;
}
}
AMRWriter::AMRWriter(int fd)
: mFile(fdopen(fd, "wb")),
mFd(mFile == NULL? -1: fileno(mFile)),
mInitCheck(mFile != NULL ? OK : NO_INIT),
: mFd(dup(fd)),
mInitCheck(mFd < 0? NO_INIT: OK),
mStarted(false),
mPaused(false),
mResumed(false) {
@ -50,9 +56,9 @@ AMRWriter::~AMRWriter() {
stop();
}
if (mFile != NULL) {
fclose(mFile);
mFile = NULL;
if (mFd != -1) {
close(mFd);
mFd = -1;
}
}
@ -92,7 +98,7 @@ status_t AMRWriter::addSource(const sp<MediaSource> &source) {
mSource = source;
const char *kHeader = isWide ? "#!AMR-WB\n" : "#!AMR\n";
size_t n = strlen(kHeader);
ssize_t n = strlen(kHeader);
if (write(mFd, kHeader, n) != n) {
return ERROR_IO;
}
@ -266,9 +272,8 @@ status_t AMRWriter::threadFunc() {
notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_COMPLETION_STATUS, UNKNOWN_ERROR);
}
fflush(mFile);
fclose(mFile);
mFile = NULL;
close(mFd);
mFd = -1;
mReachedEOS = true;
if (err == ERROR_END_OF_STREAM) {
return OK;

View File

@ -18,12 +18,14 @@
#include <media/stagefright/MediaDebug.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
namespace android {
FileSource::FileSource(const char *filename)
: mFile(fopen(filename, "rb")),
mFd(mFile == NULL ? -1 : fileno(mFile)),
: mFd(-1),
mOffset(0),
mLength(-1),
mDecryptHandle(NULL),
@ -31,11 +33,12 @@ FileSource::FileSource(const char *filename)
mDrmBufOffset(0),
mDrmBufSize(0),
mDrmBuf(NULL){
mFd = open(filename, O_LARGEFILE | O_RDONLY);
}
FileSource::FileSource(int fd, int64_t offset, int64_t length)
: mFile(fdopen(fd, "rb")),
mFd(fd),
: mFd(fd),
mOffset(offset),
mLength(length),
mDecryptHandle(NULL),
@ -48,9 +51,9 @@ FileSource::FileSource(int fd, int64_t offset, int64_t length)
}
FileSource::~FileSource() {
if (mFile != NULL) {
fclose(mFile);
mFile = NULL;
if (mFd >= 0) {
close(mFd);
mFd = -1;
}
if (mDrmBuf != NULL) {
@ -60,11 +63,11 @@ FileSource::~FileSource() {
}
status_t FileSource::initCheck() const {
return mFile != NULL ? OK : NO_INIT;
return mFd >= 0 ? OK : NO_INIT;
}
ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) {
if (mFile == NULL) {
if (mFd < 0) {
return NO_INIT;
}
@ -95,7 +98,7 @@ ssize_t FileSource::readAt(off64_t offset, void *data, size_t size) {
}
status_t FileSource::getSize(off64_t *size) {
if (mFile == NULL) {
if (mFd < 0) {
return NO_INIT;
}

View File

@ -410,7 +410,7 @@ void MPEG2TSWriter::SourceInfo::onMessageReceived(const sp<AMessage> &msg) {
////////////////////////////////////////////////////////////////////////////////
MPEG2TSWriter::MPEG2TSWriter(int fd)
: mFile(fdopen(fd, "wb")),
: mFile(fdopen(dup(fd), "wb")),
mStarted(false),
mNumSourcesDone(0),
mNumTSPacketsWritten(0),

View File

@ -33,6 +33,10 @@
#include <media/stagefright/MediaSource.h>
#include <media/stagefright/Utils.h>
#include <media/mediarecorder.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "include/ESDS.h"
@ -214,8 +218,8 @@ private:
};
MPEG4Writer::MPEG4Writer(const char *filename)
: mFile(fopen(filename, "wb")),
mFd(mFile == NULL? -1: fileno(mFile)),
: mFd(-1),
mInitCheck(NO_INIT),
mUse4ByteNalLength(true),
mUse32BitOffset(true),
mIsFileSizeLimitExplicitlyRequested(false),
@ -225,12 +229,16 @@ MPEG4Writer::MPEG4Writer(const char *filename)
mMdatOffset(0),
mEstimatedMoovBoxSize(0),
mInterleaveDurationUs(1000000) {
CHECK(mFile != NULL);
mFd = open(filename, O_CREAT | O_LARGEFILE | O_TRUNC);
if (mFd >= 0) {
mInitCheck = OK;
}
}
MPEG4Writer::MPEG4Writer(int fd)
: mFile(fdopen(fd, "wb")),
mFd(mFile == NULL? -1: fileno(mFile)),
: mFd(dup(fd)),
mInitCheck(mFd < 0? NO_INIT: OK),
mUse4ByteNalLength(true),
mUse32BitOffset(true),
mIsFileSizeLimitExplicitlyRequested(false),
@ -240,7 +248,6 @@ MPEG4Writer::MPEG4Writer(int fd)
mMdatOffset(0),
mEstimatedMoovBoxSize(0),
mInterleaveDurationUs(1000000) {
CHECK(mFile != NULL);
}
MPEG4Writer::~MPEG4Writer() {
@ -370,7 +377,7 @@ int64_t MPEG4Writer::estimateMoovBoxSize(int32_t bitRate) {
}
status_t MPEG4Writer::start(MetaData *param) {
if (mFile == NULL) {
if (mInitCheck != OK) {
return UNKNOWN_ERROR;
}
@ -493,7 +500,7 @@ bool MPEG4Writer::use32BitFileOffset() const {
}
status_t MPEG4Writer::pause() {
if (mFile == NULL) {
if (mInitCheck != OK) {
return OK;
}
mPaused = true;
@ -577,7 +584,7 @@ void MPEG4Writer::writeCompositionMatrix(int degrees) {
status_t MPEG4Writer::stop() {
if (mFile == NULL) {
if (mInitCheck != OK) {
return OK;
}
@ -600,9 +607,9 @@ status_t MPEG4Writer::stop() {
// Do not write out movie header on error.
if (err != OK) {
fflush(mFile);
fclose(mFile);
mFile = NULL;
close(mFd);
mFd = -1;
mInitCheck = NO_INIT;
mStarted = false;
return err;
}
@ -665,7 +672,7 @@ status_t MPEG4Writer::stop() {
// Moov box
lseek64(mFd, mFreeBoxOffset, SEEK_SET);
mOffset = mFreeBoxOffset;
write(mMoovBoxBuffer, 1, mMoovBoxBufferOffset, mFile);
write(mMoovBoxBuffer, 1, mMoovBoxBufferOffset);
// Free box
lseek64(mFd, mOffset, SEEK_SET);
@ -682,9 +689,9 @@ status_t MPEG4Writer::stop() {
CHECK(mBoxes.empty());
fflush(mFile);
fclose(mFile);
mFile = NULL;
close(mFd);
mFd = -1;
mInitCheck = NO_INIT;
mStarted = false;
return err;
}
@ -763,20 +770,21 @@ off64_t MPEG4Writer::addLengthPrefixedSample_l(MediaBuffer *buffer) {
}
size_t MPEG4Writer::write(
const void *ptr, size_t size, size_t nmemb, FILE *stream) {
const void *ptr, size_t size, size_t nmemb) {
const size_t bytes = size * nmemb;
int fd = fileno(stream);
if (mWriteMoovBoxToMemory) {
// This happens only when we write the moov box at the end of
// recording, not for each output video/audio frame we receive.
off64_t moovBoxSize = 8 + mMoovBoxBufferOffset + bytes;
if (moovBoxSize > mEstimatedMoovBoxSize) {
for (List<off64_t>::iterator it = mBoxes.begin();
it != mBoxes.end(); ++it) {
(*it) += mOffset;
}
lseek64(fd, mOffset, SEEK_SET);
::write(fd, mMoovBoxBuffer, mMoovBoxBufferOffset);
::write(fd, ptr, size * nmemb);
lseek64(mFd, mOffset, SEEK_SET);
::write(mFd, mMoovBoxBuffer, mMoovBoxBufferOffset);
::write(mFd, ptr, size * nmemb);
mOffset += (bytes + mMoovBoxBufferOffset);
free(mMoovBoxBuffer);
mMoovBoxBuffer = NULL;
@ -788,7 +796,7 @@ size_t MPEG4Writer::write(
mMoovBoxBufferOffset += bytes;
}
} else {
::write(fd, ptr, size * nmemb);
::write(mFd, ptr, size * nmemb);
mOffset += bytes;
}
return bytes;
@ -822,36 +830,36 @@ void MPEG4Writer::endBox() {
}
void MPEG4Writer::writeInt8(int8_t x) {
write(&x, 1, 1, mFile);
write(&x, 1, 1);
}
void MPEG4Writer::writeInt16(int16_t x) {
x = htons(x);
write(&x, 1, 2, mFile);
write(&x, 1, 2);
}
void MPEG4Writer::writeInt32(int32_t x) {
x = htonl(x);
write(&x, 1, 4, mFile);
write(&x, 1, 4);
}
void MPEG4Writer::writeInt64(int64_t x) {
x = hton64(x);
write(&x, 1, 8, mFile);
write(&x, 1, 8);
}
void MPEG4Writer::writeCString(const char *s) {
size_t n = strlen(s);
write(s, 1, n + 1, mFile);
write(s, 1, n + 1);
}
void MPEG4Writer::writeFourcc(const char *s) {
CHECK_EQ(strlen(s), 4);
write(s, 1, 4, mFile);
write(s, 1, 4);
}
void MPEG4Writer::write(const void *data, size_t size) {
write(data, 1, size, mFile);
write(data, 1, size);
}
bool MPEG4Writer::isFileStreamable() const {

View File

@ -46,7 +46,7 @@ static int UniformRand(int limit) {
ARTPWriter::ARTPWriter(int fd)
: mFlags(0),
mFd(fd),
mFd(dup(fd)),
mLooper(new ALooper),
mReflector(new AHandlerReflector<ARTPWriter>(this)) {
CHECK_GE(fd, 0);