2020-09-27 18:38:33 +00:00
|
|
|
/*
|
|
|
|
Sailfin: a Jellyfin client written using Qt
|
|
|
|
Copyright (C) 2020 Chris Josten
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QObject>
|
2021-02-13 23:21:49 +00:00
|
|
|
#include <QtGlobal>
|
2020-10-01 19:45:34 +00:00
|
|
|
#include <QVariant>
|
2020-09-25 12:46:39 +00:00
|
|
|
|
|
|
|
#include <QUrlQuery>
|
|
|
|
|
|
|
|
#include <QtMultimedia/QMediaPlayer>
|
|
|
|
|
|
|
|
|
|
|
|
#include "jellyfinapiclient.h"
|
2021-02-13 23:21:49 +00:00
|
|
|
#include "jellyfinitem.h"
|
2020-09-25 12:46:39 +00:00
|
|
|
|
|
|
|
namespace Jellyfin {
|
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
// Forward declaration of Jellyfin::Item found in jellyfinitem.h
|
|
|
|
class Item;
|
|
|
|
// Forward declaration of Jellyfin::ApiClient found in jellyfinapiclient.h
|
|
|
|
class ApiClient;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 and so on.
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
|
2020-10-08 01:00:08 +00:00
|
|
|
explicit PlaybackManager(QObject *parent = nullptr);
|
2020-09-25 12:46:39 +00:00
|
|
|
Q_PROPERTY(ApiClient *apiClient MEMBER m_apiClient)
|
2021-02-13 23:21:49 +00:00
|
|
|
Q_PROPERTY(Item *item READ item WRITE setItem NOTIFY itemChanged)
|
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)
|
|
|
|
Q_PROPERTY(QObject* mediaPlayer READ mediaPlayer WRITE setMediaPlayer NOTIFY mediaPlayerChanged)
|
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-13 23:21:49 +00:00
|
|
|
Item *item() const { return m_item; }
|
|
|
|
void setItem(Item *newItem);
|
2020-10-01 19:45:34 +00:00
|
|
|
|
2021-02-13 23:21:49 +00:00
|
|
|
QObject *mediaPlayer() const {
|
|
|
|
return m_qmlMediaPlayer;
|
|
|
|
}
|
|
|
|
void setMediaPlayer(QObject *qmlMediaPlayer);
|
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; }
|
2020-09-25 12:46:39 +00:00
|
|
|
signals:
|
2021-02-13 23:21:49 +00:00
|
|
|
void itemChanged(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
|
|
|
|
|
|
|
public slots:
|
2020-10-01 19:45:34 +00:00
|
|
|
void updatePlaybackInfo();
|
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-13 23:21:49 +00:00
|
|
|
private slots:
|
|
|
|
void mediaPlayerStateChanged(QMediaPlayer::State newState);
|
|
|
|
void mediaPlayerPositionChanged(qint64 position);
|
|
|
|
void mediaPlayerMediaStatusChanged(QMediaPlayer::MediaStatus newStatus);
|
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-13 23:21:49 +00:00
|
|
|
Item *m_item = nullptr;
|
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-13 23:21:49 +00:00
|
|
|
QObject *m_qmlMediaPlayer = nullptr;
|
|
|
|
QMediaPlayer * m_mediaPlayer = nullptr;
|
|
|
|
bool m_resumePlayback = true;
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-09-25 12:46:39 +00:00
|
|
|
void fetchStreamUrl();
|
|
|
|
void setStreamUrl(const QString &streamUrl);
|
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
|
|
|
|
|
|
|
void classBegin() override {
|
|
|
|
m_qmlIsParsingComponent = true;
|
|
|
|
}
|
|
|
|
void componentComplete() override;
|
2020-09-25 12:46:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // JELLYFIN_MEDIA_SOURCE_H
|