Skip to content

Commit

Permalink
[media] Move concrete SbAudioSink impl to namespace (#4634)
Browse files Browse the repository at this point in the history
SbAudioSink is a typedef to SbAudioSinkPrivate*, and the concrete
implementation of SbAudioSinkPrivate was in the global namespace. Now
it's moved into a new class named SbAudioSinkImpl in the namespace
starboard::shared::starboard::audio_sink, and it's derived from
SbAudioSinkPrivate, which is now an interface.

This allows to have multiple SbAudioSink implementations in different
namespaces.

b/327287075
  • Loading branch information
xiaomings authored Jan 3, 2025
1 parent affac16 commit a6d7996
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 73 deletions.
2 changes: 1 addition & 1 deletion starboard/android/shared/application_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ApplicationAndroid::ApplicationAndroid(
"getResourceOverlay", "()Ldev/cobalt/coat/ResourceOverlay;");
resource_overlay_ = env->ConvertLocalRefToGlobalRef(local_ref);

SbAudioSinkPrivate::Initialize();
::starboard::shared::starboard::audio_sink::SbAudioSinkImpl::Initialize();

app_start_timestamp_ = starboard_bridge_->GetAppStartTimestamp();

Expand Down
16 changes: 13 additions & 3 deletions starboard/android/shared/audio_track_audio_sink_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -561,20 +561,30 @@ int AudioTrackAudioSinkType::GetMinBufferSizeInFramesInternal(
} // namespace android
} // namespace starboard

