Skip to content

Commit

Permalink
Event manager (#3)
Browse files Browse the repository at this point in the history
* moved trackmetrics playStart back to TrackPlayer because needed

* changes in track queueing

* Changed structural from spirc to connectState

* minor changes

* minor clang-format changes
  • Loading branch information
tobiasguyer authored Sep 24, 2024
1 parent b489cc9 commit 4f15632
Show file tree
Hide file tree
Showing 39 changed files with 2,816 additions and 1,612 deletions.
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"
}
}
4 changes: 4 additions & 0 deletions cspot/include/CSpotContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

#include "Crypto.h"
#include "EventManager.h"
Expand Down Expand Up @@ -37,6 +38,8 @@ 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 @@ -68,6 +71,7 @@ struct Context {
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()};

ctx->session = std::make_shared<MercurySession>(ctx->timeProvider);
ctx->playbackMetrics = std::make_shared<PlaybackMetrics>(ctx);
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
110 changes: 110 additions & 0 deletions cspot/include/DeviceStateHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* 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 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*);
};
} // namespace cspot
3 changes: 2 additions & 1 deletion cspot/include/EventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ class TrackMetrics {

void newPosition(uint64_t pos);
void endInterval(uint64_t pos);
void pauseInterval(uint64_t pos, bool pause);
void endTrack();
void startTrack();
void startTrackDecoding();
void startTrackPlaying(uint64_t pos);
uint64_t getPosition();
uint64_t getPosition(bool paused = false);

private:
std::shared_ptr<cspot::Context> ctx;
Expand Down
15 changes: 10 additions & 5 deletions cspot/include/MercurySession.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class MercurySession : public bell::Task, public cspot::Session {
UNSUB = 0xb4,
SUBRES = 0xb5,
SEND = 0xb2,
GET = 0xFF, // Shitty workaround, it's value is actually same as SEND
GET = 0xFF, // Shitty workaround, it's value is actually same as SEND
POST = 0xb6, //??
PUT = 0xb7, //??
PING = 0x04,
PONG_ACK = 0x4a,
AUDIO_CHUNK_REQUEST_COMMAND = 0x08,
Expand All @@ -57,14 +59,16 @@ class MercurySession : public bell::Task, public cspot::Session {
};

std::unordered_map<RequestType, std::string> RequestTypeMap = {
{RequestType::GET, "GET"},
{RequestType::SEND, "SEND"},
{RequestType::SUB, "SUB"},
{RequestType::UNSUB, "UNSUB"},
{RequestType::GET, "GET"}, {RequestType::SEND, "SEND"},
{RequestType::SUB, "SUB"}, {RequestType::UNSUB, "UNSUB"},
{RequestType::POST, "POST"}, {RequestType::PUT, "PUT"},
};

void handlePacket();

void addSubscriptionListener(const std::string& uri,
ResponseCallback subscription);

uint64_t executeSubscription(RequestType type, const std::string& uri,
ResponseCallback callback,
ResponseCallback subscription, DataParts& parts);
Expand Down Expand Up @@ -129,6 +133,7 @@ class MercurySession : public bell::Task, public cspot::Session {
std::atomic<bool> isRunning = false;
std::atomic<bool> isReconnecting = false;
std::atomic<bool> executeEstabilishedCallback = false;
std::atomic<bool> connection_lost = false;

void failAllPending();

Expand Down
97 changes: 0 additions & 97 deletions cspot/include/PlaybackState.h

This file was deleted.

Loading

0 comments on commit 4f15632

Please sign in to comment.