2020-09-27 18:38:33 +00:00
|
|
|
/*
|
|
|
|
Sailfin: a Jellyfin client written using Qt
|
2021-02-17 18:42:10 +00:00
|
|
|
Copyright (C) 2021 Chris Josten
|
2020-09-27 18:38:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
#ifndef JELLYFIN_MEDIA_SOURCE_H
|
|
|
|
#define JELLYFIN_MEDIA_SOURCE_H
|
|
|
|
|
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-02-21 04:02:05 +00:00
|
|
|
#include <JellyfinQt/DTO/baseitemdto.h>
|
|
|
|
#include "remotedata.h"
|
2020-09-25 12:46:39 +00:00
|
|
|
|
2021-02-17 18:42:10 +00:00
|
|
|
#include "apiclient.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-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 {
|
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:
|
2020-10-01 19:45:34 +00:00
|
|
|
enum PlayMethod {
|
|
|
|
Transcode,
|
|
|
|
Stream,
|
|
|
|
DirectPlay
|
|
|
|
};
|
|
|
|
Q_ENUM(PlayMethod)
|
2021-02-20 22:20:39 +00:00
|
|
|
using FetchCallback = std::function<void(QUrl &&, PlayMethod)>;
|
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-02-14 12:29:30 +00:00
|
|
|
Q_PROPERTY(PlayMethod 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
|
|
|
|
Q_PROPERTY(Item *item READ item NOTIFY itemChanged)
|
|
|
|
Q_PROPERTY(QAbstractItemModel *queue READ queue NOTIFY queueChanged)
|
|
|
|
Q_PROPERTY(int queueIndex READ queueIndex NOTIFY queueIndexChanged)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
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)
|
2020-10-01 19:45:34 +00:00
|
|
|
|
2021-02-21 04:02:05 +00:00
|
|
|
BaseItemDto *item() const { return m_item->data(); }
|
|
|
|
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(); }
|
|
|
|
ItemModel *queue() const { return m_queue; }
|
|
|
|
int queueIndex() const { return m_queueIndex; }
|
|
|
|
|
|
|
|
// 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(); }
|
|
|
|
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-02-21 04:02:05 +00:00
|
|
|
void itemChanged(BaseItemDto *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);
|
|
|
|
void queueChanged(ItemModel *newQue);
|
|
|
|
void queueIndexChanged(int newIndex);
|
|
|
|
void playbackStateChanged(QMediaPlayer::State newState);
|
|
|
|
void mediaStatusChanged(QMediaPlayer::MediaStatus newMediaStatus);
|
|
|
|
void hasVideoChanged(bool newHasVideo);
|
|
|
|
void errorChanged(QMediaPlayer::Error newError);
|
|
|
|
void errorStringChanged(const QString &newErrorString);
|
2020-09-25 12:46:39 +00:00
|
|
|
public slots:
|
2021-02-14 17:40:46 +00:00
|
|
|
/**
|
|
|
|
* @brief playItem Plays the item with the given id. This will construct the Jellyfin::Item internally
|
|
|
|
* and delete it later.
|
|
|
|
* @param itemId The id of the item to play.
|
|
|
|
*/
|
|
|
|
void playItem(const QString &itemId);
|
2021-02-20 22:20:39 +00:00
|
|
|
void playItemInList(ItemModel *itemList, int index);
|
|
|
|
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);
|
|
|
|
/**
|
|
|
|
* @brief updatePlaybackInfo Updates the Jellyfin server with the current playback progress etc.
|
|
|
|
*/
|
|
|
|
void updatePlaybackInfo();
|
2020-09-25 12:46:39 +00:00
|
|
|
|
|
|
|
private:
|
2020-10-01 19:45:34 +00:00
|
|
|
QTimer m_updateTimer;
|
2020-09-25 12:46:39 +00:00
|
|
|
ApiClient *m_apiClient = nullptr;
|
2021-02-21 04:02:05 +00:00
|
|
|
RemoteItem *m_item;
|
2020-09-25 12:46:39 +00:00
|
|
|
QString m_streamUrl;
|
|
|
|
QString m_playSessionId;
|
2020-09-25 15:14:44 +00:00
|
|
|
int m_audioIndex = 0;
|
|
|
|
int m_subtitleIndex = -1;
|
2021-02-13 23:21:49 +00:00
|
|
|
qint64 m_resumePosition = 0;
|
|
|
|
qint64 m_oldPosition = 0;
|
2020-10-01 19:45:34 +00:00
|
|
|
qint64 m_stopPosition = 0;
|
2021-02-13 23:21:49 +00:00
|
|
|
QMediaPlayer::State m_oldState = QMediaPlayer::StoppedState;
|
2021-02-14 12:29:30 +00:00
|
|
|
PlayMethod m_playMethod = Transcode;
|
2021-02-20 22:20:39 +00:00
|
|
|
QMediaPlayer::State m_playbackState = QMediaPlayer::StoppedState;
|
|
|
|
// Pointer to the current media player.
|
|
|
|
QMediaPlayer *m_mediaPlayer = nullptr;
|
|
|
|
|
|
|
|
QMediaPlayer *m_mediaPlayer1;
|
|
|
|
QMediaPlayer *m_mediaPlayer2;
|
|
|
|
ItemModel *m_queue = nullptr;
|
|
|
|
int m_queueIndex = 0;
|
2021-02-13 23:21:49 +00:00
|
|
|
bool m_resumePlayback = true;
|
|
|
|
|
2021-02-21 04:02:05 +00:00
|
|
|
void setItem(BaseItemDto *newItem);
|
2021-02-20 22:20:39 +00:00
|
|
|
void swapMediaPlayer();
|
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
bool m_qmlIsParsingComponent = false;
|
2020-09-25 15:14:44 +00:00
|
|
|
|
2020-09-25 12:46:39 +00:00
|
|
|
/**
|
|
|
|
* @brief Whether to automatically open the livestream of the item;
|
|
|
|
*/
|
|
|
|
bool m_autoOpen = false;
|
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
/**
|
|
|
|
* @brief Retrieves the URL of the stream to open.
|
|
|
|
*/
|
2021-02-21 04:02:05 +00:00
|
|
|
void fetchStreamUrl(const BaseItemDto *item, bool autoOpen, const FetchCallback &callback);
|
|
|
|
void fetchAndSetStreamUrl(const BaseItemDto *item);
|
2020-09-25 12:46:39 +00:00
|
|
|
void setStreamUrl(const QString &streamUrl);
|
2021-02-20 22:20:39 +00:00
|
|
|
void setPlaybackState(QMediaPlayer::State newState);
|
|
|
|
|
2021-02-21 04:02:05 +00:00
|
|
|
BaseItemDto *nextItem();
|
2021-02-20 22:20:39 +00:00
|
|
|
void setQueue(ItemModel *itemModel);
|
2020-10-01 19:45:34 +00:00
|
|
|
|
|
|
|
// Factor to multiply with when converting from milliseconds to ticks.
|
2021-02-13 23:21:49 +00:00
|
|
|
const static int MS_TICK_FACTOR = 10000;
|
2020-10-01 19:45:34 +00:00
|
|
|
|
|
|
|
enum PlaybackInfoType { Started, Stopped, Progress };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Posts the playback information
|
|
|
|
*/
|
|
|
|
void postPlaybackInfo(PlaybackInfoType type);
|
2021-02-13 23:21:49 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
void classBegin() override {
|
|
|
|
m_qmlIsParsingComponent = true;
|
|
|
|
}
|
|
|
|
void componentComplete() override;
|
2020-09-25 12:46:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // JELLYFIN_MEDIA_SOURCE_H
|