Merge "media: fix encrypted buffer handling" into rvc-dev
This commit is contained in:
@@ -43,7 +43,7 @@
|
|||||||
|
|
||||||
#include <android_runtime/android_hardware_HardwareBuffer.h>
|
#include <android_runtime/android_hardware_HardwareBuffer.h>
|
||||||
|
|
||||||
#include <binder/MemoryHeapBase.h>
|
#include <binder/MemoryDealer.h>
|
||||||
|
|
||||||
#include <cutils/compiler.h>
|
#include <cutils/compiler.h>
|
||||||
|
|
||||||
@@ -306,6 +306,7 @@ status_t JMediaCodec::configure(
|
|||||||
CHECK(format->findString("mime", &mime));
|
CHECK(format->findString("mime", &mime));
|
||||||
mGraphicOutput = (mime.startsWithIgnoreCase("video/") || mime.startsWithIgnoreCase("image/"))
|
mGraphicOutput = (mime.startsWithIgnoreCase("video/") || mime.startsWithIgnoreCase("image/"))
|
||||||
&& !(flags & CONFIGURE_FLAG_ENCODE);
|
&& !(flags & CONFIGURE_FLAG_ENCODE);
|
||||||
|
mHasCryptoOrDescrambler = (crypto != nullptr) || (descrambler != nullptr);
|
||||||
|
|
||||||
return mCodec->configure(
|
return mCodec->configure(
|
||||||
format, mSurfaceTextureClient, crypto, descrambler, flags);
|
format, mSurfaceTextureClient, crypto, descrambler, flags);
|
||||||
@@ -1603,14 +1604,13 @@ struct NativeCryptoInfo {
|
|||||||
ScopedLocalRef<jobject> patternObj{
|
ScopedLocalRef<jobject> patternObj{
|
||||||
env, env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoPatternID)};
|
env, env->GetObjectField(cryptoInfoObj, gFields.cryptoInfoPatternID)};
|
||||||
|
|
||||||
CryptoPlugin::Pattern pattern;
|
|
||||||
if (patternObj.get() == nullptr) {
|
if (patternObj.get() == nullptr) {
|
||||||
pattern.mEncryptBlocks = 0;
|
mPattern.mEncryptBlocks = 0;
|
||||||
pattern.mSkipBlocks = 0;
|
mPattern.mSkipBlocks = 0;
|
||||||
} else {
|
} else {
|
||||||
pattern.mEncryptBlocks = env->GetIntField(
|
mPattern.mEncryptBlocks = env->GetIntField(
|
||||||
patternObj.get(), gFields.patternEncryptBlocksID);
|
patternObj.get(), gFields.patternEncryptBlocksID);
|
||||||
pattern.mSkipBlocks = env->GetIntField(
|
mPattern.mSkipBlocks = env->GetIntField(
|
||||||
patternObj.get(), gFields.patternSkipBlocksID);
|
patternObj.get(), gFields.patternSkipBlocksID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1679,6 +1679,18 @@ struct NativeCryptoInfo {
|
|||||||
mIv = env->GetByteArrayElements(mIvObj.get(), nullptr);
|
mIv = env->GetByteArrayElements(mIvObj.get(), nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit NativeCryptoInfo(jint size)
|
||||||
|
: mIvObj{nullptr, nullptr},
|
||||||
|
mKeyObj{nullptr, nullptr},
|
||||||
|
mMode{CryptoPlugin::kMode_Unencrypted},
|
||||||
|
mPattern{0, 0} {
|
||||||
|
mSubSamples = new CryptoPlugin::SubSample[1];
|
||||||
|
mNumSubSamples = 1;
|
||||||
|
mSubSamples[0].mNumBytesOfClearData = size;
|
||||||
|
mSubSamples[0].mNumBytesOfEncryptedData = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
~NativeCryptoInfo() {
|
~NativeCryptoInfo() {
|
||||||
@@ -2128,10 +2140,13 @@ static void android_media_MediaCodec_native_queueLinearBlock(
|
|||||||
if (env->GetBooleanField(bufferObj, gLinearBlockInfo.validId)) {
|
if (env->GetBooleanField(bufferObj, gLinearBlockInfo.validId)) {
|
||||||
JMediaCodecLinearBlock *context =
|
JMediaCodecLinearBlock *context =
|
||||||
(JMediaCodecLinearBlock *)env->GetLongField(bufferObj, gLinearBlockInfo.contextId);
|
(JMediaCodecLinearBlock *)env->GetLongField(bufferObj, gLinearBlockInfo.contextId);
|
||||||
if (cryptoInfoObj != nullptr) {
|
if (codec->hasCryptoOrDescrambler()) {
|
||||||
memory = context->toHidlMemory();
|
memory = context->toHidlMemory();
|
||||||
|
// TODO: copy if memory is null
|
||||||
|
offset += context->mHidlMemoryOffset;
|
||||||
} else {
|
} else {
|
||||||
buffer = context->toC2Buffer(offset, size);
|
buffer = context->toC2Buffer(offset, size);
|
||||||
|
// TODO: copy if buffer is null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
env->MonitorExit(lock.get());
|
env->MonitorExit(lock.get());
|
||||||
@@ -2141,13 +2156,19 @@ static void android_media_MediaCodec_native_queueLinearBlock(
|
|||||||
}
|
}
|
||||||
|
|
||||||
AString errorDetailMsg;
|
AString errorDetailMsg;
|
||||||
if (cryptoInfoObj != nullptr) {
|
if (codec->hasCryptoOrDescrambler()) {
|
||||||
if (!memory) {
|
if (!memory) {
|
||||||
|
ALOGI("queueLinearBlock: no ashmem memory for encrypted content");
|
||||||
throwExceptionAsNecessary(env, BAD_VALUE);
|
throwExceptionAsNecessary(env, BAD_VALUE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
NativeCryptoInfo cryptoInfo = [env, cryptoInfoObj, size]{
|
||||||
NativeCryptoInfo cryptoInfo{env, cryptoInfoObj};
|
if (cryptoInfoObj == nullptr) {
|
||||||
|
return NativeCryptoInfo{size};
|
||||||
|
} else {
|
||||||
|
return NativeCryptoInfo{env, cryptoInfoObj};
|
||||||
|
}
|
||||||
|
}();
|
||||||
err = codec->queueEncryptedLinearBlock(
|
err = codec->queueEncryptedLinearBlock(
|
||||||
index,
|
index,
|
||||||
memory,
|
memory,
|
||||||
@@ -2162,6 +2183,7 @@ static void android_media_MediaCodec_native_queueLinearBlock(
|
|||||||
&errorDetailMsg);
|
&errorDetailMsg);
|
||||||
} else {
|
} else {
|
||||||
if (!buffer) {
|
if (!buffer) {
|
||||||
|
ALOGI("queueLinearBlock: no C2Buffer found");
|
||||||
throwExceptionAsNecessary(env, BAD_VALUE);
|
throwExceptionAsNecessary(env, BAD_VALUE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -2955,13 +2977,13 @@ static jobject android_media_MediaCodec_LinearBlock_native_map(
|
|||||||
context->mLegacyBuffer->size(),
|
context->mLegacyBuffer->size(),
|
||||||
true, // readOnly
|
true, // readOnly
|
||||||
true /* clearBuffer */);
|
true /* clearBuffer */);
|
||||||
} else if (context->mHeap) {
|
} else if (context->mMemory) {
|
||||||
return CreateByteBuffer(
|
return CreateByteBuffer(
|
||||||
env,
|
env,
|
||||||
static_cast<uint8_t *>(context->mHeap->getBase()) + context->mHeap->getOffset(),
|
context->mMemory->unsecurePointer(),
|
||||||
context->mHeap->getSize(),
|
context->mMemory->size(),
|
||||||
0,
|
0,
|
||||||
context->mHeap->getSize(),
|
context->mMemory->size(),
|
||||||
false, // readOnly
|
false, // readOnly
|
||||||
true /* clearBuffer */);
|
true /* clearBuffer */);
|
||||||
}
|
}
|
||||||
@@ -3011,8 +3033,26 @@ static void android_media_MediaCodec_LinearBlock_native_obtain(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (hasSecure && !hasNonSecure) {
|
if (hasSecure && !hasNonSecure) {
|
||||||
context->mHeap = new MemoryHeapBase(capacity);
|
constexpr size_t kInitialDealerCapacity = 1048576; // 1MB
|
||||||
context->mMemory = hardware::fromHeap(context->mHeap);
|
thread_local sp<MemoryDealer> sDealer = new MemoryDealer(
|
||||||
|
kInitialDealerCapacity, "JNI(1MB)");
|
||||||
|
context->mMemory = sDealer->allocate(capacity);
|
||||||
|
if (context->mMemory == nullptr) {
|
||||||
|
size_t newDealerCapacity = sDealer->getMemoryHeap()->getSize() * 2;
|
||||||
|
while (capacity * 2 > newDealerCapacity) {
|
||||||
|
newDealerCapacity *= 2;
|
||||||
|
}
|
||||||
|
ALOGI("LinearBlock.native_obtain: "
|
||||||
|
"Dealer capacity increasing from %zuMB to %zuMB",
|
||||||
|
sDealer->getMemoryHeap()->getSize() / 1048576,
|
||||||
|
newDealerCapacity / 1048576);
|
||||||
|
sDealer = new MemoryDealer(
|
||||||
|
newDealerCapacity,
|
||||||
|
AStringPrintf("JNI(%zuMB)", newDealerCapacity).c_str());
|
||||||
|
context->mMemory = sDealer->allocate(capacity);
|
||||||
|
}
|
||||||
|
context->mHidlMemory = hardware::fromHeap(context->mMemory->getMemory(
|
||||||
|
&context->mHidlMemoryOffset, &context->mHidlMemorySize));
|
||||||
} else {
|
} else {
|
||||||
context->mBlock = MediaCodec::FetchLinearBlock(capacity, names);
|
context->mBlock = MediaCodec::FetchLinearBlock(capacity, names);
|
||||||
if (!context->mBlock) {
|
if (!context->mBlock) {
|
||||||
|
|||||||
@@ -162,6 +162,8 @@ struct JMediaCodec : public AHandler {
|
|||||||
|
|
||||||
void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
|
void selectAudioPresentation(const int32_t presentationId, const int32_t programId);
|
||||||
|
|
||||||
|
bool hasCryptoOrDescrambler() { return mHasCryptoOrDescrambler; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual ~JMediaCodec();
|
virtual ~JMediaCodec();
|
||||||
|
|
||||||
@@ -181,6 +183,7 @@ private:
|
|||||||
sp<MediaCodec> mCodec;
|
sp<MediaCodec> mCodec;
|
||||||
AString mNameAtCreation;
|
AString mNameAtCreation;
|
||||||
bool mGraphicOutput{false};
|
bool mGraphicOutput{false};
|
||||||
|
bool mHasCryptoOrDescrambler{false};
|
||||||
std::once_flag mReleaseFlag;
|
std::once_flag mReleaseFlag;
|
||||||
|
|
||||||
sp<AMessage> mCallbackNotification;
|
sp<AMessage> mCallbackNotification;
|
||||||
|
|||||||
@@ -31,8 +31,10 @@ struct JMediaCodecLinearBlock {
|
|||||||
std::shared_ptr<C2LinearBlock> mBlock;
|
std::shared_ptr<C2LinearBlock> mBlock;
|
||||||
std::shared_ptr<C2WriteView> mReadWriteMapping;
|
std::shared_ptr<C2WriteView> mReadWriteMapping;
|
||||||
|
|
||||||
sp<IMemoryHeap> mHeap;
|
sp<IMemory> mMemory;
|
||||||
sp<hardware::HidlMemory> mMemory;
|
sp<hardware::HidlMemory> mHidlMemory;
|
||||||
|
ssize_t mHidlMemoryOffset;
|
||||||
|
size_t mHidlMemorySize;
|
||||||
|
|
||||||
sp<MediaCodecBuffer> mLegacyBuffer;
|
sp<MediaCodecBuffer> mLegacyBuffer;
|
||||||
|
|
||||||
@@ -56,8 +58,8 @@ struct JMediaCodecLinearBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sp<hardware::HidlMemory> toHidlMemory() {
|
sp<hardware::HidlMemory> toHidlMemory() {
|
||||||
if (mMemory) {
|
if (mHidlMemory) {
|
||||||
return mMemory;
|
return mHidlMemory;
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -65,4 +67,4 @@ struct JMediaCodecLinearBlock {
|
|||||||
|
|
||||||
} // namespace android
|
} // namespace android
|
||||||
|
|
||||||
#endif // _ANDROID_MEDIA_MEDIACODECLINEARBLOCK_H_
|
#endif // _ANDROID_MEDIA_MEDIACODECLINEARBLOCK_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user