Remove timeout for handle creation API
[platform/core/api/recorder.git] / src / recorder.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 <camera_internal.h>
21 #include <recorder.h>
22 #include <sound_manager.h>
23 #include <sound_manager_internal.h>
24 #include <storage.h>
25 #include <storage-internal.h>
26 #include <muse_recorder.h>
27 #include <muse_recorder_msg.h>
28 #include <muse_core_ipc.h>
29 #include <muse_core_module.h>
30 #include <recorder_private.h>
31 #include <glib.h>
32 #include <gio/gio.h>
33 #include <dlog.h>
34 #include <tzplatform_config.h>
35
36 #ifdef LOG_TAG
37 #undef LOG_TAG
38 #endif
39 #define LOG_TAG "TIZEN_N_RECORDER"
40
41 /* for device changed callback */
42 static GMutex g_rec_dev_state_changed_cb_lock;
43 static GList *g_rec_dev_state_changed_cb_list;
44 static int g_rec_dev_state_changed_cb_id;
45 static GDBusConnection *g_rec_dev_state_changed_cb_conn;
46 static guint g_rec_dev_state_changed_cb_subscribe_id;
47
48
49
50 static void __recorder_update_api_waiting(recorder_cb_info_s *cb_info, int api, int value)
51 {
52         if (!cb_info ||
53                 api < 0 || api >= MUSE_RECORDER_API_MAX) {
54                 LOGE("invalid param %p %d", cb_info, api);
55                 return;
56         }
57
58         g_mutex_lock(&(cb_info->api_mutex[api]));
59         cb_info->api_waiting[api] += value;
60         g_mutex_unlock(&(cb_info->api_mutex[api]));
61
62         /*LOGD("api %d, value %d, waiting %d",
63                 api, value, cb_info->api_waiting[api]);*/
64
65         return;
66 }
67
68
69 static void __recorder_device_state_changed_cb(GDBusConnection *connection,
70         const gchar *sender_name, const gchar *object_path, const gchar *interface_name,
71         const gchar *signal_name, GVariant *param, gpointer user_data)
72 {
73         int value = 0;
74         recorder_type_e type = RECORDER_TYPE_AUDIO;
75         recorder_device_state_e state = RECORDER_DEVICE_STATE_IDLE;
76         GList *tmp_list = NULL;
77         recorder_cb_info *info = NULL;
78
79         g_mutex_lock(&g_rec_dev_state_changed_cb_lock);
80
81         if (!g_rec_dev_state_changed_cb_list || !param) {
82                 LOGW("no callback or NULL param %p", param);
83                 goto _DONE;
84         }
85
86         /* get device type and state */
87         g_variant_get(param, "(i)", &value);
88
89         type = value >> 16;
90         state = 0x0000ffff & value;
91
92         LOGD("type %d, state %d", type, state);
93
94         tmp_list = g_rec_dev_state_changed_cb_list;
95
96         do {
97                 info = (recorder_cb_info *)tmp_list->data;
98
99                 if (info) {
100                         if (info->callback) {
101                                 LOGD("start id[%d] callback", info->id);
102                                 ((recorder_device_state_changed_cb)info->callback)(type, state, info->user_data);
103                                 LOGD("returned id[%d] callback", info->id);
104                         } else {
105                                 LOGW("NULL callback for id %d", info->id);
106                         }
107                 }
108
109                 tmp_list = tmp_list->next;
110         } while (tmp_list);
111
112 _DONE:
113         g_mutex_unlock(&g_rec_dev_state_changed_cb_lock);
114
115         return;
116 }
117
118
119 static int _recorder_import_tbm_key(tbm_bufmgr bufmgr, unsigned int tbm_key, tbm_bo *bo, tbm_bo_handle *bo_handle)
120 {
121         tbm_bo tmp_bo = NULL;
122         tbm_bo_handle tmp_bo_handle = {NULL, };
123
124         if (bufmgr == NULL || bo == NULL || bo_handle == NULL || tbm_key == 0) {
125                 LOGE("invalid parameter - bufmgr %p, bo %p, bo_handle %p, key %d",
126                      bufmgr, bo, bo_handle, tbm_key);
127                 return false;
128         }
129
130         tmp_bo = tbm_bo_import(bufmgr, tbm_key);
131         if (tmp_bo == NULL) {
132                 LOGE("bo import failed - bufmgr %p, key %d", bufmgr, tbm_key);
133                 return false;
134         }
135
136         tmp_bo_handle = tbm_bo_map(tmp_bo, TBM_DEVICE_CPU, TBM_OPTION_READ);
137         if (tmp_bo_handle.ptr == NULL) {
138                 LOGE("bo map failed %p", tmp_bo);
139                 tbm_bo_unref(tmp_bo);
140                 tmp_bo = NULL;
141                 return false;
142         }
143
144         /* set bo and bo_handle */
145         *bo = tmp_bo;
146         *bo_handle = tmp_bo_handle;
147
148         return true;
149 }
150
151 static void _recorder_release_imported_bo(tbm_bo *bo)
152 {
153         if (bo == NULL || *bo == NULL) {
154                 LOGW("NULL bo");
155                 return;
156         }
157
158         tbm_bo_unmap(*bo);
159         tbm_bo_unref(*bo);
160         *bo = NULL;
161
162         return;
163 }
164
165 static void _recorder_client_user_callback(recorder_cb_info_s *cb_info, char *recv_msg, muse_recorder_event_e event)
166 {
167         if (recv_msg == NULL || event >= MUSE_RECORDER_EVENT_TYPE_NUM) {
168                 LOGE("invalid parameter - recorder msg %p, event %d", recv_msg, event);
169                 return;
170         }
171
172         /*LOGD("get recorder msg %s, event %d", recv_msg, event);*/
173
174         if (cb_info->user_cb[event] == NULL) {
175                 LOGW("user callback for event %d is not set", event);
176                 return;
177         }
178
179         switch (event) {
180         case MUSE_RECORDER_EVENT_TYPE_STATE_CHANGE:
181                 {
182                         int previous = 0;
183                         int current = 0;
184                         int by_policy = 0;
185
186                         muse_recorder_msg_get(previous, recv_msg);
187                         muse_recorder_msg_get(current, recv_msg);
188                         muse_recorder_msg_get(by_policy, recv_msg);
189
190                         ((recorder_state_changed_cb)cb_info->user_cb[event])((recorder_state_e)previous,
191                                 (recorder_state_e)current,
192                                 (bool)by_policy,
193                                 cb_info->user_data[event]);
194                         break;
195                 }
196         case MUSE_RECORDER_EVENT_TYPE_RECORDING_LIMITED:
197                 {
198                         int type = 0;
199
200                         muse_recorder_msg_get(type, recv_msg);
201
202                         ((recorder_recording_limit_reached_cb)cb_info->user_cb[event])((recorder_recording_limit_type_e)type,
203                                 cb_info->user_data[event]);
204                         break;
205                 }
206         case MUSE_RECORDER_EVENT_TYPE_RECORDING_STATUS:
207                 {
208                         int64_t cb_elapsed_time = 0;
209                         int64_t cb_file_size = 0;
210
211                         muse_recorder_msg_get(cb_elapsed_time, recv_msg);
212                         muse_recorder_msg_get(cb_file_size, recv_msg);
213
214                         ((recorder_recording_status_cb)cb_info->user_cb[event])((unsigned long long)cb_elapsed_time,
215                                 (unsigned long long)cb_file_size,
216                                 cb_info->user_data[event]);
217                         break;
218                 }
219         case MUSE_RECORDER_EVENT_TYPE_INTERRUPTED:
220                 {
221                         int policy = 0;
222                         int previous = 0;
223                         int current = 0;
224
225                         muse_recorder_msg_get(policy, recv_msg);
226                         muse_recorder_msg_get(previous, recv_msg);
227                         muse_recorder_msg_get(current, recv_msg);
228
229                         if (policy == RECORDER_POLICY_SOUND)
230                                 LOGW("DEPRECATION WARNING: RECORDER_POLICY_SOUND is deprecated and will be removed from next release.");
231                         else if (policy == RECORDER_POLICY_SOUND_BY_CALL)
232                                 LOGW("DEPRECATION WARNING: RECORDER_POLICY_SOUND_BY_CALL is deprecated and will be removed from next release.");
233                         else if (policy == RECORDER_POLICY_SOUND_BY_ALARM)
234                                 LOGW("DEPRECATION WARNING: RECORDER_POLICY_SOUND_BY_ALARM is deprecated and will be removed from next release.");
235
236                         ((recorder_interrupted_cb)cb_info->user_cb[event])((recorder_policy_e)policy,
237                                 (recorder_state_e)previous,
238                                 (recorder_state_e)current,
239                                 cb_info->user_data[event]);
240                         break;
241                 }
242         case MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM:
243                 {
244                         int tbm_key = 0;
245                         int size = 0;
246                         int format = 0;
247                         int channel = 0;
248                         int timestamp = 0;
249                         tbm_bo bo = NULL;
250                         tbm_bo_handle bo_handle = {.ptr = NULL};
251                         char *send_msg = NULL;
252
253                         muse_recorder_msg_get(tbm_key, recv_msg);
254                         if (tbm_key == 0) {
255                                 LOGE("invalid key");
256                                 break;
257                         }
258
259                         if (!_recorder_import_tbm_key(cb_info->bufmgr, tbm_key, &bo, &bo_handle)) {
260                                 LOGE("tbm key %d import failed", tbm_key);
261                                 break;
262                         }
263
264                         muse_recorder_msg_get(size, recv_msg);
265                         muse_recorder_msg_get(format, recv_msg);
266                         muse_recorder_msg_get(channel, recv_msg);
267                         muse_recorder_msg_get(timestamp, recv_msg);
268
269                         ((recorder_audio_stream_cb)cb_info->user_cb[event])((void *)bo_handle.ptr,
270                                 size,
271                                 (audio_sample_type_e)format,
272                                 channel,
273                                 (unsigned int)timestamp,
274                                 cb_info->user_data[event]);
275
276                         /* release imported bo */
277                         _recorder_release_imported_bo(&bo);
278
279                         /* return buffer */
280                         send_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_API_RETURN_BUFFER,
281                                 MUSE_TYPE_INT, "tbm_key", tbm_key, NULL);
282
283                         if (muse_core_ipc_send_msg(cb_info->fd, send_msg) <= 0)
284                                 LOGE("sending message failed");
285
286                         muse_core_msg_json_factory_free(send_msg);
287                         break;
288                 }
289         case MUSE_RECORDER_EVENT_TYPE_MUXED_STREAM:
290                 {
291                         int tbm_key = 0;
292                         int size = 0;
293                         int64_t offset = 0;
294
295                         tbm_bo bo = NULL;
296                         tbm_bo_handle bo_handle = {.ptr = NULL};
297                         char *send_msg = NULL;
298
299                         muse_recorder_msg_get(tbm_key, recv_msg);
300                         if (tbm_key == 0) {
301                                 LOGE("invalid key");
302                                 break;
303                         }
304
305                         if (!_recorder_import_tbm_key(cb_info->bufmgr, tbm_key, &bo, &bo_handle)) {
306                                 LOGE("tbm key %d import failed", tbm_key);
307                                 break;
308                         }
309
310                         muse_recorder_msg_get(size, recv_msg);
311                         muse_recorder_msg_get(offset, recv_msg);
312
313                         ((recorder_muxed_stream_cb)cb_info->user_cb[event])((void *)bo_handle.ptr,
314                                 size, (unsigned long long)offset, cb_info->user_data[event]);
315
316                         /* release imported bo */
317                         _recorder_release_imported_bo(&bo);
318
319                         /* return buffer */
320                         send_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_API_RETURN_BUFFER,
321                                 MUSE_TYPE_INT, "tbm_key", tbm_key, NULL);
322                         if (send_msg) {
323                                 if (muse_core_ipc_send_msg(cb_info->fd, send_msg) <= 0)
324                                         LOGE("sending message failed");
325
326                                 muse_core_msg_json_factory_free(send_msg);
327                                 send_msg = NULL;
328                         } else {
329                                 LOGE("failed to create send msg for key %d", tbm_key);
330                         }
331                         break;
332                 }
333         case MUSE_RECORDER_EVENT_TYPE_ERROR:
334                 {
335                         int error = 0;
336                         int current_state = 0;
337
338                         muse_recorder_msg_get(error, recv_msg);
339                         muse_recorder_msg_get(current_state, recv_msg);
340
341                         if (error == RECORDER_ERROR_SOUND_POLICY)
342                                 LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY is deprecated and will be removed from next release.");
343                         else if (error == RECORDER_ERROR_SOUND_POLICY_BY_CALL)
344                                 LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY_BY_CALL is deprecated and will be removed from next release.");
345                         else if (error == RECORDER_ERROR_SOUND_POLICY_BY_ALARM)
346                                 LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY_BY_ALARM is deprecated and will be removed from next release.");
347
348                         ((recorder_error_cb)cb_info->user_cb[event])((recorder_error_e)error,
349                                 (recorder_state_e)current_state,
350                                 cb_info->user_data[event]);
351                         break;
352                 }
353         case MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_AUDIO_ENCODER:
354                 {
355                         int codec = 0;
356
357                         muse_recorder_msg_get(codec, recv_msg);
358
359                         if (((recorder_supported_audio_encoder_cb)cb_info->user_cb[event])((recorder_audio_codec_e)codec, cb_info->user_data[event]) == false) {
360                                 cb_info->user_cb[event] = NULL;
361                                 cb_info->user_data[event] = NULL;
362                                 /*LOGD("stop foreach callback for SUPPORTED_AUDIO_ENCODER");*/
363                         }
364                         break;
365                 }
366         case MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_FILE_FORMAT:
367                 {
368                         int format = 0;
369
370                         muse_recorder_msg_get(format, recv_msg);
371
372                         if (((recorder_supported_file_format_cb)cb_info->user_cb[event])((recorder_file_format_e)format, cb_info->user_data[event]) == false) {
373                                 cb_info->user_cb[event] = NULL;
374                                 cb_info->user_data[event] = NULL;
375                                 /*LOGD("stop foreach callback for SUPPORTED_FILE_FORMAT");*/
376                         }
377                         break;
378                 }
379         case MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_ENCODER:
380                 {
381                         int codec = 0;
382
383                         muse_recorder_msg_get(codec, recv_msg);
384
385                         if (((recorder_supported_video_encoder_cb)cb_info->user_cb[event])((recorder_video_codec_e)codec, cb_info->user_data[event]) == false) {
386                                 cb_info->user_cb[event] = NULL;
387                                 cb_info->user_data[event] = NULL;
388                                 /*LOGD("stop foreach callback for SUPPORTED_VIDEO_ENCODER");*/
389                         }
390                         break;
391                 }
392         default: /* MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_RESOLUTION */
393                 {
394                         int width = 0;
395                         int height = 0;
396
397                         muse_recorder_msg_get(width, recv_msg);
398                         muse_recorder_msg_get(height, recv_msg);
399
400                         if (((recorder_supported_video_resolution_cb)cb_info->user_cb[event])(width, height, cb_info->user_data[event]) == false) {
401                                 cb_info->user_cb[event] = NULL;
402                                 cb_info->user_data[event] = NULL;
403                                 /*LOGD("stop foreach callback for SUPPORTED_VIDEO_RESOLUTION");*/
404                         }
405                         break;
406                 }
407         }
408
409         return;
410 }
411
412
413 static bool _recorder_idle_event_callback(void *data)
414 {
415         recorder_cb_info_s *cb_info = NULL;
416         recorder_idle_event_s *rec_idle_event = (recorder_idle_event_s *)data;
417
418         if (rec_idle_event == NULL) {
419                 LOGE("rec_idle_event is NULL");
420                 return false;
421         }
422
423         /* lock event */
424         g_mutex_lock(&rec_idle_event->event_mutex);
425
426         cb_info = rec_idle_event->cb_info;
427         if (cb_info == NULL) {
428                 LOGW("recorder cb_info is NULL. event %d", rec_idle_event->event);
429                 goto IDLE_EVENT_CALLBACK_DONE;
430         }
431
432         /* remove event from list */
433         g_mutex_lock(&cb_info->idle_event_mutex);
434         if (cb_info->idle_event_list)
435                 cb_info->idle_event_list = g_list_remove(cb_info->idle_event_list, (gpointer)rec_idle_event);
436
437         /*LOGD("remove recorder idle event %p, %p", rec_idle_event, cb_info->idle_event_list);*/
438         g_mutex_unlock(&cb_info->idle_event_mutex);
439
440         /* user callback */
441         _recorder_client_user_callback(rec_idle_event->cb_info, rec_idle_event->recv_msg, rec_idle_event->event);
442
443         /* send signal for waiting thread */
444         g_cond_signal(&cb_info->idle_event_cond);
445
446 IDLE_EVENT_CALLBACK_DONE:
447         /* unlock and release event */
448         g_mutex_unlock(&rec_idle_event->event_mutex);
449         g_mutex_clear(&rec_idle_event->event_mutex);
450
451         g_free(rec_idle_event);
452         rec_idle_event = NULL;
453
454         return false;
455 }
456
457
458 static void _recorder_remove_idle_event_all(recorder_cb_info_s *cb_info)
459 {
460         recorder_idle_event_s *rec_idle_event = NULL;
461         gboolean ret = true;
462         GList *list = NULL;
463         gint64 end_time = 0;
464
465         if (cb_info == NULL) {
466                 LOGE("cb_info is NULL");
467                 return;
468         }
469
470         g_mutex_lock(&cb_info->idle_event_mutex);
471
472         if (cb_info->idle_event_list == NULL) {
473                 LOGD("No event");
474         } else {
475                 list = cb_info->idle_event_list;
476
477                 while (list) {
478                         rec_idle_event = list->data;
479                         list = g_list_next(list);
480
481                         if (!rec_idle_event) {
482                                 LOGW("The event is NULL");
483                                 continue;
484                         }
485
486                         if (!g_mutex_trylock(&rec_idle_event->event_mutex)) {
487                                 LOGW("lock failed");
488
489                                 end_time = g_get_monotonic_time() + G_TIME_SPAN_MILLISECOND * 100;
490
491                                 if (g_cond_wait_until(&cb_info->idle_event_cond, &cb_info->idle_event_mutex, end_time))
492                                         LOGW("signal received");
493                                 else
494                                         LOGW("timeout");
495
496                                 continue;
497                         }
498
499                         ret = g_idle_remove_by_data(rec_idle_event);
500
501                         LOGD("remove event %p, ret %d", rec_idle_event, ret);
502
503                         if (!ret) {
504                                 rec_idle_event->cb_info = NULL;
505                                 LOGW("idle cb for event %p will be called later", rec_idle_event);
506                         }
507
508                         cb_info->idle_event_list = g_list_remove(cb_info->idle_event_list, (gpointer)rec_idle_event);
509
510                         g_mutex_unlock(&rec_idle_event->event_mutex);
511
512                         if (ret) {
513                                 g_mutex_clear(&rec_idle_event->event_mutex);
514
515                                 g_free(rec_idle_event);
516                                 rec_idle_event = NULL;
517
518                                 LOGD("remove event done");
519                         }
520                 }
521
522                 g_list_free(cb_info->idle_event_list);
523                 cb_info->idle_event_list = NULL;
524         }
525
526         g_mutex_unlock(&cb_info->idle_event_mutex);
527
528         return;
529 }
530
531
532 static void __recorder_add_msg_to_queue(recorder_cb_info_s *cb_info, int api, int event, int event_class, char *msg)
533 {
534         recorder_message_s *rec_msg = NULL;
535
536         if (!cb_info || !msg) {
537                 LOGE("NULL pointer %p %p", cb_info, msg);
538                 return;
539         }
540
541         rec_msg = g_new0(recorder_message_s, 1);
542         if (!rec_msg) {
543                 LOGE("failed to alloc rec_msg for [%s]", msg);
544                 return;
545         }
546
547         rec_msg->api = api;
548         rec_msg->event = event;
549         rec_msg->event_class = event_class;
550
551         strncpy(rec_msg->recv_msg, msg, sizeof(rec_msg->recv_msg) - 1);
552
553         /*LOGD("add recorder message to queue : api %d, event %d, event_class %d", api, event, event_class);*/
554
555         if (event == MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM) {
556                 g_mutex_lock(&cb_info->audio_stream_cb_info.mutex);
557                 g_queue_push_tail(cb_info->audio_stream_cb_info.queue, (gpointer)rec_msg);
558                 g_cond_signal(&cb_info->audio_stream_cb_info.cond);
559                 g_mutex_unlock(&cb_info->audio_stream_cb_info.mutex);
560         } else {
561                 g_mutex_lock(&cb_info->msg_handler_info.mutex);
562                 g_queue_push_tail(cb_info->msg_handler_info.queue, (gpointer)rec_msg);
563                 g_cond_signal(&cb_info->msg_handler_info.cond);
564                 g_mutex_unlock(&cb_info->msg_handler_info.mutex);
565         }
566
567         rec_msg = NULL;
568
569         return;
570 }
571
572
573 static void __recorder_get_api_operation(int api, recorder_cb_info_s *cb_info, char *msg)
574 {
575         if (!cb_info || !msg) {
576                 LOGE("NULL pointer %p %p", cb_info, msg);
577                 return;
578         }
579
580         switch (api) {
581         case MUSE_RECORDER_API_GET_STATE:
582                 {
583                         int get_state = 0;
584                         muse_recorder_msg_get(get_state, msg);
585                         cb_info->get_int_value[_RECORDER_GET_INT_STATE] = get_state;
586                 }
587                 break;
588         case MUSE_RECORDER_API_GET_VIDEO_RESOLUTION:
589                 {
590                         int get_width = 0;
591                         int get_height = 0;
592                         muse_recorder_msg_get(get_width, msg);
593                         muse_recorder_msg_get(get_height, msg);
594                         cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_RESOLUTION] = get_width << 16;
595                         cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_RESOLUTION] |= get_height;
596                 }
597                 break;
598         case MUSE_RECORDER_API_GET_FILE_FORMAT:
599                 {
600                         int get_format = 0;
601                         muse_recorder_msg_get(get_format, msg);
602                         cb_info->get_int_value[_RECORDER_GET_INT_FILE_FORMAT] = get_format;
603                 }
604                 break;
605         case MUSE_RECORDER_API_GET_AUDIO_ENCODER:
606                 {
607                         int get_codec = 0;
608                         muse_recorder_msg_get(get_codec, msg);
609                         cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_ENCODER] = get_codec;
610                 }
611                 break;
612         case MUSE_RECORDER_API_GET_VIDEO_ENCODER:
613                 {
614                         int get_codec = 0;
615                         muse_recorder_msg_get(get_codec, msg);
616                         cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_ENCODER] = get_codec;
617                 }
618                 break;
619         case MUSE_RECORDER_API_ATTR_GET_SIZE_LIMIT:
620                 {
621                         int get_kbyte = 0;
622                         muse_recorder_msg_get(get_kbyte, msg);
623                         cb_info->get_int_value[_RECORDER_GET_INT_SIZE_LIMIT] = get_kbyte;
624                 }
625                 break;
626         case MUSE_RECORDER_API_ATTR_GET_TIME_LIMIT:
627                 {
628                         int get_second = 0;
629                         muse_recorder_msg_get(get_second, msg);
630                         cb_info->get_int_value[_RECORDER_GET_INT_TIME_LIMIT] = get_second;
631                 }
632                 break;
633         case MUSE_RECORDER_API_ATTR_GET_AUDIO_DEVICE:
634                 {
635                         int get_device = 0;
636                         muse_recorder_msg_get(get_device, msg);
637                         cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_DEVICE] = get_device;
638                 }
639                 break;
640         case MUSE_RECORDER_API_ATTR_GET_AUDIO_SAMPLERATE:
641                 {
642                         int get_samplerate = 0;
643                         muse_recorder_msg_get(get_samplerate, msg);
644                         cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_SAMPLERATE] = get_samplerate;
645                 }
646                 break;
647         case MUSE_RECORDER_API_ATTR_GET_AUDIO_ENCODER_BITRATE:
648                 {
649                         int get_bitrate = 0;
650                         muse_recorder_msg_get(get_bitrate, msg);
651                         cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_ENCODER_BITRATE] = get_bitrate;
652                 }
653                 break;
654         case MUSE_RECORDER_API_ATTR_GET_VIDEO_ENCODER_BITRATE:
655                 {
656                         int get_bitrate = 0;
657                         muse_recorder_msg_get(get_bitrate, msg);
658                         cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_ENCODER_BITRATE] = get_bitrate;
659                 }
660                 break;
661         case MUSE_RECORDER_API_ATTR_GET_RECORDING_MOTION_RATE:
662                 {
663                         double get_rate = 0;
664                         muse_recorder_msg_get_double(get_rate, msg);
665                         cb_info->get_double_value[_RECORDER_GET_DOUBLE_RECORDING_MOTION_RATE] = get_rate;
666                 }
667                 break;
668         case MUSE_RECORDER_API_ATTR_GET_AUDIO_CHANNEL:
669                 {
670                         int get_channel_count = 0;
671                         muse_recorder_msg_get(get_channel_count, msg);
672                         cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_CHANNEL] = get_channel_count;
673                 }
674                 break;
675         case MUSE_RECORDER_API_ATTR_GET_ORIENTATION_TAG:
676                 {
677                         int get_orientation = 0;
678                         muse_recorder_msg_get(get_orientation, msg);
679                         cb_info->get_int_value[_RECORDER_GET_INT_ORIENTATION_TAG] = get_orientation;
680                 }
681                 break;
682         case MUSE_RECORDER_API_GET_AUDIO_LEVEL:
683                 {
684                         double get_level = 0.0;
685                         muse_recorder_msg_get_double(get_level, msg);
686                         cb_info->get_double_value[_RECORDER_GET_DOUBLE_AUDIO_LEVEL] = get_level;
687                 }
688                 break;
689         case MUSE_RECORDER_API_GET_FILENAME:
690                 {
691                         char get_filename[MUSE_RECORDER_MSG_MAX_LENGTH] = {'\0',};
692                         muse_recorder_msg_get_string(get_filename, msg);
693                         if (cb_info->get_filename) {
694                                 free(cb_info->get_filename);
695                                 cb_info->get_filename = NULL;
696                         }
697                         cb_info->get_filename = strdup(get_filename);
698                 }
699                 break;
700         default:
701                 break;
702         }
703
704         return;
705 }
706
707
708 static void __recorder_process_msg(recorder_cb_info_s *cb_info, char *msg)
709 {
710         int ret = RECORDER_ERROR_NONE;
711         int api = -1;
712         int api_class = -1;
713         int event = -1;
714         int event_class = -1;
715
716         if (!cb_info || !msg) {
717                 LOGE("invalid ptr %p %p", cb_info, msg);
718                 return;
719         }
720
721         /*LOGD("msg [%s]", msg);*/
722
723         if (!muse_recorder_msg_get(api, msg)) {
724                 LOGE("failed to get recorder api");
725                 return;
726         }
727
728         if (api == MUSE_RECORDER_CB_EVENT) {
729                 if (!muse_recorder_msg_get(event, msg) ||
730                         !muse_recorder_msg_get(event_class, msg)) {
731                         LOGE("failed to get event or event_class [%s]", msg);
732                         return;
733                 }
734         } else {
735                 if (!muse_recorder_msg_get(api_class, msg)) {
736                         LOGE("failed to get api_class [%s]", msg);
737                         return;
738                 }
739         }
740
741         if (api_class == MUSE_RECORDER_API_CLASS_IMMEDIATE) {
742                 if (api >= MUSE_RECORDER_API_MAX) {
743                         LOGE("invalid api %d", api);
744                         return;
745                 }
746
747                 if (!muse_recorder_msg_get(ret, msg)) {
748                         LOGE("failed to get recorder ret");
749                         return;
750                 }
751
752                 g_mutex_lock(&cb_info->api_mutex[api]);
753
754                 if (api == MUSE_RECORDER_API_GET_DEVICE_STATE) {
755                         g_atomic_int_set(&cb_info->msg_recv_running, 0);
756                         LOGD("get device state done. close client cb handler");
757                 } else {
758                         switch (api) {
759                         case MUSE_RECORDER_API_CREATE:
760                                 if (ret != RECORDER_ERROR_NONE) {
761                                         g_atomic_int_set(&cb_info->msg_recv_running, 0);
762                                         LOGE("create error 0x%x. closing..", ret);
763                                 }
764                                 break;
765                         case MUSE_RECORDER_API_DESTROY:
766                                 if (ret == RECORDER_ERROR_NONE) {
767                                         g_atomic_int_set(&cb_info->msg_recv_running, 0);
768                                         LOGD("destroy done. closing..");
769                                 }
770                                 break;
771                         default:
772                                 __recorder_get_api_operation(api, cb_info, msg);
773                                 break;
774                         }
775                 }
776
777                 if (cb_info->api_waiting[api] > 0) {
778                         cb_info->api_ret[api] = ret;
779                         cb_info->api_activating[api] = 1;
780
781                         g_cond_signal(&cb_info->api_cond[api]);
782                 } else {
783                         LOGE("no waiting for api [%d]", api);
784                 }
785
786                 g_mutex_unlock(&cb_info->api_mutex[api]);
787         } else if (api_class == MUSE_RECORDER_API_CLASS_THREAD_SUB || api == MUSE_RECORDER_CB_EVENT) {
788                 __recorder_add_msg_to_queue(cb_info, api, event, event_class, msg);
789         } else {
790                 LOGW("unknown recorder api %d and api_class %d", api, api_class);
791         }
792
793         return;
794 }
795
796
797 static void *_recorder_msg_handler_func(gpointer data)
798 {
799         int api = 0;
800         int type = 0;
801         recorder_message_s *rec_msg = NULL;
802         recorder_idle_event_s *rec_idle_event = NULL;
803         recorder_msg_handler_info_s *handler_info = (recorder_msg_handler_info_s *)data;
804         recorder_cb_info_s *cb_info = NULL;
805
806         if (!handler_info || !handler_info->cb_info) {
807                 LOGE("NULL handler %p", handler_info);
808                 return NULL;
809         }
810
811         cb_info = (recorder_cb_info_s *)handler_info->cb_info;
812         type = handler_info->type;
813
814         LOGD("t:%d start", type);
815
816         g_mutex_lock(&handler_info->mutex);
817
818         while (g_atomic_int_get(&handler_info->running)) {
819                 if (g_queue_is_empty(handler_info->queue)) {
820                         /*LOGD("signal wait...");*/
821                         g_cond_wait(&handler_info->cond, &handler_info->mutex);
822                         /*LOGD("signal received");*/
823
824                         if (g_atomic_int_get(&handler_info->running) == 0) {
825                                 LOGD("stop event thread");
826                                 break;
827                         }
828                 }
829
830                 rec_msg = (recorder_message_s *)g_queue_pop_head(handler_info->queue);
831                 g_mutex_unlock(&handler_info->mutex);
832                 if (rec_msg == NULL) {
833                         LOGE("NULL message");
834                         g_mutex_lock(&handler_info->mutex);
835                         continue;
836                 }
837
838                 api = rec_msg->api;
839
840                 if (api < MUSE_RECORDER_API_MAX) {
841                         int ret = 0;
842
843                         g_mutex_lock(&cb_info->api_mutex[api]);
844
845                         if (muse_recorder_msg_get(ret, rec_msg->recv_msg)) {
846                                 if (cb_info->api_waiting[api] > 0) {
847                                         cb_info->api_ret[api] = ret;
848                                         cb_info->api_activating[api] = 1;
849
850                                         /*LOGD("recorder api %d - return 0x%x", ret);*/
851
852                                         g_cond_signal(&cb_info->api_cond[api]);
853                                 } else {
854                                         LOGE("no waiting for api [%d]", api);
855                                 }
856                         } else {
857                                 LOGE("t:%d failed to get ret for api %d, msg %s", type, api, rec_msg->recv_msg);
858                         }
859
860                         g_mutex_unlock(&cb_info->api_mutex[api]);
861                 } else if (api == MUSE_RECORDER_CB_EVENT) {
862                         switch (rec_msg->event_class) {
863                         case MUSE_RECORDER_EVENT_CLASS_THREAD_SUB:
864                                 _recorder_client_user_callback(cb_info, rec_msg->recv_msg, rec_msg->event);
865                                 break;
866                         case MUSE_RECORDER_EVENT_CLASS_THREAD_MAIN:
867                                 rec_idle_event = g_new0(recorder_idle_event_s, 1);
868                                 if (rec_idle_event == NULL) {
869                                         LOGE("event alloc failed");
870                                         break;
871                                 }
872
873                                 rec_idle_event->event = rec_msg->event;
874                                 rec_idle_event->cb_info = cb_info;
875                                 g_mutex_init(&rec_idle_event->event_mutex);
876                                 strncpy(rec_idle_event->recv_msg, rec_msg->recv_msg, sizeof(rec_idle_event->recv_msg) - 1);
877
878                                 /*LOGD("add recorder event[%d, %p] to IDLE", rec_msg->event, rec_idle_event);*/
879
880                                 g_mutex_lock(&cb_info->idle_event_mutex);
881                                 cb_info->idle_event_list = g_list_append(cb_info->idle_event_list, (gpointer)rec_idle_event);
882                                 g_mutex_unlock(&cb_info->idle_event_mutex);
883
884                                 g_idle_add_full(G_PRIORITY_DEFAULT,
885                                         (GSourceFunc)_recorder_idle_event_callback,
886                                         (gpointer)rec_idle_event,
887                                         NULL);
888                                 break;
889                         default:
890                                 LOGE("unknown event class %d", rec_msg->event_class);
891                                 break;
892                         }
893                 } else {
894                         LOGE("unknown api[%d] message", api);
895                 }
896
897                 g_free(rec_msg);
898                 rec_msg = NULL;
899
900                 g_mutex_lock(&handler_info->mutex);
901         }
902
903         /* remove remained event */
904         while (!g_queue_is_empty(handler_info->queue)) {
905                 rec_msg = (recorder_message_s *)g_queue_pop_head(handler_info->queue);
906                 if (rec_msg) {
907                         LOGD("remove message %p", rec_msg);
908                         free(rec_msg);
909                         rec_msg = NULL;
910                 } else {
911                         LOGW("NULL message");
912                 }
913         }
914
915         g_mutex_unlock(&handler_info->mutex);
916
917         LOGD("return");
918
919         return NULL;
920 }
921
922
923 static void *_recorder_msg_recv_func(gpointer data)
924 {
925         int recv_length = 0;
926         int single_length = 0;
927         int remained_length = 0;
928         char *recv_msg = NULL;
929         char *single_msg = NULL;
930         char *remained_msg = NULL;
931         int num_msg = 0;
932         int cur_pos = 0;
933         int prev_pos = 0;
934         recorder_cb_info_s *cb_info = (recorder_cb_info_s *)data;
935
936         if (cb_info == NULL) {
937                 LOGE("cb_info NULL");
938                 return NULL;
939         }
940
941         LOGD("start");
942
943         single_msg = (char *)malloc(sizeof(char) * MUSE_RECORDER_MSG_MAX_LENGTH);
944         if (single_msg == NULL) {
945                 LOGE("single_msg malloc failed");
946                 goto CB_HANDLER_EXIT;
947         }
948
949         recv_msg = cb_info->recv_msg;
950
951         while (g_atomic_int_get(&cb_info->msg_recv_running)) {
952                 recv_length = muse_core_ipc_recv_msg(cb_info->fd, recv_msg);
953                 if (recv_length <= 0) {
954                         cb_info->is_server_connected = FALSE;
955                         LOGE("receive msg failed - server disconnected");
956                         break;
957                 }
958
959                 recv_msg[recv_length] = '\0';
960
961                 cur_pos = 0;
962                 prev_pos = 0;
963                 num_msg = 0;
964
965                 /*LOGD("recv msg : %s, length : %d", recv_msg, recv_length);*/
966
967                 /* Need to split the combined entering msgs */
968                 for (cur_pos = 0; cur_pos < recv_length; cur_pos++) {
969                         if (recv_msg[cur_pos] == '}') {
970                                 single_length = cur_pos - prev_pos + 1;
971
972                                 if (single_length < MUSE_RECORDER_MSG_MAX_LENGTH) {
973                                         /* check remained msg */
974                                         if (remained_length > 0) {
975                                                 if (remained_msg) {
976                                                         strncpy(single_msg, remained_msg, remained_length);
977                                                         strncpy(single_msg + remained_length, recv_msg + prev_pos, single_length);
978                                                         single_msg[remained_length + single_length] = '\0';
979
980                                                         free(remained_msg);
981                                                         remained_msg = NULL;
982                                                 } else {
983                                                         strncpy(single_msg, recv_msg + prev_pos, single_length);
984                                                         single_msg[single_length] = '\0';
985                                                         LOGE("lost msg [%s], skip...", single_msg);
986                                                 }
987
988                                                 remained_length = 0;
989                                         } else {
990                                                 strncpy(single_msg, recv_msg + prev_pos, single_length);
991                                                 single_msg[single_length] = '\0';
992                                         }
993
994                                         if (single_msg[0] == '{') {
995                                                 num_msg++;
996                                                 /*LOGD("splitted msg : [%s], Index : %d", single_msg, num_msg);*/
997                                                 __recorder_process_msg(cb_info, single_msg);
998                                         } else {
999                                                 LOGE("invalid msg [%s]", single_msg);
1000                                         }
1001                                 } else {
1002                                         LOGE("too long message [len %d] skip...", single_length);
1003                                 }
1004
1005                                 prev_pos = cur_pos + 1;
1006                         }
1007                 }
1008
1009                 /* check incompleted message */
1010                 if (recv_msg[recv_length - 1] != '}') {
1011                         remained_length = recv_length - prev_pos;
1012
1013                         LOGW("incompleted message [len %d]", remained_length);
1014
1015                         remained_msg = (char *)malloc(remained_length + 1);
1016                         if (remained_msg) {
1017                                 strncpy(remained_msg, recv_msg + prev_pos, remained_length);
1018                                 remained_msg[remained_length] = '\0';
1019                         } else {
1020                                 LOGE("failed to alloc for remained msg");
1021                         }
1022                 } else {
1023                         remained_length = 0;
1024                 }
1025         }
1026
1027         LOGD("client cb exit - server connected %d", cb_info->is_server_connected);
1028
1029         if (!cb_info->is_server_connected) {
1030                 /* send error msg for server disconnection */
1031                 char *error_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_CB_EVENT,
1032                         MUSE_TYPE_INT, "error", RECORDER_ERROR_SERVICE_DISCONNECTED,
1033                         MUSE_TYPE_INT, "current_state", RECORDER_STATE_NONE,
1034                         NULL);
1035
1036                 if (!error_msg) {
1037                         LOGE("error_msg failed");
1038                         goto CB_HANDLER_EXIT;
1039                 }
1040
1041                 LOGE("add error msg for service disconnection done");
1042
1043                 __recorder_add_msg_to_queue(cb_info,
1044                         MUSE_RECORDER_CB_EVENT,
1045                         MUSE_RECORDER_EVENT_TYPE_ERROR,
1046                         MUSE_RECORDER_EVENT_CLASS_THREAD_MAIN,
1047                         error_msg);
1048
1049                 muse_core_msg_json_factory_free(error_msg);
1050                 error_msg = NULL;
1051
1052                 LOGE("add error msg for service disconnection done");
1053         }
1054
1055 CB_HANDLER_EXIT:
1056         if (single_msg) {
1057                 free(single_msg);
1058                 single_msg = NULL;
1059         }
1060
1061         if (remained_msg) {
1062                 free(remained_msg);
1063                 remained_msg = NULL;
1064         }
1065
1066         return NULL;
1067 }
1068
1069
1070 static bool __create_msg_handler_thread(recorder_msg_handler_info_s *handler_info,
1071         int type, const char *thread_name, recorder_cb_info_s *cb_info)
1072 {
1073         if (!handler_info || !thread_name || !cb_info) {
1074                 LOGE("t:%d NULL %p %p %p",
1075                         type, handler_info, thread_name, cb_info);
1076                 return false;
1077         }
1078
1079         LOGD("t:%d", type);
1080
1081         handler_info->type = type;
1082         handler_info->queue = g_queue_new();
1083         if (handler_info->queue == NULL) {
1084                 LOGE("t:%d queue failed", type);
1085                 return false;
1086         }
1087
1088         g_mutex_init(&handler_info->mutex);
1089         g_cond_init(&handler_info->cond);
1090
1091         handler_info->cb_info = (void *)cb_info;
1092         g_atomic_int_set(&handler_info->running, 1);
1093
1094         handler_info->thread = g_thread_try_new(thread_name,
1095                 _recorder_msg_handler_func, (gpointer)handler_info, NULL);
1096         if (handler_info->thread == NULL) {
1097                 LOGE("t:%d thread failed", type);
1098
1099                 g_mutex_clear(&handler_info->mutex);
1100                 g_cond_clear(&handler_info->cond);
1101                 g_queue_free(handler_info->queue);
1102                 handler_info->queue = NULL;
1103
1104                 return false;
1105         }
1106
1107         LOGD("t:%d done", type);
1108
1109         return true;
1110 }
1111
1112
1113 static void __destroy_msg_handler_thread(recorder_msg_handler_info_s *handler_info)
1114 {
1115         int type = 0;
1116
1117         if (!handler_info) {
1118                 LOGE("NULL handler");
1119                 return;
1120         }
1121
1122         if (!handler_info->thread) {
1123                 LOGW("thread is not created");
1124                 return;
1125         }
1126
1127         type = handler_info->type;
1128
1129         LOGD("t:%d thread %p", type, handler_info->thread);
1130
1131         g_mutex_lock(&handler_info->mutex);
1132         g_atomic_int_set(&handler_info->running, 0);
1133         g_cond_signal(&handler_info->cond);
1134         g_mutex_unlock(&handler_info->mutex);
1135
1136         g_thread_join(handler_info->thread);
1137         handler_info->thread = NULL;
1138
1139         g_mutex_clear(&handler_info->mutex);
1140         g_cond_clear(&handler_info->cond);
1141         g_queue_free(handler_info->queue);
1142         handler_info->queue = NULL;
1143
1144         LOGD("t:%d done", type);
1145
1146         return;
1147 }
1148
1149
1150 static recorder_cb_info_s *_recorder_client_callback_new(gint sockfd)
1151 {
1152         recorder_cb_info_s *cb_info = NULL;
1153         gint i = 0;
1154
1155         g_return_val_if_fail(sockfd > 0, NULL);
1156
1157         cb_info = g_new0(recorder_cb_info_s, 1);
1158         if (cb_info == NULL) {
1159                 LOGE("cb_info failed");
1160                 goto ErrorExit;
1161         }
1162
1163         cb_info->api_waiting[MUSE_RECORDER_API_CREATE] = 1;
1164
1165         for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
1166                 g_mutex_init(&cb_info->api_mutex[i]);
1167                 g_cond_init(&cb_info->api_cond[i]);
1168         }
1169
1170         g_mutex_init(&cb_info->idle_event_mutex);
1171         g_cond_init(&cb_info->idle_event_cond);
1172
1173         /* message handler thread */
1174         if (!__create_msg_handler_thread(&cb_info->msg_handler_info,
1175                 _RECORDER_MESSAGE_HANDLER_TYPE_GENERAL, "recorder_msg_handler", cb_info)) {
1176                 LOGE("msg_handler_info failed");
1177                 goto ErrorExit;
1178         }
1179
1180         /* message handler thread for audio stream callback */
1181         if (!__create_msg_handler_thread(&cb_info->audio_stream_cb_info,
1182                 _RECORDER_MESSAGE_HANDLER_TYPE_AUDIO_STREAM_CB, "recorder_msg_handler:audio_stream_cb", cb_info)) {
1183                 LOGE("audio_stream_cb_info failed");
1184                 goto ErrorExit;
1185         }
1186
1187         cb_info->fd = sockfd;
1188
1189         /* message receive thread */
1190         g_atomic_int_set(&cb_info->msg_recv_running, 1);
1191         cb_info->msg_recv_thread = g_thread_try_new("recorder_msg_recv",
1192                 _recorder_msg_recv_func, (gpointer)cb_info, NULL);
1193         if (cb_info->msg_recv_thread == NULL) {
1194                 LOGE("message receive thread creation failed");
1195                 goto ErrorExit;
1196         }
1197
1198         cb_info->is_server_connected = TRUE;
1199
1200         return cb_info;
1201
1202 ErrorExit:
1203         if (cb_info) {
1204                 __destroy_msg_handler_thread(&cb_info->msg_handler_info);
1205                 __destroy_msg_handler_thread(&cb_info->audio_stream_cb_info);
1206
1207                 g_mutex_clear(&cb_info->idle_event_mutex);
1208                 g_cond_clear(&cb_info->idle_event_cond);
1209
1210                 for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
1211                         g_mutex_clear(&cb_info->api_mutex[i]);
1212                         g_cond_clear(&cb_info->api_cond[i]);
1213                 }
1214
1215                 g_free(cb_info);
1216                 cb_info = NULL;
1217         }
1218
1219         return NULL;
1220 }
1221
1222 static int _recorder_client_wait_for_cb_return(muse_recorder_api_e api, recorder_cb_info_s *cb_info, int time_out)
1223 {
1224         int ret = RECORDER_ERROR_NONE;
1225         gint64 end_time;
1226
1227         /*LOGD("Enter api : %d", api);*/
1228
1229         if (!cb_info->is_server_connected) {
1230                 LOGE("server is disconnected");
1231                 return RECORDER_ERROR_SERVICE_DISCONNECTED;
1232         }
1233
1234         g_mutex_lock(&(cb_info->api_mutex[api]));
1235
1236         if (cb_info->api_activating[api] == 0) {
1237                 if (time_out == RECORDER_CB_NO_TIMEOUT) {
1238                         LOGW("wait for api %d", api);
1239                         g_cond_wait(&(cb_info->api_cond[api]), &(cb_info->api_mutex[api]));
1240                         ret = cb_info->api_ret[api];
1241                         cb_info->api_activating[api] = 0;
1242                         LOGW("api %d returned 0x%x", api, ret);
1243                 } else {
1244                         end_time = g_get_monotonic_time() + time_out * G_TIME_SPAN_SECOND;
1245                         if (g_cond_wait_until(&(cb_info->api_cond[api]), &(cb_info->api_mutex[api]), end_time)) {
1246                                 ret = cb_info->api_ret[api];
1247                                 cb_info->api_activating[api] = 0;
1248                                 /*LOGD("return value : 0x%x", ret);*/
1249                         } else {
1250                                 ret = RECORDER_ERROR_INVALID_OPERATION;
1251                                 LOGE("api %d was TIMED OUT!", api);
1252                         }
1253                 }
1254         } else {
1255                 ret = cb_info->api_ret[api];
1256                 cb_info->api_activating[api] = 0;
1257
1258                 /*LOGD("condition is already checked for the api[%d], return[0x%x]", api, ret);*/
1259         }
1260
1261         if (ret != RECORDER_ERROR_NONE) {
1262                 LOGE("ERROR : api %d - ret 0x%x", api, ret);
1263
1264                 if (ret == RECORDER_ERROR_SOUND_POLICY)
1265                         LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY is deprecated and will be removed from next release.");
1266                 else if (ret == RECORDER_ERROR_SOUND_POLICY_BY_CALL)
1267                         LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY_BY_CALL is deprecated and will be removed from next release.");
1268                 else if (ret == RECORDER_ERROR_SOUND_POLICY_BY_ALARM)
1269                         LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY_BY_ALARM is deprecated and will be removed from next release.");
1270         }
1271
1272         g_mutex_unlock(&(cb_info->api_mutex[api]));
1273
1274         return ret;
1275 }
1276
1277
1278 static int _recorder_msg_send(int api, recorder_cb_info_s *cb_info, int *ret)
1279 {
1280         int send_ret = 0;
1281         char *msg = NULL;
1282
1283         if (!cb_info || !ret) {
1284                 LOGE("invalid pointer for api %d - %p %p", api, cb_info, ret);
1285                 return RECORDER_ERROR_INVALID_PARAMETER;
1286         }
1287
1288         msg = muse_core_msg_json_factory_new(api, NULL);
1289         if (!msg) {
1290                 LOGE("msg creation failed: api %d", api);
1291                 return RECORDER_ERROR_OUT_OF_MEMORY;
1292         }
1293
1294         /*LOGD("send msg %s", msg);*/
1295
1296         if (cb_info->is_server_connected) {
1297                 __recorder_update_api_waiting(cb_info, api, 1);
1298
1299                 send_ret = muse_core_ipc_send_msg(cb_info->fd, msg);
1300         }
1301
1302         if (send_ret < 0) {
1303                 LOGE("message send failed");
1304                 *ret = RECORDER_ERROR_INVALID_OPERATION;
1305         } else {
1306                 *ret = _recorder_client_wait_for_cb_return(api, cb_info, RECORDER_CB_TIMEOUT);
1307         }
1308
1309         __recorder_update_api_waiting(cb_info, api, -1);
1310
1311         muse_core_msg_json_factory_free(msg);
1312
1313         return RECORDER_ERROR_NONE;
1314 }
1315
1316
1317 static int _recorder_msg_send_param1(int api, recorder_cb_info_s *cb_info, int *ret, recorder_msg_param *param)
1318 {
1319         int send_ret = 0;
1320         char *msg = NULL;
1321
1322         if (!cb_info || !ret || !param) {
1323                 LOGE("invalid pointer for api %d - %p %p %p", api, cb_info, ret, param);
1324                 return RECORDER_ERROR_INVALID_PARAMETER;
1325         }
1326
1327         /*LOGD("type %d, name %s", param->type, param->name);*/
1328
1329         switch (param->type) {
1330         case MUSE_TYPE_INT:
1331                 msg = muse_core_msg_json_factory_new(api, param->type, param->name, param->value.value_INT, NULL);
1332                 break;
1333         case MUSE_TYPE_DOUBLE:
1334                 msg = muse_core_msg_json_factory_new(api, param->type, param->name, param->value.value_DOUBLE, NULL);
1335                 break;
1336         case MUSE_TYPE_STRING:
1337                 msg = muse_core_msg_json_factory_new(api, param->type, param->name, param->value.value_STRING, NULL);
1338                 break;
1339         default:
1340                 LOGE("unknown type %d", param->type);
1341                 break;
1342         }
1343
1344         if (!msg) {
1345                 LOGE("msg creation failed: api %d, type %d, param name %s",
1346                         api, param->type, param->name);
1347                 return RECORDER_ERROR_OUT_OF_MEMORY;
1348         }
1349
1350         /*LOGD("send msg %s", msg);*/
1351
1352         if (cb_info->is_server_connected) {
1353                 __recorder_update_api_waiting(cb_info, api, 1);
1354
1355                 send_ret = muse_core_ipc_send_msg(cb_info->fd, msg);
1356         }
1357
1358         if (send_ret < 0) {
1359                 LOGE("message send failed");
1360                 *ret = RECORDER_ERROR_INVALID_OPERATION;
1361         } else {
1362                 *ret = _recorder_client_wait_for_cb_return(api, cb_info, RECORDER_CB_TIMEOUT);
1363         }
1364
1365         __recorder_update_api_waiting(cb_info, api, -1);
1366
1367         muse_core_msg_json_factory_free(msg);
1368
1369         return RECORDER_ERROR_NONE;
1370 }
1371
1372
1373 static void _recorder_client_callback_destroy(recorder_cb_info_s *cb_info)
1374 {
1375         gint i = 0;
1376
1377         g_return_if_fail(cb_info != NULL);
1378
1379         LOGD("MSG receive thread[%p] destroy", cb_info->msg_recv_thread);
1380
1381         g_thread_join(cb_info->msg_recv_thread);
1382         cb_info->msg_recv_thread = NULL;
1383
1384         LOGD("msg_recv thread removed");
1385
1386         __destroy_msg_handler_thread(&cb_info->msg_handler_info);
1387         __destroy_msg_handler_thread(&cb_info->audio_stream_cb_info);
1388
1389         g_mutex_clear(&cb_info->idle_event_mutex);
1390         g_cond_clear(&cb_info->idle_event_cond);
1391
1392         for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
1393                 g_mutex_clear(&cb_info->api_mutex[i]);
1394                 g_cond_clear(&cb_info->api_cond[i]);
1395         }
1396
1397         if (cb_info->fd > -1) {
1398                 muse_core_connection_close(cb_info->fd);
1399                 cb_info->fd = -1;
1400         }
1401
1402         if (cb_info->bufmgr) {
1403                 tbm_bufmgr_deinit(cb_info->bufmgr);
1404                 cb_info->bufmgr = NULL;
1405         }
1406         if (cb_info->get_filename) {
1407                 free(cb_info->get_filename);
1408                 cb_info->get_filename = NULL;
1409         }
1410
1411         g_free(cb_info);
1412         cb_info = NULL;
1413
1414         return;
1415 }
1416
1417
1418 static int _recorder_storage_device_supported_cb(int storage_id, storage_type_e type, storage_state_e state, const char *path, void *user_data)
1419 {
1420         char **root_directory = (char **)user_data;
1421
1422         if (root_directory == NULL) {
1423                 LOGE("user data is NULL");
1424                 return false;
1425         }
1426
1427         LOGD("storage id %d, type %d, state %d, path %s",
1428                 storage_id, type, state, path ? path : "NULL");
1429
1430         if (type == STORAGE_TYPE_INTERNAL && path) {
1431                 if (*root_directory) {
1432                         free(*root_directory);
1433                         *root_directory = NULL;
1434                 }
1435
1436                 *root_directory = strdup(path);
1437                 if (*root_directory) {
1438                         LOGD("get root directory %s", *root_directory);
1439                         return false;
1440                 } else {
1441                         LOGE("strdup %s failed", path);
1442                 }
1443         }
1444
1445         return true;
1446 }
1447
1448 static int _recorder_client_get_root_directory(char **root_directory)
1449 {
1450         int ret = STORAGE_ERROR_NONE;
1451
1452         if (root_directory == NULL) {
1453                 LOGE("user data is NULL");
1454                 return false;
1455         }
1456
1457         ret = storage_foreach_device_supported((storage_device_supported_cb)_recorder_storage_device_supported_cb, root_directory);
1458         if (ret != STORAGE_ERROR_NONE) {
1459                 LOGE("storage_foreach_device_supported failed 0x%x", ret);
1460                 return false;
1461         }
1462
1463         return true;
1464 }
1465
1466 static int _recorder_create_common(recorder_h *recorder, muse_recorder_type_e type, camera_h camera)
1467 {
1468         int ret = RECORDER_ERROR_NONE;
1469         int destroy_ret = RECORDER_ERROR_NONE;
1470         int sock_fd = -1;
1471         int send_ret = 0;
1472         char *send_msg = NULL;
1473         char *root_directory = NULL;
1474         intptr_t camera_handle = 0;
1475         intptr_t handle = 0;
1476         tbm_bufmgr bufmgr = NULL;
1477         recorder_cli_s *pc = NULL;
1478         recorder_msg_param param;
1479
1480         LOGD("Enter - type %d", type);
1481
1482         if (recorder == NULL) {
1483                 LOGE("NULL pointer for recorder handle");
1484                 return RECORDER_ERROR_INVALID_PARAMETER;
1485         }
1486
1487         if (type == MUSE_RECORDER_TYPE_VIDEO && camera == NULL) {
1488                 LOGE("NULL pointer for camera handle on video recorder mode");
1489                 return RECORDER_ERROR_INVALID_PARAMETER;
1490         }
1491
1492         bufmgr = tbm_bufmgr_init(-1);
1493         if (bufmgr == NULL) {
1494                 LOGE("get tbm bufmgr failed");
1495                 return RECORDER_ERROR_INVALID_OPERATION;
1496         }
1497
1498         pc = g_new0(recorder_cli_s, 1);
1499         if (pc == NULL) {
1500                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
1501                 goto _ERR_RECORDER_EXIT;
1502         }
1503
1504         sock_fd = muse_core_client_new();
1505         if (sock_fd < 0) {
1506                 LOGE("muse_core_client_new failed - returned fd %d", sock_fd);
1507                 ret = RECORDER_ERROR_INVALID_OPERATION;
1508                 goto _ERR_RECORDER_EXIT;
1509         }
1510
1511         if (type == MUSE_RECORDER_TYPE_AUDIO) {
1512                 send_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_API_CREATE,
1513                         MUSE_TYPE_INT, "module", MUSE_RECORDER,
1514                         MUSE_TYPE_INT, PARAM_RECORDER_TYPE, MUSE_RECORDER_TYPE_AUDIO,
1515                         MUSE_TYPE_INT, "pid", getpid(),
1516                         NULL);
1517         } else {
1518                 pc->camera = camera;
1519                 camera_handle = (intptr_t)((camera_cli_s *)camera)->remote_handle;
1520                 send_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_API_CREATE,
1521                         MUSE_TYPE_INT, "module", MUSE_RECORDER,
1522                         MUSE_TYPE_INT, PARAM_RECORDER_TYPE, MUSE_RECORDER_TYPE_VIDEO,
1523                         MUSE_TYPE_POINTER, "camera_handle", camera_handle,
1524                         NULL);
1525         }
1526
1527         if (!send_msg) {
1528                 LOGE("NULL msg");
1529                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
1530                 goto _ERR_RECORDER_EXIT;
1531         }
1532
1533         LOGD("sock_fd : %d, msg : %s", sock_fd, send_msg);
1534
1535         send_ret = muse_core_ipc_send_msg(sock_fd, send_msg);
1536
1537         muse_core_msg_json_factory_free(send_msg);
1538         send_msg = NULL;
1539
1540         if (send_ret < 0) {
1541                 LOGE("send msg failed %d", errno);
1542                 ret = RECORDER_ERROR_INVALID_OPERATION;
1543                 goto _ERR_RECORDER_EXIT;
1544         }
1545
1546         pc->cb_info = _recorder_client_callback_new(sock_fd);
1547         if (pc->cb_info == NULL) {
1548                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
1549                 goto _ERR_RECORDER_EXIT;
1550         }
1551
1552         sock_fd = -1;
1553
1554         ret = _recorder_client_wait_for_cb_return(MUSE_RECORDER_API_CREATE, pc->cb_info, RECORDER_CB_NO_TIMEOUT);
1555
1556         pc->cb_info->api_waiting[MUSE_RECORDER_API_CREATE] = 0;
1557
1558         if (ret != RECORDER_ERROR_NONE) {
1559                 LOGE("API_CREATE failed 0x%x", ret);
1560                 goto _ERR_RECORDER_EXIT;
1561         }
1562
1563         muse_recorder_msg_get_pointer(handle, pc->cb_info->recv_msg);
1564         if (handle == 0) {
1565                 LOGE("Receiving Handle Failed!!");
1566                 ret = RECORDER_ERROR_INVALID_OPERATION;
1567                 goto _ERR_RECORDER_AFTER_CREATE;
1568         }
1569
1570         if (!_recorder_client_get_root_directory(&root_directory) || root_directory == NULL) {
1571                 LOGE("failed to get root directory of internal storage");
1572                 ret = RECORDER_ERROR_INVALID_OPERATION;
1573                 goto _ERR_RECORDER_AFTER_CREATE;
1574         }
1575
1576         LOGD("root directory [%s]", root_directory);
1577
1578         RECORDER_MSG_PARAM_SET(param, STRING, root_directory);
1579
1580         _recorder_msg_send_param1(MUSE_RECORDER_API_ATTR_SET_ROOT_DIRECTORY, pc->cb_info, &ret, &param);
1581
1582         if (ret != RECORDER_ERROR_NONE) {
1583                 LOGE("failed to set root directory %s", root_directory);
1584                 ret = RECORDER_ERROR_INVALID_OPERATION;
1585                 goto _ERR_RECORDER_AFTER_CREATE;
1586         }
1587
1588         free(root_directory);
1589         root_directory = NULL;
1590
1591         pc->remote_handle = handle;
1592         pc->cb_info->bufmgr = bufmgr;
1593
1594         LOGD("recorder[type %d] %p create success : remote handle 0x%x",
1595                 type, pc, pc->remote_handle);
1596
1597         *recorder = (recorder_h)pc;
1598
1599         LOGD("done");
1600
1601         return RECORDER_ERROR_NONE;
1602
1603 _ERR_RECORDER_AFTER_CREATE:
1604         _recorder_msg_send(MUSE_RECORDER_API_DESTROY, pc->cb_info, &destroy_ret);
1605         LOGE("destroy return 0x%x", destroy_ret);
1606
1607 _ERR_RECORDER_EXIT:
1608         tbm_bufmgr_deinit(bufmgr);
1609         bufmgr = NULL;
1610
1611         if (root_directory) {
1612                 free(root_directory);
1613                 root_directory = NULL;
1614         }
1615
1616         if (sock_fd > -1) {
1617                 muse_core_connection_close(sock_fd);
1618                 sock_fd = -1;
1619         }
1620
1621         if (pc) {
1622                 if (pc->cb_info) {
1623                         _recorder_client_callback_destroy(pc->cb_info);
1624                         pc->cb_info = NULL;
1625                 }
1626                 g_free(pc);
1627                 pc = NULL;
1628         }
1629
1630         return ret;
1631 }
1632
1633
1634 int recorder_create_videorecorder(camera_h camera, recorder_h *recorder)
1635 {
1636         return _recorder_create_common(recorder, MUSE_RECORDER_TYPE_VIDEO, camera);
1637 }
1638
1639
1640 int recorder_create_audiorecorder(recorder_h *recorder)
1641 {
1642         return _recorder_create_common(recorder, MUSE_RECORDER_TYPE_AUDIO, NULL);
1643 }
1644
1645
1646 int recorder_get_state(recorder_h recorder, recorder_state_e *state)
1647 {
1648         int ret = RECORDER_ERROR_NONE;
1649         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1650         muse_recorder_api_e api = MUSE_RECORDER_API_GET_STATE;
1651
1652         if (!pc || !pc->cb_info) {
1653                 LOGE("NULL handle");
1654                 return RECORDER_ERROR_INVALID_PARAMETER;
1655         }
1656
1657         if (state == NULL) {
1658                 LOGE("NULL pointer state");
1659                 return RECORDER_ERROR_INVALID_PARAMETER;
1660         }
1661
1662         LOGD("Enter, remote_handle : %x", pc->remote_handle);
1663
1664         _recorder_msg_send(api, pc->cb_info, &ret);
1665
1666         if (ret == RECORDER_ERROR_NONE)
1667                 *state = (recorder_state_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_STATE];
1668
1669         LOGD("ret : 0x%x, state : %d", ret, *state);
1670
1671         return ret;
1672 }
1673
1674
1675 int recorder_destroy(recorder_h recorder)
1676 {
1677         int ret = RECORDER_ERROR_NONE;
1678         muse_recorder_api_e api = MUSE_RECORDER_API_DESTROY;
1679         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1680
1681         if (!pc || !pc->cb_info) {
1682                 LOGE("NULL handle");
1683                 return RECORDER_ERROR_INVALID_PARAMETER;
1684         }
1685
1686         LOGD("ENTER");
1687
1688         if (pc->cb_info->is_server_connected)
1689                 _recorder_msg_send(api, pc->cb_info, &ret);
1690         else
1691                 LOGW("server disconnected. release resource without send message.");
1692
1693         if (ret == RECORDER_ERROR_NONE) {
1694                 _recorder_remove_idle_event_all(pc->cb_info);
1695                 _recorder_client_callback_destroy(pc->cb_info);
1696                 g_free(pc);
1697                 pc = NULL;
1698         }
1699
1700         LOGD("ret : 0x%x", ret);
1701
1702         return ret;
1703 }
1704
1705
1706 int recorder_prepare(recorder_h recorder)
1707 {
1708         int ret = RECORDER_ERROR_NONE;
1709         muse_recorder_api_e api = MUSE_RECORDER_API_PREPARE;
1710         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1711
1712         if (!pc || !pc->cb_info) {
1713                 LOGE("NULL handle");
1714                 return RECORDER_ERROR_INVALID_PARAMETER;
1715         }
1716
1717         LOGD("ENTER");
1718
1719         _recorder_msg_send(api, pc->cb_info, &ret);
1720
1721         LOGD("ret : 0x%x", ret);
1722
1723         if (ret == RECORDER_ERROR_NONE && pc->camera)
1724                 camera_start_evas_rendering(pc->camera);
1725
1726         return ret;
1727 }
1728
1729
1730 int recorder_unprepare(recorder_h recorder)
1731 {
1732         int ret = RECORDER_ERROR_NONE;
1733         muse_recorder_api_e api = MUSE_RECORDER_API_UNPREPARE;
1734         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1735         camera_state_e camera_state = CAMERA_STATE_NONE;
1736
1737         if (!pc || !pc->cb_info) {
1738                 LOGE("NULL handle");
1739                 return RECORDER_ERROR_INVALID_PARAMETER;
1740         }
1741
1742         LOGD("ENTER");
1743
1744         if (pc->camera) {
1745                 ret = camera_get_state(pc->camera, &camera_state);
1746                 if (ret != CAMERA_ERROR_NONE) {
1747                         LOGE("failed to get camera state 0x%x", ret);
1748                         return RECORDER_ERROR_INVALID_OPERATION;
1749                 }
1750
1751                 if (camera_state == CAMERA_STATE_PREVIEW) {
1752                         ret = camera_stop_evas_rendering(pc->camera, false);
1753                         if (ret != CAMERA_ERROR_NONE) {
1754                                 LOGE("camera_stop_evas_rendering failed 0x%x", ret);
1755                                 return RECORDER_ERROR_INVALID_OPERATION;
1756                         }
1757                 }
1758         }
1759
1760         _recorder_msg_send(api, pc->cb_info, &ret);
1761
1762         LOGD("ret : 0x%x", ret);
1763
1764         return ret;
1765 }
1766
1767
1768 int recorder_start(recorder_h recorder)
1769 {
1770         int ret = RECORDER_ERROR_NONE;
1771         muse_recorder_api_e api = MUSE_RECORDER_API_START;
1772         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1773         recorder_state_e current_state = RECORDER_STATE_NONE;
1774
1775         if (!pc || !pc->cb_info) {
1776                 LOGE("NULL handle");
1777                 return RECORDER_ERROR_INVALID_PARAMETER;
1778         }
1779
1780         LOGD("ENTER");
1781
1782         if (pc->camera) {
1783                 ret = recorder_get_state(recorder, &current_state);
1784                 if (ret != RECORDER_ERROR_NONE) {
1785                         LOGE("failed to get current state 0x%x", ret);
1786                         return RECORDER_ERROR_INVALID_OPERATION;
1787                 }
1788
1789                 if (current_state == RECORDER_STATE_READY) {
1790                         ret = camera_stop_evas_rendering(pc->camera, true);
1791                         if (ret != CAMERA_ERROR_NONE) {
1792                                 LOGE("camera_stop_evas_rendering failed 0x%x", ret);
1793                                 return RECORDER_ERROR_INVALID_OPERATION;
1794                         }
1795                 }
1796         }
1797
1798         _recorder_msg_send(api, pc->cb_info, &ret);
1799
1800         if (pc->camera && current_state == RECORDER_STATE_READY)
1801                 camera_start_evas_rendering(pc->camera);
1802
1803         LOGD("ret : 0x%x", ret);
1804
1805         return ret;
1806 }
1807
1808
1809 int recorder_pause(recorder_h recorder)
1810 {
1811         int ret = RECORDER_ERROR_NONE;
1812         muse_recorder_api_e api = MUSE_RECORDER_API_PAUSE;
1813         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1814
1815         if (!pc || !pc->cb_info) {
1816                 LOGE("NULL handle");
1817                 return RECORDER_ERROR_INVALID_PARAMETER;
1818         }
1819
1820         LOGD("ENTER");
1821
1822         _recorder_msg_send(api, pc->cb_info, &ret);
1823
1824         LOGD("ret : 0x%x", ret);
1825
1826         return ret;
1827 }
1828
1829
1830 int recorder_commit(recorder_h recorder)
1831 {
1832         int ret = RECORDER_ERROR_NONE;
1833         muse_recorder_api_e api = MUSE_RECORDER_API_COMMIT;
1834         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1835         recorder_state_e current_state = RECORDER_STATE_NONE;
1836
1837         if (!pc || !pc->cb_info) {
1838                 LOGE("NULL handle");
1839                 return RECORDER_ERROR_INVALID_PARAMETER;
1840         }
1841
1842         LOGD("ENTER");
1843
1844         if (pc->camera) {
1845                 ret = recorder_get_state(recorder, &current_state);
1846                 if (ret != RECORDER_ERROR_NONE) {
1847                         LOGE("failed to get current state 0x%x", ret);
1848                         return RECORDER_ERROR_INVALID_OPERATION;
1849                 }
1850
1851                 if (current_state >= RECORDER_STATE_RECORDING) {
1852                         ret = camera_stop_evas_rendering(pc->camera, true);
1853                         if (ret != CAMERA_ERROR_NONE) {
1854                                 LOGE("camera_stop_evas_rendering failed 0x%x", ret);
1855                                 return RECORDER_ERROR_INVALID_OPERATION;
1856                         }
1857                 }
1858         }
1859
1860         _recorder_msg_send(api, pc->cb_info, &ret);
1861
1862         if (pc->camera && current_state >= RECORDER_STATE_RECORDING)
1863                 camera_start_evas_rendering(pc->camera);
1864
1865         LOGD("ret : 0x%x", ret);
1866
1867         return ret;
1868 }
1869
1870
1871 int recorder_cancel(recorder_h recorder)
1872 {
1873         int ret = RECORDER_ERROR_NONE;
1874         muse_recorder_api_e api = MUSE_RECORDER_API_CANCEL;
1875         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1876         recorder_state_e current_state = RECORDER_STATE_NONE;
1877
1878         if (!pc || !pc->cb_info) {
1879                 LOGE("NULL handle");
1880                 return RECORDER_ERROR_INVALID_PARAMETER;
1881         }
1882
1883         LOGD("ENTER");
1884
1885         if (pc->camera) {
1886                 ret = recorder_get_state(recorder, &current_state);
1887                 if (ret != RECORDER_ERROR_NONE) {
1888                         LOGE("failed to get current state 0x%x", ret);
1889                         return RECORDER_ERROR_INVALID_OPERATION;
1890                 }
1891
1892                 if (current_state >= RECORDER_STATE_RECORDING) {
1893                         ret = camera_stop_evas_rendering(pc->camera, true);
1894                         if (ret != CAMERA_ERROR_NONE) {
1895                                 LOGE("camera_stop_evas_rendering failed 0x%x", ret);
1896                                 return RECORDER_ERROR_INVALID_OPERATION;
1897                         }
1898                 }
1899         }
1900
1901         _recorder_msg_send(api, pc->cb_info, &ret);
1902
1903         if (pc->camera && current_state >= RECORDER_STATE_RECORDING)
1904                 camera_start_evas_rendering(pc->camera);
1905
1906         LOGD("ret : 0x%x", ret);
1907
1908         return ret;
1909 }
1910
1911
1912 int recorder_set_video_resolution(recorder_h recorder, int width, int height)
1913 {
1914         int ret = RECORDER_ERROR_NONE;
1915         int send_ret = 0;
1916         char *send_msg = NULL;
1917         muse_recorder_api_e api = MUSE_RECORDER_API_SET_VIDEO_RESOLUTION;
1918         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1919
1920         if (!pc || !pc->cb_info) {
1921                 LOGE("NULL handle");
1922                 return RECORDER_ERROR_INVALID_PARAMETER;
1923         }
1924
1925         LOGD("ENTER");
1926
1927         send_msg = muse_core_msg_json_factory_new(api,
1928                 MUSE_TYPE_INT, "width", width,
1929                 MUSE_TYPE_INT, "height", height,
1930                 NULL);
1931         if (send_msg) {
1932                 if (pc->cb_info->is_server_connected) {
1933                         __recorder_update_api_waiting(pc->cb_info, api, 1);
1934
1935                         send_ret = muse_core_ipc_send_msg(pc->cb_info->fd, send_msg);
1936                 }
1937
1938                 if (send_ret < 0) {
1939                         LOGE("message send failed");
1940                         ret = RECORDER_ERROR_INVALID_OPERATION;
1941                 } else {
1942                         ret = _recorder_client_wait_for_cb_return(api, pc->cb_info, RECORDER_CB_TIMEOUT);
1943                 }
1944
1945                 __recorder_update_api_waiting(pc->cb_info, api, -1);
1946
1947                 muse_core_msg_json_factory_free(send_msg);
1948         } else {
1949                 LOGE("failed to create msg");
1950                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
1951         }
1952
1953         LOGD("ret : 0x%x", ret);
1954
1955         return ret;
1956 }
1957
1958
1959 int recorder_get_video_resolution(recorder_h recorder, int *width, int *height)
1960 {
1961         int ret = RECORDER_ERROR_NONE;
1962         muse_recorder_api_e api = MUSE_RECORDER_API_GET_VIDEO_RESOLUTION;
1963         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1964
1965         if (!pc || !pc->cb_info) {
1966                 LOGE("NULL handle");
1967                 return RECORDER_ERROR_INVALID_PARAMETER;
1968         }
1969
1970         if (!width || !height) {
1971                 LOGE("NULL pointer width = [%p], height = [%p]", width, height);
1972                 return RECORDER_ERROR_INVALID_PARAMETER;
1973         }
1974
1975         LOGD("ENTER");
1976
1977         _recorder_msg_send(api, pc->cb_info, &ret);
1978
1979         if (ret == RECORDER_ERROR_NONE) {
1980                 *width = pc->cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_RESOLUTION] >> 16;
1981                 *height = (0x0000ffff & pc->cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_RESOLUTION]);
1982         }
1983
1984         LOGD("ret : 0x%x, %dx%d", ret, *width, *height);
1985
1986         return ret;
1987 }
1988
1989
1990 int recorder_foreach_supported_video_resolution(recorder_h recorder,
1991         recorder_supported_video_resolution_cb foreach_cb, void *user_data)
1992 {
1993         int ret = RECORDER_ERROR_NONE;
1994         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1995         muse_recorder_api_e api = MUSE_RECORDER_API_FOREACH_SUPPORTED_VIDEO_RESOLUTION;
1996
1997         if (!pc || !pc->cb_info || foreach_cb == NULL) {
1998                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
1999                 return RECORDER_ERROR_INVALID_PARAMETER;
2000         }
2001
2002         LOGD("Enter, handle :%x", pc->remote_handle);
2003
2004         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_RESOLUTION] = foreach_cb;
2005         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_RESOLUTION] = user_data;
2006
2007         _recorder_msg_send(api, pc->cb_info, &ret);
2008
2009         LOGD("ret : 0x%x", ret);
2010
2011         return ret;
2012 }
2013
2014
2015 int recorder_get_audio_level(recorder_h recorder, double *level)
2016 {
2017         int ret = RECORDER_ERROR_NONE;
2018         muse_recorder_api_e api = MUSE_RECORDER_API_GET_AUDIO_LEVEL;
2019         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2020
2021         if (!pc || !pc->cb_info || level == NULL) {
2022                 LOGE("NULL pointer %p %p", pc, level);
2023                 return RECORDER_ERROR_INVALID_PARAMETER;
2024         }
2025
2026         LOGD("ENTER");
2027
2028         _recorder_msg_send(api, pc->cb_info, &ret);
2029
2030         if (ret == RECORDER_ERROR_NONE)
2031                 *level = pc->cb_info->get_double_value[_RECORDER_GET_DOUBLE_AUDIO_LEVEL];
2032
2033         LOGD("ret : 0x%x, level %lf", ret, *level);
2034
2035         return ret;
2036 }
2037
2038
2039 int recorder_set_filename(recorder_h recorder,  const char *filename)
2040 {
2041         int ret = RECORDER_ERROR_NONE;
2042         muse_recorder_api_e api = MUSE_RECORDER_API_SET_FILENAME;
2043         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2044         recorder_msg_param param;
2045         char set_filename[RECORDER_FILENAME_MAX] = {0, };
2046
2047         if (!pc || !pc->cb_info) {
2048                 LOGE("NULL handle");
2049                 return RECORDER_ERROR_INVALID_PARAMETER;
2050         }
2051
2052         if (filename == NULL) {
2053                 LOGE("filename is NULL");
2054                 return RECORDER_ERROR_INVALID_PARAMETER;
2055         }
2056
2057         LOGD("ENTER");
2058
2059         if (storage_get_origin_internal_path(filename, RECORDER_FILENAME_MAX, set_filename) < 0) {
2060                 /* Cannot convert. Use original path. */
2061                 strncpy(set_filename, filename, strlen(filename));
2062         } else {
2063                 /* Converted. Use converted path. */
2064                 LOGD("Converted filename : %s -> %s", filename, set_filename);
2065         }
2066
2067         RECORDER_MSG_PARAM_SET(param, STRING, set_filename);
2068
2069         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2070
2071         LOGD("ret : 0x%x", ret);
2072
2073         return ret;
2074 }
2075
2076
2077 int recorder_get_filename(recorder_h recorder,  char **filename)
2078 {
2079         int ret = RECORDER_ERROR_NONE;
2080         muse_recorder_api_e api = MUSE_RECORDER_API_GET_FILENAME;
2081         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2082         char compat_filename[RECORDER_FILENAME_MAX] = {0, };
2083
2084         if (!pc || !pc->cb_info) {
2085                 LOGE("NULL handle");
2086                 return RECORDER_ERROR_INVALID_PARAMETER;
2087         }
2088
2089         if (filename == NULL) {
2090                 LOGE("filename is NULL");
2091                 return RECORDER_ERROR_INVALID_PARAMETER;
2092         }
2093
2094         LOGD("ENTER");
2095
2096         _recorder_msg_send(api, pc->cb_info, &ret);
2097
2098         if (ret == RECORDER_ERROR_NONE) {
2099                 if (storage_get_compat_internal_path(pc->cb_info->get_filename, RECORDER_FILENAME_MAX, compat_filename) < 0) {
2100                         /* Cannot convert. Use original path. */
2101                         *filename = pc->cb_info->get_filename;
2102                 } else {
2103                         /* Converted. Use converted path. */
2104                         LOGD("Converted filename : %s -> %s", pc->cb_info->get_filename, compat_filename);
2105                         *filename = strdup(compat_filename);
2106                         free(pc->cb_info->get_filename);
2107                 }
2108
2109                 pc->cb_info->get_filename = NULL;
2110         }
2111
2112         LOGD("ret : 0x%x, filename : [%s]", ret, (*filename) ? *filename : "NULL");
2113
2114         return ret;
2115 }
2116
2117
2118 int recorder_set_file_format(recorder_h recorder, recorder_file_format_e format)
2119 {
2120         int ret = RECORDER_ERROR_NONE;
2121         int set_format = (int)format;
2122         muse_recorder_api_e api = MUSE_RECORDER_API_SET_FILE_FORMAT;
2123         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2124         recorder_msg_param param;
2125
2126         if (!pc || !pc->cb_info) {
2127                 LOGE("NULL handle");
2128                 return RECORDER_ERROR_INVALID_PARAMETER;
2129         }
2130
2131         LOGD("ENTER, set_format : %d", set_format);
2132
2133         RECORDER_MSG_PARAM_SET(param, INT, set_format);
2134
2135         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2136
2137         LOGD("ret : 0x%x", ret);
2138         return ret;
2139 }
2140
2141
2142 int recorder_get_file_format(recorder_h recorder, recorder_file_format_e *format)
2143 {
2144         int ret = RECORDER_ERROR_NONE;
2145         muse_recorder_api_e api = MUSE_RECORDER_API_GET_FILE_FORMAT;
2146         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2147
2148         if (!pc || !pc->cb_info) {
2149                 LOGE("NULL handle");
2150                 return RECORDER_ERROR_INVALID_PARAMETER;
2151         }
2152
2153         if (format == NULL) {
2154                 LOGE("NULL pointer data");
2155                 return RECORDER_ERROR_INVALID_PARAMETER;
2156         }
2157
2158         LOGD("ENTER");
2159
2160         _recorder_msg_send(api, pc->cb_info, &ret);
2161
2162         if (ret == RECORDER_ERROR_NONE)
2163                 *format = (recorder_file_format_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_FILE_FORMAT];
2164
2165         LOGD("ret : 0x%x, format %d", ret, *format);
2166
2167         return ret;
2168 }
2169
2170
2171 int recorder_set_sound_stream_info(recorder_h recorder, sound_stream_info_h stream_info)
2172 {
2173         int ret = RECORDER_ERROR_NONE;
2174         muse_recorder_api_e api = MUSE_RECORDER_API_SET_SOUND_STREAM_INFO;
2175         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2176         bool is_available = false;
2177         int stream_index = 0;
2178         char *stream_type = NULL;
2179         char *send_msg = NULL;
2180         int send_ret = 0;
2181
2182         if (!pc || !pc->cb_info || stream_info == NULL) {
2183                 LOGE("NULL handle");
2184                 return RECORDER_ERROR_INVALID_PARAMETER;
2185         }
2186
2187         LOGD("ENTER");
2188
2189         ret = sound_manager_is_available_stream_information(stream_info, NATIVE_API_RECORDER, &is_available);
2190         if (ret != SOUND_MANAGER_ERROR_NONE) {
2191                 LOGE("stream info verification failed");
2192                 return RECORDER_ERROR_INVALID_OPERATION;
2193         }
2194
2195         if (is_available == false) {
2196                 LOGE("stream information is not available");
2197                 return RECORDER_ERROR_INVALID_OPERATION;
2198         }
2199
2200         ret = sound_manager_get_type_from_stream_information(stream_info, &stream_type);
2201         ret |= sound_manager_get_index_from_stream_information(stream_info, &stream_index);
2202
2203         LOGD("sound manager return [0x%x]", ret);
2204
2205         if (ret == SOUND_MANAGER_ERROR_NONE) {
2206                 send_msg = muse_core_msg_json_factory_new(api,
2207                         MUSE_TYPE_STRING, "stream_type", stream_type,
2208                         MUSE_TYPE_INT, "stream_index", stream_index,
2209                         NULL);
2210                 if (send_msg) {
2211                         if (pc->cb_info->is_server_connected) {
2212                                 __recorder_update_api_waiting(pc->cb_info, api, 1);
2213
2214                                 send_ret = muse_core_ipc_send_msg(pc->cb_info->fd, send_msg);
2215                         }
2216
2217                         if (send_ret < 0) {
2218                                 LOGE("message send failed");
2219                                 ret = RECORDER_ERROR_INVALID_OPERATION;
2220                         } else {
2221                                 ret = _recorder_client_wait_for_cb_return(api, pc->cb_info, RECORDER_CB_TIMEOUT);
2222                         }
2223
2224                         __recorder_update_api_waiting(pc->cb_info, api, -1);
2225
2226                         muse_core_msg_json_factory_free(send_msg);
2227                 } else {
2228                         LOGE("failed to create msg");
2229                         ret = RECORDER_ERROR_OUT_OF_MEMORY;
2230                 }
2231         } else {
2232                 ret = RECORDER_ERROR_INVALID_OPERATION;
2233         }
2234
2235         return ret;
2236 }
2237
2238
2239 int recorder_set_state_changed_cb(recorder_h recorder, recorder_state_changed_cb callback, void *user_data)
2240 {
2241         int ret = RECORDER_ERROR_NONE;
2242         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2243         muse_recorder_api_e api = MUSE_RECORDER_API_SET_STATE_CHANGED_CB;
2244
2245         if (!pc || !pc->cb_info || callback == NULL) {
2246                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2247                 return RECORDER_ERROR_INVALID_PARAMETER;
2248         }
2249
2250         LOGD("Enter, handle :%x", pc->remote_handle);
2251
2252         _recorder_msg_send(api, pc->cb_info, &ret);
2253
2254         if (ret == RECORDER_ERROR_NONE) {
2255                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_STATE_CHANGE] = callback;
2256                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_STATE_CHANGE] = user_data;
2257         }
2258
2259         LOGD("ret : 0x%x", ret);
2260
2261         return ret;
2262 }
2263
2264
2265 int recorder_unset_state_changed_cb(recorder_h recorder)
2266 {
2267         int ret = RECORDER_ERROR_NONE;
2268         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_STATE_CHANGED_CB;
2269         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2270
2271         if (!pc || !pc->cb_info) {
2272                 LOGE("NULL handle");
2273                 return RECORDER_ERROR_INVALID_PARAMETER;
2274         }
2275
2276         LOGD("ENTER");
2277
2278         _recorder_msg_send(api, pc->cb_info, &ret);
2279
2280         if (ret == RECORDER_ERROR_NONE) {
2281                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_STATE_CHANGE] = NULL;
2282                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_STATE_CHANGE] = NULL;
2283         }
2284
2285         LOGD("ret : 0x%x", ret);
2286
2287         return ret;
2288 }
2289
2290
2291 int recorder_set_interrupted_cb(recorder_h recorder, recorder_interrupted_cb callback, void *user_data)
2292 {
2293         int ret = RECORDER_ERROR_NONE;
2294         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2295         muse_recorder_api_e api = MUSE_RECORDER_API_SET_INTERRUPTED_CB;
2296
2297         if (!pc || !pc->cb_info || callback == NULL) {
2298                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2299                 return RECORDER_ERROR_INVALID_PARAMETER;
2300         }
2301
2302         LOGD("Enter, handle :%x", pc->remote_handle);
2303
2304         _recorder_msg_send(api, pc->cb_info, &ret);
2305
2306         if (ret == RECORDER_ERROR_NONE) {
2307                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_INTERRUPTED] = callback;
2308                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_INTERRUPTED] = user_data;
2309         }
2310
2311         LOGD("ret : 0x%x", ret);
2312
2313         return ret;
2314 }
2315
2316
2317 int recorder_unset_interrupted_cb(recorder_h recorder)
2318 {
2319         int ret = RECORDER_ERROR_NONE;
2320         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_INTERRUPTED_CB;
2321         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2322
2323         if (!pc || !pc->cb_info) {
2324                 LOGE("NULL handle");
2325                 return RECORDER_ERROR_INVALID_PARAMETER;
2326         }
2327
2328         LOGD("ENTER");
2329
2330         _recorder_msg_send(api, pc->cb_info, &ret);
2331
2332         if (ret == RECORDER_ERROR_NONE) {
2333                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_INTERRUPTED] = NULL;
2334                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_INTERRUPTED] = NULL;
2335         }
2336
2337         LOGD("ret : 0x%x", ret);
2338
2339         return ret;
2340 }
2341
2342
2343 int recorder_set_audio_stream_cb(recorder_h recorder, recorder_audio_stream_cb callback, void *user_data)
2344 {
2345         int ret = RECORDER_ERROR_NONE;
2346         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2347         muse_recorder_api_e api = MUSE_RECORDER_API_SET_AUDIO_STREAM_CB;
2348
2349         if (!pc || !pc->cb_info || callback == NULL) {
2350                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2351                 return RECORDER_ERROR_INVALID_PARAMETER;
2352         }
2353
2354         LOGD("Enter, handle :%x", pc->remote_handle);
2355
2356         _recorder_msg_send(api, pc->cb_info, &ret);
2357
2358         if (ret == RECORDER_ERROR_NONE) {
2359                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM] = callback;
2360                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM] = user_data;
2361         }
2362
2363         LOGD("ret : 0x%x", ret);
2364
2365         return ret;
2366 }
2367
2368
2369 int recorder_unset_audio_stream_cb(recorder_h recorder)
2370 {
2371         int ret = RECORDER_ERROR_NONE;
2372         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_AUDIO_STREAM_CB;
2373         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2374
2375         if (!pc || !pc->cb_info) {
2376                 LOGE("NULL handle");
2377                 return RECORDER_ERROR_INVALID_PARAMETER;
2378         }
2379
2380         LOGD("ENTER");
2381
2382         _recorder_msg_send(api, pc->cb_info, &ret);
2383
2384         if (ret == RECORDER_ERROR_NONE) {
2385                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM] = NULL;
2386                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM] = NULL;
2387         }
2388
2389         LOGD("ret : 0x%x", ret);
2390
2391         return ret;
2392 }
2393
2394
2395 int recorder_set_muxed_stream_cb(recorder_h recorder, recorder_muxed_stream_cb callback, void *user_data)
2396 {
2397         int ret = RECORDER_ERROR_NONE;
2398         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2399         muse_recorder_api_e api = MUSE_RECORDER_API_SET_MUXED_STREAM_CB;
2400
2401         if (!pc || !pc->cb_info || !callback) {
2402                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2403                 return RECORDER_ERROR_INVALID_PARAMETER;
2404         }
2405
2406         LOGD("Enter, handle :%x", pc->remote_handle);
2407
2408         _recorder_msg_send(api, pc->cb_info, &ret);
2409
2410         if (ret == RECORDER_ERROR_NONE) {
2411                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_MUXED_STREAM] = callback;
2412                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_MUXED_STREAM] = user_data;
2413         }
2414
2415         LOGD("ret : 0x%x", ret);
2416
2417         return ret;
2418 }
2419
2420
2421 int recorder_unset_muxed_stream_cb(recorder_h recorder)
2422 {
2423         int ret = RECORDER_ERROR_NONE;
2424         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_MUXED_STREAM_CB;
2425         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2426
2427         if (!pc || !pc->cb_info) {
2428                 LOGE("NULL handle");
2429                 return RECORDER_ERROR_INVALID_PARAMETER;
2430         }
2431
2432         LOGD("ENTER");
2433
2434         _recorder_msg_send(api, pc->cb_info, &ret);
2435
2436         if (ret == RECORDER_ERROR_NONE) {
2437                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_MUXED_STREAM] = NULL;
2438                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_MUXED_STREAM] = NULL;
2439         }
2440
2441         LOGD("ret : 0x%x", ret);
2442
2443         return ret;
2444 }
2445
2446
2447 int recorder_set_error_cb(recorder_h recorder, recorder_error_cb callback, void *user_data)
2448 {
2449         int ret = RECORDER_ERROR_NONE;
2450         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2451         muse_recorder_api_e api = MUSE_RECORDER_API_SET_ERROR_CB;
2452
2453         if (!pc || !pc->cb_info || callback == NULL) {
2454                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2455                 return RECORDER_ERROR_INVALID_PARAMETER;
2456         }
2457
2458         LOGD("Enter, handle :%x", pc->remote_handle);
2459
2460         _recorder_msg_send(api, pc->cb_info, &ret);
2461
2462         if (ret == RECORDER_ERROR_NONE) {
2463                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_ERROR] = callback;
2464                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_ERROR] = user_data;
2465         }
2466
2467         LOGD("ret : 0x%x", ret);
2468
2469         return ret;
2470 }
2471
2472
2473 int recorder_unset_error_cb(recorder_h recorder)
2474 {
2475         int ret = RECORDER_ERROR_NONE;
2476         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_ERROR_CB;
2477         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2478
2479         if (!pc || !pc->cb_info) {
2480                 LOGE("NULL handle");
2481                 return RECORDER_ERROR_INVALID_PARAMETER;
2482         }
2483
2484         LOGD("ENTER");
2485
2486         _recorder_msg_send(api, pc->cb_info, &ret);
2487
2488         if (ret == RECORDER_ERROR_NONE) {
2489                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_ERROR] = NULL;
2490                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_ERROR] = NULL;
2491         }
2492
2493         LOGD("ret : 0x%x", ret);
2494
2495         return ret;
2496 }
2497
2498
2499 int recorder_set_recording_status_cb(recorder_h recorder, recorder_recording_status_cb callback, void *user_data)
2500 {
2501         int ret = RECORDER_ERROR_NONE;
2502         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2503         muse_recorder_api_e api = MUSE_RECORDER_API_SET_RECORDING_STATUS_CB;
2504
2505         if (!pc || !pc->cb_info || callback == NULL) {
2506                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2507                 return RECORDER_ERROR_INVALID_PARAMETER;
2508         }
2509
2510         LOGD("Enter, handle :%x", pc->remote_handle);
2511
2512         _recorder_msg_send(api, pc->cb_info, &ret);
2513
2514         if (ret == RECORDER_ERROR_NONE) {
2515                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_RECORDING_STATUS] = callback;
2516                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_RECORDING_STATUS] = user_data;
2517         }
2518
2519         LOGD("ret : 0x%x", ret);
2520
2521         return ret;
2522 }
2523
2524
2525 int recorder_unset_recording_status_cb(recorder_h recorder)
2526 {
2527         int ret = RECORDER_ERROR_NONE;
2528         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_RECORDING_STATUS_CB;
2529         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2530
2531         if (!pc || !pc->cb_info) {
2532                 LOGE("NULL handle");
2533                 return RECORDER_ERROR_INVALID_PARAMETER;
2534         }
2535
2536         LOGD("ENTER");
2537
2538         _recorder_msg_send(api, pc->cb_info, &ret);
2539
2540         if (ret == RECORDER_ERROR_NONE) {
2541                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_RECORDING_STATUS] = NULL;
2542                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_RECORDING_STATUS] = NULL;
2543         }
2544
2545         LOGD("ret : 0x%x", ret);
2546
2547         return ret;
2548 }
2549
2550
2551 int recorder_set_recording_limit_reached_cb(recorder_h recorder, recorder_recording_limit_reached_cb callback, void *user_data)
2552 {
2553         int ret = RECORDER_ERROR_NONE;
2554         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2555         muse_recorder_api_e api = MUSE_RECORDER_API_SET_RECORDING_LIMIT_REACHED_CB;
2556
2557         if (!pc || !pc->cb_info || callback == NULL) {
2558                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2559                 return RECORDER_ERROR_INVALID_PARAMETER;
2560         }
2561
2562         _recorder_msg_send(api, pc->cb_info, &ret);
2563
2564         if (ret == RECORDER_ERROR_NONE) {
2565                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_RECORDING_LIMITED] = callback;
2566                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_RECORDING_LIMITED] = user_data;
2567         }
2568
2569         LOGD("ret : 0x%x", ret);
2570
2571         return ret;
2572 }
2573
2574
2575 int recorder_unset_recording_limit_reached_cb(recorder_h recorder)
2576 {
2577         int ret = RECORDER_ERROR_NONE;
2578         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_RECORDING_LIMIT_REACHED_CB;
2579         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2580
2581         if (!pc || !pc->cb_info) {
2582                 LOGE("NULL handle");
2583                 return RECORDER_ERROR_INVALID_PARAMETER;
2584         }
2585
2586         LOGD("ENTER");
2587
2588         _recorder_msg_send(api, pc->cb_info, &ret);
2589
2590         if (ret == RECORDER_ERROR_NONE) {
2591                 pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_RECORDING_LIMITED] = NULL;
2592                 pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_RECORDING_LIMITED] = NULL;
2593         }
2594
2595         LOGD("ret : 0x%x", ret);
2596
2597         return ret;
2598 }
2599
2600
2601 int recorder_foreach_supported_file_format(recorder_h recorder, recorder_supported_file_format_cb foreach_cb, void *user_data)
2602 {
2603         int ret = RECORDER_ERROR_NONE;
2604         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2605         muse_recorder_api_e api = MUSE_RECORDER_API_FOREACH_SUPPORTED_FILE_FORMAT;
2606
2607         if (!pc || !pc->cb_info || foreach_cb == NULL) {
2608                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2609                 return RECORDER_ERROR_INVALID_PARAMETER;
2610         }
2611
2612         LOGD("Enter, handle :%x", pc->remote_handle);
2613
2614         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_FILE_FORMAT] = foreach_cb;
2615         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_FILE_FORMAT] = user_data;
2616
2617         _recorder_msg_send(api, pc->cb_info, &ret);
2618
2619         LOGD("ret : 0x%x", ret);
2620
2621         return ret;
2622 }
2623
2624
2625 int recorder_attr_set_size_limit(recorder_h recorder, int kbyte)
2626 {
2627         int ret = RECORDER_ERROR_NONE;
2628         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_SIZE_LIMIT;
2629         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2630         recorder_msg_param param;
2631
2632         if (!pc || !pc->cb_info) {
2633                 LOGE("NULL handle");
2634                 return RECORDER_ERROR_INVALID_PARAMETER;
2635         }
2636
2637         LOGD("ENTER");
2638
2639         RECORDER_MSG_PARAM_SET(param, INT, kbyte);
2640
2641         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2642
2643         LOGD("ret : 0x%x", ret);
2644
2645         return ret;
2646 }
2647
2648
2649 int recorder_attr_set_time_limit(recorder_h recorder, int second)
2650 {
2651         int ret = RECORDER_ERROR_NONE;
2652         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_TIME_LIMIT;
2653         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2654         recorder_msg_param param;
2655
2656         if (!pc || !pc->cb_info) {
2657                 LOGE("NULL handle");
2658                 return RECORDER_ERROR_INVALID_PARAMETER;
2659         }
2660
2661         LOGD("ENTER");
2662
2663         RECORDER_MSG_PARAM_SET(param, INT, second);
2664
2665         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2666
2667         LOGD("ret : 0x%x", ret);
2668
2669         return ret;
2670 }
2671
2672
2673 int recorder_attr_set_audio_device(recorder_h recorder, recorder_audio_device_e device)
2674 {
2675         int ret = RECORDER_ERROR_NONE;
2676         int set_device = (int)device;
2677         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_AUDIO_DEVICE;
2678         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2679         recorder_msg_param param;
2680
2681         if (!pc || !pc->cb_info) {
2682                 LOGE("NULL handle");
2683                 return RECORDER_ERROR_INVALID_PARAMETER;
2684         };
2685
2686         LOGD("ENTER");
2687
2688         RECORDER_MSG_PARAM_SET(param, INT, set_device);
2689
2690         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2691
2692         LOGD("ret : 0x%x", ret);
2693
2694         return ret;
2695 }
2696
2697
2698 int recorder_set_audio_encoder(recorder_h recorder, recorder_audio_codec_e codec)
2699 {
2700         int ret = RECORDER_ERROR_NONE;
2701         int set_codec = (int)codec;
2702         muse_recorder_api_e api = MUSE_RECORDER_API_SET_AUDIO_ENCODER;
2703         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2704         recorder_msg_param param;
2705
2706         if (!pc || !pc->cb_info) {
2707                 LOGE("NULL handle");
2708                 return RECORDER_ERROR_INVALID_PARAMETER;
2709         }
2710
2711         LOGD("ENTER");
2712
2713         RECORDER_MSG_PARAM_SET(param, INT, set_codec);
2714
2715         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2716
2717         LOGD("ret : 0x%x", ret);
2718
2719         return ret;
2720 }
2721
2722
2723 int recorder_get_audio_encoder(recorder_h recorder, recorder_audio_codec_e *codec)
2724 {
2725         int ret = RECORDER_ERROR_NONE;
2726         muse_recorder_api_e api = MUSE_RECORDER_API_GET_AUDIO_ENCODER;
2727         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2728
2729         if (!pc || !pc->cb_info) {
2730                 LOGE("NULL handle");
2731                 return RECORDER_ERROR_INVALID_PARAMETER;
2732         }
2733
2734         if (codec == NULL) {
2735                 LOGE("codec is NULL");
2736                 return RECORDER_ERROR_INVALID_PARAMETER;
2737         }
2738
2739         LOGD("ENTER");
2740
2741         _recorder_msg_send(api, pc->cb_info, &ret);
2742
2743         if (ret == RECORDER_ERROR_NONE)
2744                 *codec = (recorder_audio_codec_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_ENCODER];
2745
2746         LOGD("ret : 0x%x, codec %d", ret, *codec);
2747
2748         return ret;
2749 }
2750
2751
2752 int recorder_set_video_encoder(recorder_h recorder, recorder_video_codec_e codec)
2753 {
2754         int ret = RECORDER_ERROR_NONE;
2755         int set_codec = (int)codec;
2756         muse_recorder_api_e api = MUSE_RECORDER_API_SET_VIDEO_ENCODER;
2757         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2758         recorder_msg_param param;
2759
2760         if (!pc || !pc->cb_info) {
2761                 LOGE("NULL handle");
2762                 return RECORDER_ERROR_INVALID_PARAMETER;
2763         }
2764
2765         LOGD("ENTER");
2766
2767         RECORDER_MSG_PARAM_SET(param, INT, set_codec);
2768
2769         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2770
2771         LOGD("ret : 0x%x", ret);
2772
2773         return ret;
2774 }
2775
2776
2777 int recorder_get_video_encoder(recorder_h recorder, recorder_video_codec_e *codec)
2778 {
2779         int ret = RECORDER_ERROR_NONE;
2780         muse_recorder_api_e api = MUSE_RECORDER_API_GET_VIDEO_ENCODER;
2781         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2782
2783         if (!pc || !pc->cb_info) {
2784                 LOGE("NULL handle");
2785                 return RECORDER_ERROR_INVALID_PARAMETER;
2786         }
2787
2788         if (codec == NULL) {
2789                 LOGE("codec is NULL");
2790                 return RECORDER_ERROR_INVALID_PARAMETER;
2791         }
2792
2793         LOGD("ENTER");
2794
2795         _recorder_msg_send(api, pc->cb_info, &ret);
2796
2797         if (ret == RECORDER_ERROR_NONE)
2798                 *codec = (recorder_video_codec_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_ENCODER];
2799
2800         LOGD("ret : 0x%x, codec %d", ret, *codec);
2801
2802         return ret;
2803 }
2804
2805
2806 int recorder_attr_set_audio_samplerate(recorder_h recorder, int samplerate)
2807 {
2808         int ret = RECORDER_ERROR_NONE;
2809         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_AUDIO_SAMPLERATE;
2810         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2811         recorder_msg_param param;
2812
2813         if (!pc || !pc->cb_info) {
2814                 LOGE("NULL handle");
2815                 return RECORDER_ERROR_INVALID_PARAMETER;
2816         }
2817
2818         LOGD("ENTER, samplerate : %d", samplerate);
2819
2820         RECORDER_MSG_PARAM_SET(param, INT, samplerate);
2821
2822         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2823
2824         LOGD("ret : 0x%x", ret);
2825
2826         return ret;
2827 }
2828
2829
2830 int recorder_attr_set_audio_encoder_bitrate(recorder_h recorder, int bitrate)
2831 {
2832         int ret = RECORDER_ERROR_NONE;
2833         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_AUDIO_ENCODER_BITRATE;
2834         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2835         recorder_msg_param param;
2836
2837         if (!pc || !pc->cb_info) {
2838                 LOGE("NULL handle");
2839                 return RECORDER_ERROR_INVALID_PARAMETER;
2840         }
2841
2842         LOGD("ENTER");
2843
2844         RECORDER_MSG_PARAM_SET(param, INT, bitrate);
2845
2846         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2847
2848         LOGD("ret : 0x%x", ret);
2849
2850         return ret;
2851 }
2852
2853
2854 int recorder_attr_set_video_encoder_bitrate(recorder_h recorder, int bitrate)
2855 {
2856         int ret = RECORDER_ERROR_NONE;
2857         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_VIDEO_ENCODER_BITRATE;
2858         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2859         recorder_msg_param param;
2860
2861         if (!pc || !pc->cb_info) {
2862                 LOGE("NULL handle");
2863                 return RECORDER_ERROR_INVALID_PARAMETER;
2864         }
2865
2866         LOGD("ENTER");
2867
2868         RECORDER_MSG_PARAM_SET(param, INT, bitrate);
2869
2870         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2871
2872         LOGD("ret : 0x%x", ret);
2873
2874         return ret;
2875 }
2876
2877
2878 int recorder_attr_get_size_limit(recorder_h recorder, int *kbyte)
2879 {
2880         int ret = RECORDER_ERROR_NONE;
2881         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_SIZE_LIMIT;
2882         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2883
2884         if (!pc || !pc->cb_info) {
2885                 LOGE("NULL handle");
2886                 return RECORDER_ERROR_INVALID_PARAMETER;
2887         }
2888
2889         if (kbyte == NULL) {
2890                 LOGE("NULL pointer kbyte");
2891                 return RECORDER_ERROR_INVALID_PARAMETER;
2892         }
2893
2894         LOGD("ENTER");
2895
2896         _recorder_msg_send(api, pc->cb_info, &ret);
2897
2898         if (ret == RECORDER_ERROR_NONE)
2899                 *kbyte = pc->cb_info->get_int_value[_RECORDER_GET_INT_SIZE_LIMIT];
2900
2901         LOGD("ret : 0x%x, %d kbyte", ret, *kbyte);
2902
2903         return ret;
2904 }
2905
2906
2907 int recorder_attr_get_time_limit(recorder_h recorder, int *second)
2908 {
2909         int ret = RECORDER_ERROR_NONE;
2910         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_TIME_LIMIT;
2911         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2912
2913         if (!pc || !pc->cb_info) {
2914                 LOGE("NULL handle");
2915                 return RECORDER_ERROR_INVALID_PARAMETER;
2916         }
2917
2918         if (second == NULL) {
2919                 LOGE("NULL pointer second");
2920                 return RECORDER_ERROR_INVALID_PARAMETER;
2921         }
2922
2923         LOGD("ENTER");
2924
2925         _recorder_msg_send(api, pc->cb_info, &ret);
2926
2927         if (ret == RECORDER_ERROR_NONE)
2928                 *second = pc->cb_info->get_int_value[_RECORDER_GET_INT_TIME_LIMIT];
2929
2930         LOGD("ret : 0x%x, %d second", ret, *second);
2931
2932         return ret;
2933 }
2934
2935
2936 int recorder_attr_get_audio_device(recorder_h recorder, recorder_audio_device_e *device)
2937 {
2938         int ret = RECORDER_ERROR_NONE;
2939         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_AUDIO_DEVICE;
2940         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2941
2942         if (!pc || !pc->cb_info) {
2943                 LOGE("NULL handle");
2944                 return RECORDER_ERROR_INVALID_PARAMETER;
2945         }
2946
2947         if (device == NULL) {
2948                 LOGE("NULL pointer device");
2949                 return RECORDER_ERROR_INVALID_PARAMETER;
2950         }
2951
2952         LOGD("ENTER");
2953
2954         _recorder_msg_send(api, pc->cb_info, &ret);
2955
2956         if (ret == RECORDER_ERROR_NONE)
2957                 *device = (recorder_audio_device_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_DEVICE];
2958
2959         LOGD("ret : 0x%x, device %d", ret, *device);
2960
2961         return ret;
2962 }
2963
2964
2965 int recorder_attr_get_audio_samplerate(recorder_h recorder, int *samplerate)
2966 {
2967         int ret = RECORDER_ERROR_NONE;
2968         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_AUDIO_SAMPLERATE;
2969         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2970
2971         if (!pc || !pc->cb_info) {
2972                 LOGE("NULL handle");
2973                 return RECORDER_ERROR_INVALID_PARAMETER;
2974         }
2975
2976         if (samplerate == NULL) {
2977                 LOGE("NULL pointer handle");
2978                 return RECORDER_ERROR_INVALID_PARAMETER;
2979         }
2980
2981         LOGD("ENTER");
2982
2983         _recorder_msg_send(api, pc->cb_info, &ret);
2984
2985         if (ret == RECORDER_ERROR_NONE)
2986                 *samplerate = pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_SAMPLERATE];
2987
2988         LOGD("ret : 0x%x, samplerate %d", ret, *samplerate);
2989
2990         return ret;
2991 }
2992
2993
2994 int recorder_attr_get_audio_encoder_bitrate(recorder_h recorder, int *bitrate)
2995 {
2996         int ret = RECORDER_ERROR_NONE;
2997         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_AUDIO_ENCODER_BITRATE;
2998         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2999
3000         if (!pc || !pc->cb_info) {
3001                 LOGE("NULL handle");
3002                 return RECORDER_ERROR_INVALID_PARAMETER;
3003         }
3004
3005         if (bitrate == NULL) {
3006                 LOGE("NULL pointer");
3007                 return RECORDER_ERROR_INVALID_PARAMETER;
3008         }
3009
3010         LOGD("ENTER");
3011
3012         _recorder_msg_send(api, pc->cb_info, &ret);
3013
3014         if (ret == RECORDER_ERROR_NONE)
3015                 *bitrate = pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_ENCODER_BITRATE];
3016
3017         LOGD("ret : 0x%x, bitrate %d", ret, *bitrate);
3018
3019         return ret;
3020 }
3021
3022
3023 int recorder_attr_get_video_encoder_bitrate(recorder_h recorder, int *bitrate)
3024 {
3025         int ret = RECORDER_ERROR_NONE;
3026         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_VIDEO_ENCODER_BITRATE;
3027         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3028
3029         if (!pc || !pc->cb_info) {
3030                 LOGE("NULL handle");
3031                 return RECORDER_ERROR_INVALID_PARAMETER;
3032         }
3033
3034         if (bitrate == NULL) {
3035                 LOGE("NULL pointer");
3036                 return RECORDER_ERROR_INVALID_PARAMETER;
3037         }
3038
3039         LOGD("ENTER");
3040
3041         _recorder_msg_send(api, pc->cb_info, &ret);
3042
3043         if (ret == RECORDER_ERROR_NONE)
3044                 *bitrate = pc->cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_ENCODER_BITRATE];
3045
3046         LOGD("ret : 0x%x", ret);
3047
3048         return ret;
3049 }
3050
3051
3052 int recorder_foreach_supported_audio_encoder(recorder_h recorder, recorder_supported_audio_encoder_cb foreach_cb, void *user_data)
3053 {
3054         int ret = RECORDER_ERROR_NONE;
3055         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3056         muse_recorder_api_e api = MUSE_RECORDER_API_FOREACH_SUPPORTED_AUDIO_ENCODER;
3057
3058         if (!pc || !pc->cb_info || foreach_cb == NULL) {
3059                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
3060                 return RECORDER_ERROR_INVALID_PARAMETER;
3061         }
3062
3063         LOGD("Enter, handle :%x", pc->remote_handle);
3064
3065         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_AUDIO_ENCODER] = foreach_cb;
3066         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_AUDIO_ENCODER] = user_data;
3067
3068         _recorder_msg_send(api, pc->cb_info, &ret);
3069
3070         LOGD("ret : 0x%x", ret);
3071
3072         return ret;
3073 }
3074
3075
3076 int recorder_foreach_supported_video_encoder(recorder_h recorder, recorder_supported_video_encoder_cb foreach_cb, void *user_data)
3077 {
3078         int ret = RECORDER_ERROR_NONE;
3079         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3080         muse_recorder_api_e api = MUSE_RECORDER_API_FOREACH_SUPPORTED_VIDEO_ENCODER;
3081
3082         if (!pc || !pc->cb_info || foreach_cb == NULL) {
3083                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
3084                 return RECORDER_ERROR_INVALID_PARAMETER;
3085         }
3086
3087         LOGD("Enter, handle :%x", pc->remote_handle);
3088
3089         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_ENCODER] = foreach_cb;
3090         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_ENCODER] = user_data;
3091
3092         _recorder_msg_send(api, pc->cb_info, &ret);
3093
3094         LOGD("ret : 0x%x", ret);
3095
3096         return ret;
3097 }
3098
3099
3100 int recorder_attr_set_mute(recorder_h recorder, bool enable)
3101 {
3102         int ret = RECORDER_ERROR_NONE;
3103         int set_enable = (int)enable;
3104         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_MUTE;
3105         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3106         recorder_msg_param param;
3107
3108         if (!pc || !pc->cb_info) {
3109                 LOGE("NULL handle");
3110                 return RECORDER_ERROR_INVALID_PARAMETER;
3111         }
3112
3113         LOGD("ENTER");
3114
3115         RECORDER_MSG_PARAM_SET(param, INT, set_enable);
3116
3117         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
3118
3119         LOGD("ret : 0x%x", ret);
3120
3121         return ret;
3122 }
3123
3124
3125 bool recorder_attr_is_muted(recorder_h recorder)
3126 {
3127         int ret = false;
3128         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_IS_MUTED;
3129         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3130
3131         if (!pc || !pc->cb_info) {
3132                 LOGE("NULL handle");
3133                 return false;
3134         }
3135
3136         LOGD("ENTER");
3137
3138         _recorder_msg_send(api, pc->cb_info, &ret);
3139
3140         if (ret == RECORDER_ERROR_SERVICE_DISCONNECTED)
3141                 ret = false;
3142
3143         LOGD("ret : %d", ret);
3144
3145         return (bool)ret;
3146 }
3147
3148
3149 int recorder_attr_set_recording_motion_rate(recorder_h recorder, double rate)
3150 {
3151         int ret = RECORDER_ERROR_NONE;
3152         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_RECORDING_MOTION_RATE;
3153         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3154         recorder_msg_param param;
3155
3156         if (!pc || !pc->cb_info) {
3157                 LOGE("NULL handle");
3158                 return RECORDER_ERROR_INVALID_PARAMETER;
3159         }
3160
3161         LOGD("ENTER - %.20lf", rate);
3162
3163         RECORDER_MSG_PARAM_SET(param, DOUBLE, rate);
3164
3165         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
3166
3167         LOGD("ret : 0x%x", ret);
3168
3169         return ret;
3170 }
3171
3172
3173 int recorder_attr_get_recording_motion_rate(recorder_h recorder, double *rate)
3174 {
3175         int ret = RECORDER_ERROR_NONE;
3176         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_RECORDING_MOTION_RATE;
3177         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3178
3179         if (!pc || !pc->cb_info) {
3180                 LOGE("NULL handle");
3181                 return RECORDER_ERROR_INVALID_PARAMETER;
3182         }
3183
3184         if (rate == NULL) {
3185                 LOGE("rate is NULL");
3186                 return RECORDER_ERROR_INVALID_PARAMETER;
3187         }
3188
3189         LOGD("ENTER");
3190
3191         _recorder_msg_send(api, pc->cb_info, &ret);
3192         if (ret == RECORDER_ERROR_NONE)
3193                 *rate = pc->cb_info->get_double_value[_RECORDER_GET_DOUBLE_RECORDING_MOTION_RATE];
3194
3195         LOGD("ret : 0x%x - rate %.20lf", ret, *rate);
3196
3197         return ret;
3198 }
3199
3200
3201 int recorder_attr_set_audio_channel(recorder_h recorder, int channel_count)
3202 {
3203         int ret = RECORDER_ERROR_NONE;
3204         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_AUDIO_CHANNEL;
3205         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3206         recorder_msg_param param;
3207
3208         if (!pc || !pc->cb_info) {
3209                 LOGE("NULL handle");
3210                 return RECORDER_ERROR_INVALID_PARAMETER;
3211         }
3212
3213         LOGD("ENTER");
3214
3215         RECORDER_MSG_PARAM_SET(param, INT, channel_count);
3216
3217         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
3218
3219         LOGD("ret : 0x%x", ret);
3220
3221         return ret;
3222 }
3223
3224
3225 int recorder_attr_get_audio_channel(recorder_h recorder, int *channel_count)
3226 {
3227         int ret = RECORDER_ERROR_NONE;
3228         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_AUDIO_CHANNEL;
3229         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3230
3231         if (!pc || !pc->cb_info) {
3232                 LOGE("NULL handle");
3233                 return RECORDER_ERROR_INVALID_PARAMETER;
3234         }
3235
3236         if (channel_count == NULL) {
3237                 LOGE("channel_count is NULL");
3238                 return RECORDER_ERROR_INVALID_PARAMETER;
3239         }
3240
3241         LOGD("ENTER");
3242
3243         _recorder_msg_send(api, pc->cb_info, &ret);
3244
3245         if (ret == RECORDER_ERROR_NONE)
3246                 *channel_count = pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_CHANNEL];
3247
3248         LOGD("ret : 0x%x, channel count %d", ret, *channel_count);
3249
3250         return ret;
3251 }
3252
3253
3254 int recorder_attr_set_orientation_tag(recorder_h recorder, recorder_rotation_e orientation)
3255 {
3256         int ret = RECORDER_ERROR_NONE;
3257         int set_orientation = (int)orientation;
3258         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_ORIENTATION_TAG;
3259         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3260         recorder_msg_param param;
3261
3262         if (!pc || !pc->cb_info) {
3263                 LOGE("NULL handle");
3264                 return RECORDER_ERROR_INVALID_PARAMETER;
3265         }
3266
3267         LOGD("ENTER");
3268
3269         RECORDER_MSG_PARAM_SET(param, INT, set_orientation);
3270
3271         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
3272
3273         LOGD("ret : 0x%x", ret);
3274
3275         return ret;
3276 }
3277
3278
3279 int  recorder_attr_get_orientation_tag(recorder_h recorder, recorder_rotation_e *orientation)
3280 {
3281         int ret = RECORDER_ERROR_NONE;
3282         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_ORIENTATION_TAG;
3283         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3284
3285         if (!pc || !pc->cb_info) {
3286                 LOGE("NULL handle");
3287                 return RECORDER_ERROR_INVALID_PARAMETER;
3288         }
3289
3290         if (orientation == NULL) {
3291                 LOGE("orientation is NULL");
3292                 return RECORDER_ERROR_INVALID_PARAMETER;
3293         }
3294
3295         LOGD("ENTER");
3296
3297         _recorder_msg_send(api, pc->cb_info, &ret);
3298
3299         if (ret == RECORDER_ERROR_NONE)
3300                 *orientation = (recorder_rotation_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_ORIENTATION_TAG];
3301
3302         LOGD("ret : 0x%x, orientation %d", ret, *orientation);
3303
3304         return ret;
3305 }
3306
3307
3308 int recorder_get_device_state(recorder_type_e type, recorder_device_state_e *state)
3309 {
3310         int ret = RECORDER_ERROR_NONE;
3311         int sock_fd = -1;
3312         int get_device_state = 0;
3313         char *send_msg = NULL;
3314         char recv_msg[MUSE_RECORDER_MSG_MAX_LENGTH] = {'\0',};
3315
3316         if (!state) {
3317                 LOGE("NULL pointer");
3318                 return RECORDER_ERROR_INVALID_PARAMETER;
3319         }
3320
3321         LOGD("Enter - type %d", type);
3322
3323         sock_fd = muse_core_client_new();
3324         if (sock_fd < 0) {
3325                 LOGE("muse_core_client_new failed - returned fd %d", sock_fd);
3326                 ret = RECORDER_ERROR_INVALID_OPERATION;
3327                 goto _GET_DEVICE_STATE_EXIT;
3328         }
3329
3330         send_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_API_GET_DEVICE_STATE,
3331                 MUSE_TYPE_INT, "module", MUSE_RECORDER,
3332                 MUSE_TYPE_INT, PARAM_RECORDER_TYPE, type,
3333                 NULL);
3334         if (!send_msg) {
3335                 LOGE("NULL msg");
3336                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
3337                 goto _GET_DEVICE_STATE_EXIT;
3338         }
3339
3340         LOGD("sock_fd : %d, msg : %s", sock_fd, send_msg);
3341
3342         ret = muse_core_ipc_send_msg(sock_fd, send_msg);
3343
3344         muse_core_msg_json_factory_free(send_msg);
3345         send_msg = NULL;
3346
3347         if (ret < 0) {
3348                 LOGE("send msg failed %d", errno);
3349                 ret = RECORDER_ERROR_INVALID_OPERATION;
3350                 goto _GET_DEVICE_STATE_EXIT;
3351         }
3352
3353         ret = muse_core_ipc_recv_msg(sock_fd, recv_msg);
3354         if (ret <= 0) {
3355                 LOGE("recv msg failed %d", errno);
3356                 ret = RECORDER_ERROR_INVALID_OPERATION;
3357                 goto _GET_DEVICE_STATE_EXIT;
3358         }
3359
3360         if (!muse_recorder_msg_get(ret, recv_msg)) {
3361                 LOGE("failed to get return value from msg [%s]", recv_msg);
3362                 ret = RECORDER_ERROR_INVALID_OPERATION;
3363                 goto _GET_DEVICE_STATE_EXIT;
3364         }
3365
3366         if (ret == RECORDER_ERROR_NONE) {
3367                 if (muse_recorder_msg_get(get_device_state, recv_msg)) {
3368                         *state = (recorder_device_state_e)get_device_state;
3369                         LOGD("device type %d state %d", type, *state);
3370                 } else {
3371                         LOGE("failed to get device state from msg [%s]", recv_msg);
3372                         ret = RECORDER_ERROR_INVALID_OPERATION;
3373                 }
3374         } else {
3375                 LOGE("failed 0x%x", ret);
3376         }
3377
3378 _GET_DEVICE_STATE_EXIT:
3379         if (sock_fd > -1) {
3380                 muse_core_connection_close(sock_fd);
3381                 sock_fd = -1;
3382         }
3383
3384         return ret;
3385 }
3386
3387
3388 int recorder_add_device_state_changed_cb(recorder_device_state_changed_cb callback, void *user_data, int *cb_id)
3389 {
3390         int ret = RECORDER_ERROR_NONE;
3391         recorder_device_state_e state = RECORDER_DEVICE_STATE_IDLE;
3392         recorder_cb_info *info = NULL;
3393
3394         if (!callback || !cb_id) {
3395                 LOGE("invalid pointer %p %p", callback, cb_id);
3396                 return RECORDER_ERROR_INVALID_PARAMETER;
3397         }
3398
3399         g_mutex_lock(&g_rec_dev_state_changed_cb_lock);
3400
3401         /* check recorder support */
3402         ret = recorder_get_device_state(RECORDER_TYPE_AUDIO, &state);
3403         if (ret != RECORDER_ERROR_NONE) {
3404                 LOGE("get device state failed");
3405                 g_mutex_unlock(&g_rec_dev_state_changed_cb_lock);
3406                 return ret;
3407         }
3408
3409         info = g_new0(recorder_cb_info, 1);
3410         if (!info) {
3411                 LOGE("info failed");
3412                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
3413                 goto _DONE;
3414         }
3415
3416         info->id = ++g_rec_dev_state_changed_cb_id;
3417         info->callback = (void *)callback;
3418         info->user_data = user_data;
3419
3420         *cb_id = info->id;
3421
3422         /* subscribe dbus signal for camera state change */
3423         if (!g_rec_dev_state_changed_cb_conn) {
3424                 g_rec_dev_state_changed_cb_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
3425                 if (!g_rec_dev_state_changed_cb_conn) {
3426                         LOGE("failed to get gdbus connection");
3427                         ret = RECORDER_ERROR_INVALID_OPERATION;
3428                         goto _DONE;
3429                 }
3430
3431                 LOGD("subscribe signal %s - %s - %s",
3432                         MM_CAMCORDER_DBUS_OBJECT,
3433                         MM_CAMCORDER_DBUS_INTERFACE_RECORDER,
3434                         MM_CAMCORDER_DBUS_SIGNAL_STATE_CHANGED);
3435
3436                 g_rec_dev_state_changed_cb_subscribe_id = g_dbus_connection_signal_subscribe(g_rec_dev_state_changed_cb_conn,
3437                         NULL, MM_CAMCORDER_DBUS_INTERFACE_RECORDER, MM_CAMCORDER_DBUS_SIGNAL_STATE_CHANGED, MM_CAMCORDER_DBUS_OBJECT, NULL,
3438                         G_DBUS_SIGNAL_FLAGS_NONE, (GDBusSignalCallback)__recorder_device_state_changed_cb, NULL, NULL);
3439                 if (!g_rec_dev_state_changed_cb_subscribe_id) {
3440                         LOGE("failed to get gdbus connection");
3441                         ret = RECORDER_ERROR_INVALID_OPERATION;
3442                         goto _DONE;
3443                 }
3444
3445                 LOGD("signal subscribe id %u", g_rec_dev_state_changed_cb_subscribe_id);
3446         }
3447
3448         g_rec_dev_state_changed_cb_list = g_list_prepend(g_rec_dev_state_changed_cb_list, (gpointer)info);
3449
3450         LOGD("callback id %d", info->id);
3451
3452 _DONE:
3453         if (ret != RECORDER_ERROR_NONE) {
3454                 if (info) {
3455                         g_free(info);
3456                         info = NULL;
3457                 }
3458
3459                 if (g_rec_dev_state_changed_cb_conn) {
3460                         g_object_unref(g_rec_dev_state_changed_cb_conn);
3461                         g_rec_dev_state_changed_cb_conn = NULL;
3462                 }
3463         }
3464
3465         g_mutex_unlock(&g_rec_dev_state_changed_cb_lock);
3466
3467         return ret;
3468 }
3469
3470
3471 int recorder_remove_device_state_changed_cb(int cb_id)
3472 {
3473         int ret = RECORDER_ERROR_NONE;
3474         recorder_device_state_e state = RECORDER_DEVICE_STATE_IDLE;
3475         GList *tmp_list = NULL;
3476         recorder_cb_info *info = NULL;
3477
3478         /* check recorder support */
3479         ret = recorder_get_device_state(RECORDER_TYPE_AUDIO, &state);
3480         if (ret != RECORDER_ERROR_NONE) {
3481                 LOGE("get device state failed");
3482                 return ret;
3483         }
3484
3485         g_mutex_lock(&g_rec_dev_state_changed_cb_lock);
3486
3487         if (!g_rec_dev_state_changed_cb_list) {
3488                 LOGE("there is no callback info");
3489                 ret = RECORDER_ERROR_INVALID_OPERATION;
3490                 goto _DONE;
3491         }
3492
3493         tmp_list = g_rec_dev_state_changed_cb_list;
3494
3495         do {
3496                 info = tmp_list->data;
3497                 tmp_list = tmp_list->next;
3498
3499                 if (!info) {
3500                         LOGW("NULL info");
3501                         continue;
3502                 }
3503
3504                 if (info->id == cb_id) {
3505                         g_rec_dev_state_changed_cb_list = g_list_remove(g_rec_dev_state_changed_cb_list, info);
3506
3507                         g_free(info);
3508                         info = NULL;
3509
3510                         if (!g_rec_dev_state_changed_cb_list) {
3511                                 /* no remained callback */
3512                                 if (g_rec_dev_state_changed_cb_conn) {
3513                                         /* unsubscribe signal */
3514                                         g_dbus_connection_signal_unsubscribe(g_rec_dev_state_changed_cb_conn, g_rec_dev_state_changed_cb_subscribe_id);
3515                                         g_rec_dev_state_changed_cb_subscribe_id = 0;
3516
3517                                         /* unref connection */
3518                                         g_object_unref(g_rec_dev_state_changed_cb_conn);
3519                                         g_rec_dev_state_changed_cb_conn = NULL;
3520                                 }
3521                         }
3522
3523                         LOGD("id %d callback removed", cb_id);
3524                         ret = RECORDER_ERROR_NONE;
3525
3526                         goto _DONE;
3527                 }
3528         } while (tmp_list);
3529
3530         LOGE("id %d callback not found", cb_id);
3531         ret = RECORDER_ERROR_INVALID_PARAMETER;
3532
3533 _DONE:
3534         g_mutex_unlock(&g_rec_dev_state_changed_cb_lock);
3535
3536         return ret;
3537 }