-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] Asynchronously destroy MediaCodecBridge (#3186)
b/331215721 Co-authored-by: Colin Liang <[email protected]>
- Loading branch information
1 parent
66936f4
commit 5e8fa2d
Showing
10 changed files
with
378 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
starboard/android/shared/media_codec_bridge_eradicator.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "starboard/android/shared/media_codec_bridge_eradicator.h" | ||
|
||
#include <pthread.h> | ||
|
||
#include "starboard/android/shared/jni_utils.h" | ||
#include "starboard/common/log.h" | ||
#include "starboard/common/once.h" | ||
|
||
namespace starboard { | ||
namespace android { | ||
namespace shared { | ||
|
||
SB_ONCE_INITIALIZE_FUNCTION(MediaCodecBridgeEradicator, | ||
MediaCodecBridgeEradicator::GetInstance); | ||
|
||
namespace { | ||
|
||
struct EradicateParam { | ||
EradicateParam(MediaCodecBridgeEradicator* eradicator, | ||
jobject j_media_codec_bridge, | ||
jobject j_reused_get_output_format_result) | ||
: eradicator(eradicator), | ||
j_media_codec_bridge(j_media_codec_bridge), | ||
j_reused_get_output_format_result(j_reused_get_output_format_result) {} | ||
|
||
MediaCodecBridgeEradicator* eradicator; | ||
jobject j_media_codec_bridge; | ||
jobject j_reused_get_output_format_result; | ||
}; | ||
|
||
} // namespace | ||
|
||
void MediaCodecBridgeEradicator::WaitForPendingDestructions() { | ||
ScopedLock scoped_lock(mutex_); | ||
while (!j_media_codec_bridge_set_.empty()) { | ||
condition_variable_.Wait(); | ||
} | ||
} | ||
|
||
bool MediaCodecBridgeEradicator::Destroy( | ||
jobject j_media_codec_bridge, | ||
jobject j_reused_get_output_format_result) { | ||
// Since the native media_codec_bridge object is about to be destroyed, remove | ||
// its reference from the Java MediaCodecBridge object to prevent further | ||
// access. Otherwise it could lead to an application crash. | ||
// The de-reference has to happen before the java MediaCodecBridge object is | ||
// destroyed. | ||
JniEnvExt* env = JniEnvExt::Get(); | ||
env->CallVoidMethodOrAbort(j_media_codec_bridge, | ||
"resetNativeMediaCodecBridge", "()V"); | ||
|
||
{ | ||
// add the j_media_codec_bridge to the set before the work is started | ||
ScopedLock scoped_lock(mutex_); | ||
j_media_codec_bridge_set_.insert(j_media_codec_bridge); | ||
} | ||
|
||
EradicateParam* param = new EradicateParam(this, j_media_codec_bridge, | ||
j_reused_get_output_format_result); | ||
pthread_t thread_id; | ||
int result = pthread_create( | ||
&thread_id, nullptr, &MediaCodecBridgeEradicator::DestroyMediaCodecBridge, | ||
param); | ||
|
||
if (result == 0) { | ||
pthread_detach(thread_id); | ||
} else { | ||
// If it fails to create the thread | ||
delete param; | ||
|
||
ScopedLock scoped_lock(mutex_); | ||
j_media_codec_bridge_set_.erase(j_media_codec_bridge); | ||
if (j_media_codec_bridge_set_.empty()) { | ||
condition_variable_.Signal(); | ||
} | ||
} | ||
|
||
return result == 0; | ||
} | ||
|
||
void* MediaCodecBridgeEradicator::DestroyMediaCodecBridge(void* context) { | ||
EradicateParam* param = static_cast<EradicateParam*>(context); | ||
SB_DCHECK(param != nullptr); | ||
|
||
MediaCodecBridgeEradicator* eradicator = param->eradicator; | ||
jobject j_media_codec_bridge = param->j_media_codec_bridge; | ||
jobject j_reused_get_output_format_result = | ||
param->j_reused_get_output_format_result; | ||
|
||
delete param; | ||
param = NULL; | ||
|
||
JniEnvExt* env = JniEnvExt::Get(); | ||
|
||
env->CallVoidMethodOrAbort(j_media_codec_bridge, "stop", "()V"); | ||
env->CallVoidMethodOrAbort(j_media_codec_bridge, "release", "()V"); | ||
env->DeleteGlobalRef(j_media_codec_bridge); | ||
|
||
SB_DCHECK(j_reused_get_output_format_result); | ||
env->DeleteGlobalRef(j_reused_get_output_format_result); | ||
|
||
{ | ||
// the work is finished, delete the j_media_codec_bridge from the set | ||
ScopedLock scoped_lock(eradicator->mutex_); | ||
eradicator->j_media_codec_bridge_set_.erase(j_media_codec_bridge); | ||
|
||
if (eradicator->j_media_codec_bridge_set_.empty()) { | ||
eradicator->condition_variable_.Signal(); | ||
} | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
} // namespace shared | ||
} // namespace android | ||
} // namespace starboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef STARBOARD_ANDROID_SHARED_MEDIA_CODEC_BRIDGE_ERADICATOR_H_ | ||
#define STARBOARD_ANDROID_SHARED_MEDIA_CODEC_BRIDGE_ERADICATOR_H_ | ||
|
||
#include <jni.h> | ||
#include <set> | ||
|
||
#include "starboard/android/shared/jni_env_ext.h" | ||
#include "starboard/common/atomic.h" | ||
#include "starboard/common/condition_variable.h" | ||
#include "starboard/common/mutex.h" | ||
|
||
namespace starboard { | ||
namespace android { | ||
namespace shared { | ||
|
||
/** | ||
* This class is a singleton utility that runs the MediaCodecBridge destructor | ||
* on a separate thread to prevent Application Not Responding (ANR) errors in | ||
* Android. | ||
*/ | ||
class MediaCodecBridgeEradicator { | ||
public: | ||
static MediaCodecBridgeEradicator* GetInstance(); | ||
|
||
~MediaCodecBridgeEradicator() { WaitForPendingDestructions(); } | ||
|
||
bool Destroy(jobject j_media_codec_bridge, | ||
jobject j_reused_get_output_format_result); | ||
void WaitForPendingDestructions(); | ||
bool IsEnabled() const { return is_enabled_.load(); } | ||
void SetEnabled(bool enabled) { is_enabled_.store(enabled); } | ||
|
||
private: | ||
static void* DestroyMediaCodecBridge(void* context); | ||
|
||
atomic_bool is_enabled_; // false by default | ||
Mutex mutex_; | ||
ConditionVariable condition_variable_{mutex_}; | ||
std::set<jobject> j_media_codec_bridge_set_; | ||
}; | ||
|
||
} // namespace shared | ||
} // namespace android | ||
} // namespace starboard | ||
|
||
#endif // STARBOARD_ANDROID_SHARED_MEDIA_CODEC_BRIDGE_ERADICATOR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "starboard/android/shared/media_settings_api.h" | ||
#include "starboard/android/shared/media_codec_bridge_eradicator.h" | ||
#include "starboard/extension/media_settings.h" | ||
|
||
namespace starboard { | ||
namespace android { | ||
namespace shared { | ||
|
||
namespace { | ||
|
||
// Definitions of any functions included as components in the extension | ||
// are added here. | ||
|
||
void EnableAsyncReleaseMediaCodecBridge(bool value) { | ||
MediaCodecBridgeEradicator::GetInstance()->SetEnabled(value); | ||
} | ||
|
||
const StarboardExtensionMediaSettingsApi kMediaSettingsApi = { | ||
kStarboardExtensionMediaSettingsName, | ||
1, // API version that's implemented. | ||
&EnableAsyncReleaseMediaCodecBridge, | ||
}; | ||
|
||
} // namespace | ||
|
||
const void* GetMediaSettingsApi() { | ||
return &kMediaSettingsApi; | ||
} | ||
|
||
} // namespace shared | ||
} // namespace android | ||
} // namespace starboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 The Cobalt Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef STARBOARD_ANDROID_SHARED_MEDIA_SETTINGS_API_H_ | ||
#define STARBOARD_ANDROID_SHARED_MEDIA_SETTINGS_API_H_ | ||
|
||
namespace starboard { | ||
namespace android { | ||
namespace shared { | ||
|
||
const void* GetMediaSettingsApi(); | ||
|
||
} // namespace shared | ||
} // namespace android | ||
} // namespace starboard | ||
|
||
#endif // STARBOARD_ANDROID_SHARED_MEDIA_SETTINGS_API_H_ |
Oops, something went wrong.