diff --git a/runtime/shims/silent_audio.cpp b/runtime/shims/silent_audio.cpp index 0e02186bb3..4dcb67a8c0 100644 --- a/runtime/shims/silent_audio.cpp +++ b/runtime/shims/silent_audio.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -415,10 +416,27 @@ class AudioClient final : public Unknown { 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 @@ class Clock final : public Service { } }; +// 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 { 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 @@ class StreamVolume final : public Service { 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 @@ class StreamVolume final : public Service { 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 @@ class SpatialStream final : public Unknown { 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 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 @@ class SpatialStream final : public Unknown { release_type(t); return E_OUTOFMEMORY; } + { + Lock l(lock_); + objects_.push_back(o); + } *object = o; return S_OK; } @@ -812,6 +852,12 @@ class SpatialStream final : public Unknown { 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 @@ class SpatialStream final : public Unknown { bool updating_ = false; std::vector scratch_; std::vector ending_; + std::vector 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(); } diff --git a/runtime/test/silent_audio_test.cpp b/runtime/test/silent_audio_test.cpp index eac3121b9d..6b10e7487b 100644 --- a/runtime/test/silent_audio_test.cpp +++ b/runtime/test/silent_audio_test.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #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::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,