]> git.sur5r.net Git - minitube/blobdiff - src/mediaview.cpp
New upstream version 3.9.1
[minitube] / src / mediaview.cpp
index 46eb8ead94571fd0d95036c2b01245daa086ff51..5d74d0ba379ec3f2fa290fb7b52521c623a47890 100644 (file)
@@ -45,7 +45,6 @@ $END_LICENSE */
 #include "videosource.h"
 #include "ytchannel.h"
 #include "ytsearch.h"
-#include "ytsinglevideosource.h"
 #ifdef APP_SNAPSHOT
 #include "snapshotsettings.h"
 #endif
@@ -53,6 +52,10 @@ $END_LICENSE */
 #include "idle.h"
 #include "videodefinition.h"
 
+#include "searchvideosource.h"
+#include "singlevideosource.h"
+#include "videoapi.h"
+
 MediaView *MediaView::instance() {
     static MediaView *i = new MediaView();
     return i;
@@ -181,25 +184,14 @@ void MediaView::initialize() {
     QAction *leftAction = new QAction(tr("Rewind %1 seconds").arg(10));
     leftAction->setShortcut(Qt::Key_Left);
     leftAction->setAutoRepeat(false);
-    connect(leftAction, &QAction::triggered, this, [this] {
-        qint64 position = media->position();
-        position -= 10000;
-        if (position < 0) position = 0;
-        media->seek(position);
-    });
+    connect(leftAction, &QAction::triggered, this, [this] { media->relativeSeek(-10000); });
     addAction(leftAction);
     playingVideoActions << leftAction;
 
     QAction *rightAction = new QAction(tr("Fast forward %1 seconds").arg(10));
     rightAction->setShortcut(Qt::Key_Right);
     rightAction->setAutoRepeat(false);
-    connect(rightAction, &QAction::triggered, this, [this] {
-        qint64 position = media->position();
-        position += 10000;
-        qint64 duration = media->duration();
-        if (position > duration) position = duration;
-        media->seek(position);
-    });
+    connect(rightAction, &QAction::triggered, this, [this] { media->relativeSeek(10000); });
     addAction(rightAction);
     playingVideoActions << rightAction;
 }
@@ -218,9 +210,11 @@ void MediaView::setMedia(Media *media) {
 
 SearchParams *MediaView::getSearchParams() {
     VideoSource *videoSource = playlistModel->getVideoSource();
-    if (videoSource && videoSource->metaObject()->className() == QLatin1String("YTSearch")) {
-        YTSearch *search = qobject_cast<YTSearch *>(videoSource);
-        return search->getSearchParams();
+    if (!videoSource) return nullptr;
+    auto clazz = videoSource->metaObject()->className();
+    if (clazz == QLatin1String("SearchVideoSource")) {
+        auto search = qobject_cast<SearchVideoSource *>(videoSource);
+        if (search) return search->getSearchParams();
     }
     return nullptr;
 }
@@ -231,19 +225,19 @@ void MediaView::search(SearchParams *searchParams) {
             searchParams->keywords().startsWith("https://")) {
             QString videoId = YTSearch::videoIdFromUrl(searchParams->keywords());
             if (!videoId.isEmpty()) {
-                YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource(this);
-                singleVideoSource->setVideoId(videoId);
-                setVideoSource(singleVideoSource);
+                auto source = new SingleVideoSource(this);
+                source->setVideoId(videoId);
+                setVideoSource(source);
+
                 QTime tstamp = YTSearch::videoTimestampFromUrl(searchParams->keywords());
                 pauseTime = QTime(0, 0).msecsTo(tstamp);
                 return;
             }
         }
     }
-    YTSearch *ytSearch = new YTSearch(searchParams);
-    ytSearch->setAsyncDetails(true);
-    connect(ytSearch, SIGNAL(gotDetails()), playlistModel, SLOT(emitDataChanged()));
-    setVideoSource(ytSearch);
+
+    VideoSource *search = new SearchVideoSource(searchParams);
+    setVideoSource(search);
 }
 
 void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool back) {
@@ -253,13 +247,6 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
 
     // qDebug() << "Adding VideoSource" << videoSource->getName() << videoSource;
 
-    YTSearch * ytSearch = qobject_cast<YTSearch *>(videoSource);
-    if (nullptr != ytSearch) {
-        if (!ytSearch->getSearchParams()->channelId().isEmpty()) {
-            updateSubscriptionActionForChannel(ytSearch->getSearchParams()->channelId());
-        }
-    }
-
     if (addToHistory) {
         int currentIndex = getHistoryIndex();
         if (currentIndex >= 0 && currentIndex < history.size() - 1) {
@@ -267,6 +254,7 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
                 VideoSource *vs = history.takeLast();
                 if (!vs->parent()) {
                     qDebug() << "Deleting VideoSource" << vs->getName() << vs;
+                    vs->abort();
                     vs->deleteLater();
                 }
             }
@@ -288,13 +276,17 @@ void MediaView::setVideoSource(VideoSource *videoSource, bool addToHistory, bool
         }
     }
 
+    SearchParams *searchParams = getSearchParams();
+
     sidebar->showPlaylist();
-    sidebar->getRefineSearchWidget()->setSearchParams(getSearchParams());
+    sidebar->getRefineSearchWidget()->setSearchParams(searchParams);
     sidebar->hideSuggestions();
     sidebar->getHeader()->updateInfo();
 
-    SearchParams *searchParams = getSearchParams();
     bool isChannel = searchParams && !searchParams->channelId().isEmpty();
+    if (isChannel) {
+        updateSubscriptionActionForChannel(searchParams->channelId());
+    }
     playlistView->setClickableAuthors(!isChannel);
 }
 
