Disable popping from audio buffer when streaming is activated 99/258199/1
authorJi-hoon Lee <dalton.lee@samsung.com>
Tue, 11 May 2021 07:42:21 +0000 (16:42 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Wed, 12 May 2021 06:21:12 +0000 (15:21 +0900)
Streaming thread reads content of audio buffer, so when the
streaming thread is joinable, modifying the buffer itself
can invalidate iterators associated with the buffer container,
which might cause an undefined behavior. For this reason,
limiting the maximum size of the buffer by popping from it
should be disabled when streaming thread is joinable.

Change-Id: I7cea5460cda6abdbee770c6b2818861a35f0080b

plugins/wakeup-manager/src/wakeup_audio_manager.cpp

index 744bb6074c39b2b15f2956d95017835bc86f7a67..88dc53a53ee1d61b0d50882d914688b524602198 100644 (file)
@@ -207,6 +207,17 @@ void CAudioManager::streaming_previous_audio_data_thread_func()
        MWR_LOGI("[EXIT]");
 }
 
+static void validate_audio_data_event_field(const mas_speech_data &data)
+{
+       if (data.event == MAS_SPEECH_STREAMING_EVENT_CONTINUE ||
+               data.event == MAS_SPEECH_STREAMING_EVENT_START ||
+               data.event == MAS_SPEECH_STREAMING_EVENT_FINISH ||
+               data.event == MAS_SPEECH_STREAMING_EVENT_FAIL) {
+               return;
+       }
+       MWR_LOGE("mas_speech_data has event field : %d", data.event);
+}
+
 void CAudioManager::streaming_audio_data_thread_func(long long start_time)
 {
        MWR_LOGI("[ENTER]");
@@ -292,6 +303,7 @@ void CAudioManager::streaming_audio_data_thread_func(long long start_time)
                        mas_speech_data& speech_data = iter->data;
                        for (const auto& observer : observers) {
                                if (observer) {
+                                       validate_audio_data_event_field(speech_data);
                                        if (!observer->on_streaming_audio_data(
                                                speech_data.event, speech_data.buffer, speech_data.len)) {
                                                LOGE("[Recorder WARNING] One of the observer returned false");
@@ -341,13 +353,16 @@ void CAudioManager::add_audio_data(mas_speech_data& data, long long time)
        lock_guard<mutex> lock(mMutex);
 
        /* Pop items only when the streaming is not activated */
-       while(false == mAudioData.empty() && mAudioData.front().time < time - delta) {
-               const auto &front = mAudioData.front();
-               if (front.data.buffer) {
-                       vm_free_simple(front.data.buffer);
+       if (!mStreamingThread.joinable()) {
+               while(false == mAudioData.empty() && mAudioData.front().time < time - delta) {
+                       const auto &front = mAudioData.front();
+                       if (front.data.buffer) {
+                               vm_free_simple(front.data.buffer);
+                       }
+                       mAudioData.pop_front();
                }
-               mAudioData.pop_front();
        }
+       validate_audio_data_event_field(data);
        mAudioData.push_back(data_with_time);
 }