Code refactoring. Unify subscribe/unsubscribe code 69/187869/4
authorhj kim <backto.kim@samsung.com>
Wed, 29 Aug 2018 06:06:01 +0000 (15:06 +0900)
committerhj kim <backto.kim@samsung.com>
Wed, 29 Aug 2018 07:32:44 +0000 (16:32 +0900)
Change-Id: I7828ffb0bdc54007a73e68c90ce361c8d4a38b38

include/media_controller_private.h
packaging/capi-media-controller.spec
src/media_controller_client.c
test/client_test/media_controller_client_test.c

index b3d810a..23e1152 100755 (executable)
@@ -271,14 +271,14 @@ typedef struct {
 } media_controller_server_s;
 
 typedef enum {
-       MC_EVENT_SERVER_STATE,
-       MC_EVENT_PLAYBACK_INFO,
-       MC_EVENT_METADATA,
-       MC_EVENT_SHUFFLE,
-       MC_EVENT_REPEAT,
-       MC_EVENT_CLIENT_CUSTOM, //from client to server
-       MC_EVENT_PLAYLIST,
+       MC_EVENT_SERVER_STATE           = MC_SUBSCRIPTION_TYPE_SERVER_STATE,
+       MC_EVENT_PLAYBACK_INFO  = MC_SUBSCRIPTION_TYPE_PLAYBACK,
+       MC_EVENT_METADATA               = MC_SUBSCRIPTION_TYPE_METADATA,
+       MC_EVENT_SHUFFLE                        = MC_SUBSCRIPTION_TYPE_SHUFFLE_MODE,
+       MC_EVENT_REPEAT                 = MC_SUBSCRIPTION_TYPE_REPEAT_MODE,
+       MC_EVENT_PLAYLIST                       = MC_SUBSCRIPTION_TYPE_PLAYLIST,
        MC_EVENT_CMD_REPLY,
+       MC_EVENT_CLIENT_CUSTOM, //from client to server
        MC_EVENT_SERVER_CUSTOM, //from server to client
        MC_EVENT_MAX,
 } mc_client_receive_event_e;
index 088b2a8..c14d71c 100755 (executable)
@@ -1,6 +1,6 @@
 Name:       capi-media-controller
 Summary:    A media controller library in Tizen Native API
-Version:    0.1.66
+Version:    0.1.67
 Release:    1
 Group:      Multimedia/API
 License:    Apache-2.0
index d403bff..bb4e8af 100755 (executable)
@@ -40,9 +40,9 @@ mc_cb_event_s cb_event[MC_EVENT_MAX] = {
        {MC_DBUS_SIGNAL_NAME_METADATA,                  __client_metadata_cb},
        {MC_DBUS_SIGNAL_NAME_PLAYBACK_SHUFFLE,  __client_shuffle_cb},
        {MC_DBUS_SIGNAL_NAME_PLAYBACK_REPEAT,   __client_repeat_cb},
-       {MC_DBUS_SIGNAL_NAME_CUSTOM_COMMAND_REPLY,      __client_reply_cb},
        {MC_DBUS_SIGNAL_NAME_PLAYLIST,                          __client_playlist_cb},
        {MC_DBUS_SIGNAL_NAME_CMD_REPLY,                 __client_cmd_reply_received_cb},
+       {MC_DBUS_SIGNAL_NAME_CUSTOM_COMMAND_REPLY,      __client_reply_cb},
        {MC_DBUS_SIGNAL_NAME_CUSTOM_EVENT,              __client_custom_event_received_cb},
 };
 
@@ -745,111 +745,42 @@ int mc_client_unset_custom_event_received_cb(mc_client_h client)
        return __mc_client_unset_updated_cb(client, MC_EVENT_SERVER_CUSTOM);
 }
 
