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