Add host volume set/get internal api 00/273100/8
authorSeungbae Shin <seungbae.shin@samsung.com>
Wed, 30 Mar 2022 11:48:44 +0000 (20:48 +0900)
committerSeungbae Shin <seungbae.shin@samsung.com>
Mon, 4 Apr 2022 06:37:04 +0000 (15:37 +0900)
these APIs can be used for container environment

[Version] 0.6.44
[Issue Type] Internal API

Change-Id: Idfd85201fa44ed0bc41a3320ad6bfade47ebfa2d

include/sound_manager_internal.h
include/sound_manager_private.h
packaging/capi-media-sound-manager.spec
src/sound_manager_internal.c
src/sound_manager_private.c
test/sound_manager_test.c

index 2e45dfb02c4f930e3efc1cdcb43cf9d9d26a1721..a9be2b1037fd5eb791b898ce7ec1d4971adfc792 100644 (file)
@@ -1275,6 +1275,36 @@ int sound_manager_set_rpi_playback_route(sound_rpi_playback_route_type type);
  */
 int sound_manager_get_rpi_playback_route(sound_rpi_playback_route_type *type);
 
+
+/**
+ * @brief Sets the host volume level specified for a particular sound type.
+ * @since_tizen 7.0
+ * @privlevel public
+ * @privilege %http://tizen.org/privilege/volume.set
+ * @param[in]  type    The sound type
+ * @param[in]  volume  The volume level to be set
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #SOUND_MANAGER_ERROR_NONE Success
+ * @retval #SOUND_MANAGER_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #SOUND_MANAGER_ERROR_PERMISSION_DENIED Permission denied
+ * @retval #SOUND_MANAGER_ERROR_INTERNAL Internal error inside the sound system
+ */
+int sound_manager_set_host_volume(sound_type_e type, int volume);
+
+/**
+ * @brief Gets the host volume level specified for a particular sound type.
+ * @since_tizen 7.0
+ * @param[in]  type    The sound type
+ * @param[out] volume  The current volume level
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #SOUND_MANAGER_ERROR_NONE Success
+ * @retval #SOUND_MANAGER_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #SOUND_MANAGER_ERROR_INTERNAL Internal error inside the sound system
+ */
+int sound_manager_get_host_volume(sound_type_e type, int *volume);
+
 /**
  * @}
  */
index fc9611d4165b3ee2dffe22c58eb11af4557eb8ce..6de1ff65d8f9816ed2c1ac7dc234504114b0d0c3 100644 (file)
@@ -399,6 +399,10 @@ int _set_rpi_playback_route(sound_rpi_playback_route_type type);
 
 int _get_rpi_playback_route(sound_rpi_playback_route_type *type);
 
+int _set_host_volume_level(const char *direction, const char *volume_type, unsigned int level);
+
+int _get_host_volume_level(const char *direction, const char *volume_type, unsigned int *level);
+
 #ifdef __cplusplus
 }
 #endif
index bc42b57d3519009bf895cf8f9cfdcd5ff934ca3e..3567f8b77f221d13a780155d478dd001a9b99b25 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-sound-manager
 Summary:    Sound Manager library
-Version:    0.6.43
+Version:    0.6.44
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index 42402c530b9d2d2cc64f74f1a6604acaf9626d66..2ec8673e5662d9af90f0bcc78754798abcbbb706 100644 (file)
@@ -917,3 +917,44 @@ int sound_manager_get_rpi_playback_route(sound_rpi_playback_route_type *type)
 {
        return _get_rpi_playback_route(type);
 }
+
+int sound_manager_set_host_volume(sound_type_e type, int volume)
+{
+       int ret = SOUND_MANAGER_ERROR_NONE;
+       const char *volume_type = NULL;
+
+       SM_ARG_CHECK(type < SOUND_TYPE_NUM);
+
+       LOGI("type[%d] volume[%d]", type, volume);
+
+       ret = _convert_sound_type(type, &volume_type);
+       if (ret != SOUND_MANAGER_ERROR_NONE)
+               return ret;
+
+       ret = _set_host_volume_level(DIRECTION_OUT_STR, volume_type, (unsigned int)volume);
+
+       return _convert_sound_manager_error_code(__func__, ret);
+}
+
+int sound_manager_get_host_volume(sound_type_e type, int *volume)
+{
+       int ret = SOUND_MANAGER_ERROR_NONE;
+       const char *volume_type = NULL;
+       unsigned int volume_level = 0;
+
+       SM_ARG_CHECK(volume);
+
+       LOGI("type[%d]", type);
+
+       ret = _convert_sound_type(type, &volume_type);
+       if (ret != SOUND_MANAGER_ERROR_NONE)
+               return ret;
+
+       ret = _get_host_volume_level(DIRECTION_OUT_STR, volume_type, &volume_level);
+       if (ret == SOUND_MANAGER_ERROR_NONE) {
+               *volume = (int)volume_level;
+               LOGI("host volume[%d]", *volume);
+       }
+
+       return _convert_sound_manager_error_code(__func__, ret);
+}
index ba1479e1a8e7e60760251bfb4e30136a1c074a29..ef739cd94aa5f22b58daee4b2b6d918219ba547c 100644 (file)
@@ -3797,4 +3797,108 @@ LEAVE:
 
        return ret;
 }
