2024-11-01 14:10:00 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winrt/windows.foundation.h>
|
|
|
|
#include <winrt/windows.media.control.h>
|
|
|
|
#include <winrt/windows.storage.streams.h>
|
|
|
|
|
|
|
|
#include <chrono>
|
2024-11-04 15:32:46 +01:00
|
|
|
#include <codecvt>
|
2024-11-01 14:10:00 +01:00
|
|
|
|
|
|
|
#include "../backend.hpp"
|
2024-11-01 16:49:48 +01:00
|
|
|
#include "../utils.hpp"
|
2024-11-01 14:10:00 +01:00
|
|
|
|
|
|
|
using namespace winrt;
|
|
|
|
using namespace Windows::Media::Control;
|
|
|
|
using namespace Windows::Storage::Streams;
|
2024-11-04 15:32:46 +01:00
|
|
|
#define EM_DASH "\xE2\x80\x94"
|
|
|
|
// codecvt is deprecated, but there is no good portable way to do this, I could technically use the winapi as this is the windows backend tho
|
2024-11-01 14:10:00 +01:00
|
|
|
std::string toStdString(winrt::hstring in) {
|
2024-11-04 15:32:46 +01:00
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
|
|
|
return converter.to_bytes(in.c_str());
|
2024-11-01 14:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<MediaInfo> backend::getMediaInformation() {
|
|
|
|
auto sessionManager = GlobalSystemMediaTransportControlsSessionManager::RequestAsync().get();
|
|
|
|
auto currentSession = sessionManager.GetCurrentSession();
|
|
|
|
if (!currentSession)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto playbackInfo = currentSession.GetPlaybackInfo();
|
|
|
|
auto mediaProperties = currentSession.TryGetMediaPropertiesAsync().get();
|
|
|
|
auto timelineInformation = currentSession.GetTimelineProperties();
|
|
|
|
if (!mediaProperties)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto endTime = std::chrono::duration_cast<std::chrono::milliseconds>(timelineInformation.EndTime()).count();
|
|
|
|
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(timelineInformation.Position()).count();
|
|
|
|
|
|
|
|
auto thumbnail = mediaProperties.Thumbnail();
|
|
|
|
std::string thumbnailData = "";
|
|
|
|
|
|
|
|
if (thumbnail) {
|
|
|
|
auto stream = thumbnail.OpenReadAsync().get();
|
|
|
|
size_t size = static_cast<size_t>(stream.Size());
|
|
|
|
|
|
|
|
DataReader reader(stream);
|
|
|
|
reader.LoadAsync(static_cast<uint32_t>(size)).get();
|
|
|
|
|
|
|
|
std::vector<uint8_t> buffer(size);
|
|
|
|
reader.ReadBytes(buffer);
|
|
|
|
thumbnailData = std::string(buffer.begin(), buffer.end());
|
2024-11-01 16:49:48 +01:00
|
|
|
stream.Close();
|
2024-11-01 14:10:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string artist = toStdString(mediaProperties.Artist());
|
2024-11-01 16:49:48 +01:00
|
|
|
std::string albumName = toStdString(mediaProperties.AlbumTitle());
|
2024-11-01 14:10:00 +01:00
|
|
|
if (artist == "")
|
|
|
|
artist = toStdString(mediaProperties.AlbumArtist()); // Needed for some apps
|
|
|
|
|
2024-11-04 15:32:46 +01:00
|
|
|
if (artist.find(EM_DASH) != std::string::npos) {
|
|
|
|
albumName = artist.substr(artist.find(EM_DASH) + 3);
|
|
|
|
artist = artist.substr(0, artist.find(EM_DASH));
|
2024-11-01 16:49:48 +01:00
|
|
|
utils::trim(artist);
|
|
|
|
utils::trim(albumName);
|
|
|
|
}
|
|
|
|
|
2024-11-01 14:10:00 +01:00
|
|
|
return std::make_shared<MediaInfo>(
|
|
|
|
playbackInfo.PlaybackStatus() == GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused,
|
2024-11-01 16:49:48 +01:00
|
|
|
toStdString(mediaProperties.Title()), artist, albumName, toStdString(currentSession.SourceAppUserModelId()),
|
|
|
|
thumbnailData, endTime, elapsedTime);
|
2024-11-01 14:10:00 +01:00
|
|
|
}
|
2024-11-04 15:32:46 +01:00
|
|
|
#undef EM_DASH
|
2024-11-01 14:10:00 +01:00
|
|
|
#endif
|