summaryrefslogtreecommitdiffstats
path: root/media/libstagefright
diff options
context:
space:
mode:
authorJames Dong <jdong@google.com>2010-05-11 14:57:02 -0700
committerJames Dong <jdong@google.com>2010-05-14 10:46:56 -0700
commit1824486e044f4f09640fbd7bef74a20e4efb35ae (patch)
tree88f56b045e6ec380d4353797ab0fa758425fa53f /media/libstagefright
parent21a534d955c8d45c50dafaf05cf2afbb8cf3696c (diff)
downloadframeworks_base-1824486e044f4f09640fbd7bef74a20e4efb35ae.zip
frameworks_base-1824486e044f4f09640fbd7bef74a20e4efb35ae.tar.gz
frameworks_base-1824486e044f4f09640fbd7bef74a20e4efb35ae.tar.bz2
Handle recording file size and/or duration limit
Change-Id: Ib9ed1f3ebd8fef550cc130a7ef11f2905fa9aedc
Diffstat (limited to 'media/libstagefright')
-rw-r--r--media/libstagefright/AMRWriter.cpp36
-rw-r--r--media/libstagefright/MPEG4Writer.cpp52
2 files changed, 88 insertions, 0 deletions
diff --git a/media/libstagefright/AMRWriter.cpp b/media/libstagefright/AMRWriter.cpp
index 73ea56d..0d54235 100644
--- a/media/libstagefright/AMRWriter.cpp
+++ b/media/libstagefright/AMRWriter.cpp
@@ -22,6 +22,7 @@
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MediaSource.h>
#include <media/stagefright/MetaData.h>
+#include <media/mediarecorder.h>
namespace android {
@@ -137,6 +138,20 @@ void AMRWriter::stop() {
mStarted = false;
}
+bool AMRWriter::exceedsFileSizeLimit() {
+ if (mMaxFileSizeLimitBytes == 0) {
+ return false;
+ }
+ return mEstimatedSizeBytes >= mMaxFileSizeLimitBytes;
+}
+
+bool AMRWriter::exceedsFileDurationLimit() {
+ if (mMaxFileDurationLimitUs == 0) {
+ return false;
+ }
+ return mEstimatedDurationUs >= mMaxFileDurationLimitUs;
+}
+
// static
void *AMRWriter::ThreadWrapper(void *me) {
static_cast<AMRWriter *>(me)->threadFunc();
@@ -145,6 +160,8 @@ void *AMRWriter::ThreadWrapper(void *me) {
}
void AMRWriter::threadFunc() {
+ mEstimatedDurationUs = 0;
+ mEstimatedSizeBytes = 0;
while (!mDone) {
MediaBuffer *buffer;
status_t err = mSource->read(&buffer);
@@ -153,6 +170,25 @@ void AMRWriter::threadFunc() {
break;
}
+ mEstimatedSizeBytes += buffer->range_length();
+ if (exceedsFileSizeLimit()) {
+ buffer->release();
+ buffer = NULL;
+ notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
+ break;
+ }
+
+ int64_t timestampUs;
+ CHECK(buffer->meta_data()->findInt64(kKeyTime, &timestampUs));
+ if (timestampUs > mEstimatedDurationUs) {
+ mEstimatedDurationUs = timestampUs;
+ }
+ if (exceedsFileDurationLimit()) {
+ buffer->release();
+ buffer = NULL;
+ notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
+ break;
+ }
ssize_t n = fwrite(
(const uint8_t *)buffer->data() + buffer->range_offset(),
1,
diff --git a/media/libstagefright/MPEG4Writer.cpp b/media/libstagefright/MPEG4Writer.cpp
index 19cccf7..044460c 100644
--- a/media/libstagefright/MPEG4Writer.cpp
+++ b/media/libstagefright/MPEG4Writer.cpp
@@ -31,6 +31,7 @@
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MediaSource.h>
#include <media/stagefright/Utils.h>
+#include <media/mediarecorder.h>
namespace android {
@@ -44,6 +45,7 @@ public:
bool reachedEOS();
int64_t getDurationUs() const;
+ int64_t getEstimatedTrackSizeBytes() const;
void writeTrackHeader(int32_t trackID);
private:
@@ -52,6 +54,7 @@ private:
sp<MediaSource> mSource;
volatile bool mDone;
int64_t mMaxTimeStampUs;
+ int64_t mEstimatedTrackSizeBytes;
pthread_t mThread;
@@ -455,6 +458,35 @@ void MPEG4Writer::write(const void *data, size_t size) {
write(data, 1, size, mFile);
}
+bool MPEG4Writer::exceedsFileSizeLimit() {
+ // No limit
+ if (mMaxFileSizeLimitBytes == 0) {
+ return false;
+ }
+
+ int64_t nTotalBytesEstimate = mEstimatedMoovBoxSize;
+ for (List<Track *>::iterator it = mTracks.begin();
+ it != mTracks.end(); ++it) {
+ nTotalBytesEstimate += (*it)->getEstimatedTrackSizeBytes();
+ }
+ return (nTotalBytesEstimate >= mMaxFileSizeLimitBytes);
+}
+
+bool MPEG4Writer::exceedsFileDurationLimit() {
+ // No limit
+ if (mMaxFileDurationLimitUs == 0) {
+ return false;
+ }
+
+ for (List<Track *>::iterator it = mTracks.begin();
+ it != mTracks.end(); ++it) {
+ if ((*it)->getDurationUs() >= mMaxFileDurationLimitUs) {
+ return true;
+ }
+ }
+ return false;
+}
+
bool MPEG4Writer::reachedEOS() {
bool allDone = true;
for (List<Track *>::iterator it = mTracks.begin();
@@ -656,6 +688,7 @@ void MPEG4Writer::Track::threadEntry() {
int32_t sampleCount = 1; // Sample count in the current stts table entry
uint32_t previousSampleSize = 0; // Size of the previous sample
+ mEstimatedTrackSizeBytes = 0;
MediaBuffer *buffer;
while (!mDone && mSource->read(&buffer) == OK) {
if (buffer->range_length() == 0) {
@@ -784,6 +817,21 @@ void MPEG4Writer::Track::threadEntry() {
#endif
: buffer->range_length();
+ // Max file size or duration handling
+ mEstimatedTrackSizeBytes += info.size;
+ if (mOwner->exceedsFileSizeLimit()) {
+ buffer->release();
+ buffer = NULL;
+ mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED, 0);
+ break;
+ }
+ if (mOwner->exceedsFileDurationLimit()) {
+ buffer->release();
+ buffer = NULL;
+ mOwner->notify(MEDIA_RECORDER_EVENT_INFO, MEDIA_RECORDER_INFO_MAX_DURATION_REACHED, 0);
+ break;
+ }
+
bool is_audio = !strncasecmp(mime, "audio/", 6);
int64_t timestampUs;
@@ -904,6 +952,10 @@ int64_t MPEG4Writer::Track::getDurationUs() const {
return mMaxTimeStampUs;
}
+int64_t MPEG4Writer::Track::getEstimatedTrackSizeBytes() const {
+ return mEstimatedTrackSizeBytes;
+}
+
void MPEG4Writer::Track::writeTrackHeader(int32_t trackID) {
const char *mime;
bool success = mMeta->findCString(kKeyMIMEType, &mime);