From 06257139f348deaa7bcedb148b113855f5a6b1af Mon Sep 17 00:00:00 2001 From: cobalt-github-releaser-bot <95661244+cobalt-github-releaser-bot@users.noreply.github.com> Date: Wed, 8 May 2024 10:14:18 -0700 Subject: [PATCH] Cherry pick PR #3170: Migrate android media threads to pthread_create (#3178) Refer to the original PR: https://github.com/youtube/cobalt/pull/3170 b/302335657 Change-Id: I900eb574cf0486a70d55dfa085c06838da4608e0 Co-authored-by: Yavor Goulishev --- .../audio_sink_min_required_frames_tester.cc | 23 +++++++------ .../audio_sink_min_required_frames_tester.h | 5 +-- .../shared/audio_track_audio_sink_type.cc | 15 +++++---- .../shared/audio_track_audio_sink_type.h | 5 +-- starboard/android/shared/media_decoder.cc | 33 ++++++++++--------- starboard/android/shared/media_decoder.h | 2 +- 6 files changed, 47 insertions(+), 36 deletions(-) diff --git a/starboard/android/shared/audio_sink_min_required_frames_tester.cc b/starboard/android/shared/audio_sink_min_required_frames_tester.cc index 4ad10fcc92d4..36031f6148ab 100644 --- a/starboard/android/shared/audio_sink_min_required_frames_tester.cc +++ b/starboard/android/shared/audio_sink_min_required_frames_tester.cc @@ -18,6 +18,7 @@ #include #include "starboard/android/shared/audio_track_audio_sink_type.h" +#include "starboard/shared/pthread/thread_create_priority.h" namespace starboard { namespace android { @@ -52,13 +53,13 @@ MinRequiredFramesTester::MinRequiredFramesTester(int max_required_frames, MinRequiredFramesTester::~MinRequiredFramesTester() { SB_DCHECK(thread_checker_.CalledOnValidThread()); destroying_.store(true); - if (SbThreadIsValid(tester_thread_)) { + if (tester_thread_ != 0) { { ScopedLock scoped_lock(mutex_); condition_variable_.Signal(); } - SbThreadJoin(tester_thread_, NULL); - tester_thread_ = kSbThreadInvalid; + pthread_join(tester_thread_, NULL); + tester_thread_ = 0; } } @@ -70,7 +71,7 @@ void MinRequiredFramesTester::AddTest( int default_required_frames) { SB_DCHECK(thread_checker_.CalledOnValidThread()); // MinRequiredFramesTester doesn't support to add test after starts. - SB_DCHECK(!SbThreadIsValid(tester_thread_)); + SB_DCHECK(tester_thread_ == 0); test_tasks_.emplace_back(number_of_channels, sample_type, sample_rate, received_cb, default_required_frames); @@ -79,17 +80,19 @@ void MinRequiredFramesTester::AddTest( void MinRequiredFramesTester::Start() { SB_DCHECK(thread_checker_.CalledOnValidThread()); // MinRequiredFramesTester only supports to start once. - SB_DCHECK(!SbThreadIsValid(tester_thread_)); + SB_DCHECK(tester_thread_ == 0); - tester_thread_ = - SbThreadCreate(0, kSbThreadPriorityLowest, kSbThreadNoAffinity, true, - "audio_track_tester", - &MinRequiredFramesTester::TesterThreadEntryPoint, this); - SB_DCHECK(SbThreadIsValid(tester_thread_)); + pthread_create(&tester_thread_, nullptr, + &MinRequiredFramesTester::TesterThreadEntryPoint, this); + SB_DCHECK(tester_thread_ != 0); } // static void* MinRequiredFramesTester::TesterThreadEntryPoint(void* context) { + ::starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityLowest); + + pthread_setname_np(pthread_self(), "audio_track_tester"); + SB_DCHECK(context); MinRequiredFramesTester* tester = static_cast(context); diff --git a/starboard/android/shared/audio_sink_min_required_frames_tester.h b/starboard/android/shared/audio_sink_min_required_frames_tester.h index 446b78da9bba..f931e370427e 100644 --- a/starboard/android/shared/audio_sink_min_required_frames_tester.h +++ b/starboard/android/shared/audio_sink_min_required_frames_tester.h @@ -15,6 +15,8 @@ #ifndef STARBOARD_ANDROID_SHARED_AUDIO_SINK_MIN_REQUIRED_FRAMES_TESTER_H_ #define STARBOARD_ANDROID_SHARED_AUDIO_SINK_MIN_REQUIRED_FRAMES_TESTER_H_ +#include + #include #include #include @@ -24,7 +26,6 @@ #include "starboard/common/mutex.h" #include "starboard/media.h" #include "starboard/shared/starboard/thread_checker.h" -#include "starboard/thread.h" namespace starboard { namespace android { @@ -116,7 +117,7 @@ class MinRequiredFramesTester { Mutex mutex_; ConditionVariable condition_variable_; - SbThread tester_thread_ = kSbThreadInvalid; + pthread_t tester_thread_ = 0; std::atomic_bool destroying_; }; diff --git a/starboard/android/shared/audio_track_audio_sink_type.cc b/starboard/android/shared/audio_track_audio_sink_type.cc index 459c7b6a95d8..9788b5fb375d 100644 --- a/starboard/android/shared/audio_track_audio_sink_type.cc +++ b/starboard/android/shared/audio_track_audio_sink_type.cc @@ -22,6 +22,7 @@ #include "starboard/android/shared/media_capabilities_cache.h" #include "starboard/common/string.h" #include "starboard/common/time.h" +#include "starboard/shared/pthread/thread_create_priority.h" #include "starboard/shared/starboard/media/media_util.h" #include "starboard/shared/starboard/player/filter/common.h" @@ -161,17 +162,16 @@ AudioTrackAudioSink::AudioTrackAudioSink( return; } - audio_out_thread_ = SbThreadCreate( - 0, kSbThreadPriorityRealTime, kSbThreadNoAffinity, true, - "audio_track_audio_out", &AudioTrackAudioSink::ThreadEntryPoint, this); - SB_DCHECK(SbThreadIsValid(audio_out_thread_)); + pthread_create(&audio_out_thread_, nullptr, + &AudioTrackAudioSink::ThreadEntryPoint, this); + SB_DCHECK(audio_out_thread_ != 0); } AudioTrackAudioSink::~AudioTrackAudioSink() { quit_ = true; - if (SbThreadIsValid(audio_out_thread_)) { - SbThreadJoin(audio_out_thread_, NULL); + if (audio_out_thread_ != 0) { + pthread_join(audio_out_thread_, NULL); } } @@ -188,7 +188,10 @@ void AudioTrackAudioSink::SetPlaybackRate(double playback_rate) { // static void* AudioTrackAudioSink::ThreadEntryPoint(void* context) { + pthread_setname_np(pthread_self(), "audio_track_audio_out"); SB_DCHECK(context); + ::starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityRealTime); + AudioTrackAudioSink* sink = reinterpret_cast(context); sink->AudioThreadFunc(); diff --git a/starboard/android/shared/audio_track_audio_sink_type.h b/starboard/android/shared/audio_track_audio_sink_type.h index 5d16f2a68651..1d2d8c16bf8e 100644 --- a/starboard/android/shared/audio_track_audio_sink_type.h +++ b/starboard/android/shared/audio_track_audio_sink_type.h @@ -15,6 +15,8 @@ #ifndef STARBOARD_ANDROID_SHARED_AUDIO_TRACK_AUDIO_SINK_TYPE_H_ #define STARBOARD_ANDROID_SHARED_AUDIO_TRACK_AUDIO_SINK_TYPE_H_ +#include + #include #include #include @@ -32,7 +34,6 @@ #include "starboard/configuration.h" #include "starboard/shared/internal_only.h" #include "starboard/shared/starboard/audio_sink/audio_sink_internal.h" -#include "starboard/thread.h" namespace starboard { namespace android { @@ -157,7 +158,7 @@ class AudioTrackAudioSink : public SbAudioSinkPrivate { int last_playback_head_position_ = 0; volatile bool quit_ = false; - SbThread audio_out_thread_ = kSbThreadInvalid; + pthread_t audio_out_thread_ = 0; Mutex mutex_; double playback_rate_ = 1.0; diff --git a/starboard/android/shared/media_decoder.cc b/starboard/android/shared/media_decoder.cc index 6953a83e41c0..5411792745c2 100644 --- a/starboard/android/shared/media_decoder.cc +++ b/starboard/android/shared/media_decoder.cc @@ -153,9 +153,9 @@ MediaDecoder::~MediaDecoder() { condition_variable_.Signal(); } - if (SbThreadIsValid(decoder_thread_)) { - SbThreadJoin(decoder_thread_, NULL); - decoder_thread_ = kSbThreadInvalid; + if (decoder_thread_ != 0) { + pthread_join(decoder_thread_, NULL); + decoder_thread_ = 0; } if (is_valid()) { @@ -197,14 +197,10 @@ void MediaDecoder::WriteInputBuffers(const InputBuffers& input_buffers) { return; } - if (!SbThreadIsValid(decoder_thread_)) { - decoder_thread_ = SbThreadCreate( - 0, - media_type_ == kSbMediaTypeAudio ? kSbThreadPriorityNormal - : kSbThreadPriorityHigh, - kSbThreadNoAffinity, true, GetDecoderName(media_type_), - &MediaDecoder::DecoderThreadEntryPoint, this); - SB_DCHECK(SbThreadIsValid(decoder_thread_)); + if (decoder_thread_ == 0) { + pthread_create(&decoder_thread_, nullptr, + &MediaDecoder::DecoderThreadEntryPoint, this); + SB_DCHECK(decoder_thread_ != 0); } ScopedLock scoped_lock(mutex_); @@ -240,6 +236,13 @@ void MediaDecoder::SetPlaybackRate(double playback_rate) { void* MediaDecoder::DecoderThreadEntryPoint(void* context) { SB_DCHECK(context); MediaDecoder* decoder = static_cast(context); + pthread_setname_np(pthread_self(), GetDecoderName(decoder->media_type_)); + if (decoder->media_type_ == kSbMediaTypeAudio) { + ::starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityNormal); + } else { + ::starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityHigh); + } + decoder->DecoderThreadFunc(); return NULL; } @@ -626,7 +629,7 @@ void MediaDecoder::OnMediaCodecOutputBufferAvailable( // TODO(b/291959069): After |decoder_thread_| is destroyed, it may still // receive output buffer, discard this invalid output buffer. - if (destroying_.load() || !SbThreadIsValid(decoder_thread_)) { + if (destroying_.load() || decoder_thread_ == 0) { return; } @@ -672,9 +675,9 @@ bool MediaDecoder::Flush() { ScopedLock scoped_lock(mutex_); condition_variable_.Signal(); } - if (SbThreadIsValid(decoder_thread_)) { - SbThreadJoin(decoder_thread_, NULL); - decoder_thread_ = kSbThreadInvalid; + if (decoder_thread_ != 0) { + pthread_join(decoder_thread_, NULL); + decoder_thread_ = 0; } // 2. Flush()/Start() |media_codec_bridge_| and clean up pending tasks. diff --git a/starboard/android/shared/media_decoder.h b/starboard/android/shared/media_decoder.h index 2ec479401f1e..8475f31857ea 100644 --- a/starboard/android/shared/media_decoder.h +++ b/starboard/android/shared/media_decoder.h @@ -205,7 +205,7 @@ class MediaDecoder bool first_call_on_handler_thread_ = true; // Working thread to avoid lengthy decoding work block the player thread. - SbThread decoder_thread_ = kSbThreadInvalid; + pthread_t decoder_thread_ = 0; scoped_ptr media_codec_bridge_; };