Showing the changes to libraries/script-engine/src/RecordingScriptingInterface.cpp (1 of 10 files). Show all
Unified · Split
| @@ -52,31 +52,47 @@ float RecordingScriptingInterface::playerLength() const { · 51 unmodified lines | ||
| 52 | 52 | return _player->length(); |
| 53 | 53 | } |
| 54 | 54 | |
| 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); | |
| 57 | 57 | |
| 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); | |
| 63 | 60 | |
| 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(); | |
| 70 | 62 | |
| 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 { | |
| 75 | 65 | |
| 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()); | |
| 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 | |
| 81 | 97 | void RecordingScriptingInterface::startPlaying() { |
| 82 | 98 | if (QThread::currentThread() != thread()) { |