[common][v0.3.20] use fd info to share tbm_bo between processes intead of tbm_key 06/87706/2
authorEunhae Choi <eunhae1.choi@samsung.com>
Fri, 9 Sep 2016 07:11:03 +0000 (16:11 +0900)
committerEunhae Choi <eunhae1.choi@samsung.com>
Fri, 9 Sep 2016 07:14:09 +0000 (16:14 +0900)
Change-Id: I17dd743ffda9acd91f46260220892241fffc10e3

include/player_msg.h
include/player_private.h
packaging/capi-media-player.spec
src/player.c

index 5ef2d98..d563f66 100644 (file)
@@ -530,7 +530,7 @@ extern "C" {
  * @brief Create and send message. Does not wait server result.
  * @remarks Does NOT guarantee thread safe.
  * @param[in] api The enum of module API.
- * @param[in] player The handle of capi media player.
+ * @param[in] fd The socket fd to send msg.
  * @param[in] type The enum of parameter type. Muse be one of thease(INT, INT64, POINTER, DOUBLE, STRING, ARRAY)
  * @param[in] param the name of param is key, must be local variable. never be pointer.
  */
@@ -550,6 +550,75 @@ extern "C" {
        } while (0)
 
 /**
+* @brief Create and send message. Wait for server result.
+* @remarks Does NOT guarantee thread safe.
+* @param[in] api The enum of module API.
+* @param[in] player The handle of capi media player.
+* @param[out] retbuf The buffer of return message. Must be char pointer.Must free after use.
+* @param[out] ret The return value from server.
+* @param[in] type The enum of parameter type. Muse be one of thease(INT, INT64, POINTER, DOUBLE, STRING, ARRAY)
+* @param[in] param# the name of param is key, must be local variable. never be pointer.
+* @param[in] tfd the tbm fd from tbm_bo_export_fd().
+*/
+#define player_msg_send1_fd(api, player, retbuf, ret, type1, param1, tfd) \
+       do {    \
+               char *__sndMsg__; \
+               int __len__; \
+               int __fd__; \
+               int __timeout__ = client_get_api_timeout(player, api); \
+               type1 __value1__ = (type1)param1; \
+               if (CALLBACK_INFO(player)) __fd__ = MSG_FD(player); \
+               else {ret = PLAYER_ERROR_INVALID_STATE; break; } \
+               __sndMsg__ = muse_core_msg_json_factory_new(api, \
+                               MUSE_TYPE_##type1, #param1, __value1__, \
+                               0); \
+               __len__ = muse_core_ipc_send_fd_msg(__fd__, tfd, __sndMsg__); \
+               if (__len__ <= 0) { \
+                       LOGE("sending message failed"); \
+                       ret = PLAYER_ERROR_INVALID_OPERATION; \
+               } else \
+                       ret = client_wait_for_cb_return(api, CALLBACK_INFO(player), &retbuf, __timeout__); \
+               muse_core_msg_json_factory_free(__sndMsg__); \
+       } while (0)
+
+/**
+* @brief Create and send message. Wait for server result.
+* @remarks Does NOT guarantee thread safe.
+* @param[in] api The enum of module API.
+* @param[in] player The handle of capi media player.
+* @param[out] retbuf The buffer of return message. Must be char pointer.Must free after use.
+* @param[out] ret The return value from server.
+* @param[in] param the name of param is key, must be local array/pointer variable.
+* @param[in] length The size of array.
+* @param[in] datum_size The size of a array's datum.
+* @param[in] tfd the tbm fd from tbm_bo_export_fd().
+*/
+#define player_msg_send_array_fd(api, player, retbuf, ret, param, length, datum_size, tfd) \
+do {   \
+       char *__sndMsg__; \
+       int __len__; \
+       int __fd__; \
+       int __timeout__ = client_get_api_timeout(player, api); \
+       int *__value__ = (int *)param; \
+       if (CALLBACK_INFO(player)) __fd__ = MSG_FD(player); \
+       else {ret = PLAYER_ERROR_INVALID_STATE; break; } \
+       __sndMsg__ = muse_core_msg_json_factory_new(api, \
+                       MUSE_TYPE_INT, #length, length, \
+                       MUSE_TYPE_ARRAY, #param, \
+                               datum_size == sizeof(int) ? length :  \
+                               length / sizeof(int) + (length % sizeof(int) ? 1 : 0), \
+                               __value__, \
+                       0); \
+       __len__ = muse_core_ipc_send_fd_msg(__fd__, tfd, __sndMsg__); \
+       if (__len__ <= 0) { \
+               LOGE("sending message failed"); \
+               ret = PLAYER_ERROR_INVALID_OPERATION; \
+       } else \
+               ret = client_wait_for_cb_return(api, CALLBACK_INFO(player), &retbuf, __timeout__); \
+       muse_core_msg_json_factory_free(__sndMsg__); \
+} while (0)
+
+/**
  * @brief Create and send message. Does not wait server result.
  * @remarks Does NOT guarantee thread safe.
  * @param[in] api The enum of module API.
@@ -576,7 +645,6 @@ extern "C" {
                } \
        } while (0)
 
-
 /**
  * @brief Create and send message for callback. Does not wait server result.
  * @remarks Does NOT guarantee thread safe.
index 7a1bf0e..151b89c 100644 (file)
@@ -103,6 +103,7 @@ typedef struct _callback_cb_info {
        media_format_h pkt_fmt;
        void *evas_info;
        tbm_bufmgr bufmgr;
+       tbm_fd tfd;
        GList *tsurf_list; /* player_tsurf_info_t */
        int video_frame_pool_size;
        bool use_tsurf_pool;
index 73b003b..2e5f216 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-player
 Summary:    A Media Player API
-Version:    0.3.19
+Version:    0.3.20
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index cf77518..422c165 100644 (file)
@@ -43,7 +43,8 @@
 #ifdef TIZEN_FEATURE_EVAS_RENDERER
 #include <mm_evas_renderer.h>
 #endif
-#define INVALID_SOCKET -1
+
+#define INVALID_DEFAULT_VALUE -1
 
 typedef struct {
        int int_data;
@@ -80,7 +81,7 @@ int _player_media_packet_finalize(media_packet_h pkt, int error_code, void *user
                return ret;
        }
 
-       if (!fin_data || fin_data->fd <= INVALID_SOCKET) {
+       if (!fin_data || fin_data->fd <= INVALID_DEFAULT_VALUE) {
                LOGE("invalid parameter, fd: %d", (fin_data) ? (fin_data->fd) : (-1));
                goto EXIT;
        }
@@ -291,7 +292,7 @@ static int player_recv_msg(callback_cb_info_s * cb_info)
        msg_buff_s *buff = &cb_info->buff;
 
        memset(buff->recvMsg, 0x00, sizeof(char)*buff->bufLen);
-       recvLen = muse_core_ipc_recv_msg(cb_info->fd, buff->recvMsg);
+       recvLen = muse_core_ipc_recv_fd_msg(cb_info->fd, buff->recvMsg, &cb_info->tfd);
 
        /* check the first msg */
        if (buff->part_of_msg && buff->recvMsg[0] != '{')
@@ -466,17 +467,17 @@ static void __capture_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
        unsigned int size = 0;
        tbm_bo bo = NULL;
        tbm_bo_handle thandle;
-       tbm_key key = 0;
+       int key = INVALID_DEFAULT_VALUE;
        bool ret_val = TRUE;
 
-       player_msg_get3(recvMsg, width, INT, height, INT, size, INT, ret_val);
+       player_msg_get4(recvMsg, width, INT, height, INT, size, INT, key, INT, ret_val);
        if (ret_val) {
-               if (!player_msg_get(key, recvMsg)) {
-                       LOGE("There is no tbm_key value. %d", key);
+               if (cb_info->tfd < 0) {
+                       LOGE("There is no valid tbm_fd");
                        goto EXIT;
                }
 
-               bo = tbm_bo_import(cb_info->bufmgr, key);
+               bo = tbm_bo_import_fd(cb_info->bufmgr, cb_info->tfd);
                if (bo == NULL) {
                        LOGE("TBM get error : bo is NULL");
                        goto EXIT;
@@ -498,11 +499,15 @@ static void __capture_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
        }
 
 EXIT:
+       if (cb_info->tfd > INVALID_DEFAULT_VALUE)
+               close(cb_info->tfd);
+       cb_info->tfd = INVALID_DEFAULT_VALUE;
+
        if (bo)
                tbm_bo_unref(bo);
 
        /* return buffer */
-       if (key != 0) {
+       if (key > INVALID_DEFAULT_VALUE) {
                LOGD("send msg to release buffer. key:%d", key);
                player_msg_send1_no_return(MUSE_PLAYER_API_RETURN_BUFFER, cb_info->fd, INT, key);
        }
@@ -559,7 +564,7 @@ static player_tsurf_info_t* __player_get_tsurf_from_list(callback_cb_info_s * cb
        g_mutex_lock(&cb_info->data_mutex);
        for (l = g_list_first(cb_info->tsurf_list); l; l = g_list_next(l)) {
                player_tsurf_info_t *tmp = (player_tsurf_info_t *)l->data;
-               if (tmp && (tmp->key == key)) {
+               if (tmp && (tmp->key == key)) { /* need to check fd, bo num, etc */
                        LOGD("found tsurf_data of tbm_key %d", key);
 
                        /* need to check tsuf info to support DRC */
@@ -759,17 +764,22 @@ static void __audio_frame_cb_handler(callback_cb_info_s * cb_info, char *recvMsg
 {
        tbm_bo bo = NULL;
        tbm_bo_handle thandle;
-       tbm_key key = 0;
+       int key = INVALID_DEFAULT_VALUE;
        player_audio_raw_data_s audio;
 
        if (!player_msg_get(key, recvMsg)) {
-               LOGE("failed to get value from msg.");
+               LOGE("failed to get key value from msg.");
                return;
        }
 
-       bo = tbm_bo_import(cb_info->bufmgr, key);
+       if (cb_info->tfd < 0) {
+               LOGE("failed to get tbm_fd(key:%d)", key);
+               goto EXIT;
+       }
+
+       bo = tbm_bo_import_fd(cb_info->bufmgr, cb_info->tfd);
        if (bo == NULL) {
-               LOGE("TBM get error : bo is NULL, key:%d", key);
+               LOGE("TBM get error : bo is NULL, tbm_fd:%d(key:%d)", cb_info->tfd, key);
                goto EXIT;
        }
 
@@ -787,11 +797,15 @@ static void __audio_frame_cb_handler(callback_cb_info_s * cb_info, char *recvMsg
        tbm_bo_unmap(bo);
 
 EXIT:
+       if (cb_info->tfd > INVALID_DEFAULT_VALUE)
+               close(cb_info->tfd);
+       cb_info->tfd = INVALID_DEFAULT_VALUE;
+
        if (bo)
                tbm_bo_unref(bo);
 
        /* return buffer */
-       if (key != 0) {
+       if (key > INVALID_DEFAULT_VALUE) {
                LOGD("send msg to release buffer. key:%d", key);
                player_msg_send1_no_return(MUSE_PLAYER_API_RETURN_BUFFER, cb_info->fd, INT, key);
        }
@@ -1249,6 +1263,7 @@ static callback_cb_info_s *callback_new(gint sockfd)
        g_atomic_int_set(&cb_info->running, 1);
        cb_info->fd = sockfd;
        cb_info->thread = g_thread_new("callback_thread", client_cb_handler, (gpointer) cb_info);
+       cb_info->tfd = INVALID_DEFAULT_VALUE;
 
        return cb_info;
 }
@@ -1258,11 +1273,11 @@ static void callback_destroy(callback_cb_info_s * cb_info)
        int i;
        g_return_if_fail(cb_info);
 
-       if (cb_info->fd > INVALID_SOCKET)
+       if (cb_info->fd > INVALID_DEFAULT_VALUE)
                muse_core_connection_close(cb_info->fd);
-       if (cb_info->data_fd > INVALID_SOCKET)
+       if (cb_info->data_fd > INVALID_DEFAULT_VALUE)
                muse_core_connection_close(cb_info->data_fd);
-       cb_info->fd = cb_info->data_fd = INVALID_SOCKET;
+       cb_info->fd = cb_info->data_fd = INVALID_DEFAULT_VALUE;
 
        g_thread_join(cb_info->thread);
        g_thread_unref(cb_info->thread);
@@ -1378,7 +1393,7 @@ int player_create(player_h * player)
        PLAYER_INSTANCE_CHECK(player);
 
        int ret = PLAYER_ERROR_NONE;
-       int sock_fd = INVALID_SOCKET;
+       int sock_fd = INVALID_DEFAULT_VALUE;
        int pid = getpid();
 
        muse_player_api_e api = MUSE_PLAYER_API_CREATE;
@@ -1396,7 +1411,7 @@ int player_create(player_h * player)
 
        while (--retry_count) {
                sock_fd = muse_core_client_new();
-               if (sock_fd <= INVALID_SOCKET) {
+               if (sock_fd <= INVALID_DEFAULT_VALUE) {
                        LOGE("connection failure %d", errno);
                        ret = PLAYER_ERROR_INVALID_OPERATION;
                        retry = true;
@@ -1614,9 +1629,9 @@ int player_set_memory_buffer(player_h player, const void *data, int size)
        muse_player_api_e api = MUSE_PLAYER_API_SET_MEMORY_BUFFER;
        player_cli_s *pc = (player_cli_s *) player;
        char *ret_buf = NULL;
-       tbm_bo bo;
+       tbm_bo bo = NULL;
        tbm_bo_handle thandle;
-       tbm_key key;
+       tbm_fd tfd = INVALID_DEFAULT_VALUE;
 
        if (SERVER_TBM_BO(pc)) {
                LOGE("Already set the memory buffer. unprepare please");
@@ -1637,17 +1652,20 @@ int player_set_memory_buffer(player_h player, const void *data, int size)
        memcpy(thandle.ptr, data, size);
        tbm_bo_unmap(bo);
 
-       key = tbm_bo_export(bo);
-       if (key == 0) {
-               LOGE("TBM get error : key is 0");
+       tfd = tbm_bo_export_fd(bo);
+       if (tfd < 0) {
+               LOGE("tbm_bo_export_fd err 0x%x", tfd);
                ret = PLAYER_ERROR_INVALID_OPERATION;
                goto EXIT;
        }
 
-       player_msg_send2(api, pc, ret_buf, ret, INT, key, INT, size);
+       player_msg_send1_fd(api, pc, ret_buf, ret, INT, size, tfd);
        pc->push_media_stream = FALSE;
 
  EXIT:
+       if (tfd > INVALID_DEFAULT_VALUE)
+               close(tfd);
+
        tbm_bo_unref(bo);
 
        if (ret == PLAYER_ERROR_NONE) {
@@ -2536,7 +2554,8 @@ int player_get_album_art(player_h player, void **palbum_art, int *psize)
        int size = 0;
        tbm_bo bo = NULL;
        tbm_bo_handle thandle;
-       tbm_key key = 0;
+       tbm_fd tfd = INVALID_DEFAULT_VALUE;
+       int key = INVALID_DEFAULT_VALUE;
        void *jobj = NULL;
 
        LOGD("ENTER");
@@ -2554,11 +2573,19 @@ int player_get_album_art(player_h player, void **palbum_art, int *psize)
                if (muse_core_msg_json_object_get_value("size", jobj, &size, MUSE_TYPE_INT) && (size > 0)) {
                        LOGD("size : %d", size);
                        if (!muse_core_msg_json_object_get_value("key", jobj, &key, MUSE_TYPE_INT)) {
+                               LOGE("failed to get key value");
                                ret = PLAYER_ERROR_INVALID_OPERATION;
                                goto EXIT;
                        }
 
-                       bo = tbm_bo_import(pc->cb_info->bufmgr, key);
+                       tfd = pc->cb_info->tfd;
+                       if (tfd < 0) {
+                               LOGE("failed to get tbm fd value");
+                               ret = PLAYER_ERROR_INVALID_OPERATION;
+                               goto EXIT;
+                       }
+
+                       bo = tbm_bo_import_fd(pc->cb_info->bufmgr, tfd);
                        if (bo == NULL) {
                                LOGE("TBM get error : bo is NULL");
                                ret = PLAYER_ERROR_INVALID_OPERATION;
@@ -2579,10 +2606,11 @@ int player_get_album_art(player_h player, void **palbum_art, int *psize)
                                ret = PLAYER_ERROR_INVALID_OPERATION;
                        }
                        tbm_bo_unmap(bo);
+                       *psize = size;
                } else {
                        *palbum_art = NULL;
+                       *psize = 0;
                }
-               *psize = size;
        }
 
 EXIT:
@@ -2592,11 +2620,15 @@ EXIT:
        if (ret_buf)
                g_free(ret_buf);
 
+       if (tfd > INVALID_DEFAULT_VALUE)
+               close(tfd);
+       pc->cb_info->tfd = INVALID_DEFAULT_VALUE;
+
        if (bo)
                tbm_bo_unref(bo);
 
        /* return buffer */
-       if (key != 0) {
+       if (key > INVALID_DEFAULT_VALUE) {
                LOGD("send msg to release buffer. key:%d", key);
                player_msg_send1_async(MUSE_PLAYER_API_RETURN_BUFFER, pc, INT, key);
        }
@@ -3281,6 +3313,7 @@ int player_push_media_stream(player_h player, media_packet_h packet)
 #ifdef __UN_USED
        tbm_bo bo = NULL;
        tbm_bo_handle thandle;
+       tbm_fd tfd = INVALID_DEFAULT_VALUE;
 #endif
        char *buf;
        media_format_h format;
@@ -3322,14 +3355,14 @@ int player_push_media_stream(player_h player, media_packet_h packet)
                memcpy(thandle.ptr, buf, push_media.size);
                tbm_bo_unmap(bo);
 
-               push_media.key = tbm_bo_export(bo);
-               if (push_media.key == 0) {
-                       LOGE("TBM get error : key is 0");
+               tfd = tbm_bo_export_fd(bo);
+               if (tfd < 0) {
+                       LOGE("tbm_bo_export_fd err 0x%x", tfd);
                        ret = PLAYER_ERROR_INVALID_OPERATION;
                        goto ERROR;
                }
 
-               player_msg_send_array(api, pc, ret_buf, ret, push_media_msg, msg_size, sizeof(char));
+               player_msg_send_array_fd(api, pc, ret_buf, ret, push_media_msg, msg_size, sizeof(char), tfd);
        } else if (push_media.buf_type == PUSH_MEDIA_BUF_TYPE_MSG) {
                buf_size = (int)push_media.size;
                player_msg_send_array2(api, pc, ret_buf, ret, push_media_msg, msg_size, sizeof(char), buf, buf_size, sizeof(char));
@@ -3345,6 +3378,9 @@ int player_push_media_stream(player_h player, media_packet_h packet)
 
 #ifdef __UN_USED
  ERROR:
+       if (tfd > INVALID_DEFAULT_VALUE)
+               close(tfd);
+
        if (push_media.buf_type == PUSH_MEDIA_BUF_TYPE_TBM)
                tbm_bo_unref(bo);
 #endif