From 57550a8424b293ce0ab0ee7adc0a247347dc3d97 Mon Sep 17 00:00:00 2001 From: Flavio Date: Sat, 29 Jan 2011 22:11:14 +0100 Subject: [PATCH] Smarter downloader --- src/downloaditem.cpp | 95 ++++++++++++++++++++++++++++++++++++-------- src/downloaditem.h | 7 ++++ 2 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/downloaditem.cpp b/src/downloaditem.cpp index 4c26c87..f37f7c2 100644 --- a/src/downloaditem.cpp +++ b/src/downloaditem.cpp @@ -18,7 +18,12 @@ DownloadItem::DownloadItem(Video *video, QUrl url, QString filename, QObject *pa , m_reply(0) , video(video) , m_status(Idle) -{ } +{ + speedCheckTimer = new QTimer(this); + speedCheckTimer->setInterval(2000); + speedCheckTimer->setSingleShot(true); + connect(speedCheckTimer, SIGNAL(timeout()), SLOT(speedCheck())); +} DownloadItem::~DownloadItem() { if (m_reply) delete m_reply; @@ -34,6 +39,9 @@ void DownloadItem::init() { if (!m_reply) return; + if (m_file.exists()) + m_file.remove(); + m_status = Starting; m_startedSaving = false; @@ -53,6 +61,7 @@ void DownloadItem::init() { // start timer for the download estimation m_downloadTime.start(); + speedCheckTimer->start(); if (m_reply->error() != QNetworkReply::NoError) { error(m_reply->error()); @@ -126,6 +135,8 @@ void DownloadItem::error(QNetworkReply::NetworkError) { qDebug() << "DownloadItem::" << __FUNCTION__ << m_reply->errorString() << m_url; #endif + qDebug() << m_reply->errorString(); + m_errorMessage = m_reply->errorString(); m_reply = 0; m_status = Failed; @@ -141,6 +152,7 @@ void DownloadItem::metaDataChanged() { QVariant locationHeader = m_reply->header(QNetworkRequest::LocationHeader); if (locationHeader.isValid()) { m_url = locationHeader.toUrl(); + // qDebug() << "Redirecting to" << m_url; m_reply->deleteLater(); m_reply = The::http()->simpleGet(m_url); init(); @@ -152,30 +164,81 @@ void DownloadItem::metaDataChanged() { #endif } +int DownloadItem::initialBufferSize() { + // qDebug() << video->getDefinitionCode(); + switch (video->getDefinitionCode()) { + case 18: + return 1024*192; + case 22: + return 1024*512; + case 37: + return 1024*768; + } + return 1024*128; +} + void DownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { - QTime now = QTime::currentTime(); - if (m_lastProgressTime.msecsTo(now) < 200) - return; - m_lastProgressTime = now; + if (m_lastProgressTime.elapsed() < 150) return; + m_lastProgressTime.start(); m_bytesReceived = bytesReceived; - if (bytesTotal > 0) { - percent = bytesReceived * 100 / bytesTotal; + + if (m_status != Downloading) { + + int neededBytes = (int) (bytesTotal * .01); + qDebug() << bytesReceived << bytesTotal << neededBytes << m_downloadTime.elapsed(); + int bufferSize = initialBufferSize(); + if (bytesReceived > bufferSize + && bytesReceived > neededBytes + && m_downloadTime.elapsed() > 1000 ) { + emit bufferProgress(100); + m_status = Downloading; + emit statusChanged(); + } else { + int bufferPercent = bytesReceived * 100 / qMax(bufferSize, neededBytes); + emit bufferProgress(bufferPercent); + } + + } else { + + if (bytesTotal > 0) { + int percent = bytesReceived * 100 / bytesTotal; + if (percent != this->percent) { + this->percent = percent; + emit progress(percent); + } + } + + } +} + +void DownloadItem::speedCheck() { + if (!m_reply) return; + if (m_bytesReceived < initialBufferSize() / 3) { + m_reply->disconnect(); + m_reply->abort(); + m_reply->deleteLater(); + m_reply = 0; + + // too slow! retry + qDebug() << "Retrying..."; + connect(video, SIGNAL(gotStreamUrl(QUrl)), SLOT(gotStreamUrl(QUrl))); + video->loadStreamUrl(); } +} - // qDebug() << bytesReceived << bytesTotal << bytesTotal * .005 << m_downloadTime.msecsTo(now) << percent; +void DownloadItem::gotStreamUrl(QUrl streamUrl) { - if (m_status != Downloading - && bytesReceived > 1024 * 512 - && bytesReceived > bytesTotal * .01 - && m_downloadTime.msecsTo(now) > 1500 ) { - m_status = Downloading; - emit statusChanged(); + Video *video = static_cast