mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-11 22:17:17 +03:00
big merge
This commit is contained in:
102
components/spotify/cspot/bell/include/BaseHTTPServer.h
Normal file
102
components/spotify/cspot/bell/include/BaseHTTPServer.h
Normal file
@@ -0,0 +1,102 @@
|
||||
#ifndef BELL_BASE_HTTP_SERV
|
||||
#define BELL_BASE_HTTP_SERV
|
||||
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
namespace bell {
|
||||
|
||||
class ResponseReader {
|
||||
public:
|
||||
ResponseReader(){};
|
||||
virtual ~ResponseReader() = default;
|
||||
|
||||
virtual size_t getTotalSize() = 0;
|
||||
virtual size_t read(char *buffer, size_t size) = 0;
|
||||
};
|
||||
|
||||
class FileResponseReader : public ResponseReader {
|
||||
public:
|
||||
FILE *file;
|
||||
size_t fileSize;
|
||||
FileResponseReader(std::string fileName) {
|
||||
file = fopen(fileName.c_str(), "r");
|
||||
fseek(file, 0, SEEK_END); // seek to end of file
|
||||
fileSize = ftell(file); // get current file pointer
|
||||
fseek(file, 0, SEEK_SET); // seek back to beginning of file
|
||||
};
|
||||
~FileResponseReader() { fclose(file); };
|
||||
|
||||
size_t read(char *buffer, size_t size) {
|
||||
return fread(buffer, 1, size, file);
|
||||
}
|
||||
|
||||
size_t getTotalSize() { return fileSize; }
|
||||
};
|
||||
|
||||
enum class RequestType { GET, POST };
|
||||
|
||||
struct HTTPRequest {
|
||||
std::map<std::string, std::string> urlParams;
|
||||
std::map<std::string, std::string> queryParams;
|
||||
std::string body;
|
||||
int handlerId;
|
||||
int connection;
|
||||
};
|
||||
|
||||
struct HTTPResponse {
|
||||
int connectionFd;
|
||||
int status;
|
||||
bool useGzip = false;
|
||||
std::string body;
|
||||
std::string contentType;
|
||||
std::unique_ptr<ResponseReader> responseReader;
|
||||
};
|
||||
|
||||
typedef std::function<void(HTTPRequest &)> httpHandler;
|
||||
struct HTTPRoute {
|
||||
RequestType requestType;
|
||||
httpHandler handler;
|
||||
};
|
||||
|
||||
struct HTTPConnection {
|
||||
std::vector<uint8_t> buffer;
|
||||
std::string currentLine = "";
|
||||
int contentLength = 0;
|
||||
bool isReadingBody = false;
|
||||
std::string httpMethod;
|
||||
bool toBeClosed = false;
|
||||
bool isEventConnection = false;
|
||||
};
|
||||
|
||||
class BaseHTTPServer {
|
||||
public:
|
||||
BaseHTTPServer() {};
|
||||
virtual ~BaseHTTPServer() = default;
|
||||
|
||||
/**
|
||||
* Should contain server's bind port
|
||||
*/
|
||||
int serverPort;
|
||||
|
||||
/**
|
||||
* Called when handler is being registered on the http server
|
||||
*
|
||||
* @param requestType GET or POST
|
||||
* @param endpoint registering under
|
||||
* httpHandler lambda to be called when given endpoint gets executed
|
||||
*/
|
||||
virtual void registerHandler(RequestType requestType, const std::string & endpoint,
|
||||
httpHandler) = 0;
|
||||
|
||||
/**
|
||||
* Writes given response to a fd
|
||||
*/
|
||||
virtual void respond(const HTTPResponse &) = 0;
|
||||
};
|
||||
} // namespace bell
|
||||
|
||||
#endif
|
||||
117
components/spotify/cspot/bell/include/BellLogger.h
Normal file
117
components/spotify/cspot/bell/include/BellLogger.h
Normal file
@@ -0,0 +1,117 @@
|
||||
#ifndef BELL_LOGGER_H
|
||||
#define BELL_LOGGER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace bell
|
||||
{
|
||||
|
||||
class AbstractLogger
|
||||
{
|
||||
public:
|
||||
bool enableSubmodule = false;
|
||||
virtual void debug(std::string filename, int line, std::string submodule, const char *format, ...) = 0;
|
||||
virtual void error(std::string filename, int line, std::string submodule, const char *format, ...) = 0;
|
||||
virtual void info(std::string filename, int line, std::string submodule, const char *format, ...) = 0;
|
||||
};
|
||||
|
||||
extern std::shared_ptr<bell::AbstractLogger> bellGlobalLogger;
|
||||
class BellLogger : public bell::AbstractLogger
|
||||
{
|
||||
public:
|
||||
// static bool enableColors = true;
|
||||
void debug(std::string filename, int line, std::string submodule, const char *format, ...)
|
||||
{
|
||||
|
||||
printf(colorRed);
|
||||
printf("D ");
|
||||
if (enableSubmodule) {
|
||||
printf(colorReset);
|
||||
printf("[%s] ", submodule.c_str());
|
||||
}
|
||||
printFilename(filename);
|
||||
printf(":%d: ", line);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args);
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
};
|
||||
|
||||
void error(std::string filename, int line, std::string submodule, const char *format, ...)
|
||||
{
|
||||
|
||||
printf(colorRed);
|
||||
printf("E ");
|
||||
if (enableSubmodule) {
|
||||
printf(colorReset);
|
||||
printf("[%s] ", submodule.c_str());
|
||||
}
|
||||
printFilename(filename);
|
||||
printf(":%d: ", line);
|
||||
printf(colorRed);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args);
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
};
|
||||
|
||||
void info(std::string filename, int line, std::string submodule, const char *format, ...)
|
||||
{
|
||||
|
||||
printf(colorBlue);
|
||||
printf("I ");
|
||||
if (enableSubmodule) {
|
||||
printf(colorReset);
|
||||
printf("[%s] ", submodule.c_str());
|
||||
}
|
||||
printFilename(filename);
|
||||
printf(":%d: ", line);
|
||||
printf(colorReset);
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args);
|
||||
va_end(args);
|
||||
printf("\n");
|
||||
};
|
||||
|
||||
void printFilename(std::string filename)
|
||||
{
|
||||
std::string basenameStr(filename.substr(filename.rfind("/") + 1));
|
||||
unsigned long hash = 5381;
|
||||
int c;
|
||||
|
||||
for (char const &c : basenameStr)
|
||||
{
|
||||
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
||||
}
|
||||
|
||||
printf("\e[0;%dm", allColors[hash % NColors]);
|
||||
|
||||
printf("%s", basenameStr.c_str());
|
||||
printf(colorReset);
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr const char *colorReset = "\e[0m";
|
||||
static constexpr const char *colorRed = "\e[0;31m";
|
||||
static constexpr const char *colorBlue = "\e[0;34m";
|
||||
static constexpr const int NColors = 15;
|
||||
static constexpr int allColors[NColors] = {31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97};
|
||||
};
|
||||
|
||||
void setDefaultLogger();
|
||||
void enableSubmoduleLogging();
|
||||
}
|
||||
|
||||
#define BELL_LOG(type, ...) \
|
||||
do \
|
||||
{ \
|
||||
bell::bellGlobalLogger->type(__FILE__, __LINE__, __VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
19
components/spotify/cspot/bell/include/BellSocket.h
Normal file
19
components/spotify/cspot/bell/include/BellSocket.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef BELL_SOCKET_H
|
||||
#define BELL_SOCKET_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace bell {
|
||||
class Socket {
|
||||
public:
|
||||
Socket() {};
|
||||
virtual ~Socket() = default;
|
||||
|
||||
virtual void open(std::string url) = 0;
|
||||
virtual size_t write(uint8_t* buf, size_t len) = 0;
|
||||
virtual size_t read(uint8_t* buf, size_t len) = 0;
|
||||
virtual void close() = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
27
components/spotify/cspot/bell/include/BellUtils.h
Normal file
27
components/spotify/cspot/bell/include/BellUtils.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef EUPHONIUM_BELL_UTILS
|
||||
#define EUPHONIUM_BELL_UTILS
|
||||
|
||||
#include <random>
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
|
||||
namespace bell {
|
||||
|
||||
std::string generateRandomUUID();
|
||||
|
||||
} // namespace bell
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <freertos/FreeRTOS.h>
|
||||
|
||||
#define BELL_SLEEP_MS(ms) vTaskDelay(ms / portTICK_PERIOD_MS)
|
||||
#define BELL_YIELD() vTaskYield()
|
||||
|
||||
#else
|
||||
#include <unistd.h>
|
||||
|
||||
#define BELL_SLEEP_MS(ms) usleep(ms * 1000)
|
||||
#define BELL_YIELD() ()
|
||||
|
||||
#endif
|
||||
#endif
|
||||
34
components/spotify/cspot/bell/include/BinaryReader.h
Normal file
34
components/spotify/cspot/bell/include/BinaryReader.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef BELL_BINARY_READER_H
|
||||
#define BELL_BINARY_READER_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include "ByteStream.h"
|
||||
|
||||
namespace bell
|
||||
{
|
||||
class BinaryReader
|
||||
{
|
||||
std::shared_ptr<ByteStream> stream;
|
||||
size_t currentPos = 0;
|
||||
|
||||
public:
|
||||
BinaryReader(std::shared_ptr<ByteStream> stream);
|
||||
int32_t readInt();
|
||||
int16_t readShort();
|
||||
uint32_t readUInt();
|
||||
long long readLong();
|
||||
void close();
|
||||
uint8_t readByte();
|
||||
size_t size();
|
||||
size_t position();
|
||||
std::vector<uint8_t> readBytes(size_t);
|
||||
void skip(size_t);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
128
components/spotify/cspot/bell/include/BiquadFilter.h
Normal file
128
components/spotify/cspot/bell/include/BiquadFilter.h
Normal file
@@ -0,0 +1,128 @@
|
||||
#ifndef BELL_BIQUADFILTER_H
|
||||
#define BELL_BIQUADFILTER_H
|
||||
|
||||
#include <mutex>
|
||||
#include <cmath>
|
||||
#include "esp_platform.h"
|
||||
|
||||
extern "C" int dsps_biquad_f32_ae32(const float* input, float* output, int len, float* coef, float* w);
|
||||
|
||||
class BiquadFilter
|
||||
{
|
||||
private:
|
||||
std::mutex processMutex;
|
||||
float coeffs[5];
|
||||
float w[2];
|
||||
|
||||
public:
|
||||
BiquadFilter(){};
|
||||
|
||||
void generateHighShelfCoEffs(float f, float gain, float q)
|
||||
{
|
||||
if (q <= 0.0001)
|
||||
{
|
||||
q = 0.0001;
|
||||
}
|
||||
float Fs = 1;
|
||||
|
||||
float A = sqrtf(pow(10, (double)gain / 20.0));
|
||||
float w0 = 2 * M_PI * f / Fs;
|
||||
float c = cosf(w0);
|
||||
float s = sinf(w0);
|
||||
float alpha = s / (2 * q);
|
||||
|
||||
float b0 = A * ((A + 1) + (A - 1) * c + 2 * sqrtf(A) * alpha);
|
||||
float b1 = -2 * A * ((A - 1) + (A + 1) * c);
|
||||
float b2 = A * ((A + 1) + (A - 1) * c - 2 * sqrtf(A) * alpha);
|
||||
float a0 = (A + 1) - (A - 1) * c + 2 * sqrtf(A) * alpha;
|
||||
float a1 = 2 * ((A - 1) - (A + 1) * c);
|
||||
float a2 = (A + 1) - (A - 1) * c - 2 * sqrtf(A) * alpha;
|
||||
|
||||
std::lock_guard lock(processMutex);
|
||||
coeffs[0] = b0 / a0;
|
||||
coeffs[1] = b1 / a0;
|
||||
coeffs[2] = b2 / a0;
|
||||
coeffs[3] = a1 / a0;
|
||||
coeffs[4] = a2 / a0;
|
||||
}
|
||||
|
||||
// Generates coefficients for a low shelf biquad filter
|
||||
void generateLowShelfCoEffs(float f, float gain, float q)
|
||||
{
|
||||
if (q <= 0.0001)
|
||||
{
|
||||
q = 0.0001;
|
||||
}
|
||||
float Fs = 1;
|
||||
|
||||
float A = sqrtf(pow(10, (double)gain / 20.0));
|
||||
float w0 = 2 * M_PI * f / Fs;
|
||||
float c = cosf(w0);
|
||||
float s = sinf(w0);
|
||||
float alpha = s / (2 * q);
|
||||
|
||||
float b0 = A * ((A + 1) - (A - 1) * c + 2 * sqrtf(A) * alpha);
|
||||
float b1 = 2 * A * ((A - 1) - (A + 1) * c);
|
||||
float b2 = A * ((A + 1) - (A - 1) * c - 2 * sqrtf(A) * alpha);
|
||||
float a0 = (A + 1) + (A - 1) * c + 2 * sqrtf(A) * alpha;
|
||||
float a1 = -2 * ((A - 1) + (A + 1) * c);
|
||||
float a2 = (A + 1) + (A - 1) * c - 2 * sqrtf(A) * alpha;
|
||||
|
||||
std::lock_guard lock(processMutex);
|
||||
coeffs[0] = b0 / a0;
|
||||
coeffs[1] = b1 / a0;
|
||||
coeffs[2] = b2 / a0;
|
||||
coeffs[3] = a1 / a0;
|
||||
coeffs[4] = a2 / a0;
|
||||
}
|
||||
|
||||
// Generates coefficients for a notch biquad filter
|
||||
void generateNotchCoEffs(float f, float gain, float q)
|
||||
{
|
||||
if (q <= 0.0001)
|
||||
{
|
||||
q = 0.0001;
|
||||
}
|
||||
float Fs = 1;
|
||||
|
||||
float A = sqrtf(pow(10, (double)gain / 20.0));
|
||||
float w0 = 2 * M_PI * f / Fs;
|
||||
float c = cosf(w0);
|
||||
float s = sinf(w0);
|
||||
float alpha = s / (2 * q);
|
||||
|
||||
float b0 = 1 + alpha * A;
|
||||
float b1 = -2 * c;
|
||||
float b2 = 1 - alpha * A;
|
||||
float a0 = 1 + alpha;
|
||||
float a1 = -2 * c;
|
||||
float a2 = 1 - alpha;
|
||||
|
||||
std::scoped_lock lock(processMutex);
|
||||
coeffs[0] = b0 / a0;
|
||||
coeffs[1] = b1 / a0;
|
||||
coeffs[2] = b2 / a0;
|
||||
coeffs[3] = a1 / a0;
|
||||
coeffs[4] = a2 / a0;
|
||||
}
|
||||
|
||||
void processSamples(float *input, int numSamples)
|
||||
{
|
||||
std::scoped_lock lock(processMutex);
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
dsps_biquad_f32_ae32(input, input, numSamples, coeffs, w);
|
||||
#else
|
||||
// Apply the set coefficients
|
||||
for (int i = 0; i < numSamples; i++)
|
||||
{
|
||||
float d0 = input[i] - coeffs[3] * w[0] - coeffs[4] * w[1];
|
||||
input[i] = coeffs[0] * d0 + coeffs[1] * w[0] + coeffs[2] * w[1];
|
||||
w[1] = w[0];
|
||||
w[0] = d0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
26
components/spotify/cspot/bell/include/ByteStream.h
Normal file
26
components/spotify/cspot/bell/include/ByteStream.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef BELL_BYTE_READER_H
|
||||
#define BELL_BYTE_READER_H
|
||||
|
||||
#include "stdlib.h"
|
||||
|
||||
/**
|
||||
* A class for reading bytes from a stream. Further implemented in HTTPStream.h
|
||||
*/
|
||||
namespace bell
|
||||
{
|
||||
class ByteStream
|
||||
{
|
||||
public:
|
||||
ByteStream(){};
|
||||
~ByteStream(){};
|
||||
|
||||
virtual size_t read(uint8_t *buf, size_t nbytes) = 0;
|
||||
virtual size_t skip(size_t nbytes) = 0;
|
||||
|
||||
virtual size_t position() = 0;
|
||||
virtual size_t size() = 0;
|
||||
virtual void close() = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
14
components/spotify/cspot/bell/include/Crypto.h
Normal file
14
components/spotify/cspot/bell/include/Crypto.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef BELL_CRYPTO_H
|
||||
#define BELL_CRYPTO_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#ifdef BELL_USE_MBEDTLS
|
||||
#include "CryptoMbedTLS.h"
|
||||
#define Crypto CryptoMbedTLS
|
||||
#else
|
||||
#include "CryptoOpenSSL.h"
|
||||
#define Crypto CryptoOpenSSL
|
||||
#endif
|
||||
#endif
|
||||
78
components/spotify/cspot/bell/include/CryptoMbedTLS.h
Normal file
78
components/spotify/cspot/bell/include/CryptoMbedTLS.h
Normal file
@@ -0,0 +1,78 @@
|
||||
#ifndef BELL_CRYPTOMBEDTLS_H
|
||||
#define BELL_CRYPTOMBEDTLS_H
|
||||
|
||||
#ifdef BELL_USE_MBEDTLS
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include <mbedtls/base64.h>
|
||||
#include <mbedtls/bignum.h>
|
||||
#include <mbedtls/md.h>
|
||||
#include <mbedtls/aes.h>
|
||||
#include <mbedtls/pkcs5.h>
|
||||
#include <mbedtls/entropy.h>
|
||||
#include <mbedtls/ctr_drbg.h>
|
||||
|
||||
|
||||
#define DH_KEY_SIZE 96
|
||||
|
||||
static unsigned char DHPrime[] = {
|
||||
/* Well-known Group 1, 768-bit prime */
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9,
|
||||
0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6,
|
||||
0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e,
|
||||
0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6,
|
||||
0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e,
|
||||
0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a,
|
||||
0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14,
|
||||
0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45,
|
||||
0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4,
|
||||
0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
};
|
||||
|
||||
static unsigned char DHGenerator[1] = {2};
|
||||
|
||||
class CryptoMbedTLS {
|
||||
private:
|
||||
mbedtls_md_context_t sha1Context;
|
||||
mbedtls_aes_context aesCtx;
|
||||
public:
|
||||
CryptoMbedTLS();
|
||||
~CryptoMbedTLS();
|
||||
// Base64
|
||||
std::vector<uint8_t> base64Decode(const std::string& data);
|
||||
std::string base64Encode(const std::vector<uint8_t>& data);
|
||||
|
||||
// Sha1
|
||||
void sha1Init();
|
||||
void sha1Update(const std::string& s);
|
||||
void sha1Update(const std::vector<uint8_t>& vec);
|
||||
std::string sha1Final();
|
||||
std::vector<uint8_t> sha1FinalBytes();
|
||||
|
||||
// HMAC SHA1
|
||||
std::vector<uint8_t> sha1HMAC(const std::vector<uint8_t>& inputKey, const std::vector<uint8_t>& message);
|
||||
|
||||
// AES CTR
|
||||
void aesCTRXcrypt(const std::vector<uint8_t>& key, std::vector<uint8_t>& iv, std::vector<uint8_t>& data);
|
||||
|
||||
// AES ECB
|
||||
void aesECBdecrypt(const std::vector<uint8_t>& key, std::vector<uint8_t>& data);
|
||||
|
||||
// Diffie Hellman
|
||||
std::vector<uint8_t> publicKey;
|
||||
std::vector<uint8_t> privateKey;
|
||||
void dhInit();
|
||||
std::vector<uint8_t> dhCalculateShared(const std::vector<uint8_t>& remoteKey);
|
||||
|
||||
// PBKDF2
|
||||
std::vector<uint8_t> pbkdf2HmacSha1(const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, int iterations, int digestSize);
|
||||
|
||||
// Random stuff
|
||||
std::vector<uint8_t> generateVectorWithRandomData(size_t length);
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
84
components/spotify/cspot/bell/include/CryptoOpenSSL.h
Normal file
84
components/spotify/cspot/bell/include/CryptoOpenSSL.h
Normal file
@@ -0,0 +1,84 @@
|
||||
#ifndef BELL_CRYPTOOPENSSL_H
|
||||
#define BELL_CRYPTOOPENSSL_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <openssl/engine.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/dh.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/modes.h>
|
||||
|
||||
#define DH_KEY_SIZE 96
|
||||
|
||||
static unsigned char DHPrime[] = {
|
||||
/* Well-known Group 1, 768-bit prime */
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9,
|
||||
0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6,
|
||||
0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e,
|
||||
0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6,
|
||||
0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e,
|
||||
0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a,
|
||||
0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14,
|
||||
0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45,
|
||||
0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4,
|
||||
0x4c, 0x42, 0xe9, 0xa6, 0x3a, 0x36, 0x20, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
||||
};
|
||||
|
||||
static unsigned char DHGenerator[1] = {2};
|
||||
|
||||
class CryptoOpenSSL {
|
||||
private:
|
||||
DH* dhContext = nullptr;
|
||||
SHA_CTX sha1Context;
|
||||
public:
|
||||
CryptoOpenSSL();
|
||||
~CryptoOpenSSL();
|
||||
// Base64
|
||||
std::vector<uint8_t> base64Decode(const std::string& data);
|
||||
std::string base64Encode(const std::vector<uint8_t>& data);
|
||||
|
||||
// Sha1
|
||||
void sha1Init();
|
||||
void sha1Update(const std::string& s);
|
||||
void sha1Update(const std::vector<uint8_t>& vec);
|
||||
|
||||
void connectSSL(std::string url);
|
||||
int readSSL(uint8_t* buf, int len);
|
||||
int writeSSL(uint8_t* buf, int len);
|
||||
void closeSSL();
|
||||
|
||||
std::string sha1Final();
|
||||
std::vector<uint8_t> sha1FinalBytes();
|
||||
|
||||
// HMAC SHA1
|
||||
std::vector<uint8_t> sha1HMAC(const std::vector<uint8_t>& inputKey, const std::vector<uint8_t>& message);
|
||||
|
||||
// AES CTR
|
||||
void aesCTRXcrypt(const std::vector<uint8_t>& key, std::vector<uint8_t>& iv, std::vector<uint8_t>& data);
|
||||
|
||||
// AES ECB
|
||||
void aesECBdecrypt(const std::vector<uint8_t>& key, std::vector<uint8_t>& data);
|
||||
|
||||
// Diffie Hellman
|
||||
std::vector<uint8_t> publicKey;
|
||||
std::vector<uint8_t> privateKey;
|
||||
void dhInit();
|
||||
std::vector<uint8_t> dhCalculateShared(const std::vector<uint8_t>& remoteKey);
|
||||
|
||||
// PBKDF2
|
||||
std::vector<uint8_t> pbkdf2HmacSha1(const std::vector<uint8_t>& password, const std::vector<uint8_t>& salt, int iterations, int digestSize);
|
||||
|
||||
// Random stuff
|
||||
std::vector<uint8_t> generateVectorWithRandomData(size_t length);
|
||||
};
|
||||
|
||||
#endif
|
||||
52
components/spotify/cspot/bell/include/DecoderGlobals.h
Normal file
52
components/spotify/cspot/bell/include/DecoderGlobals.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef BELL_DISABLE_CODECS
|
||||
#ifndef DECODER_GLOBALS_H
|
||||
#define DECODER_GLOBALS_H
|
||||
|
||||
#define AAC_READBUF_SIZE (4 * AAC_MAINBUF_SIZE * AAC_MAX_NCHANS)
|
||||
#define MP3_READBUF_SIZE (2 * 1024);
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <memory>
|
||||
#include "aacdec.h"
|
||||
#include "mp3dec.h"
|
||||
|
||||
namespace bell
|
||||
{
|
||||
class DecodersInstance
|
||||
{
|
||||
public:
|
||||
DecodersInstance(){};
|
||||
~DecodersInstance()
|
||||
{
|
||||
MP3FreeDecoder(mp3Decoder);
|
||||
AACFreeDecoder(aacDecoder);
|
||||
};
|
||||
|
||||
HAACDecoder aacDecoder = NULL;
|
||||
HMP3Decoder mp3Decoder = NULL;
|
||||
|
||||
void ensureAAC()
|
||||
{
|
||||
if (aacDecoder == NULL)
|
||||
{
|
||||
aacDecoder = AACInitDecoder();
|
||||
}
|
||||
}
|
||||
|
||||
void ensureMP3()
|
||||
{
|
||||
if (mp3Decoder == NULL)
|
||||
{
|
||||
mp3Decoder = MP3InitDecoder();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
extern std::shared_ptr<bell::DecodersInstance> decodersInstance;
|
||||
|
||||
void createDecoders();
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
70
components/spotify/cspot/bell/include/HTTPServer.h
Normal file
70
components/spotify/cspot/bell/include/HTTPServer.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef BELL_HTTP_SERVER_H
|
||||
#define BELL_HTTP_SERVER_H
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include <BellLogger.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fstream>
|
||||
#include <sys/socket.h>
|
||||
#include <string>
|
||||
#include <netdb.h>
|
||||
#include <mutex>
|
||||
#include <fcntl.h>
|
||||
#include "BaseHTTPServer.h"
|
||||
|
||||
#ifndef SOCK_NONBLOCK
|
||||
#define SOCK_NONBLOCK O_NONBLOCK
|
||||
#endif
|
||||
|
||||
namespace bell
|
||||
{
|
||||
class HTTPServer : public bell::BaseHTTPServer
|
||||
{
|
||||
private:
|
||||
std::regex routerPattern = std::regex(":([^\\/]+)?");
|
||||
fd_set master;
|
||||
fd_set readFds;
|
||||
fd_set activeFdSet, readFdSet;
|
||||
|
||||
bool isClosed = true;
|
||||
bool writingResponse = false;
|
||||
|
||||
std::map<std::string, std::vector<HTTPRoute>> routes;
|
||||
std::map<int, HTTPConnection> connections;
|
||||
void writeResponse(const HTTPResponse &);
|
||||
void writeResponseEvents(int connFd);
|
||||
void findAndHandleRoute(std::string &, std::string &, int connectionFd);
|
||||
|
||||
std::vector<std::string> splitUrl(const std::string &url, char delimiter);
|
||||
std::mutex responseMutex;
|
||||
std::vector<char> responseBuffer = std::vector<char>(128);
|
||||
|
||||
void readFromClient(int clientFd);
|
||||
std::map<std::string, std::string> parseQueryString(const std::string &queryString);
|
||||
unsigned char h2int(char c);
|
||||
std::string urlDecode(std::string str);
|
||||
|
||||
public:
|
||||
HTTPServer(int serverPort);
|
||||
|
||||
void registerHandler(RequestType requestType, const std::string &, httpHandler);
|
||||
void respond(const HTTPResponse &);
|
||||
void publishEvent(std::string eventName, std::string eventData);
|
||||
void closeConnection(int connection);
|
||||
void listen();
|
||||
};
|
||||
}
|
||||
#endif
|
||||
72
components/spotify/cspot/bell/include/HTTPStream.h
Normal file
72
components/spotify/cspot/bell/include/HTTPStream.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef BELL_HTTP_STREAM_H
|
||||
#define BELL_HTTP_STREAM_H
|
||||
|
||||
#include <string>
|
||||
#include <BellLogger.h>
|
||||
#include <ByteStream.h>
|
||||
#include <BellSocket.h>
|
||||
#include <TCPSocket.h>
|
||||
#include <platform/TLSSocket.h>
|
||||
|
||||
/*
|
||||
* HTTPStream
|
||||
*
|
||||
* A class for reading and writing HTTP streams implementing the ByteStream interface.
|
||||
*
|
||||
*/
|
||||
namespace bell
|
||||
{
|
||||
enum class StreamStatus
|
||||
{
|
||||
OPENING,
|
||||
READING_HEADERS,
|
||||
READING_DATA,
|
||||
CLOSED
|
||||
};
|
||||
|
||||
class HTTPStream : public ByteStream
|
||||
{
|
||||
public:
|
||||
HTTPStream();
|
||||
~HTTPStream();
|
||||
|
||||
std::unique_ptr<bell::Socket> socket;
|
||||
|
||||
|
||||
bool hasFixedSize = false;
|
||||
size_t contentLength = -1;
|
||||
size_t currentPos = -1;
|
||||
|
||||
StreamStatus status = StreamStatus::OPENING;
|
||||
|
||||
/*
|
||||
* opens connection to given url and reads header
|
||||
*
|
||||
* @param url the http url to connect to
|
||||
*/
|
||||
void connectToUrl(std::string url, bool disableSSL = false);
|
||||
|
||||
/*
|
||||
* Reads data from the stream.
|
||||
*
|
||||
* @param buf The buffer to read data into.
|
||||
* @param nbytes The size of the buffer.
|
||||
* @return The number of bytes read.
|
||||
* @throws std::runtime_error if the stream is closed.
|
||||
*/
|
||||
size_t read(uint8_t *buf, size_t nbytes);
|
||||
|
||||
/*
|
||||
* Skips nbytes bytes in the stream.
|
||||
*/
|
||||
size_t skip(size_t nbytes);
|
||||
|
||||
size_t position() { return currentPos; }
|
||||
|
||||
size_t size() { return contentLength; }
|
||||
|
||||
// Closes the connection
|
||||
void close();
|
||||
};
|
||||
}
|
||||
#endif
|
||||
33
components/spotify/cspot/bell/include/JSONObject.h
Normal file
33
components/spotify/cspot/bell/include/JSONObject.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef JSONOBJECT_H
|
||||
#define JSONOBJECT_H
|
||||
#include <cJSON.h>
|
||||
#include <string>
|
||||
|
||||
namespace bell {
|
||||
class JSONValue
|
||||
{
|
||||
public:
|
||||
JSONValue(cJSON* body, std::string key);
|
||||
void operator=(const std::string val);
|
||||
void operator=(const char* val);
|
||||
void operator=(int val);
|
||||
|
||||
private:
|
||||
cJSON* body;
|
||||
std::string key;
|
||||
};
|
||||
|
||||
class JSONObject
|
||||
{
|
||||
public:
|
||||
JSONObject();
|
||||
~JSONObject();
|
||||
JSONValue operator[](std::string index);
|
||||
std::string toString();
|
||||
|
||||
private:
|
||||
cJSON* body;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
55
components/spotify/cspot/bell/include/MpegDashDemuxer.h
Normal file
55
components/spotify/cspot/bell/include/MpegDashDemuxer.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "BinaryReader.h"
|
||||
#include "ByteStream.h"
|
||||
#include "MpegDashTypes.h"
|
||||
#include <memory>
|
||||
|
||||
namespace bell::mpeg
|
||||
{
|
||||
class MpegDashDemuxer
|
||||
{
|
||||
std::shared_ptr<bell::BinaryReader> reader;
|
||||
std::unique_ptr<bell::mpeg::MpegBox> lastBox;
|
||||
std::unique_ptr<bell::mpeg::Moof> lastMoof;
|
||||
std::vector<bell::mpeg::Mp4Track> tracks;
|
||||
bool chunkZero = false;
|
||||
|
||||
public:
|
||||
MpegDashDemuxer(std::shared_ptr<ByteStream> stream);
|
||||
~MpegDashDemuxer() {
|
||||
reader->close();
|
||||
}
|
||||
void parse();
|
||||
std::unique_ptr<bell::mpeg::MpegBox> readBox();
|
||||
// std::vector<Mp4Track> getTracks();
|
||||
std::vector<uint8_t> readFullBox(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
void ensureBox(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
size_t position();
|
||||
void close();
|
||||
std::unique_ptr<bell::mpeg::Mvhd> parseMvhd();
|
||||
std::unique_ptr<bell::mpeg::Chunk> getNextChunk(bool infoOnly);
|
||||
std::unique_ptr<bell::mpeg::TrunEntry> getTrunEntry(std::unique_ptr<Trun> &, int);
|
||||
std::unique_ptr<bell::mpeg::TrunEntry> getAbsoluteTrunEntry(std::unique_ptr<bell::mpeg::Trun> &, int, std::unique_ptr<bell::mpeg::Tfhd> &);
|
||||
std::vector<bell::mpeg::Trex> parseMvex(std::unique_ptr<bell::mpeg::MpegBox> &, int possibleTrackCount);
|
||||
std::unique_ptr<bell::mpeg::Mp4DashSample> getNextSample(std::unique_ptr<bell::mpeg::Chunk> &);
|
||||
bool hasFlag(int flags, int mask);
|
||||
std::unique_ptr<bell::mpeg::Hdlr> parseHdlr(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
std::unique_ptr<bell::mpeg::Moov> parseMoov(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
int parseMfhd();
|
||||
std::unique_ptr<bell::mpeg::Traf> parseTraf(std::unique_ptr<bell::mpeg::MpegBox> &, int);
|
||||
std::unique_ptr<bell::mpeg::Moof> parseMoof(std::unique_ptr<bell::mpeg::MpegBox> &, int);
|
||||
std::unique_ptr<bell::mpeg::Tfhd> parseTfhd(int);
|
||||
long parseTfdt();
|
||||
std::unique_ptr<bell::mpeg::Trun> parseTrun();
|
||||
std::unique_ptr<bell::mpeg::Tkhd> parseTkhd();
|
||||
std::unique_ptr<bell::mpeg::Trak> parseTrak(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
bell::mpeg::Trex parseTrex();
|
||||
std::unique_ptr<bell::mpeg::Mdia> parseMdia(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
std::unique_ptr<bell::mpeg::Minf> parseMinf(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
std::unique_ptr<bell::mpeg::Elst> parseEdts(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
std::vector<uint8_t> parseStbl(std::unique_ptr<bell::mpeg::MpegBox> &);
|
||||
std::unique_ptr<bell::mpeg::MpegBox> untilBox(std::unique_ptr<bell::mpeg::MpegBox> &, int, int, int);
|
||||
std::unique_ptr<bell::mpeg::MpegBox> untilBox(std::unique_ptr<bell::mpeg::MpegBox> &, int, int);
|
||||
std::unique_ptr<bell::mpeg::MpegBox> untilBox(std::unique_ptr<bell::mpeg::MpegBox> &, int);
|
||||
std::unique_ptr<bell::mpeg::MpegBox> untilAnyBox(std::unique_ptr<MpegBox> &);
|
||||
};
|
||||
}
|
||||
179
components/spotify/cspot/bell/include/MpegDashTypes.h
Normal file
179
components/spotify/cspot/bell/include/MpegDashTypes.h
Normal file
@@ -0,0 +1,179 @@
|
||||
#ifndef BELL_MPEG_DASH_TYPES_H
|
||||
#define BELL_MPEG_DASH_TYPES_H
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace bell::mpeg
|
||||
{
|
||||
|
||||
struct Tkhd
|
||||
{
|
||||
int trackId;
|
||||
long duration;
|
||||
short bVolume;
|
||||
int bWidth;
|
||||
int bHeight;
|
||||
std::vector<uint8_t> matrix;
|
||||
short bLayer;
|
||||
short bAlternateGroup;
|
||||
};
|
||||
|
||||
struct Elst
|
||||
{
|
||||
long mediaTime;
|
||||
int bMediaRate;
|
||||
};
|
||||
|
||||
struct Trex
|
||||
{
|
||||
int trackId;
|
||||
int defaultSampleDescriptionIndex;
|
||||
int defaultSampleDuration;
|
||||
int defaultSampleSize;
|
||||
int defaultSampleFlags;
|
||||
};
|
||||
|
||||
struct Mvhd
|
||||
{
|
||||
long timeScale;
|
||||
long nextTrackId;
|
||||
};
|
||||
|
||||
struct Hdlr
|
||||
{
|
||||
int type;
|
||||
int subType;
|
||||
std::vector<uint8_t> bReserved;
|
||||
};
|
||||
|
||||
struct Minf
|
||||
{
|
||||
std::vector<uint8_t> dinf;
|
||||
std::vector<uint8_t> stblStsd;
|
||||
std::vector<uint8_t> mhd;
|
||||
};
|
||||
|
||||
struct Tfhd
|
||||
{
|
||||
int bFlags;
|
||||
int trackId;
|
||||
int defaultSampleDuration;
|
||||
int defaultSampleSize;
|
||||
int defaultSampleFlags;
|
||||
};
|
||||
|
||||
struct TrunEntry
|
||||
{
|
||||
int sampleDuration;
|
||||
int sampleSize;
|
||||
int sampleFlags;
|
||||
int sampleCompositionTimeOffset;
|
||||
bool hasCompositionTimeOffset;
|
||||
bool isKeyframe;
|
||||
};
|
||||
|
||||
struct Trun
|
||||
{
|
||||
int chunkDuration;
|
||||
int chunkSize;
|
||||
int bFlags;
|
||||
int bFirstSampleFlags;
|
||||
int dataOffset;
|
||||
int entryCount;
|
||||
std::vector<uint8_t> bEntries;
|
||||
int entriesRowSize;
|
||||
};
|
||||
|
||||
struct Traf
|
||||
{
|
||||
std::unique_ptr<Tfhd> tfhd;
|
||||
long tfdt = -1;
|
||||
std::unique_ptr<Trun> trun;
|
||||
};
|
||||
|
||||
struct Moof
|
||||
{
|
||||
int mfgdSequenceNumber;
|
||||
std::unique_ptr<Traf> traf;
|
||||
};
|
||||
struct Chunk
|
||||
{
|
||||
std::vector<uint8_t> data = std::vector<uint8_t>();
|
||||
std::unique_ptr<Moof> moof;
|
||||
int i = 0;
|
||||
int size = 0;
|
||||
int sampleRead = 0;
|
||||
};
|
||||
|
||||
struct Mp4DashSample
|
||||
{
|
||||
std::unique_ptr<TrunEntry> info;
|
||||
std::vector<uint8_t> data = std::vector<uint8_t>();
|
||||
};
|
||||
|
||||
struct Mdia
|
||||
{
|
||||
int mhdTimeScale;
|
||||
std::vector<uint8_t> mdhd;
|
||||
std::unique_ptr<Hdlr> hdlr;
|
||||
std::unique_ptr<Minf> minf;
|
||||
};
|
||||
|
||||
struct Trak
|
||||
{
|
||||
std::unique_ptr<Tkhd> tkhd;
|
||||
std::unique_ptr<Elst> edstElst;
|
||||
std::unique_ptr<Mdia> mdia;
|
||||
};
|
||||
|
||||
struct Mp4Track
|
||||
{
|
||||
std::unique_ptr<Trak> trak;
|
||||
};
|
||||
|
||||
struct Moov
|
||||
{
|
||||
std::unique_ptr<Mvhd> mvhd;
|
||||
std::vector<std::unique_ptr<Trak>> trak;
|
||||
std::vector<Trex> mvexTrex;
|
||||
};
|
||||
|
||||
struct MpegBox
|
||||
{
|
||||
long size = 0;
|
||||
int32_t type = 0;
|
||||
long offset;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#define ATOM_MOOF 0x6D6F6F66
|
||||
#define ATOM_MFHD 0x6D666864
|
||||
#define ATOM_TRAF 0x74726166
|
||||
#define ATOM_TFHD 0x74666864
|
||||
#define ATOM_TFDT 0x74666474
|
||||
#define ATOM_TRUN 0x7472756E
|
||||
#define ATOM_MDIA 0x6D646961
|
||||
#define ATOM_FTYP 0x66747970
|
||||
#define ATOM_SIDX 0x73696478
|
||||
#define ATOM_MOOV 0x6D6F6F76
|
||||
#define ATOM_MDAT 0x6D646174
|
||||
#define ATOM_MVHD 0x6D766864
|
||||
#define ATOM_TRAK 0x7472616B
|
||||
#define ATOM_MVEX 0x6D766578
|
||||
#define ATOM_TREX 0x74726578
|
||||
#define ATOM_TKHD 0x746B6864
|
||||
#define ATOM_MFRA 0x6D667261
|
||||
#define ATOM_MDHD 0x6D646864
|
||||
#define ATOM_EDTS 0x65647473
|
||||
#define ATOM_ELST 0x656C7374
|
||||
#define ATOM_HDLR 0x68646C72
|
||||
#define ATOM_MINF 0x6D696E66
|
||||
#define ATOM_DINF 0x64696E66
|
||||
#define ATOM_STBL 0x7374626C
|
||||
#define ATOM_STSD 0x73747364
|
||||
#define ATOM_VMHD 0x766D6864
|
||||
#define ATOM_SMHD 0x736D6864
|
||||
|
||||
#endif
|
||||
115
components/spotify/cspot/bell/include/Queue.h
Normal file
115
components/spotify/cspot/bell/include/Queue.h
Normal file
@@ -0,0 +1,115 @@
|
||||
#ifndef BELL_QUEUE_H
|
||||
#define BELL_QUEUE_H
|
||||
|
||||
#include <queue>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <atomic>
|
||||
|
||||
namespace bell
|
||||
{
|
||||
template <typename dataType>
|
||||
class Queue
|
||||
{
|
||||
private:
|
||||
/// Queue
|
||||
std::queue<dataType> m_queue;
|
||||
/// Mutex to controll multiple access
|
||||
std::mutex m_mutex;
|
||||
/// Conditional variable used to fire event
|
||||
std::condition_variable m_cv;
|
||||
/// Atomic variable used to terminate immediately wpop and wtpop functions
|
||||
std::atomic<bool> m_forceExit = false;
|
||||
|
||||
public:
|
||||
/// <summary> Add a new element in the queue. </summary>
|
||||
/// <param name="data"> New element. </param>
|
||||
void push(dataType const &data)
|
||||
{
|
||||
m_forceExit.store(false);
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
m_queue.push(data);
|
||||
lk.unlock();
|
||||
m_cv.notify_one();
|
||||
}
|
||||
/// <summary> Check queue empty. </summary>
|
||||
/// <returns> True if the queue is empty. </returns>
|
||||
bool isEmpty() const
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
return m_queue.empty();
|
||||
}
|
||||
/// <summary> Pop element from queue. </summary>
|
||||
/// <param name="popped_value"> [in,out] Element. </param>
|
||||
/// <returns> false if the queue is empty. </returns>
|
||||
bool pop(dataType &popped_value)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
if (m_queue.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
popped_value = m_queue.front();
|
||||
m_queue.pop();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary> Wait and pop an element in the queue. </summary>
|
||||
/// <param name="popped_value"> [in,out] Element. </param>
|
||||
/// <returns> False for forced exit. </returns>
|
||||
bool wpop(dataType &popped_value)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
m_cv.wait(lk, [&]() -> bool
|
||||
{ return !m_queue.empty() || m_forceExit.load(); });
|
||||
if (m_forceExit.load())
|
||||
return false;
|
||||
popped_value = m_queue.front();
|
||||
m_queue.pop();
|
||||
return true;
|
||||
}
|
||||
/// <summary> Timed wait and pop an element in the queue. </summary>
|
||||
/// <param name="popped_value"> [in,out] Element. </param>
|
||||
/// <param name="milliseconds"> [in] Wait time. </param>
|
||||
/// <returns> False for timeout or forced exit. </returns>
|
||||
bool wtpop(dataType &popped_value, long milliseconds = 1000)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
m_cv.wait_for(lk, std::chrono::milliseconds(milliseconds), [&]() -> bool
|
||||
{ return !m_queue.empty() || m_forceExit.load(); });
|
||||
if (m_forceExit.load())
|
||||
return false;
|
||||
if (m_queue.empty())
|
||||
return false;
|
||||
popped_value = m_queue.front();
|
||||
m_queue.pop();
|
||||
return true;
|
||||
}
|
||||
/// <summary> Queue size. </summary>
|
||||
int size()
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
return static_cast<int>(m_queue.size());
|
||||
}
|
||||
/// <summary> Free the queue and force stop. </summary>
|
||||
void clear()
|
||||
{
|
||||
m_forceExit.store(true);
|
||||
std::unique_lock<std::mutex> lk(m_mutex);
|
||||
while (!m_queue.empty())
|
||||
{
|
||||
//delete m_queue.front();
|
||||
m_queue.pop();
|
||||
}
|
||||
}
|
||||
/// <summary> Check queue in forced exit state. </summary>
|
||||
bool isExit() const
|
||||
{
|
||||
return m_forceExit.load();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
110
components/spotify/cspot/bell/include/TCPSocket.h
Normal file
110
components/spotify/cspot/bell/include/TCPSocket.h
Normal file
@@ -0,0 +1,110 @@
|
||||
#ifndef BELL_BASIC_SOCKET_H
|
||||
#define BELL_BASIC_SOCKET_H
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <ctype.h>
|
||||
#include <cstring>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <netinet/tcp.h>
|
||||
#include <BellLogger.h>
|
||||
|
||||
namespace bell
|
||||
{
|
||||
class TCPSocket : public bell::Socket
|
||||
{
|
||||
private:
|
||||
int sockFd;
|
||||
bool isClosed = false;
|
||||
|
||||
public:
|
||||
TCPSocket() {};
|
||||
~TCPSocket() {
|
||||
close();
|
||||
};
|
||||
|
||||
void open(std::string url)
|
||||
{
|
||||
// remove https or http from url
|
||||
url.erase(0, url.find("://") + 3);
|
||||
|
||||
// split by first "/" in url
|
||||
std::string hostUrl = url.substr(0, url.find('/'));
|
||||
std::string pathUrl = url.substr(url.find('/'));
|
||||
|
||||
std::string portString = "80";
|
||||
|
||||
// check if hostUrl contains ':'
|
||||
if (hostUrl.find(':') != std::string::npos)
|
||||
{
|
||||
// split by ':'
|
||||
std::string host = hostUrl.substr(0, hostUrl.find(':'));
|
||||
portString = hostUrl.substr(hostUrl.find(':') + 1);
|
||||
hostUrl = host;
|
||||
}
|
||||
|
||||
int domain = AF_INET;
|
||||
int socketType = SOCK_STREAM;
|
||||
|
||||
addrinfo hints, *addr;
|
||||
//fine-tune hints according to which socket you want to open
|
||||
hints.ai_family = domain;
|
||||
hints.ai_socktype = socketType;
|
||||
hints.ai_protocol = IPPROTO_IP; // no enum : possible value can be read in /etc/protocols
|
||||
hints.ai_flags = AI_CANONNAME | AI_ALL | AI_ADDRCONFIG;
|
||||
|
||||
BELL_LOG(info, "http", "%s %s", hostUrl.c_str(), portString.c_str());
|
||||
|
||||
if (getaddrinfo(hostUrl.c_str(), portString.c_str(), &hints, &addr) != 0)
|
||||
{
|
||||
BELL_LOG(error, "webradio", "DNS lookup error");
|
||||
throw std::runtime_error("Resolve failed");
|
||||
}
|
||||
|
||||
sockFd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
||||
|
||||
if (connect(sockFd, addr->ai_addr, addr->ai_addrlen) < 0)
|
||||
{
|
||||
close();
|
||||
BELL_LOG(error, "http", "Could not connect to %s", url.c_str());
|
||||
throw std::runtime_error("Resolve failed");
|
||||
}
|
||||
|
||||
int flag = 1;
|
||||
setsockopt(sockFd, /* socket affected */
|
||||
IPPROTO_TCP, /* set option at TCP level */
|
||||
TCP_NODELAY, /* name of option */
|
||||
(char *)&flag, /* the cast is historical cruft */
|
||||
sizeof(int)); /* length of option value */
|
||||
|
||||
freeaddrinfo(addr);
|
||||
}
|
||||
|
||||
size_t read(uint8_t *buf, size_t len) {
|
||||
return recv(sockFd, buf, len, 0);
|
||||
}
|
||||
|
||||
size_t write(uint8_t *buf, size_t len) {
|
||||
return send(sockFd, buf, len, 0);
|
||||
}
|
||||
|
||||
void close() {
|
||||
if (!isClosed) {
|
||||
::close(sockFd);
|
||||
isClosed = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
118
components/spotify/cspot/bell/include/Task.h
Normal file
118
components/spotify/cspot/bell/include/Task.h
Normal file
@@ -0,0 +1,118 @@
|
||||
#ifndef BELL_TASK_H
|
||||
#define BELL_TASK_H
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <esp_pthread.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/timers.h>
|
||||
#include <freertos/task.h>
|
||||
#endif
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
namespace bell
|
||||
{
|
||||
class Task
|
||||
{
|
||||
public:
|
||||
std::string taskName;
|
||||
int stackSize, core;
|
||||
bool isRunning = false;
|
||||
bool runOnPSRAM;
|
||||
Task(std::string taskName, int stackSize, int priority, int core, bool runOnPSRAM = true)
|
||||
{
|
||||
this->taskName = taskName;
|
||||
this->stackSize = stackSize;
|
||||
this->core = core;
|
||||
this->runOnPSRAM = runOnPSRAM;
|
||||
#ifdef ESP_PLATFORM
|
||||
this->priority = ESP_TASK_PRIO_MIN + 1 + priority;
|
||||
if (this->priority < 0) this->priority = ESP_TASK_PRIO_MIN + 1;
|
||||
#endif
|
||||
}
|
||||
virtual ~Task() {}
|
||||
|
||||
bool startTask()
|
||||
{
|
||||
#ifdef ESP_PLATFORM
|
||||
if (runOnPSRAM)
|
||||
{
|
||||
|
||||
xTaskBuffer = (StaticTask_t *)heap_caps_malloc(sizeof(StaticTask_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
||||
xStack = (StackType_t *)heap_caps_malloc(this->stackSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
|
||||
return (xTaskCreateStaticPinnedToCore(taskEntryFuncPSRAM, this->taskName.c_str(), this->stackSize, this, 2, xStack, xTaskBuffer, this->core) != NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
|
||||
cfg.stack_size = stackSize;
|
||||
cfg.inherit_cfg = true;
|
||||
cfg.thread_name = this->taskName.c_str();
|
||||
cfg.pin_to_core = core;
|
||||
cfg.prio = priority;
|
||||
esp_pthread_set_cfg(&cfg);
|
||||
}
|
||||
#endif
|
||||
return (pthread_create(&_thread, NULL, taskEntryFunc, this) == 0);
|
||||
}
|
||||
void waitForTaskToReturn()
|
||||
{
|
||||
#ifdef ESP_PLATFORM
|
||||
if (runOnPSRAM)
|
||||
{
|
||||
while (isRunning)
|
||||
{
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
heap_caps_free(xStack);
|
||||
// TCB are cleanup in IDLE task, so give it some time
|
||||
TimerHandle_t timer = xTimerCreate( "cleanup", pdMS_TO_TICKS(5000), pdFALSE, xTaskBuffer,
|
||||
[](TimerHandle_t xTimer) {
|
||||
heap_caps_free(pvTimerGetTimerID(xTimer));
|
||||
xTimerDelete(xTimer, portMAX_DELAY);
|
||||
} );
|
||||
xTimerStart(timer, portMAX_DELAY);
|
||||
}
|
||||
else
|
||||
{
|
||||
(void)pthread_join(_thread, NULL);
|
||||
}
|
||||
#else
|
||||
(void)pthread_join(_thread, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void runTask() = 0;
|
||||
|
||||
private:
|
||||
#ifdef ESP_PLATFORM
|
||||
int priority;
|
||||
StaticTask_t *xTaskBuffer;
|
||||
StackType_t *xStack;
|
||||
#endif
|
||||
static void *taskEntryFunc(void *This)
|
||||
{
|
||||
((Task *)This)->runTask();
|
||||
return NULL;
|
||||
}
|
||||
#ifdef ESP_PLATFORM
|
||||
static void taskEntryFuncPSRAM(void *This)
|
||||
{
|
||||
|
||||
((Task *)This)->isRunning = true;
|
||||
|
||||
Task *self = (Task *)This;
|
||||
self->isRunning = true;
|
||||
self->runTask();
|
||||
self->isRunning = false;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
pthread_t _thread;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
4
components/spotify/cspot/bell/include/esp_platform.h
Normal file
4
components/spotify/cspot/bell/include/esp_platform.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifdef __XTENSA__
|
||||
#include <xtensa/config/core-isa.h>
|
||||
#include <xtensa/config/core-matmap.h>
|
||||
#endif
|
||||
71
components/spotify/cspot/bell/include/platform/TLSSocket.h
Normal file
71
components/spotify/cspot/bell/include/platform/TLSSocket.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef BELL_TLS_SOCKET_H
|
||||
#define BELL_TLS_SOCKET_H
|
||||
|
||||
#include "BellLogger.h"
|
||||
#include "BellSocket.h"
|
||||
#include <cstring>
|
||||
#include <ctype.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sstream>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#ifdef BELL_USE_MBEDTLS
|
||||
|
||||
#include "mbedtls/net_sockets.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/debug.h"
|
||||
#else
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#endif
|
||||
|
||||
namespace bell {
|
||||
class TLSSocket : public bell::Socket {
|
||||
private:
|
||||
#ifdef BELL_USE_MBEDTLS
|
||||
mbedtls_net_context server_fd;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_ssl_context ssl;
|
||||
mbedtls_ssl_config conf;
|
||||
#else
|
||||
|
||||
BIO *sbio, *out;
|
||||
int len;
|
||||
char tmpbuf[1024];
|
||||
SSL_CTX *ctx;
|
||||
SSL *ssl;
|
||||
|
||||
int sockFd;
|
||||
int sslFd;
|
||||
#endif
|
||||
|
||||
bool isClosed = false;
|
||||
public:
|
||||
TLSSocket();
|
||||
~TLSSocket() { close(); };
|
||||
|
||||
void open(std::string host);
|
||||
|
||||
size_t read(uint8_t *buf, size_t len);
|
||||
size_t write(uint8_t *buf, size_t len);
|
||||
|
||||
void close();
|
||||
};
|
||||
|
||||
} // namespace bell
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#ifdef _WIN32
|
||||
#include "win32/WrappedMutex.h"
|
||||
#else
|
||||
#include "unixlike/WrappedMutex.h"
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef WRAPPEDSEMAPHORE_H
|
||||
#define WRAPPEDSEMAPHORE_H
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#elif __APPLE__
|
||||
#include <dispatch/dispatch.h>
|
||||
#else
|
||||
#include <semaphore.h>
|
||||
#endif
|
||||
|
||||
class WrappedSemaphore
|
||||
{
|
||||
private:
|
||||
#ifdef ESP_PLATFORM
|
||||
xSemaphoreHandle semaphoreHandle;
|
||||
#elif __APPLE__
|
||||
dispatch_semaphore_t semaphoreHandle;
|
||||
#else
|
||||
sem_t semaphoreHandle;
|
||||
#endif
|
||||
public:
|
||||
WrappedSemaphore(int maxVal = 200);
|
||||
~WrappedSemaphore();
|
||||
|
||||
int wait();
|
||||
int twait(long milliseconds = 10);
|
||||
void give();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef UNIXLIKE_WRAPPED_MUTEX_H
|
||||
#define UNIXLIKE_WRAPPED_MUTEX_H
|
||||
#include <pthread.h>
|
||||
|
||||
/**
|
||||
* Wraps a mutex on unixlike patforms (linux, macOS, esp32 etc.).
|
||||
* Header only since all the methods call one function, we want them to be inlined
|
||||
**/
|
||||
class WrappedMutex
|
||||
{
|
||||
public:
|
||||
WrappedMutex()
|
||||
{
|
||||
pthread_mutex_init(&this->_mutex, NULL);
|
||||
}
|
||||
|
||||
~WrappedMutex()
|
||||
{
|
||||
pthread_mutex_destroy(&this->_mutex);
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
pthread_mutex_lock(&this->_mutex);
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&this->_mutex);
|
||||
}
|
||||
|
||||
private:
|
||||
pthread_mutex_t _mutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef WIN32_WRAPPED_MUTEX_H
|
||||
#define WIN32_WRAPPED_MUTEX_H
|
||||
#include <Windows.h>
|
||||
|
||||
/**
|
||||
* Wraps a windows mutex.
|
||||
* Header only since all the methods call one function, we want them to be inlined
|
||||
**/
|
||||
class WrappedMutex
|
||||
{
|
||||
public:
|
||||
WrappedMutex()
|
||||
{
|
||||
this->_mutex = CreateMutex(
|
||||
NULL, // default security attributes
|
||||
FALSE, // initially not owned
|
||||
NULL); // unnamed mutex
|
||||
}
|
||||
|
||||
~WrappedMutex()
|
||||
{
|
||||
CloseHandle(_mutex);
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
WaitForSingleObject(_mutex, INFINITE);
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
ReleaseMutex(_mutex);
|
||||
}
|
||||
|
||||
private:
|
||||
HANDLE _mutex;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user