!3970 修复循环引用导致的内存泄漏

Merge pull request !3970 from SuRuoyan/master
This commit is contained in:
openharmony_ci 2024-11-20 13:26:18 +00:00 committed by Gitee
commit 8238b24b68
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 37 additions and 28 deletions

View File

@ -69,9 +69,8 @@ public:
int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &codecCallback) override
{
callback_ = codecCallback;
std::shared_ptr<Media::AudioBaseCodecCallback> mediaCallback =
std::make_shared<AudioCodecCallback>(shared_from_this());
return StatusToAVCodecServiceErrCode(static_cast<Media::Status>(mediaCodec_->SetCodecCallback(mediaCallback)));
mediaCallback_ = std::make_shared<AudioCodecCallback>(shared_from_this());
return StatusToAVCodecServiceErrCode(static_cast<Media::Status>(mediaCodec_->SetCodecCallback(mediaCallback_)));
}
int32_t Prepare() override
@ -106,7 +105,9 @@ public:
int32_t Release() override
{
return StatusToAVCodecServiceErrCode(static_cast<Media::Status>(mediaCodec_->Release()));
int32_t ret = StatusToAVCodecServiceErrCode(static_cast<Media::Status>(mediaCodec_->Release()));
mediaCallback_ = nullptr;
return ret;
}
int32_t NotifyEos() override
@ -149,16 +150,17 @@ public:
void OnError(CodecErrorType errorType, int32_t errorCode)
{
if (callback_ == nullptr) {
auto realPtr = callback_.lock();
if (realPtr == nullptr) {
return;
}
switch (errorType) {
case CodecErrorType::CODEC_DRM_DECRYTION_FAILED:
callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_DECRYTION_FAILED,
realPtr->OnError(AVCodecErrorType::AVCODEC_ERROR_DECRYTION_FAILED,
StatusToAVCodecServiceErrCode(static_cast<Media::Status>(errorCode)));
break;
default:
callback_->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL,
realPtr->OnError(AVCodecErrorType::AVCODEC_ERROR_INTERNAL,
StatusToAVCodecServiceErrCode(static_cast<Media::Status>(errorCode)));
break;
}
@ -166,8 +168,9 @@ public:
void OnOutputBufferDone(const std::shared_ptr<AVBuffer> &outputBuffer)
{
if (callback_) {
callback_->OnOutputBufferAvailable(0, outputBuffer);
auto realPtr = callback_.lock();
if (realPtr != nullptr) {
realPtr->OnOutputBufferAvailable(0, outputBuffer);
}
}
@ -182,7 +185,10 @@ public:
private:
std::shared_ptr<Media::MediaCodec> mediaCodec_;
std::shared_ptr<MediaCodecCallback> callback_;
// other callback from north interface(codec server)
std::weak_ptr<MediaCodecCallback> callback_;
// self callback register in mediaCodec_
std::shared_ptr<Media::AudioBaseCodecCallback> mediaCallback_ = nullptr;
};
void AudioCodecCallback::OnError(Media::CodecErrorType errorType, int32_t errorCode)

View File

@ -58,8 +58,6 @@ MediaCodec::MediaCodec()
inputBufferQueueProducer_(nullptr),
inputBufferQueueConsumer_(nullptr),
outputBufferQueueProducer_(nullptr),
codecCallback_(nullptr),
mediaCodecCallback_(nullptr),
isEncoder_(false),
isSurfaceMode_(false),
isBufferMode_(false),
@ -87,12 +85,6 @@ MediaCodec::~MediaCodec()
if (outputBufferQueueProducer_) {
outputBufferQueueProducer_ = nullptr;
}
if (codecCallback_) {
codecCallback_ = nullptr;
}
if (mediaCodecCallback_) {
mediaCodecCallback_ = nullptr;
}
}
int32_t MediaCodec::Init(const std::string &mime, bool isEncoder)
@ -509,8 +501,9 @@ Status MediaCodec::DrmAudioCencDecrypt(std::shared_ptr<AVBuffer> &filledInputBuf
void MediaCodec::HandleAudioCencDecryptError()
{
MEDIA_LOG_E("MediaCodec DrmAudioCencDecrypt failed.");
if (mediaCodecCallback_ != nullptr) {
mediaCodecCallback_->OnError(CodecErrorType::CODEC_DRM_DECRYTION_FAILED,
auto realPtr = mediaCodecCallback_.lock();
if (realPtr != nullptr) {
realPtr->OnError(CodecErrorType::CODEC_DRM_DECRYTION_FAILED,
static_cast<int32_t>(Status::ERROR_DRM_DECRYPT_FAILED));
}
}
@ -747,8 +740,9 @@ void MediaCodec::OnOutputBufferDone(const std::shared_ptr<AVBuffer> &outputBuffe
DumpAVBufferToFile(DUMP_PARAM, dumpPrefix_ + DUMP_FILE_NAME, outputBuffer);
}
Status ret = outputBufferQueueProducer_->PushBuffer(outputBuffer, true);
if (mediaCodecCallback_) {
mediaCodecCallback_->OnOutputBufferDone(outputBuffer);
auto realPtr = mediaCodecCallback_.lock();
if (realPtr != nullptr) {
realPtr->OnOutputBufferDone(outputBuffer);
}
MEDIA_LOG_D("0x%{public}06" PRIXPTR " OnOutputBufferDone, buffer->pts" PUBLIC_LOG_D64,
FAKE_POINTER(this), outputBuffer->pts_);

View File

@ -162,8 +162,9 @@ private:
sptr<AVBufferQueueProducer> inputBufferQueueProducer_;
sptr<AVBufferQueueConsumer> inputBufferQueueConsumer_;
sptr<AVBufferQueueProducer> outputBufferQueueProducer_;
std::shared_ptr<CodecCallback> codecCallback_;
std::shared_ptr<AudioBaseCodecCallback> mediaCodecCallback_;
std::weak_ptr<CodecCallback> codecCallback_;
// callback from north interface(AudioCodec..)
std::weak_ptr<AudioBaseCodecCallback> mediaCodecCallback_;
AVBufferConfig outputBufferConfig_;
bool isEncoder_;
bool isSurfaceMode_;

View File

@ -165,6 +165,8 @@ CodecServer::~CodecServer()
if (thread->joinable()) {
thread->join();
}
shareBufCallback_ = nullptr;
avBufCallback_ = nullptr;
(void)mallopt(M_FLUSH_THREAD_CACHE, 0);
AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
@ -194,12 +196,12 @@ int32_t CodecServer::Init(AVCodecType type, bool isMimeType, const std::string &
name.c_str(), ret);
SetCallerInfo(callerInfo);
std::shared_ptr<AVCodecCallback> callback = std::make_shared<CodecBaseCallback>(shared_from_this());
ret = codecBase_->SetCallback(callback);
shareBufCallback_ = std::make_shared<CodecBaseCallback>(shared_from_this());
ret = codecBase_->SetCallback(shareBufCallback_);
CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "SetCallback failed.");
std::shared_ptr<MediaCodecCallback> videoCallback = std::make_shared<VCodecBaseCallback>(shared_from_this());
ret = codecBase_->SetCallback(videoCallback);
avBufCallback_ = std::make_shared<VCodecBaseCallback>(shared_from_this());
ret = codecBase_->SetCallback(avBufCallback_);
CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, ret, "SetCallback failed.");
StatusChanged(INITIALIZED);
@ -438,6 +440,8 @@ int32_t CodecServer::Release()
if (thread && thread->joinable()) {
thread->join();
}
shareBufCallback_ = nullptr;
avBufCallback_ = nullptr;
(void)ReleasePostProcessing();
if (ret == AVCS_ERR_OK) {
isSurfaceMode_ = false;
@ -955,6 +959,7 @@ CodecBaseCallback::CodecBaseCallback(const std::shared_ptr<CodecServer> &codec)
CodecBaseCallback::~CodecBaseCallback()
{
codec_ = nullptr;
AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
}
@ -994,6 +999,7 @@ VCodecBaseCallback::VCodecBaseCallback(const std::shared_ptr<CodecServer> &codec
VCodecBaseCallback::~VCodecBaseCallback()
{
codec_ = nullptr;
AVCODEC_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
}

View File

@ -194,6 +194,8 @@ private:
std::atomic<uint64_t> decodedFrameCount_{0};
std::atomic<uint64_t> processedFrameCount_{0};
std::atomic<bool> decoderIsEOS_{false};
std::shared_ptr<AVCodecCallback> shareBufCallback_ = nullptr;
std::shared_ptr<MediaCodecCallback> avBufCallback_ = nullptr;
};
class CodecBaseCallback : public AVCodecCallback, public NoCopyable {