Merge "Camera2: Add prepare for output streams"
diff --git a/include/media/mediametadataretriever.h b/include/media/mediametadataretriever.h
index b35cf32..7191965 100644
--- a/include/media/mediametadataretriever.h
+++ b/include/media/mediametadataretriever.h
@@ -57,6 +57,7 @@
METADATA_KEY_IS_DRM = 22,
METADATA_KEY_LOCATION = 23,
METADATA_KEY_VIDEO_ROTATION = 24,
+ METADATA_KEY_CAPTURE_FRAMERATE = 25,
// Add more here...
};
diff --git a/include/media/stagefright/MediaCodec.h b/include/media/stagefright/MediaCodec.h
index 3e3c276..d98fa1a 100644
--- a/include/media/stagefright/MediaCodec.h
+++ b/include/media/stagefright/MediaCodec.h
@@ -20,6 +20,7 @@
#include <gui/IGraphicBufferProducer.h>
#include <media/hardware/CryptoAPI.h>
+#include <media/MediaResource.h>
#include <media/stagefright/foundation/AHandler.h>
#include <utils/Vector.h>
@@ -34,6 +35,8 @@
struct ICrypto;
struct IMemory;
struct MemoryDealer;
+class IResourceManagerClient;
+class IResourceManagerService;
struct SoftwareRenderer;
struct Surface;
@@ -53,13 +56,7 @@
CB_OUTPUT_AVAILABLE = 2,
CB_ERROR = 3,
CB_OUTPUT_FORMAT_CHANGED = 4,
- CB_CODEC_RELEASED = 5,
- };
-
- // used by CB_CODEC_RELEASED to tell the upper layer the cause of the release.
- enum ReleaseReason {
- REASON_UNKNOWN = 0,
- REASON_RECLAIMED, // resources reclaimed by resource manager
+ CB_RESOURCE_RECLAIMED = 5,
};
struct BatteryNotifier;
@@ -230,6 +227,30 @@
bool mOwnedByClient;
};
+ struct ResourceManagerServiceProxy : public IBinder::DeathRecipient {
+ ResourceManagerServiceProxy();
+ ~ResourceManagerServiceProxy();
+
+ void init();
+
+ // implements DeathRecipient
+ virtual void binderDied(const wp<IBinder>& /*who*/);
+
+ void addResource(
+ int pid,
+ int64_t clientId,
+ const sp<IResourceManagerClient> client,
+ const Vector<MediaResource> &resources);
+
+ void removeResource(int64_t clientId);
+
+ bool reclaimResource(int callingPid, const Vector<MediaResource> &resources);
+
+ private:
+ Mutex mLock;
+ sp<IResourceManagerService> mService;
+ };
+
State mState;
sp<ALooper> mLooper;
sp<ALooper> mCodecLooper;
@@ -245,14 +266,22 @@
sp<AMessage> mCallback;
sp<MemoryDealer> mDealer;
+ sp<IResourceManagerClient> mResourceManagerClient;
+ sp<ResourceManagerServiceProxy> mResourceManagerService;
+
bool mBatteryStatNotified;
bool mIsVideo;
+ int32_t mVideoWidth;
+ int32_t mVideoHeight;
// initial create parameters
AString mInitName;
bool mInitNameIsType;
bool mInitIsEncoder;
+ // configure parameter
+ sp<AMessage> mConfigureMsg;
+
// Used only to synchronize asynchronous getBufferAndFormat
// across all the other (synchronous) buffer state change
// operations, such as de/queueIn/OutputBuffer, start and
@@ -320,6 +349,9 @@
void updateBatteryStat();
bool isExecuting() const;
+ uint64_t getGraphicBufferSize();
+ void addResource(const char *type, uint64_t value);
+
/* called to get the last codec error when the sticky flag is set.
* if no such codec error is found, returns UNKNOWN_ERROR.
*/
diff --git a/include/media/stagefright/MetaData.h b/include/media/stagefright/MetaData.h
index 087d016..8bdebf6 100644
--- a/include/media/stagefright/MetaData.h
+++ b/include/media/stagefright/MetaData.h
@@ -75,6 +75,8 @@
kKeyDecoderComponent = 'decC', // cstring
kKeyBufferID = 'bfID',
kKeyMaxInputSize = 'inpS',
+ kKeyMaxWidth = 'maxW',
+ kKeyMaxHeight = 'maxH',
kKeyThumbnailTime = 'thbT', // int64_t (usecs)
kKeyTrackID = 'trID',
kKeyIsDRM = 'idrm', // int32_t (bool)
@@ -98,6 +100,7 @@
kKeyCompilation = 'cpil', // cstring
kKeyLocation = 'loc ', // cstring
kKeyTimeScale = 'tmsl', // int32_t
+ kKeyCaptureFramerate = 'capF', // float (capture fps)
// video profile and level
kKeyVideoProfile = 'vprf', // int32_t
diff --git a/media/libstagefright/MPEG4Extractor.cpp b/media/libstagefright/MPEG4Extractor.cpp
index f7fa2b6..297a186 100644
--- a/media/libstagefright/MPEG4Extractor.cpp
+++ b/media/libstagefright/MPEG4Extractor.cpp
@@ -739,6 +739,16 @@
&& path[3] == FOURCC('i', 'l', 's', 't');
}
+static bool underQTMetaPath(const Vector<uint32_t> &path, int32_t depth) {
+ return path.size() >= 2
+ && path[0] == FOURCC('m', 'o', 'o', 'v')
+ && path[1] == FOURCC('m', 'e', 't', 'a')
+ && (depth == 2
+ || (depth == 3
+ && (path[2] == FOURCC('i', 'l', 's', 't')
+ || path[2] == FOURCC('k', 'e', 'y', 's'))));
+}
+
// Given a time in seconds since Jan 1 1904, produce a human-readable string.
static void convertTimeToDate(int64_t time_1904, String8 *s) {
time_t time_1970 = time_1904 - (((66 * 365 + 17) * 24) * 3600);
@@ -1732,31 +1742,35 @@
case FOURCC('m', 'e', 't', 'a'):
{
- uint8_t buffer[4];
- if (chunk_data_size < (off64_t)sizeof(buffer)) {
- *offset += chunk_size;
- return ERROR_MALFORMED;
- }
-
- if (mDataSource->readAt(
- data_offset, buffer, 4) < 4) {
- *offset += chunk_size;
- return ERROR_IO;
- }
-
- if (U32_AT(buffer) != 0) {
- // Should be version 0, flags 0.
-
- // If it's not, let's assume this is one of those
- // apparently malformed chunks that don't have flags
- // and completely different semantics than what's
- // in the MPEG4 specs and skip it.
- *offset += chunk_size;
- return OK;
- }
-
off64_t stop_offset = *offset + chunk_size;
- *offset = data_offset + sizeof(buffer);
+ *offset = data_offset;
+ bool isParsingMetaKeys = underQTMetaPath(mPath, 2);
+ if (!isParsingMetaKeys) {
+ uint8_t buffer[4];
+ if (chunk_data_size < (off64_t)sizeof(buffer)) {
+ *offset += chunk_size;
+ return ERROR_MALFORMED;
+ }
+
+ if (mDataSource->readAt(
+ data_offset, buffer, 4) < 4) {
+ *offset += chunk_size;
+ return ERROR_IO;
+ }
+
+ if (U32_AT(buffer) != 0) {
+ // Should be version 0, flags 0.
+
+ // If it's not, let's assume this is one of those
+ // apparently malformed chunks that don't have flags
+ // and completely different semantics than what's
+ // in the MPEG4 specs and skip it.
+ *offset += chunk_size;
+ return OK;
+ }
+ *offset += sizeof(buffer);
+ }
+
while (*offset < stop_offset) {
status_t err = parseChunk(offset, depth + 1);
if (err != OK) {
@@ -1920,6 +1934,16 @@
break;
}
+ case FOURCC('k', 'e', 'y', 's'):
+ {
+ *offset += chunk_size;
+
+ if (underQTMetaPath(mPath, 3)) {
+ parseQTMetaKey(data_offset, chunk_data_size);
+ }
+ break;
+ }
+
case FOURCC('t', 'r', 'e', 'x'):
{
*offset += chunk_size;
@@ -2050,6 +2074,12 @@
default:
{
+ // check if we're parsing 'ilst' for meta keys
+ // if so, treat type as a number (key-id).
+ if (underQTMetaPath(mPath, 3)) {
+ parseQTMetaVal(chunk_type, data_offset, chunk_data_size);
+ }
+
*offset += chunk_size;
break;
}
@@ -2180,7 +2210,108 @@
return OK;
}
+status_t MPEG4Extractor::parseQTMetaKey(off64_t offset, size_t size) {
+ if (size < 8) {
+ return ERROR_MALFORMED;
+ }
+ uint32_t count;
+ if (!mDataSource->getUInt32(offset + 4, &count)) {
+ return ERROR_MALFORMED;
+ }
+
+ if (mMetaKeyMap.size() > 0) {
+ ALOGW("'keys' atom seen again, discarding existing entries");
+ mMetaKeyMap.clear();
+ }
+
+ off64_t keyOffset = offset + 8;
+ off64_t stopOffset = offset + size;
+ for (size_t i = 1; i <= count; i++) {
+ if (keyOffset + 8 > stopOffset) {
+ return ERROR_MALFORMED;
+ }
+
+ uint32_t keySize;
+ if (!mDataSource->getUInt32(keyOffset, &keySize)
+ || keySize < 8
+ || keyOffset + keySize > stopOffset) {
+ return ERROR_MALFORMED;
+ }
+
+ uint32_t type;
+ if (!mDataSource->getUInt32(keyOffset + 4, &type)
+ || type != FOURCC('m', 'd', 't', 'a')) {
+ return ERROR_MALFORMED;
+ }
+
+ keySize -= 8;
+ keyOffset += 8;
+
+ sp<ABuffer> keyData = new ABuffer(keySize);
+ if (keyData->data() == NULL) {
+ return ERROR_MALFORMED;
+ }
+ if (mDataSource->readAt(
+ keyOffset, keyData->data(), keySize) < (ssize_t) keySize) {
+ return ERROR_MALFORMED;
+ }
+
+ AString key((const char *)keyData->data(), keySize);
+ mMetaKeyMap.add(i, key);
+
+ keyOffset += keySize;
+ }
+ return OK;
+}
+
+status_t MPEG4Extractor::parseQTMetaVal(
+ int32_t keyId, off64_t offset, size_t size) {
+ ssize_t index = mMetaKeyMap.indexOfKey(keyId);
+ if (index < 0) {
+ // corresponding key is not present, ignore
+ return ERROR_MALFORMED;
+ }
+
+ if (size <= 16) {
+ return ERROR_MALFORMED;
+ }
+ uint32_t dataSize;
+ if (!mDataSource->getUInt32(offset, &dataSize)
+ || dataSize > size || dataSize <= 16) {
+ return ERROR_MALFORMED;
+ }
+ uint32_t atomFourCC;
+ if (!mDataSource->getUInt32(offset + 4, &atomFourCC)
+ || atomFourCC != FOURCC('d', 'a', 't', 'a')) {
+ return ERROR_MALFORMED;
+ }
+ uint32_t dataType;
+ if (!mDataSource->getUInt32(offset + 8, &dataType)
+ || ((dataType & 0xff000000) != 0)) {
+ // not well-known type
+ return ERROR_MALFORMED;
+ }
+
+ dataSize -= 16;
+ offset += 16;
+
+ if (dataType == 23 && dataSize >= 4) {
+ // BE Float32
+ uint32_t val;
+ if (!mDataSource->getUInt32(offset, &val)) {
+ return ERROR_MALFORMED;
+ }
+ if (!strcasecmp(mMetaKeyMap[index].c_str(), "com.android.capture.fps")) {
+ mFileMetaData->setFloat(kKeyCaptureFramerate, *(float *)&val);
+ }
+ } else {
+ // add more keys if needed
+ ALOGV("ignoring key: type %d, size %d", dataType, dataSize);
+ }
+
+ return OK;
+}
status_t MPEG4Extractor::parseTrackHeader(
off64_t data_offset, off64_t data_size) {
diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp
index 5538cb0..40818f9 100644
--- a/media/libstagefright/MediaCodec.cpp
+++ b/media/libstagefright/MediaCodec.cpp
@@ -23,10 +23,12 @@
#include <binder/IBatteryStats.h>
#include <binder/IMemory.h>
+#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/MemoryDealer.h>
#include <gui/Surface.h>
#include <media/ICrypto.h>
+#include <media/IResourceManagerService.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AMessage.h>
@@ -47,6 +49,45 @@
namespace android {
+static inline int getCallingPid() {
+ return IPCThreadState::self()->getCallingPid();
+}
+
+static int64_t getId(sp<IResourceManagerClient> client) {
+ return (int64_t) client.get();
+}
+
+static bool isResourceError(status_t err) {
+ return (err == OMX_ErrorInsufficientResources);
+}
+
+static const int kMaxRetry = 2;
+
+struct ResourceManagerClient : public BnResourceManagerClient {
+ ResourceManagerClient(MediaCodec* codec) : mMediaCodec(codec) {}
+
+ virtual bool reclaimResource() {
+ sp<MediaCodec> codec = mMediaCodec.promote();
+ if (codec == NULL) {
+ // codec is already gone.
+ return true;
+ }
+ status_t err = codec->release();
+ if (err != OK) {
+ ALOGW("ResourceManagerClient failed to release codec with err %d", err);
+ }
+ return (err == OK);
+ }
+
+protected:
+ virtual ~ResourceManagerClient() {}
+
+private:
+ wp<MediaCodec> mMediaCodec;
+
+ DISALLOW_EVIL_CONSTRUCTORS(ResourceManagerClient);
+};
+
struct MediaCodec::BatteryNotifier : public Singleton<BatteryNotifier> {
BatteryNotifier();
virtual ~BatteryNotifier();
@@ -178,6 +219,65 @@
// started.
}
+MediaCodec::ResourceManagerServiceProxy::ResourceManagerServiceProxy() {
+}
+
+MediaCodec::ResourceManagerServiceProxy::~ResourceManagerServiceProxy() {
+ if (mService != NULL) {
+ IInterface::asBinder(mService)->unlinkToDeath(this);
+ }
+}
+
+void MediaCodec::ResourceManagerServiceProxy::init() {
+ sp<IServiceManager> sm = defaultServiceManager();
+ sp<IBinder> binder = sm->getService(String16("media.resource_manager"));
+ mService = interface_cast<IResourceManagerService>(binder);
+ if (mService == NULL) {
+ ALOGE("Failed to get ResourceManagerService");
+ return;
+ }
+ if (IInterface::asBinder(mService)->linkToDeath(this) != OK) {
+ mService.clear();
+ ALOGE("Failed to linkToDeath to ResourceManagerService.");
+ return;
+ }
+}
+
+void MediaCodec::ResourceManagerServiceProxy::binderDied(const wp<IBinder>& /*who*/) {
+ ALOGW("ResourceManagerService died.");
+ Mutex::Autolock _l(mLock);
+ mService.clear();
+}
+
+void MediaCodec::ResourceManagerServiceProxy::addResource(
+ int pid,
+ int64_t clientId,
+ const sp<IResourceManagerClient> client,
+ const Vector<MediaResource> &resources) {
+ Mutex::Autolock _l(mLock);
+ if (mService == NULL) {
+ return;
+ }
+ mService->addResource(pid, clientId, client, resources);
+}
+
+void MediaCodec::ResourceManagerServiceProxy::removeResource(int64_t clientId) {
+ Mutex::Autolock _l(mLock);
+ if (mService == NULL) {
+ return;
+ }
+ mService->removeResource(clientId);
+}
+
+bool MediaCodec::ResourceManagerServiceProxy::reclaimResource(
+ int callingPid, const Vector<MediaResource> &resources) {
+ Mutex::Autolock _l(mLock);
+ if (mService == NULL) {
+ return false;
+ }
+ return mService->reclaimResource(callingPid, resources);
+}
+
// static
sp<MediaCodec> MediaCodec::CreateByType(
const sp<ALooper> &looper, const char *mime, bool encoder, status_t *err) {
@@ -208,10 +308,14 @@
mCodec(NULL),
mReplyID(0),
mFlags(0),
+ mResourceManagerClient(new ResourceManagerClient(this)),
+ mResourceManagerService(new ResourceManagerServiceProxy()),
mStickyError(OK),
mSoftRenderer(NULL),
mBatteryStatNotified(false),
mIsVideo(false),
+ mVideoWidth(0),
+ mVideoHeight(0),
mDequeueInputTimeoutGeneration(0),
mDequeueInputReplyID(0),
mDequeueOutputTimeoutGeneration(0),
@@ -221,6 +325,7 @@
MediaCodec::~MediaCodec() {
CHECK_EQ(mState, UNINITIALIZED);
+ mResourceManagerService->removeResource(getId(mResourceManagerClient));
}
// static
@@ -247,6 +352,8 @@
}
status_t MediaCodec::init(const AString &name, bool nameIsType, bool encoder) {
+ mResourceManagerService->init();
+
// save init parameters for reset
mInitName = name;
mInitNameIsType = nameIsType;
@@ -266,12 +373,13 @@
return NAME_NOT_FOUND;
}
- bool needDedicatedLooper = false;
+ bool secureCodec = false;
if (nameIsType && !strncasecmp(name.c_str(), "video/", 6)) {
- needDedicatedLooper = true;
+ mIsVideo = true;
} else {
AString tmp = name;
if (tmp.endsWith(".secure")) {
+ secureCodec = true;
tmp.erase(tmp.size() - 7, 7);
}
const sp<IMediaCodecList> mcl = MediaCodecList::getInstance();
@@ -282,14 +390,15 @@
info->getSupportedMimes(&mimes);
for (size_t i = 0; i < mimes.size(); i++) {
if (mimes[i].startsWith("video/")) {
- needDedicatedLooper = true;
+ mIsVideo = true;
break;
}
}
}
}
- if (needDedicatedLooper) {
+ if (mIsVideo) {
+ // video codec needs dedicated looper
if (mCodecLooper == NULL) {
mCodecLooper = new ALooper;
mCodecLooper->setName("CodecLooper");
@@ -313,8 +422,25 @@
msg->setInt32("encoder", encoder);
}
- sp<AMessage> response;
- return PostAndAwaitResponse(msg, &response);
+ status_t err;
+ Vector<MediaResource> resources;
+ const char *type = secureCodec ? kResourceSecureCodec : kResourceNonSecureCodec;
+ resources.push_back(MediaResource(String8(type), 1));
+ for (int i = 0; i <= kMaxRetry; ++i) {
+ if (i > 0) {
+ // Don't try to reclaim resource for the first time.
+ if (!mResourceManagerService->reclaimResource(getCallingPid(), resources)) {
+ break;
+ }
+ }
+
+ sp<AMessage> response;
+ err = PostAndAwaitResponse(msg, &response);
+ if (!isResourceError(err)) {
+ break;
+ }
+ }
+ return err;
}
status_t MediaCodec::setCallback(const sp<AMessage> &callback) {
@@ -332,6 +458,11 @@
uint32_t flags) {
sp<AMessage> msg = new AMessage(kWhatConfigure, this);
+ if (mIsVideo) {
+ format->findInt32("width", &mVideoWidth);
+ format->findInt32("height", &mVideoHeight);
+ }
+
msg->setMessage("format", format);
msg->setInt32("flags", flags);
@@ -345,20 +476,41 @@
msg->setPointer("crypto", crypto.get());
}
- sp<AMessage> response;
- status_t err = PostAndAwaitResponse(msg, &response);
+ // save msg for reset
+ mConfigureMsg = msg;
- if (err != OK && err != INVALID_OPERATION) {
- // MediaCodec now set state to UNINITIALIZED upon any fatal error.
- // To maintain backward-compatibility, do a reset() to put codec
- // back into INITIALIZED state.
- // But don't reset if the err is INVALID_OPERATION, which means
- // the configure failure is due to wrong state.
+ status_t err;
+ Vector<MediaResource> resources;
+ const char *type = (mFlags & kFlagIsSecure) ?
+ kResourceSecureCodec : kResourceNonSecureCodec;
+ resources.push_back(MediaResource(String8(type), 1));
+ // Don't know the buffer size at this point, but it's fine to use 1 because
+ // the reclaimResource call doesn't consider the requester's buffer size for now.
+ resources.push_back(MediaResource(String8(kResourceGraphicMemory), 1));
+ for (int i = 0; i <= kMaxRetry; ++i) {
+ if (i > 0) {
+ // Don't try to reclaim resource for the first time.
+ if (!mResourceManagerService->reclaimResource(getCallingPid(), resources)) {
+ break;
+ }
+ }
- ALOGE("configure failed with err 0x%08x, resetting...", err);
- reset();
+ sp<AMessage> response;
+ err = PostAndAwaitResponse(msg, &response);
+ if (err != OK && err != INVALID_OPERATION) {
+ // MediaCodec now set state to UNINITIALIZED upon any fatal error.
+ // To maintain backward-compatibility, do a reset() to put codec
+ // back into INITIALIZED state.
+ // But don't reset if the err is INVALID_OPERATION, which means
+ // the configure failure is due to wrong state.
+
+ ALOGE("configure failed with err 0x%08x, resetting...", err);
+ reset();
+ }
+ if (!isResourceError(err)) {
+ break;
+ }
}
-
return err;
}
@@ -382,11 +534,65 @@
return err;
}
+uint64_t MediaCodec::getGraphicBufferSize() {
+ if (!mIsVideo) {
+ return 0;
+ }
+
+ uint64_t size = 0;
+ size_t portNum = sizeof(mPortBuffers) / sizeof((mPortBuffers)[0]);
+ for (size_t i = 0; i < portNum; ++i) {
+ // TODO: this is just an estimation, we should get the real buffer size from ACodec.
+ size += mPortBuffers[i].size() * mVideoWidth * mVideoHeight * 3 / 2;
+ }
+ return size;
+}
+
+void MediaCodec::addResource(const char *type, uint64_t value) {
+ Vector<MediaResource> resources;
+ resources.push_back(MediaResource(String8(type), value));
+ mResourceManagerService->addResource(
+ getCallingPid(), getId(mResourceManagerClient), mResourceManagerClient, resources);
+}
+
status_t MediaCodec::start() {
sp<AMessage> msg = new AMessage(kWhatStart, this);
- sp<AMessage> response;
- return PostAndAwaitResponse(msg, &response);
+ status_t err;
+ Vector<MediaResource> resources;
+ const char *type = (mFlags & kFlagIsSecure) ?
+ kResourceSecureCodec : kResourceNonSecureCodec;
+ resources.push_back(MediaResource(String8(type), 1));
+ // Don't know the buffer size at this point, but it's fine to use 1 because
+ // the reclaimResource call doesn't consider the requester's buffer size for now.
+ resources.push_back(MediaResource(String8(kResourceGraphicMemory), 1));
+ for (int i = 0; i <= kMaxRetry; ++i) {
+ if (i > 0) {
+ // Don't try to reclaim resource for the first time.
+ if (!mResourceManagerService->reclaimResource(getCallingPid(), resources)) {
+ break;
+ }
+ // Recover codec from previous error before retry start.
+ err = reset();
+ if (err != OK) {
+ ALOGE("retrying start: failed to reset codec");
+ break;
+ }
+ sp<AMessage> response;
+ err = PostAndAwaitResponse(mConfigureMsg, &response);
+ if (err != OK) {
+ ALOGE("retrying start: failed to configure codec");
+ break;
+ }
+ }
+
+ sp<AMessage> response;
+ err = PostAndAwaitResponse(msg, &response);
+ if (!isResourceError(err)) {
+ break;
+ }
+ }
+ return err;
}
status_t MediaCodec::stop() {
@@ -960,11 +1166,15 @@
mFlags &= ~kFlagUsesSoftwareRenderer;
}
+ String8 resourceType;
if (mComponentName.endsWith(".secure")) {
mFlags |= kFlagIsSecure;
+ resourceType = String8(kResourceSecureCodec);
} else {
mFlags &= ~kFlagIsSecure;
+ resourceType = String8(kResourceNonSecureCodec);
}
+ addResource(resourceType, 1);
(new AMessage)->postReply(mReplyID);
break;
@@ -1077,6 +1287,9 @@
// We're always allocating output buffers after
// allocating input buffers, so this is a good
// indication that now all buffers are allocated.
+ if (mIsVideo) {
+ addResource(kResourceGraphicMemory, getGraphicBufferSize());
+ }
setState(STARTED);
(new AMessage)->postReply(mReplyID);
} else {
@@ -1256,6 +1469,8 @@
}
mFlags &= ~kFlagIsComponentAllocated;
+ mResourceManagerService->removeResource(getId(mResourceManagerClient));
+
(new AMessage)->postReply(mReplyID);
break;
}
@@ -2357,12 +2572,6 @@
void MediaCodec::updateBatteryStat() {
if (mState == CONFIGURED && !mBatteryStatNotified) {
- AString mime;
- CHECK(mOutputFormat != NULL &&
- mOutputFormat->findString("mime", &mime));
-
- mIsVideo = mime.startsWithIgnoreCase("video/");
-
BatteryNotifier& notifier(BatteryNotifier::getInstance());
if (mIsVideo) {
diff --git a/media/libstagefright/StagefrightMetadataRetriever.cpp b/media/libstagefright/StagefrightMetadataRetriever.cpp
index 101fc8a..820b2fc 100644
--- a/media/libstagefright/StagefrightMetadataRetriever.cpp
+++ b/media/libstagefright/StagefrightMetadataRetriever.cpp
@@ -519,6 +519,12 @@
mMetaData.add(METADATA_KEY_NUM_TRACKS, String8(tmp));
+ float captureFps;
+ if (meta->findFloat(kKeyCaptureFramerate, &captureFps)) {
+ sprintf(tmp, "%f", captureFps);
+ mMetaData.add(METADATA_KEY_CAPTURE_FRAMERATE, String8(tmp));
+ }
+
bool hasAudio = false;
bool hasVideo = false;
int32_t videoWidth = -1;
diff --git a/media/libstagefright/Utils.cpp b/media/libstagefright/Utils.cpp
index 8506e37..dfe8ad1 100644
--- a/media/libstagefright/Utils.cpp
+++ b/media/libstagefright/Utils.cpp
@@ -166,6 +166,16 @@
msg->setInt32("max-input-size", maxInputSize);
}
+ int32_t maxWidth;
+ if (meta->findInt32(kKeyMaxWidth, &maxWidth)) {
+ msg->setInt32("max-width", maxWidth);
+ }
+
+ int32_t maxHeight;
+ if (meta->findInt32(kKeyMaxHeight, &maxHeight)) {
+ msg->setInt32("max-height", maxHeight);
+ }
+
int32_t rotationDegrees;
if (meta->findInt32(kKeyRotation, &rotationDegrees)) {
msg->setInt32("rotation-degrees", rotationDegrees);
@@ -568,6 +578,16 @@
meta->setInt32(kKeyMaxInputSize, maxInputSize);
}
+ int32_t maxWidth;
+ if (msg->findInt32("max-width", &maxWidth)) {
+ meta->setInt32(kKeyMaxWidth, maxWidth);
+ }
+
+ int32_t maxHeight;
+ if (msg->findInt32("max-height", &maxHeight)) {
+ meta->setInt32(kKeyMaxHeight, maxHeight);
+ }
+
// reassemble the csd data into its original form
sp<ABuffer> csd0;
if (msg->findBuffer("csd-0", &csd0)) {
diff --git a/media/libstagefright/httplive/LiveSession.cpp b/media/libstagefright/httplive/LiveSession.cpp
index 74f58e9..4886000 100644
--- a/media/libstagefright/httplive/LiveSession.cpp
+++ b/media/libstagefright/httplive/LiveSession.cpp
@@ -171,6 +171,8 @@
mOrigBandwidthIndex(-1),
mLastBandwidthBps(-1ll),
mBandwidthEstimator(new BandwidthEstimator()),
+ mMaxWidth(720),
+ mMaxHeight(480),
mStreamMask(0),
mNewStreamMask(0),
mSwapMask(0),
@@ -345,6 +347,9 @@
if (stream == STREAMTYPE_AUDIO) {
// set AAC input buffer size to 32K bytes (256kbps x 1sec)
meta->setInt32(kKeyMaxInputSize, 32 * 1024);
+ } else if (stream == STREAMTYPE_VIDEO) {
+ meta->setInt32(kKeyMaxWidth, mMaxWidth);
+ meta->setInt32(kKeyMaxHeight, mMaxHeight);
}
return convertMetaDataToMessage(meta, format);
@@ -847,6 +852,9 @@
size_t initialBandwidth = 0;
size_t initialBandwidthIndex = 0;
+ int32_t maxWidth = 0;
+ int32_t maxHeight = 0;
+
if (mPlaylist->isVariantPlaylist()) {
Vector<BandwidthItem> itemsWithVideo;
for (size_t i = 0; i < mPlaylist->size(); ++i) {
@@ -860,6 +868,14 @@
CHECK(meta->findInt32("bandwidth", (int32_t *)&item.mBandwidth));
+ int32_t width, height;
+ if (meta->findInt32("width", &width)) {
+ maxWidth = max(maxWidth, width);
+ }
+ if (meta->findInt32("height", &height)) {
+ maxHeight = max(maxHeight, height);
+ }
+
mBandwidthItems.push(item);
if (mPlaylist->hasType(i, "video")) {
itemsWithVideo.push(item);
@@ -893,6 +909,9 @@
mBandwidthItems.push(item);
}
+ mMaxWidth = maxWidth > 0 ? maxWidth : mMaxWidth;
+ mMaxHeight = maxHeight > 0 ? maxHeight : mMaxHeight;
+
mPlaylist->pickRandomMediaItems();
changeConfiguration(
0ll /* timeUs */, initialBandwidthIndex, false /* pickTrack */);
diff --git a/media/libstagefright/httplive/LiveSession.h b/media/libstagefright/httplive/LiveSession.h
index 9117bb1..ed74bc2 100644
--- a/media/libstagefright/httplive/LiveSession.h
+++ b/media/libstagefright/httplive/LiveSession.h
@@ -191,6 +191,8 @@
sp<BandwidthEstimator> mBandwidthEstimator;
sp<M3UParser> mPlaylist;
+ int32_t mMaxWidth;
+ int32_t mMaxHeight;
sp<ALooper> mFetcherLooper;
KeyedVector<AString, FetcherInfo> mFetcherInfos;
diff --git a/media/libstagefright/httplive/M3UParser.cpp b/media/libstagefright/httplive/M3UParser.cpp
index 7bb7f2c..ef9145c 100644
--- a/media/libstagefright/httplive/M3UParser.cpp
+++ b/media/libstagefright/httplive/M3UParser.cpp
@@ -808,6 +808,29 @@
*meta = new AMessage;
}
(*meta)->setString(key.c_str(), codecs.c_str());
+ } else if (!strcasecmp("resolution", key.c_str())) {
+ const char *s = val.c_str();
+ char *end;
+ unsigned long width = strtoul(s, &end, 10);
+
+ if (end == s || *end != 'x') {
+ // malformed
+ continue;
+ }
+
+ s = end + 1;
+ unsigned long height = strtoul(s, &end, 10);
+
+ if (end == s || *end != '\0') {
+ // malformed
+ continue;
+ }
+
+ if (meta->get() == NULL) {
+ *meta = new AMessage;
+ }
+ (*meta)->setInt32("width", width);
+ (*meta)->setInt32("height", height);
} else if (!strcasecmp("audio", key.c_str())
|| !strcasecmp("video", key.c_str())
|| !strcasecmp("subtitles", key.c_str())) {
diff --git a/media/libstagefright/include/MPEG4Extractor.h b/media/libstagefright/include/MPEG4Extractor.h
index 8c16251..3067c3d 100644
--- a/media/libstagefright/include/MPEG4Extractor.h
+++ b/media/libstagefright/include/MPEG4Extractor.h
@@ -104,11 +104,15 @@
String8 mLastCommentName;
String8 mLastCommentData;
+ KeyedVector<uint32_t, AString> mMetaKeyMap;
+
status_t readMetaData();
status_t parseChunk(off64_t *offset, int depth);
status_t parseITunesMetaData(off64_t offset, size_t size);
status_t parse3GPPMetaData(off64_t offset, size_t size, int depth);
void parseID3v2MetaData(off64_t offset);
+ status_t parseQTMetaKey(off64_t data_offset, size_t data_size);
+ status_t parseQTMetaVal(int32_t keyId, off64_t data_offset, size_t data_size);
status_t updateAudioTrackInfoFromESDS_MPEG4Audio(
const void *esds_data, size_t esds_size);
diff --git a/media/mediaserver/main_mediaserver.cpp b/media/mediaserver/main_mediaserver.cpp
index 99572f8..06b3c6e 100644
--- a/media/mediaserver/main_mediaserver.cpp
+++ b/media/mediaserver/main_mediaserver.cpp
@@ -33,6 +33,7 @@
#include "CameraService.h"
#include "MediaLogService.h"
#include "MediaPlayerService.h"
+#include "ResourceManagerService.h"
#include "service/AudioPolicyService.h"
#include "SoundTriggerHwService.h"
#include "RadioService.h"
@@ -128,6 +129,7 @@
ALOGI("ServiceManager: %p", sm.get());
AudioFlinger::instantiate();
MediaPlayerService::instantiate();
+ ResourceManagerService::instantiate();
CameraService::instantiate();
AudioPolicyService::instantiate();
SoundTriggerHwService::instantiate();
diff --git a/services/mediaresourcemanager/ResourceManagerService.cpp b/services/mediaresourcemanager/ResourceManagerService.cpp
index 7296d47..75a69ed 100644
--- a/services/mediaresourcemanager/ResourceManagerService.cpp
+++ b/services/mediaresourcemanager/ResourceManagerService.cpp
@@ -126,6 +126,7 @@
Mutex::Autolock lock(mLock);
ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
ResourceInfo& info = getResourceInfoForEdit(clientId, client, infos);
+ // TODO: do the merge instead of append.
info.resources.appendVector(resources);
}
@@ -197,19 +198,58 @@
}
}
}
+
+ if (clients.size() == 0) {
+ // if we are here, run the third pass to free one codec with the same type.
+ for (size_t i = 0; i < resources.size(); ++i) {
+ String8 type = resources[i].mType;
+ if (type == kResourceSecureCodec || type == kResourceNonSecureCodec) {
+ sp<IResourceManagerClient> client;
+ if (!getLowestPriorityBiggestClient_l(callingPid, type, &client)) {
+ return false;
+ }
+ clients.push_back(client);
+ }
+ }
+ }
}
if (clients.size() == 0) {
return false;
}
+ sp<IResourceManagerClient> failedClient;
for (size_t i = 0; i < clients.size(); ++i) {
ALOGV("reclaimResource from client %p", clients[i].get());
if (!clients[i]->reclaimResource()) {
- return false;
+ failedClient = clients[i];
+ break;
}
}
- return true;
+
+ {
+ Mutex::Autolock lock(mLock);
+ bool found = false;
+ for (size_t i = 0; i < mMap.size(); ++i) {
+ ResourceInfos &infos = mMap.editValueAt(i);
+ for (size_t j = 0; j < infos.size();) {
+ if (infos[j].client == failedClient) {
+ j = infos.removeAt(j);
+ found = true;
+ } else {
+ ++j;
+ }
+ }
+ if (found) {
+ break;
+ }
+ }
+ if (!found) {
+ ALOGV("didn't find failed client");
+ }
+ }
+
+ return (failedClient == NULL);
}
bool ResourceManagerService::getAllClients_l(
diff --git a/services/mediaresourcemanager/test/ResourceManagerService_test.cpp b/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
index b73e1bc..48d1395 100644
--- a/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
+++ b/services/mediaresourcemanager/test/ResourceManagerService_test.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -118,6 +118,20 @@
client3->reset();
}
+ // test set up
+ // ---------------------------------------------------------------------------------
+ // pid priority client type number
+ // ---------------------------------------------------------------------------------
+ // kTestPid1(30) 30 mTestClient1 secure codec 1
+ // graphic memory 200
+ // graphic memory 200
+ // ---------------------------------------------------------------------------------
+ // kTestPid2(20) 20 mTestClient2 non-secure codec 1
+ // graphic memory 300
+ // -------------------------------------------
+ // mTestClient3 secure codec 1
+ // graphic memory 100
+ // ---------------------------------------------------------------------------------
void addResource() {
// kTestPid1 mTestClient1
Vector<MediaResource> resources1;
@@ -202,10 +216,12 @@
int lowPriorityPid = 100;
EXPECT_FALSE(mService->getAllClients_l(lowPriorityPid, type, &clients));
int midPriorityPid = 25;
- EXPECT_FALSE(mService->getAllClients_l(lowPriorityPid, type, &clients));
+ // some higher priority process (e.g. kTestPid2) owns the resource, so getAllClients_l
+ // will fail.
+ EXPECT_FALSE(mService->getAllClients_l(midPriorityPid, type, &clients));
int highPriorityPid = 10;
- EXPECT_TRUE(mService->getAllClients_l(10, unknowType, &clients));
- EXPECT_TRUE(mService->getAllClients_l(10, type, &clients));
+ EXPECT_TRUE(mService->getAllClients_l(highPriorityPid, unknowType, &clients));
+ EXPECT_TRUE(mService->getAllClients_l(highPriorityPid, type, &clients));
EXPECT_EQ(2u, clients.size());
EXPECT_EQ(mTestClient3, clients[0]);
@@ -308,6 +324,30 @@
// nothing left
EXPECT_FALSE(mService->reclaimResource(10, resources));
}
+
+ // ### secure codecs can coexist and secure codec can coexist with non-secure codec ###
+ {
+ addResource();
+ mService->mSupportsMultipleSecureCodecs = true;
+ mService->mSupportsSecureWithNonSecureCodec = true;
+
+ Vector<MediaResource> resources;
+ resources.push_back(MediaResource(String8(kResourceSecureCodec), 1));
+
+ EXPECT_TRUE(mService->reclaimResource(10, resources));
+ // secure codec from lowest process got reclaimed
+ verifyClients(true, false, false);
+
+ // call again should reclaim another secure codec from lowest process
+ EXPECT_TRUE(mService->reclaimResource(10, resources));
+ verifyClients(false, false, true);
+
+ // nothing left
+ EXPECT_FALSE(mService->reclaimResource(10, resources));
+
+ // clean up client 2 which still has non secure codec left
+ mService->removeResource((int64_t) mTestClient2.get());
+ }
}
void testReclaimResourceNonSecure() {
@@ -360,6 +400,26 @@
// nothing left
EXPECT_FALSE(mService->reclaimResource(10, resources));
}
+
+ // ### secure codec can coexist with non-secure codec ###
+ {
+ addResource();
+ mService->mSupportsSecureWithNonSecureCodec = true;
+
+ Vector<MediaResource> resources;
+ resources.push_back(MediaResource(String8(kResourceNonSecureCodec), 1));
+
+ EXPECT_TRUE(mService->reclaimResource(10, resources));
+ // one non secure codec from lowest process got reclaimed
+ verifyClients(false, true, false);
+
+ // nothing left
+ EXPECT_FALSE(mService->reclaimResource(10, resources));
+
+ // clean up client 1 and 3 which still have secure codec left
+ mService->removeResource((int64_t) mTestClient1.get());
+ mService->removeResource((int64_t) mTestClient3.get());
+ }
}
void testGetLowestPriorityBiggestClient() {