-//LCOV_EXCL_STOP
+
+#define CONTAINER_FILE "/run/systemd/container"
+#define DBUS_HOST_SYSTEM_BUS_ADDRESS "unix:path=/run/host/dbus/system_bus_socket"
+
+static GDBusConnection * __get_host_dbus_connection()
+{
+       GDBusConnection *conn = NULL;
+
+       g_autoptr(GError) err = NULL;
+
+       if (access(CONTAINER_FILE, F_OK) != 0) {
+               LOGE("Not in container....");
+               return NULL;
+       }
+
+       conn = g_dbus_connection_new_for_address_sync(DBUS_HOST_SYSTEM_BUS_ADDRESS,
+                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
+                               NULL, NULL, &err);
+       if (!conn)
+               LOGE("g_dbus_connection_new_for_address_sync() error (%s)", err ? err->message : "(null)");
+
+       return conn;
+}
+
+int _set_host_volume_level(const char *direction, const char *volume_type, unsigned int level)
+{
+       int ret = SOUND_MANAGER_ERROR_NONE;
+       const gchar *dbus_ret = NULL;
+
+       g_autoptr(GVariant) result = NULL;
+       g_autoptr(GDBusConnection) conn = NULL;
+       g_autoptr(GError) err = NULL;
+
+       SM_ARG_CHECK(direction);
+       SM_ARG_CHECK(volume_type);
+
+       if (!(conn = __get_host_dbus_connection()))
+               return SOUND_MANAGER_ERROR_INVALID_OPERATION;
+
+       result = g_dbus_connection_call_sync(conn,
+                                                       PA_BUS_NAME,
+                                                       PA_STREAM_MANAGER_OBJECT_PATH,
+                                                       PA_STREAM_MANAGER_INTERFACE,
+                                                       PA_STREAM_MANAGER_METHOD_NAME_SET_VOLUME_LEVEL,
+                                                       g_variant_new("(ssu)", direction, volume_type, level),
+                                                       G_VARIANT_TYPE("(s)"),
+                                                       G_DBUS_CALL_FLAGS_NONE,
+                                                       DBUS_METHOD_TIMEOUT,
+                                                       NULL,
+                                                       &err);
+       if (!result)
+               return _convert_sound_manager_error_code(__func__, _convert_dbus_error(err ? err->message : NULL));
+
+       g_variant_get(result, "(&s)", &dbus_ret);
+
+       if (strncmp("STREAM_MANAGER_RETURN_OK", dbus_ret, strlen(dbus_ret)))
+               ret = SOUND_MANAGER_ERROR_INTERNAL;
+
+       LOGI("dbus_ret[%s] ret[0x%x]", dbus_ret, ret);
+
+       return ret;
+}
+
+int _get_host_volume_level(const char *direction, const char *volume_type, unsigned int *level)
+{
+       int ret = SOUND_MANAGER_ERROR_NONE;
+       const gchar *dbus_ret = NULL;
+
+       g_autoptr(GVariant) result = NULL;
+       g_autoptr(GDBusConnection) conn = NULL;
+       g_autoptr(GError) err = NULL;
+
+       SM_ARG_CHECK(direction);
+       SM_ARG_CHECK(volume_type);
+       SM_ARG_CHECK(level);
+
+       if (!(conn = __get_host_dbus_connection()))
+               return SOUND_MANAGER_ERROR_INVALID_OPERATION;
+
+       result = g_dbus_connection_call_sync(conn,
+                                                       PA_BUS_NAME,
+                                                       PA_STREAM_MANAGER_OBJECT_PATH,
+                                                       PA_STREAM_MANAGER_INTERFACE,
+                                                       PA_STREAM_MANAGER_METHOD_NAME_GET_VOLUME_LEVEL,
+                                                       g_variant_new("(ss)", direction, volume_type),
+                                                       G_VARIANT_TYPE("(us)"),
+                                                       G_DBUS_CALL_FLAGS_NONE,
+                                                       DBUS_METHOD_TIMEOUT,
+                                                       NULL,
+                                                       &err);
+       if (!result)
+               return _convert_sound_manager_error_code(__func__, _convert_dbus_error(err ? err->message : NULL));
+
+       g_variant_get(result, "(u&s)", level, &dbus_ret);
+
+       if (strncmp("STREAM_MANAGER_RETURN_OK", dbus_ret, strlen(dbus_ret)))
+               ret = SOUND_MANAGER_ERROR_INTERNAL;
+       else
+               LOGI("level[%u]", *level);
+
+       LOGI("dbus_ret[%s] ret[0x%x]", dbus_ret, ret);
+
+       return ret;
+}
+//LCOV_EXCL_STOP
\ No newline at end of file
index 6c9e74b31a2c1173a04127797d86e3c34d05c8ab..14eb9a5a19c1d2c16f8e53a97b716356947e9c76 100644 (file)
@@ -120,6 +120,8 @@ enum {
        CURRENT_STATUS_SET_REMOTE_PERMISSION,
        CURRENT_STATUS_SET_RPI_PLAYBACK_ROUTE,
        CURRENT_STATUS_GET_RPI_PLAYBACK_ROUTE,
+       CURRENT_STATUS_SET_HOST_VOLUME,
+       CURRENT_STATUS_GET_HOST_VOLUME,
 };
 
 
