History

Merge remote-tracking branch 'upstream/master' into 21249

David Rowe committed 9 years ago

Commit 2731c132 parents f715850 45bb63952 files · +1134 −440Browse files

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

Unified · Split

libraries/script-engine/src/RecordingScriptingInterface.cpp+37−20
@@ -52,31 +52,48 @@ 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,
73 return false;65 [this, weakClipLoader, url, callback]() mutable {
74 }
75 66
76 _player->queueClip(loader->getClip());67 if (auto clipLoader = weakClipLoader.toStrongRef()) {
77 return true;68 qCDebug(scriptengine) << "Loaded recording from" << url;
78}69
70 _player->queueClip(clipLoader->getClip());
79 71
72 if (callback.isFunction()) {
73 QScriptValueList args { true, url };
74 callback.call(_scriptEngine->globalObject(), args);
75 }
76
77 // drop our strong pointer to this clip so it is cleaned up
78 _clipLoaders.remove(clipLoader);
79 }
80 });
81
82 // when clip load fails, call the callback with the URL and failure boolean
83 connect(clipLoader.data(), &recording::NetworkClipLoader::failed, this, [this, weakClipLoader, url, callback](QNetworkReply::NetworkError error) mutable {
84 qCDebug(scriptengine) << "Failed to load recording from" << url;
85
86 if (callback.isFunction()) {
87 QScriptValueList args { false, url };
88 callback.call(_scriptEngine->currentContext()->thisObject(), args);
89 }
90
91 if (auto clipLoader = weakClipLoader.toStrongRef()) {
92 // drop out strong pointer to this clip so it is cleaned up
93 _clipLoaders.remove(clipLoader);
94 }
95 });
96}
80 97
81void RecordingScriptingInterface::startPlaying() {98void RecordingScriptingInterface::startPlaying() {
82 if (QThread::currentThread() != thread()) {99 if (QThread::currentThread() != thread()) {