silent_audio: NaN volumes refused, master volume under the lock, Reset revokes objects (RT-SILENT-AUDIO-VOLUME-AND-SPATIAL)

From kevincommits' finding on #1608 (item 4):

- SimpleVolume::SetMasterVolume, StreamVolume::SetChannelVolume and
  StreamVolume::SetAllVolumes refuse NaN. They tested
  `level < 0.0f || level > 1.0f`, which is false for NaN, and stored it.
  One helper checks `level >= 0.0f && level <= 1.0f`, as
  SpatialObject::SetVolume does.
- ISimpleAudioVolume's master volume and mute are kept under the client's
  lock, like the channel volumes, instead of read and written bare.
- SpatialStream::Reset revokes every active object, as Microsoft's
  reference for ISpatialAudioObjectRenderStreamBase::Reset says: each is
  inactive, its type can be activated again, and queued ends are dropped.
  The stream keeps a list of its live objects (not owned); an object
  leaves it first thing in its destructor.

Not changed: ActivateSpatialAudioObject during an update pass. The
reference lists no SPTLAUDCLNT_E_OUT_OF_ORDER for it, and activating
objects inside a pass is the documented pattern (this file's own test
does it).

6 new checks in silent_audio_test fail on main and pass with the fix.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tyrannys
2026-11-27 18:25:16 -06:00
parent d442851972
commit 53ea681a07
2 changed files with 84 additions and 11 deletions

View File

@ -13,6 +13,7 @@
#include <propsys.h>
#include <spatialaudioclient.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cwchar>
@ -415,10 +416,27 @@ public:
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: the title may set it from any thread.
float master() {
Lock l(lock_);
return master_;
}
void set_master(float level) {
Lock l(lock_);
master_ = level;
}
BOOL mute() {
Lock l(lock_);
return mute_;
}
void set_mute(BOOL mute) {
Lock l(lock_);
mute_ = mute;
}
private:
float master_ = 1.0f;
BOOL mute_ = FALSE;
struct Lock {
explicit Lock(SRWLOCK& s) : s_(s) { AcquireSRWLockExclusive(&s_); }
~Lock() { ReleaseSRWLockExclusive(&s_); }
@ -551,26 +569,30 @@ public:
}
};
// A volume level is 0.0 to 1.0. Written so that NaN, which compares false either way, is out of range too (as
// SpatialObject::SetVolume checks it).
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;
}
};
@ -584,7 +606,7 @@ public:
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;
}
@ -598,7 +620,7 @@ public:
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;
@ -744,9 +766,23 @@ public:
running_ = false;
return S_OK;
}
// A stopped stream's reset revokes every active object (Microsoft's reference for
// ISpatialAudioObjectRenderStreamBase::Reset): each is inactive from now on and its type can be activated again;
// the ends a pass had queued go too.
HRESULT STDMETHODCALLTYPE Reset() override {
Lock l(lock_);
return running_ ? SPTLAUDCLNT_E_STREAM_NOT_STOPPED : S_OK;
std::vector<SpatialObject*> ended;
{
Lock l(lock_);
if (running_) return SPTLAUDCLNT_E_STREAM_NOT_STOPPED;
for (SpatialObject* o : objects_) {
o->make_inactive();
o->take_type();
}
active_types_ = 0;
ended.swap(ending_);
}
for (SpatialObject* o : ended) o->Release();
return S_OK;
}
HRESULT STDMETHODCALLTYPE BeginUpdatingAudioObjects(UINT32* available, UINT32* frames) override {
if (!available || !frames) return E_POINTER;
@ -789,6 +825,10 @@ public:
release_type(t);
return E_OUTOFMEMORY;
}
{
Lock l(lock_);
objects_.push_back(o);
}
*object = o;
return S_OK;
}
@ -812,6 +852,12 @@ public:
Lock l(lock_);
active_types_ &= ~t;
}
// An object going away: Reset no longer reaches it. First in its destructor, so a Reset that holds the lock
// meanwhile still sees the whole object.
void forget(SpatialObject* o) {
Lock l(lock_);
objects_.erase(std::remove(objects_.begin(), objects_.end(), o), objects_.end());
}
private:
struct Lock {
@ -838,6 +884,7 @@ private:
bool updating_ = false;
std::vector<BYTE> scratch_;
std::vector<SpatialObject*> ending_;
std::vector<SpatialObject*> objects_; // the live objects (not owned): each holds the stream
PeriodicThread clock_;
};
@ -845,6 +892,7 @@ SpatialObject::SpatialObject(SpatialStream* stream, uint32_t type) : stream_(str
stream_->AddRef();
}
SpatialObject::~SpatialObject() {
stream_->forget(this);
if (take_type()) stream_->release_type(type_);
stream_->Release();
}

View File

@ -14,6 +14,7 @@
#include <cstdio>
#include <cstring>
#include <cwchar>
#include <limits>
#include "shims.h"
#include "silent_audio.h"
@ -429,6 +430,19 @@ void test_render_stream() {
"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");
{
const float nan = std::numeric_limits<float>::quiet_NaN();
float levels[8] = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, nan};
check(simple->SetMasterVolume(nan, nullptr) == E_INVALIDARG && simple->GetMasterVolume(&level) == S_OK &&
level == 0.25f,
"SetMasterVolume(NaN) is refused and the volume stays");
check(stream->SetChannelVolume(7, nan) == E_INVALIDARG && stream->GetChannelVolume(7, &level) == S_OK &&
level == 0.5f,
"SetChannelVolume(NaN) is refused and the volume stays");
check(stream->SetAllVolumes(8, levels) == E_INVALIDARG && stream->GetChannelVolume(0, &level) == S_OK &&
level == 1.0f,
"SetAllVolumes with a NaN level is refused and changes nothing");
}
UINT64 freq = 0;
check(clock->GetFrequency(&freq) == S_OK && freq == 48000, "IAudioClock counts frames at 48 kHz");
@ -668,6 +682,17 @@ void test_spatial_stream() {
}
check(WaitForSingleObject(event, 60) == WAIT_TIMEOUT, "no spatial event after Stop");
check(stream->Reset() == S_OK, "Reset");
check(fl->IsActive(&active) == S_OK && !active, "Reset revokes the objects that were active");
buffer = nullptr;
check(fl->GetBuffer(&buffer, &length) == SPTLAUDCLNT_E_RESOURCES_INVALIDATED && !buffer,
"a revoked object gets no buffer");
if (SUCCEEDED(stream->BeginUpdatingAudioObjects(&available, &frames))) {
check(stream->ActivateSpatialAudioObject(AudioObjectType_FrontLeft, &other) == S_OK && other,
"after Reset, a type that was active can be activated again");
if (other) other->Release();
other = nullptr;
stream->EndUpdatingAudioObjects();
}
fr->Release();
if (SUCCEEDED(stream->BeginUpdatingAudioObjects(&available, &frames))) {
check(stream->ActivateSpatialAudioObject(AudioObjectType_FrontRight, &other) == S_OK && other,