1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2025-09-04 01:42:44 +00:00

core: Split PlaybackManager up into smaller parts

The PlaybackManager was a giant class that handled UI bindings, fetching
stream URLS, playback logic.

It now has been split up into:

- ViewModel::PlaybackManager, which handles UI interfacing and allowing
  to swap out the Model::Playback implementation on the fly.
- Model::PlaybackManager, which is an interface for what a
  PlaybackManager must do, handling queues/playlists, and controlling a
  player.
- Model::LocalPlaybackManager, which is an Model::PlaybackManager
  implementation for playing back Jellyfin media within the application.
- Model::PlaybackReporter, which reports the current playback state to
  the Jellyfin server, for keeping track of played items.
- Model::Player, which handles playing back media from an URL and
  the usual play/pause et cetera.

In a future commit, this would allow for introducing a
Model::RemoteJellyfinPlaybackManager, to control other Jellyfin
instances.
This commit is contained in:
Chris Josten 2022-01-05 21:24:52 +01:00 committed by Henk Kalkwater
parent f91e9f88e7
commit c72c10bad4
No known key found for this signature in database
GPG key ID: A69C050E9FD9FF6A
20 changed files with 1916 additions and 684 deletions

View file

@ -489,6 +489,10 @@ public:
this->endResetModel();
}
const QList<QSharedPointer<T>> &toList() {
return m_array;
}
// From AbstractListModel, gets implemented in ApiModel<T>
//virtual QHash<int, QByteArray> roleNames() const override = 0;
/*virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override = 0;*/

View file

