Skip to content

Commit fdac4b4

Browse files
authored
Merge pull request #293 from ainyan03/mic_isrecording_race
Report a recording as pending from the moment it is requested
2 parents 8745301 + 7917eba commit fdac4b4

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

‎src/utility/Mic_Class.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ if (_cfg.pin_bck < 0 || _cfg.pin_ws < 0) {
584584
dst_remain = current_rec->length;
585585
if (dst_remain == 0)
586586
{
587-
self->_is_recording = false;
588587
ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
589588
src_idx = ~0u;
590589
src_len = 0;
@@ -593,7 +592,6 @@ if (_cfg.pin_bck < 0 || _cfg.pin_ws < 0) {
593592
continue;
594593
}
595594
}
596-
self->_is_recording = true;
597595

598596
for (;;)
599597
{
@@ -702,7 +700,6 @@ if (_cfg.pin_bck < 0 || _cfg.pin_ws < 0) {
702700
}
703701
}
704702
}
705-
self->_is_recording = false;
706703
_i2s_stop(self->_cfg.i2s_port);
707704

708705
self->_task_handle = nullptr;
@@ -736,13 +733,16 @@ if (_cfg.pin_bck < 0 || _cfg.pin_ws < 0) {
736733
#if portNUM_PROCESSORS > 1
737734
if (_cfg.task_pinned_core < portNUM_PROCESSORS)
738735
{
739-
xTaskCreatePinnedToCore(mic_task, "mic_task", stack_size, this, _cfg.task_priority, &_task_handle, _cfg.task_pinned_core);
736+
res = (pdPASS == xTaskCreatePinnedToCore(mic_task, "mic_task", stack_size, this, _cfg.task_priority, &_task_handle, _cfg.task_pinned_core));
740737
}
741738
else
742739
#endif
743740
{
744-
xTaskCreate(mic_task, "mic_task", stack_size, this, _cfg.task_priority, &_task_handle);
741+
res = (pdPASS == xTaskCreate(mic_task, "mic_task", stack_size, this, _cfg.task_priority, &_task_handle));
745742
}
743+
// end() takes the driver and the callback back down; it still sees the
744+
// class as running, which is what lets it do that.
745+
if (!res) { end(); }
746746
}
747747

748748
return res;
@@ -758,6 +758,10 @@ if (_cfg.pin_bck < 0 || _cfg.pin_ws < 0) {
758758
do { vTaskDelay(1); } while (_task_handle);
759759
}
760760

761+
// an unfinished request would otherwise keep isRecording() reporting a recording.
762+
_rec_info[0] = recording_info_t();
763+
_rec_info[1] = recording_info_t();
764+
761765
if (_cb_set_enabled) { _cb_set_enabled(_cb_set_enabled_args, false); }
762766
_i2s_driver_uninstall(_cfg.i2s_port);
763767
}

‎src/utility/Mic_Class.hpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ namespace m5
114114

115115
/// now in recording or not.
116116
/// @return 0=not recording / 1=recording (There's room in the queue) / 2=recording (There's no room in the queue.)
117-
size_t isRecording(void) const { return _is_recording ? ((bool)_rec_info[0].length) + ((bool)_rec_info[1].length) : 0; }
117+
size_t isRecording(void) const volatile { return ((bool)_rec_info[0].length) + ((bool)_rec_info[1].length); }
118118

119119
/// set recording sampling rate.
120120
/// @param sample_rate the sampling rate (Hz)
@@ -186,7 +186,6 @@ namespace m5
186186

187187
int32_t _offset = 0;
188188
volatile bool _task_running = false;
189-
volatile bool _is_recording = false;
190189
#if defined (SDL_h_)
191190
SDL_Thread* _task_handle = nullptr;
192191
#else

‎src/utility/Speaker_Class.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,20 +930,24 @@ namespace m5
930930
_task_running = true;
931931
#if defined (SDL_h_)
932932
_task_handle = SDL_CreateThread(reinterpret_cast<SDL_ThreadFunction>(spk_task), "spk_task", this);
933+
res = (_task_handle != nullptr);
933934
#else
934935
size_t stack_size = 1280 + (_cfg.dma_buf_len * sizeof(uint32_t));
935936

936937
#if portNUM_PROCESSORS > 1
937938
if (_cfg.task_pinned_core < portNUM_PROCESSORS)
938939
{
939-
xTaskCreatePinnedToCore(spk_task, "spk_task", stack_size, this, _cfg.task_priority, &_task_handle, _cfg.task_pinned_core);
940+
res = (pdPASS == xTaskCreatePinnedToCore(spk_task, "spk_task", stack_size, this, _cfg.task_priority, &_task_handle, _cfg.task_pinned_core));
940941
}
941942
else
942943
#endif
943944
{
944-
xTaskCreate(spk_task, "spk_task", stack_size, this, _cfg.task_priority, &_task_handle);
945+
res = (pdPASS == xTaskCreate(spk_task, "spk_task", stack_size, this, _cfg.task_priority, &_task_handle));
945946
}
946947
#endif
948+
// end() takes the driver and the callback back down; it still sees the
949+
// class as running, which is what lets it do that.
950+
if (!res) { end(); }
947951
}
948952

949953
return res;

0 commit comments

Comments
 (0)