SpotifyTrack.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "SpotifyTrack.h"
  2. #include "unistd.h"
  3. #include "MercuryManager.h"
  4. #include <cassert>
  5. #include "CspotAssert.h"
  6. #include "Logger.h"
  7. #include "ConfigJSON.h"
  8. SpotifyTrack::SpotifyTrack(std::shared_ptr<MercuryManager> manager, std::shared_ptr<TrackReference> trackReference, uint32_t position_ms, bool isPaused)
  9. {
  10. this->manager = manager;
  11. this->fileId = std::vector<uint8_t>();
  12. episodeInfo = Episode_init_default;
  13. trackInfo = Track_init_default;
  14. mercuryCallback trackResponseLambda = [=](std::unique_ptr<MercuryResponse> res) {
  15. this->trackInformationCallback(std::move(res), position_ms, isPaused);
  16. };
  17. mercuryCallback episodeResponseLambda = [=](std::unique_ptr<MercuryResponse> res) {
  18. this->episodeInformationCallback(std::move(res), position_ms, isPaused);
  19. };
  20. if (trackReference->isEpisode)
  21. {
  22. this->reqSeqNum = this->manager->execute(MercuryType::GET, "hm://metadata/3/episode/" + bytesToHexString(trackReference->gid), episodeResponseLambda);
  23. }
  24. else
  25. {
  26. this->reqSeqNum = this->manager->execute(MercuryType::GET, "hm://metadata/3/track/" + bytesToHexString(trackReference->gid), trackResponseLambda);
  27. }
  28. }
  29. SpotifyTrack::~SpotifyTrack()
  30. {
  31. this->manager->unregisterMercuryCallback(this->reqSeqNum);
  32. this->manager->freeAudioKeyCallback();
  33. pb_release(Track_fields, this->trackInfo);
  34. pb_release(Episode_fields, this->episodeInfo);
  35. }
  36. bool SpotifyTrack::countryListContains(std::string countryList, std::string country)
  37. {
  38. for (int x = 0; x < countryList.size(); x += 2)
  39. {
  40. if (countryList.substr(x, 2) == country)
  41. {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. bool SpotifyTrack::canPlayTrack()
  48. {
  49. for (int x = 0; x < trackInfo.restriction_count; x++)
  50. {
  51. if (trackInfo.restriction[x].countries_allowed != nullptr)
  52. {
  53. return countryListContains(std::string(trackInfo.restriction[x].countries_allowed), manager->countryCode);
  54. }
  55. if (trackInfo.restriction[x].countries_forbidden != nullptr)
  56. {
  57. return !countryListContains(std::string(trackInfo.restriction[x].countries_forbidden), manager->countryCode);
  58. }
  59. }
  60. return true;
  61. }
  62. void SpotifyTrack::trackInformationCallback(std::unique_ptr<MercuryResponse> response, uint32_t position_ms, bool isPaused)
  63. {
  64. if (this->fileId.size() != 0)
  65. return;
  66. CSPOT_ASSERT(response->parts.size() > 0, "response->parts.size() must be greater than 0");
  67. pb_release(Track_fields, trackInfo);
  68. pbDecode(trackInfo, Track_fields, response->parts[0]);
  69. CSPOT_LOG(info, "Track name: %s", trackInfo.name);
  70. CSPOT_LOG(info, "Track duration: %d", trackInfo.duration);
  71. CSPOT_LOG(debug, "trackInfo.restriction.size() = %d", trackInfo.restriction_count);
  72. int altIndex = 0;
  73. while (!canPlayTrack())
  74. {
  75. trackInfo.restriction = trackInfo.alternative[altIndex].restriction;
  76. trackInfo.restriction_count = trackInfo.alternative[altIndex].restriction_count;
  77. trackInfo.gid = trackInfo.alternative[altIndex].gid;
  78. trackInfo.file = trackInfo.alternative[altIndex].file;
  79. altIndex++;
  80. CSPOT_LOG(info, "Trying alternative %d", altIndex);
  81. }
  82. auto trackId = pbArrayToVector(trackInfo.gid);
  83. this->fileId = std::vector<uint8_t>();
  84. for (int x = 0; x < trackInfo.file_count; x++)
  85. {
  86. if (trackInfo.file[x].format == configMan->format)
  87. {
  88. this->fileId = pbArrayToVector(trackInfo.file[x].file_id);
  89. break; // If file found stop searching
  90. }
  91. }
  92. if (trackInfoReceived != nullptr)
  93. {
  94. auto imageId = pbArrayToVector(trackInfo.album.cover_group.image[0].file_id);
  95. TrackInfo simpleTrackInfo = {
  96. .name = std::string(trackInfo.name),
  97. .album = std::string(trackInfo.album.name),
  98. .artist = std::string(trackInfo.artist[0].name),
  99. .imageUrl = "https://i.scdn.co/image/" + bytesToHexString(imageId),
  100. .duration = trackInfo.duration,
  101. };
  102. trackInfoReceived(simpleTrackInfo);
  103. }
  104. this->requestAudioKey(this->fileId, trackId, trackInfo.duration, position_ms, isPaused);
  105. }
  106. void SpotifyTrack::episodeInformationCallback(std::unique_ptr<MercuryResponse> response, uint32_t position_ms, bool isPaused)
  107. {
  108. if (this->fileId.size() != 0)
  109. return;
  110. CSPOT_LOG(debug, "Got to episode");
  111. CSPOT_ASSERT(response->parts.size() > 0, "response->parts.size() must be greater than 0");
  112. pb_release(Episode_fields, episodeInfo);
  113. pbDecode(episodeInfo, Episode_fields, response->parts[0]);
  114. CSPOT_LOG(info, "--- Episode name: %s", episodeInfo.name);
  115. this->fileId = std::vector<uint8_t>();
  116. // TODO: option to set file quality
  117. for (int x = 0; x < episodeInfo.audio_count; x++)
  118. {
  119. if (episodeInfo.audio[x].format == AudioFormat_OGG_VORBIS_96)
  120. {
  121. this->fileId = pbArrayToVector(episodeInfo.audio[x].file_id);
  122. break; // If file found stop searching
  123. }
  124. }
  125. if (trackInfoReceived != nullptr)
  126. {
  127. auto imageId = pbArrayToVector(episodeInfo.covers->image[0].file_id);
  128. TrackInfo simpleTrackInfo = {
  129. .name = std::string(episodeInfo.name),
  130. .album = "",
  131. .artist = "",
  132. .imageUrl = "https://i.scdn.co/image/" + bytesToHexString(imageId),
  133. .duration = trackInfo.duration,
  134. };
  135. trackInfoReceived(simpleTrackInfo);
  136. }
  137. this->requestAudioKey(pbArrayToVector(episodeInfo.gid), this->fileId, episodeInfo.duration, position_ms, isPaused);
  138. }
  139. void SpotifyTrack::requestAudioKey(std::vector<uint8_t> fileId, std::vector<uint8_t> trackId, int32_t trackDuration, uint32_t position_ms, bool isPaused)
  140. {
  141. audioKeyCallback audioKeyLambda = [=](bool success, std::vector<uint8_t> res) {
  142. if (success)
  143. {
  144. CSPOT_LOG(info, "Successfully got audio key!");
  145. auto audioKey = std::vector<uint8_t>(res.begin() + 4, res.end());
  146. if (this->fileId.size() > 0)
  147. {
  148. this->audioStream = std::make_unique<ChunkedAudioStream>(this->fileId, audioKey, trackDuration, this->manager, position_ms, isPaused);
  149. loadedTrackCallback();
  150. }
  151. else
  152. {
  153. CSPOT_LOG(error, "Error while fetching audiokey...");
  154. }
  155. }
  156. else
  157. {
  158. auto code = ntohs(extract<uint16_t>(res, 4));
  159. CSPOT_LOG(error, "Error while fetching audiokey, error code: %d", code);
  160. }
  161. };
  162. this->manager->requestAudioKey(trackId, fileId, audioKeyLambda);
  163. }