Add byte range support to HTTPResourceRequest

This commit is contained in:
Ryan Huffman 2017-03-31 15:07:34 -07:00 committed by Atlante45
parent 6e6fd608dd
commit 105d17e85e
11 changed files with 43 additions and 3 deletions

View file

@ -188,6 +188,7 @@
#include <src/scripting/LimitlessVoiceRecognitionScriptingInterface.h>
#include <EntityScriptClient.h>
#include <ModelScriptingInterface.h>
#include <QtNetwork/QNetworkProxy>
// On Windows PC, NVidia Optimus laptop, we want to enable NVIDIA GPU
// FIXME seems to be broken.
@ -604,6 +605,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo
{
const QString TEST_SCRIPT = "--testScript";
const QString TRACE_FILE = "--traceFile";
const QString HTTP_PROXY = "--httpProxy";
const QStringList args = arguments();
for (int i = 0; i < args.size() - 1; ++i) {
if (args.at(i) == TEST_SCRIPT) {
@ -615,10 +617,17 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo
QString traceFilePath = args.at(i + 1);
setProperty(hifi::properties::TRACING, traceFilePath);
DependencyManager::get<tracing::Tracer>()->startTracing();
} else if (args.at(i) == HTTP_PROXY) {
}
}
}
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName("127.0.0.1");
proxy.setPort(8888);
QNetworkProxy::setApplicationProxy(proxy);
// make sure the debug draw singleton is initialized on the main thread.
DebugDraw::getInstance().removeMarker("");

View file

@ -53,6 +53,7 @@ void Image3DOverlay::update(float deltatime) {
}
void Image3DOverlay::render(RenderArgs* args) {
qDebug() << _url;
if (!_isLoaded) {
_isLoaded = true;
_texture = DependencyManager::get<TextureCache>()->getTexture(_url);

View file

@ -379,6 +379,8 @@ namespace ktx {
void setCubeArray(uint32_t width, uint32_t height, uint32_t numSlices) { setDimensions(width, height, 0, (numSlices > 0 ? numSlices : 1), NUM_CUBEMAPFACES); }
};
static const size_t KTX_HEADER_SIZE = 64;
static_assert(sizeof(Header) == KTX_HEADER_SIZE, "KTX Header size is static");
// Key Values
struct KeyValue {

View file

@ -266,7 +266,8 @@ QSharedPointer<Resource> TextureCache::createResource(const QUrl& url, const QSh
NetworkTexture::NetworkTexture(const QUrl& url, image::TextureUsage::Type type, const QByteArray& content, int maxNumPixels) :
Resource(url),
_type(type),
_maxNumPixels(maxNumPixels)
_maxNumPixels(maxNumPixels),
_sourceIsKTX(url.path().endsWith(".ktx"))
{
_textureSource = std::make_shared<gpu::TextureSource>();
@ -274,6 +275,11 @@ NetworkTexture::NetworkTexture(const QUrl& url, image::TextureUsage::Type type,
_loaded = true;
}
if (_sourceIsKTX) {
_requestByteRange.fromInclusive = 0;
_requestByteRange.toExclusive = 1000;
}
// if we have content, load it after we have our self pointer
if (!content.isEmpty()) {
_startedLoading = true;

View file

@ -73,6 +73,7 @@ private:
image::TextureUsage::Type _type;
KTXFilePointer _file;
bool _sourceIsKTX { false };
int _originalWidth { 0 };
int _originalHeight { 0 };
int _width { 0 };

View file

@ -59,6 +59,12 @@ void HTTPResourceRequest::doSend() {
networkRequest.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
}
if (_byteRange.isSet()) {
auto byteRange = QString("bytes={}-{}").arg(_byteRange.fromInclusive).arg(_byteRange.toExclusive);
networkRequest.setRawHeader("Range", byteRange.toLatin1());
}
networkRequest.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
_reply = NetworkAccessManager::getInstance().get(networkRequest);
connect(_reply, &QNetworkReply::finished, this, &HTTPResourceRequest::onRequestFinished);

View file

@ -676,6 +676,8 @@ void Resource::makeRequest() {
return;
}
_request->setByteRange(_requestByteRange);
qCDebug(resourceLog).noquote() << "Starting request for:" << _url.toDisplayString();
emit loading();

View file

@ -442,6 +442,7 @@ protected:
QUrl _url;
QUrl _activeUrl;
ByteRange _requestByteRange;
bool _startedLoading = false;
bool _failedToLoad = false;
bool _loaded = false;

View file

@ -26,6 +26,7 @@ const QString URL_SCHEME_ATP = "atp";
class ResourceManager {
public:
static void setUrlPrefixOverride(const QString& prefix, const QString& replacement);
static QString normalizeURL(const QString& urlString);
static QUrl normalizeURL(const QUrl& url);

View file

@ -17,6 +17,13 @@
#include <cstdint>
struct ByteRange {
int64_t fromInclusive { 0 };
int64_t toExclusive { 0 };
bool isSet() { return fromInclusive < -1 || fromInclusive < toExclusive; }
};
class ResourceRequest : public QObject {
Q_OBJECT
public:
@ -48,6 +55,7 @@ public:
bool loadedFromCache() const { return _loadedFromCache; }
void setCacheEnabled(bool value) { _cacheEnabled = value; }
void setByteRange(ByteRange byteRange) { _byteRange = byteRange; }
public slots:
void send();
@ -65,6 +73,7 @@ protected:
QByteArray _data;
bool _cacheEnabled { true };
bool _loadedFromCache { false };
ByteRange _byteRange;
};
#endif

View file

@ -2317,6 +2317,8 @@ void ScriptEngine::unloadEntityScript(const EntityItemID& entityID, bool shouldR
if (_entityScripts.contains(entityID)) {
const EntityScriptDetails &oldDetails = _entityScripts[entityID];
auto scriptText = oldDetails.scriptText;
if (isEntityScriptRunning(entityID)) {
callEntityScriptMethod(entityID, "unload");
}
@ -2334,14 +2336,14 @@ void ScriptEngine::unloadEntityScript(const EntityItemID& entityID, bool shouldR
newDetails.status = EntityScriptStatus::UNLOADED;
newDetails.lastModified = QDateTime::currentMSecsSinceEpoch();
// keep scriptText populated for the current need to "debouce" duplicate calls to unloadEntityScript
newDetails.scriptText = oldDetails.scriptText;
newDetails.scriptText = scriptText;
setEntityScriptDetails(entityID, newDetails);
}
stopAllTimersForEntityScript(entityID);
{
// FIXME: shouldn't have to do this here, but currently something seems to be firing unloads moments after firing initial load requests
processDeferredEntityLoads(oldDetails.scriptText, entityID);
processDeferredEntityLoads(scriptText, entityID);
}
}
}