Skip to content

Commit

Permalink
[WIP] Refactor audio16int support
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidokert committed Apr 4, 2024
1 parent 9f487ec commit 4c2040e
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 39 deletions.
7 changes: 0 additions & 7 deletions cobalt/site/docs/reference/starboard/configuration-public.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ Book: /youtube/cobalt/_book.yaml

# Starboard Configuration Reference Guide

## Media Configuration

| Properties |
| :--- |
| **`SB_HAS_QUIRK_SUPPORT_INT16_AUDIO_SAMPLES`**<br><br>The implementation is allowed to support kSbMediaAudioSampleTypeInt16 only when this macro is defined.<br><br>By default, this property is undefined. |


## Memory Configuration

| Properties |
Expand Down
4 changes: 3 additions & 1 deletion starboard/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ since the version previous to it.

## Removed configs for `SB_EXPORT_PLATFORM` and `SB_IMPORT_PLATFORM`
These are auto-detected based on compilers, platforms can optionally override.

## Quirk `SUPPORT_INT16_AUDIO_SAMPLES` is now a configuration constant
`SUPPORT_INT16_AUDIO_SAMPLES` is replaced by `kSbHas16BitAudioSamples`
configuration constant.
## Removed configs for `SB_C_FORCE_INLINE`
This is now automatically defined based on compilers, platforms must not provide
a definition.
Expand Down
5 changes: 5 additions & 0 deletions starboard/android/shared/configuration_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@ const uint32_t kSbUserMaxSignedIn = 1;
// The maximum size the cache directory is allowed to use in bytes.
const uint32_t kSbMaxSystemPathCacheDirectorySize = 24 << 20; // 24MiB
#endif

#if SB_API_VERSION > 15
// Platform can use MediaAudioSampleTypeInt16
SB_EXPORT extern const bool kSbHas16BitAudioSamples = false;
#endif
5 changes: 5 additions & 0 deletions starboard/configuration_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,9 @@ SB_EXPORT extern const uint32_t kSbUserMaxSignedIn;
SB_EXPORT extern const uint32_t kSbMaxSystemPathCacheDirectorySize;
#endif

#if SB_API_VERSION > 15
// Platform can use MediaAudioSampleTypeInt16
SB_EXPORT extern const bool kSbHas16BitAudioSamples;
#endif

#endif // STARBOARD_CONFIGURATION_CONSTANTS_H_
5 changes: 5 additions & 0 deletions starboard/linux/shared/configuration_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,8 @@ const uint32_t kSbUserMaxSignedIn = 1;
// The maximum size the cache directory is allowed to use in bytes.
const uint32_t kSbMaxSystemPathCacheDirectorySize = 24 << 20; // 24MiB
#endif