-static int __mc_client_subscribe(mc_client_h client, mc_client_receive_event_e event, const char *server_name)
+int mc_client_subscribe(mc_client_h client, const mc_subscription_type_e subscription_type, const char *server_name)
 {
        int ret = MEDIA_CONTROLLER_ERROR_NONE;
        media_controller_client_s *mc_client = (media_controller_client_s *)client;
 
        mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL");
        mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name");
-       mc_retvm_if(((event < MC_EVENT_SERVER_STATE) || (event >= MC_EVENT_MAX)) , MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid event [%d]", event);
-
-       mc_retvm_if(mc_client->updated_cb[event].callback == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid callback");
-
-       ret = mc_ipc_unregister_listener(&mc_client->listeners, mc_client->dconn, MC_DBUS_UPDATE_INTERFACE, cb_event[event].signal_name);
-       ret = __mc_client_register_filter_listener(mc_client, &mc_client->updated_cb[event].filter_list, server_name, cb_event[event].signal_name,
-                                       cb_event[event].cb_func, (void *)&(mc_client->updated_cb[event]));
-
-       return ret;
-}
-
-int mc_client_subscribe(mc_client_h client, const mc_subscription_type_e subscription_type, const char *server_name)
-{
-       int ret = MEDIA_CONTROLLER_ERROR_NONE;
-
-       switch (subscription_type) {
-       case MC_SUBSCRIPTION_TYPE_SERVER_STATE:
-               ret = __mc_client_subscribe(client, MC_EVENT_SERVER_STATE, server_name);
-               break;
-
-       case MC_SUBSCRIPTION_TYPE_PLAYBACK:
-               ret = __mc_client_subscribe(client, MC_EVENT_PLAYBACK_INFO, server_name);
-               break;
-
-       case MC_SUBSCRIPTION_TYPE_METADATA:
-               ret = __mc_client_subscribe(client, MC_EVENT_METADATA, server_name);
-               break;
-
-       case MC_SUBSCRIPTION_TYPE_SHUFFLE_MODE:
-               ret = __mc_client_subscribe(client, MC_EVENT_SHUFFLE, server_name);
-               break;
-
-       case MC_SUBSCRIPTION_TYPE_REPEAT_MODE:
-               ret = __mc_client_subscribe(client, MC_EVENT_REPEAT, server_name);
-               break;
+       mc_retvm_if(((subscription_type < MC_SUBSCRIPTION_TYPE_SERVER_STATE) || (subscription_type > MC_SUBSCRIPTION_TYPE_PLAYLIST)) , MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid subscription_type [%d]", subscription_type);
+       mc_retvm_if(mc_client->updated_cb[subscription_type].callback == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid callback");
 
-       case MC_SUBSCRIPTION_TYPE_PLAYLIST:
-               ret = __mc_client_subscribe(client, MC_EVENT_PLAYLIST, server_name);
-               break;
+       ret = mc_ipc_unregister_listener(&mc_client->listeners, mc_client->dconn, MC_DBUS_UPDATE_INTERFACE, cb_event[subscription_type].signal_name);
 
-       default:
-               mc_error("Invalid subscription_type [%d]", subscription_type);
-               return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER;
+       if (subscription_type == MC_SUBSCRIPTION_TYPE_METADATA) {
+               ret = __mc_client_register_filter_listener(mc_client, &mc_client->updated_cb[subscription_type].filter_list, server_name, cb_event[subscription_type].signal_name,
+                                       cb_event[subscription_type].cb_func,  (void *)(mc_client));
+       } else {
+               ret = __mc_client_register_filter_listener(mc_client, &mc_client->updated_cb[subscription_type].filter_list, server_name, cb_event[subscription_type].signal_name,
+                                       cb_event[subscription_type].cb_func, (void *)&(mc_client->updated_cb[subscription_type]));
        }
 
        return ret;
 }
 
-static int __mc_client_unsubscribe(mc_client_h client, mc_client_receive_event_e event, const char *server_name)
+int mc_client_unsubscribe(mc_client_h client, const mc_subscription_type_e subscription_type, const char *server_name)
 {
        int ret = MEDIA_CONTROLLER_ERROR_NONE;
        media_controller_client_s *mc_client = (media_controller_client_s *)client;
 
        mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL");
        mc_retvm_if(!MC_STRING_VALID(server_name), MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid server_name");
-       mc_retvm_if(((event < MC_EVENT_SERVER_STATE) || (event >= MC_EVENT_MAX)) , MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid event [%d]", event);
-
-       mc_retvm_if(mc_client->updated_cb[event].callback == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid callback. No list to unsubscribe [%d]", event);
-       mc_retvm_if(mc_client->updated_cb[event].filter_list == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid filter_list. No list to unsubscribe [%d]", event);
-
-       ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[event].filter_list, server_name, cb_event[event].signal_name);
-
-       return ret;
-}
-
-int mc_client_unsubscribe(mc_client_h client, const mc_subscription_type_e subscription_type, const char *server_name)
-{
-       int ret = MEDIA_CONTROLLER_ERROR_NONE;
-
-       switch (subscription_type) {
-       case MC_SUBSCRIPTION_TYPE_SERVER_STATE:
-               ret = __mc_client_unsubscribe(client, MC_EVENT_SERVER_STATE, server_name);
-               break;
-
-       case MC_SUBSCRIPTION_TYPE_PLAYBACK:
-               ret = __mc_client_unsubscribe(client, MC_EVENT_PLAYBACK_INFO, server_name);
-               break;
-
-       case MC_SUBSCRIPTION_TYPE_METADATA:
-               ret = __mc_client_unsubscribe(client, MC_EVENT_METADATA, server_name);
-               break;
-
-       case MC_SUBSCRIPTION_TYPE_SHUFFLE_MODE:
-               ret = __mc_client_unsubscribe(client, MC_EVENT_SHUFFLE, server_name);
-               break;
-
-       case MC_SUBSCRIPTION_TYPE_REPEAT_MODE:
-               ret = __mc_client_unsubscribe(client, MC_EVENT_REPEAT, server_name);
-               break;
+       mc_retvm_if(((subscription_type < MC_SUBSCRIPTION_TYPE_SERVER_STATE) || (subscription_type > MC_SUBSCRIPTION_TYPE_PLAYLIST)) , MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid subscription_type [%d]", subscription_type);
 
-       case MC_SUBSCRIPTION_TYPE_PLAYLIST:
-               ret = __mc_client_unsubscribe(client, MC_EVENT_PLAYLIST, server_name);
-               break;
+       mc_retvm_if(mc_client->updated_cb[subscription_type].callback == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid callback. No list to unsubscribe [%d]", subscription_type);
+       mc_retvm_if(mc_client->updated_cb[subscription_type].filter_list == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid filter_list. No list to unsubscribe [%d]", subscription_type);
 
-       default:
-               mc_error("Invalid subscription_type [%d]", subscription_type);
-               return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER;
-       }
+       ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[subscription_type].filter_list, server_name, cb_event[subscription_type].signal_name);
 
        return ret;
 }
@@ -865,30 +796,9 @@ int mc_client_foreach_server_subscribed(mc_client_h client, const mc_subscriptio
 
        mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL");
        mc_retvm_if(callback == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "callback is NULL");
+       mc_retvm_if(((subscription_type < MC_SUBSCRIPTION_TYPE_SERVER_STATE) || (subscription_type > MC_SUBSCRIPTION_TYPE_PLAYLIST)) , MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Invalid subscription_type [%d]", subscription_type);
 
-       switch (subscription_type) {
-       case MC_SUBSCRIPTION_TYPE_SERVER_STATE:
-               filter_list = mc_client->updated_cb[MC_EVENT_SERVER_STATE].filter_list;
-               break;
-       case MC_SUBSCRIPTION_TYPE_PLAYBACK:
-               filter_list = mc_client->updated_cb[MC_EVENT_PLAYBACK_INFO].filter_list;
-               break;
-       case MC_SUBSCRIPTION_TYPE_METADATA:
-               filter_list = mc_client->updated_cb[MC_EVENT_METADATA].filter_list;
-               break;
-       case MC_SUBSCRIPTION_TYPE_SHUFFLE_MODE:
-               filter_list = mc_client->updated_cb[MC_EVENT_SHUFFLE].filter_list;
-               break;
-       case MC_SUBSCRIPTION_TYPE_REPEAT_MODE:
-               filter_list = mc_client->updated_cb[MC_EVENT_REPEAT].filter_list;
-               break;
-       case MC_SUBSCRIPTION_TYPE_PLAYLIST:
-               filter_list = mc_client->updated_cb[MC_EVENT_PLAYLIST].filter_list;
-               break;
-       default:
-               mc_error("Invalid subscription_type [%d]", subscription_type);
-               return MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER;
-       }
+       filter_list = mc_client->updated_cb[subscription_type].filter_list;
 
        if (filter_list == NULL) {
                mc_debug("No filter list for the subscription_type [%d]", subscription_type);
@@ -1402,18 +1312,18 @@ int mc_client_destroy(mc_client_h client)
 {
        int ret = MEDIA_CONTROLLER_ERROR_NONE;
        media_controller_client_s *mc_client = (media_controller_client_s *)client;
+       int idx = 0;
 
        mc_debug_fenter();
 
        mc_retvm_if(mc_client == NULL, MEDIA_CONTROLLER_ERROR_INVALID_PARAMETER, "Handle is NULL");
 
        /*Unregister all filter listener*/
-       ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[MC_EVENT_SERVER_STATE].filter_list, NULL, cb_event[MC_EVENT_SERVER_STATE].signal_name);
-       ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[MC_EVENT_PLAYBACK_INFO].filter_list, NULL, cb_event[MC_EVENT_PLAYBACK_INFO].signal_name);
-       ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[MC_EVENT_METADATA].filter_list, NULL, cb_event[MC_EVENT_METADATA].signal_name);
-       ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[MC_EVENT_SHUFFLE].filter_list, NULL, cb_event[MC_EVENT_SHUFFLE].signal_name);
-       ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[MC_EVENT_REPEAT].filter_list, NULL, cb_event[MC_EVENT_REPEAT].signal_name);
-       ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[MC_EVENT_PLAYLIST].filter_list, NULL, cb_event[MC_EVENT_PLAYLIST].signal_name);
+       for (idx = 0; idx <= MC_SUBSCRIPTION_TYPE_PLAYLIST; idx++) {
+               ret = __mc_client_unregister_filter_listener(mc_client, &mc_client->updated_cb[idx].filter_list, NULL, cb_event[idx].signal_name);
+               if (ret != MEDIA_CONTROLLER_ERROR_NONE)
+                       mc_error("Error __mc_client_unregister_filter_listener [%d]", ret);
+       }
 
        /*Unregister all listener*/
        ret = mc_ipc_unregister_all_listener(&mc_client->listeners, mc_client->dconn);
index cc5e994..fac56ee 100755 (executable)
@@ -558,6 +558,46 @@ static gboolean _send()
        return TRUE;
 }
 
+static gboolean _subscribe()
+{
+       int ret = MEDIA_CONTROLLER_ERROR_NONE;
+       int idx = 0;
+
+       mc_debug_fenter();
+
+       for (idx = 0; idx <= MC_SUBSCRIPTION_TYPE_PLAYLIST; idx++) {
+               ret = mc_client_subscribe(g_mc_client, idx, g_server_name);
+               if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+                       g_print("Fail to mc_client_subscribe");
+                       return FALSE;
+               }
+       }
+
+       mc_debug_fleave();
+
+       return TRUE;
+}
+
+static gboolean _unsubscribe()
+{
+       int ret = MEDIA_CONTROLLER_ERROR_NONE;
+       int idx = 0;
+
+       mc_debug_fenter();
+
+       for (idx = 0; idx <= MC_SUBSCRIPTION_TYPE_PLAYLIST; idx++) {
+               ret = mc_client_unsubscribe(g_mc_client, idx, g_server_name);
+               if (ret != MEDIA_CONTROLLER_ERROR_NONE) {
+                       g_print("Fail to mc_client_unsubscribe");
+                       return FALSE;
+               }
+       }
+
+       mc_debug_fleave();
+
+       return TRUE;
+}
+
 static gboolean _destroy()
 {
        int ret = MEDIA_CONTROLLER_ERROR_NONE;
@@ -674,6 +714,8 @@ static void display_main_menu(void)
        g_print("4. unset default callback \n");
        g_print("5. get information from server \n");
        g_print("6. send command \n");
+       g_print("7. subscribe \n");
+       g_print("8. unsubscribe \n");
        g_print("9. destroy media controller client \n");
        g_print("0. quit \n");
        g_print("----------------------------------------------------\n");
@@ -734,6 +776,10 @@ void _interpret_main_menu(char *cmd)
                        g_menu_state = CURRENT_STATE_INFORMATION_GET_MENU;
                else if (!strncmp(cmd, "6", len))
                        _send();
+               else if (!strncmp(cmd, "7", len))
+                       _subscribe();
+               else if (!strncmp(cmd, "8", len))
+                       _unsubscribe();
                else if (!strncmp(cmd, "9", len))
                        _destroy();
                else if (!strncmp(cmd, "0", len))