Restore background volume if last requester gets deactivated 75/265775/1
authorJi-hoon Lee <dalton.lee@samsung.com>
Thu, 28 Oct 2021 07:53:22 +0000 (16:53 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Thu, 28 Oct 2021 07:58:51 +0000 (16:58 +0900)
Change-Id: I8e70185d093741ff87a7084a915d65453c385f0d

inc/service_main.h
src/service_config.cpp
src/service_main.cpp

index cf947e6..168b59f 100644 (file)
@@ -193,6 +193,8 @@ private:
 
        IApplicationManager& mApplicationManager;
        IPreferenceManager& mPreferenceManager;
+
+       pid_t mLastDuckingRequester{-1};
 };
 
 #endif /* __SERVICE_MAIN_H__ */
index b035179..efd5682 100644 (file)
@@ -250,8 +250,9 @@ int CServiceConfig::get_assistant_info(service_config_assistant_info_cb callback
                while (NULL != (dir = readdir(d))) {
                        if (suffix && strlen(suffix) <= strlen(dir->d_name)) {
                                if (0 == strcmp(dir->d_name + strlen(dir->d_name) - strlen(suffix), suffix)) {
-                                       char fullpath[_POSIX_PATH_MAX];
-                                       snprintf(fullpath, _POSIX_PATH_MAX - 1, "%s/%s", MA_ASSISTANT_INFO, dir->d_name);
+                                       const size_t path_max = _POSIX_PATH_MAX * 2;
+                                       char fullpath[path_max];
+                                       snprintf(fullpath, path_max - 1, "%s/%s", MA_ASSISTANT_INFO, dir->d_name);
                                        MAS_LOGI("Parsing file : %s\n", fullpath);
                                        parse_assistant_info(callback, fullpath, user_data);
                                }
index 0e616fa..8a401c1 100644 (file)
 #include <string.h>
 #include <glib.h>
 
+#include <chrono>
+#include <ctime>
+#include <list>
+#include <sstream>
+#include <tuple>
+
 #include "service_common.h"
 #include "service_main.h"
 #include "service_plugin.h"
@@ -316,13 +322,48 @@ int CServiceMain::client_set_assistant_specific_command(pid_t pid, const char *c
 
 int CServiceMain::client_set_background_volume(pid_t pid, double ratio)
 {
-       if (!is_current_assistant(pid)) return -1;
+       bool valid = is_current_assistant(pid);
 
        std::string pid_appid;
        boost::optional<std::string> appid_by_pid = mApplicationManager.get_appid_by_pid(pid);
        if (appid_by_pid) {
                pid_appid = *appid_by_pid;
        }
+
+       /* Better extracting this into a new Ducking Management class */
+       const int max_history_size = 5;
+       static std::list<std::tuple<pid_t, std::string, double, std::time_t, bool>> history;
+       history.push_front(std::make_tuple(pid, pid_appid, ratio,
+               std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()), valid));
+       if (history.size() > max_history_size) history.pop_back();
+
+       std::stringstream ss;
+       for (auto item : history) {
+               std::time_t time_info = std::get<3>(item);
+               char time_string[32];
+               std::strftime(time_string, sizeof(time_string), "%H%M%S", std::localtime(&time_info));
+               ss << "[";
+               ss << std::get<0>(item);
+               ss << ",";
+               ss << std::get<1>(item);
+               ss << ",";
+               ss << std::get<2>(item);
+               ss << ",";
+               ss << time_string;
+               ss << ",";
+               ss << std::get<4>(item);
+               ss << "]";
+       }
+       MAS_LOGW("History : %s", ss.str().c_str());
+
+       if (!valid) return -1;
+
+       if (ratio < 1.0f) {
+               mLastDuckingRequester = pid;
+       } else {
+               mLastDuckingRequester = -1;
+       }
+
        mServicePlugin.set_background_volume(pid_appid.c_str(), ratio);
        return 0;
 }
@@ -950,6 +991,11 @@ int CServiceMain::set_current_client_by_wakeup_word(const char *wakeup_word)
                if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
                        pid_t pid = get_client_pid_by_appid(items[prev_selection].appid);
                        mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_INACTIVE);
+                       if (mLastDuckingRequester == pid) {
+                               MAS_LOGE("Last ducking requester is deactivated, resetting background volume");
+                               mServicePlugin.set_background_volume(items[prev_selection].appid, 1.0f);
+                               mLastDuckingRequester = -1;
+                       }
                }
        }
 
@@ -978,6 +1024,11 @@ int CServiceMain::set_current_client_by_appid(const char *appid)
                if (prev_selection >= 0 && prev_selection < MAX_MACLIENT_INFO_NUM) {
                        pid_t pid = get_client_pid_by_appid(items[prev_selection].appid);
                        mServiceIpc.change_active_state(pid, MA_ACTIVE_STATE_INACTIVE);
+                       if (mLastDuckingRequester == pid) {
+                               MAS_LOGE("Last ducking requester is deactivated, resetting background volume");
+                               mServicePlugin.set_background_volume(items[prev_selection].appid, 1.0f);
+                               mLastDuckingRequester = -1;
+                       }
                }
        }
 
@@ -1388,7 +1439,15 @@ int CServiceMain::on_initialize(pid_t pid) {
 
 int CServiceMain::on_deinitialize(pid_t pid) {
        MAS_LOGD("[Enter] pid(%d)", pid);
+       if (mLastDuckingRequester == pid) {
+               MAS_LOGE("Last ducking requester has disconnected, resetting background volume");
+               std::string pid_appid = mClientManager.find_client_appid_by_pid( pid);
+               mServicePlugin.set_background_volume(pid_appid.c_str(), 1.0f);
+               mLastDuckingRequester = -1;
+       }
+
        mClientManager.destroy_client_by_pid(pid);
+
        return 0;
 }