Skip to content

Commit

Permalink
Cherry pick PR #3170: Migrate android media threads to pthread_create (
Browse files Browse the repository at this point in the history
…#3178)

Refer to the original PR: #3170

b/302335657

Change-Id: I900eb574cf0486a70d55dfa085c06838da4608e0

Co-authored-by: Yavor Goulishev <[email protected]>
  • Loading branch information
cobalt-github-releaser-bot and y4vor authored May 8, 2024
1 parent be6c323 commit 0625713
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
23 changes: 13 additions & 10 deletions starboard/android/shared/audio_sink_min_required_frames_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <vector>

#include "starboard/android/shared/audio_track_audio_sink_type.h"
#include "starboard/shared/pthread/thread_create_priority.h"

namespace starboard {
namespace android {
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
Expand All @@ -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<MinRequiredFramesTester*>(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pthread.h>

#include <atomic>
#include <functional>
#include <string>
Expand All @@ -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 {
Expand Down Expand Up @@ -116,7 +117,7 @@ class MinRequiredFramesTester {

Mutex mutex_;
ConditionVariable condition_variable_;
SbThread tester_thread_ = kSbThreadInvalid;
pthread_t tester_thread_ = 0;
std::atomic_bool destroying_;
};

Expand Down
15 changes: 9 additions & 6 deletions starboard/android/shared/audio_track_audio_sink_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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<AudioTrackAudioSink*>(context);
sink->AudioThreadFunc();

Expand Down
5 changes: 3 additions & 2 deletions starboard/android/shared/audio_track_audio_sink_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pthread.h>

#include <atomic>
#include <functional>
#include <map>
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 18 additions & 15 deletions starboard/android/shared/media_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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_);
Expand Down Expand Up @@ -240,6 +236,13 @@ void MediaDecoder::SetPlaybackRate(double playback_rate) {
void* MediaDecoder::DecoderThreadEntryPoint(void* context) {
SB_DCHECK(context);
MediaDecoder* decoder = static_cast<MediaDecoder*>(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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion starboard/android/shared/media_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MediaCodecBridge> media_codec_bridge_;
};

Expand Down

0 comments on commit 0625713

Please sign in to comment.