Skip to content

Commit

Permalink
Migrate starboard impl code to use pthread_create (#3174)
Browse files Browse the repository at this point in the history
b/302335657

Change-Id: I607c76ce02a24ac0ef9099a45e0da845f7d55798
  • Loading branch information
y4vor committed May 10, 2024
1 parent d3e5bed commit db41af7
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 86 deletions.
18 changes: 10 additions & 8 deletions starboard/raspi/shared/open_max/video_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include <unistd.h>

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

namespace starboard {
namespace raspi {
namespace shared {
Expand All @@ -34,20 +36,20 @@ const int64_t kUpdateIntervalUsec = 5'000;
VideoDecoder::VideoDecoder(SbMediaVideoCodec video_codec)
: resource_pool_(new DispmanxResourcePool(kResourcePoolSize)),
eos_written_(false),
thread_(kSbThreadInvalid),
thread_(0),
request_thread_termination_(false) {
SB_DCHECK(video_codec == kSbMediaVideoCodecH264);
update_job_ = std::bind(&VideoDecoder::Update, this);
update_job_token_ = Schedule(update_job_, kUpdateIntervalUsec);
}

VideoDecoder::~VideoDecoder() {
if (SbThreadIsValid(thread_)) {
if (thread_ != 0) {
{
ScopedLock scoped_lock(mutex_);
request_thread_termination_ = true;
}
SbThreadJoin(thread_, NULL);
pthread_join(thread_, NULL);
}
RemoveJobByToken(update_job_token_);
}
Expand All @@ -62,11 +64,9 @@ void VideoDecoder::Initialize(const DecoderStatusCB& decoder_status_cb,
decoder_status_cb_ = decoder_status_cb;
error_cb_ = error_cb;

SB_DCHECK(!SbThreadIsValid(thread_));
thread_ = SbThreadCreate(0, kSbThreadPriorityHigh, kSbThreadNoAffinity, true,
"omx_video_decoder", &VideoDecoder::ThreadEntryPoint,
this);
SB_DCHECK(SbThreadIsValid(thread_));
SB_DCHECK(thread_ == 0);
pthread_create(&thread_, nullptr, &VideoDecoder::ThreadEntryPoint, this);
SB_DCHECK(thread_ != 0);
}

void VideoDecoder::WriteInputBuffers(const InputBuffers& input_buffers) {
Expand Down Expand Up @@ -134,6 +134,8 @@ bool VideoDecoder::TryToDeliverOneFrame() {

// static
void* VideoDecoder::ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "omx_video_decoder");
::starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityHigh);
VideoDecoder* decoder = reinterpret_cast<VideoDecoder*>(context);
decoder->RunLoop();
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion starboard/raspi/shared/open_max/video_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class VideoDecoder
bool eos_written_;
bool first_input_written_ = false;

SbThread thread_;
pthread_t thread_;
bool request_thread_termination_;
Queue<Event*> queue_;

Expand Down
18 changes: 10 additions & 8 deletions starboard/shared/alsa/alsa_audio_sink_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <alsa/asoundlib.h>

#include <pthread.h>
#include <unistd.h>

#include <algorithm>
Expand All @@ -29,7 +30,7 @@
#include "starboard/configuration.h"
#include "starboard/memory.h"
#include "starboard/shared/alsa/alsa_util.h"
#include "starboard/thread.h"
#include "starboard/shared/pthread/thread_create_priority.h"

namespace starboard {
namespace shared {
Expand Down Expand Up @@ -141,7 +142,7 @@ class AlsaAudioSink : public SbAudioSinkPrivate {
int sampling_frequency_hz_;
SbMediaAudioSampleType sample_type_;

SbThread audio_out_thread_;
pthread_t audio_out_thread_;
starboard::Mutex mutex_;
starboard::ConditionVariable creation_signal_;

Expand Down Expand Up @@ -177,7 +178,7 @@ AlsaAudioSink::AlsaAudioSink(
update_source_status_func_(update_source_status_func),
consume_frames_func_(consume_frames_func),
context_(context),
audio_out_thread_(kSbThreadInvalid),
audio_out_thread_(0),
creation_signal_(mutex_),
time_to_wait_(kFramesPerRequest * 1'000'000LL / sampling_frequency_hz /
2),
Expand All @@ -196,10 +197,9 @@ AlsaAudioSink::AlsaAudioSink(
channels * kFramesPerRequest * GetSampleSize(sample_type));

ScopedLock lock(mutex_);
audio_out_thread_ =
SbThreadCreate(0, kSbThreadPriorityRealTime, kSbThreadNoAffinity, true,
"alsa_audio_out", &AlsaAudioSink::ThreadEntryPoint, this);
SB_DCHECK(SbThreadIsValid(audio_out_thread_));
pthread_create(&audio_out_thread_, nullptr, &AlsaAudioSink::ThreadEntryPoint,
this);
SB_DCHECK(audio_out_thread_ != 0);
creation_signal_.Wait();
}

Expand All @@ -208,13 +208,15 @@ AlsaAudioSink::~AlsaAudioSink() {
ScopedLock lock(mutex_);
destroying_ = true;
}
SbThreadJoin(audio_out_thread_, NULL);
pthread_join(audio_out_thread_, NULL);

delete[] static_cast<uint8_t*>(silence_frames_);
}

// static
void* AlsaAudioSink::ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "alsa_audio_out");
starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityRealTime);
SB_DCHECK(context);
AlsaAudioSink* sink = reinterpret_cast<AlsaAudioSink*>(context);
sink->AudioThreadFunc();
Expand Down
23 changes: 12 additions & 11 deletions starboard/shared/ffmpeg/ffmpeg_video_decoder_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "starboard/common/string.h"
#include "starboard/linux/shared/decode_target_internal.h"
#include "starboard/thread.h"
#include "starboard/shared/pthread/thread_create_priority.h"

namespace starboard {
namespace shared {
Expand Down Expand Up @@ -111,7 +111,7 @@ VideoDecoderImpl<FFMPEG>::VideoDecoderImpl(
av_frame_(NULL),
stream_ended_(false),
error_occurred_(false),
decoder_thread_(kSbThreadInvalid),
decoder_thread_(0),
output_mode_(output_mode),
decode_target_graphics_context_provider_(
decode_target_graphics_context_provider),
Expand Down Expand Up @@ -165,11 +165,10 @@ void VideoDecoderImpl<FFMPEG>::WriteInputBuffers(
return;
}

if (!SbThreadIsValid(decoder_thread_)) {
decoder_thread_ = SbThreadCreate(
0, kSbThreadPriorityHigh, kSbThreadNoAffinity, true, "ff_video_dec",
&VideoDecoderImpl<FFMPEG>::ThreadEntryPoint, this);
SB_DCHECK(SbThreadIsValid(decoder_thread_));
if (decoder_thread_ == 0) {
pthread_create(&decoder_thread_, nullptr,
&VideoDecoderImpl<FFMPEG>::ThreadEntryPoint, this);
SB_DCHECK(decoder_thread_ != 0);
}
queue_.Put(Event(input_buffer));
}
Expand All @@ -181,7 +180,7 @@ void VideoDecoderImpl<FFMPEG>::WriteEndOfStream() {
// Decode() is not called when the stream is ended.
stream_ended_ = true;

if (!SbThreadIsValid(decoder_thread_)) {
if (decoder_thread_ == 0) {
// In case there is no WriteInputBuffers() call before WriteEndOfStream(),
// don't create the decoder thread and send the EOS frame directly.
decoder_status_cb_(kBufferFull, VideoFrame::CreateEOSFrame());
Expand All @@ -193,16 +192,16 @@ void VideoDecoderImpl<FFMPEG>::WriteEndOfStream() {

void VideoDecoderImpl<FFMPEG>::Reset() {
// Join the thread to ensure that all callbacks in process are finished.
if (SbThreadIsValid(decoder_thread_)) {
if (decoder_thread_ != 0) {
queue_.Put(Event(kReset));
SbThreadJoin(decoder_thread_, NULL);
pthread_join(decoder_thread_, NULL);
}

if (codec_context_ != NULL) {
ffmpeg_->avcodec_flush_buffers(codec_context_);
}

decoder_thread_ = kSbThreadInvalid;
decoder_thread_ = 0;
stream_ended_ = false;

if (output_mode_ == kSbPlayerOutputModeDecodeToTexture) {
Expand All @@ -221,6 +220,8 @@ bool VideoDecoderImpl<FFMPEG>::is_valid() const {

// static
void* VideoDecoderImpl<FFMPEG>::ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "ff_video_dec");
shared::pthread::ThreadSetPriority(kSbThreadPriorityHigh);
SB_DCHECK(context);
VideoDecoderImpl<FFMPEG>* decoder =
reinterpret_cast<VideoDecoderImpl<FFMPEG>*>(context);
Expand Down
5 changes: 3 additions & 2 deletions starboard/shared/ffmpeg/ffmpeg_video_decoder_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef STARBOARD_SHARED_FFMPEG_FFMPEG_VIDEO_DECODER_IMPL_H_
#define STARBOARD_SHARED_FFMPEG_FFMPEG_VIDEO_DECODER_IMPL_H_

#include <pthread.h>

#include <queue>

#include "starboard/common/log.h"
Expand All @@ -29,7 +31,6 @@
#include "starboard/shared/starboard/player/filter/cpu_video_frame.h"
#include "starboard/shared/starboard/player/filter/video_decoder_internal.h"
#include "starboard/shared/starboard/player/input_buffer_internal.h"
#include "starboard/thread.h"

namespace starboard {
namespace shared {
Expand Down Expand Up @@ -134,7 +135,7 @@ class VideoDecoderImpl<FFMPEG> : public VideoDecoder {
bool error_occurred_;

// Working thread to avoid lengthy decoding work block the player thread.
SbThread decoder_thread_;
pthread_t decoder_thread_;

// Decode-to-texture related state.
SbPlayerOutputMode output_mode_;
Expand Down
22 changes: 17 additions & 5 deletions starboard/shared/linux/system_network_status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "starboard/common/log.h"
#include "starboard/shared/linux/singleton.h"
#include "starboard/shared/linux/system_network_status.h"
#include "starboard/shared/pthread/thread_create_priority.h"
#include "starboard/shared/starboard/application.h"
#include "starboard/system.h"

Expand Down Expand Up @@ -96,15 +97,26 @@ bool GetOnlineStatus(bool* is_online_ptr, int netlink_fd) {
} // namespace

bool NetworkNotifier::Initialize() {
SB_DCHECK(!SbThreadIsValid(notifier_thread_));
notifier_thread_ = SbThreadCreate(
0, kSbThreadPriorityLow, kSbThreadNoAffinity, false, "NetworkNotifier",
&NetworkNotifier::NotifierThreadEntry, this);
SB_DCHECK(SbThreadIsValid(notifier_thread_));
SB_DCHECK(notifier_thread_ == 0);

pthread_attr_t attributes;
int result = pthread_attr_init(&attributes);
if (result != 0) {
return false;
}

pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
pthread_create(&notifier_thread_, &attributes,
&NetworkNotifier::NotifierThreadEntry, this);
pthread_attr_destroy(&attributes);

SB_DCHECK(notifier_thread_ != 0);
return true;
}

void* NetworkNotifier::NotifierThreadEntry(void* context) {
pthread_setname_np(pthread_self(), "NetworkNotifier");
starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityLow);
auto* notifier = static_cast<NetworkNotifier*>(context);
int netlink_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
bool is_online;
Expand Down
3 changes: 1 addition & 2 deletions starboard/shared/linux/system_network_status.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include "starboard/shared/linux/singleton.h"
#include "starboard/system.h"
#include "starboard/thread.h"

class NetworkNotifier : public starboard::Singleton<NetworkNotifier> {
public:
Expand All @@ -29,7 +28,7 @@ class NetworkNotifier : public starboard::Singleton<NetworkNotifier> {
void set_online(bool is_online) { is_online_ = is_online; }

private:
SbThread notifier_thread_;
pthread_t notifier_thread_;
bool is_online_ = true;
};

Expand Down
23 changes: 14 additions & 9 deletions starboard/shared/pulse/pulse_audio_sink_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <pulse/pulseaudio.h>

#include <pthread.h>
#include <unistd.h>

#include <algorithm>
Expand All @@ -27,6 +28,7 @@
#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
#include "starboard/common/time.h"
#include "starboard/shared/pthread/thread_create_priority.h"
#include "starboard/shared/pulse/pulse_dynamic_load_dispatcher.h"
#include "starboard/shared/starboard/audio_sink/audio_sink_internal.h"
#include "starboard/shared/starboard/media/media_util.h"
Expand Down Expand Up @@ -163,7 +165,7 @@ class PulseAudioSinkType : public SbAudioSinkPrivate::Type {
pa_mainloop* mainloop_ = NULL;
pa_context* context_ = NULL;
Mutex mutex_;
SbThread audio_thread_ = kSbThreadInvalid;
pthread_t audio_thread_ = 0;
bool destroying_ = false;
};

Expand Down Expand Up @@ -373,12 +375,12 @@ void PulseAudioSink::HandleRequest(size_t length) {
PulseAudioSinkType::PulseAudioSinkType() {}

PulseAudioSinkType::~PulseAudioSinkType() {
if (SbThreadIsValid(audio_thread_)) {
if (audio_thread_ != 0) {
{
ScopedLock lock(mutex_);
destroying_ = true;
}
SbThreadJoin(audio_thread_, NULL);
pthread_join(audio_thread_, NULL);
}
SB_DCHECK(sinks_.empty());
if (context_) {
Expand Down Expand Up @@ -474,17 +476,16 @@ bool PulseAudioSinkType::Initialize() {
context_ = NULL;
return false;
}
audio_thread_ = SbThreadCreate(0, kSbThreadPriorityRealTime,
kSbThreadNoAffinity, true, "pulse_audio",
&PulseAudioSinkType::ThreadEntryPoint, this);
SB_DCHECK(SbThreadIsValid(audio_thread_));
pthread_create(&audio_thread_, nullptr, &PulseAudioSinkType::ThreadEntryPoint,
this);
SB_DCHECK(audio_thread_ != 0);

return true;
}

bool PulseAudioSinkType::BelongToAudioThread() {
SB_DCHECK(SbThreadIsValid(audio_thread_));
return SbThreadIsCurrent(audio_thread_);
SB_DCHECK(audio_thread_ != 0);
return pthread_equal(pthread_self(), audio_thread_);
}

pa_stream* PulseAudioSinkType::CreateNewStream(
Expand Down Expand Up @@ -551,6 +552,10 @@ void PulseAudioSinkType::StateCallback(pa_context* context, void* userdata) {

// static
void* PulseAudioSinkType::ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "pulse_audio");

shared::pthread::ThreadSetPriority(kSbThreadPriorityRealTime);

SB_DCHECK(context);
PulseAudioSinkType* type = static_cast<PulseAudioSinkType*>(context);
type->AudioThreadFunc();
Expand Down

0 comments on commit db41af7

Please sign in to comment.