namespace starboard {
namespace shared {
namespace starboard {
namespace audio_sink {

// static
void SbAudioSinkPrivate::PlatformInitialize() {
void SbAudioSinkImpl::PlatformInitialize() {
SB_DCHECK(!audio_track_audio_sink_type_);
audio_track_audio_sink_type_ =
new starboard::android::shared::AudioTrackAudioSinkType;
new ::starboard::android::shared::AudioTrackAudioSinkType;
SetPrimaryType(audio_track_audio_sink_type_);
EnableFallbackToStub();
audio_track_audio_sink_type_->TestMinRequiredFrames();
}

// static
void SbAudioSinkPrivate::PlatformTearDown() {
void SbAudioSinkImpl::PlatformTearDown() {
SB_DCHECK(audio_track_audio_sink_type_ == GetPrimaryType());
SetPrimaryType(NULL);
delete audio_track_audio_sink_type_;
audio_track_audio_sink_type_ = NULL;
}

} // namespace audio_sink
} // namespace starboard
} // namespace shared
} // namespace starboard
3 changes: 2 additions & 1 deletion starboard/android/shared/audio_track_audio_sink_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class AudioTrackAudioSinkType : public SbAudioSinkPrivate::Type {
bool has_remote_audio_output_ = false;
};

class AudioTrackAudioSink : public SbAudioSinkPrivate {
class AudioTrackAudioSink
: public ::starboard::shared::starboard::audio_sink::SbAudioSinkImpl {
public:
AudioTrackAudioSink(
Type* type,
Expand Down
3 changes: 2 additions & 1 deletion starboard/android/shared/player_components_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ class AudioRendererSinkAndroid : public ::starboard::shared::starboard::player::
SbAudioSinkPrivate::ErrorFunc error_func,
void* context) {
auto type = static_cast<AudioTrackAudioSinkType*>(
SbAudioSinkPrivate::GetPreferredType());
::starboard::shared::starboard::audio_sink::SbAudioSinkImpl::
GetPreferredType());

return type->Create(
channels, sampling_frequency_hz, audio_sample_type,
Expand Down
2 changes: 1 addition & 1 deletion starboard/android/shared/starboard_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace android {
namespace shared {

extern "C" SB_EXPORT_PLATFORM void JNI_StarboardBridge_OnStop(JNIEnv* env) {
SbAudioSinkPrivate::TearDown();
::starboard::shared::starboard::audio_sink::SbAudioSinkImpl::TearDown();
SbFileAndroidTeardown();
}

Expand Down
26 changes: 18 additions & 8 deletions starboard/linux/shared/audio_sink_type_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,38 @@
#include "starboard/shared/pulse/pulse_audio_sink_type.h"
#include "starboard/shared/starboard/audio_sink/audio_sink_internal.h"

namespace starboard {
namespace shared {
namespace starboard {
namespace audio_sink {
namespace {
bool is_fallback_to_alsa = false;
}
} // namespace

// static
void SbAudioSinkPrivate::PlatformInitialize() {
starboard::shared::pulse::PlatformInitialize();
void SbAudioSinkImpl::PlatformInitialize() {
::starboard::shared::pulse::PlatformInitialize();
if (GetPrimaryType()) {
SB_LOG(INFO) << "Use PulseAudio";
} else {
SB_LOG(INFO) << "Use ALSA";
starboard::shared::alsa::PlatformInitialize();
::starboard::shared::alsa::PlatformInitialize();
is_fallback_to_alsa = true;
}
SbAudioSinkPrivate::EnableFallbackToStub();
::starboard::shared::starboard::audio_sink::SbAudioSinkImpl::
EnableFallbackToStub();
}

// static
void SbAudioSinkPrivate::PlatformTearDown() {
void SbAudioSinkImpl::PlatformTearDown() {
if (is_fallback_to_alsa) {
starboard::shared::alsa::PlatformTearDown();
::starboard::shared::alsa::PlatformTearDown();
} else {
starboard::shared::pulse::PlatformTearDown();
::starboard::shared::pulse::PlatformTearDown();
}
}

} // namespace audio_sink
} // namespace starboard
} // namespace shared
} // namespace starboard
4 changes: 2 additions & 2 deletions starboard/raspi/shared/application_dispmanx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ bool ApplicationDispmanx::DestroyWindow(SbWindow window) {
}

void ApplicationDispmanx::Initialize() {
SbAudioSinkPrivate::Initialize();
::starboard::shared::starboard::audio_sink::SbAudioSinkImpl::Initialize();
}

void ApplicationDispmanx::Teardown() {
ShutdownDispmanx();
SbAudioSinkPrivate::TearDown();
::starboard::shared::starboard::audio_sink::SbAudioSinkImpl::TearDown();
}

void ApplicationDispmanx ::OnSuspend() {
Expand Down
18 changes: 14 additions & 4 deletions starboard/raspi/shared/audio_sink_type_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@
#include "starboard/shared/alsa/alsa_audio_sink_type.h"
#include "starboard/shared/starboard/audio_sink/audio_sink_internal.h"

namespace starboard {
namespace shared {
namespace starboard {
namespace audio_sink {

// static
void SbAudioSinkPrivate::PlatformInitialize() {
starboard::shared::alsa::PlatformInitialize();
void SbAudioSinkImpl::PlatformInitialize() {
::starboard::shared::alsa::PlatformInitialize();
SbAudioSinkPrivate::EnableFallbackToStub();
}

// static
void SbAudioSinkPrivate::PlatformTearDown() {
starboard::shared::alsa::PlatformTearDown();
void SbAudioSinkImpl::PlatformTearDown() {
::starboard::shared::alsa::PlatformTearDown();
}

} // namespace audio_sink
} // namespace starboard
} // namespace shared
} // namespace starboard
31 changes: 15 additions & 16 deletions starboard/shared/alsa/alsa_audio_sink_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ namespace shared {
namespace alsa {
namespace {

using starboard::ScopedLock;
using starboard::ScopedTryLock;
using starboard::shared::alsa::AlsaGetBufferedFrames;
using starboard::shared::alsa::AlsaWriteFrames;
using ::starboard::ScopedLock;
using ::starboard::ScopedTryLock;
using ::starboard::shared::alsa::AlsaGetBufferedFrames;
using ::starboard::shared::alsa::AlsaWriteFrames;
using ::starboard::shared::starboard::audio_sink::SbAudioSinkImpl;

// The maximum number of frames that can be written to ALSA once. It must be a
// power of 2. It is also used as the ALSA polling size. A small number will
Expand Down Expand Up @@ -81,7 +82,7 @@ void* IncrementPointerByBytes(void* pointer, size_t offset) {
// 2. It never stops the underlying ALSA audio sink once created. When its
// source cannot provide enough data to continue playback, it simply writes
// silence to ALSA.
class AlsaAudioSink : public SbAudioSinkPrivate {
class AlsaAudioSink : public SbAudioSinkImpl {
public:
AlsaAudioSink(Type* type,
int channels,
Expand Down Expand Up @@ -142,8 +143,8 @@ class AlsaAudioSink : public SbAudioSinkPrivate {
SbMediaAudioSampleType sample_type_;

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

int64_t time_to_wait_;

Expand Down Expand Up @@ -215,7 +216,7 @@ AlsaAudioSink::~AlsaAudioSink() {
// static
void* AlsaAudioSink::ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "alsa_audio_out");
starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityRealTime);
::starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityRealTime);
SB_DCHECK(context);
AlsaAudioSink* sink = reinterpret_cast<AlsaAudioSink*>(context);
sink->AudioThreadFunc();
Expand All @@ -228,7 +229,7 @@ void AlsaAudioSink::AudioThreadFunc() {
sample_type_ == kSbMediaAudioSampleTypeFloat32 ? SND_PCM_FORMAT_FLOAT_LE
: SND_PCM_FORMAT_S16;

playback_handle_ = starboard::shared::alsa::AlsaOpenPlaybackDevice(
playback_handle_ = ::starboard::shared::alsa::AlsaOpenPlaybackDevice(
channels_, sampling_frequency_hz_, kFramesPerRequest,
kALSABufferSizeInFrames, alsa_sample_type);
{
Expand All @@ -249,7 +250,7 @@ void AlsaAudioSink::AudioThreadFunc() {
}
}

starboard::shared::alsa::AlsaCloseDevice(playback_handle_);
::starboard::shared::alsa::AlsaCloseDevice(playback_handle_);
ScopedLock lock(mutex_);
playback_handle_ = NULL;
}
Expand Down Expand Up @@ -450,25 +451,23 @@ SbAudioSink AlsaAudioSinkType::Create(
return audio_sink;
}

} // namespace

namespace {
AlsaAudioSinkType* alsa_audio_sink_type_;

} // namespace

// static
void PlatformInitialize() {
SB_DCHECK(!alsa_audio_sink_type_);
alsa_audio_sink_type_ = new AlsaAudioSinkType();
SbAudioSinkPrivate::SetPrimaryType(alsa_audio_sink_type_);
SbAudioSinkImpl::SetPrimaryType(alsa_audio_sink_type_);
}

// static
void PlatformTearDown() {
SB_DCHECK(alsa_audio_sink_type_);
SB_DCHECK(alsa_audio_sink_type_ == SbAudioSinkPrivate::GetPrimaryType());
SB_DCHECK(alsa_audio_sink_type_ == SbAudioSinkImpl::GetPrimaryType());

SbAudioSinkPrivate::SetPrimaryType(NULL);
SbAudioSinkImpl::SetPrimaryType(NULL);
delete alsa_audio_sink_type_;
alsa_audio_sink_type_ = NULL;
}
Expand Down
11 changes: 6 additions & 5 deletions starboard/shared/pulse/pulse_audio_sink_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace pulse {
namespace {

using starboard::media::GetBytesPerSample;
using ::starboard::shared::starboard::audio_sink::SbAudioSinkImpl;

const int64_t kAudioIdleSleepIntervalUsec = 15'000; // 15ms
const int64_t kAudioRunningSleepIntervalUsec = 5'000; // 5ms
Expand All @@ -65,7 +66,7 @@ const size_t kPulseBufferSizeInFrames = 8192;

class PulseAudioSinkType;

class PulseAudioSink : public SbAudioSinkPrivate {
class PulseAudioSink : public SbAudioSinkImpl {
public:
PulseAudioSink(PulseAudioSinkType* type,
int channels,
Expand Down Expand Up @@ -554,7 +555,7 @@ void PulseAudioSinkType::StateCallback(pa_context* context, void* userdata) {
void* PulseAudioSinkType::ThreadEntryPoint(void* context) {
pthread_setname_np(pthread_self(), "pulse_audio");

shared::pthread::ThreadSetPriority(kSbThreadPriorityRealTime);
::starboard::shared::pthread::ThreadSetPriority(kSbThreadPriorityRealTime);

SB_DCHECK(context);
PulseAudioSinkType* type = static_cast<PulseAudioSinkType*>(context);
Expand Down Expand Up @@ -602,16 +603,16 @@ void PlatformInitialize() {
std::unique_ptr<PulseAudioSinkType>(new PulseAudioSinkType());
if (audio_sink_type->Initialize()) {
pulse_audio_sink_type_ = audio_sink_type.release();
SbAudioSinkPrivate::SetPrimaryType(pulse_audio_sink_type_);
SbAudioSinkImpl::SetPrimaryType(pulse_audio_sink_type_);
}
}

// static
void PlatformTearDown() {
SB_DCHECK(pulse_audio_sink_type_);
SB_DCHECK(pulse_audio_sink_type_ == SbAudioSinkPrivate::GetPrimaryType());
SB_DCHECK(pulse_audio_sink_type_ == SbAudioSinkImpl::GetPrimaryType());

SbAudioSinkPrivate::SetPrimaryType(NULL);
SbAudioSinkImpl::SetPrimaryType(NULL);
delete pulse_audio_sink_type_;
pulse_audio_sink_type_ = NULL;
pulse_unload_library();
Expand Down
2 changes: 1 addition & 1 deletion starboard/shared/starboard/audio_sink/audio_sink_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SbAudioSink SbAudioSinkCreate(
SbAudioSinkUpdateSourceStatusFunc update_source_status_func,
SbAudioSinkConsumeFramesFunc consume_frames_func,
void* context) {
return SbAudioSinkPrivate::Create(
return ::starboard::shared::starboard::audio_sink::SbAudioSinkImpl::Create(
channels, sampling_frequency_hz, audio_sample_type,
audio_frame_storage_type, frame_buffers, frame_buffers_size_in_frames,
update_source_status_func, consume_frames_func, NULL /*error_func*/,
Expand Down
6 changes: 4 additions & 2 deletions starboard/shared/starboard/audio_sink/audio_sink_destroy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
#include "starboard/shared/starboard/audio_sink/audio_sink_internal.h"

void SbAudioSinkDestroy(SbAudioSink audio_sink) {
using ::starboard::shared::starboard::audio_sink::SbAudioSinkImpl;

if (audio_sink == kSbAudioSinkInvalid) {
return;
}

SbAudioSinkPrivate::Type* type = SbAudioSinkPrivate::GetPrimaryType();
SbAudioSinkPrivate::Type* type = SbAudioSinkImpl::GetPrimaryType();
if (type && type->IsValid(audio_sink)) {
type->Destroy(audio_sink);
return;
}
type = SbAudioSinkPrivate::GetFallbackType();
type = SbAudioSinkImpl::GetFallbackType();
if (type && type->IsValid(audio_sink)) {
type->Destroy(audio_sink);
return;
Expand Down
Loading

0 comments on commit a6d7996

Please sign in to comment.