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