Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions runtime/shims/silent_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,23 @@ class AudioClient final : public Unknown<IAudioClient3> {
Lock l(lock_);
if (i < kMaxChannels) channel_volume_[i] = v;
}
float master_ = 1.0f;
BOOL mute_ = FALSE;
// ISimpleAudioVolume's state, under the same lock as the channel volumes.
float master() {
Lock l(lock_);
return master_;
}
void set_master(float v) {
Lock l(lock_);
master_ = v;
}
BOOL mute() {
Lock l(lock_);
return mute_;
}
void set_mute(BOOL m) {
Lock l(lock_);
mute_ = m;
}

private:
struct Lock {
Expand Down Expand Up @@ -511,6 +526,8 @@ class AudioClient final : public Unknown<IAudioClient3> {
uint64_t played_at_anchor_ = 0;
int64_t anchor_qpc_ = 0;
float channel_volume_[kMaxChannels]{};
float master_ = 1.0f;
BOOL mute_ = FALSE;
};

// The services hold their client.
Expand Down Expand Up @@ -551,26 +568,29 @@ class Clock final : public Service<IAudioClock> {
}
};

// A volume level in [0, 1]. Written so NaN fails too: `level < 0 || level > 1` is false for NaN.
bool volume_level(float level) { return level >= 0.0f && level <= 1.0f; }

class SimpleVolume final : public Service<ISimpleAudioVolume> {
public:
using Service::Service;
HRESULT STDMETHODCALLTYPE SetMasterVolume(float level, LPCGUID) override {
if (level < 0.0f || level > 1.0f) return E_INVALIDARG;
client_->master_ = level;
if (!volume_level(level)) return E_INVALIDARG;
client_->set_master(level);
return S_OK;
}
HRESULT STDMETHODCALLTYPE GetMasterVolume(float* level) override {
if (!level) return E_POINTER;
*level = client_->master_;
*level = client_->master();
return S_OK;
}
HRESULT STDMETHODCALLTYPE SetMute(BOOL mute, LPCGUID) override {
client_->mute_ = mute;
client_->set_mute(mute);
return S_OK;
}
HRESULT STDMETHODCALLTYPE GetMute(BOOL* mute) override {
if (!mute) return E_POINTER;
*mute = client_->mute_;
*mute = client_->mute();
return S_OK;
}
};
Expand All @@ -584,7 +604,7 @@ class StreamVolume final : public Service<IAudioStreamVolume> {
return S_OK;
}
HRESULT STDMETHODCALLTYPE SetChannelVolume(UINT32 index, const float level) override {
if (index >= client_->channels() || level < 0.0f || level > 1.0f) return E_INVALIDARG;
if (index >= client_->channels() || !volume_level(level)) return E_INVALIDARG;
client_->set_volume(index, level);
return S_OK;
}
Expand All @@ -598,7 +618,7 @@ class StreamVolume final : public Service<IAudioStreamVolume> {
if (!levels) return E_POINTER;
if (count != client_->channels()) return E_INVALIDARG;
for (UINT32 i = 0; i < count; ++i) {
if (levels[i] < 0.0f || levels[i] > 1.0f) return E_INVALIDARG;
if (!volume_level(levels[i])) return E_INVALIDARG;
}
for (UINT32 i = 0; i < count; ++i) client_->set_volume(i, levels[i]);
return S_OK;
Expand Down
21 changes: 21 additions & 0 deletions runtime/test/silent_audio_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cstdio>
#include <cstring>
#include <cwchar>
#include <limits>

#include "shims.h"
#include "silent_audio.h"
Expand Down Expand Up @@ -424,11 +425,31 @@ void test_render_stream() {
if (!render || !clock || !simple || !stream) return;
UINT32 channels = 0;
float level = 0.0f;
BOOL muted = FALSE;
check(stream->GetChannelCount(&channels) == S_OK && channels == 8 && stream->SetChannelVolume(7, 0.5f) == S_OK &&
stream->GetChannelVolume(7, &level) == S_OK && level == 0.5f,
"IAudioStreamVolume keeps a channel volume");
check(simple->SetMasterVolume(0.25f, nullptr) == S_OK && simple->GetMasterVolume(&level) == S_OK && level == 0.25f,
"ISimpleAudioVolume keeps the master volume");
// NaN is no volume (kevincommits' finding in #1608): `level < 0 || level > 1` let it through and stored it.
const float nan = std::numeric_limits<float>::quiet_NaN();
check(simple->SetMasterVolume(nan, nullptr) == E_INVALIDARG && simple->GetMasterVolume(&level) == S_OK &&
level == 0.25f,
"SetMasterVolume(NaN) is refused and keeps the master volume");
check(stream->SetChannelVolume(7, nan) == E_INVALIDARG && stream->GetChannelVolume(7, &level) == S_OK &&
level == 0.5f,
"SetChannelVolume(NaN) is refused and keeps the channel volume");
{
float before = -1.0f;
stream->GetChannelVolume(0, &before);
float levels[8] = {0.125f, 0.125f, 0.125f, 0.125f, 0.125f, 0.125f, 0.125f, nan};
check(stream->SetAllVolumes(8, levels) == E_INVALIDARG && stream->GetChannelVolume(0, &level) == S_OK &&
level == before && before != 0.125f,
"SetAllVolumes with a NaN level is refused and sets none");
}
check(simple->SetMute(TRUE, nullptr) == S_OK && simple->GetMute(&muted) == S_OK && muted == TRUE &&
simple->SetMute(FALSE, nullptr) == S_OK && simple->GetMute(&muted) == S_OK && muted == FALSE,
"ISimpleAudioVolume keeps the mute state");
UINT64 freq = 0;
check(clock->GetFrequency(&freq) == S_OK && freq == 48000, "IAudioClock counts frames at 48 kHz");

Expand Down
Loading