Skip to content

Commit

Permalink
Merge remote-tracking branch 'biologist/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sascha committed Dec 4, 2023
2 parents e4952d8 + 8407213 commit 6ce9edc
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## DEV-branch

* 04.12.2023: fix stuttering sound with some WAV & MP3 files, thanks to @wolle !
* 04.12.2023: change trackprogress communication from websocket to http to improve stability
* 04.12.2023: Remove some convertAsciiToUtf8() #272
* 30.11.2023: Fix a nullptr access after trying to replay an invalid filename (#271), thanks to Olaf!
* 29.11.2023: Updated audio library to play more MP3s, faster track change & delivery of the cover image
* 25.11.2023: Save some cpu time in audio task by only updating the playtime statistics every 250ms
Expand Down
19 changes: 10 additions & 9 deletions html/management.html
Original file line number Diff line number Diff line change
Expand Up @@ -1876,18 +1876,19 @@ <h5 class="modal-title" data-i18n="tools.nvserase.title"></h5>
clearTimeout(tm);
}

function getTrackProgress() {
async function getTrackProgress() {
if (ActiveTab !== 'nav-control-tab') {
return;
};

var myObj = {
"trackProgress": {
trackProgress: 'trackProgress'
}
};
var myJSON = JSON.stringify(myObj);
socket.send(myJSON);
// use http request here, websocket comminication seems to be unstable with many messages
let data = await (await fetch("/trackprogress")).json();
if (data && data.trackProgress) {
// console.log(data.trackProgress);
setTrackProgress(data.trackProgress);
} else {
console.log("failed to fetch trackprogress: " + data);
}
return;
}

function setTrackProgress(msg) {
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extra_scripts =
pre:updateSdkConfig.py
pre:processHtml.py
lib_deps =
https://github.com/schreibfaul1/ESP32-audioI2S.git#4311587
https://github.com/schreibfaul1/ESP32-audioI2S.git#2a0ab81
https://github.com/madhephaestus/ESP32Encoder.git#9979722
https://github.com/knolleary/pubsubclient.git#2d228f2
https://github.com/peterus/ESP-FTP-Server-Lib#554959f
Expand Down
4 changes: 1 addition & 3 deletions src/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,10 @@ uint32_t AudioPlayer_GetFileDuration(void) {
}

void Audio_setTitle(const char *format, ...) {
char buf[256];
va_list args;
va_start(args, format);
vsnprintf(buf, sizeof(buf) / sizeof(buf[0]), format, args);
vsnprintf(gPlayProperties.title, sizeof(gPlayProperties.title) / sizeof(gPlayProperties.title[0]), format, args);
va_end(args);
convertAsciiToUtf8(buf, gPlayProperties.title);

// notify web ui and mqtt
Web_SendWebsocketData(0, 30);
Expand Down
41 changes: 34 additions & 7 deletions src/Web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static void explorerHandleDeleteRequest(AsyncWebServerRequest *request);
static void explorerHandleCreateRequest(AsyncWebServerRequest *request);
static void explorerHandleRenameRequest(AsyncWebServerRequest *request);
static void explorerHandleAudioRequest(AsyncWebServerRequest *request);
static void handleTrackProgressRequest(AsyncWebServerRequest *request);
static void handleGetSavedSSIDs(AsyncWebServerRequest *request);
static void handlePostSavedSSIDs(AsyncWebServerRequest *request, JsonVariant &json);
static void handleDeleteSavedSSIDs(AsyncWebServerRequest *request);
Expand Down Expand Up @@ -282,11 +283,16 @@ static void handleWiFiScanRequest(AsyncWebServerRequest *request) {
json = String();
}

unsigned long lastCleanupClientsTimestamp;

void Web_Cyclic(void) {
webserverStart();
ws.cleanupClients();
if ((millis() - lastCleanupClientsTimestamp) > 1000u) {
// cleanup closed/deserted websocket clients once per second
lastCleanupClientsTimestamp = millis();
ws.cleanupClients();
}
}

// handle not found
void notFound(AsyncWebServerRequest *request) {
Log_Printf(LOGLEVEL_ERROR, "%s not found, redirect to startpage", request->url().c_str());
Expand Down Expand Up @@ -477,6 +483,8 @@ void webserverStart(void) {

wServer.on("/exploreraudio", HTTP_POST, explorerHandleAudioRequest);

wServer.on("/trackprogress", HTTP_GET, handleTrackProgressRequest);

wServer.on("/savedSSIDs", HTTP_GET, handleGetSavedSSIDs);
wServer.addHandler(new AsyncCallbackJsonWebHandler("/savedSSIDs", handlePostSavedSSIDs));

Expand Down Expand Up @@ -966,6 +974,18 @@ void Web_SendWebsocketData(uint32_t client, uint8_t code) {
// we do not have any webclient connected
return;
}
// check if we can send message to the client(s)
if (client == 0) {
if (!ws.availableForWriteAll()) {
Log_Println("Websocket: Cannot send data (Too many messages queued)!", LOGLEVEL_ERROR);
return;
}
} else {
if (!ws.availableForWrite(client)) {
Log_Printf(LOGLEVEL_ERROR, "Websocket: Cannot send data to client %d (Too many messages queued)!", client);
return;
}
}
char *jBuf = (char *) x_calloc(1024, sizeof(char));
StaticJsonDocument<1024> doc;
JsonObject object = doc.to<JsonObject>();
Expand Down Expand Up @@ -1304,12 +1324,9 @@ void explorerHandleListRequest(AsyncWebServerRequest *request) {
String MyfileName = root.getNextFileName(&isDir);
while (MyfileName != "") {
// ignore hidden folders, e.g. MacOS spotlight files
if (!startsWith(MyfileName.c_str(), (char *) "/.")) {
if (!MyfileName.startsWith("/.")) {
JsonObject entry = obj.createNestedObject();
convertAsciiToUtf8(MyfileName.c_str(), filePath);
std::string path = filePath;
std::string fileName = path.substr(path.find_last_of("/") + 1);
entry["name"] = fileName;
entry["name"] = MyfileName.substring(MyfileName.lastIndexOf('/') + 1);
if (isDir) {
entry["dir"].set(true);
}
Expand Down Expand Up @@ -1502,6 +1519,16 @@ void explorerHandleAudioRequest(AsyncWebServerRequest *request) {
request->send(200);
}

// Handles track progress requests
void handleTrackProgressRequest(AsyncWebServerRequest *request) {
String json = "{\"trackProgress\":{";
json += "\"posPercent\":" + String(gPlayProperties.currentRelPos);
json += ",\"time\":" + String(AudioPlayer_GetCurrentTime());
json += ",\"duration\":" + String(AudioPlayer_GetFileDuration());
json += "}}";
request->send(200, "application/json", json);
}

void handleGetSavedSSIDs(AsyncWebServerRequest *request) {
AsyncJsonResponse *response = new AsyncJsonResponse(true);
JsonArray json_ssids = response->getRoot();
Expand Down

0 comments on commit 6ce9edc

Please sign in to comment.