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

Reporting trough event-manager, so the played songs will pop up in the listening history #172

Open
wants to merge 52 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 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
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
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
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
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"
}
}
7 changes: 7 additions & 0 deletions 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"
feelfreelinux marked this conversation as resolved.
Show resolved Hide resolved
#include "LoginBlob.h"
#include "MercurySession.h"
#include "TimeProvider.h"
Expand Down Expand Up @@ -35,6 +37,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 @@ -66,8 +71,10 @@ 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);
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
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
121 changes: 121 additions & 0 deletions cspot/include/EventManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#pragma once

#include <stdint.h> // for uint8_t, uint32_t
#include <algorithm> //for sort
#include <memory> // for shared_ptr
#include <string> // for string
#include <vector> // for vector

namespace cspot {
struct Context;
class QueuedTrack;

struct TrackInterval {
uint64_t start;
uint64_t end;
uint64_t position;
uint64_t length;

TrackInterval();
TrackInterval(uint64_t start, uint64_t position) {
this->start = start;
this->position = position;
}
};

struct skip {
uint8_t count = 0;
uint64_t amount = 0;

void add(uint64_t amount) {
this->amount += amount;
count++;
}
};

class TrackMetrics {
public:
TrackMetrics(std::shared_ptr<cspot::Context> ctx, uint64_t pos = 0);

std::shared_ptr<TrackInterval> currentInterval;
uint64_t longestInterval = 0, totalAmountOfPlayTime = 0,
totalMsOfPlayedTrack = 0, totalAmountPlayed = 0, audioKeyTime = 0,
trackHeaderTime = 0, written_bytes = 0, track_size = 0,
timestamp = 0, trackLatency = 0;
std::vector<std::pair<uint64_t, uint64_t>> intervals;
skip skipped_backward, skipped_forward;

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(bool paused = false);

private:
std::shared_ptr<cspot::Context> ctx;
std::vector<std::pair<uint64_t, uint64_t>> addInterval(
std::vector<std::pair<uint64_t, uint64_t>>& intervals,
std::pair<std::uint64_t, std::uint64_t> newInterval) {
// Add the new interval to the list of intervals
intervals.push_back(newInterval);

// Sort intervals by starting time
std::sort(intervals.begin(), intervals.end());

// Result vector to hold merged intervals
std::vector<std::pair<uint64_t, uint64_t>> merged;

// Iterate over intervals to merge overlapping ones
for (const auto& interval : intervals) {
// If merged is empty or current interval does not overlap with the previous one
if (merged.empty() || merged.back().second < interval.first) {
merged.push_back(interval);
} else {
// If it overlaps, merge the intervals
merged.back().second = std::max(merged.back().second, interval.second);
}
}

return merged;
}
};
class PlaybackMetrics {
private:
std::shared_ptr<cspot::Context> ctx;
uint32_t seqNum = 0;
size_t timestamp;

std::string get_source_from_context();
std::string get_end_source();

void append(std::vector<uint8_t>* data, std::string to_do) {
data->insert(data->end(), to_do.begin(), to_do.end());
data->push_back(9);
}

public:
PlaybackMetrics(std::shared_ptr<cspot::Context> ctx) { this->ctx = ctx; }
enum reason {
TRACK_DONE,
TRACK_ERROR,
FORWARD_BTN,
BACKWARD_BTN,
END_PLAY,
PLAY_BTN,
CLICK_ROW,
LOGOUT,
APP_LOAD,
REMOTE,
UNKNOWN
} end_reason = UNKNOWN,
start_reason = UNKNOWN, nested_reason = UNKNOWN;

std::string context_uri, correlation_id, start_source, end_source;
std::shared_ptr<cspot::TrackMetrics> trackMetrics;

std::vector<uint8_t> sendEvent(std::shared_ptr<cspot::QueuedTrack> track);
};
} // namespace cspot
Loading
Loading