// This file is part of JustVideo. // JustVideo is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // JustVideo is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. #include "playlist_backend.h" PlaylistBE::PlaylistBE(shared_t *share, const QString &path, Player *media, bool frontPlay, QObject *parent) : QObject(nullptr) { Q_UNUSED(parent); shared = share; player = media; vidDir = QDir::cleanPath(path) + QDir::separator(); buffer = true; startOnMoreMedia = false; vidClipBuffer = 3; index = 0; live = frontPlay; auto thr = new QThread(nullptr); connect(this, &PlaylistBE::playFile, media, &Player::setFile); connect(this, &PlaylistBE::endOfList, media, &Player::stop); connect(this, &PlaylistBE::noMedia, media, &Player::stop); connect(this, &PlaylistBE::buffering, media, &Player::stop); connect(this, &PlaylistBE::mPlay, media, &Player::play); connect(this, &PlaylistBE::mStop, media, &Player::stop); connect(this, &PlaylistBE::mPause, media, &Player::pause); connect(thr, &QThread::finished, this, &PlaylistBE::deleteLater); connect(thr, &QThread::started, this, &PlaylistBE::init); connect(media, &Player::mediaStatusChanged, this, &PlaylistBE::mediaStatusChanged); connect(media, &Player::stateChanged, this, &PlaylistBE::stateChanged); moveToThread(thr); thr->start(); } void PlaylistBE::kill() { thread()->terminate(); } void PlaylistBE::init() { timer = new QTimer(this); timer->setInterval(1000); timer->start(); connect(timer, &QTimer::timeout, this, &PlaylistBE::scan); } void PlaylistBE::buffOrEnd() { if (live) { buffer = true; emit buffering(); } else { emit endOfList(); } } QString PlaylistBE::seek(char direction) { auto ret = QString(); auto newInd = index; auto max = fileList.size() - 1; if (direction == '+') newInd = index + 1; else newInd = index - 1; if (fileList.isEmpty()) { emit noMedia(); if (!live) { startOnMoreMedia = true; } } else if (newInd < 0) { ret = vidDir + fileList[0]; } else if (newInd <= max) { index = newInd; ret = vidDir + fileList[index]; } else { buffOrEnd(); } return ret; } void PlaylistBE::list() { for (auto i = 0; i < fileList.size(); ++i) { emit newFile(vidDir + fileList[i]); } } void PlaylistBE::next() { emit playFile(seek('+')); } void PlaylistBE::prev() { emit playFile(seek('-')); } void PlaylistBE::start() { if (!fileList.isEmpty()) { index = 0; emit playFile(vidDir + fileList[index]); } else { startOnMoreMedia = true; emit noMedia(); } } void PlaylistBE::front() { if (fileList.size() >= vidClipBuffer) { index = fileList.size() - vidClipBuffer; buffer = false; emit playFile(vidDir + fileList[index]); } else { buffOrEnd(); } } void PlaylistBE::play() { if (player->state() == QAVPlayer::PausedState) { emit mPlay(); } else if (player->source().isEmpty()) { next(); } } void PlaylistBE::pause() { emit mPause(); } void PlaylistBE::stop() { emit mStop(); } void PlaylistBE::scan() { for (auto &&file : fileList) { if (!QFileInfo::exists(vidDir + file)) { emit fileDel(vidDir + file); fileList.removeOne(file); index--; } } auto newVids = lsVidFiles(vidDir); for (auto i = 0; i < newVids.size(); ++i) { if (!fileList.contains(newVids[i])) { emit newFile(vidDir + newVids[i]); fileList.append(newVids[i]); } } if (!fileList.isEmpty()) { if (startOnMoreMedia) { startOnMoreMedia = false; start(); } else if (buffer && live) { buffer = false; next(); } } } void PlaylistBE::mediaStatusChanged(QAVPlayer::MediaStatus status) { if (status == QAVPlayer::EndOfMedia || status == QAVPlayer::InvalidMedia) { next(); } } void PlaylistBE::stateChanged(QAVPlayer::State newState) { if (newState == QAVPlayer::PlayingState) { emit playbackResumed(); } }