@@ -395,6 +397,10 @@ void _interpret_main_menu(char *cmd)
                g_menu_state = CURRENT_STATUS_SET_RPI_PLAYBACK_ROUTE;
        else if (strncmp(cmd, "grpr", MAX_CMD_LEN) == 0)
                g_menu_state = CURRENT_STATUS_GET_RPI_PLAYBACK_ROUTE;
+       else if (strncmp(cmd, "shv", MAX_CMD_LEN) == 0)
+               g_menu_state = CURRENT_STATUS_SET_HOST_VOLUME;
+       else if (strncmp(cmd, "ghv", MAX_CMD_LEN) == 0)
+               g_menu_state = CURRENT_STATUS_GET_HOST_VOLUME;
        else if (strncmp(cmd, "q", MAX_CMD_LEN) == 0) {
                g_print("closing the test suite\n");
                quit_program();
@@ -506,6 +512,8 @@ void display_sub_basic()
        g_print("alw. *Allow remote device\n");
        g_print("srpr. *Set RPI playback route\t");
        g_print("grpr. *Get RPI playback route\n");
+       g_print("shv. *Set HOST Volume\t");
+       g_print("ghv. *Get HOST Volume\n");
        g_print("                                                                 * is for internal usage.\n");
        g_print("=========================================================================================\n");
 }
@@ -708,6 +716,12 @@ static void displaymenu()
                g_print("*** input rpi playback route to set (0:auto, 1:headphone, 2:HDMI1, 3:HDMI2(rpi4 only)\n");
        else if (g_menu_state == CURRENT_STATUS_GET_RPI_PLAYBACK_ROUTE)
                g_print("*** press enter to get rpi playback route\n");
+       else if (g_menu_state == CURRENT_STATUS_SET_HOST_VOLUME) {
+               if (flag == 0)
+                       g_print("*** input sound type and desired volume level for HOST(0:SYSTEM 1:NOTIFICATION 2:ALARM 3:RINGTONE 4:MEDIA 5:CALL 6:VOIP 7:VOICE, (0~max volume))\n");
+               flag = 1;
+       } else if (g_menu_state == CURRENT_STATUS_GET_HOST_VOLUME)
+               g_print("*** input sound type for HOST(0:SYSTEM 1:NOTIFICATION 2:ALARM 3:RINGTONE 4:MEDIA 5:CALL 6:VOIP 7:VOICE)\n");
        else {
                g_print("*** unknown status.\n");
                quit_program();
@@ -2846,6 +2860,43 @@ static void interpret(char *cmd)
                reset_menu_state();
                break;
        }
+       case CURRENT_STATUS_SET_HOST_VOLUME: {
+               static int cnt = 0;
+               static sound_type_e type;
+               int volume;
+               switch (cnt) {
+               case 0:
+                       if (convert_sound_type(&type, cmd) == 1)
+                               cnt++;
+                       else
+                               reset_menu_state();
+                       break;
+               case 1:
+                       volume = atoi(cmd);
+                       if (sound_manager_set_host_volume(type, volume) != SOUND_MANAGER_ERROR_NONE)
+                               g_print("fail to set host volume(%d) check sound type(%d)'s available volume level\n", volume, type);
+                       else
+                               g_print("set host volume success : sound type(%d), volume(%d)\n", type, volume);
+                       cnt = 0;
+                       reset_menu_state();
+                       break;
+               default:
+                       break;
+               }
+               break;
+       }
+       case CURRENT_STATUS_GET_HOST_VOLUME: {
+               sound_type_e type;
+               int volume;
+               if (convert_sound_type(&type, cmd) == 1) {
+                       if (sound_manager_get_host_volume(type, &volume) != SOUND_MANAGER_ERROR_NONE)
+                               g_print("fail to get host volume\n");
+                       else
+                               g_print("current host volume of this type(%d) is : %d\n", type, volume);
+               }
+               reset_menu_state();
+               break;
+       }
        }
 end:
        g_timeout_add(100, timeout_menu_display, 0);