@ -0,0 +1,218 @@
/*
* Sailfin: a Jellyfin client written using Qt
* Copyright (C) 2021-2022 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_MODEL_PLAYBACKMANAGER_H
#define JELLYFIN_MODEL_PLAYBACKMANAGER_H
#include <QLoggingCategory>
#include <QObject>
#include <QSharedPointer>
#include <JellyfinQt/dto/playmethod.h>
#include <JellyfinQt/model/player.h>
namespace Jellyfin {
namespace Model {
class Item;
class Playlist;
Q_DECLARE_LOGGING_CATEGORY(playbackManager);
class PlaybackManagerErrorClass {
Q_GADGET
public:
enum Value {
NoError,
PlaybackInfoError,
RemoteClientNotReachable,
PlayerGeneralError
};
Q_ENUM(Value);
};
using PlaybackManagerError = PlaybackManagerErrorClass::Value;
class PlaybackManagerPrivate;
/**
* @brief Base class for a playback manager.
*
* Besides some glue code for the properties,
* most of the actual playback logic is implemented in the two subclasses: {@link LocalPlaybackManager}
* and {@link RemotePlaybackManager}
*/
class PlaybackManager : public QObject {
Q_OBJECT
Q_DECLARE_PRIVATE(PlaybackManager);
Q_PROPERTY(bool resumePlayback READ resumePlayback WRITE setResumePlayback NOTIFY resumePlaybackChanged)
Q_PROPERTY(int audioIndex READ audioIndex WRITE setAudioIndex NOTIFY audioIndexChanged)
Q_PROPERTY(int subtitleIndex READ subtitleIndex WRITE setSubtitleIndex NOTIFY subtitleIndexChanged)
Q_PROPERTY(qint64 position READ position NOTIFY positionChanged)
Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged)
Q_PROPERTY(bool seekable READ seekable NOTIFY seekableChanged)
Q_PROPERTY(bool hasAudio READ hasAudio NOTIFY hasAudioChanged)
Q_PROPERTY(bool hasVideo READ hasVideo NOTIFY hasVideoChanged)
Q_PROPERTY(Jellyfin::Model::PlayerStateClass::Value playbackState READ playbackState NOTIFY playbackStateChanged)
Q_PROPERTY(Jellyfin::Model::MediaStatusClass::Value mediaStatus READ mediaStatus NOTIFY mediaStatusChanged)
Q_PROPERTY(Jellyfin::Model::Playlist *queue READ queue NOTIFY queueChanged)
Q_PROPERTY(int queueIndex READ queueIndex NOTIFY queueIndexChanged)
public:
explicit PlaybackManager(QObject *parent = nullptr);
virtual ~PlaybackManager();
virtual void swap(PlaybackManager& other) = 0;
ApiClient * apiClient() const;
void setApiClient(ApiClient *apiClient);
// Getters
QSharedPointer<Item> currentItem() const;
Playlist *queue() const;
int queueIndex() const;
bool resumePlayback() const;
void setResumePlayback(bool newResumePlayback);
int audioIndex() const;
void setAudioIndex(int newAudioIndex);
int subtitleIndex() const;
void setSubtitleIndex(int newSubtitleIndex);
virtual PlayerState playbackState() const = 0;
virtual MediaStatus mediaStatus() const = 0;
virtual bool hasNext() const = 0;
virtual bool hasPrevious() const = 0;
virtual PlaybackManagerError error() const = 0;
virtual const QString &errorString() const = 0;
virtual qint64 position() const = 0;
virtual qint64 duration() const = 0;
virtual bool seekable() const = 0;
virtual bool hasAudio() const = 0;
virtual bool hasVideo() const = 0;
virtual void playItem(QSharedPointer<Model::Item> item) = 0;
virtual void playItemInList(const QList<QSharedPointer<Model::Item>> &items, int index) = 0;
signals:
void playbackStateChanged(Jellyfin::Model::PlayerStateClass::Value newPlaybackState);
void mediaStatusChanged(Jellyfin::Model::MediaStatusClass::Value newMediaStatus);
void queueChanged(Jellyfin::Model::Playlist *newPlaylist);
void hasNextChanged(bool newHasNext);
void hasPreviousChanged(bool newHasPrevious);
void itemChanged();
void queueIndexChanged(int index);
void errorChanged(Jellyfin::Model::PlaybackManagerErrorClass::Value newError);
void errorStringChanged(const QString &newErrorString);
void positionChanged(qint64 newPosition);
void durationChanged(qint64 newDuration);
void seekableChanged(bool newSeekable);
void hasAudioChanged();
void hasVideoChanged();
void resumePlaybackChanged(bool newPlaybackChanged);
void audioIndexChanged(int newAudioIndex);
void subtitleIndexChanged(int newSubtitleIndex);
public slots:
virtual void pause() = 0;
virtual void play() = 0;
virtual void playItemId(const QString &id) = 0;
virtual void previous() = 0;
virtual void next() = 0;
/**
* @brief Play the item at the index in the current playlist
* @param index the item to go to;
*/
virtual void goTo(int index) = 0;
virtual void stop() = 0;
virtual void seek(qint64 pos) = 0;
protected:
explicit PlaybackManager(PlaybackManagerPrivate *d, QObject *parent = nullptr);
QScopedPointer<PlaybackManagerPrivate> d_ptr;
};
class LocalPlaybackManagerPrivate;
/**
* @brief Controls playback whithin this app.
*
* This class mostly consists of bookkeeping between the actual media player implementation (which is
* abstracted into yet another class) and the ViewModel.
*
* It does things like:
* * Fetching the actual media URL of an item and deciding which playback method to use
* * Managing the current playlist state and instructing the media player which item to play next
* * Keeping track of the playback state that the user would expect from a media player,
* instead of what the media player implementation reports.
*
*/
class LocalPlaybackManager : public PlaybackManager {
Q_DECLARE_PRIVATE(LocalPlaybackManager);
Q_OBJECT
Q_PROPERTY(Jellyfin::Model::Player *player READ player NOTIFY playerChanged)
Q_PROPERTY(Jellyfin::DTO::PlayMethodClass::Value playMethod READ playMethod NOTIFY playMethodChanged)
Q_PROPERTY(QUrl streamUrl READ streamUrl NOTIFY streamUrlChanged)
public:
explicit LocalPlaybackManager(QObject *parent = nullptr);
void swap(PlaybackManager& other) override;
Player *player() const;
QString sessionId() const;
DTO::PlayMethod playMethod() const;
const QUrl &streamUrl() const;
PlayerState playbackState() const override;
MediaStatus mediaStatus() const override;
PlaybackManagerError error() const override;
const QString& errorString() const override;
qint64 position() const override;
qint64 duration() const override;
bool seekable() const override;
bool hasAudio() const override;
bool hasVideo() const override;
bool hasNext() const override;
bool hasPrevious() const override;
void playItemInList(const QList<QSharedPointer<Model::Item>> &items, int index) override;
public slots:
void pause() override;
void play() override;
void playItem(QSharedPointer<Model::Item> item) override;
void playItemId(const QString &itemId) override;
void next() override;
void previous() override;
void stop() override;
void seek(qint64 pos) override;
void goTo(int index) override;
signals:
void playerChanged(Jellyfin::Model::Player *newPlayer);
void playMethodChanged(Jellyfin::DTO::PlayMethodClass::Value newPlayMethod);
void streamUrlChanged(const QUrl &newStreamUrl);
};
/**
* @brief Controls playback for remote devices, such as other Jellyfin clients, over the network.
*/
class RemoteJellyfinPlaybackManager {
};
} // NS Model
} // NS Jellyfin
#endif // JELLYFIN_MODEL_PLAYBACKMANAGER_H

