airplay artwork and CSpot leak fix (temporary)

This commit is contained in:
Philippe G
2022-01-09 19:40:18 -08:00
parent e9da432bfc
commit 3125a095fa
43 changed files with 396 additions and 271 deletions

View File

@@ -11,7 +11,7 @@ std::map<MercuryType, std::string> MercuryTypeMap({
MercuryManager::MercuryManager(std::unique_ptr<Session> session): bell::Task("mercuryManager", 6 * 1024, 2, 1)
{
tempMercuryHeader = Header_init_default;
tempMercuryHeader = {};
this->timeProvider = std::make_shared<TimeProvider>();
this->callbacks = std::map<uint64_t, mercuryCallback>();
this->subscriptions = std::map<std::string, mercuryCallback>();
@@ -294,8 +294,11 @@ uint64_t MercuryManager::execute(MercuryType method, std::string uri, mercuryCal
// Construct mercury header
CSPOT_LOG(debug, "executing MercuryType %s", MercuryTypeMap[method].c_str());
tempMercuryHeader.uri = (char *)(uri.c_str());
tempMercuryHeader.method = (char *)(MercuryTypeMap[method].c_str());
pbPutString(uri, tempMercuryHeader.uri);
pbPutString(MercuryTypeMap[method], tempMercuryHeader.method);
tempMercuryHeader.has_method = true;
tempMercuryHeader.has_uri = true;
// GET and SEND are actually the same. Therefore the override
// The difference between them is only in header's method

View File

@@ -3,13 +3,12 @@
MercuryResponse::MercuryResponse(std::vector<uint8_t> &data)
{
// this->mercuryHeader = std::make_unique<Header>();
this->mercuryHeader = Header_init_default;
this->mercuryHeader = {};
this->parts = mercuryParts(0);
this->parseResponse(data);
}
MercuryResponse::~MercuryResponse() {
pb_release(Header_fields, &mercuryHeader);
}
void MercuryResponse::parseResponse(std::vector<uint8_t> &data)

View File

@@ -1,6 +1,6 @@
#include "Packet.h"
Packet::Packet(uint8_t command, std::vector<uint8_t> &data) {
Packet::Packet(uint8_t command, const std::vector<uint8_t> &data) {
this->command = command;
this->data = data;
};

View File

@@ -24,7 +24,8 @@ PlayerState::PlayerState(std::shared_ptr<TimeProvider> timeProvider)
innerFrame.state.repeat = false;
innerFrame.state.has_repeat = true;
innerFrame.device_state.sw_version = (char*) swVersion;
innerFrame.device_state.has_sw_version = true;
pbPutCharArray(swVersion, innerFrame.device_state.sw_version);
innerFrame.device_state.is_active = false;
innerFrame.device_state.has_is_active = true;
@@ -35,7 +36,8 @@ PlayerState::PlayerState(std::shared_ptr<TimeProvider> timeProvider)
innerFrame.device_state.volume = configMan->volume;
innerFrame.device_state.has_volume = true;
innerFrame.device_state.name = (char*) configMan->deviceName.c_str();
innerFrame.device_state.has_name = true;
pbPutString(configMan->deviceName, innerFrame.device_state.name);
// Prepare player's capabilities
addCapability(CapabilityType_kCanBePlayer, 1);
@@ -137,7 +139,8 @@ void PlayerState::updatePositionMs(uint32_t position)
void PlayerState::updateTracks()
{
CSPOT_LOG(info, "---- Track count %d", remoteFrame.state.track_count);
innerFrame.state.context_uri = remoteFrame.state.context_uri == nullptr ? nullptr : strdup(remoteFrame.state.context_uri);
strcpy(innerFrame.state.context_uri, remoteFrame.state.context_uri);
std::copy(std::begin(remoteFrame.state.track), std::end(remoteFrame.state.track), std::begin(innerFrame.state.track));
innerFrame.state.track_count = remoteFrame.state.track_count;
innerFrame.state.has_playing_track_index = true;
@@ -198,9 +201,14 @@ std::vector<uint8_t> PlayerState::encodeCurrentFrame(MessageType typ)
{
// Prepare current frame info
innerFrame.version = 1;
innerFrame.ident = (char *) deviceId;
pbPutCharArray(deviceId, innerFrame.ident);
innerFrame.has_ident = true;
innerFrame.seq_nr = this->seqNum;
innerFrame.protocol_version = (char*) protocolVersion;
pbPutCharArray(protocolVersion, innerFrame.protocol_version);
innerFrame.has_protocol_version = true;
innerFrame.typ = typ;
innerFrame.state_update_id = timeProvider->getSyncedTimestamp();
innerFrame.has_version = true;

View File

@@ -49,17 +49,26 @@ std::vector<uint8_t> Session::authenticate(std::shared_ptr<LoginBlob> blob)
authBlob = blob;
// prepare authentication request proto
authRequest.login_credentials.username = (char *)(blob->username.c_str());
authRequest.login_credentials.auth_data = vectorToPbArray(blob->authData);
pbPutString(blob->username, authRequest.login_credentials.username);
std::copy(blob->authData.begin(), blob->authData.end(), authRequest.login_credentials.auth_data.bytes);
authRequest.login_credentials.auth_data.size = blob->authData.size();
authRequest.login_credentials.typ = (AuthenticationType) blob->authType;
authRequest.system_info.cpu_family = CpuFamily_CPU_UNKNOWN;
authRequest.system_info.os = Os_OS_UNKNOWN;
authRequest.system_info.system_information_string = (char *)informationString;
authRequest.system_info.device_id = (char *)deviceId;
authRequest.version_string = (char *)versionString;
auto infoStr = std::string(informationString);
pbPutString(infoStr, authRequest.system_info.system_information_string);
auto deviceIdStr = std::string(deviceId);
pbPutString(deviceId, authRequest.system_info.device_id);
auto versionStr = std::string(versionString);
pbPutString(versionStr, authRequest.version_string);
authRequest.has_version_string = true;
auto data = pbEncode(ClientResponseEncrypted_fields, &authRequest);
free(authRequest.login_credentials.auth_data);
// Send login request
this->shanConn->sendPacket(LOGIN_REQUEST_COMMAND, data);

View File

@@ -10,8 +10,8 @@ SpotifyTrack::SpotifyTrack(std::shared_ptr<MercuryManager> manager, std::shared_
{
this->manager = manager;
this->fileId = std::vector<uint8_t>();
episodeInfo = Episode_init_default;
trackInfo = Track_init_default;
episodeInfo = {};
trackInfo = {};
mercuryCallback trackResponseLambda = [=](std::unique_ptr<MercuryResponse> res) {
this->trackInformationCallback(std::move(res), position_ms, isPaused);
@@ -53,18 +53,18 @@ bool SpotifyTrack::countryListContains(std::string countryList, std::string coun
bool SpotifyTrack::canPlayTrack()
{
for (int x = 0; x < trackInfo.restriction_count; x++)
{
if (trackInfo.restriction[x].countries_allowed != nullptr)
{
return countryListContains(std::string(trackInfo.restriction[x].countries_allowed), manager->countryCode);
}
if (trackInfo.restriction[x].countries_forbidden != nullptr)
{
return !countryListContains(std::string(trackInfo.restriction[x].countries_forbidden), manager->countryCode);
}
}
// for (int x = 0; x < trackInfo.restriction_count; x++)
// {
// if (strlen(trackInfo.restriction[x].countries_allowed) > 0)
// {
// return countryListContains(std::string(trackInfo.restriction[x].countries_allowed), manager->countryCode);
// }
//
// if (strlen(trackInfo.restriction[x].countries_forbidden) > 0)
// {
// return !countryListContains(std::string(trackInfo.restriction[x].countries_forbidden), manager->countryCode);
// }
// }
return true;
}
@@ -77,35 +77,39 @@ void SpotifyTrack::trackInformationCallback(std::unique_ptr<MercuryResponse> res
pb_release(Track_fields, &trackInfo);
pbDecode(trackInfo, Track_fields, response->parts[0]);
//
// CSPOT_LOG(info, "Track name: %s", trackInfo.name);
// CSPOT_LOG(info, "Track duration: %d", trackInfo.duration);
// CSPOT_LOG(debug, "trackInfo.restriction.size() = %d", trackInfo.restriction_count);
// int altIndex = 0;
// while (!canPlayTrack())
// {
// auto src = trackInfo.alternative[altIndex].restriction;
//// std::copy(std::begin(src), std::end(src), std::begin(trackInfo.restriction));
//// trackInfo.restriction_count = trackInfo.alternative[altIndex].restriction_count;
////
//// free(trackInfo.gid);
// trackInfo.gid = trackInfo.alternative[altIndex].gid;
// altIndex++;
// CSPOT_LOG(info, "Trying alternative %d", altIndex);
// }
CSPOT_LOG(info, "Track name: %s", trackInfo.name);
CSPOT_LOG(info, "Track duration: %d", trackInfo.duration);
CSPOT_LOG(debug, "trackInfo.restriction.size() = %d", trackInfo.restriction_count);
int altIndex = 0;
while (!canPlayTrack())
{
trackInfo.restriction = trackInfo.alternative[altIndex].restriction;
trackInfo.restriction_count = trackInfo.alternative[altIndex].restriction_count;
trackInfo.gid = trackInfo.alternative[altIndex].gid;
trackInfo.file = trackInfo.alternative[altIndex].file;
altIndex++;
CSPOT_LOG(info, "Trying alternative %d", altIndex);
}
auto trackId = pbArrayToVector(trackInfo.gid);
auto trackId = std::vector(trackInfo.gid.bytes, trackInfo.gid.bytes + trackInfo.gid.size);
this->fileId = std::vector<uint8_t>();
for (int x = 0; x < trackInfo.file_count; x++)
{
if (trackInfo.file[x].format == configMan->format)
{
this->fileId = pbArrayToVector(trackInfo.file[x].file_id);
this->fileId = std::vector(trackInfo.file[x].file_id.bytes, trackInfo.file[x].file_id.bytes + trackInfo.file[x].file_id.size);
break; // If file found stop searching
}
}
if (trackInfoReceived != nullptr)
{
auto imageId = pbArrayToVector(trackInfo.album.cover_group.image[0].file_id);
auto imageIdBytes = trackInfo.album.cover_group.image[0].file_id;
auto imageId = std::vector(imageIdBytes.bytes, imageIdBytes.bytes + imageIdBytes.size);
TrackInfo simpleTrackInfo = {
.name = std::string(trackInfo.name),
.album = std::string(trackInfo.album.name),
@@ -139,14 +143,15 @@ void SpotifyTrack::episodeInformationCallback(std::unique_ptr<MercuryResponse> r
{
if (episodeInfo.audio[x].format == AudioFormat_OGG_VORBIS_96)
{
this->fileId = pbArrayToVector(episodeInfo.audio[x].file_id);
this->fileId = std::vector(episodeInfo.audio[x].file_id.bytes, episodeInfo.audio[x].file_id.bytes + episodeInfo.audio[x].file_id.size);
break; // If file found stop searching
}
}
if (trackInfoReceived != nullptr)
{
auto imageId = pbArrayToVector(episodeInfo.covers->image[0].file_id);
auto imageFileId = episodeInfo.covers.image[0].file_id;
auto imageId = std::vector(imageFileId.bytes, imageFileId.bytes + imageFileId.size);
TrackInfo simpleTrackInfo = {
.name = std::string(episodeInfo.name),
.album = "",
@@ -159,7 +164,7 @@ void SpotifyTrack::episodeInformationCallback(std::unique_ptr<MercuryResponse> r
trackInfoReceived(simpleTrackInfo);
}
this->requestAudioKey(pbArrayToVector(episodeInfo.gid), this->fileId, episodeInfo.duration, position_ms, isPaused);
this->requestAudioKey(std::vector(episodeInfo.gid.bytes, episodeInfo.gid.bytes + episodeInfo.gid.size), this->fileId, episodeInfo.duration, position_ms, isPaused);
}
void SpotifyTrack::requestAudioKey(std::vector<uint8_t> fileId, std::vector<uint8_t> trackId, int32_t trackDuration, uint32_t position_ms, bool isPaused)

View File

@@ -3,11 +3,11 @@
TrackReference::TrackReference(TrackRef *ref)
{
if (ref->gid != nullptr)
if (ref->gid.size > 0)
{
gid = pbArrayToVector(ref->gid);
gid = std::vector(ref->gid.bytes, ref->gid.bytes + ref->gid.size);
}
else if (ref->uri != nullptr)
else if (strlen(ref->uri) > 0)
{
auto uri = std::string(ref->uri);
auto idString = uri.substr(uri.find_last_of(":") + 1, uri.size());