From 56bda1d94c321ec05e12841a562c0996f946ff4d Mon Sep 17 00:00:00 2001 From: sharpshooter_t Date: Mon, 14 Feb 2022 19:58:42 +0800 Subject: [PATCH 1/8] fix the codec crash due to patch which fix the fd leak Signed-off-by: sharpshooter_t Change-Id: I004a71637c4c307dcc7fca410295639a3bb764a5 --- .../avcodec/sink_wrapper/sink_bytebuffer_impl.cpp | 2 -- .../avcodec/src_wrapper/src_bytebuffer_impl.cpp | 2 -- .../services/avcodec/ipc/avcodec_service_proxy.cpp | 14 -------------- .../services/avcodec/ipc/avcodec_service_stub.cpp | 11 ----------- 4 files changed, 29 deletions(-) diff --git a/services/engine/gstreamer/avcodec/sink_wrapper/sink_bytebuffer_impl.cpp b/services/engine/gstreamer/avcodec/sink_wrapper/sink_bytebuffer_impl.cpp index 5d85701e0..89493d68d 100644 --- a/services/engine/gstreamer/avcodec/sink_wrapper/sink_bytebuffer_impl.cpp +++ b/services/engine/gstreamer/avcodec/sink_wrapper/sink_bytebuffer_impl.cpp @@ -70,10 +70,8 @@ int32_t SinkBytebufferImpl::Flush() gst_buffer_unref((*it)->gstBuffer_); (*it)->gstBuffer_ = nullptr; } - (*it)->mem_ = nullptr; } } - bufferList_.clear(); isFirstFrame_ = true; isEos_ = false; return MSERR_OK; diff --git a/services/engine/gstreamer/avcodec/src_wrapper/src_bytebuffer_impl.cpp b/services/engine/gstreamer/avcodec/src_wrapper/src_bytebuffer_impl.cpp index 872012d19..a5e21ac7f 100644 --- a/services/engine/gstreamer/avcodec/src_wrapper/src_bytebuffer_impl.cpp +++ b/services/engine/gstreamer/avcodec/src_wrapper/src_bytebuffer_impl.cpp @@ -91,10 +91,8 @@ int32_t SrcBytebufferImpl::Flush() gst_buffer_unref((*it)->gstBuffer_); (*it)->gstBuffer_ = nullptr; } - (*it)->mem_ = nullptr; } } - bufferList_.clear(); return MSERR_OK; } diff --git a/services/services/avcodec/ipc/avcodec_service_proxy.cpp b/services/services/avcodec/ipc/avcodec_service_proxy.cpp index 689c49aef..bd11394fe 100644 --- a/services/services/avcodec/ipc/avcodec_service_proxy.cpp +++ b/services/services/avcodec/ipc/avcodec_service_proxy.cpp @@ -68,11 +68,6 @@ public: return MSERR_INVALID_VAL; } - void ClearCache() - { - caches_.clear(); - } - private: DISALLOW_COPY_AND_MOVE(AVCodecBufferCache); @@ -193,15 +188,6 @@ int32_t AVCodecServiceProxy::Flush() MessageParcel reply; MessageOption option; int32_t ret = Remote()->SendRequest(FLUSH, data, reply, option); - - if (inputBufferCache_ != nullptr) { - inputBufferCache_->ClearCache(); - } - - if (outputBufferCache_ != nullptr) { - outputBufferCache_->ClearCache(); - } - if (ret != MSERR_OK) { MEDIA_LOGE("Flush failed, error: %{public}d", ret); return ret; diff --git a/services/services/avcodec/ipc/avcodec_service_stub.cpp b/services/services/avcodec/ipc/avcodec_service_stub.cpp index cea76802f..256b9334d 100644 --- a/services/services/avcodec/ipc/avcodec_service_stub.cpp +++ b/services/services/avcodec/ipc/avcodec_service_stub.cpp @@ -68,11 +68,6 @@ public: return WriteAVSharedMemoryToParcel(memory, parcel); } - void ClearCache() - { - caches_.clear(); - } - private: DISALLOW_COPY_AND_MOVE(AVCodecBufferCache); @@ -216,12 +211,6 @@ int32_t AVCodecServiceStub::Stop() int32_t AVCodecServiceStub::Flush() { CHECK_AND_RETURN_RET_LOG(codecServer_ != nullptr, MSERR_NO_MEMORY, "avcodec server is nullptr"); - if (inputBufferCache_ != nullptr) { - inputBufferCache_->ClearCache(); - } - if (outputBufferCache_ != nullptr) { - outputBufferCache_->ClearCache(); - } return codecServer_->Flush(); } -- Gitee From 48c978c57f5f4279eefd9350ed4a00e9422b4ea9 Mon Sep 17 00:00:00 2001 From: sharpshooter_t Date: Mon, 14 Feb 2022 22:00:05 +0800 Subject: [PATCH 2/8] fix the codec crash due to patch which fix the fd leak Signed-off-by: sharpshooter_t Change-Id: I43b3a1ab8e6ef1f651a7fed6dca6cd2d5877e05b --- .../audio_decoder_callback_napi.cpp | 21 +++++------ .../audio_decoder_callback_napi.h | 1 + .../audio_decoder/audio_decoder_napi.cpp | 4 +++ .../audio_encoder_callback_napi.cpp | 21 +++++------ .../audio_encoder/audio_encoder_napi.cpp | 4 +++ .../js/avcodec/utils/avcodec_napi_helper.cpp | 35 +++++++++++++++++++ .../js/avcodec/utils/avcodec_napi_helper.h | 8 +++++ 7 files changed, 74 insertions(+), 20 deletions(-) diff --git a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp index 347389d77..58267b1fe 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp @@ -18,6 +18,7 @@ #include "avcodec_napi_utils.h" #include "media_errors.h" #include "media_log.h" +#include "scope_guard.h" namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AudioDecoderCallbackNapi"}; @@ -242,21 +243,16 @@ void AudioDecoderCallbackNapi::OnJsErrorCallBack(AudioDecoderJsCallback *jsCb) c void AudioDecoderCallbackNapi::OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, bool isInput) const { + ON_SCOPE_EXIT(0) { delete jsCb; }; + uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env_, &loop); - if (loop == nullptr) { - MEDIA_LOGE("Fail to get uv event loop"); - delete jsCb; - return; - } + CHECK_AND_RETURN_LOG(loop != nullptr, "Fail to get uv event loop"); uv_work_t *work = new(std::nothrow) uv_work_t; - if (work == nullptr) { - MEDIA_LOGE("No memory"); - delete jsCb; - return; - } + CHECK_AND_RETURN_LOG(work != nullptr, "No memory"); + codecHelper_->PushWork(work); jsCb->isInput = isInput; work->data = reinterpret_cast(jsCb); // async callback, jsWork and jsWork->data should be heap object. @@ -287,6 +283,10 @@ void AudioDecoderCallbackNapi::OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result); CHECK_AND_BREAK(nstatus == napi_ok); } while (0); + auto codecHelper = event->codecHelper.lock(); + if (codecHelper != nullptr) { + codecHelper->RemoveWork(work); + } delete event; delete work; }); @@ -295,6 +295,7 @@ void AudioDecoderCallbackNapi::OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, delete jsCb; delete work; } + CANCEL_SCOPE_EXIT_GUARD(0); } void AudioDecoderCallbackNapi::OnJsFormatCallBack(AudioDecoderJsCallback *jsCb) const diff --git a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.h b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.h index d6dfb1b00..27ba8d564 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.h +++ b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.h @@ -57,6 +57,7 @@ private: std::shared_ptr memory = nullptr; bool isInput = false; Format format; + std::weak_ptr codecHelper; }; void OnJsErrorCallBack(AudioDecoderJsCallback *jsCb) const; void OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, bool isInput) const; diff --git a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_napi.cpp index 2de921040..6f329d88e 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_napi.cpp @@ -377,6 +377,7 @@ napi_value AudioDecoderNapi::Stop(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "Stop", NAPI_AUTO_LENGTH, &resource); asyncCtx->napi->codecHelper_->SetStop(true); asyncCtx->napi->codecHelper_->SetEos(false); + asyncCtx->napi->codecHelper_->CancelAllWorks(); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) { auto asyncCtx = reinterpret_cast(data); @@ -421,6 +422,7 @@ napi_value AudioDecoderNapi::Flush(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "Flush", NAPI_AUTO_LENGTH, &resource); asyncCtx->napi->codecHelper_->SetEos(false); asyncCtx->napi->codecHelper_->SetFlushing(true); + asyncCtx->napi->codecHelper_->CancelAllWorks(); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) { auto asyncCtx = reinterpret_cast(data); @@ -468,6 +470,7 @@ napi_value AudioDecoderNapi::Reset(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "Reset", NAPI_AUTO_LENGTH, &resource); asyncCtx->napi->codecHelper_->SetStop(true); asyncCtx->napi->codecHelper_->SetEos(false); + asyncCtx->napi->codecHelper_->CancelAllWorks(); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) { auto asyncCtx = reinterpret_cast(data); @@ -512,6 +515,7 @@ napi_value AudioDecoderNapi::Release(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "Release", NAPI_AUTO_LENGTH, &resource); asyncCtx->napi->codecHelper_->SetStop(true); asyncCtx->napi->codecHelper_->SetEos(false); + asyncCtx->napi->codecHelper_->CancelAllWorks(); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) { auto asyncCtx = reinterpret_cast(data); diff --git a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp index ff9b37253..59afbdfa0 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp @@ -18,6 +18,7 @@ #include "avcodec_napi_utils.h" #include "media_errors.h" #include "media_log.h" +#include "scope_guard.h" namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AudioEncoderCallbackNapi"}; @@ -242,21 +243,16 @@ void AudioEncoderCallbackNapi::OnJsErrorCallBack(AudioEncoderJsCallback *jsCb) c void AudioEncoderCallbackNapi::OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, bool isInput) const { + ON_SCOPE_EXIT(0) { delete jsCb; }; + uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env_, &loop); - if (loop == nullptr) { - MEDIA_LOGE("Fail to get uv event loop"); - delete jsCb; - return; - } + CHECK_AND_RETURN_LOG(loop != nullptr, "Fail to get uv event loop"); uv_work_t *work = new(std::nothrow) uv_work_t; - if (work == nullptr) { - MEDIA_LOGE("No memory"); - delete jsCb; - return; - } + CHECK_AND_RETURN_LOG(work != nullptr, "No memory"); + codecHelper_->PushWork(work); jsCb->isInput = isInput; work->data = reinterpret_cast(jsCb); // async callback, jsWork and jsWork->data should be heap object. @@ -286,6 +282,10 @@ void AudioEncoderCallbackNapi::OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result); CHECK_AND_BREAK(nstatus == napi_ok); } while (0); + auto codecHelper = event->codecHelper.lock(); + if (codecHelper != nullptr) { + codecHelper->RemoveWork(work); + } delete event; delete work; }); @@ -294,6 +294,7 @@ void AudioEncoderCallbackNapi::OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, delete jsCb; delete work; } + CANCEL_SCOPE_EXIT_GUARD(0); } void AudioEncoderCallbackNapi::OnJsFormatCallBack(AudioEncoderJsCallback *jsCb) const diff --git a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_napi.cpp index c20e7303c..edff9c52f 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_napi.cpp @@ -377,6 +377,7 @@ napi_value AudioEncoderNapi::Stop(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "Stop", NAPI_AUTO_LENGTH, &resource); asyncCtx->napi->codecHelper_->SetStop(true); asyncCtx->napi->codecHelper_->SetEos(false); + asyncCtx->napi->codecHelper_->CancelAllWorks(); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) { auto asyncCtx = reinterpret_cast(data); @@ -421,6 +422,7 @@ napi_value AudioEncoderNapi::Flush(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "Flush", NAPI_AUTO_LENGTH, &resource); asyncCtx->napi->codecHelper_->SetEos(false); asyncCtx->napi->codecHelper_->SetFlushing(true); + asyncCtx->napi->codecHelper_->CancelAllWorks(); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) { auto asyncCtx = reinterpret_cast(data); @@ -468,6 +470,7 @@ napi_value AudioEncoderNapi::Reset(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "Reset", NAPI_AUTO_LENGTH, &resource); asyncCtx->napi->codecHelper_->SetStop(true); asyncCtx->napi->codecHelper_->SetEos(false); + asyncCtx->napi->codecHelper_->CancelAllWorks(); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) { auto asyncCtx = reinterpret_cast(data); @@ -512,6 +515,7 @@ napi_value AudioEncoderNapi::Release(napi_env env, napi_callback_info info) napi_create_string_utf8(env, "Release", NAPI_AUTO_LENGTH, &resource); asyncCtx->napi->codecHelper_->SetStop(true); asyncCtx->napi->codecHelper_->SetEos(false); + asyncCtx->napi->codecHelper_->CancelAllWorks(); NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, [](napi_env env, void* data) { auto asyncCtx = reinterpret_cast(data); diff --git a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp index 17c2620b7..fc0c40dd4 100644 --- a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp +++ b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp @@ -14,6 +14,11 @@ */ #include "avcodec_napi_helper.h" +#include "media_log.h" + +namespace { + constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecNapiHelper"}; +} namespace OHOS { namespace Media { @@ -52,5 +57,35 @@ void AVCodecNapiHelper::SetFlushing(bool flushing) { isFlushing_.store(flushing); } + +void AVCodecNapiHelper::PushWork(uv_work_t *work) +{ + std::lock_guard lock(mutex_); + if (works_.find(work) == works_.end()) { + works_.emplace(work); + } +} + +void AVCodecNapiHelper::RemoveWork(uv_work_t *work) +{ + std::lock_guard lock(mutex_); + auto iter = works_.find(work); + if (iter != works_.end()) { + works_.erase(iter); + } +} + +void AVCodecNapiHelper::CancelAllWorks() +{ + std::lock_guard lock(mutex_); + for (auto &works : works_) { + int status = uv_cancel(reinterpret_cast(work)); + if (status != 0) { + MEDIA_LOGE("work cancel failed"); + } + } + works_.clear(); +} + } } diff --git a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.h b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.h index 328e76071..0375b6dc5 100644 --- a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.h +++ b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.h @@ -16,6 +16,9 @@ #ifndef AVCODEC_NAPI_HELPER_H #define AVCODEC_NAPI_HELPER_H #include +#include +#include +#include #include "nocopyable.h" namespace OHOS { @@ -30,12 +33,17 @@ public: void SetEos(bool eos); void SetStop(bool stop); void SetFlushing(bool flushing); + void PushWork(uv_work_t *work); + void RemoveWork(uv_work_t *work); + void CancelAllWorks(); DISALLOW_COPY_AND_MOVE(AVCodecNapiHelper); private: std::atomic isEos_ = false; std::atomic isStop_ = false; std::atomic isFlushing_ = false; + std::mutex mutex_; + std::unordered_set works_; }; } } -- Gitee From a44c8d939e4ce756fc9f9bcd398b0de61ba2f64d Mon Sep 17 00:00:00 2001 From: sharpshooter_t Date: Mon, 14 Feb 2022 22:39:40 +0800 Subject: [PATCH 3/8] fix the codec crash due to patch which fix the fd leak Signed-off-by: sharpshooter_t Change-Id: Ia290c217e6e39dd8256945545c98619d061d3b41 --- .../audio_decoder_callback_napi.cpp | 2 +- .../audio_decoder_callback_napi.h | 3 ++- .../audio_encoder_callback_napi.cpp | 2 +- .../audio_encoder_callback_napi.h | 3 ++- .../js/avcodec/utils/avcodec_napi_helper.cpp | 20 ++++++++++--------- .../js/avcodec/utils/avcodec_napi_utils.h | 4 ++++ 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp index 58267b1fe..62cc6a2a7 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp @@ -264,7 +264,7 @@ void AudioDecoderCallbackNapi::OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, MEDIA_LOGD("JsCallBack %{public}s, uv_queue_work start, index: %{public}u", event->callbackName.c_str(), event->index); do { - CHECK_AND_BREAK(status != UV_ECANCELED); + CHECK_AND_BREAK(!event->cancelled && status != UV_ECANCELED); napi_value jsCallback = nullptr; napi_status nstatus = napi_get_reference_value(env, event->callback->cb_, &jsCallback); CHECK_AND_BREAK(nstatus == napi_ok && jsCallback != nullptr); diff --git a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.h b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.h index 27ba8d564..0dcc17d66 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.h +++ b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.h @@ -22,6 +22,7 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "avcodec_napi_helper.h" +#include "avcodec_napi_utils.h" namespace OHOS { namespace Media { @@ -46,7 +47,7 @@ protected: void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override; private: - struct AudioDecoderJsCallback { + struct AudioDecoderJsCallback : public AVCodecJSCallback { std::shared_ptr callback = nullptr; std::string callbackName = "unknown"; std::string errorMsg = "unknown"; diff --git a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp index 59afbdfa0..2947dceb7 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp @@ -263,7 +263,7 @@ void AudioEncoderCallbackNapi::OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, napi_env env = event->callback->env_; MEDIA_LOGD("JsCallBack %{public}s, uv_queue_work start", event->callbackName.c_str()); do { - CHECK_AND_BREAK(status != UV_ECANCELED); + CHECK_AND_BREAK(!event->cancelled && status != UV_ECANCELED); napi_value jsCallback = nullptr; napi_status nstatus = napi_get_reference_value(env, event->callback->cb_, &jsCallback); CHECK_AND_BREAK(nstatus == napi_ok && jsCallback != nullptr); diff --git a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.h b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.h index 89c665310..0f4f51ba6 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.h +++ b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.h @@ -21,6 +21,7 @@ #include "common_napi.h" #include "napi/native_api.h" #include "napi/native_node_api.h" +#include "avcodec_napi_utils.h" namespace OHOS { namespace Media { @@ -45,7 +46,7 @@ protected: void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override; private: - struct AudioEncoderJsCallback { + struct AudioEncoderJsCallback : public AVCodecJSCallback { std::shared_ptr callback = nullptr; std::string callbackName = "unknown"; std::string errorMsg = "unknown"; diff --git a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp index fc0c40dd4..65dea2342 100644 --- a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp +++ b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp @@ -14,11 +14,7 @@ */ #include "avcodec_napi_helper.h" -#include "media_log.h" - -namespace { - constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecNapiHelper"}; -} +#include "avcodec_napi_utils.h" namespace OHOS { namespace Media { @@ -60,6 +56,10 @@ void AVCodecNapiHelper::SetFlushing(bool flushing) void AVCodecNapiHelper::PushWork(uv_work_t *work) { + if (work == nullptr || work->data == nullptr) { + return; + } + std::lock_guard lock(mutex_); if (works_.find(work) == works_.end()) { works_.emplace(work); @@ -68,6 +68,10 @@ void AVCodecNapiHelper::PushWork(uv_work_t *work) void AVCodecNapiHelper::RemoveWork(uv_work_t *work) { + if (work == nullptr) { + return; + } + std::lock_guard lock(mutex_); auto iter = works_.find(work); if (iter != works_.end()) { @@ -79,10 +83,8 @@ void AVCodecNapiHelper::CancelAllWorks() { std::lock_guard lock(mutex_); for (auto &works : works_) { - int status = uv_cancel(reinterpret_cast(work)); - if (status != 0) { - MEDIA_LOGE("work cancel failed"); - } + AVCodecJsCallback *jsCb = reinterpret_cast(work->data); + jsCb->cancelled = false; } works_.clear(); } diff --git a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_utils.h b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_utils.h index 4108df023..25ee2c8be 100644 --- a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_utils.h +++ b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_utils.h @@ -24,6 +24,10 @@ namespace OHOS { namespace Media { +struct AVCodecJSCallback { + bool cancelled = false; +}; + class AVCodecNapiUtil { public: AVCodecNapiUtil() = delete; -- Gitee From dc60872a5c7d5327b6c343972ca603cb91d4da8b Mon Sep 17 00:00:00 2001 From: sharpshooter_t Date: Mon, 14 Feb 2022 22:40:40 +0800 Subject: [PATCH 4/8] fix the codec crash due to patch which fix the fd leak Signed-off-by: sharpshooter_t Change-Id: I9e69a8c40143ac14ef537a16c52e824fda9616e1 --- frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp index 65dea2342..27a2f1950 100644 --- a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp +++ b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp @@ -88,6 +88,5 @@ void AVCodecNapiHelper::CancelAllWorks() } works_.clear(); } - } } -- Gitee From 08fb2eed6bf8ca1739701c18e8553eec2ca064b6 Mon Sep 17 00:00:00 2001 From: sharpshooter_t Date: Mon, 14 Feb 2022 23:46:05 +0800 Subject: [PATCH 5/8] fix the codec crash due to patch which fix the fd leak Signed-off-by: sharpshooter_t Change-Id: I443e8b595dcaf5e143b021a0241fd270ea48d93a --- .../avcodec/audio_decoder/audio_decoder_callback_napi.cpp | 1 + .../avcodec/audio_encoder/audio_encoder_callback_napi.cpp | 1 + .../kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp | 6 +++++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp index 62cc6a2a7..7d8adc1f0 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp @@ -292,6 +292,7 @@ void AudioDecoderCallbackNapi::OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, }); if (ret != 0) { MEDIA_LOGE("Failed to execute libuv work queue"); + codecHelper_->RemoveWork(work); delete jsCb; delete work; } diff --git a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp index 2947dceb7..330e4d1a7 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp @@ -291,6 +291,7 @@ void AudioEncoderCallbackNapi::OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, }); if (ret != 0) { MEDIA_LOGE("Failed to execute libuv work queue"); + codecHelper_->RemoveWork(work); delete jsCb; delete work; } diff --git a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp index 27a2f1950..4406cb12a 100644 --- a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp +++ b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp @@ -84,9 +84,13 @@ void AVCodecNapiHelper::CancelAllWorks() std::lock_guard lock(mutex_); for (auto &works : works_) { AVCodecJsCallback *jsCb = reinterpret_cast(work->data); + if (jsCb == nullptr) { + continue; + } jsCb->cancelled = false; } - works_.clear(); + std::unordered_set tmp; + tmp.swap(works_); } } } -- Gitee From 72eeedd0c21f6b1b231dd31789b436d8f84a4c85 Mon Sep 17 00:00:00 2001 From: sharpshooter_t Date: Tue, 15 Feb 2022 10:28:05 +0800 Subject: [PATCH 6/8] fix the codec crash due to patch which fix the fd leak Signed-off-by: sharpshooter_t Change-Id: I501714284b05b3f541c91dc4c04f80359e27e6ef --- .../audio_decoder/audio_decoder_callback_napi.cpp | 6 +++--- .../audio_encoder/audio_encoder_callback_napi.cpp | 6 +++--- .../js/avcodec/utils/avcodec_napi_helper.cpp | 15 +++++++-------- .../js/avcodec/utils/avcodec_napi_helper.h | 8 ++++---- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp index 7d8adc1f0..8e5d5d4b4 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp @@ -252,7 +252,7 @@ void AudioDecoderCallbackNapi::OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, uv_work_t *work = new(std::nothrow) uv_work_t; CHECK_AND_RETURN_LOG(work != nullptr, "No memory"); - codecHelper_->PushWork(work); + codecHelper_->PushWork(jsCb); jsCb->isInput = isInput; work->data = reinterpret_cast(jsCb); // async callback, jsWork and jsWork->data should be heap object. @@ -285,14 +285,14 @@ void AudioDecoderCallbackNapi::OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, } while (0); auto codecHelper = event->codecHelper.lock(); if (codecHelper != nullptr) { - codecHelper->RemoveWork(work); + codecHelper->RemoveWork(event); } delete event; delete work; }); if (ret != 0) { MEDIA_LOGE("Failed to execute libuv work queue"); - codecHelper_->RemoveWork(work); + codecHelper_->RemoveWork(jsCb); delete jsCb; delete work; } diff --git a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp index 330e4d1a7..ff8ed223c 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp @@ -252,7 +252,7 @@ void AudioEncoderCallbackNapi::OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, uv_work_t *work = new(std::nothrow) uv_work_t; CHECK_AND_RETURN_LOG(work != nullptr, "No memory"); - codecHelper_->PushWork(work); + codecHelper_->PushWork(jsCb); jsCb->isInput = isInput; work->data = reinterpret_cast(jsCb); // async callback, jsWork and jsWork->data should be heap object. @@ -284,14 +284,14 @@ void AudioEncoderCallbackNapi::OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, } while (0); auto codecHelper = event->codecHelper.lock(); if (codecHelper != nullptr) { - codecHelper->RemoveWork(work); + codecHelper->RemoveWork(event); } delete event; delete work; }); if (ret != 0) { MEDIA_LOGE("Failed to execute libuv work queue"); - codecHelper_->RemoveWork(work); + codecHelper_->RemoveWork(jsCb); delete jsCb; delete work; } diff --git a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp index 4406cb12a..d954e04f4 100644 --- a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp +++ b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.cpp @@ -14,7 +14,6 @@ */ #include "avcodec_napi_helper.h" -#include "avcodec_napi_utils.h" namespace OHOS { namespace Media { @@ -54,9 +53,9 @@ void AVCodecNapiHelper::SetFlushing(bool flushing) isFlushing_.store(flushing); } -void AVCodecNapiHelper::PushWork(uv_work_t *work) +void AVCodecNapiHelper::PushWork(AVCodecJSCallback *work) { - if (work == nullptr || work->data == nullptr) { + if (work == nullptr) { return; } @@ -66,7 +65,7 @@ void AVCodecNapiHelper::PushWork(uv_work_t *work) } } -void AVCodecNapiHelper::RemoveWork(uv_work_t *work) +void AVCodecNapiHelper::RemoveWork(AVCodecJSCallback *work) { if (work == nullptr) { return; @@ -82,14 +81,14 @@ void AVCodecNapiHelper::RemoveWork(uv_work_t *work) void AVCodecNapiHelper::CancelAllWorks() { std::lock_guard lock(mutex_); - for (auto &works : works_) { - AVCodecJsCallback *jsCb = reinterpret_cast(work->data); - if (jsCb == nullptr) { + for (auto iter = works_.begin(); iter != works_.end(); ++iter) { + if ((*iter) == nullptr) { continue; } + AVCodecJSCallback *jsCb = reinterpret_cast(*iter); jsCb->cancelled = false; } - std::unordered_set tmp; + std::unordered_set tmp; tmp.swap(works_); } } diff --git a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.h b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.h index 0375b6dc5..5c3d9a727 100644 --- a/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.h +++ b/frameworks/kitsimpl/js/avcodec/utils/avcodec_napi_helper.h @@ -18,7 +18,7 @@ #include #include #include -#include +#include "avcodec_napi_utils.h" #include "nocopyable.h" namespace OHOS { @@ -33,8 +33,8 @@ public: void SetEos(bool eos); void SetStop(bool stop); void SetFlushing(bool flushing); - void PushWork(uv_work_t *work); - void RemoveWork(uv_work_t *work); + void PushWork(AVCodecJSCallback *work); + void RemoveWork(AVCodecJSCallback *work); void CancelAllWorks(); DISALLOW_COPY_AND_MOVE(AVCodecNapiHelper); @@ -43,7 +43,7 @@ private: std::atomic isStop_ = false; std::atomic isFlushing_ = false; std::mutex mutex_; - std::unordered_set works_; + std::unordered_set works_; }; } } -- Gitee From 58fdaf99082ef9a0f9bfee221957a1b0c30f4e82 Mon Sep 17 00:00:00 2001 From: sharpshooter_t Date: Tue, 15 Feb 2022 10:29:27 +0800 Subject: [PATCH 7/8] fix the codec crash due to patch which fix the fd leak Signed-off-by: sharpshooter_t Change-Id: Ibbcbe9d878b5a3797e2c350116dcaca2b7a65f21 --- services/services/avcodec/ipc/avcodec_service_proxy.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/services/avcodec/ipc/avcodec_service_proxy.cpp b/services/services/avcodec/ipc/avcodec_service_proxy.cpp index bd11394fe..ea7858500 100644 --- a/services/services/avcodec/ipc/avcodec_service_proxy.cpp +++ b/services/services/avcodec/ipc/avcodec_service_proxy.cpp @@ -33,7 +33,6 @@ public: int32_t ReadFromParcel(uint32_t index, MessageParcel &parcel, std::shared_ptr &memory) { - MEDIA_LOGD("caches size: %{public}zu", caches_.size()); auto iter = caches_.find(index); CacheFlag flag = static_cast(parcel.ReadUint8()); if (flag == CacheFlag::HIT_CACHE) { -- Gitee From a2708715aa07ef9ce157f39c5c7e7f97858b824c Mon Sep 17 00:00:00 2001 From: sharpshooter_t Date: Tue, 15 Feb 2022 11:12:02 +0800 Subject: [PATCH 8/8] fix the codec crash due to patch which fix the fd leak Signed-off-by: sharpshooter_t Change-Id: Ie44b0aee7962a041e13b628d9819509c4725d7c0 --- .../js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp | 1 + .../js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp | 1 + .../js/avcodec/audio_encoder/audio_encoder_callback_napi.h | 1 + 3 files changed, 3 insertions(+) diff --git a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp index 8e5d5d4b4..ee60146fd 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_decoder/audio_decoder_callback_napi.cpp @@ -254,6 +254,7 @@ void AudioDecoderCallbackNapi::OnJsBufferCallBack(AudioDecoderJsCallback *jsCb, codecHelper_->PushWork(jsCb); jsCb->isInput = isInput; + jsCb->codecHelper = codecHelper_; work->data = reinterpret_cast(jsCb); // async callback, jsWork and jsWork->data should be heap object. int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { diff --git a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp index ff8ed223c..1c089c0d6 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp +++ b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.cpp @@ -254,6 +254,7 @@ void AudioEncoderCallbackNapi::OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, codecHelper_->PushWork(jsCb); jsCb->isInput = isInput; + jsCb->codecHelper = codecHelper_; work->data = reinterpret_cast(jsCb); // async callback, jsWork and jsWork->data should be heap object. int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) { diff --git a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.h b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.h index 0f4f51ba6..89d322d95 100644 --- a/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.h +++ b/frameworks/kitsimpl/js/avcodec/audio_encoder/audio_encoder_callback_napi.h @@ -57,6 +57,7 @@ private: std::shared_ptr memory = nullptr; bool isInput = false; Format format; + std::weak_ptr codecHelper; }; void OnJsErrorCallBack(AudioEncoderJsCallback *jsCb) const; void OnJsBufferCallBack(AudioEncoderJsCallback *jsCb, bool isInput) const; -- Gitee