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
52 return _player->length();52 return _player->length();
53}53}
54 54
55bool RecordingScriptingInterface::loadRecording(const QString& url) {55void RecordingScriptingInterface::loadRecording(const QString& url, QScriptValue callback) {
56 using namespace recording;56 auto clipLoader = DependencyManager::get<recording::ClipCache>()->getClipLoader(url);
57 57
58 auto loader = ClipCache::instance().getClipLoader(url);58 // hold a strong pointer to the loading clip so that it has a chance to load
59 if (!loader) {59 _clipLoaders.insert(clipLoader);
60 qWarning() << "Clip failed to load from " << url;
61 return false;
62 }
63 60
64 if (!loader->isLoaded()) {61 auto weakClipLoader = clipLoader.toWeakRef();
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 }
70 62
71 if (!loader->isLoaded()) {63 // when clip loaded, call the callback with the URL and success boolean
72 qWarning() << "Clip failed to load from " << url;64 connect(clipLoader.data(), &recording::NetworkClipLoader::clipLoaded, this, [this, weakClipLoader, url, callback]() mutable {
73 return false;
74 }
75 65
76 _player->queueClip(loader->getClip());66 if (auto clipLoader = weakClipLoader.toStrongRef()) {
77 return true;67 qCDebug(scriptengine) << "Loaded recording from" << url;
78}68
69 _player->queueClip(clipLoader->getClip());
79 70
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}
80 96
81void RecordingScriptingInterface::startPlaying() {97void RecordingScriptingInterface::startPlaying() {
82 if (QThread::currentThread() != thread()) {98 if (QThread::currentThread() != thread()) {