Fix MediaEvent release issue

Bug: 158876323
Test: make;
Change-Id: Idedf41620b5a32ca552836d00b1db78adcfd1a7f
This commit is contained in:
shubang
2020-06-17 11:00:11 -07:00
parent 892d269b65
commit c84fe12f1d
3 changed files with 26 additions and 8 deletions

View File

@@ -29,6 +29,7 @@ import android.media.MediaCodec.LinearBlock;
@SystemApi
public class MediaEvent extends FilterEvent {
private long mNativeContext;
private boolean mReleased = false;
private final Object mLock = new Object();
private native Long nativeGetAudioHandle();
@@ -181,7 +182,21 @@ public class MediaEvent extends FilterEvent {
*/
@Override
protected void finalize() {
nativeFinalize();
mNativeContext = 0;
release();
}
/**
* Releases the MediaEvent object.
* @hide
*/
public void release() {
synchronized (mLock) {
if (mReleased) {
return;
}
nativeFinalize();
mNativeContext = 0;
mReleased = true;
}
}
}

View File

@@ -314,8 +314,9 @@ MediaEvent::~MediaEvent() {
if (mIonHandle != NULL) {
delete mIonHandle;
}
if (mC2Buffer != NULL) {
mC2Buffer->unregisterOnDestroyNotify(&DestroyCallback, this);
std::shared_ptr<C2Buffer> pC2Buffer = mC2Buffer.lock();
if (pC2Buffer != NULL) {
pC2Buffer->unregisterOnDestroyNotify(&DestroyCallback, this);
}
}
@@ -340,15 +341,17 @@ jobject MediaEvent::getLinearBlock() {
JNIEnv *env = AndroidRuntime::getJNIEnv();
std::unique_ptr<JMediaCodecLinearBlock> context{new JMediaCodecLinearBlock};
context->mBlock = block;
mC2Buffer = context->toC2Buffer(0, mDataLength);
std::shared_ptr<C2Buffer> pC2Buffer = context->toC2Buffer(0, mDataLength);
context->mBuffer = pC2Buffer;
mC2Buffer = pC2Buffer;
if (mAvHandle->numInts > 0) {
// use first int in the native_handle as the index
int index = mAvHandle->data[mAvHandle->numFds];
std::shared_ptr<C2Param> c2param = std::make_shared<C2DataIdInfo>(index, mDataId);
std::shared_ptr<C2Info> info(std::static_pointer_cast<C2Info>(c2param));
mC2Buffer->setInfo(info);
pC2Buffer->setInfo(info);
}
mC2Buffer->registerOnDestroyNotify(&DestroyCallback, this);
pC2Buffer->registerOnDestroyNotify(&DestroyCallback, this);
jobject linearBlock =
env->NewObject(
env->FindClass("android/media/MediaCodec$LinearBlock"),

View File

@@ -130,7 +130,7 @@ struct MediaEvent : public RefBase {
jweak mMediaEventObj;
jweak mLinearBlockObj;
C2HandleIon* mIonHandle;
std::shared_ptr<C2Buffer> mC2Buffer;
std::weak_ptr<C2Buffer> mC2Buffer;
};
struct Filter : public RefBase {