2020-09-27 18:38:33 +00:00
|
|
|
/*
|
2021-03-24 19:04:03 +00:00
|
|
|
* Sailfin: a Jellyfin client written using Qt
|
|
|
|
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
#ifndef JELLYFIN_VIEWMODEL_PLAYBACKMANAGER_H
|
|
|
|
#define JELLYFIN_VIEWMODEL_PLAYBACKMANAGER_H
|
2020-09-25 12:46:39 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
#include <QAbstractItemModel>
|
2020-09-25 12:46:39 +00:00
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
2021-02-20 22:20:39 +00:00
|
|
|
#include <QFuture>
|
2020-09-25 12:46:39 +00:00
|
|
|
#include <QObject>
|
2021-02-13 23:21:49 +00:00
|
|
|
#include <QtGlobal>
|
2020-09-25 12:46:39 +00:00
|
|
|
#include <QUrlQuery>
|
2021-02-20 22:20:39 +00:00
|
|
|
#include <QVariant>
|
2020-09-25 12:46:39 +00:00
|
|
|
|
|
|
|
#include <QtMultimedia/QMediaPlayer>
|
2021-02-20 22:20:39 +00:00
|
|
|
#include <QtMultimedia/QMediaPlaylist>
|
|
|
|
|
|
|
|
#include <functional>
|
2020-09-25 12:46:39 +00:00
|
|
|
|
2021-03-24 19:04:03 +00:00
|
|
|
#include "../dto/baseitemdto.h"
|
2021-07-31 13:06:17 +00:00
|
|
|
#include "../dto/playbackinfodto.h"
|
2021-09-01 12:50:02 +00:00
|
|
|
#include "../dto/playbackinforesponse.h"
|
2021-07-31 13:06:17 +00:00
|
|
|
#include "../dto/playmethod.h"
|
|
|
|
#include "../loader/requesttypes.h"
|
|
|
|
#include "../model/playlist.h"
|
2021-03-24 19:04:03 +00:00
|
|
|
#include "../support/jsonconv.h"
|
|
|
|
#include "../viewmodel/item.h"
|
2021-08-21 20:01:13 +00:00
|
|
|
#include "../viewmodel/playlist.h"
|
2021-03-24 19:04:03 +00:00
|
|
|
#include "../apiclient.h"
|
2021-03-26 20:27:35 +00:00
|
|
|
#include "itemmodel.h"
|
2020-09-25 12:46:39 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
namespace Jellyfin {
|
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
// Forward declaration of Jellyfin::ApiClient found in jellyfinapiclient.h
|
|
|
|
class ApiClient;
|
2021-02-20 22:20:39 +00:00
|
|
|
class ItemModel;
|
2021-02-21 04:02:05 +00:00
|
|
|
class RemoteItem;
|
|
|
|
|
2021-03-24 19:04:03 +00:00
|
|
|
namespace ViewModel {
|
2021-02-13 23:21:49 +00:00
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
// Later defined in this file
|
|
|
|
class ItemUrlFetcherThread;
|
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
/**
|
|
|
|
* @brief The PlaybackManager class manages the playback of Jellyfin items. It fetches streams based on Jellyfin items, posts
|
2021-02-20 22:20:39 +00:00
|
|
|
* the current playback state to the Jellyfin Server, contains the actual media player and so on.
|
|
|
|
*
|
|
|
|
* The PlaybackManager actually keeps two mediaPlayers, m_mediaPlayer1 and m_mediaPlayer2. When one is playing, the other is
|
|
|
|
* preloading the next item in the queue. The current media player is pointed to by m_mediaPlayer.
|
2021-02-13 23:21:49 +00:00
|
|
|
*/
|
|
|
|
class PlaybackManager : public QObject, public QQmlParserStatus {
|
2021-07-31 13:06:17 +00:00
|
|
|
friend class ItemUrlFetcherThread;
|
2020-09-25 12:46:39 +00:00
|
|
|
Q_OBJECT
|
2021-02-13 23:21:49 +00:00
|
|
|
Q_INTERFACES(QQmlParserStatus)
|
2020-09-25 12:46:39 +00:00
|
|
|
public:
|
2021-08-22 00:33:04 +00:00
|
|
|
using ItemUrlLoader = Support::Loader<DTO::PlaybackInfoResponse, Jellyfin::Loader::GetPostedPlaybackInfoParams>;
|
2020-10-01 19:45:34 +00:00
|
|
|
|
2020-10-08 01:00:08 +00:00
|
|
|
explicit PlaybackManager(QObject *parent = nullptr);
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-02-21 04:02:05 +00:00
|
|
|
Q_PROPERTY(ApiClient *apiClient MEMBER m_apiClient WRITE setApiClient)
|
2020-09-25 12:46:39 +00:00
|
|
|
Q_PROPERTY(QString streamUrl READ streamUrl NOTIFY streamUrlChanged)
|
|
|
|
Q_PROPERTY(bool autoOpen MEMBER m_autoOpen NOTIFY autoOpenChanged)
|
2020-09-25 15:14:44 +00:00
|
|
|
Q_PROPERTY(int audioIndex MEMBER m_audioIndex NOTIFY audioIndexChanged)
|
|
|
|
Q_PROPERTY(int subtitleIndex MEMBER m_subtitleIndex NOTIFY subtitleIndexChanged)
|
2021-02-13 23:21:49 +00:00
|
|
|
Q_PROPERTY(bool resumePlayback MEMBER m_resumePlayback NOTIFY resumePlaybackChanged)
|
2021-07-31 13:06:17 +00:00
|
|
|
Q_PROPERTY(Jellyfin::DTO::PlayMethodClass::Value playMethod READ playMethod NOTIFY playMethodChanged)
|
2020-09-25 12:46:39 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
// Current Item and queue informatoion
|
2021-07-31 13:06:17 +00:00
|
|
|
Q_PROPERTY(QObject *item READ item NOTIFY itemChanged)
|
2021-02-20 22:20:39 +00:00
|
|
|
Q_PROPERTY(int queueIndex READ queueIndex NOTIFY queueIndexChanged)
|
2021-08-21 20:01:13 +00:00
|
|
|
Q_PROPERTY(Jellyfin::ViewModel::Playlist *queue READ queue NOTIFY queueChanged)
|
2021-02-20 22:20:39 +00:00
|
|
|
|
|
|
|
// Current media player related property getters
|
|
|
|
Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged)
|
|
|
|
Q_PROPERTY(QMediaPlayer::Error error READ error NOTIFY errorChanged)
|
|
|
|
Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged)
|
|
|
|
Q_PROPERTY(bool hasVideo READ hasVideo NOTIFY hasVideoChanged)
|
2021-08-11 21:35:33 +00:00
|
|
|
Q_PROPERTY(bool seekable READ seekable NOTIFY seekableChanged)
|
2021-02-20 22:20:39 +00:00
|
|
|
Q_PROPERTY(QObject* mediaObject READ mediaObject NOTIFY mediaObjectChanged)
|
|
|
|
Q_PROPERTY(QMediaPlayer::MediaStatus mediaStatus READ mediaStatus NOTIFY mediaStatusChanged)
|
|
|
|
Q_PROPERTY(QMediaPlayer::State playbackState READ playbackState NOTIFY playbackStateChanged)
|
|
|
|
Q_PROPERTY(qint64 position READ position NOTIFY positionChanged)
|
2021-08-30 23:29:51 +00:00
|
|
|
Q_PROPERTY(bool hasNext READ hasNext NOTIFY hasNextChanged)
|
|
|
|
Q_PROPERTY(bool hasPrevious READ hasPrevious NOTIFY hasPreviousChanged)
|
2020-10-01 19:45:34 +00:00
|
|
|
|
2021-05-21 13:46:30 +00:00
|
|
|
ViewModel::Item *item() const { return m_displayItem; }
|
2021-08-30 23:29:51 +00:00
|
|
|
QSharedPointer<Model::Item> dataItem() const { return m_item; }
|
|
|
|
ApiClient *apiClient() const { return m_apiClient; }
|
2021-02-21 04:02:05 +00:00
|
|
|
void setApiClient(ApiClient *apiClient);
|
2020-10-01 19:45:34 +00:00
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
QString streamUrl() const { return m_streamUrl; }
|
2021-02-14 12:29:30 +00:00
|
|
|
PlayMethod playMethod() const { return m_playMethod; }
|
2021-02-20 22:20:39 +00:00
|
|
|
QObject *mediaObject() const { return m_mediaPlayer; }
|
|
|
|
qint64 position() const { return m_mediaPlayer->position(); }
|
|
|
|
qint64 duration() const { return m_mediaPlayer->duration(); }
|
2021-08-21 20:01:13 +00:00
|
|
|
ViewModel::Playlist *queue() const { return m_displayQueue; }
|
2021-02-20 22:20:39 +00:00
|
|
|
int queueIndex() const { return m_queueIndex; }
|
2021-08-30 23:29:51 +00:00
|
|
|
bool hasNext() const { return m_queue->hasNext(); }
|
|
|
|
bool hasPrevious() const { return m_queue->hasPrevious(); }
|
2021-02-20 22:20:39 +00:00
|
|
|
|
|
|
|
// Current media player related property getters
|
2021-08-11 21:35:33 +00:00
|
|
|
QMediaPlayer::State playbackState() const { return m_mediaPlayer->state()/*m_playbackState*/; }
|
2021-02-20 22:20:39 +00:00
|
|
|
QMediaPlayer::MediaStatus mediaStatus() const { return m_mediaPlayer->mediaStatus(); }
|
|
|
|
bool hasVideo() const { return m_mediaPlayer->isVideoAvailable(); }
|
2021-08-11 21:35:33 +00:00
|
|
|
bool seekable() const { return m_mediaPlayer->isSeekable(); }
|
2021-02-20 22:20:39 +00:00
|
|
|
QMediaPlayer::Error error () const { return m_mediaPlayer->error(); }
|
|
|
|
QString errorString() const { return m_mediaPlayer->errorString(); }
|
2020-09-25 12:46:39 +00:00
|
|
|
signals:
|
2021-07-31 13:06:17 +00:00
|
|
|
void itemChanged(ViewModel::Item *newItemId);
|
2020-09-25 12:46:39 +00:00
|
|
|
void streamUrlChanged(const QString &newStreamUrl);
|
|
|
|
void autoOpenChanged(bool autoOpen);
|
2020-09-25 15:14:44 +00:00
|
|
|
void audioIndexChanged(int audioIndex);
|
|
|
|
void subtitleIndexChanged(int subtitleIndex);
|
2021-02-13 23:21:49 +00:00
|
|
|
void mediaPlayerChanged(QObject *newMediaPlayer);
|
|
|
|
void resumePlaybackChanged(bool newResumePlayback);
|
2021-02-14 12:29:30 +00:00
|
|
|
void playMethodChanged(PlayMethod newPlayMethod);
|
2020-09-25 12:46:39 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
// Current media player related property signals
|
|
|
|
void mediaObjectChanged(QObject *newMediaObject);
|
|
|
|
void positionChanged(qint64 newPosition);
|
|
|
|
void durationChanged(qint64 newDuration);
|
2021-08-21 20:01:13 +00:00
|
|
|
void queueChanged(QAbstractItemModel *newQueue);
|
2021-02-20 22:20:39 +00:00
|
|
|
void queueIndexChanged(int newIndex);
|
|
|
|
void playbackStateChanged(QMediaPlayer::State newState);
|
|
|
|
void mediaStatusChanged(QMediaPlayer::MediaStatus newMediaStatus);
|
|
|
|
void hasVideoChanged(bool newHasVideo);
|
2021-08-11 21:35:33 +00:00
|
|
|
void seekableChanged(bool newSeekable);
|
2021-02-20 22:20:39 +00:00
|
|
|
void errorChanged(QMediaPlayer::Error newError);
|
|
|
|
void errorStringChanged(const QString &newErrorString);
|
2021-08-30 23:29:51 +00:00
|
|
|
void hasNextChanged(bool newHasNext);
|
|
|
|
void hasPreviousChanged(bool newHasPrevious);
|
2020-09-25 12:46:39 +00:00
|
|
|
public slots:
|
2021-02-14 17:40:46 +00:00
|
|
|
/**
|
2021-05-21 13:46:30 +00:00
|
|
|
* @brief playItem Replaces the current queue and plays the item with the given id.
|
|
|
|
*
|
|
|
|
* This will construct the Jellyfin::Item internally
|
2021-02-14 17:40:46 +00:00
|
|
|
* and delete it later.
|
2021-07-31 13:06:17 +00:00
|
|
|
* @param item The item to play.
|
2021-02-14 17:40:46 +00:00
|
|
|
*/
|
2021-07-31 13:06:17 +00:00
|
|
|
void playItem(Item *item);
|
2021-02-20 22:20:39 +00:00
|
|
|
void playItemInList(ItemModel *itemList, int index);
|
2021-08-21 20:33:23 +00:00
|
|
|
/**
|
|
|
|
* @brief skipToItemIndex Skips to an item in the current playlist
|
|
|
|
* @param index The index to skip to
|
|
|
|
*/
|
|
|
|
void skipToItemIndex(int index);
|
2021-02-20 22:20:39 +00:00
|
|
|
void play() { m_mediaPlayer->play(); }
|
|
|
|
void pause() { m_mediaPlayer->pause(); }
|
|
|
|
void seek(qint64 pos) { m_mediaPlayer->setPosition(pos); }
|
|
|
|
void stop() { m_mediaPlayer->stop(); }
|
2021-02-17 18:42:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief previous Play the previous track in the current playlist.
|
|
|
|
*/
|
|
|
|
void previous();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief next Play the next track in the current playlist.
|
|
|
|
*/
|
|
|
|
void next();
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
private slots:
|
|
|
|
void mediaPlayerStateChanged(QMediaPlayer::State newState);
|
|
|
|
void mediaPlayerPositionChanged(qint64 position);
|
|
|
|
void mediaPlayerMediaStatusChanged(QMediaPlayer::MediaStatus newStatus);
|
2021-02-20 22:20:39 +00:00
|
|
|
void mediaPlayerError(QMediaPlayer::Error error);
|
2021-07-31 13:06:17 +00:00
|
|
|
void mediaPlayerDurationChanged(qint64 newDuration);
|
2021-02-20 22:20:39 +00:00
|
|
|
/**
|
|
|
|
* @brief updatePlaybackInfo Updates the Jellyfin server with the current playback progress etc.
|
|
|
|
*/
|
|
|
|
void updatePlaybackInfo();
|
2020-09-25 12:46:39 +00:00
|
|
|
|
2021-08-22 00:33:04 +00:00
|
|
|
/// Called when we have fetched the playback URL and playSession
|
|
|
|
void onItemUrlReceived(const QString &itemId, const QUrl &url, const QString &playSession,
|
2021-07-31 13:06:17 +00:00
|
|
|
// Fully specify class to please MOC
|
|
|
|
Jellyfin::DTO::PlayMethodClass::Value playMethod);
|
2021-08-22 00:33:04 +00:00
|
|
|
/// Called when we have encountered an error
|
2021-07-31 13:06:17 +00:00
|
|
|
void onItemErrorReceived(const QString &itemId, const QString &errorString);
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
private:
|
2021-05-21 13:46:30 +00:00
|
|
|
/// Factor to multiply with when converting from milliseconds to ticks.
|
|
|
|
const static int MS_TICK_FACTOR = 10000;
|
|
|
|
enum PlaybackInfoType { Started, Stopped, Progress };
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
/// Timer used to update the play progress on the Jellyfin server
|
2020-10-01 19:45:34 +00:00
|
|
|
QTimer m_updateTimer;
|
2021-07-31 13:06:17 +00:00
|
|
|
/// Timer used to notify ourselves when we need to preload the next item
|
|
|
|
QTimer m_preloadTimer;
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
ApiClient *m_apiClient = nullptr;
|
2021-07-31 13:06:17 +00:00
|
|
|
/// The currently playing item
|
2021-03-24 19:04:03 +00:00
|
|
|
QSharedPointer<Model::Item> m_item;
|
2021-07-31 13:06:17 +00:00
|
|
|
/// The item that will be played next
|
|
|
|
QSharedPointer<Model::Item> m_nextItem;
|
|
|
|
/// The currently played item that will be shown in the GUI
|
2021-05-21 13:46:30 +00:00
|
|
|
ViewModel::Item *m_displayItem = new ViewModel::Item(this);
|
2021-08-21 20:01:13 +00:00
|
|
|
/// The currently played queue that will be shown in the GUI
|
|
|
|
ViewModel::Playlist *m_displayQueue = nullptr;
|
2021-03-29 21:48:16 +00:00
|
|
|
|
2021-05-21 13:46:30 +00:00
|
|
|
// Properties for making the streaming request.
|
2020-09-25 12:46:39 +00:00
|
|
|
QString m_streamUrl;
|
2021-07-31 13:06:17 +00:00
|
|
|
QString m_nextStreamUrl;
|
2020-09-25 12:46:39 +00:00
|
|
|
QString m_playSessionId;
|
2021-07-31 13:06:17 +00:00
|
|
|
QString m_nextPlaySessionId;
|
2021-05-21 13:46:30 +00:00
|
|
|
/// The index of the mediastreams of the to-be-played item containing the audio
|
2020-09-25 15:14:44 +00:00
|
|
|
int m_audioIndex = 0;
|
2021-05-21 13:46:30 +00:00
|
|
|
/// The index of the mediastreams of the to-be-played item containing subtitles
|
2020-09-25 15:14:44 +00:00
|
|
|
int m_subtitleIndex = -1;
|
2021-05-21 13:46:30 +00:00
|
|
|
/// The position in ticks to resume playback from
|
2021-02-13 23:21:49 +00:00
|
|
|
qint64 m_resumePosition = 0;
|
2021-05-21 13:46:30 +00:00
|
|
|
/// The position in ticks the playback was stopped
|
2020-10-01 19:45:34 +00:00
|
|
|
qint64 m_stopPosition = 0;
|
2021-05-21 13:46:30 +00:00
|
|
|
|
|
|
|
/// Keeps track of latest playback position
|
|
|
|
qint64 m_oldPosition = 0;
|
|
|
|
/**
|
|
|
|
* @brief Whether to automatically open the livestream of the item;
|
|
|
|
*/
|
|
|
|
bool m_autoOpen = false;
|
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
QMediaPlayer::State m_oldState = QMediaPlayer::StoppedState;
|
2021-07-31 13:06:17 +00:00
|
|
|
PlayMethod m_playMethod = PlayMethod::Transcode;
|
2021-02-20 22:20:39 +00:00
|
|
|
QMediaPlayer::State m_playbackState = QMediaPlayer::StoppedState;
|
2021-05-21 13:46:30 +00:00
|
|
|
/// Pointer to the current media player.
|
2021-02-20 22:20:39 +00:00
|
|
|
QMediaPlayer *m_mediaPlayer = nullptr;
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
Model::Playlist *m_queue = nullptr;
|
2021-02-20 22:20:39 +00:00
|
|
|
int m_queueIndex = 0;
|
2021-02-13 23:21:49 +00:00
|
|
|
bool m_resumePlayback = true;
|
|
|
|
|
2021-05-21 13:46:30 +00:00
|
|
|
// Helper methods
|
2021-07-31 13:06:17 +00:00
|
|
|
void setItem(QSharedPointer<Model::Item> newItem);
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
void setStreamUrl(const QUrl &streamUrl);
|
2021-02-20 22:20:39 +00:00
|
|
|
void setPlaybackState(QMediaPlayer::State newState);
|
|
|
|
|
2020-10-01 19:45:34 +00:00
|
|
|
/**
|
|
|
|
* @brief Posts the playback information
|
|
|
|
*/
|
|
|
|
void postPlaybackInfo(PlaybackInfoType type);
|
2021-02-13 23:21:49 +00:00
|
|
|
|
2021-08-22 00:33:04 +00:00
|
|
|
void requestItemUrl(QSharedPointer<Model::Item> item);
|
|
|
|
void handlePlaybackInfoResponse(QString itemId, QString mediaType, DTO::PlaybackInfoResponse &response);
|
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-05-21 13:46:30 +00:00
|
|
|
// QQmlParserListener interface
|
|
|
|
void classBegin() override { m_qmlIsParsingComponent = true; }
|
2021-02-13 23:21:49 +00:00
|
|
|
void componentComplete() override;
|
2021-05-21 13:46:30 +00:00
|
|
|
bool m_qmlIsParsingComponent = false;
|
2021-07-31 13:06:17 +00:00
|
|
|
|
|
|
|
/// Time in ms at what moment this playbackmanager should start loading the next item.
|
|
|
|
const qint64 PRELOAD_DURATION = 15 * 1000;
|
|
|
|
};
|
|
|
|
|
2021-03-24 19:04:03 +00:00
|
|
|
} // NS ViewModel
|
|
|
|
} // NS Jellyfin
|
2020-09-25 12:46:39 +00:00
|
|
|
|
2021-03-24 19:04:03 +00:00
|
|
|
#endif // JELLYFIN_VIEWMODEL_PLAYBACKMANAGER_H
|