Access audio buffer mutually exclusive using state variable 48/258848/1 tizen_6.0
authorJi-hoon Lee <dalton.lee@samsung.com>
Wed, 26 May 2021 12:53:39 +0000 (21:53 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Wed, 26 May 2021 12:53:54 +0000 (21:53 +0900)
The previous implementation replies on the thread's
joinable value, which is not set back to false
if the thread exits but no join() gets called.
For this reason, use a dedicated state variable
instead of joinable state, which correctly reflects
the active state of the streaming thread.

Change-Id: I1596a4649109011f8acb412896bfe9fdbefb7e05

plugins/wakeup-manager/inc/wakeup_audio_manager.h
plugins/wakeup-manager/src/wakeup_audio_manager.cpp
plugins/wakeup-manager/src/wakeup_manager_wrapper.cpp

index 2c8ac66..16d4059 100644 (file)
@@ -107,6 +107,7 @@ private:
 
        thread mStreamingThread;
        atomic_bool mStopStreamingThread{false};
+       atomic_bool mStreamingThreadActive{false};
 
        thread mStreamingPreviousThread;
        atomic_bool mStopStreamingPreviousThread{false};
index 88dc53a..78e4eec 100644 (file)
@@ -222,6 +222,8 @@ void CAudioManager::streaming_audio_data_thread_func(long long start_time)
 {
        MWR_LOGI("[ENTER]");
 
+       mStreamingThreadActive.store(true);
+
        unique_lock<mutex> lock(mMutex, defer_lock);
        bool finish_event_sent = false;
 
@@ -282,6 +284,7 @@ void CAudioManager::streaming_audio_data_thread_func(long long start_time)
                                                        }
                                                }
                                        }
+                                       mStreamingThreadActive.store(false);
                                        return;
                                }
                                cnt++;
@@ -337,6 +340,7 @@ void CAudioManager::streaming_audio_data_thread_func(long long start_time)
                        }
                }
        }
+       mStreamingThreadActive.store(false);
        MWR_LOGI("[EXIT]");
 }
 
@@ -350,10 +354,22 @@ void CAudioManager::add_audio_data(mas_speech_data& data, long long time)
        data_with_time.data = data;
        data_with_time.time = time;
 
+       static unsigned int num = 0;
+       const std::chrono::seconds interval(3);
+       static auto last = std::chrono::steady_clock::now();
+       auto now = std::chrono::steady_clock::now();
+       if (now - last > interval) {
+               MWR_LOGI("Feeding audio data : num(%d), now(%" PRId64 "), %d %zu",
+                       num, now.time_since_epoch().count(),
+                       mStreamingThreadActive.load(), mAudioData.size());
+               last = now;
+       }
+       num++;
+
        lock_guard<mutex> lock(mMutex);
 
        /* Pop items only when the streaming is not activated */
-       if (!mStreamingThread.joinable()) {
+       if (!mStreamingThreadActive.load()) {
                while(false == mAudioData.empty() && mAudioData.front().time < time - delta) {
                        const auto &front = mAudioData.front();
                        if (front.data.buffer) {
index da02392..5642bb5 100644 (file)
@@ -744,17 +744,6 @@ int wakeup_manager_feed_audio_data(mas_speech_streaming_event_e event, void* buf
 {
        if (nullptr == g_wakeup_manager) return -1;
 
-       static unsigned int num = 0;
-       const std::chrono::seconds interval(5);
-       static auto last = std::chrono::steady_clock::now();
-       auto now = std::chrono::steady_clock::now();
-       if (now - last > interval) {
-               MWR_LOGI("Feeding audio data : num(%d), now(%" PRId64 ")",
-                       num, now.time_since_epoch().count());
-               last = now;
-       }
-       num++;
-
        g_wakeup_manager->feed_audio_data(event, buffer, len);
 
        return 0;