, m_startedSaving(false)
, m_finishedDownloading(false)
, m_url(url)
+ , m_offset(0)
+ , sendStatusChanges(true)
, m_file(filename)
, m_reply(0)
, video(video)
speedCheckTimer->setInterval(2000);
speedCheckTimer->setSingleShot(true);
connect(speedCheckTimer, SIGNAL(timeout()), SLOT(speedCheck()));
+
+ if (m_file.exists())
+ m_file.remove();
}
DownloadItem::~DownloadItem() {
}
}
+bool DownloadItem::needsDownload(qint64 offset) {
+ return offset < m_offset || offset > m_offset + m_bytesReceived;
+}
+
+bool DownloadItem::isBuffered(qint64 offset) {
+ QMap<qint64, qint64>::iterator i;
+ for (i = buffers.begin(); i != buffers.end(); ++i) {
+ if (offset >= i.key() && offset <= i.value()) {
+ // qDebug() << "Buffered! " << i.key() << ":" << i.value();
+ return true;
+ }
+ }
+ // qDebug() << offset << "is not buffered";
+ return false;
+}
+
+qint64 DownloadItem::blankAtOffset(qint64 offset) {
+ // qDebug() << buffers;
+ QMap<qint64, qint64>::iterator i;
+ for (i = buffers.begin(); i != buffers.end(); ++i) {
+ if (offset >= i.key() && offset <= i.value()) {
+ // qDebug() << "Offset was" << offset << "now" << i.value();
+ return i.value() + 1;
+ }
+ }
+ return offset;
+}
+
+void DownloadItem::seekTo(qint64 offset, bool sendStatusChanges) {
+ // qDebug() << __PRETTY_FUNCTION__ << offset << sendStatusChanges;
+ stop();
+ if (m_bytesReceived > 0) {
+ bool bufferModified = false;
+ QMap<qint64, qint64>::iterator i;
+ for (i = buffers.begin(); i != buffers.end(); ++i) {
+ if (m_offset - 1 <= i.value()) {
+ /*
+ qDebug() << "Extending existing buffer "
+ << i.key() << i.value() << "now" << m_offset + m_bytesReceived;
+ */
+ bufferModified = true;
+ i.value() = m_offset + m_bytesReceived;
+ break;
+ }
+ }
+ if (!bufferModified)
+ buffers.insert(m_offset, m_offset + m_bytesReceived);
+ }
+ m_offset = offset;
+ this->sendStatusChanges = sendStatusChanges;
+ bool seekSuccess = m_file.seek(offset);
+ if (!seekSuccess) {
+ qWarning() << "Cannot seek to offset" << offset << "in file" << m_file.fileName();
+ return;
+ }
+ start();
+}
+
void DownloadItem::start() {
- m_reply = The::http()->request(m_url);
+ // qDebug() << "Starting download at" << m_offset;
+ if (m_offset > 0)
+ m_reply = The::http()->request(m_url, QNetworkAccessManager::GetOperation, QByteArray(), m_offset);
+ else
+ m_reply = The::http()->request(m_url);
init();
}
if (!m_reply)
return;
- if (m_file.exists())
- m_file.remove();
-
m_status = Starting;
-
+ m_bytesReceived = 0;
m_startedSaving = false;
m_finishedDownloading = false;
emit statusChanged();
}
+ // qWarning() << __PRETTY_FUNCTION__ << m_file.pos();
if (-1 == m_file.write(m_reply->readAll())) {
qWarning() << "Error saving." << m_file.errorString();
} else {
tryAgain();
return;
}
-
-#ifdef DOWNLOADMANAGER_DEBUG
- qDebug() << "DownloadItem::" << __FUNCTION__ << "not handled.";
-#endif
+ // qDebug() << m_reply->rawHeaderList();
}
int DownloadItem::initialBufferSize() {
void DownloadItem::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
- // qDebug() << bytesReceived << bytesTotal << m_downloadTime.elapsed();
+ // qDebug() << __PRETTY_FUNCTION__ << bytesReceived << bytesTotal << m_downloadTime.elapsed();
+
+ m_bytesReceived = bytesReceived;
+
+ if (!sendStatusChanges) return;
if (m_lastProgressTime.elapsed() < 150) return;
m_lastProgressTime.start();
- m_bytesReceived = bytesReceived;
-
if (m_status != Downloading) {
int neededBytes = (int) (bytesTotal * .005);
m_status = Downloading;
emit statusChanged();
}
- m_file.close();
+ if (m_offset == 0) m_file.close();
m_status = Finished;
m_totalTime = m_downloadTime.elapsed() / 1000.0;
emit statusChanged();
#include "database.h"
#include "videoareawidget.h"
#include "jsfunctions.h"
+#include "seekslider.h"
static MainWindow *singleton = 0;
connect(action, SIGNAL(triggered()), mediaView, SLOT(relatedVideos()));
actions->insert("related-videos", action);
+ action = new QAction(tr("Open in &Browser..."), this);
+ action->setEnabled(false);
+ actions->insert("open-in-browser", action);
+ connect(action, SIGNAL(triggered()), mediaView, SLOT(openInBrowser()));
+
#ifdef APP_ACTIVATION
Extra::createActivationAction(tr("Buy %1...").arg(Constants::NAME));
#endif
videoMenu->addSeparator();
videoMenu->addAction(The::globalActions()->value("download"));
videoMenu->addAction(copyLinkAct);
+ videoMenu->addAction(The::globalActions()->value("open-in-browser"));
// videoMenu->addAction(The::globalActions()->value("snapshot"));
QMenu* viewMenu = menuBar()->addMenu(tr("&View"));
currentTime->setFont(smallerFont);
mainToolBar->addWidget(currentTime);
+#ifdef APP_PHONON_SEEK
mainToolBar->addWidget(new Spacer());
-
seekSlider = new Phonon::SeekSlider(this);
+ seekSlider->setVisible(false);
seekSlider->setIconVisible(false);
seekSlider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
mainToolBar->addWidget(seekSlider);
+#endif
- /*
mainToolBar->addWidget(new Spacer());
- slider = new QSlider(this);
+ slider = new SeekSlider(this);
+ slider->setEnabled(false);
+ slider->setTracking(false);
+ slider->setMaximum(1000);
slider->setOrientation(Qt::Horizontal);
slider->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
mainToolBar->addWidget(slider);
-*/
mainToolBar->addWidget(new Spacer());
this, SLOT(stateChanged(Phonon::State, Phonon::State)));
connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
connect(mediaObject, SIGNAL(totalTimeChanged(qint64)), this, SLOT(totalTimeChanged(qint64)));
+#ifdef APP_PHONON_SEEK
seekSlider->setMediaObject(mediaObject);
+#endif
audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory, this);
connect(audioOutput, SIGNAL(volumeChanged(qreal)), this, SLOT(volumeChanged(qreal)));
connect(audioOutput, SIGNAL(mutedChanged(bool)), this, SLOT(volumeMutedChanged(bool)));
const qint64 remainingTime = mediaObject->remainingTime();
currentTime->setStatusTip(tr("Remaining time: %1").arg(formatTime(remainingTime)));
- /*
slider->blockSignals(true);
- slider->setValue(time/1000);
+ const qint64 totalTime = mediaObject->totalTime();
+ // qWarning() << totalTime << time << time * 100 / totalTime;
+ if (totalTime > 0 && time > 0 && !slider->isSliderDown() && mediaObject->state() == Phonon::PlayingState)
+ slider->setValue(time * slider->maximum() / totalTime);
slider->blockSignals(false);
- */
}
void MainWindow::totalTimeChanged(qint64 time) {
<< The::globalActions()->value("webpage")
<< The::globalActions()->value("pagelink")
<< The::globalActions()->value("videolink")
+ << The::globalActions()->value("open-in-browser")
<< The::globalActions()->value("findVideoParts")
<< The::globalActions()->value("skip")
<< The::globalActions()->value("previous")
- // << The::globalActions()->value("download")
<< The::globalActions()->value("stopafterthis")
<< The::globalActions()->value("related-videos")
<< The::globalActions()->value("refine-search")
<< The::globalActions()->value("facebook")
<< The::globalActions()->value("buffer")
<< The::globalActions()->value("email");
+
+ QSlider *slider = MainWindow::instance()->getSlider();
+ connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
}
void MediaView::setMediaObject(Phonon::MediaObject *mediaObject) {
connect(mediaObject, SIGNAL(finished()), SLOT(playbackFinished()));
connect(mediaObject, SIGNAL(stateChanged(Phonon::State, Phonon::State)),
SLOT(stateChanged(Phonon::State, Phonon::State)));
- /*
- const char* s = Constants::NAME;
- const int l = strlen(s);
- int t = The::globalActions()->count();
- for (int i = 0; i < l; i++) {
- t += s[i];
- qDebug() << t << The::globalActions()->count();
- }
- qDebug() << t << The::globalActions()->count();
- if (t != s[0]) return;
- */
connect(mediaObject, SIGNAL(aboutToFinish()), SLOT(aboutToFinish()));
}
downloadItem->stop();
delete downloadItem;
downloadItem = 0;
+ currentVideoSize = 0;
}
The::globalActions()->value("refine-search")->setChecked(false);
updateSubscriptionAction(0, false);
mediaObject->stop();
currentVideoId.clear();
+
+ QSlider *slider = MainWindow::instance()->getSlider();
+ slider->setEnabled(false);
+ slider->setValue(0);
}
const QString & MediaView::getCurrentVideoId() {
downloadItem->stop();
delete downloadItem;
downloadItem = 0;
+ currentVideoSize = 0;
}
Video *video = playlistModel->videoAt(row);
// enable/disable actions
The::globalActions()->value("download")->setEnabled(
DownloadManager::instance()->itemForVideo(video) == 0);
- // The::globalActions()->value("skip")->setEnabled(true);
The::globalActions()->value("previous")->setEnabled(row > 0);
The::globalActions()->value("stopafterthis")->setEnabled(true);
The::globalActions()->value("related-videos")->setEnabled(true);
foreach (QAction *action, currentVideoActions)
action->setEnabled(true);
+ QSlider *slider = MainWindow::instance()->getSlider();
+ slider->setEnabled(false);
+ slider->setValue(0);
+
// see you in gotStreamUrl...
}
mediaObject->setCurrentSource(streamUrl);
mediaObject->play();
#else
- QString tempFile = Temporary::filename();
- Video *videoCopy = video->clone();
- if (downloadItem) {
- downloadItem->stop();
- delete downloadItem;
- }
- downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
- connect(downloadItem, SIGNAL(statusChanged()),
- SLOT(downloadStatusChanged()), Qt::UniqueConnection);
- // connect(downloadItem, SIGNAL(progress(int)), SLOT(downloadProgress(int)));
- connect(downloadItem, SIGNAL(bufferProgress(int)),
- loadingWidget, SLOT(bufferStatus(int)), Qt::UniqueConnection);
- // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
- connect(video, SIGNAL(errorStreamUrl(QString)),
- SLOT(handleError(QString)), Qt::UniqueConnection);
- connect(downloadItem, SIGNAL(error(QString)),
- SLOT(handleError(QString)), Qt::UniqueConnection);
- downloadItem->start();
+ startDownloading();
#endif
// ensure we always have 10 videos ahead
ChannelAggregator::instance()->videoWatched(video);
}
-/*
-void MediaView::downloadProgress(int percent) {
- MainWindow* mainWindow = dynamic_cast<MainWindow*>(window());
-
- mainWindow->getSeekSlider()->setStyleSheet(" QSlider::groove:horizontal {"
- "border: 1px solid #999999;"
- // "border-left: 50px solid rgba(255, 0, 0, 128);"
- "height: 8px;"
- "background: qlineargradient(x1:0, y1:0, x2:.5, y2:0, stop:0 rgba(255, 0, 0, 92), stop:"
- + QString::number(percent/100.0) +
-
- " rgba(255, 0, 0, 92), stop:" + QString::number((percent+1)/100.0) + " transparent, stop:1 transparent);"
- "margin: 2px 0;"
- "}"
- "QSlider::handle:horizontal {"
- "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);"
- "border: 1px solid #5c5c5c;"
- "width: 16px;"
- "height: 16px;"
- "margin: -2px 0;"
- "border-radius: 8px;"
- "}"
-
- );
-}
-*/
-
void MediaView::downloadStatusChanged() {
+ // qDebug() << __PRETTY_FUNCTION__;
switch(downloadItem->status()) {
case Downloading:
- startPlaying();
+ // qDebug() << "Downloading";
+ if (downloadItem->offset() == 0) startPlaying();
+ else {
+ // qDebug() << "Seeking to" << downloadItem->offset();
+ mediaObject->seek(offsetToTime(downloadItem->offset()));
+ mediaObject->play();
+ }
break;
case Starting:
// qDebug() << "Starting";
break;
case Finished:
// qDebug() << "Finished" << mediaObject->state();
- // if (mediaObject->state() == Phonon::StoppedState) startPlaying();
#ifdef Q_WS_X11
- MainWindow::instance()->getSeekSlider()->setEnabled(mediaObject->isSeekable());
+ // MainWindow::instance()->getSeekSlider()->setEnabled(mediaObject->isSeekable());
#endif
break;
case Failed:
// qDebug() << "Failed";
+ skip();
+ break;
case Idle:
// qDebug() << "Idle";
break;
}
void MediaView::startPlaying() {
+ // qDebug() << __PRETTY_FUNCTION__;
if (stopped) return;
if (!downloadItem) {
skip();
return;
}
+ if (downloadItem->offset() == 0) {
+ currentVideoSize = downloadItem->bytesTotal();
+ // qDebug() << "currentVideoSize" << currentVideoSize;
+ }
+
// go!
QString source = downloadItem->currentFilename();
- // qDebug() << "Playing" << source;
+ qDebug() << "Playing" << source << QFile::exists(source);
mediaObject->setCurrentSource(source);
mediaObject->play();
#ifdef Q_WS_X11
- MainWindow::instance()->getSeekSlider()->setEnabled(false);
+ // MainWindow::instance()->getSeekSlider()->setEnabled(false);
#endif
+ QSlider *slider = MainWindow::instance()->getSlider();
+ slider->setEnabled(true);
}
void MediaView::itemActivated(const QModelIndex &index) {
Video *activeVideo = playlistModel->activeVideo();
Video *video = playlistModel->videoAt(index.row());
if (activeVideo && video && activeVideo == video) {
- mediaObject->seek(0);
+ // mediaObject->seek(0);
+ sliderMoved(0);
mediaObject->play();
} else playlistModel->setActiveRow(index.row());
qint64 totalTime = mediaObject->totalTime();
qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
if (totalTime < 1 || currentTime + 10000 < totalTime) {
- // mediaObject->seek(mediaObject->currentTime());
// QTimer::singleShot(500, this, SLOT(playbackResume()));
mediaObject->seek(currentTime);
mediaObject->play();
void MediaView::playbackFinished() {
if (stopped) return;
- const int totalTime = mediaObject->totalTime();
- const int currentTime = mediaObject->currentTime();
+ const qint64 totalTime = mediaObject->totalTime();
+ const qint64 currentTime = mediaObject->currentTime();
qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime() << totalTime;
// add 10 secs for imprecise Phonon backends (VLC, Xine)
if (totalTime < 1 || (currentTime > 0 && currentTime + 10000 < totalTime)) {
void MediaView::playbackResume() {
if (stopped) return;
- qDebug() << __PRETTY_FUNCTION__ << mediaObject->currentTime();
- mediaObject->seek(mediaObject->currentTime());
+ const qint64 currentTime = mediaObject->currentTime();
+ qDebug() << __PRETTY_FUNCTION__ << currentTime;
+ if (currentTime > 0)
+ mediaObject->seek(currentTime);
mediaObject->play();
}
MainWindow::instance()->showMessage(message);
}
+void MediaView::openInBrowser() {
+ Video* video = playlistModel->activeVideo();
+ if (!video) return;
+ mediaObject->pause();
+ QDesktopServices::openUrl(video->getStreamUrl());
+}
+
void MediaView::removeSelected() {
if (!playlistView->selectionModel()->hasSelection()) return;
QModelIndexList indexes = playlistView->selectionModel()->selectedIndexes();
videoAreaWidget->showFullScreen();
}
-/*
-void MediaView::setSlider(QSlider *slider) {
- this->slider = slider;
- // slider->setEnabled(false);
- slider->setTracking(false);
- // connect(slider, SIGNAL(valueChanged(int)), SLOT(sliderMoved(int)));
+void MediaView::startDownloading() {
+ Video *video = playlistModel->activeVideo();
+ if (!video) return;
+ Video *videoCopy = video->clone();
+ if (downloadItem) {
+ downloadItem->stop();
+ delete downloadItem;
+ }
+ QString tempFile = Temporary::filename();
+ downloadItem = new DownloadItem(videoCopy, video->getStreamUrl(), tempFile, this);
+ connect(downloadItem, SIGNAL(statusChanged()),
+ SLOT(downloadStatusChanged()), Qt::UniqueConnection);
+ connect(downloadItem, SIGNAL(bufferProgress(int)),
+ loadingWidget, SLOT(bufferStatus(int)), Qt::UniqueConnection);
+ // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
+ connect(video, SIGNAL(errorStreamUrl(QString)),
+ SLOT(handleError(QString)), Qt::UniqueConnection);
+ connect(downloadItem, SIGNAL(error(QString)),
+ SLOT(handleError(QString)), Qt::UniqueConnection);
+ downloadItem->start();
}
void MediaView::sliderMoved(int value) {
- qDebug() << __func__;
- int sliderPercent = (value * 100) / (slider->maximum() - slider->minimum());
- qDebug() << slider->minimum() << value << slider->maximum();
- if (sliderPercent <= downloadItem->currentPercent()) {
- qDebug() << sliderPercent << downloadItem->currentPercent();
- mediaObject->seek(value);
+ if (currentVideoSize <= 0 || !downloadItem || !mediaObject->isSeekable())
+ return;
+
+ QSlider *slider = MainWindow::instance()->getSlider();
+ if (slider->isSliderDown()) return;
+
+ qint64 offset = (currentVideoSize * value) / slider->maximum();
+
+ bool needsDownload = downloadItem->needsDownload(offset);
+ if (needsDownload) {
+ if (downloadItem->isBuffered(offset)) {
+ qint64 realOffset = downloadItem->blankAtOffset(offset);
+ if (offset < currentVideoSize)
+ downloadItem->seekTo(realOffset, false);
+ mediaObject->seek(offsetToTime(offset));
+ } else {
+ mediaObject->pause();
+ downloadItem->seekTo(offset);
+ }
} else {
- seekTo(value);
+ // qDebug() << "simple seek";
+ mediaObject->seek(offsetToTime(offset));
}
}
-void MediaView::seekTo(int value) {
- qDebug() << __func__;
- mediaObject->pause();
- errorTimer->stop();
- // mediaObject->clear();
-
- QString tempDir = QDesktopServices::storageLocation(QDesktopServices::TempLocation);
- QString tempFile = tempDir + "/minitube" + QString::number(value) + ".mp4";
- if (!QFile::remove(tempFile)) {
- qDebug() << "Cannot remove temp file";
- }
- Video *videoCopy = downloadItem->getVideo()->clone();
- QUrl streamUrl = videoCopy->getStreamUrl();
- streamUrl.addQueryItem("begin", QString::number(value));
- if (downloadItem) delete downloadItem;
- downloadItem = new DownloadItem(videoCopy, streamUrl, tempFile, this);
- connect(downloadItem, SIGNAL(statusChanged()), SLOT(downloadStatusChanged()));
- // connect(downloadItem, SIGNAL(finished()), SLOT(itemFinished()));
- downloadItem->start();
-
- // slider->setMinimum(value);
-
+qint64 MediaView::offsetToTime(qint64 offset) {
+ const qint64 totalTime = mediaObject->totalTime();
+ return ((offset * totalTime) / currentVideoSize);
}
-*/
-
void MediaView::findVideoParts() {
// parts