Skip to content

Commit

Permalink
Add ScopedJobThreadPtr class to be used with JobThread
Browse files Browse the repository at this point in the history
b/372515171
  • Loading branch information
alexanderbobrovnik committed Oct 22, 2024
1 parent a337e43 commit 30e7e60
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion starboard/android/shared/audio_renderer_passthrough.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class AudioRendererPassthrough
// ensure the thread completes all tasks before |audio_track_bridge_| is
// invalidated.
std::unique_ptr<AudioTrackBridge> audio_track_bridge_;
std::unique_ptr<JobThread> audio_track_thread_;
JobThread::ScopedJobThreadPtr audio_track_thread_;
};

} // namespace shared
Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/libaom/aom_video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void VideoDecoder::Reset() {
SB_DCHECK(BelongsToCurrentThread());

if (decoder_thread_) {
decoder_thread_->job_queue()->ScheduleAndWait(
decoder_thread_->job_queue()->Schedule(
std::bind(&VideoDecoder::TeardownCodec, this));

// Join the thread to ensure that all callbacks in process are finished.
Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/libaom/aom_video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class VideoDecoder : public starboard::player::filter::VideoDecoder,
bool error_occurred_;

// Working thread to avoid lengthy decoding work block the player thread.
std::unique_ptr<starboard::player::JobThread> decoder_thread_;
starboard::player::JobThread::ScopedJobThreadPtr decoder_thread_;

// Decode-to-texture related state.
SbPlayerOutputMode output_mode_;
Expand Down
5 changes: 2 additions & 3 deletions starboard/shared/libdav1d/dav1d_video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ void VideoDecoder::Reset() {
SB_DCHECK(BelongsToCurrentThread());

if (decoder_thread_) {
// Wait to ensure all tasks are done before decoder_thread_ reset.
decoder_thread_->job_queue()->ScheduleAndWait(
decoder_thread_->job_queue()->Schedule(
std::bind(&VideoDecoder::TeardownCodec, this));

// Join the thread to ensure that all callbacks in process are finished.
decoder_thread_.reset();
}

Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/libdav1d/dav1d_video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class VideoDecoder : public starboard::player::filter::VideoDecoder,
bool stream_ended_ = false;

// Working thread to avoid lengthy decoding work block the player thread.
std::unique_ptr<starboard::player::JobThread> decoder_thread_;
starboard::player::JobThread::ScopedJobThreadPtr decoder_thread_;

// Decode-to-texture related state.
const SbPlayerOutputMode output_mode_;
Expand Down
5 changes: 2 additions & 3 deletions starboard/shared/libde265/de265_video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ void VideoDecoder::Reset() {
SB_DCHECK(BelongsToCurrentThread());

if (decoder_thread_) {
// Wait to ensure all tasks are done before decoder_thread_ reset.
decoder_thread_->job_queue()->ScheduleAndWait(
decoder_thread_->job_queue()->Schedule(
std::bind(&VideoDecoder::TeardownCodec, this));

// Join the thread to ensure that all callbacks in process are finished.
decoder_thread_.reset();
}

Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/libde265/de265_video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class VideoDecoder : public starboard::player::filter::VideoDecoder,
bool error_occurred_ = false;

// Working thread to avoid lengthy decoding work block the player thread.
std::unique_ptr<starboard::player::JobThread> decoder_thread_;
starboard::player::JobThread::ScopedJobThreadPtr decoder_thread_;

// Decode-to-texture related state.
SbPlayerOutputMode output_mode_;
Expand Down
5 changes: 2 additions & 3 deletions starboard/shared/libvpx/vpx_video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ void VideoDecoder::Reset() {
SB_DCHECK(BelongsToCurrentThread());

if (decoder_thread_) {
// Wait to ensure all tasks are done before decoder_thread_ reset.
decoder_thread_->job_queue()->ScheduleAndWait(
decoder_thread_->job_queue()->Schedule(
std::bind(&VideoDecoder::TeardownCodec, this));

// Join the thread to ensure that all callbacks in process are finished.
decoder_thread_.reset();
}

Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/libvpx/vpx_video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class VideoDecoder : public starboard::player::filter::VideoDecoder,
bool error_occurred_;

// Working thread to avoid lengthy decoding work block the player thread.
std::unique_ptr<starboard::player::JobThread> decoder_thread_;
starboard::player::JobThread::ScopedJobThreadPtr decoder_thread_;

// Decode-to-texture related state.
SbPlayerOutputMode output_mode_;
Expand Down
4 changes: 2 additions & 2 deletions starboard/shared/openh264/openh264_video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ void VideoDecoder::Reset() {
SB_DCHECK(BelongsToCurrentThread());

if (decoder_thread_) {
// Wait to ensure all tasks are done before decoder_thread_ reset.
decoder_thread_->job_queue()->ScheduleAndWait(
decoder_thread_->job_queue()->Schedule(
std::bind(&VideoDecoder::TeardownCodec, this));
// Join the thread to ensure that all callbacks in process are finished.
decoder_thread_.reset();
}

Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/openh264/openh264_video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class VideoDecoder : public starboard::player::filter::VideoDecoder,
Mutex decode_target_mutex_;

// Working thread to avoid lengthy decoding work block the player thread.
std::unique_ptr<starboard::player::JobThread> decoder_thread_;
starboard::player::JobThread::ScopedJobThreadPtr decoder_thread_;

// Openh264 decode handler.
ISVCDecoder* decoder_ = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class StubAudioDecoder : public AudioDecoder, private JobQueue::JobOwner {
OutputCB output_cb_;
ErrorCB error_cb_;

std::unique_ptr<starboard::player::JobThread> decoder_thread_;
starboard::player::JobThread::ScopedJobThreadPtr decoder_thread_;
Mutex decoded_audios_mutex_;
std::queue<scoped_refptr<DecodedAudio> > decoded_audios_;
scoped_refptr<InputBuffer> last_input_buffer_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class StubVideoDecoder : public VideoDecoder, private JobQueue::JobOwner {
DecoderStatusCB decoder_status_cb_;
media::VideoStreamInfo video_stream_info_;

std::unique_ptr<starboard::player::JobThread> decoder_thread_;
starboard::player::JobThread::ScopedJobThreadPtr decoder_thread_;
// std::set<> keeps frame timestamps sorted in ascending order.
std::set<int64_t> output_frame_timestamps_;
// Used to determine when to send kBufferFull in DecodeOneBuffer().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const int kJobThreadStackSize = 0;
std::unique_ptr<VideoDmpReader> s_video_dmp_reader;
std::unique_ptr<PlayerComponents> s_player_components;
int s_audio_sample_index;
std::unique_ptr<JobThread> s_job_thread;
JobThread::ScopedJobThreadPtr s_job_thread;
int64_t s_duration;

static void DeallocateSampleFunc(SbPlayer player,
Expand Down
29 changes: 27 additions & 2 deletions starboard/shared/starboard/player/job_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,35 @@ namespace player {
// This class implements a thread that holds a JobQueue.
class JobThread {
public:
class ScopedJobThreadPtr {
public:
ScopedJobThreadPtr(JobThread* p = nullptr): job_thread_(p) {
}

~ScopedJobThreadPtr() {
delete job_thread_;
}

void reset(JobThread* p = nullptr) {
delete job_thread_;
job_thread_ = p;
}

JobThread* operator->() const {
return job_thread_;
}

operator bool() const {
return job_thread_ != nullptr;
}

private:
JobThread* job_thread_;
};

explicit JobThread(const char* thread_name,
int64_t stack_size = 0,
SbThreadPriority priority = kSbThreadPriorityNormal);
~JobThread();

JobQueue* job_queue() { return job_queue_.get(); }
const JobQueue* job_queue() const { return job_queue_.get(); }
Expand Down Expand Up @@ -83,8 +108,8 @@ class JobThread {

return job_queue_->RemoveJobByToken(job_token);
}

private:
~JobThread();
static void* ThreadEntryPoint(void* context);
void RunLoop();

Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/uwp/wasapi_audio_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class WASAPIAudioSink {
Mutex output_frames_mutex_;
std::queue<scoped_refptr<DecodedAudio>> pending_decoded_audios_;

std::unique_ptr<JobThread> job_thread_;
JobThread::ScopedJobThreadPtr job_thread_;

starboard::ThreadChecker thread_checker_;
};
Expand Down
12 changes: 6 additions & 6 deletions starboard/shared/win32/audio_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class XAudioAudioSinkType : public SbAudioSinkPrivate::Type,
sink->StopCallbacks();
}

restart_audio_thread_.job_queue()->Schedule(
restart_audio_thread_->job_queue()->Schedule(
std::bind(&XAudioAudioSinkType::RestartAudioDevice, this));
}

Expand Down Expand Up @@ -251,7 +251,7 @@ class XAudioAudioSinkType : public SbAudioSinkPrivate::Type,
std::list<XAudioAudioSink*> audio_sinks_on_xaudio_callbacks_;
starboard::ThreadChecker thread_checker_;

starboard::player::JobThread restart_audio_thread_;
starboard::player::JobThread::ScopedJobThreadPtr restart_audio_thread_;
std::list<XAudioAudioSink*> audio_sinks_to_restart_;
bool sink_shutdown_in_progress_;
};
Expand Down Expand Up @@ -383,7 +383,7 @@ void XAudioAudioSink::SubmitSourceBuffer(int offset_in_frames,
}

XAudioAudioSinkType::XAudioAudioSinkType()
: restart_audio_thread_("RestartAudioDevice"),
: restart_audio_thread_(new starboard::player::JobThread("RestartAudioDevice")),
sink_shutdown_in_progress_(false),
thread_checker_(starboard::ThreadChecker::kSetThreadIdOnFirstCheck) {
x_audio2_ = XAudioCreate();
Expand Down Expand Up @@ -537,7 +537,7 @@ void XAudioAudioSinkType::TryAcquireSinkVoices() {
if (!x_audio2_) {
hr = XAudio2Create(&x_audio2_, 0, XAUDIO2_DEFAULT_PROCESSOR);
if (FAILED(hr)) {
restart_audio_thread_.job_queue()->Schedule(
restart_audio_thread_->job_queue()->Schedule(
std::bind(&XAudioAudioSinkType::TryAcquireSinkVoices, this));
return;
}
Expand All @@ -547,7 +547,7 @@ void XAudioAudioSinkType::TryAcquireSinkVoices() {
if (!mastering_voice_) {
hr = x_audio2_->CreateMasteringVoice(&mastering_voice_);
if (FAILED(hr)) {
restart_audio_thread_.job_queue()->Schedule(
restart_audio_thread_->job_queue()->Schedule(
std::bind(&XAudioAudioSinkType::TryAcquireSinkVoices, this));
return;
}
Expand All @@ -570,7 +570,7 @@ void XAudioAudioSinkType::TryAcquireSinkVoices() {
if (audio_sinks_to_restart_.empty()) {
return;
}
restart_audio_thread_.job_queue()->Schedule(
restart_audio_thread_->job_queue()->Schedule(
std::bind(&XAudioAudioSinkType::TryAcquireSinkVoices, this));
}

Expand Down
2 changes: 1 addition & 1 deletion starboard/xb1/shared/gpu_base_video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ void GpuVideoDecoderBase::Reset() {
if (decoder_thread_) {
// Release stored frames to free frame buffers.
decoder_status_cb_(kReleaseAllFrames, nullptr);
decoder_thread_->job_queue()->ScheduleAndWait(
decoder_thread_->job_queue()->Schedule(
std::bind(&GpuVideoDecoderBase::DrainDecoder, this));
decoder_thread_.reset();
}
Expand Down
2 changes: 1 addition & 1 deletion starboard/xb1/shared/gpu_base_video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class GpuVideoDecoderBase
void* egl_display_ = nullptr;
DecodeTargetContextRunner decode_target_context_runner_;

std::unique_ptr<starboard::shared::starboard::player::JobThread>
starboard::shared::starboard::player::JobThread::ScopedJobThreadPtr
decoder_thread_;

// |pending_inputs_| is shared between player main thread and decoder thread.
Expand Down

0 comments on commit 30e7e60

Please sign in to comment.