History

cleanup Agent recording handling, make loadRecording async

Stephen Birarda committed 9 years ago

Commit c7a28a51 parent 6ed429510 files · +107 −37Browse files

Showing the changes to libraries/script-engine/src/RecordingScriptingInterface.cpp (1 of 10 files). Show all

Unified · Split

libraries/script-engine/src/RecordingScriptingInterface.cpp+36−20
@@ -52,31 +52,47 @@ float RecordingScriptingInterface::playerLength() const { · 51 unmodified lines
5252 return _player->length();
5353 }
5454
55−bool RecordingScriptingInterface::loadRecording(const QString& url) {
56− using namespace recording;
55+void RecordingScriptingInterface::loadRecording(const QString& url, QScriptValue callback) {
56+ auto clipLoader = DependencyManager::get<recording::ClipCache>()->getClipLoader(url);
5757
58− auto loader = ClipCache::instance().getClipLoader(url);
59− if (!loader) {
60− qWarning() << "Clip failed to load from " << url;
61− return false;
62− }
58+ // hold a strong pointer to the loading clip so that it has a chance to load
59+ _clipLoaders.insert(clipLoader);
6360
64− if (!loader->isLoaded()) {
65− QEventLoop loop;
66− QObject::connect(loader.data(), &Resource::loaded, &loop, &QEventLoop::quit);
67− QObject::connect(loader.data(), &Resource::failed, &loop, &QEventLoop::quit);
68− loop.exec();
69− }
61+ auto weakClipLoader = clipLoader.toWeakRef();
7062
71− if (!loader->isLoaded()) {
72− qWarning() << "Clip failed to load from " << url;
73− return false;
74− }
63+ // when clip loaded, call the callback with the URL and success boolean
64+ connect(clipLoader.data(), &recording::NetworkClipLoader::clipLoaded, this, [this, weakClipLoader, url, callback]() mutable {
7565
76− _player->queueClip(loader->getClip());
77− return true;
78−}
66+ if (auto clipLoader = weakClipLoader.toStrongRef()) {
67+ qCDebug(scriptengine) << "Loaded recording from" << url;
68+
69+ _player->queueClip(clipLoader->getClip());
7970
71+ if (callback.isFunction()) {
72+ QScriptValueList args { true, url };
73+ callback.call(_scriptEngine->globalObject(), args);
74+ }
75+
76+ // drop our strong pointer to this clip so it is cleaned up
77+ _clipLoaders.remove(clipLoader);
78+ }
79+ });
80+
81+ // when clip load fails, call the callback with the URL and failure boolean
82+ connect(clipLoader.data(), &recording::NetworkClipLoader::failed, this, [this, weakClipLoader, url, callback](QNetworkReply::NetworkError error) mutable {
83+ qCDebug(scriptengine) << "Failed to load recording from" << url;
84+
85+ if (callback.isFunction()) {
86+ QScriptValueList args { false, url };
87+ callback.call(_scriptEngine->currentContext()->thisObject(), args);
88+ }
89+
90+ if (auto clipLoader = weakClipLoader.toStrongRef()) {
91+ // drop out strong pointer to this clip so it is cleaned up
92+ _clipLoaders.remove(clipLoader);
93+ }
94+ });
95+}
8096
8197 void RecordingScriptingInterface::startPlaying() {
8298 if (QThread::currentThread() != thread()) {