mpriscontrolplugin-win.cpp 12.5 KB
Newer Older
Yuri Chornoivan's avatar
Yuri Chornoivan committed
1
/**
2
 * SPDX-FileCopyrightText: 2018 Jun Bo Bi <jambonmcyeah@gmail.com>
Yuri Chornoivan's avatar
Yuri Chornoivan committed
3
 *
4
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
Yuri Chornoivan's avatar
Yuri Chornoivan committed
5
6
 */

7
#include "mpriscontrolplugin-win.h"
8
#include "plugin_mpris_debug.h"
9

10
11
#include <core/device.h>

12
13
#include <KPluginFactory>

14
15
16
17
18
19
20
21
#include <QBuffer>

#include <chrono>
#include <random>

#include <ppltasks.h>

using namespace Windows::Foundation;
22

Nicolas Fella's avatar
Nicolas Fella committed
23
K_PLUGIN_CLASS_WITH_JSON(MprisControlPlugin, "kdeconnect_mpriscontrol.json")
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
MprisControlPlugin::MprisControlPlugin(QObject *parent, const QVariantList &args) : KdeConnectPlugin(parent, args) {
    sessionManager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync().get();
    sessionManager->SessionsChanged([this](GlobalSystemMediaTransportControlsSessionManager, SessionsChangedEventArgs){
        this->updatePlayerList();
    });
    this->updatePlayerList();
}

std::optional<QString> MprisControlPlugin::getPlayerName(GlobalSystemMediaTransportControlsSession const& player) {
    auto entry = std::find(this->playerList.constBegin(), this->playerList.constEnd(), player);

    if(entry == this->playerList.constEnd()) {
        qCWarning(KDECONNECT_PLUGIN_MPRIS) << "PlaybackInfoChanged received for no longer tracked session" << player.SourceAppUserModelId().c_str();
        return std::nullopt;
    }

    return entry.key();
}

