Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for ESP32 #169

Open
wants to merge 63 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
96e9690
fixes for esp32
tobiasguyer Dec 24, 2023
eb9d203
fixes for esp32
tobiasguyer Dec 24, 2023
84e298a
reuploaded sdkconfig.defaults
tobiasguyer Dec 25, 2023
18c2cfa
Merge branch 'feelfreelinux:master' into master
tobiasguyer Jul 10, 2024
1354a28
added gzip compatible mercury decoder (failed if message were trucate…
tobiasguyer Jul 10, 2024
5edbb8d
reporting trough event-manager, so the played songs will pop up in th…
tobiasguyer Jul 11, 2024
98e96c4
Merge branch 'gzip-working' of github.com:tobiasguyer/cspot into tobi…
feelfreelinux Jul 12, 2024
a450f3b
fix: clang-format
feelfreelinux Jul 12, 2024
aca2a2c
fix: Minor style fixes
feelfreelinux Jul 12, 2024
7a61b09
fix: clear partials on sub res and reconnect
feelfreelinux Jul 12, 2024
1ea97b4
fix: Restore bell version
feelfreelinux Jul 12, 2024
5771aa2
fix: Update bell
feelfreelinux Jul 12, 2024
5c35a33
Implement partial responses support in MercuryManager (#171)
tobiasguyer Jul 12, 2024
34bf9e1
reporting trough event-manager, so the played songs will pop up in th…
tobiasguyer Jul 11, 2024
9a1591f
Merge pull request #1 from tobiasguyer/gzip-working
tobiasguyer Jul 13, 2024
b41251c
reporting trough event-manager, so the played songs will pop up in th…
tobiasguyer Jul 11, 2024
f744c9b
Merge branch 'master' of https://github.com/tobiasguyer/cspot
tobiasguyer Jul 13, 2024
e041d9a
added repeat and shuffle support, a context-resolver and a radio func…
tobiasguyer Jul 15, 2024
5539d29
missing change in CliPlayer.cpp
tobiasguyer Jul 15, 2024
8f412a6
Minor style fixes
tobiasguyer Jul 15, 2024
a53083a
Minor style fixes
tobiasguyer Jul 15, 2024
35b253d
Merge branch 'feelfreelinux:master' into EventManager
tobiasguyer Jul 15, 2024
2e65f19
Merge branch 'master' into EventManager
tobiasguyer Jul 15, 2024
da825f5
Merge branch 'EventManager' of https://github.com/tobiasguyer/cspot i…
tobiasguyer Jul 15, 2024
a87f828
Minor fixes
tobiasguyer Jul 15, 2024
2e7df5e
fixing formating mistakes
tobiasguyer Jul 15, 2024
24f7f3e
fixing formating mistakes
tobiasguyer Jul 15, 2024
0547c41
Merge branch 'develop' into EventManager
tobiasguyer Jul 15, 2024
cb9f489
Update currentTracksSize in the resolveContext function
tobiasguyer Jul 15, 2024
b5fe362
Merge branch 'EventManager' of https://github.com/tobiasguyer/cspot i…
tobiasguyer Jul 15, 2024
cd72ee6
Solved problems with resolveContext in case of smart shuffle
tobiasguyer Jul 16, 2024
a7426ec
currentTracksIndex is pointing to the currentlyPlaying song, VS1053 s…
tobiasguyer Jul 16, 2024
ef5a614
currentTracksIndex is pointing to the currentlyPlaying song, VS1053 s…
tobiasguyer Jul 16, 2024
25913c6
adjusted some mistakes
tobiasguyer Jul 16, 2024
d9b8373
adjusted some mistakes
tobiasguyer Jul 16, 2024
879197a
repeat is working - chnaged vs1053 structure to a constant stream - a…
tobiasguyer Jul 17, 2024
e9906bc
fixed vs1053.cpp
tobiasguyer Jul 17, 2024
8f2b03e
added a deque for queued tracks
tobiasguyer Jul 17, 2024
8adac01
a little tinkering for the queued tracks
tobiasguyer Jul 17, 2024
326e9d7
Small changes for the cli player
tobiasguyer Jul 17, 2024
40d2383
PlayerContext.h, some structural changes, especially with EventManage…
tobiasguyer Jul 21, 2024
b0c62c0
minor changes
tobiasguyer Jul 21, 2024
4fc99ce
changed the track feedback a little, so it'll skip to the next track …
tobiasguyer Jul 21, 2024
050cb32
finally a proper track loading process
tobiasguyer Jul 22, 2024
c6a5c4d
moved trackmetrics playStart back to TrackPlayer because needed
tobiasguyer Jul 22, 2024
ff981b1
changes in track queueing
tobiasguyer Jul 30, 2024
b489cc9
Event manager (#2)
tobiasguyer Aug 1, 2024
b399b03
Changed structural from spirc to connectState
tobiasguyer Sep 24, 2024
7db5067
minor changes
tobiasguyer Sep 24, 2024
7b152de
minor clang-format changes
tobiasguyer Sep 24, 2024
16dde87
Merge remote-tracking branch 'origin/master' into EventManager
tobiasguyer Sep 24, 2024
4f15632
Event manager (#3)
tobiasguyer Sep 24, 2024
d6e1e6f
Seeking now supported on all devices
tobiasguyer Sep 24, 2024
209d278
adjusting stack-sizes to new structure, minor adjustments for bell_no…
tobiasguyer Sep 29, 2024
4d06ccb
Event manager (#4)
tobiasguyer Sep 29, 2024
2a7c4a0
Merge branch 'EventManager'
tobiasguyer Sep 29, 2024
88c96c4
resolving merge issues
tobiasguyer Sep 29, 2024
ea556ec
tracklist will always be filled with enough tracks, fix for play command
tobiasguyer Oct 4, 2024
07be112
feat: Improve streaming stability, error handling, and refactor track…
tobiasguyer Nov 6, 2024
3500fc1
Update submodule bell to the latest commit
tobiasguyer Nov 6, 2024
3c14707
fix: Revert CMake formatting and optimize queryAutoplay behavior
tobiasguyer Nov 7, 2024
d620c1a
fix: Adjust VS1053 task priority and stream buffer size
tobiasguyer Nov 7, 2024
8d2d5ec
fix: Update submodule bell to the latest commit
tobiasguyer Nov 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "cspot/bell"]
path = cspot/bell
url = https://github.com/feelfreelinux/bell
branch = develop
url = https://github.com/tobiasguyer/bell
branch = master
91 changes: 90 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,94 @@
"--background-index",
"--compile-commands-dir=${workspaceFolder}/targets/cli/build"
],
"editor.tabSize": 2
"editor.tabSize": 2,
"files.associations": {
"vector": "cpp",
"memory": "cpp",
"chrono": "cpp",
"variant": "cpp",
"mutex": "cpp",
"deque": "cpp",
"array": "cpp",
"ranges": "cpp",
"span": "cpp",
"xstring": "cpp",
"xutility": "cpp",
"algorithm": "cpp",
"any": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"filesystem": "cpp",
"format": "cpp",
"forward_list": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"queue": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"utility": "cpp",
"valarray": "cpp",
"xfacet": "cpp",
"xhash": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xtr1common": "cpp",
"xtree": "cpp"
}
}
19 changes: 16 additions & 3 deletions cspot/include/CDNAudioFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ class CDNAudioFile {
public:
CDNAudioFile(const std::string& cdnUrl, const std::vector<uint8_t>& audioKey);

#ifndef CONFIG_BELL_NOCODEC
/**
* @brief Opens connection to the provided cdn url, and fetches track metadata.
*/
void openStream();

#else
/**
* @brief Opens connection to the provided cdn url, and fetches track header.
*
* @param header_size
*
* @returns char* where to read data from
*/
uint8_t* openStream(size_t&);
#endif
/**
* @brief Read and decrypt part of the cdn stream
*
Expand All @@ -34,7 +44,7 @@ class CDNAudioFile {
*
* @returns amount of bytes read
*/
size_t readBytes(uint8_t* dst, size_t bytes);
long readBytes(uint8_t* dst, size_t bytes);

/**
* @brief Returns current position in CDN stream
Expand All @@ -52,6 +62,8 @@ class CDNAudioFile {
*/
void seek(size_t position);

long getHeader();

private:
const int OPUS_HEADER_SIZE = 8 * 1024;
const int OPUS_FOOTER_PREFFERED = 1024 * 12; // 12K should be safe
Expand All @@ -60,10 +72,11 @@ class CDNAudioFile {
const int HTTP_BUFFER_SIZE = 1024 * 14;
const int SPOTIFY_OPUS_HEADER = 167;

#ifndef CONFIG_BELL_NOCODEC
// Used to store opus metadata, speeds up read
std::vector<uint8_t> header = std::vector<uint8_t>(OPUS_HEADER_SIZE);
std::vector<uint8_t> footer;

#endif
// General purpose buffer to read data
std::vector<uint8_t> httpBuffer = std::vector<uint8_t>(HTTP_BUFFER_SIZE);

Expand Down
27 changes: 26 additions & 1 deletion cspot/include/CSpotContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include <stdint.h>
#include <memory>
#include <random> //for random_device and default_random_engine

#include "Crypto.h"
#include "EventManager.h"
#include "LoginBlob.h"
#include "MercurySession.h"
#include "TimeProvider.h"
Expand All @@ -17,6 +19,14 @@
#include "nlohmann/json_fwd.hpp" // for json
#endif

#ifdef ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
#define CSPOT_CONNECTED_BIT BIT2
#endif

namespace cspot {
struct Context {
struct ConfigState {
Expand All @@ -27,6 +37,10 @@ struct Context {
std::vector<uint8_t> authData;
int volume;

#ifdef ESP_PLATFORM
EventGroupHandle_t s_cspot_event_group;
#endif

std::string username;
std::string countryCode;
};
Expand All @@ -35,6 +49,9 @@ struct Context {

std::shared_ptr<TimeProvider> timeProvider;
std::shared_ptr<cspot::MercurySession> session;
std::shared_ptr<PlaybackMetrics> playbackMetrics;
std::random_device rd;
std::default_random_engine rng;
std::string getCredentialsJson() {
#ifdef BELL_ONLY_CJSON
cJSON* json_obj = cJSON_CreateObject();
Expand Down Expand Up @@ -62,12 +79,20 @@ struct Context {
#endif
}

void lost_connection(void*) {
//if(!connection)
}

static std::shared_ptr<Context> createFromBlob(
std::shared_ptr<LoginBlob> blob) {
auto ctx = std::make_shared<Context>();
ctx->timeProvider = std::make_shared<TimeProvider>();

ctx->rng = std::default_random_engine{ctx->rd()};
#ifdef ESP_PLATFORM
//s_cspot_event_group = xEventGroupCreate();
#endif
ctx->session = std::make_shared<MercurySession>(ctx->timeProvider);
ctx->playbackMetrics = std::make_shared<PlaybackMetrics>(ctx);
ctx->config.deviceId = blob->getDeviceId();
ctx->config.deviceName = blob->getDeviceName();
ctx->config.authData = blob->authData;
Expand Down
2 changes: 1 addition & 1 deletion cspot/include/ConstantParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ const char* const brandName = "cspot";
const char* const versionString = "cspot-1.1";
const char* const protocolVersion = "2.7.1";
const char* const defaultDeviceName = "CSpot";
const char* const swVersion = "1.0.0";
const char* const swVersion = "1.1.0";

} // namespace cspot
113 changes: 113 additions & 0 deletions cspot/include/DeviceStateHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* TO DO
*
* autoplay doesn't work for episodes
*
*/
#pragma once

#include <stdint.h> // for uint8_t, uint32_t
#include <deque> //for deque..
#include <functional> //for function
#include <memory> // for shared_ptr
#include <string> // for string
#include <utility> // for pair
#include <variant> // for variant
#include <vector> // for vector

#include "PlayerContext.h" // for PlayerContext::resolveTracklist, jsonToTracklist...
#include "TrackPlayer.h" // for TrackPlayer
#include "TrackQueue.h"
#include "TrackReference.h"
#include "protobuf/connect.pb.h" // for PutStateRequest, DeviceState, PlayerState...

namespace cspot {
struct Context;
struct PlayerContext;

class DeviceStateHandler {
public:
//Command Callback Structure
enum CommandType {
STOP,
PLAY,
PAUSE,
DISC,
DEPLETED,
FLUSH,
PLAYBACK_START,
PLAYBACK,
SKIP_NEXT,
SKIP_PREV,
SEEK,
SET_SHUFFLE,
SET_REPEAT,
VOLUME,
TRACK_INFO,
};

typedef std::variant<std::shared_ptr<cspot::QueuedTrack>, int32_t, bool>
CommandData;

struct Command {
CommandType commandType;
CommandData data;
};

typedef std::function<void(Command)> StateCallback;

DeviceStateHandler(std::shared_ptr<cspot::Context>);
~DeviceStateHandler();

void disconnect();

void putDeviceState(PutStateReason member_type =
PutStateReason::PutStateReason_PLAYER_STATE_CHANGED);

void setDeviceState(PutStateReason put_state_reason);
void putPlayerState(PutStateReason member_type =
PutStateReason::PutStateReason_PLAYER_STATE_CHANGED);
void handleConnectState();
void sendCommand(CommandType, CommandData data = {});

Device device = Device_init_zero;

std::vector<ProvidedTrack> currentTracks = {};
StateCallback stateCallback;

uint64_t started_playing_at = 0;
uint32_t last_message_id = -1;
uint8_t offset = 0;
int64_t offsetFromStartInMillis = 0;

bool is_active = false;
bool reloadPreloadedTracks = true;
bool needsToBeSkipped = true;
bool playerStateChanged = false;

std::shared_ptr<cspot::TrackPlayer> trackPlayer;
std::shared_ptr<cspot::TrackQueue> trackQueue;
std::shared_ptr<cspot::Context> ctx;

private:
std::shared_ptr<PlayerContext> playerContext;

std::pair<uint8_t*, std::vector<ProvidedTrack>*> queuePacket = {
&offset, &currentTracks};
std::vector<std::pair<std::string, std::string>> metadata_map = {};
std::vector<std::pair<std::string, std::string>> context_metadata_map = {};
std::mutex playerStateMutex;
std::string context_uri, context_url;
void parseCommand(std::vector<uint8_t>& data);
void skip(CommandType dir, bool notify);

void unreference(char* string) {
if (string != NULL)
free(string);
string = NULL;
}

static void reloadTrackList(void*);
std::atomic<bool> resolvingContext = false;
};
} // namespace cspot
Loading
Loading