update cspot

This commit is contained in:
philippe44
2023-10-02 19:06:59 -07:00
parent eb7df4a5e9
commit 719b289659
26 changed files with 156 additions and 146 deletions

View File

@@ -1,3 +1,6 @@
2023-10-02
- update cspot
2023-09-29
- sleep mechanism
- spotify can store credentials so that zeroconf is optional and players are always registered

View File

@@ -1,8 +1,8 @@
#include "AACDecoder.h"
#include <assert.h>
#include <stdlib.h> // for free, malloc
#include <string.h>
#include <assert.h>
#include "e_tmp4audioobjecttype.h"
#include "pvmp4audiodecoder_api.h"

View File

@@ -7,7 +7,8 @@
using namespace bell;
MP3Container::MP3Container(std::istream& istr, const std::byte* headingBytes) : bell::AudioContainer(istr) {
MP3Container::MP3Container(std::istream& istr, const std::byte* headingBytes)
: bell::AudioContainer(istr) {
if (headingBytes != nullptr) {
memcpy(buffer.data(), headingBytes, 7);
bytesInBuffer = 7;
@@ -38,7 +39,6 @@ std::byte* MP3Container::readSample(uint32_t& len) {
bytesInBuffer -= toConsume;
}
if (!this->fillBuffer()) {
len = 0;
return nullptr;

View File

@@ -34,11 +34,11 @@ int mg_buf::sync() {
return 0;
}
MGStreamAdapter::MGStreamAdapter(struct mg_connection* _conn) : std::ostream(&buf), buf(_conn) {
MGStreamAdapter::MGStreamAdapter(struct mg_connection* _conn)
: std::ostream(&buf), buf(_conn) {
rdbuf(&buf); // set the custom streambuf
}
mg_read_buf::mg_read_buf(struct mg_connection* _conn) : conn(_conn) {
setg(buffer + BUF_SIZE, // beginning of putback area
buffer + BUF_SIZE, // read position
@@ -72,6 +72,7 @@ mg_read_buf::int_type mg_read_buf::underflow() {
return traits_type::to_int_type(*gptr());
}
MGInputStreamAdapter::MGInputStreamAdapter(struct mg_connection* _conn) : std::istream(&buf), buf(_conn) {
MGInputStreamAdapter::MGInputStreamAdapter(struct mg_connection* _conn)
: std::istream(&buf), buf(_conn) {
rdbuf(&buf); // set the custom streambuf
}

View File

@@ -1,8 +1,8 @@
#pragma once
#include <cstring>
#include <iostream>
#include <ostream>
#include <cstring>
#include "civetweb.h"
const size_t BUF_SIZE = 1024;
@@ -57,4 +57,3 @@ private:
public:
MGInputStreamAdapter(struct mg_connection* _conn);
};

View File

@@ -145,9 +145,9 @@ std::vector<uint8_t> CryptoMbedTLS::pbkdf2HmacSha1(
// Free sha context
mbedtls_md_free(&sha1Context);
#else
mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, password.data(), password.size(),
salt.data(), salt.size(), iterations, digestSize,
digest.data());
mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA1, password.data(),
password.size(), salt.data(), salt.size(),
iterations, digestSize, digest.data());
#endif
return digest;

View File

@@ -3,12 +3,12 @@
#include <stdint.h>
#include <memory>
#include "Crypto.h"
#include "LoginBlob.h"
#include "MercurySession.h"
#include "TimeProvider.h"
#include "Crypto.h"
#include "protobuf/metadata.pb.h"
#include "protobuf/authentication.pb.h" // for AuthenticationType_AUTHE...
#include "protobuf/metadata.pb.h"
#ifdef BELL_ONLY_CJSON
#include "cJSON.h"
#else
@@ -38,8 +38,11 @@ struct Context {
std::string getCredentialsJson() {
#ifdef BELL_ONLY_CJSON
cJSON* json_obj = cJSON_CreateObject();
cJSON_AddStringToObject(json_obj, "authData", Crypto::base64Encode(config.authData).c_str());
cJSON_AddNumberToObject(json_obj, "authType", AuthenticationType_AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS);
cJSON_AddStringToObject(json_obj, "authData",
Crypto::base64Encode(config.authData).c_str());
cJSON_AddNumberToObject(
json_obj, "authType",
AuthenticationType_AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS);
cJSON_AddStringToObject(json_obj, "username", config.username.c_str());
char* str = cJSON_PrintUnformatted(json_obj);
@@ -51,7 +54,8 @@ struct Context {
#else
nlohmann::json obj;
obj["authData"] = Crypto::base64Encode(config.authData);
obj["authType"] = AuthenticationType_AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS;
obj["authType"] =
AuthenticationType_AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS;
obj["username"] = config.username;
return obj.dump();

View File

@@ -33,7 +33,8 @@ class TrackPlayer : bell::Task {
public:
// Callback types
typedef std::function<void(std::shared_ptr<QueuedTrack>)> TrackLoadedCallback;
typedef std::function<size_t(uint8_t*, size_t, std::string_view)> DataCallback;
typedef std::function<size_t(uint8_t*, size_t, std::string_view)>
DataCallback;
typedef std::function<void()> EOFCallback;
typedef std::function<bool()> isAiringCallback;

View File

@@ -3,8 +3,8 @@
#include <stddef.h> // for size_t
#include <atomic>
#include <deque>
#include <mutex>
#include <functional>
#include <mutex>
#include "BellTask.h"
#include "PlaybackState.h"
@@ -94,7 +94,6 @@ class TrackQueue : public bell::Task {
std::shared_ptr<bell::WrappedSemaphore> playableSemaphore;
std::atomic<bool> notifyPending = false;
void runTask() override;
void stopTask();

View File

@@ -1,9 +1,9 @@
#pragma once
#include <pb_encode.h>
#include <optional>
#include <string_view>
#include <vector>
#include <optional>
#include "NanoPBHelper.h"
#include "pb_decode.h"
#include "protobuf/spirc.pb.h"

View File

@@ -142,7 +142,8 @@ void LoginBlob::loadJson(const std::string& json) {
cJSON* root = cJSON_Parse(json.c_str());
this->authType = cJSON_GetObjectItem(root, "authType")->valueint;
this->username = cJSON_GetObjectItem(root, "username")->valuestring;
std::string authDataObject = cJSON_GetObjectItem(root, "authData")->valuestring;
std::string authDataObject =
cJSON_GetObjectItem(root, "authData")->valuestring;
this->authData = crypto->base64Decode(authDataObject);
cJSON_Delete(root);
#else

View File

@@ -2,14 +2,14 @@
#ifndef _WIN32
#include <netdb.h> // for addrinfo, freeaddrinfo, getaddrinfo
#include <netdb.h>
#include <netinet/in.h> // for IPPROTO_IP, IPPROTO_TCP
#include <netinet/tcp.h> // for TCP_NODELAY
#include <sys/errno.h> // for EAGAIN, EINTR, ETIMEDOUT, errno
#include <sys/socket.h> // for setsockopt, connect, recv, send, shutdown
#include <sys/time.h> // for timeval
#include <cstring> // for memset
#include <stdexcept> // for runtime_error
#include <netinet/tcp.h> // for TCP_NODELAY
#include <netdb.h>
#else
#include <ws2tcpip.h>
#endif

View File

@@ -17,8 +17,8 @@
#include "PlainConnection.h" // for PlainConnection, timeoutCallback
#include "ShannonConnection.h" // for ShannonConnection
#include "pb_decode.h"
#include "NanoPBHelper.h" // for pbPutString, pbEncode, pbDecode
#include "pb_decode.h"
#include "protobuf/authentication.pb.h"
using random_bytes_engine =
@@ -86,10 +86,9 @@ std::vector<uint8_t> Session::authenticate(std::shared_ptr<LoginBlob> blob) {
APWelcome welcome;
CSPOT_LOG(debug, "Authorization successful");
pbDecode(welcome, APWelcome_fields, packet.data);
return std::vector<uint8_t>(
welcome.reusable_auth_credentials.bytes,
welcome.reusable_auth_credentials.bytes + welcome.reusable_auth_credentials.size
);
return std::vector<uint8_t>(welcome.reusable_auth_credentials.bytes,
welcome.reusable_auth_credentials.bytes +
welcome.reusable_auth_credentials.size);
break;
}
case AUTH_DECLINED_COMMAND: {

View File

@@ -111,7 +111,7 @@ void SpircHandler::updatePositionMs(uint32_t position) {
void SpircHandler::disconnect() {
this->trackQueue->stopTask();
this->trackPlayer->resetState();
this->trackPlayer->stop();
this->ctx->session->disconnect();
}

View File

@@ -13,8 +13,10 @@
#include "WrappedSemaphore.h" // for WrappedSemaphore
#ifdef BELL_VORBIS_FLOAT
#define VORBIS_SEEK(file, position) (ov_time_seek(file, (double)position / 1000))
#define VORBIS_READ(file, buffer, bufferSize, section) (ov_read(file, buffer, bufferSize, 0, 2, 1, section))
#define VORBIS_SEEK(file, position) \
(ov_time_seek(file, (double)position / 1000))
#define VORBIS_READ(file, buffer, bufferSize, section) \
(ov_read(file, buffer, bufferSize, 0, 2, 1, section))
#else
#define VORBIS_SEEK(file, position) (ov_time_seek(file, position))
#define VORBIS_READ(file, buffer, bufferSize, section) \
@@ -68,6 +70,7 @@ TrackPlayer::TrackPlayer(std::shared_ptr<cspot::Context> ctx,
TrackPlayer::~TrackPlayer() {
isRunning = false;
resetState();
std::scoped_lock lock(runningMutex);
}
@@ -233,8 +236,8 @@ void TrackPlayer::runTask() {
if (!currentSongPlaying || pendingReset)
break;
written =
dataCallback(pcmBuffer.data() + (ret - toWrite), toWrite, track->identifier);
written = dataCallback(pcmBuffer.data() + (ret - toWrite),
toWrite, track->identifier);
}
if (written == 0) {
BELL_SLEEP_MS(50);

View File

@@ -1,12 +1,12 @@
#include "Utils.h"
#include <stdlib.h> // for strtol
#include <chrono>
#include <iomanip> // for operator<<, setfill, setw
#include <iostream> // for basic_ostream, hex
#include <sstream> // for stringstream
#include <string> // for string
#include <type_traits> // for enable_if<>::type
#include <chrono>
#ifndef _WIN32
#include <arpa/inet.h>
#endif