Send dbus IPC directly to TonePlayer 61/233061/18 accepted/tizen/unified/20200617.055843 submit/tizen/20200608.021130 submit/tizen/20200610.005134 submit/tizen/20200610.094452 submit/tizen/20200615.061529 submit/tizen/20200616.024230 submit/tizen/20200616.100553
authorJaechul Lee <jcsing.lee@samsung.com>
Mon, 11 May 2020 04:51:00 +0000 (13:51 +0900)
committerJaechul Lee <jcsing.lee@samsung.com>
Thu, 4 Jun 2020 02:01:13 +0000 (11:01 +0900)
[Version] 0.2.4
[Issue Type] Improvement

Change-Id: I96f36d06193b7606136791e8737840340150f6fb
Signed-off-by: Jaechul Lee <jcsing.lee@samsung.com>
CMakeLists.txt
packaging/capi-media-tone-player.spec
src/tone_player.c
test/tone_player_test.c

index fdfff13..6ee540b 100755 (executable)
@@ -1,6 +1,6 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 
-SET(Services 
+SET(Services
         "application"
         "base"
         "content"
@@ -24,7 +24,7 @@ SET(service "media")
 SET(submodule "tone-player")
 
 # for package file
-SET(dependents "mm-sound dlog capi-base-common capi-media-sound-manager")
+SET(dependents "mm-sound dlog capi-base-common capi-media-sound-manager gio-2.0")
 SET(pc_dependents "capi-base-common capi-media-sound-manager")
 
 SET(fw_name "${project_prefix}-${service}-${submodule}")
@@ -93,10 +93,10 @@ IF(UNIX)
 
 ADD_CUSTOM_TARGET (distclean @echo cleaning for source distribution)
 ADD_CUSTOM_COMMAND(
-        DEPENDS clean 
+        DEPENDS clean
         COMMENT "distribution clean"
         COMMAND find
-        ARGS    . 
+        ARGS    .
         -not -name config.cmake -and \(
         -name tester.c -or
         -name Testing -or
index 07cfc20..85e8df0 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       capi-media-tone-player
 Summary:    A tone player library in Tizen C API
-Version:    0.2.3
+Version:    0.2.4
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
@@ -11,6 +11,7 @@ BuildRequires:  pkgconfig(mm-sound)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(capi-base-common)
 BuildRequires:  pkgconfig(capi-media-sound-manager)
+BuildRequires:  pkgconfig(gio-2.0)
 
 %description
 A tone player library in Tizen C API.
index e9daa57..d525a75 100755 (executable)
 * limitations under the License.
 */
 
-
-
-
 #define LOG_TAG "TIZEN_N_TONE_PLAYER"
 
 #include <sound_manager.h>
 #include <sound_manager_internal.h>
 #include <tone_player.h>
-#include <mm_sound.h>
-#include <mm_sound_private.h>
 #include <stdio.h>
 #include <limits.h>
 #include <string.h>
 #include <unistd.h>
 #include <dlog.h>
+#include <gio/gio.h>
+#include <glib.h>
+
+#define PA_BUS_NAME "org.pulseaudio.Server"
+#define PA_TONE_PLAYER_OBJECT_PATH "/org/pulseaudio/TonePlayer"
+#define PA_TONE_PLAYER_INTERFACE "org.pulseaudio.TonePlayer"
+
+#define PA_TONE_PLAYER_METHOD_NAME_TONE_PLAY "TonePlay"
+#define PA_TONE_PLAYER_METHOD_NAME_TONE_STOP "ToneStop"
 
-static int __convert_tone_player_error_code(const char *func, int code)
+static int _convert_dbus_error(const char *error_msg)
 {
-       int ret = TONE_PLAYER_ERROR_NONE;
-       char *errorstr = NULL;
-       switch (code) {
-       case MM_ERROR_NONE:
-               ret = TONE_PLAYER_ERROR_NONE;
-               errorstr = "ERROR_NONE";
-               break;
-       case TONE_PLAYER_ERROR_INVALID_PARAMETER:
-       case MM_ERROR_INVALID_ARGUMENT:
-       case MM_ERROR_SOUND_INVALID_POINTER:
-               ret = TONE_PLAYER_ERROR_INVALID_PARAMETER;
-               errorstr = "INVALID_PARAMETER";
-               break;
-       case MM_ERROR_SOUND_INTERNAL:
-               ret = TONE_PLAYER_ERROR_INVALID_OPERATION;
-               errorstr = "INVALID_OPERATION";
-               break;
+       if (error_msg && strstr(error_msg, "InvalidArgument"))
+               return TONE_PLAYER_ERROR_INVALID_PARAMETER;
+
+       return TONE_PLAYER_ERROR_INVALID_OPERATION;
+}
+
+static GDBusConnection *__get_dbus_connection(void)
+{
+       GDBusConnection *conn = NULL;
+       GError *err = NULL;
+
+       conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
+       if (!conn) {
+               LOGE("g_bus_get_sync() error (%s)", err->message);
+               g_error_free(err);
        }
-       LOGE("[%s] %s(0x%08x) : core frameworks error code(0x%08x)", func, errorstr, ret, code);
-       return ret;
+
+       return conn;
 }
 
+static int __dbus_method_call(const char *method, GVariant *param, GVariant **reply)
+{
+       GDBusConnection *conn = NULL;
+       GVariant * _reply = NULL;
+       GError *err = NULL;
+
+       if (!method || !param) {
+               LOGE("invalid parameter");
+               return TONE_PLAYER_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       }
+
+       if (!(conn = __get_dbus_connection()))
+               return TONE_PLAYER_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
+
+       _reply = g_dbus_connection_call_sync(conn, PA_BUS_NAME,
+               PA_TONE_PLAYER_OBJECT_PATH,
+               PA_TONE_PLAYER_INTERFACE,
+               method, param, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
+
+       g_object_unref(conn);
+
+       if (!_reply) {
+               int ret;
+
+               LOGE("g_dbus_connection_call_sync() method(%s), err-msg(%s)", method, err->message);
+               ret = _convert_dbus_error(err->message);
+               g_error_free(err);
+               return ret;
+       }
+
+       if (reply)
+               *reply = _reply;
+       else
+               g_variant_unref(_reply);
+
+       return TONE_PLAYER_ERROR_NONE;
+}
+
+
 int tone_player_start_new(tone_type_e tone, sound_stream_info_h stream_info, int duration_ms, int *id)
 {
        int ret;
-       int player;
-       double vol = 1.0;
        char *stream_type = NULL;
        int stream_id;
        bool result = false;
 
-       if (tone < TONE_TYPE_DEFAULT || tone > TONE_TYPE_USER_DEFINED_HIGH_FRE || stream_info == NULL)
-               return __convert_tone_player_error_code(__func__, TONE_PLAYER_ERROR_INVALID_PARAMETER);
+       GVariant *reply = NULL;
+       int handle;
+
+       LOGI("tone(%d), duration_ms(%d)", tone, duration_ms);
+
+       if (tone < TONE_TYPE_DEFAULT || tone > TONE_TYPE_USER_DEFINED_HIGH_FRE || !stream_info) {
+               LOGE("invalid parameter.");
+               return TONE_PLAYER_ERROR_INVALID_PARAMETER;
+       }
 
        ret = sound_manager_is_available_stream_information(stream_info, NATIVE_API_TONE_PLAYER, &result);
-       if (!result)
-               return __convert_tone_player_error_code(__func__, TONE_PLAYER_ERROR_NOT_SUPPORTED_TYPE);
+       if (!result || ret) {
+               LOGE("stream info is not available. ret(0x%x), result(%d)", ret, result);
+               return TONE_PLAYER_ERROR_NOT_SUPPORTED_TYPE;
+       }
 
        ret = sound_manager_get_type_from_stream_information(stream_info, &stream_type);
-       if (ret)
-               return __convert_tone_player_error_code(__func__, ret); //LCOV_EXCL_LINE
+       if (ret) {
+               LOGE("can't get stream type. ret(0x%x)", ret);
+               return TONE_PLAYER_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
+       }
 
        ret = sound_manager_get_index_from_stream_information(stream_info, &stream_id);
-       if (ret)
-               return __convert_tone_player_error_code(__func__, ret); //LCOV_EXCL_LINE
+       if (ret) {
+               LOGE("can't get stream index. ret(0x%x)", ret);
+               return TONE_PLAYER_ERROR_INVALID_OPERATION; //LCOV_EXCL_LINE
+       }
+
+       ret =  __dbus_method_call(PA_TONE_PLAYER_METHOD_NAME_TONE_PLAY,
+                       g_variant_new("(uiisi)", tone, duration_ms, getpid(), stream_type, stream_id), &reply);
+       if (ret) {
+               LOGE("__dbus_method_call ret(0x%x)", ret);
+               return ret;
+       }
 
-       ret = mm_sound_play_tone_with_stream_info(tone, stream_type, stream_id, vol, duration_ms, &player);
-       if (ret == 0 && id != NULL)
-               *id = player;
+       g_variant_get(reply, "(u)", &handle);
+       g_variant_unref(reply);
+       if (id)
+               *id = handle;
 
-       return __convert_tone_player_error_code(__func__, ret);
+       LOGI("handle : %d", handle);
+
+       return TONE_PLAYER_ERROR_NONE;
 }
 
 int tone_player_stop(int id)
 {
-       return __convert_tone_player_error_code(__func__, mm_sound_stop_tone(id));
+       int ret;
+
+       LOGI("handle(%d)", id);
+
+       ret =  __dbus_method_call(PA_TONE_PLAYER_METHOD_NAME_TONE_STOP, g_variant_new("(u)", id), NULL);
+       if (ret) {
+               LOGE("__dbus_method_call ret(0x%x)", ret);
+               return ret;
+       }
+
+       return TONE_PLAYER_ERROR_NONE;
 }
 
index 0c7ab3f..f3addc4 100755 (executable)
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <getopt.h>
+#include <signal.h>
 #include <sound_manager.h>
 
 #define TONE_FIRST          TONE_TYPE_DTMF_0
@@ -28,6 +29,8 @@
 
 static GMainLoop *g_mainloop;
 static GThread *event_thread;
+struct sigaction sig_int_old_action;
+static int gid = -1;
 
 static gpointer _g_mainloop_thread(gpointer data)
 {
@@ -77,7 +80,6 @@ static void _stream_focus_cb(sound_stream_info_h stream_info, sound_stream_focus
 static void _tone_play_test(int from, int to, int duration, int sleep_time)
 {
        int i, j;
-       int id = -1;
        sound_stream_info_h stream_info = NULL;
 
        g_print("From : %2d,   To : %2d,    Duration : %4d,  sleep_time : %4d\n", from, to, duration, sleep_time);
@@ -94,11 +96,11 @@ static void _tone_play_test(int from, int to, int duration, int sleep_time)
 
        for (i = from; i <= to; i++) {
                g_print("* Playing Tone : %3d -> ", i);
-               tone_player_start_new(i, stream_info, duration, &id);
-               g_print("id(%d) ", id);
+               tone_player_start_new(i, stream_info, duration, &gid);
+               g_print("gid(%d) ", gid);
 
                if (duration != sleep_time)
-                       g_timeout_add(sleep_time, _g_timeout_cb, (void *)id);
+                       g_timeout_add(sleep_time, _g_timeout_cb, (void *)gid);
 
                for (j = 0; j < sleep_time / 100; j++) {
                        g_print(".");
@@ -114,11 +116,40 @@ static void _tone_play_test(int from, int to, int duration, int sleep_time)
        g_main_loop_quit(g_mainloop);
 }
 
+void __sig_handler(int signo)
+{
+       sigset_t old_mask, all_mask;
+       sigfillset(&all_mask);
+       sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
+       sigprocmask(SIG_SETMASK, &old_mask, NULL);
+
+       printf("sig.num(%d)\n", signo);
+
+       switch (signo) {
+       case SIGINT:
+               if (gid > -1) {
+                       printf("stop tone-player gid(%d)\n", gid);
+                       tone_player_stop(gid);
+               }
+               sigaction(SIGINT, &sig_int_old_action, NULL);
+               raise(signo);
+               break;
+       default:
+               break;
+       }
+}
+
 int main(int argc, char **argv)
 {
        int from = TONE_TYPE_DTMF_0, to = TONE_TYPE_DTMF_S;
        int duration = 500, sleep_time = -1;
 
+       struct sigaction sig_action;
+       sig_action.sa_handler = __sig_handler;
+       sig_action.sa_flags = SA_NOCLDSTOP;
+       sigemptyset(&sig_action.sa_mask);
+       sigaction(SIGINT, &sig_action, &sig_int_old_action);
+
        while (1) {
                int opt;
                int opt_idx = 0;