X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fdownloaditem.cpp;h=f70624be170554c1275c5436aa5216150d42f819;hb=d1f0fac328bdf71b840ac6ac9f6fed8f324d02bb;hp=ab92fc328a345c102d82f1a9acbb2534c12acf4b;hpb=7b529d6a918efe39ca6d63201fcdb954a3c881b4;p=minitube diff --git a/src/downloaditem.cpp b/src/downloaditem.cpp index ab92fc3..f70624b 100644 --- a/src/downloaditem.cpp +++ b/src/downloaditem.cpp @@ -18,7 +18,17 @@ 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; + if (video) delete video; +} void DownloadItem::start() { m_reply = The::http()->simpleGet(m_url); @@ -29,6 +39,9 @@ void DownloadItem::init() { if (!m_reply) return; + if (m_file.exists()) + m_file.remove(); + m_status = Starting; m_startedSaving = false; @@ -48,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()); @@ -90,7 +104,7 @@ void DownloadItem::tryAgain() { void DownloadItem::downloadReadyRead() { if (!m_file.isOpen()) { - if (!m_file.open(QIODevice::WriteOnly)) { + if (!m_file.open(QIODevice::ReadWrite)) { qDebug() << QString("Error opening output file: %1").arg(m_file.errorString()); stop(); emit statusChanged(); @@ -99,7 +113,6 @@ void DownloadItem::downloadReadyRead() { emit statusChanged(); } - m_status = Downloading; if (-1 == m_file.write(m_reply->readAll())) { /* downloadInfoLabel->setText(tr("Error saving: %1") @@ -108,7 +121,10 @@ void DownloadItem::downloadReadyRead() { */ } else { m_startedSaving = true; - if (m_finishedDownloading) + if (m_status != Downloading) { + // m_status = Downloading; + // emit statusChanged(); + } else if (m_finishedDownloading) requestFinished(); } } @@ -119,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; @@ -134,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(); @@ -145,20 +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(); + } +} + +void DownloadItem::gotStreamUrl(QUrl streamUrl) { + + Video *video = static_cast