@@ -360,6 +352,7 @@ void MediaView::mediaStateChanged(Media::State state) {
     if (pauseTime > 0 && (state == Media::PlayingState || state == Media::BufferingState)) {
         qDebug() << "Seeking to" << pauseTime;
         media->seek(pauseTime);
+        media->play();
         pauseTime = 0;
     }
     if (state == Media::PlayingState) {
@@ -368,7 +361,7 @@ void MediaView::mediaStateChanged(Media::State state) {
         handleError(media->errorString());
     }
 
-    bool enablePlayingVideoActions = state == Media::PlayingState || state == Media::PausedState;
+    bool enablePlayingVideoActions = state != Media::StoppedState;
     for (QAction *action : qAsConst(playingVideoActions))
         action->setEnabled(enablePlayingVideoActions);
 
@@ -384,17 +377,25 @@ void MediaView::mediaStateChanged(Media::State state) {
 void MediaView::pause() {
     switch (media->state()) {
     case Media::PlayingState:
+        qDebug() << "Pausing";
         media->pause();
         pauseTimer.start();
         break;
     default:
-        if (pauseTimer.hasExpired(60000)) {
+        if (pauseTimer.isValid() && pauseTimer.hasExpired(60000)) {
+            qDebug() << "Pause timer expired";
             pauseTimer.invalidate();
-            connect(playlistModel->activeVideo(), &Video::gotStreamUrl, this,
-                    &MediaView::resumeWithNewStreamUrl);
-            playlistModel->activeVideo()->loadStreamUrl();
-        } else
+            auto activeVideo = playlistModel->activeVideo();
+            if (activeVideo) {
+                connect(activeVideo, &Video::gotStreamUrl, this,
+                        &MediaView::resumeWithNewStreamUrl);
+                activeVideo->loadStreamUrl();
+            } else
+                qDebug() << "No active video";
+        } else {
+            qDebug() << "Playing" << media->file();
             media->play();
+        }
         break;
     }
 }
@@ -410,6 +411,7 @@ void MediaView::stop() {
         VideoSource *videoSource = history.takeFirst();
         // Don't delete videoSource in the Browse view
         if (!videoSource->parent()) {
+            videoSource->abort();
             videoSource->deleteLater();
         }
     }
@@ -426,7 +428,7 @@ void MediaView::stop() {
     demoTimer->stop();
 #endif
 
-    for (QAction *action : currentVideoActions)
+    for (QAction *action : qAsConst(currentVideoActions))
         action->setEnabled(false);
 
     QAction *a = MainWindow::instance()->getAction("download");
@@ -609,14 +611,7 @@ void MediaView::skipBackward() {
 }
 
 void MediaView::onAboutToFinish() {
-    qint64 currentTime = media->position();
-    qint64 totalTime = media->duration();
-    // qDebug() << __PRETTY_FUNCTION__ << currentTime << totalTime;
-    if (totalTime < 1 || currentTime + 10000 < totalTime) {
-        // QTimer::singleShot(500, this, SLOT(playbackResume()));
-        media->seek(currentTime);
-        media->play();
-    }
+
 }
 
 void MediaView::onPlaybackFinished() {
@@ -706,7 +701,7 @@ void MediaView::moveUpSelected() {
     if (!playlistView->selectionModel()->hasSelection()) return;
 
     QModelIndexList indexes = playlistView->selectionModel()->selectedIndexes();
-    qStableSort(indexes.begin(), indexes.end());
+    std::stable_sort(indexes.begin(), indexes.end());
     playlistModel->move(indexes, true);
 
     // set current index after row moves to something more intuitive
@@ -719,7 +714,8 @@ void MediaView::moveDownSelected() {
     if (!playlistView->selectionModel()->hasSelection()) return;
 
     QModelIndexList indexes = playlistView->selectionModel()->selectedIndexes();
-    qStableSort(indexes.begin(), indexes.end(), qGreater<QModelIndex>());
+    std::stable_sort(indexes.begin(), indexes.end(),
+                     [](const QModelIndex &a, const QModelIndex &b) { return b < a; });
     playlistModel->move(indexes, false);
 
     // set current index after row moves to something more intuitive
@@ -901,10 +897,11 @@ void MediaView::findVideoParts() {
 void MediaView::relatedVideos() {
     Video *video = playlistModel->activeVideo();
     if (!video) return;
-    YTSingleVideoSource *singleVideoSource = new YTSingleVideoSource();
-    singleVideoSource->setVideo(video->clone());
-    singleVideoSource->setAsyncDetails(true);
-    setVideoSource(singleVideoSource);
+
+    auto source = new SingleVideoSource(this);
+    source->setVideo(video->clone());
+    setVideoSource(source);
+
     MainWindow::instance()->getAction("relatedVideos")->setEnabled(false);
 }