From 0fa70786bd2f3e40e649621c3122c40374400ee4 Mon Sep 17 00:00:00 2001 From: cobalt-github-releaser-bot <95661244+cobalt-github-releaser-bot@users.noreply.github.com> Date: Thu, 26 Oct 2023 11:15:07 -0700 Subject: [PATCH] Cherry pick PR #1846: [android] Lazy load media capability to avoid ANR (#1852) Refer to the original PR: https://github.com/youtube/cobalt/pull/1846 1. Avoid any media capability queries from system callback, so if there's any display/audio device change, media capability cache would do a lazy reload. 2. Remove capability pre-loading to avoid querying capability right after app launch. 3. Reload all capabilities when there's any display device change to simplify the code. b/304602770 Co-authored-by: Jason --- .../java/dev/cobalt/coat/CobaltActivity.java | 4 -- .../shared/media_capabilities_cache.cc | 59 +++++-------------- .../android/shared/media_capabilities_cache.h | 12 ++-- 3 files changed, 18 insertions(+), 57 deletions(-) diff --git a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/CobaltActivity.java b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/CobaltActivity.java index 2a763280dd1db..129b3c1aca2f4 100644 --- a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/CobaltActivity.java +++ b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/CobaltActivity.java @@ -159,8 +159,6 @@ protected void onStart() { getStarboardBridge().onActivityStart(this, keyboardEditor); super.onStart(); - - nativeInitializeMediaCapabilitiesInBackground(); } @Override @@ -396,6 +394,4 @@ public void onLowMemory() { public long getAppStartTimestamp() { return timeInNanoseconds; } - - private static native void nativeInitializeMediaCapabilitiesInBackground(); } diff --git a/starboard/android/shared/media_capabilities_cache.cc b/starboard/android/shared/media_capabilities_cache.cc index ed2674d08e864..fe96cc8c96c89 100644 --- a/starboard/android/shared/media_capabilities_cache.cc +++ b/starboard/android/shared/media_capabilities_cache.cc @@ -604,24 +604,6 @@ std::string MediaCapabilitiesCache::FindVideoDecoder( return ""; } -void MediaCapabilitiesCache::ClearCache() { - ScopedLock scoped_lock(mutex_); - is_initialized_ = false; - supported_hdr_types_is_dirty_ = true; - is_widevine_supported_ = false; - is_cbcs_supported_ = false; - supported_transfer_ids_.clear(); - passthrough_supportabilities_.clear(); - audio_codec_capabilities_map_.clear(); - video_codec_capabilities_map_.clear(); - audio_configurations_.clear(); -} - -void MediaCapabilitiesCache::Initialize() { - ScopedLock scoped_lock(mutex_); - UpdateMediaCapabilities_Locked(); -} - MediaCapabilitiesCache::MediaCapabilitiesCache() { // Enable mime and key system caches. MimeSupportabilityCache::GetInstance()->SetCacheEnabled(true); @@ -630,19 +612,20 @@ MediaCapabilitiesCache::MediaCapabilitiesCache() { void MediaCapabilitiesCache::UpdateMediaCapabilities_Locked() { mutex_.DCheckAcquired(); - - if (supported_hdr_types_is_dirty_.exchange(false)) { + if (capabilities_is_dirty_.exchange(false)) { + // We use a different cache strategy (load and cache) for passthrough + // supportabilities, so we only clear |passthrough_supportabilities_| here. + passthrough_supportabilities_.clear(); + + audio_codec_capabilities_map_.clear(); + video_codec_capabilities_map_.clear(); + audio_configurations_.clear(); + is_widevine_supported_ = GetIsWidevineSupported(); + is_cbcs_supported_ = GetIsCbcsSupported(); supported_transfer_ids_ = GetSupportedHdrTypes(); + LoadCodecInfos_Locked(); + LoadAudioConfigurations_Locked(); } - - if (is_initialized_) { - return; - } - is_widevine_supported_ = GetIsWidevineSupported(); - is_cbcs_supported_ = GetIsCbcsSupported(); - LoadCodecInfos_Locked(); - LoadAudioConfigurations_Locked(); - is_initialized_ = true; } void MediaCapabilitiesCache::LoadCodecInfos_Locked() { @@ -713,33 +696,19 @@ void MediaCapabilitiesCache::LoadAudioConfigurations_Locked() { extern "C" SB_EXPORT_PLATFORM void Java_dev_cobalt_util_DisplayUtil_nativeOnDisplayChanged() { - SB_DLOG(INFO) << "Display device has changed."; - MediaCapabilitiesCache::GetInstance()->ClearSupportedHdrTypes(); + // Display device change could change hdr capabilities. + MediaCapabilitiesCache::GetInstance()->ClearCache(); MimeSupportabilityCache::GetInstance()->ClearCachedMimeSupportabilities(); } extern "C" SB_EXPORT_PLATFORM void Java_dev_cobalt_media_AudioOutputManager_nativeOnAudioDeviceChanged() { - SB_DLOG(INFO) << "Audio device has changed."; // Audio output device change could change passthrough decoder capabilities, // so we have to reload codec capabilities. MediaCapabilitiesCache::GetInstance()->ClearCache(); MimeSupportabilityCache::GetInstance()->ClearCachedMimeSupportabilities(); } -void* MediaCapabilitiesCacheInitializationThreadEntry(void* context) { - SB_LOG(INFO) << "Initialize MediaCapabilitiesCache in background."; - MediaCapabilitiesCache::GetInstance()->Initialize(); - return 0; -} - -extern "C" SB_EXPORT_PLATFORM void -Java_dev_cobalt_coat_CobaltActivity_nativeInitializeMediaCapabilitiesInBackground() { - SbThreadCreate(0, kSbThreadPriorityNormal, kSbThreadNoAffinity, false, - "media_capabilities_cache_thread", - MediaCapabilitiesCacheInitializationThreadEntry, nullptr); -} - } // namespace shared } // namespace android } // namespace starboard diff --git a/starboard/android/shared/media_capabilities_cache.h b/starboard/android/shared/media_capabilities_cache.h index cedfe99db720a..656398a30a9e8 100644 --- a/starboard/android/shared/media_capabilities_cache.h +++ b/starboard/android/shared/media_capabilities_cache.h @@ -155,10 +155,7 @@ class MediaCapabilitiesCache { bool IsEnabled() const { return is_enabled_; } void SetCacheEnabled(bool enabled) { is_enabled_ = enabled; } - void ClearCache(); - - void Initialize(); - void ClearSupportedHdrTypes() { supported_hdr_types_is_dirty_ = true; } + void ClearCache() { capabilities_is_dirty_ = true; } private: MediaCapabilitiesCache(); @@ -184,12 +181,11 @@ class MediaCapabilitiesCache { std::map audio_codec_capabilities_map_; std::map video_codec_capabilities_map_; std::vector audio_configurations_; - - std::atomic_bool is_enabled_{true}; - std::atomic_bool supported_hdr_types_is_dirty_{true}; - bool is_initialized_ = false; bool is_widevine_supported_ = false; bool is_cbcs_supported_ = false; + + std::atomic_bool is_enabled_{true}; + std::atomic_bool capabilities_is_dirty_{true}; }; } // namespace shared