View file

@ -0,0 +1,58 @@
/*
* Sailfin: a Jellyfin client written using Qt
* Copyright (C) 2021-2022 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_MODEL_PLAYBACKREPORTER_H
#define JELLYFIN_MODEL_PLAYBACKREPORTER_H
#include <QLoggingCategory>
#include <QObject>
#include <QScopedPointer>
namespace Jellyfin {
class ApiClient;
namespace Model {
Q_DECLARE_LOGGING_CATEGORY(playbackReporter);
class LocalPlaybackManager;
class PlaybackReporterPrivate;
/**
* @brief Reports the current playback state to the Jellyfin server
*
* Set a playbackManager using setPlaybackmanager() and this class
* will do its job.
*/
class PlaybackReporter : public QObject {
Q_OBJECT
Q_DECLARE_PRIVATE(PlaybackReporter);
public:
explicit PlaybackReporter(QObject *parent = nullptr);
void setPlaybackManager(LocalPlaybackManager *playbackManager);
private:
PlaybackReporterPrivate *d_ptr;
};
}
}
#endif //JELLYFIN_MODEL_PLAYBACKREPORTER_H

View file

@ -0,0 +1,152 @@
/*
* Sailfin: a Jellyfin client written using Qt
* Copyright (C) 2021-2022 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_MODEL_PLAYER_H
#define JELLYFIN_MODEL_PLAYER_H
#include <QLoggingCategory>
#include <QObject>
#include <QUrl>
namespace Jellyfin {
namespace Model {
Q_DECLARE_LOGGING_CATEGORY(player)
class PlayerStateClass {
Q_GADGET
public:
enum Value {
Stopped,
Playing,
Paused
};
Q_ENUM(Value);
private:
PlayerStateClass() {}
};
class MediaStatusClass {
Q_GADGET
public:
enum Value {
NoMedia,
Loading,
Loaded,
Stalled,
Buffering,
Buffered,
EndOfMedia,
Error
};
Q_ENUM(Value);
private:
MediaStatusClass() {}
};
using PlayerState = PlayerStateClass::Value;
using MediaStatus = MediaStatusClass::Value;
/**
* @brief Abstract class for a player
*/
class Player : public QObject {
Q_OBJECT
Q_PROPERTY(Jellyfin::Model::PlayerStateClass::Value state READ state NOTIFY stateChanged)
Q_PROPERTY(Jellyfin::Model::MediaStatusClass::Value mediaStatus READ mediaStatus NOTIFY mediaStatusChanged)
Q_PROPERTY(qint64 position READ position NOTIFY positionChanged)
Q_PROPERTY(qint64 duration READ duration NOTIFY durationChanged)
Q_PROPERTY(bool seekable READ seekable NOTIFY seekableChanged)
Q_PROPERTY(bool hasAudio READ hasAudio NOTIFY hasAudioChanged)
Q_PROPERTY(bool hasVideo READ hasVideo NOTIFY hasVideoChanged)
Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged)
// Used in QML by the VideoOutput
Q_PROPERTY(QObject* videoOutputSource READ videoOutputSource NOTIFY videoOutputSourceChanged);
public:
public:
~Player();
virtual PlayerState state() const = 0;
virtual MediaStatus mediaStatus() const = 0;
virtual qint64 position() const = 0;
virtual qint64 duration() const = 0;
virtual bool seekable() const = 0;
virtual bool hasVideo() const = 0;
virtual bool hasAudio() const = 0;
virtual QString errorString() const = 0;
virtual QObject *videoOutputSource() const = 0;
public slots:
virtual void pause() = 0;
virtual void play(qint64 startPos = 0) = 0;
virtual void stop() = 0;
virtual void seek(qint64 position) = 0;
virtual void setMedia(const QUrl &url, int audioIndex = -1, int subTitleIndex = -1) = 0;
signals:
void stateChanged(Jellyfin::Model::PlayerStateClass::Value newState);
void mediaStatusChanged(Jellyfin::Model::MediaStatusClass::Value newMediaStatus);
void positionChanged(qint64 newPosition);
void durationChanged(qint64 newDuration);
void errorStringChanged();
/**
* @brief Sent when the position changed due to calling the seek method.
*/
void seeked();
void seekableChanged(bool seekable);
void hasAudioChanged();
void hasVideoChanged();
void aboutToFinish();
void videoOutputSourceChanged();
};
#define USE_QTMULTIMEDIA_PLAYER
#ifdef USE_QTMULTIMEDIA_PLAYER
class QtMultimediaPlayerPrivate;
/**
* @brief Player implementation that uses QtMultimedia
*/
class QtMultimediaPlayer : public Player {
Q_OBJECT
Q_DECLARE_PRIVATE(QtMultimediaPlayer);
public:
explicit QtMultimediaPlayer(QObject *parent = nullptr);
virtual ~QtMultimediaPlayer();
PlayerState state() const override;
MediaStatus mediaStatus() const override;
qint64 position() const override;
qint64 duration() const override;
bool seekable() const override;
bool hasVideo() const override;
bool hasAudio() const override;
QString errorString() const override;
QObject *videoOutputSource() const override;
public slots:
void pause() override;
void play(qint64 startPos = 0) override;
void stop() override;
void seek(qint64 position) override;
void setMedia(const QUrl &url, int audioIndex, int subtitleIndex) override;
private:
QScopedPointer<QtMultimediaPlayerPrivate> d_ptr;
};
#endif // ifdef USE_QTMULTIMEDIA_PLAYER
} // NS Model
} // NS Jellyfin
#endif // JELLYFIN_MODEL_PLAYER_H

