From 0edbfe5e71863fc34f2d2de00254776cf59a3be9 Mon Sep 17 00:00:00 2001 From: Littlegnal <8847263+littleGnAl@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:32:55 +0800 Subject: [PATCH] fix: [windows] Fix TextureRender crash (#1999) We can't delete the `TextureRender` in the `VideoViewController::DestroyTextureRender`, because the `flutter::TextureRegistrar::UnregisterTexture` is called in the Flutter rasterizer thread, it's not thread-safe. We change the `VideoViewController::renderers_` to `std::map`, which holds the raw pointer `TextureRender *`, and delete the `TextureRender ` in the `flutter::TextureRegistrar::UnregisterTexture` callback which introduces from Flutter SDK 3.7.0. --- example/windows/flutter/CMakeLists.txt | 7 ++++++- example/windows/runner/Runner.rc | 10 +++++----- pubspec.yaml | 4 ++-- .../include/agora_rtc_engine/video_view_controller.h | 4 ++-- windows/texture_render.cc | 5 +++-- windows/video_view_controller.cc | 8 +++++--- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/example/windows/flutter/CMakeLists.txt b/example/windows/flutter/CMakeLists.txt index 930d2071a..903f4899d 100644 --- a/example/windows/flutter/CMakeLists.txt +++ b/example/windows/flutter/CMakeLists.txt @@ -10,6 +10,11 @@ include(${EPHEMERAL_DIR}/generated_config.cmake) # https://github.com/flutter/flutter/issues/57146. set(WRAPPER_ROOT "${EPHEMERAL_DIR}/cpp_client_wrapper") +# Set fallback configurations for older versions of the flutter tool. +if (NOT DEFINED FLUTTER_TARGET_PLATFORM) + set(FLUTTER_TARGET_PLATFORM "windows-x64") +endif() + # === Flutter Library === set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/flutter_windows.dll") @@ -92,7 +97,7 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E env ${FLUTTER_TOOL_ENVIRONMENT} "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat" - windows-x64 $ + ${FLUTTER_TARGET_PLATFORM} $ VERBATIM ) add_custom_target(flutter_assemble DEPENDS diff --git a/example/windows/runner/Runner.rc b/example/windows/runner/Runner.rc index e8af55a63..20892ea8b 100644 --- a/example/windows/runner/Runner.rc +++ b/example/windows/runner/Runner.rc @@ -60,14 +60,14 @@ IDI_APP_ICON ICON "resources\\app_icon.ico" // Version // -#ifdef FLUTTER_BUILD_NUMBER -#define VERSION_AS_NUMBER FLUTTER_BUILD_NUMBER +#if defined(FLUTTER_VERSION_MAJOR) && defined(FLUTTER_VERSION_MINOR) && defined(FLUTTER_VERSION_PATCH) && defined(FLUTTER_VERSION_BUILD) +#define VERSION_AS_NUMBER FLUTTER_VERSION_MAJOR,FLUTTER_VERSION_MINOR,FLUTTER_VERSION_PATCH,FLUTTER_VERSION_BUILD #else -#define VERSION_AS_NUMBER 1,0,0 +#define VERSION_AS_NUMBER 1,0,0,0 #endif -#ifdef FLUTTER_BUILD_NAME -#define VERSION_AS_STRING #FLUTTER_BUILD_NAME +#if defined(FLUTTER_VERSION) +#define VERSION_AS_STRING FLUTTER_VERSION #else #define VERSION_AS_STRING "1.0.0" #endif diff --git a/pubspec.yaml b/pubspec.yaml index 4b576372e..80a558c86 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,8 +6,8 @@ version: 6.3.2 homepage: https://www.agora.io repository: https://github.com/AgoraIO-Extensions/Agora-Flutter-SDK/tree/main environment: - sdk: '>=2.14.0 <3.0.0' - flutter: '>=2.5.0' + sdk: '>=2.17.0 <4.0.0' + flutter: '>=3.0.0' dependencies: flutter: sdk: flutter diff --git a/windows/include/agora_rtc_engine/video_view_controller.h b/windows/include/agora_rtc_engine/video_view_controller.h index c3d4d4d36..f54f7a10f 100644 --- a/windows/include/agora_rtc_engine/video_view_controller.h +++ b/windows/include/agora_rtc_engine/video_view_controller.h @@ -15,7 +15,7 @@ class VideoViewController private: flutter::BinaryMessenger *messenger_; flutter::TextureRegistrar *texture_registrar_; - std::map> renderers_; + std::map renderers_; void HandleMethodCall( const flutter::MethodCall &method_call, @@ -29,7 +29,7 @@ class VideoViewController const intptr_t &irisRtcRenderingHandle, unsigned int uid, const std::string &channelId, - unsigned int videoSourceType, + unsigned int videoSourceType, unsigned int videoViewSetupMode); bool DestroyTextureRender(int64_t textureId); diff --git a/windows/texture_render.cc b/windows/texture_render.cc index c8ffd7174..e884b9f7a 100644 --- a/windows/texture_render.cc +++ b/windows/texture_render.cc @@ -33,7 +33,6 @@ TextureRender::TextureRender(flutter::BinaryMessenger *messenger, TextureRender::~TextureRender() { - Dispose(); } int64_t TextureRender::texture_id() { return texture_id_; } @@ -150,7 +149,9 @@ void TextureRender::Dispose() if (registrar_ && texture_id_ != -1) { - registrar_->UnregisterTexture(texture_id_); + auto self = this; + registrar_->UnregisterTexture(texture_id_, [self]() + { delete self; }); registrar_ = nullptr; texture_id_ = -1; diff --git a/windows/video_view_controller.cc b/windows/video_view_controller.cc index 0847b6977..198fd5671 100644 --- a/windows/video_view_controller.cc +++ b/windows/video_view_controller.cc @@ -41,6 +41,7 @@ VideoViewController::VideoViewController( VideoViewController::~VideoViewController() { + Dispose(); } void VideoViewController::HandleMethodCall( @@ -154,15 +155,16 @@ int64_t VideoViewController::CreateTextureRender( unsigned int videoViewSetupMode) { agora::iris::IrisRtcRendering *iris_rtc_rendering = reinterpret_cast(irisRtcRenderingHandle); - std::unique_ptr textureRender = std::make_unique( + auto textureRender = new TextureRender( messenger_, texture_registrar_, iris_rtc_rendering); + int64_t texture_id = textureRender->texture_id(); - textureRender.get()->UpdateData(uid, channelId, videoSourceType, videoViewSetupMode); + textureRender->UpdateData(uid, channelId, videoSourceType, videoViewSetupMode); - renderers_[texture_id] = std::move(textureRender); + renderers_[texture_id] = textureRender; return texture_id; }