Add minimum flush time constraint
[platform/core/uifw/multi-assistant-service.git] / src / service_ipc_dbus.cpp
index a816966..a0973f6 100644 (file)
@@ -19,6 +19,8 @@
 #include <dlfcn.h>
 #include <message_port.h>
 
+#include <chrono>
+
 #include "service_common.h"
 #include "service_main.h"
 #include "service_ipc_dbus.h"
@@ -206,6 +208,16 @@ static void masc_message_port_error(int error)
                "MESSAGE_PORT_ERROR_UNKNOWN");
 }
 
+static long long get_current_milliseconds_after_epoch()
+{
+       auto now = std::chrono::steady_clock::now();
+       auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
+       /* number of milliseconds since the epoch of system_clock */
+       auto value = now_ms.time_since_epoch();
+
+       return value.count();
+}
+
 int CServiceIpcDbus::send_streaming_audio_data(pid_t pid, int event, void* data, unsigned int data_size)
 {
        if (nullptr == mClientManager) {
@@ -234,13 +246,19 @@ int CServiceIpcDbus::send_streaming_audio_data(pid_t pid, int event, void* data,
        memcpy(buffer + total_size, data, data_size);
        total_size += data_size;
 
+       const long long minimum_flush_interval = 20;
+       static long long last_flush_time = get_current_milliseconds_after_epoch();
+       long long current_time = get_current_milliseconds_after_epoch();
+
        static int last_serial_waiting_for_flush = -1;
        if (0 == header.streaming_data_serial % 50) {
                last_serial_waiting_for_flush = header.streaming_data_serial;
                MAS_LOGI("queueing streaming data, serial : %d", last_serial_waiting_for_flush);
        }
        if (pending_buffer_size + total_size > STREAMING_BUFFER_SIZE ||
-               MAS_SPEECH_STREAMING_EVENT_FINISH == event) {
+               MAS_SPEECH_STREAMING_EVENT_FINISH == event ||
+               current_time - last_flush_time > minimum_flush_interval) {
+               last_flush_time = current_time;
                bundle *b = bundle_create();
                if (b) {
                        bundle_add_byte(b, "content", pending_buffer, pending_buffer_size);