#if SB_API_VERSION > 15
// Platform can use MediaAudioSampleTypeInt16
SB_EXPORT extern const bool kSbHas16BitAudioSamples = false;
#endif
2 changes: 0 additions & 2 deletions starboard/media.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ typedef enum SbMediaAudioCodingType {
typedef enum SbMediaAudioSampleType {
kSbMediaAudioSampleTypeInt16Deprecated,
kSbMediaAudioSampleTypeFloat32,
#if SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
kSbMediaAudioSampleTypeInt16 = kSbMediaAudioSampleTypeInt16Deprecated,
#endif // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
} SbMediaAudioSampleType;

// Possible audio frame storage types.
Expand Down
5 changes: 5 additions & 0 deletions starboard/raspi/shared/configuration_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ const uint32_t kSbUserMaxSignedIn = 1;
// The maximum size the cache directory is allowed to use in bytes.
const uint32_t kSbMaxSystemPathCacheDirectorySize = 24 << 20; // 24MiB
#endif

#if SB_API_VERSION > 15
// Platform can use MediaAudioSampleTypeInt16
SB_EXPORT extern const bool kSbHas16BitAudioSamples = false;
#endif
33 changes: 28 additions & 5 deletions starboard/shared/opus/opus_audio_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "starboard/common/log.h"
#include "starboard/common/string.h"
#include "starboard/configuration.h"

namespace starboard {
namespace shared {
Expand Down Expand Up @@ -130,6 +131,26 @@ bool OpusAudioDecoder::DecodeInternal(
audio_stream_info_.number_of_channels * frames_per_au_ *
starboard::media::GetBytesPerSample(GetSampleType()));

#if SB_API_VERSION >= 16
const char* kDecodeFunctionName = kSbHas16BitAudioSamples
? "opus_multistream_decode"
: "opus_multistream_decode_float";
int decoded_frames =
kSbHas16BitAudioSamples
? opus_multistream_decode(
decoder_,
static_cast<const unsigned char*>(input_buffer->data()),
input_buffer->size(),
reinterpret_cast<opus_int16*>(decoded_audio->data()),
frames_per_au_, 0)
: opus_multistream_decode_float(
decoder_,
static_cast<const unsigned char*>(input_buffer->data()),
input_buffer->size(),
reinterpret_cast<float*>(decoded_audio->data()), frames_per_au_,
0);
#else

#if SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
const char kDecodeFunctionName[] = "opus_multistream_decode";
int decoded_frames = opus_multistream_decode(
Expand All @@ -143,6 +164,8 @@ bool OpusAudioDecoder::DecodeInternal(
input_buffer->size(), reinterpret_cast<float*>(decoded_audio->data()),
frames_per_au_, 0);
#endif // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)

#endif
if (decoded_frames == OPUS_BUFFER_TOO_SMALL &&
frames_per_au_ < kMaxOpusFramesPerAU) {
frames_per_au_ = kMaxOpusFramesPerAU;
Expand Down Expand Up @@ -260,11 +283,11 @@ bool OpusAudioDecoder::is_valid() const {

SbMediaAudioSampleType OpusAudioDecoder::GetSampleType() const {
SB_DCHECK(BelongsToCurrentThread());
#if SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
return kSbMediaAudioSampleTypeInt16;
#else // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
return kSbMediaAudioSampleTypeFloat32;
#endif // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
if (kSbHas16BitAudioSamples) {
return kSbMediaAudioSampleTypeInt16;
} else {
return kSbMediaAudioSampleTypeFloat32;
}
}

} // namespace opus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,12 @@ TEST_F(AudioRendererTest, SunnyDay) {
EXPECT_TRUE(audio_renderer_->IsEndOfStreamPlayed());
}

#if SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
#if SB_API_VERSION >= 16 || SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
TEST_F(AudioRendererTest, SunnyDayWithDoublePlaybackRateAndInt16Samples) {
#if SB_API_VERSION >= 16
if (!kSbHas16BitAudioSamples)
return;
#endif
if (HasAsyncAudioFramesReporting()) {
SB_LOG(INFO) << "Platform has async audio frames reporting. Test skipped.";
return;
Expand Down Expand Up @@ -465,7 +469,7 @@ TEST_F(AudioRendererTest, SunnyDayWithDoublePlaybackRateAndInt16Samples) {

EXPECT_TRUE(audio_renderer_->IsEndOfStreamPlayed());
}
#endif // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
#endif // SB_API_VERSION >= 16 || SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)

TEST_F(AudioRendererTest, StartPlayBeforePreroll) {
if (HasAsyncAudioFramesReporting()) {
Expand Down
7 changes: 1 addition & 6 deletions starboard/shared/win32/audio_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ AudioDecoder::AudioDecoder(const AudioStreamInfo& audio_stream_info,
drm_system_(drm_system),
sample_type_((audio_stream_info.codec == kSbMediaAudioCodecAc3 ||
audio_stream_info.codec == kSbMediaAudioCodecEac3)
?
#if SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
kSbMediaAudioSampleTypeInt16
#else
kSbMediaAudioSampleTypeInt16Deprecated
#endif // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
? kSbMediaAudioSampleTypeInt16
: kSbMediaAudioSampleTypeFloat32),
stream_ended_(false) {
SB_DCHECK(audio_stream_info.codec == kSbMediaAudioCodecAac ||
Expand Down
4 changes: 0 additions & 4 deletions starboard/shared/win32/audio_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ void CHECK_HRESULT_OK(HRESULT hr) {

WORD SampleTypeToFormatTag(SbMediaAudioSampleType type) {
switch (type) {
#if SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
case kSbMediaAudioSampleTypeInt16:
return WAVE_FORMAT_PCM;
#endif // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
case kSbMediaAudioSampleTypeFloat32:
return WAVE_FORMAT_IEEE_FLOAT;
default:
Expand All @@ -61,10 +59,8 @@ WORD SampleTypeToFormatTag(SbMediaAudioSampleType type) {

WORD SampleTypeToBitsPerSample(SbMediaAudioSampleType type) {
switch (type) {
#if SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
case kSbMediaAudioSampleTypeInt16:
return 16;
#endif // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
case kSbMediaAudioSampleTypeFloat32:
return 32;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
bool SbAudioSinkIsAudioSampleTypeSupported(
SbMediaAudioSampleType audio_sample_type) {
switch (audio_sample_type) {
#if SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
case kSbMediaAudioSampleTypeInt16:
return true;
#endif // SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES)
case kSbMediaAudioSampleTypeFloat32:
return true;
default:
Expand Down
3 changes: 3 additions & 0 deletions starboard/stub/configuration_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ const uint32_t kSbUserMaxSignedIn = 1;
// The maximum size the cache directory is allowed to use in bytes.
const uint32_t kSbMaxSystemPathCacheDirectorySize = 24 << 20; // 24MiB
#endif

// Platform can use MediaAudioSampleTypeInt16
SB_EXPORT extern const bool kSbHas16BitAudioSamples = false;
6 changes: 0 additions & 6 deletions starboard/stub/configuration_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@
#define SB_IS_WCHAR_T_UNSIGNED 1
#endif

// --- Media Configuration ---------------------------------------------------

// The implementation is allowed to support kSbMediaAudioSampleTypeInt16 only
// when this macro is defined.
#undef SB_HAS_QUIRK_SUPPORT_INT16_AUDIO_SAMPLES

// --- Memory Configuration --------------------------------------------------

// Whether this platform can map executable memory. Implies SB_HAS_MMAP. This is
Expand Down
5 changes: 5 additions & 0 deletions starboard/win/shared/configuration_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,8 @@ const uint32_t kSbUserMaxSignedIn = 1;
// The maximum size the cache directory is allowed to use in bytes.
const uint32_t kSbMaxSystemPathCacheDirectorySize = 24 << 20; // 24MiB
#endif

#if SB_API_VERSION > 15
// Platform can use MediaAudioSampleTypeInt16
SB_EXPORT extern const bool kSbHas16BitAudioSamples = false;
#endif
3 changes: 3 additions & 0 deletions starboard/xb1/shared/configuration_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,6 @@ const uint32_t kSbUserMaxSignedIn = 1;
// The maximum size the cache directory is allowed to use in bytes.
const uint32_t kSbMaxSystemPathCacheDirectorySize = 24 << 20; // 24MiB
#endif

// Platform can use MediaAudioSampleTypeInt16
SB_EXPORT extern const bool kSbHas16BitAudioSamples = true;
4 changes: 0 additions & 4 deletions starboard/xb1/shared/configuration_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,4 @@

// --- Platform Specific Quirks ----------------------------------------------

// The implementation is allowed to support kSbMediaAudioSampleTypeInt16 only
// when this macro is defined.
#define SB_HAS_QUIRK_SUPPORT_INT16_AUDIO_SAMPLES 1

#endif // STARBOARD_XB1_SHARED_CONFIGURATION_PUBLIC_H_

0 comments on commit 4c2040e

Please sign in to comment.