Skip to content

Commit

Permalink
[android] Lazy load media capability to avoid ANR (#1846)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jasonzhangxx authored Oct 26, 2023
1 parent 0af7498 commit 3483cb4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ protected void onStart() {

getStarboardBridge().onActivityStart(this, keyboardEditor);
super.onStart();

nativeInitializeMediaCapabilitiesInBackground();
}

@Override
Expand Down Expand Up @@ -396,6 +394,4 @@ public void onLowMemory() {
public long getAppStartTimestamp() {
return timeInNanoseconds;
}

private static native void nativeInitializeMediaCapabilitiesInBackground();
}
59 changes: 14 additions & 45 deletions starboard/android/shared/media_capabilities_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -592,24 +592,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);
Expand All @@ -618,19 +600,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() {
Expand Down Expand Up @@ -701,33 +684,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
12 changes: 4 additions & 8 deletions starboard/android/shared/media_capabilities_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,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();
Expand All @@ -181,12 +178,11 @@ class MediaCapabilitiesCache {
std::map<std::string, AudioCodecCapabilities> audio_codec_capabilities_map_;
std::map<std::string, VideoCodecCapabilities> video_codec_capabilities_map_;
std::vector<SbMediaAudioConfiguration> 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
Expand Down

0 comments on commit 3483cb4

Please sign in to comment.