Skip to content

Commit ba8630b

Browse files
author
Sascha
committed
Fix playing a webstream while still connecting to WiFi
The code is simplified a lot. I removed the functions and variables that were never running anyway. The webstream command is now always accepted, and the error is handled inside the audio task. The audio task also blocks as long as a connection try is in progress. In addition to the code that did not work anyway, this should also fix the oversight that we want to immediately play a different webstream after starting, not only the last RFID.
1 parent bb559de commit ba8630b

File tree

5 files changed

+18
-31
lines changed

5 files changed

+18
-31
lines changed

src/AudioPlayer.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,18 @@ void AudioPlayer_Task(void *parameter) {
751751
audioReturnCode = false;
752752

753753
if (gPlayProperties.playMode == WEBSTREAM || (gPlayProperties.playMode == LOCAL_M3U && gPlayProperties.isWebstream)) { // Webstream
754-
audioReturnCode = audio->connecttohost(gPlayProperties.playlist->at(gPlayProperties.currentTrackNumber));
755-
gPlayProperties.playlistFinished = false;
756-
gTriedToConnectToHost = true;
754+
// wait for wlan to connect or fail to connect before continuing
755+
// NOTE: In the current implementation, the Wifi only tries to connect for a few seconds. So this should not block too long.
756+
while (Wlan_ConnectionTryInProgress()) {
757+
vTaskDelay(portTICK_PERIOD_MS * 100u);
758+
}
759+
if (!Wlan_IsConnected()) {
760+
Log_Println(webstreamNotAvailable, LOGLEVEL_ERROR);
761+
audioReturnCode = false;
762+
} else {
763+
audioReturnCode = audio->connecttohost(gPlayProperties.playlist->at(gPlayProperties.currentTrackNumber));
764+
gPlayProperties.playlistFinished = false;
765+
}
757766
} else if (gPlayProperties.playMode != WEBSTREAM && !gPlayProperties.isWebstream) {
758767
// Files from SD
759768
if (!gFSystem.exists(gPlayProperties.playlist->at(gPlayProperties.currentTrackNumber))) { // Check first if file/folder exists
@@ -1140,10 +1149,6 @@ void AudioPlayer_TrackQueueDispatcher(const char *_itemToPlay, const uint32_t _l
11401149

11411150
case WEBSTREAM: { // This is always just one "track"
11421151
Log_Println(modeWebstream, LOGLEVEL_NOTICE);
1143-
if (!Wlan_IsConnected()) {
1144-
Log_Println(webstreamNotAvailable, LOGLEVEL_ERROR);
1145-
error = true;
1146-
}
11471152
break;
11481153
}
11491154

src/Wlan.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -496,18 +496,6 @@ void handleWifiStateConnectionSuccess() {
496496
delete dnsServer;
497497
dnsServer = nullptr;
498498

499-
bool playLastRfidAfterReboot;
500-
#ifdef PLAY_LAST_RFID_AFTER_REBOOT
501-
playLastRfidAfterReboot = gPrefsSettings.getBool("playLastOnBoot", true);
502-
#else
503-
playLastRfidAfterReboot = gPrefsSettings.getBool("playLastOnBoot", false);
504-
#endif
505-
506-
if (playLastRfidAfterReboot && gPlayLastRfIdWhenWiFiConnected && gTriedToConnectToHost) {
507-
gPlayLastRfIdWhenWiFiConnected = false;
508-
recoverLastRfidPlayedFromNvs(true);
509-
}
510-
511499
wifiState = WIFI_STATE_CONNECTED;
512500
Mqtt_OnWifiConnected();
513501
}
@@ -720,7 +708,7 @@ bool Wlan_DeleteNetwork(String ssid) {
720708
}
721709

722710
bool Wlan_ConnectionTryInProgress(void) {
723-
return wifiState == WIFI_STATE_SCAN_CONN;
711+
return wifiState == WIFI_STATE_INIT || wifiState == WIFI_STATE_CONNECT_LAST || wifiState == WIFI_STATE_SCAN_CONN;
724712
}
725713

726714
String Wlan_GetIpAddress(void) {

src/main.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030

3131
#include <Wire.h>
3232

33-
bool gPlayLastRfIdWhenWiFiConnected = false;
34-
bool gTriedToConnectToHost = false;
35-
3633
static constexpr const char *logo = R"literal(
3734
_____ ____ ____ _
3835
| ____| / ___| | _ \ _ _ (_) _ __ ___
@@ -94,8 +91,8 @@ void recoverBootCountFromNvs(void) {
9491
}
9592

9693
// Get last RFID-tag applied from NVS
97-
void recoverLastRfidPlayedFromNvs(bool force) {
98-
if (recoverLastRfid || force) {
94+
void recoverLastRfidPlayedFromNvs() {
95+
if (recoverLastRfid) {
9996
if (System_GetOperationMode() == OPMODE_BLUETOOTH_SINK) { // Don't recover if BT-mode is desired
10097
recoverLastRfid = false;
10198
return;
@@ -106,7 +103,6 @@ void recoverLastRfidPlayedFromNvs(bool force) {
106103
Log_Println(unableToRestoreLastRfidFromNVS, LOGLEVEL_INFO);
107104
} else {
108105
xQueueSend(gRfidCardQueue, lastRfidPlayed.c_str(), 0);
109-
gPlayLastRfIdWhenWiFiConnected = !force;
110106
Log_Printf(LOGLEVEL_INFO, restoredLastRfidFromNVS, lastRfidPlayed.c_str());
111107
}
112108
}

src/main.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
#pragma once
22

3-
extern bool gPlayLastRfIdWhenWiFiConnected;
4-
extern bool gTriedToConnectToHost;
5-
6-
extern void recoverLastRfidPlayedFromNvs(bool force = false);
3+
extern void recoverLastRfidPlayedFromNvs();

src/revision.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
22

33
#include "gitrevision.h"
4-
constexpr const char softwareRevision[] = "Software-revision: 20250515-1-DEV";
4+
5+
constexpr const char softwareRevision[] = "Software-revision: 20250613-1-DEV";

0 commit comments

Comments
 (0)