MediaSource.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "MediaSource.h"
  2. #include "CoverDownloader.h"
  3. #include "CoverImage.h"
  4. #include <ranges>
  5. using namespace WallpaperEngine::Media;
  6. MediaSource::MediaSource (std::chrono::milliseconds updateInterval) :
  7. m_aliveFlag (std::make_unique<bool> (true)), m_mediaInfo (
  8. {
  9. .playbackState = PlaybackState::Stopped,
  10. .title = "",
  11. .artist = "",
  12. .url = std::nullopt,
  13. .duration = 0.0f,
  14. .position = 0.0f,
  15. .available = false,
  16. }
  17. ),
  18. m_updateInterval (updateInterval) { }
  19. MediaSource::~MediaSource () {
  20. if (this->m_aliveFlag) {
  21. *m_aliveFlag = false;
  22. }
  23. m_albumArtListeners.clear ();
  24. m_metadataListeners.clear ();
  25. }
  26. void MediaSource::update () {
  27. if (this->m_covers) {
  28. for (const auto& finished : this->m_covers->collect ()) {
  29. // a download of the previous track can land after the player moved on
  30. if (!finished.path.has_value () || this->m_mediaInfo.url != finished.url) {
  31. continue;
  32. }
  33. this->m_mediaInfo.cover = finished.path;
  34. this->notifyAlbumArtListeners ();
  35. }
  36. }
  37. auto now = std::chrono::steady_clock::now ();
  38. if (now <= m_nextUpdate) {
  39. return;
  40. }
  41. m_nextUpdate = now + m_updateInterval;
  42. this->performUpdate ();
  43. }
  44. std::function<void ()> MediaSource::addMetadataListener (std::function<void (const MediaInfo&)> listener) {
  45. int listenerId = ++m_metadataListenerId;
  46. m_metadataListeners.emplace (listenerId, listener);
  47. auto alive = m_aliveFlag;
  48. return [this, listenerId, alive] () {
  49. if (!alive || !*alive) {
  50. return;
  51. }
  52. m_metadataListeners.erase (listenerId);
  53. };
  54. }
  55. std::function<void ()> MediaSource::addAlbumArtListener (std::function<void (const MediaInfo&)> listener) {
  56. int listenerId = ++m_albumArtListenerId;
  57. m_albumArtListeners.emplace (listenerId, listener);
  58. auto alive = m_aliveFlag;
  59. return [this, listenerId, alive] () {
  60. if (!alive || !*alive) {
  61. return;
  62. }
  63. m_albumArtListeners.erase (listenerId);
  64. };
  65. }
  66. void MediaSource::fireMetadataListeners () {
  67. for (auto& listener : m_metadataListeners | std::views::values) {
  68. listener (m_mediaInfo);
  69. }
  70. }
  71. void MediaSource::fireAlbumArtListeners () {
  72. this->resolveCover ();
  73. this->notifyAlbumArtListeners ();
  74. }
  75. void MediaSource::notifyAlbumArtListeners () {
  76. for (auto& listener : m_albumArtListeners | std::views::values) {
  77. listener (m_mediaInfo);
  78. }
  79. }
  80. void MediaSource::resolveCover () {
  81. this->m_mediaInfo.cover.reset ();
  82. if (!this->m_mediaInfo.url.has_value ()) {
  83. return;
  84. }
  85. const auto& url = *this->m_mediaInfo.url;
  86. if (url.starts_with ("http://") || url.starts_with ("https://")) {
  87. if (!this->m_covers) {
  88. this->m_covers = std::make_unique<CoverDownloader> ();
  89. }
  90. // starts the download if not cached, listeners fire again once it lands
  91. this->m_mediaInfo.cover = this->m_covers->request (url);
  92. return;
  93. }
  94. if (const auto path = localCoverPath (url); path.has_value ()) {
  95. std::error_code error;
  96. if (std::filesystem::is_regular_file (*path, error)) {
  97. this->m_mediaInfo.cover = *path;
  98. }
  99. }
  100. }