Skip to content

Commit

Permalink
Merge pull request #959 from overte-org/fix/avatar_recording
Browse files Browse the repository at this point in the history
Fixed deadlocks in Recording API
  • Loading branch information
ksuprynowicz authored May 24, 2024
2 parents ec5aa9f + 8badda8 commit 09e5a60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
8 changes: 4 additions & 4 deletions assignment-client/src/AgentScriptingInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
class AgentScriptingInterface : public QObject {
Q_OBJECT
Q_PROPERTY(bool isAvatar READ isAvatar WRITE setIsAvatar)
Q_PROPERTY(bool isAvatar READ getIsAvatar WRITE setIsAvatar)
Q_PROPERTY(bool isPlayingAvatarSound READ isPlayingAvatarSound)
Q_PROPERTY(bool isListeningToAudioStream READ isListeningToAudioStream WRITE setIsListeningToAudioStream)
Q_PROPERTY(bool isNoiseGateEnabled READ isNoiseGateEnabled WRITE setIsNoiseGateEnabled)
Expand Down Expand Up @@ -77,15 +77,15 @@ public slots:

/*@jsdoc
* Checks whether the script is emulating an avatar.
* @function Agent.isAvatar
* @function Agent.getIsAvatar
* @returns {boolean} <code>true</code> if the script is emulating an avatar, otherwise <code>false</code>.
* @example <caption>Check whether the agent is emulating an avatar.</caption>
* (function () {
* print("Agent is avatar: " + Agent.isAvatar());
* print("Agent is avatar: " + Agent.getIsAvatar());
* print("Agent is avatar: " + Agent.isAvatar); // Same result.
* }());
*/
bool isAvatar() const { return _agent->isAvatar(); }
bool getIsAvatar() const { return _agent->isAvatar(); }

/*@jsdoc
* Plays a sound from the position and with the orientation of the emulated avatar's head. No sound is played unless
Expand Down
63 changes: 21 additions & 42 deletions libraries/recording/src/recording/RecordingScriptingInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,33 @@ using namespace recording;
static const QString HFR_EXTENSION = "hfr";

RecordingScriptingInterface::RecordingScriptingInterface() {
Locker(_mutex);
_player = DependencyManager::get<Deck>();
_recorder = DependencyManager::get<Recorder>();
}

bool RecordingScriptingInterface::isPlaying() const {
Locker(_mutex);
return _player->isPlaying();
}

bool RecordingScriptingInterface::isPaused() const {
Locker(_mutex);
return _player->isPaused();
}

float RecordingScriptingInterface::playerElapsed() const {
Locker(_mutex);
return _player->position();
}

float RecordingScriptingInterface::playerLength() const {
Locker(_mutex);
return _player->length();
}

void RecordingScriptingInterface::playClip(NetworkClipLoaderPointer clipLoader, const QString& url, const ScriptValue& callback) {
Locker(_mutex);
_player->queueClip(clipLoader->getClip());

if (callback.isFunction()) {
Expand All @@ -69,10 +75,7 @@ void RecordingScriptingInterface::playClip(NetworkClipLoaderPointer clipLoader,
}

void RecordingScriptingInterface::loadRecording(const QString& url, const ScriptValue& callback) {
if (QThread::currentThread() != thread()) {
BLOCKING_INVOKE_METHOD(this, "loadRecording", Q_ARG(const QString&, url), Q_ARG(const ScriptValue&, callback));
return;
}
Locker(_mutex);

auto clipLoader = DependencyManager::get<recording::ClipCache>()->getClipLoader(url);

Expand Down Expand Up @@ -131,15 +134,12 @@ void RecordingScriptingInterface::startPlaying() {
return;
}

Locker(_mutex);
_player->play();
}

void RecordingScriptingInterface::setPlayerVolume(float volume) {
if (QThread::currentThread() != thread()) {
BLOCKING_INVOKE_METHOD(this, "setPlayerVolume", Q_ARG(float, volume));
return;
}

Locker(_mutex);
_player->setVolume(std::min(std::max(volume, 0.0f), 1.0f));
}

Expand All @@ -152,6 +152,8 @@ void RecordingScriptingInterface::setPlayerTime(float time) {
BLOCKING_INVOKE_METHOD(this, "setPlayerTime", Q_ARG(float, time));
return;
}

Locker(_mutex);
_player->seek(time);
}

Expand All @@ -160,11 +162,7 @@ void RecordingScriptingInterface::setPlayFromCurrentLocation(bool playFromCurren
}

void RecordingScriptingInterface::setPlayerLoop(bool loop) {
if (QThread::currentThread() != thread()) {
BLOCKING_INVOKE_METHOD(this, "setPlayerLoop", Q_ARG(bool, loop));
return;
}

Locker(_mutex);
_player->loop(loop);
}

Expand All @@ -185,10 +183,7 @@ void RecordingScriptingInterface::setPlayerUseSkeletonModel(bool useSkeletonMode
}

void RecordingScriptingInterface::pausePlayer() {
if (QThread::currentThread() != thread()) {
BLOCKING_INVOKE_METHOD(this, "pausePlayer");
return;
}
Locker(_mutex);
_player->pause();
}

Expand All @@ -197,6 +192,8 @@ void RecordingScriptingInterface::stopPlaying() {
BLOCKING_INVOKE_METHOD(this, "stopPlaying");
return;
}

Locker(_mutex);
_player->stop();
}

Expand All @@ -214,11 +211,7 @@ void RecordingScriptingInterface::startRecording() {
return;
}

if (QThread::currentThread() != thread()) {
BLOCKING_INVOKE_METHOD(this, "startRecording");
return;
}

Locker(_mutex);
_recorder->start();
}

Expand All @@ -228,11 +221,7 @@ void RecordingScriptingInterface::stopRecording() {
return;
}

if (QThread::currentThread() != thread()) {
BLOCKING_INVOKE_METHOD(this, "stopRecording");
return;
}

Locker(_mutex);
_recorder->stop();
_lastClip = _recorder->getClip();
_lastClip->seek(0);
Expand All @@ -247,12 +236,7 @@ QString RecordingScriptingInterface::getDefaultRecordingSaveDirectory() {
}

void RecordingScriptingInterface::saveRecording(const QString& filename) {
if (QThread::currentThread() != thread()) {
BLOCKING_INVOKE_METHOD(this, "saveRecording",
Q_ARG(QString, filename));
return;
}

Locker(_mutex);
if (!_lastClip) {
qWarning() << "There is no recording to save";
return;
Expand All @@ -267,14 +251,7 @@ bool RecordingScriptingInterface::saveRecordingToAsset(const ScriptValue& getCli
return false;
}

if (QThread::currentThread() != thread()) {
bool result;
BLOCKING_INVOKE_METHOD(this, "saveRecordingToAsset",
Q_RETURN_ARG(bool, result),
Q_ARG(const ScriptValue&, getClipAtpUrl));
return result;
}

Locker(_mutex);
if (!_lastClip) {
qWarning() << "There is no recording to save";
return false;
Expand Down Expand Up @@ -316,6 +293,8 @@ void RecordingScriptingInterface::loadLastRecording() {
return;
}

Locker(_mutex);

if (!_lastClip) {
qCDebug(scriptengine) << "There is no recording to load";
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ public slots:
using Locker = std::unique_lock<Mutex>;
using Flag = std::atomic<bool>;

Mutex _mutex;

QSharedPointer<recording::Deck> _player;
QSharedPointer<recording::Recorder> _recorder;

Expand Down

0 comments on commit 09e5a60

Please sign in to comment.