apply tizen coding rules
[platform/core/api/player.git] / src / player.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <tbm_bufmgr.h>
23 #include <tbm_surface.h>
24 #include <tbm_surface_internal.h>
25 #include <Evas.h>
26 #include <Elementary.h>
27 #include <Ecore.h>
28 #include <Ecore_Wayland.h>
29 #include <dlog.h>
30 #include <mm_error.h>
31 #include <muse_core.h>
32 #include <muse_core_msg_json.h>
33 #include <muse_core_module.h>
34 #include <muse_core_ipc.h>
35 #include <muse_player.h>
36 #include <muse_player_msg.h>
37 #include <sound_manager.h>
38 #include <sound_manager_internal.h>
39 #include "player_internal.h"
40 #include "player_private.h"
41 #include "player_display.h"
42 #include "player_msg.h"
43 #ifdef TIZEN_FEATURE_EVAS_RENDERER
44 #include <mm_evas_renderer.h>
45 #endif
46 #define INVALID_SOCKET -1
47
48 typedef struct {
49         int int_data;
50         char *buf;
51         callback_cb_info_s *cb_info;
52 } _player_cb_data;
53
54 typedef struct {
55         intptr_t remote_pkt;
56         callback_cb_info_s *cb_info; /* to monitor the packet_list */
57         gint fd;
58 } _media_pkt_fin_data;
59
60 /*
61  Global varialbe
62 */
63
64 /*
65 * Internal Implementation
66 */
67 static int _player_deinit_memory_buffer(player_cli_s * pc);
68
69 int _player_media_packet_finalize(media_packet_h pkt, int error_code, void *user_data)
70 {
71         int ret = MEDIA_PACKET_FINALIZE;
72         muse_player_api_e api = MUSE_PLAYER_API_MEDIA_PACKET_FINALIZE_CB;
73         _media_pkt_fin_data *fin_data = (_media_pkt_fin_data *) user_data;
74         intptr_t packet;
75         char *snd_msg = NULL;
76         int snd_len = 0;
77
78         if (!pkt) {
79                 LOGE("invalid parameter buffer %p, user_data %p", pkt, user_data);
80                 return ret;
81         }
82
83         if (!fin_data || fin_data->fd <= INVALID_SOCKET) {
84                 LOGE("invalid parameter, fd: %d", (fin_data) ? (fin_data->fd) : (-1));
85                 goto EXIT;
86         }
87
88         /* Do not destroy tbm surface here to reuse during playback          *
89          * they will be destroyed at player_unprepare() or player_destroy(). *
90          * ref: __player_remove_tsurf_list()                                 */
91
92         packet = fin_data->remote_pkt;
93         snd_msg = muse_core_msg_json_factory_new(api, MUSE_TYPE_POINTER, "packet", packet, 0);
94         snd_len = muse_core_ipc_send_msg(fin_data->fd, snd_msg);
95         muse_core_msg_json_factory_free(snd_msg);
96
97         if (snd_len > 0) {
98                 /* player handle is available. remove packet from the list */
99                 if (fin_data->cb_info->packet_list) {
100                         g_mutex_lock(&fin_data->cb_info->data_mutex);
101                         if (g_list_find(fin_data->cb_info->packet_list, pkt)) {
102                                 fin_data->cb_info->packet_list = g_list_remove(fin_data->cb_info->packet_list, pkt);
103                         } else {
104                                 LOGW("there is no packet(%p) in exported list.", pkt);
105                         }
106                         g_mutex_unlock(&fin_data->cb_info->data_mutex);
107                 } else {
108                         LOGW("there is no packet list.", pkt);
109                 }
110         } else {
111                 LOGE("fail to send msg.");
112         }
113
114 EXIT:
115         if (fin_data) {
116                 g_free(fin_data);
117                 fin_data = NULL;
118         }
119
120         return ret;
121 }
122
123 static int __player_convert_error_code(int code, char *func_name)
124 {
125         int ret = PLAYER_ERROR_INVALID_OPERATION;
126         char *msg = "PLAYER_ERROR_INVALID_OPERATION";
127         switch (code) {
128         case MM_ERROR_NONE:
129         case MM_ERROR_PLAYER_AUDIO_CODEC_NOT_FOUND:
130         case MM_ERROR_PLAYER_VIDEO_CODEC_NOT_FOUND:
131                 ret = PLAYER_ERROR_NONE;
132                 msg = "PLAYER_ERROR_NONE";
133                 break;
134         case MM_ERROR_INVALID_ARGUMENT:
135                 ret = PLAYER_ERROR_INVALID_PARAMETER;
136                 msg = "PLAYER_ERROR_INVALID_PARAMETER";
137                 break;
138         case MM_ERROR_PLAYER_CODEC_NOT_FOUND:
139         case MM_ERROR_PLAYER_STREAMING_UNSUPPORTED_AUDIO:
140         case MM_ERROR_PLAYER_STREAMING_UNSUPPORTED_VIDEO:
141         case MM_ERROR_PLAYER_STREAMING_UNSUPPORTED_MEDIA_TYPE:
142         case MM_ERROR_PLAYER_NOT_SUPPORTED_FORMAT:
143                 ret = PLAYER_ERROR_NOT_SUPPORTED_FILE;
144                 msg = "PLAYER_ERROR_NOT_SUPPORTED_FILE";
145                 break;
146         case MM_ERROR_PLAYER_INVALID_STATE:
147         case MM_ERROR_PLAYER_NOT_INITIALIZED:
148                 ret = PLAYER_ERROR_INVALID_STATE;
149                 msg = "PLAYER_ERROR_INVALID_STATE";
150                 break;
151         case MM_ERROR_PLAYER_INTERNAL:
152         case MM_ERROR_PLAYER_INVALID_STREAM:
153         case MM_ERROR_PLAYER_STREAMING_FAIL:
154         case MM_ERROR_PLAYER_NO_OP:
155                 ret = PLAYER_ERROR_INVALID_OPERATION;
156                 msg = "PLAYER_ERROR_INVALID_OPERATION";
157                 break;
158         case MM_ERROR_PLAYER_SOUND_EFFECT_INVALID_STATUS:
159         case MM_ERROR_NOT_SUPPORT_API:
160         case MM_ERROR_PLAYER_SOUND_EFFECT_NOT_SUPPORTED_FILTER:
161                 ret = PLAYER_ERROR_FEATURE_NOT_SUPPORTED_ON_DEVICE;
162                 msg = "PLAYER_ERROR_FEATURE_NOT_SUPPORTED_ON_DEVICE";
163                 break;
164         case MM_ERROR_PLAYER_NO_FREE_SPACE:
165                 ret = PLAYER_ERROR_FILE_NO_SPACE_ON_DEVICE;
166                 msg = "PLAYER_ERROR_FILE_NO_SPACE_ON_DEVICE";
167                 break;
168         case MM_ERROR_PLAYER_FILE_NOT_FOUND:
169                 ret = PLAYER_ERROR_NO_SUCH_FILE;
170                 msg = "PLAYER_ERROR_NO_SUCH_FILE";
171                 break;
172         case MM_ERROR_PLAYER_SEEK:
173                 ret = PLAYER_ERROR_SEEK_FAILED;
174                 msg = "PLAYER_ERROR_SEEK_FAILED";
175                 break;
176         case MM_ERROR_PLAYER_INVALID_URI:
177         case MM_ERROR_PLAYER_STREAMING_INVALID_URL:
178                 ret = PLAYER_ERROR_INVALID_URI;
179                 msg = "PLAYER_ERROR_INVALID_URI";
180                 break;
181         case MM_ERROR_PLAYER_STREAMING_CONNECTION_FAIL:
182         case MM_ERROR_PLAYER_STREAMING_DNS_FAIL:
183         case MM_ERROR_PLAYER_STREAMING_SERVER_DISCONNECTED:
184         case MM_ERROR_PLAYER_STREAMING_INVALID_PROTOCOL:
185         case MM_ERROR_PLAYER_STREAMING_UNEXPECTED_MSG:
186         case MM_ERROR_PLAYER_STREAMING_OUT_OF_MEMORIES:
187         case MM_ERROR_PLAYER_STREAMING_RTSP_TIMEOUT:
188         case MM_ERROR_PLAYER_STREAMING_BAD_REQUEST:
189         case MM_ERROR_PLAYER_STREAMING_NOT_AUTHORIZED:
190         case MM_ERROR_PLAYER_STREAMING_PAYMENT_REQUIRED:
191         case MM_ERROR_PLAYER_STREAMING_FORBIDDEN:
192         case MM_ERROR_PLAYER_STREAMING_CONTENT_NOT_FOUND:
193         case MM_ERROR_PLAYER_STREAMING_METHOD_NOT_ALLOWED:
194         case MM_ERROR_PLAYER_STREAMING_NOT_ACCEPTABLE:
195         case MM_ERROR_PLAYER_STREAMING_PROXY_AUTHENTICATION_REQUIRED:
196         case MM_ERROR_PLAYER_STREAMING_SERVER_TIMEOUT:
197         case MM_ERROR_PLAYER_STREAMING_GONE:
198         case MM_ERROR_PLAYER_STREAMING_LENGTH_REQUIRED:
199         case MM_ERROR_PLAYER_STREAMING_PRECONDITION_FAILED:
200         case MM_ERROR_PLAYER_STREAMING_REQUEST_ENTITY_TOO_LARGE:
201         case MM_ERROR_PLAYER_STREAMING_REQUEST_URI_TOO_LARGE:
202         case MM_ERROR_PLAYER_STREAMING_PARAMETER_NOT_UNDERSTOOD:
203         case MM_ERROR_PLAYER_STREAMING_CONFERENCE_NOT_FOUND:
204         case MM_ERROR_PLAYER_STREAMING_NOT_ENOUGH_BANDWIDTH:
205         case MM_ERROR_PLAYER_STREAMING_NO_SESSION_ID:
206         case MM_ERROR_PLAYER_STREAMING_METHOD_NOT_VALID_IN_THIS_STATE:
207         case MM_ERROR_PLAYER_STREAMING_HEADER_FIELD_NOT_VALID_FOR_SOURCE:
208         case MM_ERROR_PLAYER_STREAMING_INVALID_RANGE:
209         case MM_ERROR_PLAYER_STREAMING_PARAMETER_IS_READONLY:
210         case MM_ERROR_PLAYER_STREAMING_AGGREGATE_OP_NOT_ALLOWED:
211         case MM_ERROR_PLAYER_STREAMING_ONLY_AGGREGATE_OP_ALLOWED:
212         case MM_ERROR_PLAYER_STREAMING_BAD_TRANSPORT:
213         case MM_ERROR_PLAYER_STREAMING_DESTINATION_UNREACHABLE:
214         case MM_ERROR_PLAYER_STREAMING_INTERNAL_SERVER_ERROR:
215         case MM_ERROR_PLAYER_STREAMING_NOT_IMPLEMENTED:
216         case MM_ERROR_PLAYER_STREAMING_BAD_GATEWAY:
217         case MM_ERROR_PLAYER_STREAMING_SERVICE_UNAVAILABLE:
218         case MM_ERROR_PLAYER_STREAMING_GATEWAY_TIME_OUT:
219         case MM_ERROR_PLAYER_STREAMING_OPTION_NOT_SUPPORTED:
220                 ret = PLAYER_ERROR_CONNECTION_FAILED;
221                 msg = "PLAYER_ERROR_CONNECTION_FAILED";
222                 break;
223         case MM_ERROR_POLICY_BLOCKED:
224         case MM_ERROR_POLICY_INTERRUPTED:
225         case MM_ERROR_POLICY_INTERNAL:
226         case MM_ERROR_POLICY_DUPLICATED:
227                 ret = PLAYER_ERROR_SOUND_POLICY;
228                 msg = "PLAYER_ERROR_SOUND_POLICY";
229                 break;
230         case MM_ERROR_PLAYER_DRM_EXPIRED:
231                 ret = PLAYER_ERROR_DRM_EXPIRED;
232                 msg = "PLAYER_ERROR_DRM_EXPIRED";
233                 break;
234         case MM_ERROR_PLAYER_DRM_NOT_AUTHORIZED:
235         case MM_ERROR_PLAYER_DRM_NO_LICENSE:
236                 ret = PLAYER_ERROR_DRM_NO_LICENSE;
237                 msg = "PLAYER_ERROR_DRM_NO_LICENSE";
238                 break;
239         case MM_ERROR_PLAYER_DRM_FUTURE_USE:
240                 ret = PLAYER_ERROR_DRM_FUTURE_USE;
241                 msg = "PLAYER_ERROR_DRM_FUTURE_USE";
242                 break;
243         case MM_ERROR_PLAYER_DRM_OUTPUT_PROTECTION:
244                 ret = PLAYER_ERROR_DRM_NOT_PERMITTED;
245                 msg = "PLAYER_ERROR_DRM_NOT_PERMITTED";
246                 break;
247         case MM_ERROR_PLAYER_RESOURCE_LIMIT:
248                 ret = PLAYER_ERROR_RESOURCE_LIMIT;
249                 msg = "PLAYER_ERROR_RESOURCE_LIMIT";
250                 break;
251         case MM_ERROR_PLAYER_PERMISSION_DENIED:
252                 ret = PLAYER_ERROR_PERMISSION_DENIED;
253                 msg = "PLAYER_ERROR_PERMISSION_DENIED";
254         }
255         LOGE("[%s] %s(0x%08x) : core fw error(0x%x)", func_name, msg, ret, code);
256         return ret;
257 }
258
259 static void *_get_mem(player_cli_s * player, int size)
260 {
261         player_data_s *mem = g_new(player_data_s, sizeof(player_data_s));
262         if (mem) {
263                 mem->data = g_new(void, size);
264                 mem->next = player->head;
265                 player->head = mem;
266                 return mem->data;
267         }
268         return NULL;
269 }
270
271 static void _del_mem(player_cli_s * player)
272 {
273         player_data_s *mem;
274         while (player->head) {
275                 mem = player->head->next;
276                 g_free(player->head->data);
277                 g_free(player->head);
278                 player->head = mem;
279         }
280 }
281
282 static int player_recv_msg(callback_cb_info_s * cb_info)
283 {
284         int recvLen = 0;
285         msg_buff_s *buff = &cb_info->buff;
286
287         memset(buff->recvMsg, 0x00, sizeof(char)*buff->bufLen);
288         recvLen = muse_core_ipc_recv_msg(cb_info->fd, buff->recvMsg);
289
290         /* check the first msg */
291         if (buff->part_of_msg && buff->recvMsg[0] != '{')
292         {
293                 gchar *tmp = strndup(buff->recvMsg, recvLen);
294                 if (!tmp) {
295                         LOGE("failed to copy msg.");
296                         return 0;
297                 }
298
299                 LOGD("get remained part of msg %d + %d, %d", recvLen, strlen(buff->part_of_msg), buff->bufLen);
300
301                 /* realloc buffer */
302                 if (recvLen+strlen(buff->part_of_msg) >= buff->bufLen) {
303                         LOGD("realloc Buffer %d -> %d", buff->bufLen, recvLen+strlen(buff->part_of_msg)+1);
304                         buff->bufLen = recvLen+strlen(buff->part_of_msg)+1;
305                         buff->recvMsg = g_renew(char, buff->recvMsg, buff->bufLen);
306                         if (!buff->recvMsg) {
307                                 LOGE("failed renew buffer.");
308                                 if (tmp)
309                                         free(tmp);
310                                 return 0;
311                         }
312                         memset(buff->recvMsg, 0x00, sizeof(char)*buff->bufLen);
313                 }
314                 snprintf(buff->recvMsg, buff->bufLen, "%s%s", buff->part_of_msg, tmp);
315                 recvLen += strlen(buff->part_of_msg);
316
317                 free(buff->part_of_msg);
318                 buff->part_of_msg = NULL;
319                 free(tmp);
320                 tmp = NULL;
321         }
322
323         /* check the last msg */
324         if (buff->recvMsg[recvLen-1] != '}') {
325                 char *part_pos = strrchr(buff->recvMsg, '}');
326                 int part_len = ((part_pos) ? (strlen(part_pos+1)) : (0));
327
328                 if (part_len > 0) {
329                         buff->part_of_msg = strndup(part_pos+1, part_len);
330                         if (!buff->part_of_msg) {
331                                 LOGE("failed to alloc buffer for part of msg.");
332                                 return 0;
333                         }
334                         LOGD("get part of msg: %d, %s", strlen(buff->part_of_msg), buff->part_of_msg);
335                         recvLen -= part_len;
336                 }
337         }
338
339         return recvLen;
340 }
341
342 static void set_null_user_cb(callback_cb_info_s * cb_info, muse_player_event_e event)
343 {
344         if (cb_info && event < MUSE_PLAYER_EVENT_TYPE_NUM) {
345                 cb_info->user_cb[event] = NULL;
346                 cb_info->user_data[event] = NULL;
347         }
348 }
349
350 static void set_null_user_cb_lock(callback_cb_info_s * cb_info, muse_player_event_e event)
351 {
352         bool lock = g_thread_self() != cb_info->event_queue.thread;
353
354         if (lock)
355                 g_mutex_lock(&cb_info->event_queue.mutex);
356
357         set_null_user_cb(cb_info, event);
358
359         if (lock)
360                 g_mutex_unlock(&cb_info->event_queue.mutex);
361 }
362
363 static int __set_callback(muse_player_event_e type, player_h player, void *callback, void *user_data)
364 {
365         PLAYER_INSTANCE_CHECK(player);
366         PLAYER_NULL_ARG_CHECK(callback);
367         int ret = PLAYER_ERROR_NONE;
368         player_cli_s *pc = (player_cli_s *) player;
369         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
370         int set = 1;
371
372         LOGI("Event type : %d ", type);
373         player_msg_set_callback(api, pc, ret, type, set);
374
375         if (ret == PLAYER_ERROR_NONE) {
376                 pc->cb_info->user_cb[type] = callback;
377                 pc->cb_info->user_data[type] = user_data;
378         }
379         return ret;
380 }
381
382 static int __unset_callback(muse_player_event_e type, player_h player)
383 {
384         PLAYER_INSTANCE_CHECK(player);
385         int ret = PLAYER_ERROR_NONE;
386         player_cli_s *pc = (player_cli_s *) player;
387         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
388         int set = 0;
389
390         LOGI("Event type : %d ", type);
391
392         PLAYER_NULL_ARG_CHECK(CALLBACK_INFO(pc));
393         set_null_user_cb_lock(CALLBACK_INFO(pc), type);
394
395         player_msg_set_callback(api, pc, ret, type, set);
396         ret = PLAYER_ERROR_NONE;
397
398         return ret;
399 }
400
401 static void __prepare_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
402 {
403         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_PREPARE;
404
405         ((player_prepared_cb) cb_info->user_cb[ev]) (cb_info->user_data[ev]);
406
407         set_null_user_cb(cb_info, ev);
408 }
409
410 static void __complete_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
411 {
412         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_COMPLETE;
413         ((player_completed_cb) cb_info->user_cb[ev]) (cb_info->user_data[ev]);
414 }
415
416 static void __interrupt_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
417 {
418         int code;
419         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_INTERRUPT;
420
421         if (player_msg_get(code, recvMsg))
422                 ((player_interrupted_cb) cb_info->user_cb[ev]) (code, cb_info->user_data[ev]);
423 }
424
425 static void __error_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
426 {
427         int code;
428         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_ERROR;
429
430         if (player_msg_get(code, recvMsg))
431                 ((player_error_cb) cb_info->user_cb[ev]) (code, cb_info->user_data[ev]);
432 }
433
434 static void __buffering_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
435 {
436         int percent;
437         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_BUFFERING;
438
439         if (player_msg_get(percent, recvMsg))
440                 ((player_buffering_cb) cb_info->user_cb[ev]) (percent, cb_info->user_data[ev]);
441 }
442
443 static void __subtitle_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
444 {
445         int duration = 0;
446         char text[MUSE_URI_MAX_LENGTH] = { 0, };
447         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_SUBTITLE;
448         bool ret = TRUE;
449
450         player_msg_get1_string(recvMsg, duration, INT, text, ret);
451         if (ret)
452                 ((player_subtitle_updated_cb) cb_info->user_cb[ev]) (duration, text, cb_info->user_data[ev]);
453 }
454
455 static void __capture_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
456 {
457         unsigned char *data = NULL;
458         int width = 0;
459         int height = 0;
460         unsigned int size = 0;
461         tbm_bo bo = NULL;
462         tbm_bo_handle thandle;
463         tbm_key key = 0;
464         bool ret_val = TRUE;
465
466         player_msg_get3(recvMsg, width, INT, height, INT, size, INT, ret_val);
467         if (ret_val) {
468                 if (!player_msg_get(key, recvMsg)) {
469                         LOGE("There is no tbm_key value. %d", key);
470                         goto EXIT;
471                 }
472
473                 bo = tbm_bo_import(cb_info->bufmgr, key);
474                 if (bo == NULL) {
475                         LOGE("TBM get error : bo is NULL");
476                         goto EXIT;
477                 }
478                 thandle = tbm_bo_map(bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE | TBM_OPTION_READ);
479                 if (thandle.ptr == NULL) {
480                         LOGE("TBM get error : handle pointer is NULL");
481                         goto EXIT;
482                 }
483                 data = g_new(unsigned char, size);
484                 if (data) {
485                         memcpy(data, thandle.ptr, size);
486                         ((player_video_captured_cb) cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_CAPTURE]) (data, width, height, size, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_CAPTURE]);
487                         g_free(data);
488                 } else
489                         LOGE("g_new failure");
490
491                 tbm_bo_unmap(bo);
492         }
493
494 EXIT:
495         if (bo)
496                 tbm_bo_unref(bo);
497
498         /* return buffer */
499         if (key != 0) {
500                 LOGD("send msg to release buffer. key:%d", key);
501                 player_msg_send1_no_return(MUSE_PLAYER_API_RETURN_BUFFER, cb_info->fd, INT, key);
502         }
503
504         set_null_user_cb(cb_info, MUSE_PLAYER_EVENT_TYPE_CAPTURE);
505 }
506
507 static void __seek_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
508 {
509         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_SEEK;
510
511         g_mutex_lock(&cb_info->seek_cb_mutex);
512         if (cb_info->user_cb[ev] && cb_info->block_seek_cb == FALSE) {
513                 LOGD("call seek cb");
514                 ((player_seek_completed_cb) cb_info->user_cb[ev]) (cb_info->user_data[ev]);
515                 set_null_user_cb(cb_info, ev);
516         } else {
517                 LOGW("ignored. seek cb %p, block %d", cb_info->user_cb[ev], cb_info->block_seek_cb);
518         }
519         g_mutex_unlock(&cb_info->seek_cb_mutex);
520 }
521
522 static void __player_remove_tsurf_list(player_cli_s * pc)
523 {
524         GList *l = NULL;
525
526         g_mutex_lock(&pc->cb_info->data_mutex);
527         if (pc->cb_info->tsurf_list) {
528                 LOGD("total num of tsurf list = %d", g_list_length(pc->cb_info->tsurf_list));
529
530                 for (l = g_list_first(pc->cb_info->tsurf_list); l; l = g_list_next(l)) {
531                         player_tsurf_info_t* tmp = (player_tsurf_info_t *)l->data;
532
533                         LOGD("%p will be removed", tmp);
534                         if (tmp) {
535                                 if (tmp->tsurf) {
536                                         tbm_surface_destroy(tmp->tsurf);
537                                         tmp->tsurf = NULL;
538                                 }
539                                 g_free(tmp);
540                         }
541                 }
542                 g_list_free(pc->cb_info->tsurf_list);
543                 pc->cb_info->tsurf_list = NULL;
544         }
545         g_mutex_unlock(&pc->cb_info->data_mutex);
546         return;
547 }
548
549 static player_tsurf_info_t* __player_get_tsurf_from_list(callback_cb_info_s * cb_info, tbm_key key)
550 {
551         GList *l = NULL;
552
553         g_mutex_lock(&cb_info->data_mutex);
554         for (l = g_list_first(cb_info->tsurf_list); l; l = g_list_next(l)) {
555                 player_tsurf_info_t *tmp = (player_tsurf_info_t *)l->data;
556                 if (tmp && (tmp->key == key)) {
557                         LOGD("found tsurf_data of tbm_key %d", key);
558                         g_mutex_unlock(&cb_info->data_mutex);
559                         return tmp;
560                 }
561         }
562         g_mutex_unlock(&cb_info->data_mutex);
563         LOGD("there is no tsurf_data for tbm_key:%d", key);
564         return NULL;
565 }
566
567 static void __media_packet_video_frame_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
568 {
569         tbm_bo bo[4] = { NULL, };
570         tbm_key key[4] = { 0, };
571         tbm_surface_info_s sinfo;
572         char *surface_info = (char *)&sinfo;
573         media_packet_h pkt = NULL;
574         tbm_surface_h tsurf = NULL;
575         player_tsurf_info_t *tsurf_data = NULL;
576         int bo_num = 0;
577         media_format_mimetype_e mimetype = MEDIA_FORMAT_NV12;
578         bool make_pkt_fmt = false;
579         int ret = MEDIA_FORMAT_ERROR_NONE;
580         _media_pkt_fin_data *fin_data = NULL;
581         intptr_t packet;
582         uint64_t pts = 0;
583         int i = 0;
584         muse_core_msg_parse_err_e err = MUSE_MSG_PARSE_ERROR_NONE;
585
586         void *jobj = muse_core_msg_json_object_new(recvMsg, NULL, &err);
587         if (!jobj ||
588                 !muse_core_msg_json_object_get_value("key[0]", jobj, &key[0], MUSE_TYPE_ANY) ||
589                 !muse_core_msg_json_object_get_value("key[1]", jobj, &key[1], MUSE_TYPE_ANY) ||
590                 !muse_core_msg_json_object_get_value("key[2]", jobj, &key[2], MUSE_TYPE_ANY) ||
591                 !muse_core_msg_json_object_get_value("key[3]", jobj, &key[3], MUSE_TYPE_ANY) ||
592                 !muse_core_msg_json_object_get_value("packet", jobj, &packet, MUSE_TYPE_POINTER) ||
593                 !muse_core_msg_json_object_get_value("mimetype", jobj, &mimetype, MUSE_TYPE_ANY) ||
594                 !muse_core_msg_json_object_get_value("pts", jobj, &pts, MUSE_TYPE_INT64) ||
595                 !muse_core_msg_json_object_get_value("surface_info", jobj, surface_info, MUSE_TYPE_ARRAY)) {
596
597                 LOGE("failed to get value from msg. jobj:%p, err:%d", jobj, err);
598                 if (jobj)
599                         muse_core_msg_json_object_free(jobj);
600                 return;
601         }
602         muse_core_msg_json_object_free(jobj);
603
604         LOGD("width %d, height %d", sinfo.width, sinfo.height);
605
606         if (!cb_info) {
607                 LOGE("cb_info is null");
608                 return;
609         }
610
611         if (!cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME]) {
612                 /* send msg to release packet. */
613                 LOGE("_video_decoded_cb is not set");
614                 player_msg_send1_no_return(MUSE_PLAYER_API_MEDIA_PACKET_FINALIZE_CB, cb_info->fd, POINTER, packet);
615                 return;
616         }
617
618         for (i = 0; i < 4; i++) {
619                 if (key[i]) {
620                         bo_num++;
621                         bo[i] = tbm_bo_import(cb_info->bufmgr, key[i]);
622                 }
623         }
624
625         tsurf_data = __player_get_tsurf_from_list(cb_info, key[0]);
626         if (!tsurf_data) {
627                 tsurf_data = g_new(player_tsurf_info_t, 1);
628                 if (!tsurf_data) {
629                         LOGE("failed to alloc tsurf info");
630                         goto ERROR;
631                 }
632
633                 tsurf = tbm_surface_internal_create_with_bos(&sinfo, bo, bo_num);
634                 if (!tsurf) {
635                         LOGE("failed to create tbm surface");
636                         g_free(tsurf_data);
637                         goto ERROR;
638                 }
639                 tsurf_data->key = key[0];
640                 tsurf_data->tsurf = tsurf;
641                 g_mutex_lock(&cb_info->data_mutex);
642                 cb_info->tsurf_list = g_list_append(cb_info->tsurf_list, tsurf_data);
643                 LOGD("key %d is added to the pool", key[0]);
644                 if (cb_info->video_frame_pool_size < g_list_length(cb_info->tsurf_list))
645                         LOGE("need to check the pool size: %d < %d", cb_info->video_frame_pool_size, g_list_length(cb_info->tsurf_list));
646                 g_mutex_unlock(&cb_info->data_mutex);
647         } else {
648                 if (tsurf_data->tsurf) {
649                         tsurf = tsurf_data->tsurf;
650                 } else {
651                         LOGE("tsurf_data->tsurf is null (never enter here)");
652                         goto ERROR;
653                 }
654         }
655
656         /* check media packet format */
657         if (cb_info->pkt_fmt) {
658                 int pkt_fmt_width = 0;
659                 int pkt_fmt_height = 0;
660                 media_format_mimetype_e pkt_fmt_mimetype = MEDIA_FORMAT_NV12;
661
662                 media_format_get_video_info(cb_info->pkt_fmt, &pkt_fmt_mimetype, &pkt_fmt_width, &pkt_fmt_height, NULL, NULL);
663                 if (pkt_fmt_mimetype != mimetype || pkt_fmt_width != sinfo.width || pkt_fmt_height != sinfo.height) {
664                         LOGW("different format. current 0x%x, %dx%d, new 0x%x, %dx%d", pkt_fmt_mimetype, pkt_fmt_width, pkt_fmt_height, mimetype, sinfo.width, sinfo.height);
665                         media_format_unref(cb_info->pkt_fmt);
666                         cb_info->pkt_fmt = NULL;
667                         make_pkt_fmt = true;
668                 }
669         } else {
670                 make_pkt_fmt = true;
671         }
672         /* create packet format */
673         if (make_pkt_fmt) {
674                 LOGI("make new pkt_fmt - mimetype 0x%x, %dx%d", mimetype, sinfo.width, sinfo.height);
675                 ret = media_format_create(&cb_info->pkt_fmt);
676                 if (ret == MEDIA_FORMAT_ERROR_NONE) {
677                         ret = media_format_set_video_mime(cb_info->pkt_fmt, mimetype);
678                         ret |= media_format_set_video_width(cb_info->pkt_fmt, sinfo.width);
679                         ret |= media_format_set_video_height(cb_info->pkt_fmt, sinfo.height);
680                         LOGI("media_format_set_video_mime,width,height ret : 0x%x", ret);
681                 } else {
682                         LOGE("media_format_create failed");
683                 }
684         }
685
686         fin_data = g_new(_media_pkt_fin_data, 1);
687         if (!fin_data) {
688                 LOGE("failed to alloc fin_data");
689                 goto ERROR;
690         }
691         fin_data->remote_pkt = packet;
692         fin_data->cb_info = cb_info;
693         fin_data->fd = cb_info->fd;
694         ret = media_packet_create_from_tbm_surface(cb_info->pkt_fmt, tsurf, (media_packet_finalize_cb) _player_media_packet_finalize, (void *)fin_data, &pkt);
695         if (ret != MEDIA_PACKET_ERROR_NONE || !pkt) {
696                 LOGE("media_packet_create_from_tbm_surface failed %d %p", ret, pkt);
697                 goto ERROR;
698         }
699
700         /* keep the media packet to avoid mem leak. */
701         g_mutex_lock(&cb_info->data_mutex);
702         cb_info->packet_list = g_list_append(cb_info->packet_list, pkt);
703         g_mutex_unlock(&cb_info->data_mutex);
704
705         if (pts != 0) {
706                 ret = media_packet_set_pts(pkt, (uint64_t) pts);
707                 if (ret != MEDIA_PACKET_ERROR_NONE)
708                         LOGE("media_packet_set_pts failed");
709         }
710         if (cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME]) {
711                 /* call media packet callback */
712                 ((player_media_packet_video_decoded_cb) cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME]) (pkt, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME]);
713         } else {
714                 LOGE("_video_decoded_cb is not set");
715                 media_packet_destroy(pkt);
716         }
717
718         for (i = 0; i < bo_num; i++) {
719                 if (bo[i])
720                         tbm_bo_unref(bo[i]);
721         }
722         return;
723
724 ERROR:
725         if (pkt)
726                 media_packet_destroy(pkt);
727
728         if (fin_data)
729                 g_free(fin_data);
730
731         for (i = 0; i < bo_num; i++) {
732                 if (bo[i])
733                         tbm_bo_unref(bo[i]);
734         }
735
736         player_msg_send1_no_return(MUSE_PLAYER_API_MEDIA_PACKET_FINALIZE_CB, cb_info->fd, POINTER, packet);
737         return;
738 }
739
740 static void __audio_frame_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
741 {
742         tbm_bo bo = NULL;
743         tbm_bo_handle thandle;
744         tbm_key key = 0;
745         player_audio_raw_data_s audio;
746
747         if (!player_msg_get(key, recvMsg)) {
748                 LOGE("failed to get value from msg.");
749                 return;
750         }
751
752         bo = tbm_bo_import(cb_info->bufmgr, key);
753         if (bo == NULL) {
754                 LOGE("TBM get error : bo is NULL, key:%d", key);
755                 goto EXIT;
756         }
757
758         thandle = tbm_bo_map(bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE | TBM_OPTION_READ);
759         if (thandle.ptr == NULL) {
760                 LOGE("TBM get error : handle pointer is NULL");
761                 goto EXIT;
762         }
763
764         memcpy(&audio, thandle.ptr, sizeof(player_audio_raw_data_s));
765         audio.data = thandle.ptr + sizeof(player_audio_raw_data_s);
766
767         LOGD("user callback data %p, size %d", audio.data, audio.size);
768         ((player_audio_pcm_extraction_cb) cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_AUDIO_FRAME]) (&audio, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_AUDIO_FRAME]);
769         tbm_bo_unmap(bo);
770
771 EXIT:
772         if (bo)
773                 tbm_bo_unref(bo);
774
775         /* return buffer */
776         if (key != 0) {
777                 LOGD("send msg to release buffer. key:%d", key);
778                 player_msg_send1_no_return(MUSE_PLAYER_API_RETURN_BUFFER, cb_info->fd, INT, key);
779         }
780 }
781
782 static void __video_frame_render_error_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
783 {
784 }
785
786 static void __pd_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
787 {
788         int type;
789         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_PD;
790
791         if (player_msg_get(type, recvMsg))
792                 ((player_pd_message_cb) cb_info->user_cb[ev]) (type, cb_info->user_data[ev]);
793 }
794
795 static void __supported_audio_effect_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
796 {
797 }
798
799 static void __supported_audio_effect_freset_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
800 {
801 }
802
803 static void __missed_plugin_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
804 {
805 }
806
807 static void __media_stream_video_buffer_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
808 {
809         /* player_media_stream_buffer_status_e status; */
810         int status;
811
812         if (player_msg_get(status, recvMsg)) {
813                 ((player_media_stream_buffer_status_cb)
814                  cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS]) ((player_media_stream_buffer_status_e) status, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS]);
815         }
816 }
817
818 static void __media_stream_audio_buffer_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
819 {
820         /* player_media_stream_buffer_status_e status; */
821         int status;
822
823         if (player_msg_get(status, recvMsg)) {
824                 ((player_media_stream_buffer_status_cb)
825                         cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS])
826                         ((player_media_stream_buffer_status_e) status, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS]);
827         }
828
829 }
830
831 static void __media_stream_video_buffer_cb_handler_ex(callback_cb_info_s * cb_info, char *recvMsg)
832 {
833         /* player_media_stream_buffer_status_e status; */
834         int status;
835         unsigned long long bytes;
836         bool ret = TRUE;
837
838         player_msg_get2(recvMsg, status, INT, bytes, INT64, ret);
839         if (ret == TRUE) {
840                 ((player_media_stream_buffer_status_cb_ex)
841                         cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS_WITH_INFO])
842                         ((player_media_stream_buffer_status_e) status, bytes, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS_WITH_INFO]);
843         }
844 }
845
846 static void __media_stream_audio_buffer_cb_handler_ex(callback_cb_info_s * cb_info, char *recvMsg)
847 {
848         /* player_media_stream_buffer_status_e status; */
849         int status;
850         unsigned long long bytes;
851         bool ret = TRUE;
852
853         player_msg_get2(recvMsg, status, INT, bytes, INT64, ret);
854         if (ret == TRUE) {
855                 ((player_media_stream_buffer_status_cb_ex)
856                         cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS_WITH_INFO])
857                         ((player_media_stream_buffer_status_e) status, bytes, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS_WITH_INFO]);
858         }
859
860 }
861
862 static void __media_stream_video_seek_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
863 {
864         unsigned long long offset;
865
866         if (player_msg_get_type(offset, recvMsg, INT64)) {
867                 ((player_media_stream_seek_cb)
868                         cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_SEEK])
869                         (offset, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_SEEK]);
870         }
871 }
872
873 static void __media_stream_audio_seek_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
874 {
875         unsigned long long offset;
876
877         if (player_msg_get_type(offset, recvMsg, INT64)) {
878                 ((player_media_stream_seek_cb)
879                         cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_SEEK])
880                         (offset, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_SEEK]);
881         }
882 }
883
884 static void __video_stream_changed_cb_handler(callback_cb_info_s * cb_info, char *recvMsg)
885 {
886         int width;
887         int height;
888         int fps;
889         int bit_rate;
890         bool ret_val = TRUE;
891
892         player_msg_get4(recvMsg, width, INT, height, INT, fps, INT, bit_rate, INT, ret_val);
893         if (ret_val) {
894                 ((player_video_stream_changed_cb) cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_VIDEO_STREAM_CHANGED])
895                         (width, height, fps, bit_rate, cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_VIDEO_STREAM_CHANGED]);
896         }
897 }
898
899 static void (*_user_callbacks[MUSE_PLAYER_EVENT_TYPE_NUM]) (callback_cb_info_s * cb_info, char *recvMsg) = {
900         __prepare_cb_handler,           /* MUSE_PLAYER_EVENT_TYPE_PREPARE */
901         __complete_cb_handler,  /* MUSE_PLAYER_EVENT_TYPE_COMPLETE */
902         __interrupt_cb_handler, /* MUSE_PLAYER_EVENT_TYPE_INTERRUPT */
903         __error_cb_handler,             /* MUSE_PLAYER_EVENT_TYPE_ERROR */
904         __buffering_cb_handler, /* MUSE_PLAYER_EVENT_TYPE_BUFFERING */
905         __subtitle_cb_handler,  /* MUSE_PLAYER_EVENT_TYPE_SUBTITLE */
906         __capture_cb_handler,   /* MUSE_PLAYER_EVENT_TYPE_CAPTURE */
907         __seek_cb_handler,              /* MUSE_PLAYER_EVENT_TYPE_SEEK */
908         __media_packet_video_frame_cb_handler,  /* MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME */
909         __audio_frame_cb_handler,       /* MUSE_PLAYER_EVENT_TYPE_AUDIO_FRAME */
910         __video_frame_render_error_cb_handler,  /* MUSE_PLAYER_EVENT_TYPE_VIDEO_FRAME_RENDER_ERROR */
911         __pd_cb_handler,                        /* MUSE_PLAYER_EVENT_TYPE_PD */
912         __supported_audio_effect_cb_handler,    /* MUSE_PLAYER_EVENT_TYPE_SUPPORTED_AUDIO_EFFECT */
913         __supported_audio_effect_freset_cb_handler,     /* MUSE_PLAYER_EVENT_TYPE_SUPPORTED_AUDIO_EFFECT_PRESET */
914         __missed_plugin_cb_handler,     /* MUSE_PLAYER_EVENT_TYPE_MISSED_PLUGIN */
915 #ifdef _PLAYER_FOR_PRODUCT
916         NULL,   /* MUSE_PLAYER_EVENT_TYPE_IMAGE_BUFFER */
917         NULL,   /* MUSE_PLAYER_EVENT_TYPE_SELECTED_SUBTITLE_LANGUAGE */
918 #endif
919         __media_stream_video_buffer_cb_handler, /* MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS */
920         __media_stream_audio_buffer_cb_handler, /* MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS */
921         __media_stream_video_buffer_cb_handler_ex,      /* MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS_WITH_INFO */
922         __media_stream_audio_buffer_cb_handler_ex,      /* MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS_WITH_INFO */
923         __media_stream_video_seek_cb_handler,   /* MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_SEEK */
924         __media_stream_audio_seek_cb_handler,   /* MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_SEEK */
925         NULL,   /* MUSE_PLAYER_EVENT_TYPE_AUDIO_STREAM_CHANGED */
926         __video_stream_changed_cb_handler,              /* MUSE_PLAYER_EVENT_TYPE_VIDEO_STREAM_CHANGED */
927 };
928
929 static void _player_event_job_function(_player_cb_data * data)
930 {
931         muse_player_event_e ev = data->int_data;
932         if (data->cb_info->user_cb[ev])
933                 _user_callbacks[ev] (data->cb_info, data->buf);
934         else
935                 LOGW("user callback is unset. type : %d", ev);
936
937         g_free(data->buf);
938         g_free(data);
939 }
940
941 static void *_player_event_queue_loop(void *param)
942 {
943         if (!param) {
944                 LOGE("NULL parameter");
945                 return NULL;
946         }
947         callback_cb_info_s *cb_info = param;
948         player_event_queue *ev = &cb_info->event_queue;
949         _player_cb_data *event_data;
950
951         g_mutex_lock(&ev->mutex);
952         while (ev->running) {
953                 g_mutex_lock(&ev->qlock);
954                 if (g_queue_is_empty(ev->queue)) {
955                         g_mutex_unlock(&ev->qlock);
956                         g_cond_wait(&ev->cond, &ev->mutex);
957                         if (!ev->running)
958                                 break;
959                 } else
960                         g_mutex_unlock(&ev->qlock);
961
962                 while (1) {
963                         g_mutex_lock(&ev->qlock);
964                         event_data = (_player_cb_data *) g_queue_pop_head(ev->queue);
965                         g_mutex_unlock(&ev->qlock);
966                         if (event_data)
967                                 _player_event_job_function(event_data);
968                         else
969                                 break;
970                 }
971         }
972         g_mutex_unlock(&ev->mutex);
973         LOGI("Exit event loop");
974         return NULL;
975 }
976
977 static gboolean _player_event_queue_new(callback_cb_info_s * cb_info)
978 {
979         g_return_val_if_fail(cb_info, FALSE);
980         player_event_queue *ev = &cb_info->event_queue;
981
982         ev->queue = g_queue_new();
983         g_return_val_if_fail(ev->queue, FALSE);
984         g_mutex_init(&ev->qlock);
985
986         g_mutex_init(&ev->mutex);
987         g_cond_init(&ev->cond);
988         ev->running = TRUE;
989         ev->thread = g_thread_new("cb_event_thread", _player_event_queue_loop, (gpointer) cb_info);
990         g_return_val_if_fail(ev->thread, FALSE);
991         LOGI("event queue thread %p", ev->thread);
992
993         return TRUE;
994
995 }
996
997 static void _player_event_queue_destroy(callback_cb_info_s * cb_info)
998 {
999         g_return_if_fail(cb_info);
1000         player_event_queue *ev = &cb_info->event_queue;
1001         _player_cb_data *event_data;
1002
1003         LOGI("event queue thread %p", ev->thread);
1004
1005         g_mutex_lock(&ev->mutex);
1006         ev->running = FALSE;
1007         g_cond_broadcast(&ev->cond);
1008         g_mutex_unlock(&ev->mutex);
1009
1010         g_thread_join(ev->thread);
1011         g_thread_unref(ev->thread);
1012
1013         while (!g_queue_is_empty(ev->queue)) {
1014                 event_data = (_player_cb_data *) g_queue_pop_head(ev->queue);
1015                 if (event_data) {
1016                         g_free(event_data->buf);
1017                         g_free(event_data);
1018                 }
1019         }
1020         g_queue_free(ev->queue);
1021         g_mutex_clear(&ev->qlock);
1022         g_mutex_clear(&ev->mutex);
1023         g_cond_clear(&ev->cond);
1024
1025 }
1026
1027 static void _player_event_queue_remove(player_event_queue * ev_queue, int ev)
1028 {
1029         GList *item;
1030
1031         g_mutex_lock(&ev_queue->qlock);
1032
1033         item = g_queue_peek_head_link(ev_queue->queue);
1034         while (item) {
1035                 GList *next = item->next;
1036                 _player_cb_data *cb_data = (_player_cb_data *)item->data;
1037
1038                 if (cb_data && cb_data->int_data == ev) {
1039                         LOGD("removing '%p (ev:%d)' from event queue", cb_data, cb_data->int_data);
1040                         g_free(cb_data->buf);
1041                         g_free(cb_data);
1042
1043                         g_queue_delete_link(ev_queue->queue, item);
1044                 }
1045                 item = next;
1046         }
1047         g_mutex_unlock(&ev_queue->qlock);
1048 }
1049
1050 static void _player_event_queue_add(player_event_queue * ev, _player_cb_data * data)
1051 {
1052         if (ev->running) {
1053                 g_mutex_lock(&ev->qlock);
1054                 g_queue_push_tail(ev->queue, (gpointer) data);
1055                 g_mutex_unlock(&ev->qlock);
1056                 g_cond_signal(&ev->cond);
1057         }
1058 }
1059
1060 static void _user_callback_handler(callback_cb_info_s * cb_info, muse_player_event_e event, char *buffer)
1061 {
1062         LOGD("get event %d", event);
1063
1064         if (event < MUSE_PLAYER_EVENT_TYPE_NUM) {
1065                 if (cb_info->user_cb[event] && _user_callbacks[event]) {
1066                         _player_cb_data *data = NULL;
1067                         data = g_new(_player_cb_data, 1);
1068                         if (!data) {
1069                                 LOGE("fail to alloc mem");
1070                                 return;
1071                         }
1072                         data->int_data = (int)event;
1073                         data->cb_info = cb_info;
1074                         data->buf = buffer;
1075                         _player_event_queue_add(&cb_info->event_queue, data);
1076                 }
1077         }
1078 }
1079
1080 static void _add_ret_msg(muse_player_api_e api, callback_cb_info_s * cb_info, int offset, int parse_len)
1081 {
1082         ret_msg_s *msg = NULL;
1083         ret_msg_s *last = cb_info->buff.retMsgHead;
1084
1085         msg = g_new(ret_msg_s, 1);
1086         if (msg) {
1087                 msg->api = api;
1088                 msg->msg = strndup(cb_info->buff.recvMsg + offset, parse_len);
1089                 msg->next = NULL;
1090                 if (last == NULL)
1091                         cb_info->buff.retMsgHead = msg;
1092                 else {
1093                         while (last->next)
1094                                 last = last->next;
1095                         last->next = msg;
1096                 }
1097         } else
1098                 LOGE("g_new failure");
1099 }
1100
1101 static ret_msg_s *_get_ret_msg(muse_player_api_e api, callback_cb_info_s * cb_info)
1102 {
1103         ret_msg_s *msg = cb_info->buff.retMsgHead;
1104         ret_msg_s *prev = NULL;
1105         while (msg) {
1106                 if (msg->api == api) {
1107                         if (!prev)
1108                                 cb_info->buff.retMsgHead = msg->next;
1109                         else
1110                                 prev->next = msg->next;
1111                         return msg;
1112                 }
1113                 prev = msg;
1114                 msg = msg->next;
1115         }
1116         return NULL;
1117 }
1118
1119 static void _notify_disconnected(callback_cb_info_s * cb_info)
1120 {
1121         int code = PLAYER_ERROR_SERVICE_DISCONNECTED;
1122         muse_player_event_e ev = MUSE_PLAYER_EVENT_TYPE_ERROR;
1123         if (!cb_info || !cb_info->user_cb[ev])
1124                 return;
1125         ((player_error_cb) cb_info->user_cb[ev]) (code, cb_info->user_data[ev]);
1126 }
1127
1128 static void *client_cb_handler(gpointer data)
1129 {
1130         int api;
1131         int len = 0;
1132         int parse_len = 0;
1133         int offset = 0;
1134         callback_cb_info_s *cb_info = data;
1135         char *recvMsg = NULL;
1136         muse_core_msg_parse_err_e err;
1137
1138         while (g_atomic_int_get(&cb_info->running)) {
1139                 len = 0;
1140                 err = MUSE_MSG_PARSE_ERROR_NONE;
1141
1142                 len = player_recv_msg(cb_info);
1143                 if (len <= 0)
1144                         break;
1145
1146                 recvMsg = cb_info->buff.recvMsg;
1147                 recvMsg[len] = '\0';
1148
1149                 parse_len = len;
1150                 offset = 0;
1151                 while (offset < len) {
1152                         api = MUSE_PLAYER_API_MAX;
1153 //                      LOGD(">>>>>> [%d/%d] %p, %s", offset, len, recvMsg + offset, recvMsg + offset);
1154 //                      usleep(10*1000);
1155
1156                         void *jobj = muse_core_msg_json_object_new(recvMsg + offset, &parse_len, &err);
1157                         if (jobj) {
1158                                 if (muse_core_msg_json_object_get_value("api", jobj, &api, MUSE_TYPE_INT)) {
1159                                         if (api < MUSE_PLAYER_API_MAX) {
1160                                                 g_mutex_lock(&cb_info->player_mutex);
1161                                                 cb_info->buff.recved++;
1162                                                 _add_ret_msg(api, cb_info, offset, parse_len);
1163                                                 g_cond_signal(&cb_info->player_cond[api]);
1164                                                 g_mutex_unlock(&cb_info->player_mutex);
1165                                                 if (api == MUSE_PLAYER_API_DESTROY)
1166                                                         g_atomic_int_set(&cb_info->running, 0);
1167                                         } else if (api == MUSE_PLAYER_CB_EVENT) {
1168                                                 int event;
1169                                                 char *buffer;
1170                                                 g_mutex_lock(&cb_info->player_mutex);
1171                                                 buffer = strndup(recvMsg + offset, parse_len);
1172                                                 g_mutex_unlock(&cb_info->player_mutex);
1173                                                 if (muse_core_msg_json_object_get_value("event", jobj, &event, MUSE_TYPE_INT))
1174                                                         _user_callback_handler(cb_info, event, buffer);
1175                                         }
1176                                 } else {
1177                                         LOGE("Failed to get value. offset:%d/%d, [msg][ %s ]", offset, len, (recvMsg+offset));
1178                                 }
1179                                 muse_core_msg_json_object_free(jobj);
1180                         } else {
1181                                 LOGE("Failed to get msg obj. err:%d", err);
1182                         }
1183
1184                         if (parse_len == 0 || err != MUSE_MSG_PARSE_ERROR_NONE)
1185                                 break;
1186
1187                         offset += parse_len;
1188                         parse_len = len - offset;
1189                 }
1190         }
1191         if (g_atomic_int_get(&cb_info->running))
1192                 _notify_disconnected(cb_info);
1193         LOGD("client cb exit");
1194
1195         return NULL;
1196 }
1197
1198 static callback_cb_info_s *callback_new(gint sockfd)
1199 {
1200         callback_cb_info_s *cb_info;
1201         msg_buff_s *buff;
1202         int i;
1203
1204         g_return_val_if_fail(sockfd > 0, NULL);
1205
1206         cb_info = g_new(callback_cb_info_s, 1);
1207         memset(cb_info, 0, sizeof(callback_cb_info_s));
1208
1209         g_mutex_init(&cb_info->player_mutex);
1210         for (i = 0; i < MUSE_PLAYER_API_MAX; i++)
1211                 g_cond_init(&cb_info->player_cond[i]);
1212
1213         g_mutex_init(&cb_info->data_mutex);
1214         g_mutex_init(&cb_info->seek_cb_mutex);
1215
1216         buff = &cb_info->buff;
1217         buff->recvMsg = g_new(char, MUSE_MSG_MAX_LENGTH + 1);
1218         buff->bufLen = MUSE_MSG_MAX_LENGTH + 1;
1219         buff->recved = 0;
1220         buff->retMsgHead = NULL;
1221         buff->part_of_msg = NULL;
1222
1223         g_atomic_int_set(&cb_info->running, 1);
1224         cb_info->fd = sockfd;
1225         cb_info->thread = g_thread_new("callback_thread", client_cb_handler, (gpointer) cb_info);
1226
1227         return cb_info;
1228 }
1229
1230 static void callback_destroy(callback_cb_info_s * cb_info)
1231 {
1232         int i;
1233         g_return_if_fail(cb_info);
1234
1235         if (cb_info->fd > INVALID_SOCKET)
1236                 muse_core_connection_close(cb_info->fd);
1237         if (cb_info->data_fd > INVALID_SOCKET)
1238                 muse_core_connection_close(cb_info->data_fd);
1239         cb_info->fd = cb_info->data_fd = INVALID_SOCKET;
1240
1241         g_thread_join(cb_info->thread);
1242         g_thread_unref(cb_info->thread);
1243         cb_info->thread = NULL;
1244
1245         LOGI("%p Callback destroyed", cb_info->thread);
1246
1247         g_mutex_clear(&cb_info->player_mutex);
1248         for (i = 0; i < MUSE_PLAYER_API_MAX; i++)
1249                 g_cond_clear(&cb_info->player_cond[i]);
1250
1251         g_mutex_clear(&cb_info->data_mutex);
1252         g_mutex_clear(&cb_info->seek_cb_mutex);
1253
1254         g_free(cb_info->buff.recvMsg);
1255         if (cb_info->buff.part_of_msg)
1256                 g_free(cb_info->buff.part_of_msg);
1257         g_free(cb_info);
1258 }
1259
1260 int client_get_api_timeout(player_cli_s * pc, muse_player_api_e api)
1261 {
1262         int timeout = 0;
1263
1264         switch (api) {
1265         case MUSE_PLAYER_API_PREPARE:
1266         case MUSE_PLAYER_API_PREPARE_ASYNC:
1267         case MUSE_PLAYER_API_UNPREPARE:
1268         case MUSE_PLAYER_API_START:
1269         case MUSE_PLAYER_API_STOP:
1270         case MUSE_PLAYER_API_PAUSE:
1271                 timeout += SERVER_TIMEOUT(pc) * G_TIME_SPAN_MILLISECOND;
1272                 break;
1273         default:
1274                 /* check prepare async is done */
1275                 if (pc && CALLBACK_INFO(pc) && CALLBACK_INFO(pc)->user_cb[MUSE_PLAYER_EVENT_TYPE_PREPARE])
1276                         timeout += SERVER_TIMEOUT(pc) * G_TIME_SPAN_MILLISECOND;
1277                 break;
1278         }
1279         timeout += CALLBACK_TIME_OUT;
1280         return timeout; /* ms */
1281 }
1282
1283 int client_wait_for_cb_return(muse_player_api_e api, callback_cb_info_s * cb_info, char **ret_buf, int time_out)
1284 {
1285         int ret = PLAYER_ERROR_NONE;
1286         gint64 end_time = g_get_monotonic_time() + time_out * G_TIME_SPAN_MILLISECOND;
1287         msg_buff_s *buff = &cb_info->buff;
1288         ret_msg_s *msg = NULL;
1289
1290         g_mutex_lock(&cb_info->player_mutex);
1291
1292         msg = _get_ret_msg(api, cb_info);
1293         if (!buff->recved || !msg) {
1294                 if (!g_cond_wait_until(&cb_info->player_cond[api], &cb_info->player_mutex, end_time)) {
1295                         LOGW("api %d return msg does not received %dms", api, time_out);
1296                         g_mutex_unlock(&cb_info->player_mutex);
1297                         return PLAYER_ERROR_INVALID_OPERATION;
1298                 }
1299         }
1300         if (!msg)
1301                 msg = _get_ret_msg(api, cb_info);
1302         if (msg) {
1303                 *ret_buf = msg->msg;
1304                 g_free(msg);
1305                 if (!player_msg_get(ret, *ret_buf))
1306                         ret = PLAYER_ERROR_INVALID_OPERATION;
1307         } else {
1308                 LOGE("api %d return msg is not exist", api);
1309                 ret = PLAYER_ERROR_INVALID_OPERATION;
1310         }
1311         buff->recved--;
1312
1313         g_mutex_unlock(&cb_info->player_mutex);
1314
1315         return ret;
1316 }
1317
1318 /*
1319 * Public Implementation
1320 */
1321
1322 int player_create(player_h * player)
1323 {
1324         PLAYER_INSTANCE_CHECK(player);
1325
1326         int ret = PLAYER_ERROR_NONE;
1327         int sock_fd = INVALID_SOCKET;
1328         int pid = getpid();
1329
1330         muse_player_api_e api = MUSE_PLAYER_API_CREATE;
1331         muse_core_api_module_e module = MUSE_PLAYER;
1332         player_cli_s *pc = NULL;
1333         char *ret_buf = NULL;
1334
1335         LOGD("ENTER");
1336
1337         sock_fd = muse_core_client_new();
1338         if (sock_fd <= INVALID_SOCKET) {
1339                 LOGE("connection failure %d", errno);
1340                 ret = PLAYER_ERROR_INVALID_OPERATION;
1341                 goto ERROR;
1342         }
1343         player_msg_create_handle(api, sock_fd, INT, module, INT, pid);
1344
1345         pc = g_new0(player_cli_s, 1);
1346         if (pc == NULL) {
1347                 ret = PLAYER_ERROR_OUT_OF_MEMORY;
1348                 goto ERROR;
1349         }
1350
1351         pc->cb_info = callback_new(sock_fd);
1352         if (!pc->cb_info) {
1353                 LOGE("fail to create callback");
1354                 ret = PLAYER_ERROR_INVALID_OPERATION;
1355                 goto ERROR;
1356         }
1357         if (!_player_event_queue_new(pc->cb_info)) {
1358                 LOGE("fail to create event queue");
1359                 ret = PLAYER_ERROR_INVALID_OPERATION;
1360                 goto ERROR;
1361         }
1362
1363         ret = client_wait_for_cb_return(api, pc->cb_info, &ret_buf, CALLBACK_TIME_OUT);
1364         if (ret == PLAYER_ERROR_NONE) {
1365                 intptr_t module_addr;
1366                 *player = (player_h) pc;
1367                 if (player_msg_get_type(module_addr, ret_buf, POINTER)) {
1368                         pc->cb_info->data_fd = muse_core_client_new_data_ch();
1369                         muse_core_send_module_addr(module_addr, pc->cb_info->data_fd);
1370                         LOGD("Data channel fd %d, muse module addr %p", pc->cb_info->data_fd, module_addr);
1371                 }
1372                 SERVER_TIMEOUT(pc) = MAX_SERVER_TIME_OUT;       /* will be update after prepare phase. */
1373         } else
1374                 goto ERROR;
1375
1376         pc->cb_info->bufmgr = tbm_bufmgr_init(-1);
1377         pc->push_media_stream = FALSE;
1378
1379         g_free(ret_buf);
1380         return ret;
1381
1382  ERROR:
1383         if (pc && pc->cb_info) {
1384                 if (pc->cb_info->event_queue.running)
1385                         _player_event_queue_destroy(pc->cb_info);
1386                 callback_destroy(pc->cb_info);
1387                 pc->cb_info = NULL;
1388         }
1389         g_free(pc);
1390         g_free(ret_buf);
1391         LOGD("ret value : %d", ret);
1392         return ret;
1393 }
1394
1395 int player_destroy(player_h player)
1396 {
1397         PLAYER_INSTANCE_CHECK(player);
1398
1399         int ret = PLAYER_ERROR_NONE;
1400         muse_player_api_e api = MUSE_PLAYER_API_DESTROY;
1401         player_cli_s *pc = (player_cli_s *) player;
1402         char *ret_buf = NULL;
1403
1404         LOGD("ENTER");
1405
1406         player_msg_send(api, pc, ret_buf, ret);
1407 #ifdef TIZEN_FEATURE_EVAS_RENDERER
1408         if (CALLBACK_INFO(pc) && EVAS_HANDLE(pc)) {
1409                 player_unset_media_packet_video_frame_decoded_cb(player);
1410                 if (mm_evas_renderer_destroy(&EVAS_HANDLE(pc)) != MM_ERROR_NONE)
1411                         LOGW("fail to unset evas client");
1412         }
1413 #endif
1414         if (pc->cb_info && pc->cb_info->packet_list) {
1415                 g_mutex_lock(&pc->cb_info->data_mutex);
1416                 LOGW("num of remained packets : %d !!", g_list_length(pc->cb_info->packet_list));
1417                 /* each media packet have to destroyed in application. */
1418                 g_list_free(pc->cb_info->packet_list);
1419                 pc->cb_info->packet_list = NULL;
1420                 g_mutex_unlock(&pc->cb_info->data_mutex);
1421         }
1422
1423         if (_wl_window_evas_object_cb_del(player) != MM_ERROR_NONE)
1424                 LOGW("fail to unset evas object callback");
1425
1426         if (CALLBACK_INFO(pc)) {
1427                 __player_remove_tsurf_list(pc);
1428                 _player_event_queue_destroy(CALLBACK_INFO(pc));
1429                 tbm_bufmgr_deinit(TBM_BUFMGR(pc));
1430
1431                 callback_destroy(CALLBACK_INFO(pc));
1432         }
1433
1434         g_free(pc);
1435         pc = NULL;
1436
1437         g_free(ret_buf);
1438         return ret;
1439 }
1440
1441 int player_prepare_async(player_h player, player_prepared_cb callback, void *user_data)
1442 {
1443         PLAYER_INSTANCE_CHECK(player);
1444         int ret = PLAYER_ERROR_NONE;
1445         muse_player_api_e api = MUSE_PLAYER_API_PREPARE_ASYNC;
1446         player_cli_s *pc = (player_cli_s *) player;
1447         char *ret_buf = NULL;
1448
1449         LOGD("ENTER");
1450
1451         if (pc->cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_PREPARE]) {
1452                 LOGE("PLAYER_ERROR_INVALID_OPERATION (0x%08x) : preparing...", PLAYER_ERROR_INVALID_OPERATION);
1453                 return PLAYER_ERROR_INVALID_OPERATION;
1454         } else {
1455                 pc->cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_PREPARE] = callback;
1456                 pc->cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_PREPARE] = user_data;
1457         }
1458         player_msg_send(api, pc, ret_buf, ret);
1459         if (ret == PLAYER_ERROR_NONE) {
1460                 int timeout = 0;
1461                 player_msg_get_type(timeout, ret_buf, INT);
1462
1463                 LOGD("server timeout will be %d", timeout);
1464                 SERVER_TIMEOUT(pc) = timeout;
1465         }
1466
1467         g_free(ret_buf);
1468         return ret;
1469 }
1470
1471 int player_prepare(player_h player)
1472 {
1473         PLAYER_INSTANCE_CHECK(player);
1474         int ret = PLAYER_ERROR_NONE;
1475         muse_player_api_e api = MUSE_PLAYER_API_PREPARE;
1476         player_cli_s *pc = (player_cli_s *) player;
1477         char *ret_buf = NULL;
1478
1479         LOGD("ENTER");
1480
1481         player_msg_send(api, pc, ret_buf, ret);
1482         if (ret == PLAYER_ERROR_NONE) {
1483                 int timeout = 0;
1484                 player_msg_get_type(timeout, ret_buf, INT);
1485
1486                 LOGD("server timeout will be %d", timeout);
1487                 SERVER_TIMEOUT(pc) = timeout;
1488         }
1489
1490         g_free(ret_buf);
1491         return ret;
1492 }
1493
1494 int player_unprepare(player_h player)
1495 {
1496         PLAYER_INSTANCE_CHECK(player);
1497         int ret = PLAYER_ERROR_NONE;
1498         muse_player_api_e api = MUSE_PLAYER_API_UNPREPARE;
1499         player_cli_s *pc = (player_cli_s *) player;
1500         char *ret_buf = NULL;
1501
1502         LOGD("ENTER");
1503
1504         if (!CALLBACK_INFO(pc))
1505                 return PLAYER_ERROR_INVALID_STATE;
1506 #ifdef TIZEN_FEATURE_EVAS_RENDERER
1507         if (EVAS_HANDLE(pc)) {
1508                 player_unset_media_packet_video_frame_decoded_cb(player);
1509                 if (mm_evas_renderer_destroy(&EVAS_HANDLE(pc)) != MM_ERROR_NONE)
1510                         LOGW("fail to unset evas client");
1511         }
1512 #endif
1513         player_msg_send(api, pc, ret_buf, ret);
1514         if (ret == PLAYER_ERROR_NONE) {
1515                 set_null_user_cb_lock(pc->cb_info, MUSE_PLAYER_EVENT_TYPE_SEEK);
1516                 set_null_user_cb_lock(pc->cb_info, MUSE_PLAYER_EVENT_TYPE_PREPARE);
1517                 _del_mem(pc);
1518                 _player_deinit_memory_buffer(pc);
1519         }
1520
1521         pc->cb_info->video_frame_pool_size = 0;
1522         __player_remove_tsurf_list(pc);
1523
1524         g_free(ret_buf);
1525         return ret;
1526 }
1527
1528 int player_set_uri(player_h player, const char *uri)
1529 {
1530         PLAYER_INSTANCE_CHECK(player);
1531         PLAYER_NULL_ARG_CHECK(uri);
1532         int ret = PLAYER_ERROR_NONE;
1533         muse_player_api_e api = MUSE_PLAYER_API_SET_URI;
1534         player_cli_s *pc = (player_cli_s *) player;
1535         char *ret_buf = NULL;
1536
1537         LOGD("ENTER");
1538
1539         player_msg_send1(api, pc, ret_buf, ret, STRING, uri);
1540         pc->push_media_stream = FALSE;
1541
1542         g_free(ret_buf);
1543         return ret;
1544 }
1545
1546 int player_set_memory_buffer(player_h player, const void *data, int size)
1547 {
1548         PLAYER_INSTANCE_CHECK(player);
1549         PLAYER_NULL_ARG_CHECK(data);
1550         int ret = PLAYER_ERROR_NONE;
1551         muse_player_api_e api = MUSE_PLAYER_API_SET_MEMORY_BUFFER;
1552         player_cli_s *pc = (player_cli_s *) player;
1553         char *ret_buf = NULL;
1554         tbm_bo bo;
1555         tbm_bo_handle thandle;
1556         tbm_key key;
1557
1558         if (SERVER_TBM_BO(pc)) {
1559                 LOGE("Already set the memory buffer. unprepare please");
1560                 return PLAYER_ERROR_INVALID_OPERATION;
1561         }
1562
1563         bo = tbm_bo_alloc(pc->cb_info->bufmgr, size, TBM_BO_DEFAULT);
1564         if (bo == NULL) {
1565                 LOGE("TBM get error : bo is NULL");
1566                 return PLAYER_ERROR_INVALID_OPERATION;
1567         }
1568         thandle = tbm_bo_map(bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE);
1569         if (thandle.ptr == NULL) {
1570                 LOGE("TBM get error : handle pointer is NULL");
1571                 ret = PLAYER_ERROR_INVALID_OPERATION;
1572                 goto EXIT;
1573         }
1574         memcpy(thandle.ptr, data, size);
1575         tbm_bo_unmap(bo);
1576
1577         key = tbm_bo_export(bo);
1578         if (key == 0) {
1579                 LOGE("TBM get error : key is 0");
1580                 ret = PLAYER_ERROR_INVALID_OPERATION;
1581                 goto EXIT;
1582         }
1583
1584         player_msg_send2(api, pc, ret_buf, ret, INT, key, INT, size);
1585         pc->push_media_stream = FALSE;
1586
1587  EXIT:
1588         tbm_bo_unref(bo);
1589
1590         if (ret == PLAYER_ERROR_NONE) {
1591                 intptr_t bo_addr = 0;
1592                 if (player_msg_get_type(bo_addr, ret_buf, POINTER))
1593                         SERVER_TBM_BO(pc) = (intptr_t) bo_addr;
1594         }
1595
1596         g_free(ret_buf);
1597         return ret;
1598 }
1599
1600 static int _player_deinit_memory_buffer(player_cli_s * pc)
1601 {
1602         PLAYER_INSTANCE_CHECK(pc);
1603         int ret = PLAYER_ERROR_NONE;
1604         muse_player_api_e api = MUSE_PLAYER_API_DEINIT_MEMORY_BUFFER;
1605         intptr_t bo_addr = SERVER_TBM_BO(pc);
1606
1607         if (!bo_addr)
1608                 return ret;
1609
1610         player_msg_send1_async(api, pc, POINTER, bo_addr);
1611
1612         SERVER_TBM_BO(pc) = 0;
1613
1614         return ret;
1615 }
1616
1617 int player_get_state(player_h player, player_state_e * pstate)
1618 {
1619         PLAYER_INSTANCE_CHECK(player);
1620         PLAYER_NULL_ARG_CHECK(pstate);
1621         int ret = PLAYER_ERROR_NONE;
1622         muse_player_api_e api = MUSE_PLAYER_API_GET_STATE;
1623         player_cli_s *pc = (player_cli_s *) player;
1624         int state;
1625         char *ret_buf = NULL;
1626
1627         LOGD("ENTER");
1628
1629         player_msg_send(api, pc, ret_buf, ret);
1630
1631         if (ret == PLAYER_ERROR_NONE) {
1632                 player_msg_get(state, ret_buf);
1633                 *pstate = state;
1634         }
1635
1636         g_free(ret_buf);
1637         return ret;
1638 }
1639
1640 int player_set_volume(player_h player, float left, float right)
1641 {
1642         PLAYER_INSTANCE_CHECK(player);
1643         PLAYER_CHECK_CONDITION(left >= 0 && left <= 1.0, PLAYER_ERROR_INVALID_PARAMETER, "PLAYER_ERROR_INVALID_PARAMETER");
1644         PLAYER_CHECK_CONDITION(right >= 0 && right <= 1.0, PLAYER_ERROR_INVALID_PARAMETER, "PLAYER_ERROR_INVALID_PARAMETER");
1645         int ret = PLAYER_ERROR_NONE;
1646         muse_player_api_e api = MUSE_PLAYER_API_SET_VOLUME;
1647         player_cli_s *pc = (player_cli_s *) player;
1648         char *ret_buf = NULL;
1649
1650         LOGD("ENTER");
1651
1652         player_msg_send2(api, pc, ret_buf, ret, DOUBLE, right, DOUBLE, left);
1653         g_free(ret_buf);
1654         return ret;
1655 }
1656
1657 int player_get_volume(player_h player, float *pleft, float *pright)
1658 {
1659         PLAYER_INSTANCE_CHECK(player);
1660         PLAYER_NULL_ARG_CHECK(pleft);
1661         PLAYER_NULL_ARG_CHECK(pright);
1662         int ret = PLAYER_ERROR_NONE;
1663         muse_player_api_e api = MUSE_PLAYER_API_GET_VOLUME;
1664         player_cli_s *pc = (player_cli_s *) player;
1665         double left = -1;
1666         double right = -1;
1667         char *ret_buf = NULL;
1668
1669         LOGD("ENTER");
1670
1671         player_msg_send(api, pc, ret_buf, ret);
1672
1673         if (ret == PLAYER_ERROR_NONE) {
1674                 bool ret_val = TRUE;
1675                 player_msg_get2(ret_buf, left, DOUBLE, right, DOUBLE, ret_val);
1676                 if (ret_val) {
1677                         *pleft = (float)left;
1678                         *pright = (float)right;
1679                 } else {
1680                         LOGE("failed to get value from msg");
1681                         ret = PLAYER_ERROR_INVALID_OPERATION;
1682                 }
1683         }
1684
1685         g_free(ret_buf);
1686         return ret;
1687 }
1688
1689 int player_set_sound_type(player_h player, sound_type_e type)
1690 {
1691         PLAYER_INSTANCE_CHECK(player);
1692         int ret = PLAYER_ERROR_NONE;
1693         muse_player_api_e api = MUSE_PLAYER_API_SET_SOUND_TYPE;
1694         player_cli_s *pc = (player_cli_s *) player;
1695         char *ret_buf = NULL;
1696
1697         LOGD("ENTER");
1698
1699         player_msg_send1(api, pc, ret_buf, ret, INT, type);
1700         g_free(ret_buf);
1701         return ret;
1702 }
1703
1704 int player_set_audio_policy_info(player_h player, sound_stream_info_h stream_info)
1705 {
1706         PLAYER_INSTANCE_CHECK(player);
1707
1708         muse_player_api_e api = MUSE_PLAYER_API_SET_AUDIO_POLICY_INFO;
1709         player_cli_s *pc = (player_cli_s *) player;
1710         char *ret_buf = NULL;
1711
1712         LOGD("ENTER");
1713
1714         bool is_available = false;
1715
1716         /* check if stream_info is valid */
1717         int ret = __player_convert_error_code(
1718                                         sound_manager_is_available_stream_information(
1719                                         stream_info, NATIVE_API_PLAYER, &is_available), (char *)__FUNCTION__);
1720
1721         if (ret == PLAYER_ERROR_NONE) {
1722                 if (is_available == false)
1723                         ret = PLAYER_ERROR_FEATURE_NOT_SUPPORTED_ON_DEVICE;
1724                 else {
1725                         char *stream_type = NULL;
1726                         int stream_index = 0;
1727                         ret = sound_manager_get_type_from_stream_information(stream_info, &stream_type);
1728                         ret = sound_manager_get_index_from_stream_information(stream_info, &stream_index);
1729                         if (ret == SOUND_MANAGER_ERROR_NONE)
1730                                 player_msg_send2(api, pc, ret_buf, ret, STRING, stream_type, INT, stream_index);
1731                         else
1732                                 ret = PLAYER_ERROR_INVALID_OPERATION;
1733                 }
1734         }
1735
1736         g_free(ret_buf);
1737         return ret;
1738
1739 }
1740
1741 int player_set_audio_latency_mode(player_h player, audio_latency_mode_e latency_mode)
1742 {
1743         PLAYER_INSTANCE_CHECK(player);
1744         int ret = PLAYER_ERROR_NONE;
1745         muse_player_api_e api = MUSE_PLAYER_API_SET_AUDIO_LATENCY_MODE;
1746         player_cli_s *pc = (player_cli_s *) player;
1747         char *ret_buf = NULL;
1748
1749         LOGD("ENTER");
1750
1751         player_msg_send1(api, pc, ret_buf, ret, INT, latency_mode);
1752         g_free(ret_buf);
1753         return ret;
1754 }
1755
1756 int player_get_audio_latency_mode(player_h player, audio_latency_mode_e * platency_mode)
1757 {
1758         PLAYER_INSTANCE_CHECK(player);
1759         PLAYER_NULL_ARG_CHECK(platency_mode);
1760         int ret = PLAYER_ERROR_NONE;
1761         muse_player_api_e api = MUSE_PLAYER_API_GET_AUDIO_LATENCY_MODE;
1762         player_cli_s *pc = (player_cli_s *) player;
1763         char *ret_buf = NULL;
1764         int latency_mode = -1;
1765
1766         LOGD("ENTER");
1767
1768         player_msg_send(api, pc, ret_buf, ret);
1769
1770         if (ret == PLAYER_ERROR_NONE) {
1771                 player_msg_get(latency_mode, ret_buf);
1772                 *platency_mode = latency_mode;
1773         }
1774
1775         g_free(ret_buf);
1776         return ret;
1777
1778 }
1779
1780 int player_start(player_h player)
1781 {
1782         PLAYER_INSTANCE_CHECK(player);
1783         int ret = PLAYER_ERROR_NONE;
1784         muse_player_api_e api = MUSE_PLAYER_API_START;
1785         player_cli_s *pc = (player_cli_s *) player;
1786         char *ret_buf = NULL;
1787
1788         LOGD("ENTER");
1789 #ifdef TIZEN_FEATURE_EVAS_RENDERER
1790         if (EVAS_HANDLE(pc)) {
1791                 ret = mm_evas_renderer_update_param(EVAS_HANDLE(pc));
1792                 if (ret != PLAYER_ERROR_NONE)
1793                         return ret;
1794         }
1795 #endif
1796         player_msg_send(api, pc, ret_buf, ret);
1797
1798         g_free(ret_buf);
1799         return ret;
1800 }
1801
1802 int player_stop(player_h player)
1803 {
1804         PLAYER_INSTANCE_CHECK(player);
1805         int ret = PLAYER_ERROR_NONE;
1806         muse_player_api_e api = MUSE_PLAYER_API_STOP;
1807         player_cli_s *pc = (player_cli_s *) player;
1808         char *ret_buf = NULL;
1809
1810         LOGD("ENTER");
1811
1812         player_msg_send(api, pc, ret_buf, ret);
1813         if (ret == PLAYER_ERROR_NONE)
1814                 set_null_user_cb_lock(pc->cb_info, MUSE_PLAYER_EVENT_TYPE_SEEK);
1815
1816         g_free(ret_buf);
1817         return ret;
1818 }
1819
1820 int player_pause(player_h player)
1821 {
1822         PLAYER_INSTANCE_CHECK(player);
1823         int ret = PLAYER_ERROR_NONE;
1824         muse_player_api_e api = MUSE_PLAYER_API_PAUSE;
1825         player_cli_s *pc = (player_cli_s *) player;
1826         char *ret_buf = NULL;
1827
1828         LOGD("ENTER");
1829
1830         player_msg_send(api, pc, ret_buf, ret);
1831         g_free(ret_buf);
1832         return ret;
1833 }
1834
1835 int player_set_play_position(player_h player, int millisecond, bool accurate, player_seek_completed_cb callback, void *user_data)
1836 {
1837         PLAYER_INSTANCE_CHECK(player);
1838         PLAYER_CHECK_CONDITION(millisecond >= 0, PLAYER_ERROR_INVALID_PARAMETER, "PLAYER_ERROR_INVALID_PARAMETER");
1839
1840         int ret = PLAYER_ERROR_NONE;
1841         muse_player_api_e api = MUSE_PLAYER_API_SET_PLAY_POSITION;
1842         player_cli_s *pc = (player_cli_s *) player;
1843         char *ret_buf = NULL;
1844         int pos = millisecond;
1845
1846         LOGD("ENTER");
1847         if (!pc->cb_info) {
1848                 LOGE("cb_info is null");
1849                 return PLAYER_ERROR_INVALID_OPERATION;
1850         }
1851
1852         if ((pc->push_media_stream == FALSE) &&
1853                 (pc->cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_SEEK])) {
1854                 LOGE("PLAYER_ERROR_SEEK_FAILED (0x%08x) : seeking...", PLAYER_ERROR_SEEK_FAILED);
1855                 return PLAYER_ERROR_SEEK_FAILED;
1856         } else {
1857                 g_mutex_lock(&pc->cb_info->seek_cb_mutex);
1858                 if (pc->push_media_stream == TRUE)
1859                         pc->cb_info->block_seek_cb = TRUE;
1860                 LOGI("Event type : %d, pos : %d ", MUSE_PLAYER_EVENT_TYPE_SEEK, millisecond);
1861                 pc->cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_SEEK] = callback;
1862                 pc->cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_SEEK] = user_data;
1863                 g_mutex_unlock(&pc->cb_info->seek_cb_mutex);
1864         }
1865
1866         player_msg_send2(api, pc, ret_buf, ret, INT, pos, INT, accurate);
1867
1868         if (ret != PLAYER_ERROR_NONE) {
1869                 g_mutex_lock(&pc->cb_info->seek_cb_mutex);
1870                 set_null_user_cb(pc->cb_info, MUSE_PLAYER_EVENT_TYPE_SEEK);
1871                 g_mutex_unlock(&pc->cb_info->seek_cb_mutex);
1872         }
1873
1874         if (pc->push_media_stream == TRUE)
1875                 _player_event_queue_remove(&pc->cb_info->event_queue, MUSE_PLAYER_EVENT_TYPE_SEEK);
1876
1877         pc->cb_info->block_seek_cb = FALSE;
1878         g_free(ret_buf);
1879         return ret;
1880 }
1881
1882 int player_get_play_position(player_h player, int *millisecond)
1883 {
1884         PLAYER_INSTANCE_CHECK(player);
1885         PLAYER_NULL_ARG_CHECK(millisecond);
1886
1887         int ret = PLAYER_ERROR_NONE;
1888         muse_player_api_e api = MUSE_PLAYER_API_GET_PLAY_POSITION;
1889         player_cli_s *pc = (player_cli_s *) player;
1890         int pos;
1891         char *ret_buf = NULL;
1892
1893         LOGD("ENTER");
1894
1895         player_msg_send(api, pc, ret_buf, ret);
1896
1897         if (ret == PLAYER_ERROR_NONE) {
1898                 player_msg_get(pos, ret_buf);
1899                 *millisecond = pos;
1900         }
1901
1902         g_free(ret_buf);
1903         return ret;
1904 }
1905
1906 int player_set_mute(player_h player, bool muted)
1907 {
1908         PLAYER_INSTANCE_CHECK(player);
1909         int ret = PLAYER_ERROR_NONE;
1910         muse_player_api_e api = MUSE_PLAYER_API_SET_MUTE;
1911         player_cli_s *pc = (player_cli_s *) player;
1912         char *ret_buf = NULL;
1913         int mute = (int)muted;
1914
1915         LOGD("ENTER");
1916
1917         player_msg_send1(api, pc, ret_buf, ret, INT, mute);
1918         g_free(ret_buf);
1919         return ret;
1920 }
1921
1922 int player_is_muted(player_h player, bool * muted)
1923 {
1924         PLAYER_INSTANCE_CHECK(player);
1925         PLAYER_NULL_ARG_CHECK(muted);
1926         int ret = PLAYER_ERROR_NONE;
1927         muse_player_api_e api = MUSE_PLAYER_API_IS_MUTED;
1928         player_cli_s *pc = (player_cli_s *) player;
1929         char *ret_buf = NULL;
1930         int mute = -1;
1931
1932         LOGD("ENTER");
1933
1934         player_msg_send(api, pc, ret_buf, ret);
1935         if (ret == PLAYER_ERROR_NONE) {
1936                 player_msg_get(mute, ret_buf);
1937                 *muted = (bool) mute;
1938         }
1939
1940         g_free(ret_buf);
1941         return ret;
1942 }
1943
1944 int player_set_looping(player_h player, bool looping)
1945 {
1946         PLAYER_INSTANCE_CHECK(player);
1947         int ret = PLAYER_ERROR_NONE;
1948         muse_player_api_e api = MUSE_PLAYER_API_SET_LOOPING;
1949         player_cli_s *pc = (player_cli_s *) player;
1950         char *ret_buf = NULL;
1951
1952         LOGD("ENTER");
1953
1954         player_msg_send1(api, pc, ret_buf, ret, INT, looping);
1955         g_free(ret_buf);
1956         return ret;
1957 }
1958
1959 int player_is_looping(player_h player, bool * plooping)
1960 {
1961         PLAYER_INSTANCE_CHECK(player);
1962         PLAYER_NULL_ARG_CHECK(plooping);
1963         int ret = PLAYER_ERROR_NONE;
1964         muse_player_api_e api = MUSE_PLAYER_API_IS_LOOPING;
1965         player_cli_s *pc = (player_cli_s *) player;
1966         char *ret_buf = NULL;
1967         int looping = 0;
1968
1969         LOGD("ENTER");
1970
1971         player_msg_send(api, pc, ret_buf, ret);
1972         if (ret == PLAYER_ERROR_NONE) {
1973                 player_msg_get(looping, ret_buf);
1974                 *plooping = looping;
1975         }
1976         g_free(ret_buf);
1977         return ret;
1978 }
1979
1980 int player_get_duration(player_h player, int *pduration)
1981 {
1982         PLAYER_INSTANCE_CHECK(player);
1983         PLAYER_NULL_ARG_CHECK(pduration);
1984         int ret = PLAYER_ERROR_NONE;
1985         muse_player_api_e api = MUSE_PLAYER_API_GET_DURATION;
1986         player_cli_s *pc = (player_cli_s *) player;
1987         char *ret_buf = NULL;
1988         int duration = 0;
1989
1990         LOGD("ENTER");
1991
1992         player_msg_send(api, pc, ret_buf, ret);
1993         if (ret == PLAYER_ERROR_NONE) {
1994                 player_msg_get(duration, ret_buf);
1995                 *pduration = duration;
1996         }
1997
1998         g_free(ret_buf);
1999         return ret;
2000 }
2001
2002 int player_set_display(player_h player, player_display_type_e type, player_display_h display)
2003 {
2004         PLAYER_INSTANCE_CHECK(player);
2005         int ret = PLAYER_ERROR_NONE;
2006         muse_player_api_e api = MUSE_PLAYER_API_SET_DISPLAY;
2007         player_cli_s *pc = (player_cli_s *) player;
2008         char *ret_buf = NULL;
2009         Evas_Object *obj = NULL;
2010         const char *object_type = NULL;
2011         wl_win_msg_type wl_win;
2012         char *wl_win_msg = (char *)&wl_win;
2013         unsigned int wl_surface_id;
2014         struct wl_surface *wl_surface;
2015         struct wl_display *wl_display;
2016         Ecore_Wl_Window *wl_window = NULL;
2017         Evas *e;
2018
2019         LOGD("ENTER");
2020
2021         if (type != PLAYER_DISPLAY_TYPE_NONE) {
2022                 obj = (Evas_Object *) display;
2023                 if (!obj)
2024                         return PLAYER_ERROR_INVALID_PARAMETER;
2025
2026                 object_type = evas_object_type_get(obj);
2027                 if (object_type) {
2028                         if (type == PLAYER_DISPLAY_TYPE_OVERLAY && !strcmp(object_type, "elm_win")) {
2029                                 /* wayland overlay surface */
2030                                 LOGI("Wayland overlay surface type");
2031                                 wl_win.type = type;
2032
2033                                 e = evas_object_evas_get(obj);
2034                                 return_val_if_fail(e != NULL, PLAYER_ERROR_INVALID_OPERATION);
2035
2036                                 ret = _wl_window_geometry_get(obj, e, &wl_win.wl_window_x, &wl_win.wl_window_y,
2037                                                         &wl_win.wl_window_width, &wl_win.wl_window_height);
2038                                 if (ret != MM_ERROR_NONE) {
2039                                         LOGE("Fail to get window geometry");
2040                                         return ret;
2041                                 }
2042                                 if (_wl_window_evas_object_cb_add(player, obj) != MM_ERROR_NONE)
2043                                         LOGW("fail to set evas object callback");
2044
2045                                 wl_window = elm_win_wl_window_get(obj);
2046                                 return_val_if_fail(wl_window != NULL, PLAYER_ERROR_INVALID_OPERATION);
2047
2048                                 wl_surface = (struct wl_surface *)ecore_wl_window_surface_get(wl_window);
2049
2050                                 /* get wl_display */
2051                                 wl_display = (struct wl_display *)ecore_wl_display_get();
2052
2053                                 if (!pc->wlclient) {
2054                                         ret = _wl_client_create(&pc->wlclient);
2055                                         if (ret != MM_ERROR_NONE) {
2056                                                 LOGE("Wayland client create failure");
2057                                                 return ret;
2058                                         }
2059                                 }
2060                                 if (wl_surface && wl_display) {
2061                                         LOGD("surface = %p, wl_display = %p", wl_surface, wl_display);
2062                                         wl_surface_id = _wl_client_get_wl_window_wl_surface_id(pc->wlclient, wl_surface, wl_display);
2063                                         LOGD("wl_surface_id = %d", wl_surface_id);
2064                                         wl_win.wl_surface_id = wl_surface_id;
2065                                         LOGD("wl_win.wl_surface_id = %d", wl_win.wl_surface_id);
2066                                 } else {
2067                                         LOGE("Fail to get wl_surface or wl_display");
2068                                         return PLAYER_ERROR_INVALID_OPERATION;
2069                                 }
2070                                 if (pc->wlclient) {
2071                                         g_free(pc->wlclient);
2072                                         pc->wlclient = NULL;
2073                                 }
2074                         }
2075 #ifdef TIZEN_FEATURE_EVAS_RENDERER
2076                         else if (type == PLAYER_DISPLAY_TYPE_EVAS && !strcmp(object_type, "image")) {
2077                                 /* evas object surface */
2078                                 LOGI("evas surface type");
2079                                 wl_win.type = type;
2080
2081                                 evas_object_geometry_get(obj, &wl_win.wl_window_x, &wl_win.wl_window_y,
2082                                         &wl_win.wl_window_width, &wl_win.wl_window_height);
2083
2084                                 if (EVAS_HANDLE(pc)) {
2085                                         LOGW("evas client already exists");
2086                                         if (mm_evas_renderer_destroy(&EVAS_HANDLE(pc)) != MM_ERROR_NONE)
2087                                                 LOGW("fail to unset evas client");
2088                                 }
2089                                 if (mm_evas_renderer_create(&EVAS_HANDLE(pc), obj) != MM_ERROR_NONE)
2090                                         LOGW("fail to set evas client");
2091
2092                                 if (player_set_media_packet_video_frame_decoded_cb(player, mm_evas_renderer_write, (void *)EVAS_HANDLE(pc)) != PLAYER_ERROR_NONE)
2093                                         LOGW("fail to set decoded callback");
2094                         }
2095 #endif
2096                         else
2097                                 return PLAYER_ERROR_INVALID_PARAMETER;
2098                 } else
2099                         return PLAYER_ERROR_INVALID_PARAMETER;
2100         } else {        /* PLAYER_DISPLAY_TYPE_NONE */
2101                 LOGI("Wayland surface type is NONE");
2102                 wl_win.type = type;
2103                 wl_win.wl_window_x = 0;
2104                 wl_win.wl_window_y = 0;
2105                 wl_win.wl_window_width = 0;
2106                 wl_win.wl_window_height = 0;
2107         }
2108         player_msg_send_array(api, pc, ret_buf, ret, wl_win_msg, sizeof(wl_win_msg_type), sizeof(char));
2109         g_free(ret_buf);
2110         return ret;
2111 }
2112
2113 int player_set_display_mode(player_h player, player_display_mode_e mode)
2114 {
2115         PLAYER_INSTANCE_CHECK(player);
2116         PLAYER_CHECK_CONDITION(PLAYER_DISPLAY_MODE_LETTER_BOX <= mode && mode < PLAYER_DISPLAY_MODE_NUM, PLAYER_ERROR_INVALID_PARAMETER, "PLAYER_ERROR_INVALID_PARAMETER");
2117         int ret = PLAYER_ERROR_NONE;
2118         muse_player_api_e api = MUSE_PLAYER_API_SET_DISPLAY_MODE;
2119         player_cli_s *pc = (player_cli_s *) player;
2120         char *ret_buf = NULL;
2121
2122         LOGD("ENTER");
2123 #ifdef TIZEN_FEATURE_EVAS_RENDERER
2124         if (EVAS_HANDLE(pc)) {
2125                 ret = mm_evas_renderer_set_geometry(EVAS_HANDLE(pc), mode);
2126                 return ret;
2127         }
2128 #endif
2129         player_msg_send1(api, pc, ret_buf, ret, INT, mode);
2130         g_free(ret_buf);
2131         return ret;
2132 }
2133
2134 int player_get_display_mode(player_h player, player_display_mode_e * pmode)
2135 {
2136         PLAYER_INSTANCE_CHECK(player);
2137         PLAYER_NULL_ARG_CHECK(pmode);
2138         int ret = PLAYER_ERROR_NONE;
2139         muse_player_api_e api = MUSE_PLAYER_API_GET_DISPLAY_MODE;
2140         player_cli_s *pc = (player_cli_s *) player;
2141         char *ret_buf = NULL;
2142         int mode = -1;
2143
2144         LOGD("ENTER");
2145 #ifdef TIZEN_FEATURE_EVAS_RENDERER
2146         if (EVAS_HANDLE(pc)) {
2147                 ret = mm_evas_renderer_get_geometry(EVAS_HANDLE(pc), &mode);
2148                 *pmode = (player_display_mode_e) mode;
2149                 return ret;
2150         }
2151 #endif
2152         player_msg_send(api, pc, ret_buf, ret);
2153         if (ret == PLAYER_ERROR_NONE) {
2154                 player_msg_get_type(mode, ret_buf, INT);
2155                 *pmode = mode;
2156         }
2157
2158         g_free(ret_buf);
2159         return ret;
2160 }
2161
2162 int player_set_playback_rate(player_h player, float rate)
2163 {
2164         PLAYER_INSTANCE_CHECK(player);
2165         PLAYER_CHECK_CONDITION(rate >= -5.0 && rate <= 5.0, PLAYER_ERROR_INVALID_PARAMETER, "PLAYER_ERROR_INVALID_PARAMETER");
2166         int ret = PLAYER_ERROR_NONE;
2167         muse_player_api_e api = MUSE_PLAYER_API_SET_PLAYBACK_RATE;
2168         player_cli_s *pc = (player_cli_s *) player;
2169         char *ret_buf = NULL;
2170
2171         LOGD("ENTER");
2172
2173         player_msg_send1(api, pc, ret_buf, ret, DOUBLE, rate);
2174         g_free(ret_buf);
2175         return ret;
2176 }
2177
2178 int player_set_display_rotation(player_h player, player_display_rotation_e rotation)
2179 {
2180         PLAYER_INSTANCE_CHECK(player);
2181         int ret = PLAYER_ERROR_NONE;
2182         muse_player_api_e api = MUSE_PLAYER_API_SET_DISPLAY_ROTATION;
2183         player_cli_s *pc = (player_cli_s *) player;
2184         char *ret_buf = NULL;
2185
2186         LOGD("ENTER");
2187 #ifdef TIZEN_FEATURE_EVAS_RENDERER
2188         if (EVAS_HANDLE(pc)) {
2189                 ret = mm_evas_renderer_set_rotation(EVAS_HANDLE(pc), rotation);
2190                 return ret;
2191         }
2192 #endif
2193         player_msg_send1(api, pc, ret_buf, ret, INT, rotation);
2194         g_free(ret_buf);
2195         return ret;
2196 }
2197
2198 int player_get_display_rotation(player_h player, player_display_rotation_e * protation)
2199 {
2200         PLAYER_INSTANCE_CHECK(player);
2201         PLAYER_NULL_ARG_CHECK(protation);
2202         int ret = PLAYER_ERROR_NONE;
2203         player_cli_s *pc = (player_cli_s *) player;
2204         muse_player_api_e api = MUSE_PLAYER_API_GET_DISPLAY_ROTATION;
2205         char *ret_buf = NULL;
2206         int rotation = -1;
2207
2208         LOGD("ENTER");
2209 #ifdef TIZEN_FEATURE_EVAS_RENDERER
2210         if (EVAS_HANDLE(pc)) {
2211                 ret = mm_evas_renderer_get_rotation(EVAS_HANDLE(pc), &rotation);
2212                 *protation = (player_display_rotation_e) rotation;
2213                 return ret;
2214         }
2215 #endif
2216         player_msg_send(api, pc, ret_buf, ret);
2217         if (ret == PLAYER_ERROR_NONE) {
2218                 player_msg_get_type(rotation, ret_buf, INT);
2219                 *protation = rotation;
2220         }
2221
2222         g_free(ret_buf);
2223         return ret;
2224 }
2225
2226 int player_set_display_visible(player_h player, bool visible)
2227 {
2228         PLAYER_INSTANCE_CHECK(player);
2229         int ret = PLAYER_ERROR_NONE;
2230         player_cli_s *pc = (player_cli_s *) player;
2231         muse_player_api_e api = MUSE_PLAYER_API_SET_DISPLAY_VISIBLE;
2232         char *ret_buf = NULL;
2233
2234         LOGD("ENTER");
2235 #ifdef TIZEN_FEATURE_EVAS_RENDERER
2236         if (EVAS_HANDLE(pc)) {
2237                 ret = mm_evas_renderer_set_visible(EVAS_HANDLE(pc), visible);
2238                 return ret;
2239         }
2240 #endif
2241         player_msg_send1(api, pc, ret_buf, ret, INT, visible);
2242         g_free(ret_buf);
2243         return ret;
2244 }
2245
2246 int player_is_display_visible(player_h player, bool * pvisible)
2247 {
2248         PLAYER_INSTANCE_CHECK(player);
2249         PLAYER_NULL_ARG_CHECK(pvisible);
2250         int ret = PLAYER_ERROR_NONE;
2251         player_cli_s *pc = (player_cli_s *) player;
2252         muse_player_api_e api = MUSE_PLAYER_API_IS_DISPLAY_VISIBLE;
2253         char *ret_buf = NULL;
2254         int value = -1;
2255 #ifdef TIZEN_FEATURE_EVAS_RENDERER
2256         bool visible = 0;
2257 #endif
2258         LOGD("ENTER");
2259 #ifdef TIZEN_FEATURE_EVAS_RENDERER
2260         if (EVAS_HANDLE(pc)) {
2261                 ret = mm_evas_renderer_get_visible(EVAS_HANDLE(pc), &visible);
2262                 if (visible)
2263                         *pvisible = TRUE;
2264                 else
2265                         *pvisible = FALSE;
2266                 return ret;
2267         }
2268 #endif
2269         player_msg_send(api, pc, ret_buf, ret);
2270         if (ret == PLAYER_ERROR_NONE) {
2271                 player_msg_get_type(value, ret_buf, INT);
2272
2273                 if (value)
2274                         *pvisible = TRUE;
2275                 else
2276                         *pvisible = FALSE;
2277         }
2278
2279         g_free(ret_buf);
2280         return ret;
2281 }
2282
2283 int player_get_content_info(player_h player, player_content_info_e key, char **pvalue)
2284 {
2285         PLAYER_INSTANCE_CHECK(player);
2286         PLAYER_NULL_ARG_CHECK(pvalue);
2287         int ret = PLAYER_ERROR_NONE;
2288         muse_player_api_e api = MUSE_PLAYER_API_GET_CONTENT_INFO;
2289         player_cli_s *pc = (player_cli_s *) player;
2290         char *ret_buf = NULL;
2291         char value[MUSE_MSG_MAX_LENGTH] = { 0, };
2292
2293         LOGD("ENTER");
2294
2295         player_msg_send1(api, pc, ret_buf, ret, INT, key);
2296         if (ret == PLAYER_ERROR_NONE) {
2297                 player_msg_get_string(value, ret_buf);
2298                 *pvalue = strndup(value, MUSE_MSG_MAX_LENGTH);
2299         }
2300         g_free(ret_buf);
2301         return ret;
2302 }
2303
2304 int player_get_codec_info(player_h player, char **paudio_codec, char **pvideo_codec)
2305 {
2306         PLAYER_INSTANCE_CHECK(player);
2307         PLAYER_NULL_ARG_CHECK(paudio_codec);
2308         PLAYER_NULL_ARG_CHECK(pvideo_codec);
2309         int ret = PLAYER_ERROR_NONE;
2310         muse_player_api_e api = MUSE_PLAYER_API_GET_CODEC_INFO;
2311         player_cli_s *pc = (player_cli_s *) player;
2312         char *ret_buf = NULL;
2313         char video_codec[MUSE_MSG_MAX_LENGTH] = { 0, };
2314         char audio_codec[MUSE_MSG_MAX_LENGTH] = { 0, };
2315         bool ret_val = TRUE;
2316
2317         LOGD("ENTER");
2318
2319         player_msg_send(api, pc, ret_buf, ret);
2320         if (ret == PLAYER_ERROR_NONE) {
2321                 player_msg_get_string2(ret_buf, video_codec, audio_codec, ret_val);
2322                 if (ret_val) {
2323                         *pvideo_codec = strndup(video_codec, MUSE_MSG_MAX_LENGTH);
2324                         *paudio_codec = strndup(audio_codec, MUSE_MSG_MAX_LENGTH);
2325                 } else {
2326                         ret = PLAYER_ERROR_INVALID_OPERATION;
2327                 }
2328         }
2329         g_free(ret_buf);
2330         return ret;
2331 }
2332
2333 int player_get_audio_stream_info(player_h player, int *psample_rate, int *pchannel, int *pbit_rate)
2334 {
2335         PLAYER_INSTANCE_CHECK(player);
2336         PLAYER_NULL_ARG_CHECK(psample_rate);
2337         PLAYER_NULL_ARG_CHECK(pchannel);
2338         PLAYER_NULL_ARG_CHECK(pbit_rate);
2339         int ret = PLAYER_ERROR_NONE;
2340         muse_player_api_e api = MUSE_PLAYER_API_GET_AUDIO_STREAM_INFO;
2341         player_cli_s *pc = (player_cli_s *) player;
2342         char *ret_buf = NULL;
2343         int sample_rate = 0;
2344         int channel = 0;
2345         int bit_rate = 0;
2346
2347         LOGD("ENTER");
2348
2349         player_msg_send(api, pc, ret_buf, ret);
2350         if (ret == PLAYER_ERROR_NONE) {
2351                 bool ret_val = TRUE;
2352                 player_msg_get3(ret_buf, sample_rate, INT, channel, INT, bit_rate, INT, ret_val);
2353                 if (ret_val) {
2354                         *psample_rate = sample_rate;
2355                         *pchannel = channel;
2356                         *pbit_rate = bit_rate;
2357                 } else {
2358                         ret = PLAYER_ERROR_INVALID_OPERATION;
2359                 }
2360         }
2361         g_free(ret_buf);
2362         return ret;
2363 }
2364
2365 int player_get_video_stream_info(player_h player, int *pfps, int *pbit_rate)
2366 {
2367         PLAYER_INSTANCE_CHECK(player);
2368         PLAYER_NULL_ARG_CHECK(pfps);
2369         PLAYER_NULL_ARG_CHECK(pbit_rate);
2370         int ret = PLAYER_ERROR_NONE;
2371         muse_player_api_e api = MUSE_PLAYER_API_GET_VIDEO_STREAM_INFO;
2372         player_cli_s *pc = (player_cli_s *) player;
2373         char *ret_buf = NULL;
2374         int fps = 0;
2375         int bit_rate = 0;
2376
2377         LOGD("ENTER");
2378
2379         player_msg_send(api, pc, ret_buf, ret);
2380         if (ret == PLAYER_ERROR_NONE) {
2381                 bool ret_val = TRUE;
2382                 player_msg_get2(ret_buf, fps, INT, bit_rate, INT, ret_val);
2383                 if (ret_val) {
2384                         *pfps = fps;
2385                         *pbit_rate = bit_rate;
2386                 } else {
2387                         ret = PLAYER_ERROR_INVALID_OPERATION;
2388                 }
2389         }
2390         g_free(ret_buf);
2391         return ret;
2392 }
2393
2394 int player_get_video_size(player_h player, int *pwidth, int *pheight)
2395 {
2396         PLAYER_INSTANCE_CHECK(player);
2397         PLAYER_NULL_ARG_CHECK(pwidth);
2398         PLAYER_NULL_ARG_CHECK(pheight);
2399         int ret = PLAYER_ERROR_NONE;
2400         muse_player_api_e api = MUSE_PLAYER_API_GET_VIDEO_SIZE;
2401         player_cli_s *pc = (player_cli_s *) player;
2402         char *ret_buf = NULL;
2403         int width = 0;
2404         int height = 0;
2405
2406         LOGD("ENTER");
2407
2408         player_msg_send(api, pc, ret_buf, ret);
2409         if (ret == PLAYER_ERROR_NONE) {
2410                 bool ret_val = TRUE;
2411                 player_msg_get2(ret_buf, width, INT, height, INT, ret_val);
2412                 if (ret_val) {
2413                         *pwidth = width;
2414                         *pheight = height;
2415                 } else {
2416                         ret = PLAYER_ERROR_INVALID_OPERATION;
2417                 }
2418         }
2419         g_free(ret_buf);
2420         return ret;
2421 }
2422
2423 int player_get_album_art(player_h player, void **palbum_art, int *psize)
2424 {
2425         PLAYER_INSTANCE_CHECK(player);
2426         PLAYER_NULL_ARG_CHECK(palbum_art);
2427         PLAYER_NULL_ARG_CHECK(psize);
2428         int ret = PLAYER_ERROR_NONE;
2429         muse_player_api_e api = MUSE_PLAYER_API_GET_ALBUM_ART;
2430         player_cli_s *pc = (player_cli_s *) player;
2431         char *ret_buf = NULL;
2432         char *album_art;
2433         int size = 0;
2434         tbm_bo bo = NULL;
2435         tbm_bo_handle thandle;
2436         tbm_key key = 0;
2437         void *jobj = NULL;
2438
2439         LOGD("ENTER");
2440
2441         player_msg_send(api, pc, ret_buf, ret);
2442         if (ret == PLAYER_ERROR_NONE) {
2443                 muse_core_msg_parse_err_e err = MUSE_MSG_PARSE_ERROR_NONE;
2444                 jobj = muse_core_msg_json_object_new(ret_buf, NULL, &err);
2445                 if (!jobj) {
2446                         LOGE("failed to get msg obj, err:%d", err);
2447                         ret = PLAYER_ERROR_INVALID_OPERATION;
2448                         goto EXIT;
2449                 }
2450
2451                 if (muse_core_msg_json_object_get_value("size", jobj, &size, MUSE_TYPE_INT) && (size > 0)) {
2452                         LOGD("size : %d", size);
2453                         if (!muse_core_msg_json_object_get_value("key", jobj, &key, MUSE_TYPE_INT)) {
2454                                 ret = PLAYER_ERROR_INVALID_OPERATION;
2455                                 goto EXIT;
2456                         }
2457
2458                         bo = tbm_bo_import(pc->cb_info->bufmgr, key);
2459                         if (bo == NULL) {
2460                                 LOGE("TBM get error : bo is NULL");
2461                                 ret = PLAYER_ERROR_INVALID_OPERATION;
2462                                 goto EXIT;
2463                         }
2464                         thandle = tbm_bo_map(bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE | TBM_OPTION_READ);
2465                         if (thandle.ptr == NULL) {
2466                                 LOGE("TBM get error : handle pointer is NULL");
2467                                 ret = PLAYER_ERROR_INVALID_OPERATION;
2468                                 goto EXIT;
2469                         }
2470                         album_art = _get_mem(pc, size);
2471                         if (album_art) {
2472                                 memcpy(album_art, thandle.ptr, size);
2473                                 *palbum_art = album_art;
2474                         } else {
2475                                 LOGE("g_new failure");
2476                                 ret = PLAYER_ERROR_INVALID_OPERATION;
2477                         }
2478                         tbm_bo_unmap(bo);
2479                 } else {
2480                         *palbum_art = NULL;
2481                 }
2482                 *psize = size;
2483         }
2484
2485 EXIT:
2486         if (jobj)
2487                 muse_core_msg_json_object_free(jobj);
2488
2489         if (ret_buf)
2490                 g_free(ret_buf);
2491
2492         if (bo)
2493                 tbm_bo_unref(bo);
2494
2495         /* return buffer */
2496         if (key != 0) {
2497                 LOGD("send msg to release buffer. key:%d", key);
2498                 player_msg_send1_async(MUSE_PLAYER_API_RETURN_BUFFER, pc, INT, key);
2499         }
2500
2501         return ret;
2502 }
2503
2504 int player_audio_effect_get_equalizer_bands_count(player_h player, int *pcount)
2505 {
2506         PLAYER_INSTANCE_CHECK(player);
2507         PLAYER_NULL_ARG_CHECK(pcount);
2508         int ret = PLAYER_ERROR_NONE;
2509         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_GET_EQUALIZER_BANDS_COUNT;
2510         player_cli_s *pc = (player_cli_s *) player;
2511         char *ret_buf = NULL;
2512         int count;
2513
2514         LOGD("ENTER");
2515
2516         player_msg_send(api, pc, ret_buf, ret);
2517         if (ret == PLAYER_ERROR_NONE) {
2518                 player_msg_get(count, ret_buf);
2519                 *pcount = count;
2520         }
2521         g_free(ret_buf);
2522         return ret;
2523 }
2524
2525 int player_audio_effect_set_equalizer_all_bands(player_h player, int *band_levels, int length)
2526 {
2527         PLAYER_INSTANCE_CHECK(player);
2528         PLAYER_NULL_ARG_CHECK(band_levels);
2529         int ret = PLAYER_ERROR_NONE;
2530         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_SET_EQUALIZER_ALL_BANDS;
2531         player_cli_s *pc = (player_cli_s *) player;
2532         char *ret_buf = NULL;
2533
2534         LOGD("ENTER");
2535
2536         player_msg_send_array(api, pc, ret_buf, ret, band_levels, length, sizeof(int));
2537
2538         g_free(ret_buf);
2539         return ret;
2540
2541 }
2542
2543 int player_audio_effect_set_equalizer_band_level(player_h player, int index, int level)
2544 {
2545         PLAYER_INSTANCE_CHECK(player);
2546         int ret = PLAYER_ERROR_NONE;
2547         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_SET_EQUALIZER_BAND_LEVEL;
2548         player_cli_s *pc = (player_cli_s *) player;
2549         char *ret_buf = NULL;
2550
2551         LOGD("ENTER");
2552
2553         player_msg_send2(api, pc, ret_buf, ret, INT, index, INT, level);
2554
2555         g_free(ret_buf);
2556         return ret;
2557 }
2558
2559 int player_audio_effect_get_equalizer_band_level(player_h player, int index, int *plevel)
2560 {
2561         PLAYER_INSTANCE_CHECK(player);
2562         PLAYER_NULL_ARG_CHECK(plevel);
2563         int ret = PLAYER_ERROR_NONE;
2564         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_GET_EQUALIZER_BAND_LEVEL;
2565         player_cli_s *pc = (player_cli_s *) player;
2566         char *ret_buf = NULL;
2567         int level;
2568
2569         LOGD("ENTER");
2570
2571         player_msg_send1(api, pc, ret_buf, ret, INT, index);
2572         if (ret == PLAYER_ERROR_NONE) {
2573                 player_msg_get(level, ret_buf);
2574                 *plevel = level;
2575         }
2576         g_free(ret_buf);
2577         return ret;
2578 }
2579
2580 int player_audio_effect_get_equalizer_level_range(player_h player, int *pmin, int *pmax)
2581 {
2582         PLAYER_INSTANCE_CHECK(player);
2583         PLAYER_NULL_ARG_CHECK(pmin);
2584         PLAYER_NULL_ARG_CHECK(pmax);
2585         int ret = PLAYER_ERROR_NONE;
2586         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_GET_EQUALIZER_LEVEL_RANGE;
2587         player_cli_s *pc = (player_cli_s *) player;
2588         char *ret_buf = NULL;
2589         int min = 0, max = 0;
2590
2591         LOGD("ENTER");
2592
2593         player_msg_send(api, pc, ret_buf, ret);
2594         if (ret == PLAYER_ERROR_NONE) {
2595                 bool ret_val = TRUE;
2596                 player_msg_get2(ret_buf, min, INT, max, INT, ret_val);
2597                 if (ret_val) {
2598                         *pmin = min;
2599                         *pmax = max;
2600                 } else {
2601                         ret = PLAYER_ERROR_INVALID_OPERATION;
2602                 }
2603         }
2604         g_free(ret_buf);
2605         return ret;
2606 }
2607
2608 int player_audio_effect_get_equalizer_band_frequency(player_h player, int index, int *pfrequency)
2609 {
2610         PLAYER_INSTANCE_CHECK(player);
2611         PLAYER_NULL_ARG_CHECK(pfrequency);
2612         int ret = PLAYER_ERROR_NONE;
2613         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_GET_EQUALIZER_BAND_FREQUENCY;
2614         player_cli_s *pc = (player_cli_s *) player;
2615         char *ret_buf = NULL;
2616         int frequency;
2617
2618         LOGD("ENTER");
2619
2620         player_msg_send1(api, pc, ret_buf, ret, INT, index);
2621         if (ret == PLAYER_ERROR_NONE) {
2622                 player_msg_get(frequency, ret_buf);
2623                 *pfrequency = frequency;
2624         }
2625         g_free(ret_buf);
2626         return ret;
2627 }
2628
2629 int player_audio_effect_get_equalizer_band_frequency_range(player_h player, int index, int *prange)
2630 {
2631         PLAYER_INSTANCE_CHECK(player);
2632         PLAYER_NULL_ARG_CHECK(prange);
2633         int ret = PLAYER_ERROR_NONE;
2634         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_GET_EQUALIZER_BAND_FREQUENCY_RANGE;
2635         player_cli_s *pc = (player_cli_s *) player;
2636         char *ret_buf = NULL;
2637         int range;
2638
2639         LOGD("ENTER");
2640
2641         player_msg_send1(api, pc, ret_buf, ret, INT, index);
2642         if (ret == PLAYER_ERROR_NONE) {
2643                 player_msg_get(range, ret_buf);
2644                 *prange = range;
2645         }
2646         g_free(ret_buf);
2647         return ret;
2648 }
2649
2650 int player_audio_effect_equalizer_clear(player_h player)
2651 {
2652         PLAYER_INSTANCE_CHECK(player);
2653         int ret = PLAYER_ERROR_NONE;
2654         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_EQUALIZER_CLEAR;
2655         player_cli_s *pc = (player_cli_s *) player;
2656         char *ret_buf = NULL;
2657
2658         LOGD("ENTER");
2659
2660         player_msg_send(api, pc, ret_buf, ret);
2661         g_free(ret_buf);
2662         return ret;
2663 }
2664
2665 int player_audio_effect_equalizer_is_available(player_h player, bool * pavailable)
2666 {
2667         PLAYER_INSTANCE_CHECK(player);
2668         PLAYER_NULL_ARG_CHECK(pavailable);
2669         int ret = PLAYER_ERROR_NONE;
2670         muse_player_api_e api = MUSE_PLAYER_API_AUDIO_EFFECT_EQUALIZER_IS_AVAILABLE;
2671         player_cli_s *pc = (player_cli_s *) player;
2672         char *ret_buf = NULL;
2673         int available;
2674
2675         LOGD("ENTER");
2676
2677         player_msg_send(api, pc, ret_buf, ret);
2678         if (ret == PLAYER_ERROR_NONE) {
2679                 player_msg_get(available, ret_buf);
2680                 *pavailable = available;
2681         }
2682         g_free(ret_buf);
2683         return ret;
2684 }
2685
2686 int player_set_subtitle_path(player_h player, const char *path)
2687 {
2688         PLAYER_INSTANCE_CHECK(player);
2689         int ret = PLAYER_ERROR_NONE;
2690         muse_player_api_e api = MUSE_PLAYER_API_SET_SUBTITLE_PATH;
2691         player_cli_s *pc = (player_cli_s *) player;
2692         char *ret_buf = NULL;
2693
2694         LOGD("ENTER");
2695
2696         player_msg_send1(api, pc, ret_buf, ret, STRING, path);
2697         g_free(ret_buf);
2698         return ret;
2699 }
2700
2701 int player_set_subtitle_position_offset(player_h player, int millisecond)
2702 {
2703         PLAYER_INSTANCE_CHECK(player);
2704         int ret = PLAYER_ERROR_NONE;
2705         muse_player_api_e api = MUSE_PLAYER_API_SET_SUBTITLE_POSITION_OFFSET;
2706         player_cli_s *pc = (player_cli_s *) player;
2707         char *ret_buf = NULL;
2708
2709         LOGD("ENTER");
2710
2711         player_msg_send1(api, pc, ret_buf, ret, INT, millisecond);
2712
2713         g_free(ret_buf);
2714         return ret;
2715 }
2716
2717 int player_set_progressive_download_path(player_h player, const char *path)
2718 {
2719         PLAYER_INSTANCE_CHECK(player);
2720         PLAYER_NULL_ARG_CHECK(path);
2721         int ret = PLAYER_ERROR_NONE;
2722         muse_player_api_e api = MUSE_PLAYER_API_SET_PROGRESSIVE_DOWNLOAD_PATH;
2723         player_cli_s *pc = (player_cli_s *) player;
2724         char *ret_buf = NULL;
2725
2726         LOGD("ENTER");
2727
2728         player_msg_send1(api, pc, ret_buf, ret, STRING, path);
2729         g_free(ret_buf);
2730         return ret;
2731 }
2732
2733 int player_get_progressive_download_status(player_h player, unsigned long *pcurrent, unsigned long *ptotal_size)
2734 {
2735         PLAYER_INSTANCE_CHECK(player);
2736         PLAYER_NULL_ARG_CHECK(pcurrent);
2737         PLAYER_NULL_ARG_CHECK(ptotal_size);
2738         int ret = PLAYER_ERROR_NONE;
2739         muse_player_api_e api = MUSE_PLAYER_API_GET_PROGRESSIVE_DOWNLOAD_STATUS;
2740         player_cli_s *pc = (player_cli_s *) player;
2741         char *ret_buf = NULL;
2742         unsigned long current, total_size;
2743
2744         LOGD("ENTER");
2745
2746         player_msg_send(api, pc, ret_buf, ret);
2747         if (ret == PLAYER_ERROR_NONE) {
2748                 bool ret_val = TRUE;
2749                 player_msg_get2(ret_buf, current, POINTER, total_size, POINTER, ret_val);
2750                 if (ret_val) {
2751                         *pcurrent = current;
2752                         *ptotal_size = total_size;
2753                 } else {
2754                         ret = PLAYER_ERROR_INVALID_OPERATION;
2755                 }
2756         }
2757         g_free(ret_buf);
2758         return ret;
2759
2760 }
2761
2762 int player_capture_video(player_h player, player_video_captured_cb callback, void *user_data)
2763 {
2764         PLAYER_INSTANCE_CHECK(player);
2765         PLAYER_NULL_ARG_CHECK(callback);
2766         int ret = PLAYER_ERROR_NONE;
2767         muse_player_api_e api = MUSE_PLAYER_API_CAPTURE_VIDEO;
2768         player_cli_s *pc = (player_cli_s *) player;
2769         char *ret_buf = NULL;
2770
2771         LOGD("ENTER");
2772         if (pc->cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_CAPTURE]) {
2773                 LOGE("PLAYER_ERROR_VIDEO_CAPTURE_FAILED (0x%08x) : capturing...", PLAYER_ERROR_VIDEO_CAPTURE_FAILED);
2774                 return PLAYER_ERROR_VIDEO_CAPTURE_FAILED;
2775         } else {
2776                 LOGI("Event type : %d ", MUSE_PLAYER_EVENT_TYPE_CAPTURE);
2777                 pc->cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_CAPTURE] = callback;
2778                 pc->cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_CAPTURE] = user_data;
2779         }
2780
2781         player_msg_send(api, pc, ret_buf, ret);
2782
2783         if (ret != PLAYER_ERROR_NONE)
2784                 set_null_user_cb(pc->cb_info, MUSE_PLAYER_EVENT_TYPE_CAPTURE);
2785
2786         g_free(ret_buf);
2787         return ret;
2788 }
2789
2790 int player_set_streaming_cookie(player_h player, const char *cookie, int size)
2791 {
2792         PLAYER_INSTANCE_CHECK(player);
2793         PLAYER_NULL_ARG_CHECK(cookie);
2794         PLAYER_CHECK_CONDITION(size >= 0, PLAYER_ERROR_INVALID_PARAMETER, "PLAYER_ERROR_INVALID_PARAMETER");
2795         int ret = PLAYER_ERROR_NONE;
2796         muse_player_api_e api = MUSE_PLAYER_API_SET_STREAMING_COOKIE;
2797         player_cli_s *pc = (player_cli_s *) player;
2798         char *ret_buf = NULL;
2799
2800         LOGD("ENTER");
2801
2802         player_msg_send2(api, pc, ret_buf, ret, STRING, cookie, INT, size);
2803         g_free(ret_buf);
2804         return ret;
2805 }
2806
2807 int player_set_streaming_user_agent(player_h player, const char *user_agent, int size)
2808 {
2809         PLAYER_INSTANCE_CHECK(player);
2810         PLAYER_NULL_ARG_CHECK(user_agent);
2811         PLAYER_CHECK_CONDITION(size >= 0, PLAYER_ERROR_INVALID_PARAMETER, "PLAYER_ERROR_INVALID_PARAMETER");
2812         int ret = PLAYER_ERROR_NONE;
2813         muse_player_api_e api = MUSE_PLAYER_API_SET_STREAMING_USER_AGENT;
2814         player_cli_s *pc = (player_cli_s *) player;
2815         char *ret_buf = NULL;
2816
2817         LOGD("ENTER");
2818
2819         player_msg_send2(api, pc, ret_buf, ret, STRING, user_agent, INT, size);
2820         g_free(ret_buf);
2821         return ret;
2822 }
2823
2824 int player_get_streaming_download_progress(player_h player, int *pstart, int *pcurrent)
2825 {
2826         PLAYER_INSTANCE_CHECK(player);
2827         PLAYER_NULL_ARG_CHECK(pstart);
2828         PLAYER_NULL_ARG_CHECK(pcurrent);
2829         int ret = PLAYER_ERROR_NONE;
2830         muse_player_api_e api = MUSE_PLAYER_API_GET_STREAMING_DOWNLOAD_PROGRESS;
2831         player_cli_s *pc = (player_cli_s *) player;
2832         char *ret_buf = NULL;
2833         int start = 0, current = 0;
2834
2835         LOGD("ENTER");
2836
2837         player_msg_send(api, pc, ret_buf, ret);
2838         if (ret == PLAYER_ERROR_NONE) {
2839                 bool ret_val = TRUE;
2840                 player_msg_get2(ret_buf, start, INT, current, INT, ret_val);
2841                 if (ret_val) {
2842                         *pstart = start;
2843                         *pcurrent = current;
2844                 } else {
2845                         ret = PLAYER_ERROR_INVALID_OPERATION;
2846                 }
2847         }
2848         g_free(ret_buf);
2849         return ret;
2850
2851 }
2852
2853 int player_set_completed_cb(player_h player, player_completed_cb callback, void *user_data)
2854 {
2855         return __set_callback(MUSE_PLAYER_EVENT_TYPE_COMPLETE, player, callback, user_data);
2856 }
2857
2858 int player_unset_completed_cb(player_h player)
2859 {
2860         return __unset_callback(MUSE_PLAYER_EVENT_TYPE_COMPLETE, player);
2861 }
2862
2863 int player_set_interrupted_cb(player_h player, player_interrupted_cb callback, void *user_data)
2864 {
2865         return __set_callback(MUSE_PLAYER_EVENT_TYPE_INTERRUPT, player, callback, user_data);
2866 }
2867
2868 int player_unset_interrupted_cb(player_h player)
2869 {
2870         return __unset_callback(MUSE_PLAYER_EVENT_TYPE_INTERRUPT, player);
2871 }
2872
2873 int player_set_error_cb(player_h player, player_error_cb callback, void *user_data)
2874 {
2875         return __set_callback(MUSE_PLAYER_EVENT_TYPE_ERROR, player, callback, user_data);
2876 }
2877
2878 int player_unset_error_cb(player_h player)
2879 {
2880         return __unset_callback(MUSE_PLAYER_EVENT_TYPE_ERROR, player);
2881 }
2882
2883 int player_set_buffering_cb(player_h player, player_buffering_cb callback, void *user_data)
2884 {
2885         return __set_callback(MUSE_PLAYER_EVENT_TYPE_BUFFERING, player, callback, user_data);
2886 }
2887
2888 int player_unset_buffering_cb(player_h player)
2889 {
2890         return __unset_callback(MUSE_PLAYER_EVENT_TYPE_BUFFERING, player);
2891 }
2892
2893 int player_set_subtitle_updated_cb(player_h player, player_subtitle_updated_cb callback, void *user_data)
2894 {
2895         return __set_callback(MUSE_PLAYER_EVENT_TYPE_SUBTITLE, player, callback, user_data);
2896 }
2897
2898 int player_unset_subtitle_updated_cb(player_h player)
2899 {
2900         return __unset_callback(MUSE_PLAYER_EVENT_TYPE_SUBTITLE, player);
2901 }
2902
2903 int player_set_progressive_download_message_cb(player_h player, player_pd_message_cb callback, void *user_data)
2904 {
2905         PLAYER_INSTANCE_CHECK(player);
2906         PLAYER_NULL_ARG_CHECK(callback);
2907         int ret = PLAYER_ERROR_NONE;
2908         player_cli_s *pc = (player_cli_s *) player;
2909         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
2910         char *ret_buf = NULL;
2911         muse_player_event_e type = MUSE_PLAYER_EVENT_TYPE_PD;
2912         int set = 1;
2913
2914         LOGD("ENTER");
2915
2916         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
2917
2918         if (ret == PLAYER_ERROR_NONE) {
2919                 pc->cb_info->user_cb[type] = callback;
2920                 pc->cb_info->user_data[type] = user_data;
2921                 LOGI("Event type : %d ", type);
2922         }
2923
2924         g_free(ret_buf);
2925         return ret;
2926 }
2927
2928 int player_unset_progressive_download_message_cb(player_h player)
2929 {
2930         PLAYER_INSTANCE_CHECK(player);
2931         int ret = PLAYER_ERROR_NONE;
2932         player_cli_s *pc = (player_cli_s *) player;
2933         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
2934         char *ret_buf = NULL;
2935         muse_player_event_e type = MUSE_PLAYER_EVENT_TYPE_PD;
2936         int set = 0;
2937
2938         LOGD("ENTER");
2939
2940         set_null_user_cb_lock(pc->cb_info, type);
2941
2942         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
2943
2944         g_free(ret_buf);
2945         return ret;
2946
2947 }
2948
2949 int player_set_media_packet_video_frame_decoded_cb(player_h player, player_media_packet_video_decoded_cb callback, void *user_data)
2950 {
2951         PLAYER_INSTANCE_CHECK(player);
2952         PLAYER_NULL_ARG_CHECK(callback);
2953         int ret = PLAYER_ERROR_NONE;
2954         player_cli_s *pc = (player_cli_s *) player;
2955         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
2956         char *ret_buf = NULL;
2957         muse_player_event_e type = MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME;
2958         int set = 1;
2959
2960         LOGD("ENTER");
2961
2962         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
2963
2964         if (ret == PLAYER_ERROR_NONE) {
2965                 pc->cb_info->user_cb[MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME] = callback;
2966                 pc->cb_info->user_data[MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME] = user_data;
2967                 LOGI("Event type : %d ", MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME);
2968         }
2969
2970         g_free(ret_buf);
2971         return ret;
2972 }
2973
2974 int player_unset_media_packet_video_frame_decoded_cb(player_h player)
2975 {
2976         PLAYER_INSTANCE_CHECK(player);
2977         int ret = PLAYER_ERROR_NONE;
2978         player_cli_s *pc = (player_cli_s *) player;
2979         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
2980         char *ret_buf = NULL;
2981         muse_player_event_e type = MUSE_PLAYER_EVENT_TYPE_MEDIA_PACKET_VIDEO_FRAME;
2982         int set = 0;
2983
2984         LOGD("ENTER");
2985
2986         set_null_user_cb_lock(pc->cb_info, type);
2987
2988         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
2989
2990         g_free(ret_buf);
2991         return ret;
2992 }
2993
2994 int player_set_video_stream_changed_cb(player_h player, player_video_stream_changed_cb callback, void *user_data)
2995 {
2996         PLAYER_INSTANCE_CHECK(player);
2997         PLAYER_NULL_ARG_CHECK(callback);
2998         int ret = PLAYER_ERROR_NONE;
2999         player_cli_s *pc = (player_cli_s *) player;
3000         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
3001         char *ret_buf = NULL;
3002         muse_player_event_e type = MUSE_PLAYER_EVENT_TYPE_VIDEO_STREAM_CHANGED;
3003         int set = 1;
3004
3005         LOGD("ENTER");
3006
3007         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
3008
3009         if (ret == PLAYER_ERROR_NONE) {
3010                 pc->cb_info->user_cb[type] = callback;
3011                 pc->cb_info->user_data[type] = user_data;
3012                 LOGI("Event type : %d ", type);
3013         }
3014
3015         g_free(ret_buf);
3016         return ret;
3017 }
3018
3019 int player_unset_video_stream_changed_cb(player_h player)
3020 {
3021         PLAYER_INSTANCE_CHECK(player);
3022         int ret = PLAYER_ERROR_NONE;
3023         player_cli_s *pc = (player_cli_s *) player;
3024         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
3025         char *ret_buf = NULL;
3026         muse_player_event_e type = MUSE_PLAYER_EVENT_TYPE_VIDEO_STREAM_CHANGED;
3027         int set = 0;
3028
3029         LOGD("ENTER");
3030
3031         set_null_user_cb_lock(pc->cb_info, type);
3032
3033         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
3034
3035         g_free(ret_buf);
3036         return ret;
3037 }
3038
3039 int player_set_media_stream_buffer_status_cb(player_h player, player_stream_type_e stream_type, player_media_stream_buffer_status_cb callback, void *user_data)
3040 {
3041         PLAYER_INSTANCE_CHECK(player);
3042         PLAYER_NULL_ARG_CHECK(callback);
3043         int ret = PLAYER_ERROR_NONE;
3044         player_cli_s *pc = (player_cli_s *) player;
3045         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
3046         char *ret_buf = NULL;
3047         muse_player_event_e type;
3048         int set = 1;
3049
3050         LOGD("ENTER");
3051
3052         if (stream_type == PLAYER_STREAM_TYPE_VIDEO)
3053                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS;
3054         else if (stream_type == PLAYER_STREAM_TYPE_AUDIO)
3055                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS;
3056         else {
3057                 LOGE("PLAYER_ERROR_INVALID_PARAMETER(type : %d)", stream_type);
3058                 return PLAYER_ERROR_INVALID_PARAMETER;
3059         }
3060
3061         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
3062
3063         if (ret == PLAYER_ERROR_NONE) {
3064                 pc->cb_info->user_cb[type] = callback;
3065                 pc->cb_info->user_data[type] = user_data;
3066                 LOGI("Event type : %d ", type);
3067         }
3068
3069         g_free(ret_buf);
3070         return ret;
3071 }
3072
3073 int player_unset_media_stream_buffer_status_cb(player_h player, player_stream_type_e stream_type)
3074 {
3075         PLAYER_INSTANCE_CHECK(player);
3076         int ret = PLAYER_ERROR_NONE;
3077         player_cli_s *pc = (player_cli_s *) player;
3078         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
3079         char *ret_buf = NULL;
3080         muse_player_event_e type;
3081         int set = 0;
3082
3083         LOGD("ENTER");
3084
3085         if (stream_type == PLAYER_STREAM_TYPE_VIDEO)
3086                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_BUFFER_STATUS;
3087         else if (stream_type == PLAYER_STREAM_TYPE_AUDIO)
3088                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_BUFFER_STATUS;
3089         else {
3090                 LOGE("PLAYER_ERROR_INVALID_PARAMETER(type : %d)", stream_type);
3091                 return PLAYER_ERROR_INVALID_PARAMETER;
3092         }
3093
3094         set_null_user_cb_lock(pc->cb_info, type);
3095
3096         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
3097
3098         g_free(ret_buf);
3099         return ret;
3100 }
3101
3102 int player_set_media_stream_seek_cb(player_h player, player_stream_type_e stream_type, player_media_stream_seek_cb callback, void *user_data)
3103 {
3104         PLAYER_INSTANCE_CHECK(player);
3105         PLAYER_NULL_ARG_CHECK(callback);
3106         int ret = PLAYER_ERROR_NONE;
3107         player_cli_s *pc = (player_cli_s *) player;
3108         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
3109         char *ret_buf = NULL;
3110         muse_player_event_e type;
3111         int set = 1;
3112
3113         LOGD("ENTER");
3114
3115         if (stream_type == PLAYER_STREAM_TYPE_VIDEO)
3116                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_SEEK;
3117         else if (stream_type == PLAYER_STREAM_TYPE_AUDIO)
3118                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_SEEK;
3119         else {
3120                 LOGE("PLAYER_ERROR_INVALID_PARAMETER(type : %d)", stream_type);
3121                 return PLAYER_ERROR_INVALID_PARAMETER;
3122         }
3123
3124         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
3125
3126         if (ret == PLAYER_ERROR_NONE) {
3127                 pc->cb_info->user_cb[type] = callback;
3128                 pc->cb_info->user_data[type] = user_data;
3129                 LOGI("Event type : %d ", type);
3130         }
3131
3132         g_free(ret_buf);
3133         return ret;
3134 }
3135
3136 int player_unset_media_stream_seek_cb(player_h player, player_stream_type_e stream_type)
3137 {
3138         PLAYER_INSTANCE_CHECK(player);
3139         int ret = PLAYER_ERROR_NONE;
3140         player_cli_s *pc = (player_cli_s *) player;
3141         muse_player_api_e api = MUSE_PLAYER_API_SET_CALLBACK;
3142         char *ret_buf = NULL;
3143         muse_player_event_e type;
3144         int set = 0;
3145
3146         LOGD("ENTER");
3147
3148         if (stream_type == PLAYER_STREAM_TYPE_VIDEO)
3149                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_VIDEO_SEEK;
3150         else if (stream_type == PLAYER_STREAM_TYPE_AUDIO)
3151                 type = MUSE_PLAYER_EVENT_TYPE_MEDIA_STREAM_AUDIO_SEEK;
3152         else {
3153                 LOGE("PLAYER_ERROR_INVALID_PARAMETER(type : %d)", stream_type);
3154                 return PLAYER_ERROR_INVALID_PARAMETER;
3155         }
3156
3157         set_null_user_cb_lock(pc->cb_info, type);
3158
3159         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, set);
3160
3161         g_free(ret_buf);
3162         return ret;
3163 }
3164
3165 /* TODO Implement raw data socket channel */
3166 int player_push_media_stream(player_h player, media_packet_h packet)
3167 {
3168         PLAYER_INSTANCE_CHECK(player);
3169         PLAYER_NULL_ARG_CHECK(packet);
3170         int ret = PLAYER_ERROR_NONE;
3171         player_cli_s *pc = (player_cli_s *) player;
3172         muse_player_api_e api = MUSE_PLAYER_API_PUSH_MEDIA_STREAM;
3173         char *ret_buf = NULL;
3174         player_push_media_msg_type push_media;
3175         char *push_media_msg = (char *)&push_media;
3176         int msg_size = sizeof(player_push_media_msg_type);
3177         int buf_size = 0;
3178 #ifdef __UN_USED
3179         tbm_bo bo = NULL;
3180         tbm_bo_handle thandle;
3181 #endif
3182         char *buf;
3183         media_format_h format;
3184         bool is_video;
3185         bool is_audio;
3186
3187         LOGD("ENTER");
3188
3189         media_packet_get_buffer_data_ptr(packet, (void **)&buf);
3190         media_packet_get_buffer_size(packet, &push_media.size);
3191         media_packet_get_pts(packet, &push_media.pts);
3192         media_packet_get_format(packet, &format);
3193         media_packet_get_flags(packet, &push_media.flags);
3194
3195         push_media.buf_type = PUSH_MEDIA_BUF_TYPE_RAW;
3196
3197         media_packet_is_video(packet, &is_video);
3198         media_packet_is_audio(packet, &is_audio);
3199         if (is_video)
3200                 media_format_get_video_info(format, &push_media.mimetype, &push_media.width, &push_media.height, NULL, NULL);
3201         else if (is_audio)
3202                 media_format_get_audio_info(format, &push_media.mimetype, NULL, NULL, NULL, NULL);
3203
3204         media_format_unref(format);
3205
3206 #ifdef __UN_USED
3207         if (push_media.buf_type == PUSH_MEDIA_BUF_TYPE_TBM) {
3208                 bo = tbm_bo_alloc(pc->cb_info->bufmgr, push_media.size, TBM_BO_DEFAULT);
3209                 if (bo == NULL) {
3210                         LOGE("TBM get error : bo is NULL");
3211                         return PLAYER_ERROR_INVALID_OPERATION;
3212                 }
3213                 thandle = tbm_bo_map(bo, TBM_DEVICE_CPU, TBM_OPTION_WRITE);
3214                 if (thandle.ptr == NULL) {
3215                         LOGE("TBM get error : handle pointer is NULL");
3216                         ret = PLAYER_ERROR_INVALID_OPERATION;
3217                         goto ERROR;
3218                 }
3219                 memcpy(thandle.ptr, buf, push_media.size);
3220                 tbm_bo_unmap(bo);
3221
3222                 push_media.key = tbm_bo_export(bo);
3223                 if (push_media.key == 0) {
3224                         LOGE("TBM get error : key is 0");
3225                         ret = PLAYER_ERROR_INVALID_OPERATION;
3226                         goto ERROR;
3227                 }
3228
3229                 player_msg_send_array(api, pc, ret_buf, ret, push_media_msg, msg_size, sizeof(char));
3230         } else if (push_media.buf_type == PUSH_MEDIA_BUF_TYPE_MSG) {
3231                 buf_size = (int)push_media.size;
3232                 player_msg_send_array2(api, pc, ret_buf, ret, push_media_msg, msg_size, sizeof(char), buf, buf_size, sizeof(char));
3233         } else
3234 #endif
3235         if (push_media.buf_type == PUSH_MEDIA_BUF_TYPE_RAW) {
3236                 buf_size = (int)push_media.size;
3237                 muse_core_ipc_push_data(pc->cb_info->data_fd, buf, buf_size, push_media.pts);
3238                 player_msg_send_array(api, pc, ret_buf, ret, push_media_msg, msg_size, sizeof(char));
3239         }
3240
3241         LOGD("ret_buf %s", ret_buf);
3242
3243 #ifdef __UN_USED
3244  ERROR:
3245         if (push_media.buf_type == PUSH_MEDIA_BUF_TYPE_TBM)
3246                 tbm_bo_unref(bo);
3247 #endif
3248
3249         g_free(ret_buf);
3250         return ret;
3251 }
3252
3253 int player_set_media_stream_info(player_h player, player_stream_type_e type, media_format_h format)
3254 {
3255         PLAYER_INSTANCE_CHECK(player);
3256         g_return_val_if_fail(format, PLAYER_ERROR_INVALID_OPERATION);
3257         int ret = PLAYER_ERROR_NONE;
3258         player_cli_s *pc = (player_cli_s *) player;
3259         muse_player_api_e api = MUSE_PLAYER_API_SET_MEDIA_STREAM_INFO;
3260         char *ret_buf = NULL;
3261         media_format_mimetype_e mimetype;
3262         int width;
3263         int height;
3264         int avg_bps;
3265         int max_bps;
3266         int channel;
3267         int samplerate;
3268         int bit;
3269
3270         LOGD("ENTER");
3271
3272         media_format_ref(format);
3273         if (type == PLAYER_STREAM_TYPE_VIDEO) {
3274                 media_format_get_video_info(format, &mimetype, &width, &height, &avg_bps, &max_bps);
3275                 player_msg_send6(api, pc, ret_buf, ret, INT, type, INT, mimetype, INT, width, INT, height, INT, avg_bps, INT, max_bps);
3276         } else if (type == PLAYER_STREAM_TYPE_AUDIO) {
3277                 media_format_get_audio_info(format, &mimetype, &channel, &samplerate, &bit, &avg_bps);
3278                 player_msg_send6(api, pc, ret_buf, ret, INT, type, INT, mimetype, INT, channel, INT, samplerate, INT, avg_bps, INT, bit);
3279         }
3280         media_format_unref(format);
3281         pc->push_media_stream = TRUE;
3282
3283         g_free(ret_buf);
3284         return ret;
3285 }
3286
3287 int player_set_media_stream_buffer_max_size(player_h player, player_stream_type_e type, unsigned long long max_size)
3288 {
3289         int ret = PLAYER_ERROR_NONE;
3290         PLAYER_INSTANCE_CHECK(player);
3291         player_cli_s *pc = (player_cli_s *) player;
3292         muse_player_api_e api = MUSE_PLAYER_API_SET_MEDIA_STREAM_BUFFER_MAX_SIZE;
3293         char *ret_buf = NULL;
3294
3295         LOGD("ENTER");
3296
3297         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT64, max_size);
3298
3299         g_free(ret_buf);
3300         return ret;
3301 }
3302
3303 int player_get_media_stream_buffer_max_size(player_h player, player_stream_type_e type, unsigned long long *pmax_size)
3304 {
3305         int ret = PLAYER_ERROR_NONE;
3306         PLAYER_INSTANCE_CHECK(player);
3307         PLAYER_NULL_ARG_CHECK(pmax_size);
3308         player_cli_s *pc = (player_cli_s *) player;
3309         muse_player_api_e api = MUSE_PLAYER_API_GET_MEDIA_STREAM_BUFFER_MAX_SIZE;
3310         char *ret_buf = NULL;
3311         unsigned long long max_size;
3312
3313         LOGD("ENTER");
3314
3315         player_msg_send1(api, pc, ret_buf, ret, INT, type);
3316         if (ret == PLAYER_ERROR_NONE) {
3317                 player_msg_get_type(max_size, ret_buf, INT64);
3318                 *pmax_size = max_size;
3319         }
3320         g_free(ret_buf);
3321         return ret;
3322 }
3323
3324 int player_set_media_stream_buffer_min_threshold(player_h player, player_stream_type_e type, unsigned int percent)
3325 {
3326         int ret = PLAYER_ERROR_NONE;
3327         PLAYER_INSTANCE_CHECK(player);
3328         player_cli_s *pc = (player_cli_s *) player;
3329         muse_player_api_e api = MUSE_PLAYER_API_SET_MEDIA_STREAM_BUFFER_MIN_THRESHOLD;
3330         char *ret_buf = NULL;
3331
3332         LOGD("ENTER");
3333
3334         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, percent);
3335
3336         g_free(ret_buf);
3337         return ret;
3338 }
3339
3340 int player_get_media_stream_buffer_min_threshold(player_h player, player_stream_type_e type, unsigned int *ppercent)
3341 {
3342         int ret = PLAYER_ERROR_NONE;
3343         PLAYER_INSTANCE_CHECK(player);
3344         PLAYER_NULL_ARG_CHECK(ppercent);
3345         player_cli_s *pc = (player_cli_s *) player;
3346         muse_player_api_e api = MUSE_PLAYER_API_GET_MEDIA_STREAM_BUFFER_MIN_THRESHOLD;
3347         char *ret_buf = NULL;
3348         uint percent;
3349
3350         LOGD("ENTER");
3351
3352         player_msg_send1(api, pc, ret_buf, ret, INT, type);
3353         if (ret == PLAYER_ERROR_NONE) {
3354                 player_msg_get(percent, ret_buf);
3355                 *ppercent = percent;
3356         }
3357
3358         g_free(ret_buf);
3359         return ret;
3360 }
3361
3362 int player_get_track_count(player_h player, player_stream_type_e type, int *pcount)
3363 {
3364         PLAYER_INSTANCE_CHECK(player);
3365         PLAYER_NULL_ARG_CHECK(pcount);
3366         int ret = PLAYER_ERROR_NONE;
3367         player_cli_s *pc = (player_cli_s *) player;
3368         muse_player_api_e api = MUSE_PLAYER_API_GET_TRACK_COUNT;
3369         char *ret_buf = NULL;
3370         int count;
3371
3372         LOGD("ENTER");
3373
3374         player_msg_send1(api, pc, ret_buf, ret, INT, type);
3375         if (ret == PLAYER_ERROR_NONE) {
3376                 player_msg_get(count, ret_buf);
3377                 *pcount = count;
3378         }
3379
3380         g_free(ret_buf);
3381         return ret;
3382 }
3383
3384 int player_get_current_track(player_h player, player_stream_type_e type, int *pindex)
3385 {
3386         PLAYER_INSTANCE_CHECK(player);
3387         PLAYER_NULL_ARG_CHECK(pindex);
3388         int ret = PLAYER_ERROR_NONE;
3389         player_cli_s *pc = (player_cli_s *) player;
3390         muse_player_api_e api = MUSE_PLAYER_API_GET_CURRENT_TRACK;
3391         char *ret_buf = NULL;
3392         int index;
3393
3394         LOGD("ENTER");
3395
3396         player_msg_send1(api, pc, ret_buf, ret, INT, type);
3397         if (ret == PLAYER_ERROR_NONE) {
3398                 player_msg_get(index, ret_buf);
3399                 *pindex = index;
3400         }
3401
3402         g_free(ret_buf);
3403         return ret;
3404 }
3405
3406 int player_select_track(player_h player, player_stream_type_e type, int index)
3407 {
3408         PLAYER_INSTANCE_CHECK(player);
3409         int ret = PLAYER_ERROR_NONE;
3410         player_cli_s *pc = (player_cli_s *) player;
3411         muse_player_api_e api = MUSE_PLAYER_API_SELECT_TRACK;
3412         char *ret_buf = NULL;
3413
3414         LOGD("ENTER");
3415
3416         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, index);
3417
3418         g_free(ret_buf);
3419         return ret;
3420 }
3421
3422 int player_get_track_language_code(player_h player, player_stream_type_e type, int index, char **pcode)
3423 {
3424         PLAYER_INSTANCE_CHECK(player);
3425         PLAYER_NULL_ARG_CHECK(pcode);
3426         int ret = PLAYER_ERROR_NONE;
3427         player_cli_s *pc = (player_cli_s *) player;
3428         muse_player_api_e api = MUSE_PLAYER_API_GET_TRACK_LANGUAGE_CODE;
3429         char *ret_buf = NULL;
3430         char code[MUSE_MSG_MAX_LENGTH] = { 0, };
3431         const int code_len = 2;
3432
3433         LOGD("ENTER");
3434
3435         player_msg_send2(api, pc, ret_buf, ret, INT, type, INT, index);
3436         if (ret == PLAYER_ERROR_NONE) {
3437                 if (player_msg_get_string(code, ret_buf))
3438                         *pcode = strndup(code, code_len);
3439         }
3440
3441         g_free(ret_buf);
3442         return ret;
3443 }