From b60231796b2b413e4572a05530b50ebec4306646 Mon Sep 17 00:00:00 2001 From: spmn Date: Sun, 6 Oct 2024 04:10:49 +0300 Subject: [PATCH] Simplify artwork logic and add error handling --- vlc-win10smtc/module.cpp | 86 ++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/vlc-win10smtc/module.cpp b/vlc-win10smtc/module.cpp index 0a7ff12..b55c780 100644 --- a/vlc-win10smtc/module.cpp +++ b/vlc-win10smtc/module.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -14,10 +15,7 @@ #include #include #include -#include -#include -#pragma comment(lib, "Shlwapi.lib") #define DEFAULT_THUMBNAIL_URI L"https://upload.wikimedia.org/wikipedia/commons/3/38/VLC_icon.png" void DebugOut(wchar_t* fmt, ...) @@ -38,6 +36,29 @@ using namespace std; using pts = duration>; +template +struct VLCMemory +{ + VLCMemory(T* buf) + : m_buf(buf) + { } + + ~VLCMemory() { + if (m_buf) { + libvlc_free(m_buf); + } + } + + VLCMemory(const VLCMemory&) = delete; + VLCMemory& operator=(const VLCMemory&) = delete; + + operator T* () const { + return m_buf; + } + + T* const m_buf; +}; + struct intf_sys_t { intf_sys_t(const intf_sys_t&) = delete; @@ -178,9 +199,8 @@ struct intf_sys_t return; input_item_t* item = input_GetItem(input); - winrt::hstring title, artist, album, thumburi, path; - char pathbuff[MAX_PATH]; - std::string pathstr; + winrt::hstring title, artist, album; + std::optional thumbnail; auto to_hstring = [](char* buf, winrt::hstring def) { winrt::hstring ret; @@ -189,12 +209,13 @@ struct intf_sys_t ret = winrt::to_hstring(buf); libvlc_free(buf); } - else { + + if (ret.empty()) { ret = def; } return ret; - }; + }; title = to_hstring(input_item_GetTitleFbName(item), L"Unknown Title"); artist = to_hstring(input_item_GetArtist(item), L"Unknown Artist"); @@ -203,40 +224,29 @@ struct intf_sys_t Disp().MusicProperties().Title(title); Disp().MusicProperties().Artist(artist); Disp().MusicProperties().AlbumTitle(album); - - // TODO: use artwork provided by ID3tag (if exists) - //input_attachment_t* p_attachment = input_GetAttachment(p_input, name); - thumburi = to_hstring(input_item_GetArtworkURL(item), L""); - int pathlen = 0; - unsigned long pathlen2 = ARRAYSIZE(pathbuff); - if (thumburi == L"") { - thumburi = DEFAULT_THUMBNAIL_URI; - winrt::Windows::Foundation::Uri uri{ thumburi }; - winrt::Windows::Storage::Streams::RandomAccessStreamReference thumbnail = winrt::Windows::Storage::Streams::RandomAccessStreamReference::CreateFromUri(uri); - Disp().Thumbnail(thumbnail); + try { + if (VLCMemory artworkUrl{ input_item_GetArtworkURL(item) }) { + DebugOut(L"SMTC ArtworkURL: %S", artworkUrl.m_buf); + + if (strnicmp(artworkUrl, "http", 4) == 0) { + winrt::Windows::Foundation::Uri uri{ winrt::to_hstring(artworkUrl.m_buf) }; + thumbnail = winrt::Windows::Storage::Streams::RandomAccessStreamReference::CreateFromUri(uri); + } + else if (VLCMemory artworkLocalPath{ vlc_uri2path(artworkUrl) }) { + auto file = winrt::Windows::Storage::StorageFile::GetFileFromPathAsync(winrt::to_hstring(artworkLocalPath.m_buf)).get(); + thumbnail = winrt::Windows::Storage::Streams::RandomAccessStreamReference::CreateFromFile(file); + } + } + } + catch (const winrt::hresult_error& ex) { + DebugOut(L"SMTC thumbnail exception: %s code: 0x%X", ex.message().c_str(), ex.code().value); } - else { - winrt::Windows::Foundation::Uri uri{ thumburi }; - PathCreateFromUrlA(winrt::to_string(thumburi).c_str(), pathbuff, &pathlen2, NULL); - std::string str(pathbuff, pathlen2); - - path = winrt::to_hstring(str); - pathlen = path.size(); - - // Uncomment to test crashes, and use Casterlabs Caffinated WMC tool to check outputs - //thumburi = DEFAULT_THUMBNAIL_URI; - //uri = winrt::Windows::Foundation::Uri::Uri(thumburi); - //winrt::Windows::Storage::Streams::RandomAccessStreamReference thumbnail = winrt::Windows::Storage::Streams::RandomAccessStreamReference::CreateFromUri(uri); - //Disp().MusicProperties().AlbumArtist(path + winrt::to_hstring(pathlen)); // set this to check the path being used - - // Comment when testing with above - winrt::Windows::Storage::Streams::IRandomAccessStream storagefile = winrt::Windows::Storage::Streams::FileRandomAccessStream::OpenAsync(path, winrt::Windows::Storage::FileAccessMode::Read).get(); - winrt::Windows::Storage::Streams::RandomAccessStreamReference thumbnail = winrt::Windows::Storage::Streams::RandomAccessStreamReference::CreateFromStream(storagefile); - - Disp().Thumbnail(thumbnail); + catch (...) { + DebugOut(L"SMTC thumbnail unknown exception. hres: 0x%X, GetLastError: %d", winrt::to_hresult().value, GetLastError()); } + Disp().Thumbnail(thumbnail.value_or(defaultArt)); Disp().Update(); }