locking attempt #2

This commit is contained in:
HifiExperiments 2024-04-14 22:04:09 -07:00
parent 6a180b14a1
commit 98c11cef82
3 changed files with 133 additions and 77 deletions

View file

@ -1924,9 +1924,7 @@ bool EntityScriptingInterface::restartSound(const QUuid& entityID) {
auto soundEntity = std::dynamic_pointer_cast<SoundEntityItem>(entity); auto soundEntity = std::dynamic_pointer_cast<SoundEntityItem>(entity);
bool isPlaying = soundEntity->getPlaying(); bool isPlaying = soundEntity->getPlaying();
if (isPlaying) { if (isPlaying) {
soundEntity->withWriteLock([&] { soundEntity->restartSound(true);
soundEntity->restartSound();
});
} }
return isPlaying; return isPlaying;
} }

View file

@ -138,68 +138,68 @@ bool SoundEntityItem::shouldCreateSound(const EntityTreePointer& tree) const {
void SoundEntityItem::update(const quint64& now) { void SoundEntityItem::update(const quint64& now) {
const auto tree = getTree(); const auto tree = getTree();
if (tree) { if (tree) {
std::lock_guard<std::recursive_mutex> lock(_soundLock);
_updateNeeded = false; _updateNeeded = false;
withWriteLock([&] { withReadLock([&] {
if (shouldCreateSound(tree)) { if (shouldCreateSound(tree)) {
_sound = DependencyManager::get<SoundCache>()->getSound(_url); _sound = DependencyManager::get<SoundCache>()->getSound(_url);
} }
}); });
withReadLock([&] { if (_sound) {
if (_sound) { if (_sound->isLoaded()) {
if (_sound->isLoaded()) { updateSound(true);
updateSound(true); } else {
} else { connect(_sound.data(), &Resource::finished, this, [&] { updateSound(true); });
connect(_sound.data(), &Resource::finished, this, [&] {
withReadLock([&] {
updateSound(true);
});
});
}
} }
}); }
} }
} }
void SoundEntityItem::locationChanged(bool tellPhysics, bool tellChildren) { void SoundEntityItem::locationChanged(bool tellPhysics, bool tellChildren) {
EntityItem::locationChanged(tellPhysics, tellChildren); EntityItem::locationChanged(tellPhysics, tellChildren);
withReadLock([&] { updateSound();
updateSound();
});
} }
void SoundEntityItem::dimensionsChanged() { void SoundEntityItem::dimensionsChanged() {
EntityItem::dimensionsChanged(); EntityItem::dimensionsChanged();
withReadLock([&] { updateSound();
updateSound();
});
} }
void SoundEntityItem::setURL(const QString& value) { void SoundEntityItem::setURL(const QString& value) {
bool changed = false;
withWriteLock([&] { withWriteLock([&] {
if (value != _url) { if (value != _url) {
_url = value; _url = value;
changed = true;
}
});
const auto tree = getTree(); if (changed) {
if (!tree) { const auto tree = getTree();
_updateNeeded = true; if (!tree) {
return; _updateNeeded = true;
} return;
}
std::lock_guard<std::recursive_mutex> lock(_soundLock);
withReadLock([&] {
if (shouldCreateSound(tree)) { if (shouldCreateSound(tree)) {
_sound = DependencyManager::get<SoundCache>()->getSound(_url); _sound = DependencyManager::get<SoundCache>()->getSound(_url);
} }
});
if (_sound) { if (_sound) {
if (_sound->isLoaded()) { if (_sound->isLoaded()) {
updateSound(true); updateSound(true);
} else { } else {
connect(_sound.data(), &Resource::finished, this, [&] { updateSound(true); }); connect(_sound.data(), &Resource::finished, this, [&] { updateSound(true); });
}
} }
} }
}); }
} }
QString SoundEntityItem::getURL() const { QString SoundEntityItem::getURL() const {
@ -209,12 +209,17 @@ QString SoundEntityItem::getURL() const {
} }
void SoundEntityItem::setVolume(float value) { void SoundEntityItem::setVolume(float value) {
bool changed = false;
withWriteLock([&] { withWriteLock([&] {
if (value != _volume) { if (value != _volume) {
_volume = value; _volume = value;
updateSound(); changed = true;
} }
}); });
if (changed) {
updateSound();
}
} }
float SoundEntityItem::getVolume() const { float SoundEntityItem::getVolume() const {
@ -224,12 +229,17 @@ float SoundEntityItem::getVolume() const {
} }
void SoundEntityItem::setTimeOffset(float value) { void SoundEntityItem::setTimeOffset(float value) {
bool changed = false;
withWriteLock([&] { withWriteLock([&] {
if (value != _timeOffset) { if (value != _timeOffset) {
_timeOffset = value; _timeOffset = value;
updateSound(true); changed = true;
} }
}); });
if (changed) {
updateSound(true);
}
} }
float SoundEntityItem::getTimeOffset() const { float SoundEntityItem::getTimeOffset() const {
@ -239,12 +249,17 @@ float SoundEntityItem::getTimeOffset() const {
} }
void SoundEntityItem::setPitch(float value) { void SoundEntityItem::setPitch(float value) {
bool changed = false;
withWriteLock([&] { withWriteLock([&] {
if (value != _pitch) { if (value != _pitch) {
_pitch = value; _pitch = value;
updateSound(true); changed = true;
} }
}); });
if (changed) {
updateSound(true);
}
} }
float SoundEntityItem::getPitch() const { float SoundEntityItem::getPitch() const {
@ -254,12 +269,17 @@ float SoundEntityItem::getPitch() const {
} }
void SoundEntityItem::setPlaying(bool value) { void SoundEntityItem::setPlaying(bool value) {
bool changed = false;
withWriteLock([&] { withWriteLock([&] {
if (value != _playing) { if (value != _playing) {
_playing = value; _playing = value;
updateSound(); changed = true;
} }
}); });
if (changed) {
updateSound();
}
} }
bool SoundEntityItem::getPlaying() const { bool SoundEntityItem::getPlaying() const {
@ -269,12 +289,17 @@ bool SoundEntityItem::getPlaying() const {
} }
void SoundEntityItem::setLoop(bool value) { void SoundEntityItem::setLoop(bool value) {
bool changed = false;
withWriteLock([&] { withWriteLock([&] {
if (value != _loop) { if (value != _loop) {
_loop = value; _loop = value;
updateSound(true); changed = true;
} }
}); });
if (changed) {
updateSound(true);
}
} }
bool SoundEntityItem::getLoop() const { bool SoundEntityItem::getLoop() const {
@ -284,12 +309,17 @@ bool SoundEntityItem::getLoop() const {
} }
void SoundEntityItem::setPositional(bool value) { void SoundEntityItem::setPositional(bool value) {
bool changed = false;
withWriteLock([&] { withWriteLock([&] {
if (value != _positional) { if (value != _positional) {
_positional = value; _positional = value;
updateSound(); changed = true;
} }
}); });
if (changed) {
updateSound();
}
} }
bool SoundEntityItem::getPositional() const { bool SoundEntityItem::getPositional() const {
@ -299,36 +329,48 @@ bool SoundEntityItem::getPositional() const {
} }
void SoundEntityItem::setLocalOnly(bool value) { void SoundEntityItem::setLocalOnly(bool value) {
bool changed = false;
withWriteLock([&] { withWriteLock([&] {
if (value != _localOnly) { if (value != _localOnly) {
_localOnly = value; _localOnly = value;
changed = true;
const auto tree = getTree();
if (!tree) {
_updateNeeded = true;
return;
}
if (shouldCreateSound(tree)) {
_sound = DependencyManager::get<SoundCache>()->getSound(_url);
} else {
_sound = nullptr;
if (_injector) {
DependencyManager::get<AudioInjectorManager>()->stop(_injector);
}
_injector = nullptr;
}
if (_sound) {
if (_sound->isLoaded()) {
updateSound(true);
} else {
connect(_sound.data(), &Resource::finished, this, [&] { updateSound(true); });
}
}
} }
}); });
if (changed) {
const auto tree = getTree();
if (!tree) {
_updateNeeded = true;
return;
}
std::lock_guard<std::recursive_mutex> lock(_soundLock);
bool createdSound = false;
withReadLock([&] {
if (shouldCreateSound(tree)) {
_sound = DependencyManager::get<SoundCache>()->getSound(_url);
createdSound = true;
}
});
if (!createdSound) {
_sound = nullptr;
if (_injector) {
DependencyManager::get<AudioInjectorManager>()->stop(_injector);
}
_injector = nullptr;
}
if (_sound) {
if (_sound->isLoaded()) {
updateSound(true);
} else {
connect(_sound.data(), &Resource::finished, this, [&] { updateSound(true); });
}
}
}
} }
bool SoundEntityItem::getLocalOnly() const { bool SoundEntityItem::getLocalOnly() const {
@ -337,21 +379,30 @@ bool SoundEntityItem::getLocalOnly() const {
}); });
} }
bool SoundEntityItem::restartSound() { bool SoundEntityItem::restartSound(bool lock) {
if (lock) {
_soundLock.lock();
}
if (!_sound) { if (!_sound) {
if (lock) {
_soundLock.unlock();
}
return false; return false;
} }
AudioInjectorOptions options; AudioInjectorOptions options;
const glm::quat orientation = getWorldOrientation(); withReadLock([&] {
options.position = getWorldPosition() + orientation * (getScaledDimensions() * (ENTITY_ITEM_DEFAULT_REGISTRATION_POINT - getRegistrationPoint())); const glm::quat orientation = getWorldOrientation();
options.positionSet = _positional; options.position = getWorldPosition() + orientation * (getScaledDimensions() * (ENTITY_ITEM_DEFAULT_REGISTRATION_POINT - getRegistrationPoint()));
options.volume = _volume; options.positionSet = _positional;
options.loop = _loop; options.volume = _volume;
options.orientation = orientation; options.loop = _loop;
options.localOnly = _localOnly || _sound->isAmbisonic(); // force localOnly when ambisonic options.orientation = orientation;
options.secondOffset = _timeOffset; options.localOnly = _localOnly || _sound->isAmbisonic(); // force localOnly when ambisonic
options.pitch = _pitch; options.secondOffset = _timeOffset;
options.pitch = _pitch;
});
// stereo option isn't set from script, this comes from sound metadata or filename // stereo option isn't set from script, this comes from sound metadata or filename
options.stereo = _sound->isStereo(); options.stereo = _sound->isStereo();
@ -363,10 +414,16 @@ bool SoundEntityItem::restartSound() {
_injector = DependencyManager::get<AudioInjectorManager>()->playSound(_sound, options); _injector = DependencyManager::get<AudioInjectorManager>()->playSound(_sound, options);
} }
if (lock) {
_soundLock.unlock();
}
return true; return true;
} }
void SoundEntityItem::updateSound(bool restart) { void SoundEntityItem::updateSound(bool restart) {
std::lock_guard<std::recursive_mutex> lock(_soundLock);
if (!_sound) { if (!_sound) {
return; return;
} }
@ -385,4 +442,4 @@ void SoundEntityItem::updateSound(bool restart) {
DependencyManager::get<AudioInjectorManager>()->stop(_injector); DependencyManager::get<AudioInjectorManager>()->stop(_injector);
} }
} }
} }

View file

@ -78,7 +78,7 @@ public:
void setLocalOnly(bool value); void setLocalOnly(bool value);
bool getLocalOnly() const; bool getLocalOnly() const;
bool restartSound(); bool restartSound(bool lock = false);
protected: protected:
bool shouldCreateSound(const EntityTreePointer& tree) const; bool shouldCreateSound(const EntityTreePointer& tree) const;
@ -93,6 +93,7 @@ protected:
bool _positional { true }; bool _positional { true };
bool _localOnly { false }; bool _localOnly { false };
std::recursive_mutex _soundLock;
SharedSoundPointer _sound; SharedSoundPointer _sound;
AudioInjectorPointer _injector; AudioInjectorPointer _injector;
bool _updateNeeded { false }; bool _updateNeeded { false };