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