QString MprisControlPlugin::randomUrl() {
    const QString VALID_CHARS = QStringLiteral("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(0,VALID_CHARS.size() - 1);
    
    const int size = 10;
    QString fileUrl(size, QChar());
    for(int i = 0; i < size; i++) {
        fileUrl[i] = VALID_CHARS[distribution(generator)];
    }

    return QStringLiteral("file://") + fileUrl;
}

void MprisControlPlugin::sendMediaProperties(std::variant<NetworkPacket, QString> const& packetOrName, GlobalSystemMediaTransportControlsSession const& player) {
    NetworkPacket np = packetOrName.index() == 0 ? std::get<0>(packetOrName) : NetworkPacket(PACKET_TYPE_MPRIS);
    if(packetOrName.index() == 1)
        np.set(QStringLiteral("player"), std::get<1>(packetOrName));

    auto mediaProperties = player.TryGetMediaPropertiesAsync().get();

    np.set(QStringLiteral("title"), QString::fromWCharArray(mediaProperties.Title().c_str()));
    np.set(QStringLiteral("artist"), QString::fromWCharArray(mediaProperties.Artist().c_str()));
    np.set(QStringLiteral("album"), QString::fromWCharArray(mediaProperties.AlbumTitle().c_str()));
    np.set(QStringLiteral("albumArtUrl"), randomUrl());
    np.set(QStringLiteral("nowPlaying"), mediaProperties.Artist().empty() ?  QString::fromWCharArray(mediaProperties.Title().c_str()) : (QString::fromWCharArray(mediaProperties.Artist().c_str()) + QStringLiteral(" - ") + QString::fromWCharArray(mediaProperties.Title().c_str())));
    
    np.set(QStringLiteral("url"), QString());
    sendTimelineProperties(np, player, true); // "length"

    if(packetOrName.index() == 1)
        sendPacket(np);
}

void MprisControlPlugin::sendPlaybackInfo(std::variant<NetworkPacket, QString> const& packetOrName, GlobalSystemMediaTransportControlsSession const& player) {
    
    NetworkPacket np = packetOrName.index() == 0 ? std::get<0>(packetOrName) : NetworkPacket(PACKET_TYPE_MPRIS);
    if(packetOrName.index() == 1)
        np.set(QStringLiteral("player"), std::get<1>(packetOrName));
    
    sendMediaProperties(np, player);

    auto playbackInfo = player.GetPlaybackInfo();
    auto playbackControls = playbackInfo.Controls();

    np.set(QStringLiteral("isPlaying"), playbackInfo.PlaybackStatus() == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Playing);
    np.set(QStringLiteral("canPause"), playbackControls.IsPauseEnabled());
    np.set(QStringLiteral("canPlay"), playbackControls.IsPlayEnabled());
    np.set(QStringLiteral("canGoNext"), playbackControls.IsNextEnabled());
    np.set(QStringLiteral("canGoPrevious"), playbackControls.IsPreviousEnabled());

    sendTimelineProperties(np, player);

    if(packetOrName.index() == 1)
        sendPacket(np);
}

void MprisControlPlugin::sendTimelineProperties(std::variant<NetworkPacket, QString> const& packetOrName, GlobalSystemMediaTransportControlsSession const& player, bool lengthOnly) {
    NetworkPacket np = packetOrName.index() == 0 ? std::get<0>(packetOrName) : NetworkPacket(PACKET_TYPE_MPRIS);
    if(packetOrName.index() == 1)
        np.set(QStringLiteral("player"), std::get<1>(packetOrName));

    auto timelineProperties = player.GetTimelineProperties();

    if(!lengthOnly){
        np.set(QStringLiteral("canSeek"), timelineProperties.MinSeekTime() != timelineProperties.MaxSeekTime());
        np.set(QStringLiteral("pos"), std::chrono::duration_cast<std::chrono::milliseconds>(timelineProperties.Position() - timelineProperties.StartTime()).count());
    }
    np.set(QStringLiteral("length"), std::chrono::duration_cast<std::chrono::milliseconds>(timelineProperties.EndTime() - timelineProperties.StartTime()).count());

    if(packetOrName.index() == 1)
        sendPacket(np);
}

void MprisControlPlugin::updatePlayerList() {
    playerList.clear();
    playbackInfoChangedHandlers.clear();
    mediaPropertiesChangedHandlers.clear();
    timelinePropertiesChangedHandlers.clear();

    auto sessions = sessionManager->GetSessions();
    playbackInfoChangedHandlers.resize(sessions.Size());
    mediaPropertiesChangedHandlers.resize(sessions.Size());
    timelinePropertiesChangedHandlers.resize(sessions.Size());

    for(uint32_t i = 0; i < sessions.Size(); i++) {
        const auto player = sessions.GetAt(i);
131
        auto playerName = player.SourceAppUserModelId();
132

133
134
135
136
137
138
139
140
        // try to resolve the AUMID to a user-friendly name
        try {
            playerName = AppInfo::GetFromAppUserModelId(playerName).DisplayInfo().DisplayName();
        } catch (winrt::hresult_error e) {
            qCDebug(KDECONNECT_PLUGIN_MPRIS) << QString::fromWCharArray(playerName.c_str()) << "doesn\'t have a valid AppUserModelID! Sending as-is..";
        }

        QString uniqueName = QString::fromWCharArray(playerName.c_str());
141
        for (int i = 2; playerList.contains(uniqueName); ++i) {
142
            uniqueName += QStringLiteral(" [") + QString::number(i) + QStringLiteral("]");
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
        }

        playerList.insert(uniqueName, player);

        player.PlaybackInfoChanged(auto_revoke, [this](GlobalSystemMediaTransportControlsSession player, PlaybackInfoChangedEventArgs args){
            if(auto name = getPlayerName(player))
                this->sendPlaybackInfo(name.value(), player);
        }).swap(playbackInfoChangedHandlers[i]);
        concurrency::create_task([this, player]{
            std::chrono::milliseconds timespan(50);
            std::this_thread::sleep_for(timespan);

            if(auto name = getPlayerName(player))
                this->sendPlaybackInfo(name.value(), player);
        });

        if(auto name = getPlayerName(player))
            sendPlaybackInfo(name.value(), player);

        player.MediaPropertiesChanged(auto_revoke, [this](GlobalSystemMediaTransportControlsSession player, MediaPropertiesChangedEventArgs args){
            if(auto name = getPlayerName(player))
                this->sendMediaProperties(name.value(), player);
        }).swap(mediaPropertiesChangedHandlers[i]);
        concurrency::create_task([this, player]{
            std::chrono::milliseconds timespan(50);
            std::this_thread::sleep_for(timespan);

            if(auto name = getPlayerName(player))
                this->sendMediaProperties(name.value(), player);
        });

        player.TimelinePropertiesChanged(auto_revoke, [this](GlobalSystemMediaTransportControlsSession player, TimelinePropertiesChangedEventArgs args){
            if(auto name = getPlayerName(player))
                this->sendTimelineProperties(name.value(), player);
        }).swap(timelinePropertiesChangedHandlers[i]);
        concurrency::create_task([this, player]{
            std::chrono::milliseconds timespan(50);
            std::this_thread::sleep_for(timespan);

            if(auto name = getPlayerName(player))
                this->sendTimelineProperties(name.value(), player);
        });
    }

    sendPlayerList();
}

void MprisControlPlugin::sendPlayerList() {
    NetworkPacket np(PACKET_TYPE_MPRIS);

    np.set(QStringLiteral("playerList"), playerList.keys());
    np.set(QStringLiteral("supportAlbumArtPayload"), false); // TODO: Sending albumArt doesn't work

    sendPacket(np);
}

bool MprisControlPlugin::sendAlbumArt(std::variant<NetworkPacket, QString> const& packetOrName, GlobalSystemMediaTransportControlsSession const& player, QString artUrl) {
    qWarning(KDECONNECT_PLUGIN_MPRIS) << "Sending Album Art";
    NetworkPacket np = packetOrName.index() == 0 ? std::get<0>(packetOrName) : NetworkPacket(PACKET_TYPE_MPRIS);
    if(packetOrName.index() == 1)
        np.set(QStringLiteral("player"), std::get<1>(packetOrName));
    
    auto thumbnail = player.TryGetMediaPropertiesAsync().get().Thumbnail();
    if(thumbnail) {
        auto stream = thumbnail.OpenReadAsync().get();
        if(stream && stream.CanRead()) {
            IBuffer data = Buffer(stream.Size());
            data = stream.ReadAsync(data, stream.Size(), InputStreamOptions::None).get();
            QSharedPointer<QBuffer> qdata = QSharedPointer<QBuffer>(new QBuffer());
            qdata->setData((char*)data.data(), data.Capacity());

            np.set(QStringLiteral("transferringAlbumArt"), true);
            np.set(QStringLiteral("albumArtUrl"), artUrl);

            np.setPayload(qdata, qdata->size());

            if(packetOrName.index() == 1)
                sendPacket(np);

            return true;
        }

        return false;
    }
    else {
        return false;
    }
}
231
232
233
234
235
236
237
238

bool MprisControlPlugin::receivePacket(const NetworkPacket &np)
{
    if (np.has(QStringLiteral("playerList"))) {
        return false; //Whoever sent this is an mpris client and not an mpris control!
    }

    //Send the player list
239
240
241
    const QString name = np.get<QString>(QStringLiteral("player"));
    auto it = playerList.find(name);
    bool valid_player = (it != playerList.end());
242
    if (!valid_player || np.get<bool>(QStringLiteral("requestPlayerList"))) {
243
        sendPlayerList();
244
245
246
247
248
        if (!valid_player) {
            return true;
        }
    }

249
250
251
252
253
254
    auto player = it.value();

    if (np.has(QStringLiteral("albumArtUrl"))) {
        return sendAlbumArt(name, player, np.get<QString>(QStringLiteral("albumArtUrl")));
    }

255
    if (np.has(QStringLiteral("action"))) {
256
257
258
259
260
261
        const QString& action = np.get<QString>(QStringLiteral("action"));
        if(action == QStringLiteral("Next")) {
            player.TrySkipNextAsync().get();
        }
        else if(action == QStringLiteral("Previous")) {
            player.TrySkipPreviousAsync().get();
262
        }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
        else if (action == QStringLiteral("Pause"))
        {
            player.TryPauseAsync().get();
        }
        else if (action == QStringLiteral("PlayPause"))
        {
            player.TryTogglePlayPauseAsync().get();
        }
        else if (action == QStringLiteral("Stop"))
        {
            player.TryStopAsync().get();
        }
        else if (action == QStringLiteral("Play"))
        {
            player.TryPlayAsync().get();
        }
    }
    if (np.has(QStringLiteral("setVolume"))) {
        qWarning(KDECONNECT_PLUGIN_MPRIS) << "Setting volume is not supported";
    }
    if (np.has(QStringLiteral("Seek"))) {
        TimeSpan offset = std::chrono::microseconds(np.get<int>(QStringLiteral("Seek")));
        qWarning(KDECONNECT_PLUGIN_MPRIS) << "Seeking" << offset.count() << "ns to" << name;
        player.TryChangePlaybackPositionAsync((player.GetTimelineProperties().Position() + offset).count()).get();
    }
Nicolas Fella's avatar
Nicolas Fella committed
288

289
290
291
    if (np.has(QStringLiteral("SetPosition"))){
        TimeSpan position = std::chrono::milliseconds(np.get<qlonglong>(QStringLiteral("SetPosition"), 0));
        player.TryChangePlaybackPositionAsync((player.GetTimelineProperties().StartTime() + position).count()).get();
292
293
    }

294
    //Send something read from the mpris interface
295
    NetworkPacket answer(PACKET_TYPE_MPRIS);
296
    answer.set(QStringLiteral("player"), name);
297
298
    bool somethingToSend = false;
    if (np.get<bool>(QStringLiteral("requestNowPlaying"))) {
299
        sendPlaybackInfo(answer, player);
300
301
302
303
304
305
306
307
308
309
310
311
312
313
        somethingToSend = true;
    }
    if (np.get<bool>(QStringLiteral("requestVolume"))) {
        answer.set(QStringLiteral("volume"), 100);
        somethingToSend = true;
    }

    if (somethingToSend) {
        sendPacket(answer);
    }

    return true;
}

Nicolas Fella's avatar
Nicolas Fella committed
314
#include "mpriscontrolplugin-win.moc"