]> git.sur5r.net Git - minitube/commitdiff
Smarter downloader
authorFlavio <flavio@odisseo.local>
Sat, 29 Jan 2011 21:11:14 +0000 (22:11 +0100)
committerFlavio <flavio@odisseo.local>
Sat, 29 Jan 2011 21:11:14 +0000 (22:11 +0100)
src/downloaditem.cpp
src/downloaditem.h

index 4c26c87a3af808c6138d17315e964e4215b22795..f37f7c21cf7e48b48decac87b5c826352257b687 100644 (file)
@@ -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<Video *>(sender());
+    if (!video) {
+        qDebug() << "Cannot get sender";
+        return;
     }
+    video->disconnect(this);
 
-    emit progress(percent);
-    // emit statusChanged();
+    m_reply = The::http()->simpleGet(video->getStreamUrl());
+    init();
 }
 
 qint64 DownloadItem::bytesTotal() const {
index b4f59f50ca3e0eda45fdc510c33c3afa413dbd0f..58bb49353e87d5dbbb5c567df9c461d2820d079d 100644 (file)
@@ -20,8 +20,10 @@ class DownloadItem : public QObject {
 
 signals:
     void statusChanged();
+    void bufferProgress(int percent);
     void progress(int percent);
     void finished();
+    void error(QString);
 
 public:
     DownloadItem(Video *video, QUrl url, QString filename, QObject *parent = 0);
@@ -52,9 +54,12 @@ private slots:
     void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
     void metaDataChanged();
     void requestFinished();
+    void gotStreamUrl(QUrl streamUrl);
+    void speedCheck();
 
 private:
     void init();
+    int initialBufferSize();
 
     qint64 m_bytesReceived;
     QTime m_downloadTime;
@@ -72,6 +77,8 @@ private:
     DownloadItemStatus m_status;
     QString m_errorMessage;
 
+    QTimer *speedCheckTimer;
+
 };
 
 // This is required in order to use QPointer<DownloadItem> as a QVariant