View file

@ -1,6 +1,6 @@
/*
* Sailfin: a Jellyfin client written using Qt
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
* Copyright (C) 2021-2022 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
@ -96,9 +96,9 @@ public:
void clearList();
/**
* @brief Appends all items from the given itemModel to this list
* @brief Appends all items from the given item list to this list
*/
void appendToList(ViewModel::ItemModel &model);
void appendToList(const QList<QSharedPointer<Item>> &model);
/**
* @brief appendToList Appends a single item to the current list

View file

@ -16,6 +16,8 @@
#include <QtCore/QObject>
#include <QtDBus/QtDBus>
#include <QMediaPlayer>
#include "JellyfinQt/model/player.h"
QT_BEGIN_NAMESPACE
class QByteArray;
template<class T> class QList;
@ -190,9 +192,9 @@ private:
ViewModel::PlatformMediaControl *m_mediaControl;
void notifyPropertiesChanged(QStringList properties);
private slots:
void onCurrentItemChanged(ViewModel::Item *newItem);
void onPlaybackStateChanged(QMediaPlayer::State state);
void onMediaStatusChanged(QMediaPlayer::MediaStatus status);
void onCurrentItemChanged();
void onPlaybackStateChanged(Jellyfin::Model::PlayerStateClass::Value state);
void onMediaStatusChanged(Jellyfin::Model::MediaStatusClass::Value status);
void onPositionChanged(qint64 position);
void onSeekableChanged(bool seekable);
void onPlaybackManagerChanged(ViewModel::PlaybackManager *newPlaybackManager);

View file

@ -1,6 +1,6 @@
/*
* Sailfin: a Jellyfin client written using Qt
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
* Copyright (C) 2021-2022 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
@ -39,6 +39,7 @@
#include "../dto/playbackinforesponse.h"
#include "../dto/playmethod.h"
#include "../loader/requesttypes.h"
#include "../model/player.h"
#include "../model/playlist.h"
#include "../support/jsonconv.h"
#include "../viewmodel/item.h"
@ -60,31 +61,34 @@ class PlaystateRequest;
namespace ViewModel {
Q_DECLARE_LOGGING_CATEGORY(playbackManager);
// Later defined in this file
class ItemUrlFetcherThread;
class PlaybackManagerPrivate;
/**
* @brief The PlaybackManager class manages the playback of Jellyfin items. It fetches streams based on Jellyfin items, posts
* the current playback state to the Jellyfin Server, contains the actual media player and so on.
* @brief The PlaybackManager class manages the playback of Jellyfin items.
*
* 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.
* It is a small wrapper around an instance of Jellyfin::Model::PlaybackManager,
* which do the actual work. The Jellyfin::Model::PlaybackManager can be switched
* on the fly, allowing this class to switch between controlling the playback locally
* or remote.
*/
class PlaybackManager : public QObject, public QQmlParserStatus {
friend class ItemUrlFetcherThread;
Q_OBJECT
Q_DECLARE_PRIVATE(PlaybackManager);
Q_INTERFACES(QQmlParserStatus)
public:
using ItemUrlLoader = Support::Loader<DTO::PlaybackInfoResponse, Jellyfin::Loader::GetPostedPlaybackInfoParams>;
explicit PlaybackManager(QObject *parent = nullptr);
virtual ~PlaybackManager();
Q_PROPERTY(ApiClient *apiClient MEMBER m_apiClient WRITE setApiClient)
Q_PROPERTY(ApiClient *apiClient READ apiClient WRITE setApiClient)
Q_PROPERTY(int audioIndex READ audioIndex WRITE setAudioIndex NOTIFY audioIndexChanged)
Q_PROPERTY(int subtitleIndex READ subtitleIndex WRITE setSubtitleIndex NOTIFY subtitleIndexChanged)
Q_PROPERTY(QString streamUrl READ streamUrl NOTIFY streamUrlChanged)
Q_PROPERTY(bool autoOpen MEMBER m_autoOpen NOTIFY autoOpenChanged)
Q_PROPERTY(int audioIndex MEMBER m_audioIndex NOTIFY audioIndexChanged)
Q_PROPERTY(int subtitleIndex MEMBER m_subtitleIndex NOTIFY subtitleIndexChanged)
Q_PROPERTY(bool resumePlayback MEMBER m_resumePlayback NOTIFY resumePlaybackChanged)
//Q_PROPERTY(bool autoOpen MEMBER m_autoOpen NOTIFY autoOpenChanged)
/**
* Whether the player should resume playback.
*/
Q_PROPERTY(bool resumePlayback READ resumePlayback WRITE setResumePlayback NOTIFY resumePlaybackChanged)
Q_PROPERTY(Jellyfin::DTO::PlayMethodClass::Value playMethod READ playMethod NOTIFY playMethodChanged)
// Current Item and queue informatoion
@ -98,42 +102,50 @@ public:
Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged)
Q_PROPERTY(bool hasVideo READ hasVideo NOTIFY hasVideoChanged)
Q_PROPERTY(bool seekable READ seekable NOTIFY seekableChanged)
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(QObject* mediaObject READ mediaObject NOTIFY mediaObjectChanged);
Q_PROPERTY(Jellyfin::Model::MediaStatusClass::Value mediaStatus READ mediaStatus NOTIFY mediaStatusChanged)
Q_PROPERTY(Jellyfin::Model::PlayerStateClass::Value playbackState READ playbackState NOTIFY playbackStateChanged)
Q_PROPERTY(qint64 position READ position NOTIFY positionChanged)
Q_PROPERTY(bool hasNext READ hasNext NOTIFY hasNextChanged)
Q_PROPERTY(bool hasPrevious READ hasPrevious NOTIFY hasPreviousChanged)
/// Whether playstate commands received over the websocket should be handled
Q_PROPERTY(bool handlePlaystateCommands READ handlePlaystateCommands WRITE setHandlePlaystateCommands NOTIFY handlePlaystateCommandsChanged)
ViewModel::Item *item() const { return m_displayItem; }
QSharedPointer<Model::Item> dataItem() const { return m_item; }
ApiClient *apiClient() const { return m_apiClient; }
// R/W props
ApiClient *apiClient() const;
void setApiClient(ApiClient *apiClient);
bool resumePlayback() const;
void setResumePlayback(bool newResumePlayback);
int audioIndex() const;
void setAudioIndex(int newAudioIndex);
int subtitleIndex() const;
void setSubtitleIndex(int newAudioIndex);
QString streamUrl() const { return m_streamUrl; }
PlayMethod playMethod() const { return m_playMethod; }
QObject *mediaObject() const { return m_mediaPlayer; }
qint64 position() const { return m_mediaPlayer->position(); }
qint64 duration() const { return m_mediaPlayer->duration(); }
ViewModel::Playlist *queue() const { return m_displayQueue; }
int queueIndex() const { return m_queueIndex; }
bool hasNext() const { return m_queue->hasNext(); }
bool hasPrevious() const { return m_queue->hasPrevious(); }
ViewModel::Item *item() const;
QSharedPointer<Model::Item> dataItem() const;
QString streamUrl() const;
PlayMethod playMethod() const;
qint64 position() const;
qint64 duration() const;
ViewModel::Playlist *queue() const;
int queueIndex() const;
bool hasNext() const;
bool hasPrevious() const;
// Current media player related property getters
QMediaPlayer::State playbackState() const { return m_playbackState; }
QMediaPlayer::MediaStatus mediaStatus() const { return m_mediaPlayer->mediaStatus(); }
bool hasVideo() const { return m_mediaPlayer->isVideoAvailable(); }
bool seekable() const { return m_mediaPlayer->isSeekable(); }
QObject* mediaObject() const;
Model::PlayerState playbackState() const;
Model::MediaStatus mediaStatus() const;
bool hasVideo() const;
bool seekable() const;
QMediaPlayer::Error error () const;
QString errorString() const;
bool handlePlaystateCommands() const { return m_handlePlaystateCommands; }
void setHandlePlaystateCommands(bool newHandlePlaystateCommands) { m_handlePlaystateCommands = newHandlePlaystateCommands; emit handlePlaystateCommandsChanged(m_handlePlaystateCommands); }
bool handlePlaystateCommands() const;
void setHandlePlaystateCommands(bool newHandlePlaystateCommands);
signals:
void itemChanged(ViewModel::Item *newItemId);
void itemChanged();
void streamUrlChanged(const QString &newStreamUrl);
void autoOpenChanged(bool autoOpen);
void audioIndexChanged(int audioIndex);
@ -145,20 +157,21 @@ signals:
// Emitted when seek has been called.
void seeked(qint64 newPosition);
void hasNextChanged(bool newHasNext);
void hasPreviousChanged(bool newHasPrevious);
// Current media player related property signals
void mediaObjectChanged(QObject *newMediaObject);
void mediaObjectChanged(QObject *newPlayer);
void positionChanged(qint64 newPosition);
void durationChanged(qint64 newDuration);
void queueChanged(QAbstractItemModel *newQueue);
void queueIndexChanged(int newIndex);
void playbackStateChanged(QMediaPlayer::State newState);
void mediaStatusChanged(QMediaPlayer::MediaStatus newMediaStatus);
void playbackStateChanged(Jellyfin::Model::PlayerStateClass::Value newState);
void mediaStatusChanged(Jellyfin::Model::MediaStatusClass::Value newMediaStatus);
void hasVideoChanged(bool newHasVideo);
void seekableChanged(bool newSeekable);
void errorChanged(QMediaPlayer::Error newError);
void errorStringChanged(const QString &newErrorString);
void hasNextChanged(bool newHasNext);
void hasPreviousChanged(bool newHasPrevious);
void handlePlaystateCommandsChanged(bool newHandlePlaystateCommands);
public slots:
/**
@ -187,7 +200,7 @@ public slots:
*/
void skipToItemIndex(int index);
void play();
void pause() { m_mediaPlayer->pause(); setPlaybackState(QMediaPlayer::PausedState); }
void pause();
void seek(qint64 pos);
void stop();
@ -204,105 +217,21 @@ public slots:
void handlePlaystateRequest(const DTO::PlaystateRequest &request);
private slots:
void mediaPlayerStateChanged(QMediaPlayer::State newState);
void mediaPlayerPositionChanged(qint64 position);
void mediaPlayerMediaStatusChanged(QMediaPlayer::MediaStatus newStatus);
void mediaPlayerError(QMediaPlayer::Error error);
void mediaPlayerDurationChanged(qint64 newDuration);
void mediaPlayerSeekableChanged(bool seekable);
/**
* @brief updatePlaybackInfo Updates the Jellyfin server with the current playback progress etc.
*/
void updatePlaybackInfo();
/// Called when we have fetched the playback URL and playSession
void onItemUrlReceived(const QString &itemId, const QUrl &url, const QString &playSession,
// Fully specify class to please MOC
Jellyfin::DTO::PlayMethodClass::Value playMethod);
/// Called when we have encountered an error
void onItemErrorReceived(const QString &itemId, const QString &errorString);
void mediaPlayerItemChanged();
private:
/// Factor to multiply with when converting from milliseconds to ticks.
const static int MS_TICK_FACTOR = 10000;
enum PlaybackInfoType { Started, Stopped, Progress };
/// Timer used to update the play progress on the Jellyfin server
QTimer m_updateTimer;
/// Timer used to notify ourselves when we need to preload the next item
QTimer m_preloadTimer;
ApiClient *m_apiClient = nullptr;
/// The currently playing item
QSharedPointer<Model::Item> m_item;
/// The item that will be played next
QSharedPointer<Model::Item> m_nextItem;
/// The currently played item that will be shown in the GUI
ViewModel::Item *m_displayItem = new ViewModel::Item(this);
/// The currently played queue that will be shown in the GUI
ViewModel::Playlist *m_displayQueue = nullptr;
// Properties for making the streaming request.
QString m_streamUrl;
QString m_nextStreamUrl;
QString m_playSessionId;
QString m_nextPlaySessionId;
QString m_errorString;
QMediaPlayer::Error m_error = QMediaPlayer::NoError;
/// The index of the mediastreams of the to-be-played item containing the audio
int m_audioIndex = 0;
/// The index of the mediastreams of the to-be-played item containing subtitles
int m_subtitleIndex = -1;
/// The position in ticks to resume playback from
qint64 m_resumePosition = 0;
/// The position in ticks the playback was stopped
qint64 m_stopPosition = 0;
/// Keeps track of latest playback position
qint64 m_oldPosition = 0;
/**
* @brief Whether to automatically open the livestream of the item;
*/
bool m_autoOpen = false;
bool m_seekToResumedPosition = false;
QMediaPlayer::State m_oldState = QMediaPlayer::StoppedState;
/// State of the playbackManager. While the internal media player stops after a
/// song has ended, this will not do so.
QMediaPlayer::State m_playbackState = QMediaPlayer::StoppedState;
PlayMethod m_playMethod = PlayMethod::Transcode;
/// Pointer to the current media player.
QMediaPlayer *m_mediaPlayer = nullptr;
Model::Playlist *m_queue = nullptr;
int m_queueIndex = 0;
bool m_resumePlayback = false;
bool m_handlePlaystateCommands = true;
// Helper methods
void setItem(QSharedPointer<Model::Item> newItem);
void setStreamUrl(const QUrl &streamUrl);
void setPlaybackState(QMediaPlayer::State newState);
/**
* @brief Posts the playback information
*/
void postPlaybackInfo(PlaybackInfoType type);
void requestItemUrl(QSharedPointer<Model::Item> item);
void handlePlaybackInfoResponse(QString itemId, QString mediaType, DTO::PlaybackInfoResponse &response);
// QQmlParserListener interface
void classBegin() override { m_qmlIsParsingComponent = true; }
void componentComplete() override;
bool m_qmlIsParsingComponent = false;
/// Time in ms at what moment this playbackmanager should start loading the next item.
const qint64 PRELOAD_DURATION = 15 * 1000;
QTimer m_forceSeekTimer;
QScopedPointer<PlaybackManagerPrivate> d_ptr;
};
} // NS ViewModel