Skip to content

Commit bf47700

Browse files
authored
Merge pull request #458 from schreibfaul1/only_for_testing
make thread safe
2 parents f3e2404 + 18c3a70 commit bf47700

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/Audio.cpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Created on: Oct 26.2018
55
*
6-
* Version 2.0.7k
7-
* Updated on: Jan 08.2023
6+
* Version 2.0.8
7+
* Updated on: Jan 09.2023
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -150,6 +150,8 @@ uint32_t AudioBuffer::getReadPos() {
150150
//---------------------------------------------------------------------------------------------------------------------
151151
Audio::Audio(bool internalDAC /* = false */, uint8_t channelEnabled /* = I2S_DAC_CHANNEL_BOTH_EN */, uint8_t i2sPort) {
152152

153+
mutex_audio = xSemaphoreCreateMutex();
154+
153155
// build-in-DAC works only with ESP32 (ESP32-S3 has no build-in-DAC)
154156
// build-in-DAC last working Arduino Version: 2.0.0-RC2
155157
// possible values for channelEnabled are:
@@ -298,6 +300,7 @@ Audio::~Audio() {
298300
if(m_playlistBuff) {free(m_playlistBuff); m_playlistBuff = NULL;}
299301
i2s_driver_uninstall((i2s_port_t)m_i2s_num); // #215 free I2S buffer
300302
if(m_chbuf) {free(m_chbuf); m_chbuf = NULL;}
303+
vSemaphoreDelete(mutex_audio);
301304
}
302305
//---------------------------------------------------------------------------------------------------------------------
303306
void Audio::setDefaults() {
@@ -369,15 +372,19 @@ void Audio::setConnectionTimeout(uint16_t timeout_ms, uint16_t timeout_ms_ssl){
369372
bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
370373
// user and pwd for authentification only, can be empty
371374

375+
xSemaphoreTakeRecursive(mutex_audio, portMAX_DELAY);
376+
372377
if(host == NULL) {
373378
AUDIO_INFO("Hostaddress is empty");
379+
xSemaphoreGiveRecursive(mutex_audio);
374380
return false;
375381
}
376382

377383
uint16_t lenHost = strlen(host);
378384

379385
if(lenHost >= 512 - 10) {
380386
AUDIO_INFO("Hostaddress is too long");
387+
xSemaphoreGiveRecursive(mutex_audio);
381388
return false;
382389
}
383390

@@ -515,6 +522,7 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
515522
if(extension) {free(extension); extension = NULL;}
516523
if(l_host ) {free(l_host); l_host = NULL;}
517524
if(h_host ) {free(h_host); h_host = NULL;}
525+
xSemaphoreGiveRecursive(mutex_audio);
518526
return res;
519527
}
520528
//---------------------------------------------------------------------------------------------------------------------
@@ -671,7 +679,12 @@ bool Audio::connecttoSD(const char* path, uint32_t resumeFilePos) {
671679
//---------------------------------------------------------------------------------------------------------------------
672680
bool Audio::connecttoFS(fs::FS &fs, const char* path, uint32_t resumeFilePos) {
673681

674-
if(strlen(path)>255) return false;
682+
xSemaphoreTake(mutex_audio, portMAX_DELAY);
683+
684+
if(strlen(path)>255){
685+
xSemaphoreGive(mutex_audio);
686+
return false;
687+
}
675688

676689
m_resumeFilePos = resumeFilePos;
677690
char audioName[256];
@@ -698,6 +711,7 @@ bool Audio::connecttoFS(fs::FS &fs, const char* path, uint32_t resumeFilePos) {
698711

699712
if(!audiofile) {
700713
if(audio_info) {vTaskDelay(2); audio_info("Failed to open file for reading");}
714+
xSemaphoreGive(mutex_audio);
701715
return false;
702716
}
703717

@@ -731,11 +745,14 @@ bool Audio::connecttoFS(fs::FS &fs, const char* path, uint32_t resumeFilePos) {
731745
bool ret = initializeDecoder();
732746
if(ret) m_f_running = true;
733747
else audiofile.close();
748+
xSemaphoreGive(mutex_audio);
734749
return ret;
735750
}
736751
//---------------------------------------------------------------------------------------------------------------------
737752
bool Audio::connecttospeech(const char* speech, const char* lang){
738753

754+
xSemaphoreTake(mutex_audio, portMAX_DELAY);
755+
739756
setDefaults();
740757
char host[] = "translate.google.com.vn";
741758
char path[] = "/translate_tts";
@@ -744,7 +761,11 @@ bool Audio::connecttospeech(const char* speech, const char* lang){
744761
uint16_t speechBuffLen = speechLen + 300;
745762
memcpy(m_lastHost, speech, 256);
746763
char* speechBuff = (char*)malloc(speechBuffLen);
747-
if(!speechBuff) {log_e("out of memory"); return false;}
764+
if(!speechBuff) {
765+
log_e("out of memory");
766+
xSemaphoreGive(mutex_audio);
767+
return false;
768+
}
748769
memcpy(speechBuff, speech, speechLen);
749770
speechBuff[speechLen] = '\0';
750771
urlencode(speechBuff, speechBuffLen);
@@ -769,6 +790,7 @@ bool Audio::connecttospeech(const char* speech, const char* lang){
769790
_client = static_cast<WiFiClient*>(&client);
770791
if(!_client->connect(host, 80)) {
771792
log_e("Connection failed");
793+
xSemaphoreGive(mutex_audio);
772794
return false;
773795
}
774796
_client->print(resp);
@@ -778,7 +800,7 @@ bool Audio::connecttospeech(const char* speech, const char* lang){
778800
m_f_ssl = false;
779801
m_f_tts = true;
780802
setDatamode(HTTP_RESPONSE_HEADER);
781-
803+
xSemaphoreGive(mutex_audio);
782804
return true;
783805
}
784806
//---------------------------------------------------------------------------------------------------------------------
@@ -822,6 +844,8 @@ bool Audio::connecttomarytts(const char* speech, const char* lang, const char* v
822844
// bits1-hsmm de female hmm
823845
// bits1 de female unitselection general
824846

847+
xSemaphoreTake(mutex_audio, portMAX_DELAY);
848+
825849
setDefaults();
826850
char host[] = "mary.dfki.de";
827851
char path[] = "/process";
@@ -831,7 +855,10 @@ bool Audio::connecttomarytts(const char* speech, const char* lang, const char* v
831855
uint16_t speechBuffLen = speechLen + 300;
832856
memcpy(m_lastHost, speech, 256);
833857
char* speechBuff = (char*)malloc(speechBuffLen);
834-
if(!speechBuff) {log_e("out of memory"); return false;}
858+
if(!speechBuff) {log_e("out of memory");
859+
xSemaphoreGive(mutex_audio);
860+
return false;
861+
}
835862
memcpy(speechBuff, speech, speechLen);
836863
speechBuff[speechLen] = '\0';
837864
urlencode(speechBuff, speechBuffLen);
@@ -861,6 +888,7 @@ bool Audio::connecttomarytts(const char* speech, const char* lang, const char* v
861888
_client = static_cast<WiFiClient*>(&client);
862889
if(!_client->connect(host, port)) {
863890
log_e("Connection failed");
891+
xSemaphoreGive(mutex_audio);
864892
return false;
865893
}
866894
_client->print(resp);
@@ -871,6 +899,7 @@ bool Audio::connecttomarytts(const char* speech, const char* lang, const char* v
871899
m_f_tts = true;
872900
setDatamode(HTTP_RESPONSE_HEADER);
873901

902+
xSemaphoreGive(mutex_audio);
874903
return true;
875904
}
876905
//---------------------------------------------------------------------------------------------------------------------
@@ -2071,6 +2100,7 @@ uint32_t Audio::stopSong() {
20712100
}
20722101
//---------------------------------------------------------------------------------------------------------------------
20732102
bool Audio::pauseResume() {
2103+
xSemaphoreTake(mutex_audio, portMAX_DELAY);
20742104
bool retVal = false;
20752105
if(getDatamode() == AUDIO_LOCALFILE || m_streamType == ST_WEBSTREAM) {
20762106
m_f_running = !m_f_running;
@@ -2080,6 +2110,7 @@ bool Audio::pauseResume() {
20802110
i2s_zero_dma_buffer((i2s_port_t) m_i2s_num);
20812111
}
20822112
}
2113+
xSemaphoreGive(mutex_audio);
20832114
return retVal;
20842115
}
20852116
//---------------------------------------------------------------------------------------------------------------------
@@ -2172,6 +2203,8 @@ void Audio::loop() {
21722203

21732204
if(!m_f_running) return;
21742205

2206+
xSemaphoreTake(mutex_audio, portMAX_DELAY);
2207+
21752208
if(m_playlistFormat != FORMAT_M3U8){ // normal process
21762209
switch(getDatamode()){
21772210
case AUDIO_LOCALFILE:
@@ -2243,6 +2276,7 @@ void Audio::loop() {
22432276
break;
22442277
}
22452278
}
2279+
xSemaphoreGive(mutex_audio);
22462280
}
22472281
//---------------------------------------------------------------------------------------------------------------------
22482282
bool Audio::readPlayListData() {
@@ -4044,10 +4078,12 @@ bool Audio::setTimeOffset(int sec){
40444078
}
40454079
//---------------------------------------------------------------------------------------------------------------------
40464080
bool Audio::setFilePos(uint32_t pos) {
4081+
xSemaphoreTakeRecursive(mutex_audio, portMAX_DELAY);
40474082
if(!audiofile) return false;
40484083
if(pos < m_audioDataStart) pos = m_audioDataStart; // issue #96
40494084
if(pos > m_file_size) pos = m_file_size;
40504085
m_resumeFilePos = pos;
4086+
xSemaphoreGiveRecursive(mutex_audio);
40514087
return true;
40524088
}
40534089
//---------------------------------------------------------------------------------------------------------------------

src/Audio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Audio.h
33
*
44
* Created on: Oct 28,2018
5-
* Updated on: Jan 08,2023
5+
* Updated on: Jan 09,2023
66
* Author: Wolle (schreibfaul1)
77
*/
88

@@ -164,7 +164,6 @@ class Audio : private AudioBuffer{
164164
~Audio();
165165
void setBufsize(int rambuf_sz, int psrambuf_sz);
166166
bool connecttohost(const char* host, const char* user = "", const char* pwd = "");
167-
168167
bool connecttospeech(const char* speech, const char* lang);
169168
bool connecttomarytts(const char* speech, const char* lang, const char* voice);
170169
bool connecttoFS(fs::FS &fs, const char* path, uint32_t resumeFilePos = 0);
@@ -465,6 +464,7 @@ class Audio : private AudioBuffer{
465464
WiFiClient client; // @suppress("Abstract class cannot be instantiated")
466465
WiFiClientSecure clientsecure; // @suppress("Abstract class cannot be instantiated")
467466
WiFiClient* _client = nullptr;
467+
SemaphoreHandle_t mutex_audio;
468468
i2s_config_t m_i2s_config = {}; // stores values for I2S driver
469469
i2s_pin_config_t m_pin_config = {};
470470
std::vector<char*> m_playlistContent; // m3u8 playlist buffer

0 commit comments

Comments
 (0)