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
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,
65+ [this, weakClipLoader, url, callback]() mutable {
7566
76− _player->queueClip(loader->getClip());
77− return true;
78−}
67+ if (auto clipLoader = weakClipLoader.toStrongRef()) {
68+ qCDebug(scriptengine) << "Loaded recording from" << url;
69+
70+ _player->queueClip(clipLoader->getClip());
7971
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+}
8097
8198 void RecordingScriptingInterface::startPlaying() {
8299 if (QThread::currentThread() != thread()) {