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

Improve PlaybackManager stability, added PlaybackBar

- PlaybackManager now takes ownership of set items
- PlaybackManager can play items just by their itemId, avoids useless
  item creation on the QML side of things.
- Allow calling Jellyfin::registerTypes with a different URI
- Minor code cleanup
This commit is contained in:
Chris Josten 2021-02-14 18:40:46 +01:00
parent 7e77abc173
commit b699f6e74d
12 changed files with 170 additions and 16 deletions

View file

@ -10,7 +10,7 @@
#include "JellyfinQt/jellyfinplaybackmanager.h"
namespace Jellyfin {
void registerTypes();
void registerTypes(const char *uri = "nl.netsoj.chris.Jellyfin");
}
#endif // JELLYFIN_H

View file

@ -302,6 +302,7 @@ class Item : public RemoteData {
Q_OBJECT
public:
Q_INVOKABLE explicit Item(QObject *parent = nullptr);
Item(QString itemId, ApiClient *apiClient, QObject *parent = nullptr);
Q_PROPERTY(QString jellyfinId READ jellyfinId WRITE setJellyfinId NOTIFY jellyfinIdChanged)
@ -473,7 +474,7 @@ protected:
QString getDataUrl() const override;
bool canReload() const override;
QString m_id;
QString m_id = QStringLiteral("");
QString m_name;
QString m_originalTitle;
QString m_serverId;

View file

@ -89,6 +89,12 @@ signals:
public slots:
void updatePlaybackInfo();
/**
* @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);
private slots:
void mediaPlayerStateChanged(QMediaPlayer::State newState);
void mediaPlayerPositionChanged(qint64 position);

View file

@ -19,19 +19,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "JellyfinQt/jellyfin.h"
namespace Jellyfin {
void registerTypes() {
const char* QML_NAMESPACE = "nl.netsoj.chris.Jellyfin";
void registerTypes(const char *uri) {
// Singletons are perhaps bad, but they are convenient :)
qmlRegisterSingletonType<Jellyfin::ApiClient>(QML_NAMESPACE, 1, 0, "ApiClient", [](QQmlEngine *eng, QJSEngine *js) {
qmlRegisterSingletonType<Jellyfin::ApiClient>(uri, 1, 0, "ApiClient", [](QQmlEngine *eng, QJSEngine *js) {
Q_UNUSED(eng)
Q_UNUSED(js)
return dynamic_cast<QObject*>(new Jellyfin::ApiClient());
});
qmlRegisterType<Jellyfin::ServerDiscoveryModel>(QML_NAMESPACE, 1, 0, "ServerDiscoveryModel");
qmlRegisterType<Jellyfin::PlaybackManager>(QML_NAMESPACE, 1, 0, "PlaybackManager");
qmlRegisterType<Jellyfin::ServerDiscoveryModel>(uri, 1, 0, "ServerDiscoveryModel");
qmlRegisterType<Jellyfin::PlaybackManager>(uri, 1, 0, "PlaybackManager");
// API models
Jellyfin::registerModels(QML_NAMESPACE);
Jellyfin::registerSerializableJsonTypes(QML_NAMESPACE);
Jellyfin::registerModels(uri);
Jellyfin::registerSerializableJsonTypes(uri);
}
}

View file

@ -25,8 +25,6 @@ ApiClient::ApiClient(QObject *parent)
: QObject(parent),
m_webSocket(new WebSocket(this)) {
m_deviceName = QHostInfo::localHostName();
uint uuid1 = qHash(m_deviceName);
uint uuid2 = qHash(QSysInfo::productVersion());
m_deviceId = QUuid::createUuid().toString(); // TODO: make this not random?
m_credManager = CredentialsManager::newInstance(this);

View file

@ -54,7 +54,7 @@ void JsonSerializable::deserialize(const QJsonObject &jObj) {
QMetaProperty realProp = obj->property(obj->indexOfProperty(prop.name() + 8));
if (!realProp.write(this, jsonToVariant(prop, val, jObj))) {
qDebug() << "Write to " << prop.name() << "failed";
};
}
}
}
}
@ -328,6 +328,14 @@ Item::Item(QObject *parent) : RemoteData(parent) {
});
}
Item::Item(QString id, ApiClient *apiClient, QObject *parent)
: RemoteData(parent), m_id(id) {
connect(this, &RemoteData::apiClientChanged, this, [this](ApiClient *newApiClient) {
connect(newApiClient, &ApiClient::userDataChanged, this, &Item::onUserDataChanged);
});
setApiClient(apiClient);
}
QString Item::getDataUrl() const {
return QString("/Users/") + m_apiClient->userId() + "/Items/" + m_id;
}

View file

@ -99,7 +99,13 @@ void PlaybackManager::fetchStreamUrl() {
void PlaybackManager::setItem(Item *newItem) {
if (m_mediaPlayer != nullptr) m_mediaPlayer->stop();
// If we own the item, delete it.
if (m_item != nullptr && m_item->parent() == this) {
m_item->deleteLater();
}
this->m_item = newItem;
emit itemChanged(newItem);
// Don't try to start fetching when we're not completely parsed yet.
if (m_qmlIsParsingComponent) return;
@ -110,6 +116,16 @@ void PlaybackManager::setItem(Item *newItem) {
// Deinitialize the streamUrl
setStreamUrl("");
if (newItem != nullptr) {
if (newItem->parent() != this) {
// The new item may outlive the lifetime of the element it was created on. In the Sailfish
// application for example, the player is given an Jellyfin::Item that sits on a Page on a PageStack.
// As soon as the user pops the Page from the PageStack, newItem would be destroyed. Therefore, we
// take ownership of the given newItem, as this object will usually exist throughout the lifetime of
// the application. A better solution would be to create a copy of the newItem, but no way I'm going
// to create an handwritten copy of that.
QQmlEngine::setObjectOwnership(newItem, QQmlEngine::ObjectOwnership::CppOwnership);
newItem->setParent(this);
}
if (m_item->status() == RemoteData::Ready) {
fetchStreamUrl();
} else {
@ -192,6 +208,11 @@ void PlaybackManager::updatePlaybackInfo() {
postPlaybackInfo(Progress);
}
void PlaybackManager::playItem(const QString &itemId) {
Item *newItem = new Item(itemId, m_apiClient, this);
setItem(newItem);
}
void PlaybackManager::postPlaybackInfo(PlaybackInfoType type) {
QJsonObject root;