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