Add streaming event validation code
authorJi-hoon Lee <dalton.lee@samsung.com>
Thu, 2 May 2019 05:31:46 +0000 (14:31 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Thu, 2 May 2019 05:45:07 +0000 (14:45 +0900)
Change-Id: I6eafbcb279c75d2e9bba657aaceccf91a4ef1985

src/multi_assistant_service_plugin.c

index a78135a..02ae392 100644 (file)
@@ -202,20 +202,77 @@ static void __wakeup_event_cb(wakeup_event_info event, void* user_data)
 
 }
 
+static bool __validate_streaming_event_order(int pid, wakeup_speech_streaming_event_e *event)
+{
+       bool ret = false;
+
+       static int previous_pid = -1;
+       static wakeup_speech_streaming_event_e previous_event = WAKEUP_SPEECH_STREAMING_EVENT_FINISH;
+
+       if (NULL == event) return false;
+
+       wakeup_speech_streaming_event_e expected_sequence [][2] = {
+               {WAKEUP_SPEECH_STREAMING_EVENT_START, WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE},
+               {WAKEUP_SPEECH_STREAMING_EVENT_START, WAKEUP_SPEECH_STREAMING_EVENT_FINISH},
+               {WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE, WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE},
+               {WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE, WAKEUP_SPEECH_STREAMING_EVENT_FINISH},
+               {WAKEUP_SPEECH_STREAMING_EVENT_FINISH, WAKEUP_SPEECH_STREAMING_EVENT_START},
+       };
+
+       if (pid != previous_pid) {
+               /* When sending streaming event to a new client, it always sends START message first */
+               previous_event = WAKEUP_SPEECH_STREAMING_EVENT_FINISH;
+       }
+
+       for (int loop = 0;loop < sizeof(expected_sequence) / sizeof(expected_sequence[0]);loop++) {
+               if (previous_event == expected_sequence[loop][0] &&
+                       *event == expected_sequence[loop][1]) {
+                       ret = true;
+               }
+       }
+       if (!ret) {
+               /* In case of FINISH -> CONTINUE without START, simply modify current event value */
+               if (WAKEUP_SPEECH_STREAMING_EVENT_FINISH == previous_event &&
+                       WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE == *event) {
+                       *event = WAKEUP_SPEECH_STREAMING_EVENT_START;
+                       ret = true;
+
+                       MAS_LOGD("[WARNING] forcibly changed CONTINUE to START : %d -> %d (PID %d -> %d)",
+                               previous_event, *event, previous_pid, pid);
+               }
+       }
+
+       if (ret) {
+               previous_pid = pid;
+               previous_event = *event;
+       } else {
+               MAS_LOGE("[ERROR] State sequence validation failed : %d -> %d (PID %d -> %d)",
+                       previous_event, *event, previous_pid, pid);
+       }
+       return ret;
+}
+
 static void __audio_streaming_cb(wakeup_speech_streaming_event_e event, unsigned char* buffer, int len, void *user_data)
 {
        if (event == WAKEUP_SPEECH_STREAMING_EVENT_FAIL) {
                mas_client_send_recognition_result(0, MA_RECOGNITION_RESULT_EVENT_ERROR);
                return;
        }
-       MAS_LOGD( "[SUCCESS] __audio_streaming_cb is called, event(%d), buffer(%p), len(%d)", event, buffer, len);
+       static int count = 0;
+       if (event != WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE || count % 100 == 0) {
+               MAS_LOGD( "[SUCCESS] __audio_streaming_cb is called, event(%d), buffer(%p), len(%d)", event, buffer, len);
+       }
+       ++count;
+
        int pid = mas_get_current_client_pid();
        if (pid == -1) {
                MAS_LOGE("[ERROR] Fail to retrieve pid of current MA client");
        } else {
-               int ret = masc_dbus_send_streaming_audio_data(pid, event, buffer, len);
-               if (0 != ret) {
-                       MAS_LOGE("[ERROR] Fail to send speech data, ret(%d)", ret);
+               if (__validate_streaming_event_order(pid, &event)) {
+                       int ret = masc_dbus_send_streaming_audio_data(pid, event, buffer, len);
+                       if (0 != ret) {
+                               MAS_LOGE("[ERROR] Fail to send speech data, ret(%d)", ret);
+                       }
                }
        }