Received message is at risk for corruption with previous message.
[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         int msg_length = 0;
795         char *recv_msg = NULL;
796         char *error_msg = NULL;
797         char **parse_str = NULL;
798         recorder_cb_info_s *cb_info = (recorder_cb_info_s *)data;
799
800         if (cb_info == NULL) {
801                 LOGE("cb_info NULL");
802                 return NULL;
803         }
804
805         LOGD("start");
806
807         parse_str = (char **)malloc(sizeof(char *) * RECORDER_PARSED_STRING_NUM_MAX);
808         if (parse_str == NULL) {
809                 LOGE("parse_str malloc failed");
810                 return NULL;
811         }
812
813         for (i = 0 ; i < RECORDER_PARSED_STRING_NUM_MAX ; i++) {
814                 parse_str[i] = (char *)malloc(sizeof(char) * MUSE_RECORDER_MSG_MAX_LENGTH);
815                 if (parse_str[i] == NULL) {
816                         LOGE("parse_str[%d] malloc failed", i);
817                         goto CB_HANDLER_EXIT;
818                 }
819         }
820
821         recv_msg = cb_info->recv_msg;
822
823         while (g_atomic_int_get(&cb_info->msg_recv_running)) {
824                 ret = muse_core_ipc_recv_msg(cb_info->fd, recv_msg);
825                 if (ret <= 0) {
826                         cb_info->is_server_connected = FALSE;
827                         LOGE("receive msg failed - server disconnected");
828                         break;
829                 }
830
831                 recv_msg[ret] = '\0';
832
833                 str_pos = 0;
834                 prev_pos = 0;
835                 num_token = 0;
836
837                 /*LOGD("recvMSg : %s, length : %d", recv_msg, ret);*/
838
839                 /* Need to split the combined entering msgs.
840                     This module supports up to 20 combined msgs. */
841                 for (str_pos = 0; str_pos < ret; str_pos++) {
842                         if (recv_msg[str_pos] == '}') {
843                                 msg_length = str_pos - prev_pos + 1;
844
845                                 if (msg_length < MUSE_RECORDER_MSG_MAX_LENGTH) {
846                                         strncpy(parse_str[num_token], recv_msg + prev_pos, msg_length);
847                                         parse_str[num_token][msg_length] = '\0';
848                                         num_token++;
849                                         /*LOGD("splitted msg : [%s], Index : %d", parse_str[num_token], num_token);*/
850                                 } else {
851                                         LOGW("too long message : length %d [%s]", msg_length, recv_msg + prev_pos);
852                                 }
853
854                                 prev_pos = str_pos + 1;
855
856                                 if (num_token >= RECORDER_PARSED_STRING_NUM_MAX) {
857                                         LOGE("There's too many tokens. Remained msg : %s", recv_msg[++str_pos]);
858                                         break;
859                                 }
860                         }
861                 }
862
863                 /*LOGD("num_token : %d", num_token);*/
864
865                 /* Re-construct to the useful single msg. */
866                 for (i = 0; i < num_token; i++) {
867                         if (i >= RECORDER_PARSED_STRING_NUM_MAX) {
868                                 LOGE("invalid token index %d", i);
869                                 break;
870                         }
871
872                         api = -1;
873                         api_class = -1;
874
875                         if (!muse_recorder_msg_get(api, parse_str[i])) {
876                                 LOGE("failed to get recorder api");
877                                 continue;
878                         }
879
880                         if (api == MUSE_RECORDER_CB_EVENT) {
881                                 if (!muse_recorder_msg_get(event, parse_str[i]) ||
882                                         !muse_recorder_msg_get(event_class, parse_str[i])) {
883                                         LOGE("failed to get event or event_class [%s]", parse_str[i]);
884                                         continue;
885                                 }
886                         } else {
887                                 if (!muse_recorder_msg_get(api_class, parse_str[i])) {
888                                         LOGE("failed to get api_class [%s]", parse_str[i]);
889                                         continue;
890                                 }
891                         }
892
893                         if (api_class == MUSE_RECORDER_API_CLASS_IMMEDIATE) {
894                                 if (api >= MUSE_RECORDER_API_MAX) {
895                                         LOGE("invalid api %d", api);
896                                         continue;
897                                 }
898
899                                 g_mutex_lock(&cb_info->api_mutex[api]);
900
901                                 if (!muse_recorder_msg_get(ret, parse_str[i])) {
902                                         LOGE("failed to get recorder ret");
903                                         g_mutex_unlock(&cb_info->api_mutex[api]);
904                                         continue;
905                                 }
906
907                                 cb_info->api_ret[api] = ret;
908                                 cb_info->api_activating[api] = 1;
909
910                                 if (api == MUSE_RECORDER_API_GET_DEVICE_STATE) {
911                                         g_atomic_int_set(&cb_info->msg_recv_running, 0);
912                                         LOGD("get device state done. close client cb handler");
913                                 } else {
914                                         switch (api) {
915                                         case MUSE_RECORDER_API_CREATE:
916                                                 if (ret != RECORDER_ERROR_NONE) {
917                                                         g_atomic_int_set(&cb_info->msg_recv_running, 0);
918                                                         LOGE("create error 0x%x. closing..", ret);
919                                                 }
920                                                 break;
921                                         case MUSE_RECORDER_API_DESTROY:
922                                                 if (ret == RECORDER_ERROR_NONE) {
923                                                         g_atomic_int_set(&cb_info->msg_recv_running, 0);
924                                                         LOGD("destroy done. closing..");
925                                                 }
926                                                 break;
927                                         default:
928                                                 _recorder_get_api_operation(api, cb_info, parse_str[i]);
929                                                 break;
930                                         }
931                                 }
932
933                                 g_cond_signal(&cb_info->api_cond[api]);
934                                 g_mutex_unlock(&cb_info->api_mutex[api]);
935                         } else if (api_class == MUSE_RECORDER_API_CLASS_THREAD_SUB || api == MUSE_RECORDER_CB_EVENT) {
936                                 __recorder_add_msg_to_queue(cb_info, api, event, event_class, parse_str[i]);
937                         } else {
938                                 LOGW("unknown recorder api %d and api_class %d", api, api_class);
939                         }
940                 }
941         }
942
943         LOGD("client cb exit - server connected %d", cb_info->is_server_connected);
944
945         if (!cb_info->is_server_connected) {
946                 /* send error msg for server disconnection */
947                 error_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_CB_EVENT,
948                         MUSE_TYPE_INT, "error", RECORDER_ERROR_SERVICE_DISCONNECTED,
949                         MUSE_TYPE_INT, "current_state", RECORDER_STATE_NONE,
950                         NULL);
951                 if (!error_msg) {
952                         LOGE("error_msg failed");
953                         goto CB_HANDLER_EXIT;
954                 }
955
956                 LOGE("add error msg for service disconnection done");
957
958                 __recorder_add_msg_to_queue(cb_info,
959                         MUSE_RECORDER_CB_EVENT,
960                         MUSE_RECORDER_EVENT_TYPE_ERROR,
961                         MUSE_RECORDER_EVENT_CLASS_THREAD_MAIN,
962                         error_msg);
963
964                 muse_core_msg_json_factory_free(error_msg);
965                 error_msg = NULL;
966
967                 LOGE("add error msg for service disconnection done");
968         }
969
970 CB_HANDLER_EXIT:
971         if (parse_str) {
972                 for (i = 0 ; i < RECORDER_PARSED_STRING_NUM_MAX ; i++) {
973                         if (parse_str[i]) {
974                                 free(parse_str[i]);
975                                 parse_str[i] = NULL;
976                         }
977                 }
978
979                 free(parse_str);
980                 parse_str = NULL;
981         }
982
983         return NULL;
984 }
985
986
987 static bool __create_msg_handler_thread(recorder_msg_handler_info_s *handler_info,
988         int type, const char *thread_name, recorder_cb_info_s *cb_info)
989 {
990         if (!handler_info || !thread_name || !cb_info) {
991                 LOGE("t:%d NULL %p %p %p",
992                         type, handler_info, thread_name, cb_info);
993                 return false;
994         }
995
996         LOGD("t:%d", type);
997
998         handler_info->type = type;
999         handler_info->queue = g_queue_new();
1000         if (handler_info->queue == NULL) {
1001                 LOGE("t:%d queue failed", type);
1002                 return false;
1003         }
1004
1005         g_mutex_init(&handler_info->mutex);
1006         g_cond_init(&handler_info->cond);
1007
1008         handler_info->cb_info = (void *)cb_info;
1009         g_atomic_int_set(&handler_info->running, 1);
1010
1011         handler_info->thread = g_thread_try_new(thread_name,
1012                 _recorder_msg_handler_func, (gpointer)handler_info, NULL);
1013         if (handler_info->thread == NULL) {
1014                 LOGE("t:%d thread failed", type);
1015
1016                 g_mutex_clear(&handler_info->mutex);
1017                 g_cond_clear(&handler_info->cond);
1018                 g_queue_free(handler_info->queue);
1019                 handler_info->queue = NULL;
1020
1021                 return false;
1022         }
1023
1024         LOGD("t:%d done", type);
1025
1026         return true;
1027 }
1028
1029
1030 static void __destroy_msg_handler_thread(recorder_msg_handler_info_s *handler_info)
1031 {
1032         int type = 0;
1033
1034         if (!handler_info) {
1035                 LOGE("NULL handler");
1036                 return;
1037         }
1038
1039         if (!handler_info->thread) {
1040                 LOGW("thread is not created");
1041                 return;
1042         }
1043
1044         type = handler_info->type;
1045
1046         LOGD("t:%d thread %p", type, handler_info->thread);
1047
1048         g_mutex_lock(&handler_info->mutex);
1049         g_atomic_int_set(&handler_info->running, 0);
1050         g_cond_signal(&handler_info->cond);
1051         g_mutex_unlock(&handler_info->mutex);
1052
1053         g_thread_join(handler_info->thread);
1054         g_thread_unref(handler_info->thread);
1055         handler_info->thread = NULL;
1056
1057         g_mutex_clear(&handler_info->mutex);
1058         g_cond_clear(&handler_info->cond);
1059         g_queue_free(handler_info->queue);
1060         handler_info->queue = NULL;
1061
1062         LOGD("t:%d done", type);
1063
1064         return;
1065 }
1066
1067
1068 static recorder_cb_info_s *_recorder_client_callback_new(gint sockfd, bool need_msg_handler_thread)
1069 {
1070         recorder_cb_info_s *cb_info = NULL;
1071         gint i = 0;
1072
1073         g_return_val_if_fail(sockfd > 0, NULL);
1074
1075         cb_info = g_new0(recorder_cb_info_s, 1);
1076         if (cb_info == NULL) {
1077                 LOGE("cb_info failed");
1078                 goto ErrorExit;
1079         }
1080
1081         for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
1082                 g_mutex_init(&cb_info->api_mutex[i]);
1083                 g_cond_init(&cb_info->api_cond[i]);
1084         }
1085
1086         g_mutex_init(&cb_info->idle_event_mutex);
1087         g_cond_init(&cb_info->idle_event_cond);
1088
1089         if (need_msg_handler_thread) {
1090                 /* message handler thread */
1091                 if (!__create_msg_handler_thread(&cb_info->msg_handler_info,
1092                         _RECORDER_MESSAGE_HANDLER_TYPE_GENERAL, "recorder_msg_handler", cb_info)) {
1093                         LOGE("msg_handler_info failed");
1094                         goto ErrorExit;
1095                 }
1096
1097                 /* message handler thread for audio stream callback */
1098                 if (!__create_msg_handler_thread(&cb_info->audio_stream_cb_info,
1099                         _RECORDER_MESSAGE_HANDLER_TYPE_AUDIO_STREAM_CB, "recorder_msg_handler:audio_stream_cb", cb_info)) {
1100                         LOGE("audio_stream_cb_info failed");
1101                         goto ErrorExit;
1102                 }
1103         }
1104
1105         cb_info->fd = sockfd;
1106
1107         /* message receive thread */
1108         g_atomic_int_set(&cb_info->msg_recv_running, 1);
1109         cb_info->msg_recv_thread = g_thread_try_new("recorder_msg_recv",
1110                 _recorder_msg_recv_func, (gpointer)cb_info, NULL);
1111         if (cb_info->msg_recv_thread == NULL) {
1112                 LOGE("message receive thread creation failed");
1113                 goto ErrorExit;
1114         }
1115
1116         cb_info->is_server_connected = TRUE;
1117
1118         return cb_info;
1119
1120 ErrorExit:
1121         if (cb_info) {
1122                 __destroy_msg_handler_thread(&cb_info->msg_handler_info);
1123                 __destroy_msg_handler_thread(&cb_info->audio_stream_cb_info);
1124
1125                 g_mutex_clear(&cb_info->idle_event_mutex);
1126                 g_cond_clear(&cb_info->idle_event_cond);
1127
1128                 for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
1129                         g_mutex_clear(&cb_info->api_mutex[i]);
1130                         g_cond_clear(&cb_info->api_cond[i]);
1131                 }
1132
1133                 g_free(cb_info);
1134                 cb_info = NULL;
1135         }
1136
1137         return NULL;
1138 }
1139
1140 static int _recorder_client_wait_for_cb_return(muse_recorder_api_e api, recorder_cb_info_s *cb_info, int time_out)
1141 {
1142         int ret = RECORDER_ERROR_NONE;
1143         gint64 end_time;
1144
1145         /*LOGD("Enter api : %d", api);*/
1146
1147         if (!cb_info->is_server_connected) {
1148                 LOGE("server is disconnected");
1149                 return RECORDER_ERROR_SERVICE_DISCONNECTED;
1150         }
1151
1152         g_mutex_lock(&(cb_info->api_mutex[api]));
1153
1154         if (cb_info->api_activating[api] == 0) {
1155                 end_time = g_get_monotonic_time() + time_out * G_TIME_SPAN_SECOND;
1156                 if (g_cond_wait_until(&(cb_info->api_cond[api]), &(cb_info->api_mutex[api]), end_time)) {
1157                         ret = cb_info->api_ret[api];
1158                         cb_info->api_activating[api] = 0;
1159
1160                         /*LOGD("return value : 0x%x", ret);*/
1161                 } else {
1162                         ret = RECORDER_ERROR_INVALID_OPERATION;
1163
1164                         LOGE("api %d was TIMED OUT!", api);
1165                 }
1166         } else {
1167                 ret = cb_info->api_ret[api];
1168                 cb_info->api_activating[api] = 0;
1169
1170                 /*LOGD("condition is already checked for the api[%d], return[0x%x]", api, ret);*/
1171         }
1172
1173         if (ret != RECORDER_ERROR_NONE) {
1174                 LOGE("ERROR : api %d - ret 0x%x", api, ret);
1175
1176                 if (ret == RECORDER_ERROR_SOUND_POLICY)
1177                         LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY is deprecated and will be removed from next release.");
1178                 else if (ret == RECORDER_ERROR_SOUND_POLICY_BY_CALL)
1179                         LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY_BY_CALL is deprecated and will be removed from next release.");
1180                 else if (ret == RECORDER_ERROR_SOUND_POLICY_BY_ALARM)
1181                         LOGW("DEPRECATION WARNING: RECORDER_ERROR_SOUND_POLICY_BY_ALARM is deprecated and will be removed from next release.");
1182         }
1183
1184         g_mutex_unlock(&(cb_info->api_mutex[api]));
1185
1186         return ret;
1187 }
1188
1189
1190 static int _recorder_msg_send(int api, recorder_cb_info_s *cb_info, int *ret)
1191 {
1192         int send_ret = 0;
1193         char *msg = NULL;
1194
1195         if (!cb_info || !ret) {
1196                 LOGE("invalid pointer for api %d - %p %p", api, cb_info, ret);
1197                 return RECORDER_ERROR_INVALID_PARAMETER;
1198         }
1199
1200         msg = muse_core_msg_json_factory_new(api, NULL);
1201         if (!msg) {
1202                 LOGE("msg creation failed: api %d", api);
1203                 return RECORDER_ERROR_OUT_OF_MEMORY;
1204         }
1205
1206         /*LOGD("send msg %s", msg);*/
1207
1208         if (cb_info->is_server_connected)
1209                 send_ret = muse_core_ipc_send_msg(cb_info->fd, msg);
1210
1211         if (send_ret < 0) {
1212                 LOGE("message send failed");
1213                 *ret = RECORDER_ERROR_INVALID_OPERATION;
1214         } else {
1215                 *ret = _recorder_client_wait_for_cb_return(api, cb_info, RECORDER_CB_TIMEOUT);
1216         }
1217
1218         muse_core_msg_json_factory_free(msg);
1219
1220         return RECORDER_ERROR_NONE;
1221 }
1222
1223
1224 static int _recorder_msg_send_param1(int api, recorder_cb_info_s *cb_info, int *ret, recorder_msg_param *param)
1225 {
1226         int send_ret = 0;
1227         char *msg = NULL;
1228
1229         if (!cb_info || !ret || !param) {
1230                 LOGE("invalid pointer for api %d - %p %p %p", api, cb_info, ret, param);
1231                 return RECORDER_ERROR_INVALID_PARAMETER;
1232         }
1233
1234         /*LOGD("type %d, name %s", param->type, param->name);*/
1235
1236         switch (param->type) {
1237         case MUSE_TYPE_INT:
1238                 msg = muse_core_msg_json_factory_new(api, param->type, param->name, param->value.value_INT, NULL);
1239                 break;
1240         case MUSE_TYPE_DOUBLE:
1241                 msg = muse_core_msg_json_factory_new(api, param->type, param->name, param->value.value_DOUBLE, NULL);
1242                 break;
1243         case MUSE_TYPE_STRING:
1244                 msg = muse_core_msg_json_factory_new(api, param->type, param->name, param->value.value_STRING, NULL);
1245                 break;
1246         default:
1247                 LOGE("unknown type %d", param->type);
1248                 break;
1249         }
1250
1251         if (!msg) {
1252                 LOGE("msg creation failed: api %d, type %d, param name %s",
1253                         api, param->type, param->name);
1254                 return RECORDER_ERROR_OUT_OF_MEMORY;
1255         }
1256
1257         /*LOGD("send msg %s", msg);*/
1258
1259         if (cb_info->is_server_connected)
1260                 send_ret = muse_core_ipc_send_msg(cb_info->fd, msg);
1261
1262         if (send_ret < 0) {
1263                 LOGE("message send failed");
1264                 *ret = RECORDER_ERROR_INVALID_OPERATION;
1265         } else {
1266                 *ret = _recorder_client_wait_for_cb_return(api, cb_info, RECORDER_CB_TIMEOUT);
1267         }
1268
1269         muse_core_msg_json_factory_free(msg);
1270
1271         return RECORDER_ERROR_NONE;
1272 }
1273
1274
1275 static void _recorder_client_callback_destroy(recorder_cb_info_s *cb_info)
1276 {
1277         gint i = 0;
1278
1279         g_return_if_fail(cb_info != NULL);
1280
1281         LOGD("MSG receive thread[%p] destroy", cb_info->msg_recv_thread);
1282
1283         g_thread_join(cb_info->msg_recv_thread);
1284         g_thread_unref(cb_info->msg_recv_thread);
1285         cb_info->msg_recv_thread = NULL;
1286
1287         LOGD("msg_recv thread removed");
1288
1289         __destroy_msg_handler_thread(&cb_info->msg_handler_info);
1290         __destroy_msg_handler_thread(&cb_info->audio_stream_cb_info);
1291
1292         g_mutex_clear(&cb_info->idle_event_mutex);
1293         g_cond_clear(&cb_info->idle_event_cond);
1294
1295         for (i = 0 ; i < MUSE_RECORDER_API_MAX ; i++) {
1296                 g_mutex_clear(&cb_info->api_mutex[i]);
1297                 g_cond_clear(&cb_info->api_cond[i]);
1298         }
1299
1300         if (cb_info->fd > -1) {
1301                 muse_core_connection_close(cb_info->fd);
1302                 cb_info->fd = -1;
1303         }
1304
1305         if (cb_info->bufmgr) {
1306                 tbm_bufmgr_deinit(cb_info->bufmgr);
1307                 cb_info->bufmgr = NULL;
1308         }
1309         if (cb_info->get_filename) {
1310                 free(cb_info->get_filename);
1311                 cb_info->get_filename = NULL;
1312         }
1313
1314         g_free(cb_info);
1315         cb_info = NULL;
1316
1317         return;
1318 }
1319
1320
1321 static int _recorder_storage_device_supported_cb(int storage_id, storage_type_e type, storage_state_e state, const char *path, void *user_data)
1322 {
1323         char **root_directory = (char **)user_data;
1324
1325         if (root_directory == NULL) {
1326                 LOGE("user data is NULL");
1327                 return false;
1328         }
1329
1330         LOGD("storage id %d, type %d, state %d, path %s",
1331                 storage_id, type, state, path ? path : "NULL");
1332
1333         if (type == STORAGE_TYPE_INTERNAL && path) {
1334                 if (*root_directory) {
1335                         free(*root_directory);
1336                         *root_directory = NULL;
1337                 }
1338
1339                 *root_directory = strdup(path);
1340                 if (*root_directory) {
1341                         LOGD("get root directory %s", *root_directory);
1342                         return false;
1343                 } else {
1344                         LOGE("strdup %s failed", path);
1345                 }
1346         }
1347
1348         return true;
1349 }
1350
1351 static int _recorder_client_get_root_directory(char **root_directory)
1352 {
1353         int ret = STORAGE_ERROR_NONE;
1354
1355         if (root_directory == NULL) {
1356                 LOGE("user data is NULL");
1357                 return false;
1358         }
1359
1360         ret = storage_foreach_device_supported((storage_device_supported_cb)_recorder_storage_device_supported_cb, root_directory);
1361         if (ret != STORAGE_ERROR_NONE) {
1362                 LOGE("storage_foreach_device_supported failed 0x%x", ret);
1363                 return false;
1364         }
1365
1366         return true;
1367 }
1368
1369 static int _recorder_create_common(recorder_h *recorder, muse_recorder_type_e type, camera_h camera)
1370 {
1371         int ret = RECORDER_ERROR_NONE;
1372         int destroy_ret = RECORDER_ERROR_NONE;
1373         int sock_fd = -1;
1374         int send_ret = 0;
1375         char *send_msg = NULL;
1376         char *root_directory = NULL;
1377         intptr_t camera_handle = 0;
1378         intptr_t handle = 0;
1379         tbm_bufmgr bufmgr = NULL;
1380         recorder_cli_s *pc = NULL;
1381         recorder_msg_param param;
1382
1383         LOGD("Enter - type %d", type);
1384
1385         if (recorder == NULL) {
1386                 LOGE("NULL pointer for recorder handle");
1387                 return RECORDER_ERROR_INVALID_PARAMETER;
1388         }
1389
1390         if (type == MUSE_RECORDER_TYPE_VIDEO && camera == NULL) {
1391                 LOGE("NULL pointer for camera handle on video recorder mode");
1392                 return RECORDER_ERROR_INVALID_PARAMETER;
1393         }
1394
1395         bufmgr = tbm_bufmgr_init(-1);
1396         if (bufmgr == NULL) {
1397                 LOGE("get tbm bufmgr failed");
1398                 return RECORDER_ERROR_INVALID_OPERATION;
1399         }
1400
1401         pc = g_new0(recorder_cli_s, 1);
1402         if (pc == NULL) {
1403                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
1404                 goto _ERR_RECORDER_EXIT;
1405         }
1406
1407         sock_fd = muse_core_client_new();
1408         if (sock_fd < 0) {
1409                 LOGE("muse_core_client_new failed - returned fd %d", sock_fd);
1410                 ret = RECORDER_ERROR_INVALID_OPERATION;
1411                 goto _ERR_RECORDER_EXIT;
1412         }
1413
1414         if (type == MUSE_RECORDER_TYPE_AUDIO) {
1415                 send_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_API_CREATE,
1416                         MUSE_TYPE_INT, "module", MUSE_RECORDER,
1417                         MUSE_TYPE_INT, PARAM_RECORDER_TYPE, MUSE_RECORDER_TYPE_AUDIO,
1418                         MUSE_TYPE_INT, "pid", getpid(),
1419                         NULL);
1420         } else {
1421                 pc->camera = camera;
1422                 camera_handle = (intptr_t)((camera_cli_s *)camera)->remote_handle;
1423                 send_msg = muse_core_msg_json_factory_new(MUSE_RECORDER_API_CREATE,
1424                         MUSE_TYPE_INT, "module", MUSE_RECORDER,
1425                         MUSE_TYPE_INT, PARAM_RECORDER_TYPE, MUSE_RECORDER_TYPE_VIDEO,
1426                         MUSE_TYPE_POINTER, "camera_handle", camera_handle,
1427                         NULL);
1428         }
1429
1430         if (!send_msg) {
1431                 LOGE("NULL msg");
1432                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
1433                 goto _ERR_RECORDER_EXIT;
1434         }
1435
1436         LOGD("sock_fd : %d, msg : %s", sock_fd, send_msg);
1437
1438         send_ret = muse_core_ipc_send_msg(sock_fd, send_msg);
1439
1440         muse_core_msg_json_factory_free(send_msg);
1441         send_msg = NULL;
1442
1443         if (send_ret < 0) {
1444                 LOGE("send msg failed %d", errno);
1445                 ret = RECORDER_ERROR_INVALID_OPERATION;
1446                 goto _ERR_RECORDER_EXIT;
1447         }
1448
1449         pc->cb_info = _recorder_client_callback_new(sock_fd, true);
1450         if (pc->cb_info == NULL) {
1451                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
1452                 goto _ERR_RECORDER_EXIT;
1453         }
1454
1455         sock_fd = -1;
1456
1457         ret = _recorder_client_wait_for_cb_return(MUSE_RECORDER_API_CREATE, pc->cb_info, RECORDER_CB_TIMEOUT);
1458         if (ret != RECORDER_ERROR_NONE) {
1459                 LOGE("API_CREATE failed 0x%x", ret);
1460                 goto _ERR_RECORDER_EXIT;
1461         }
1462
1463         muse_recorder_msg_get_pointer(handle, pc->cb_info->recv_msg);
1464         if (handle == 0) {
1465                 LOGE("Receiving Handle Failed!!");
1466                 ret = RECORDER_ERROR_INVALID_OPERATION;
1467                 goto _ERR_RECORDER_AFTER_CREATE;
1468         }
1469
1470         if (!_recorder_client_get_root_directory(&root_directory) || root_directory == NULL) {
1471                 LOGE("failed to get root directory of internal storage");
1472                 ret = RECORDER_ERROR_INVALID_OPERATION;
1473                 goto _ERR_RECORDER_AFTER_CREATE;
1474         }
1475
1476         LOGD("root directory [%s]", root_directory);
1477
1478         RECORDER_MSG_PARAM_SET(param, STRING, root_directory);
1479
1480         _recorder_msg_send_param1(MUSE_RECORDER_API_ATTR_SET_ROOT_DIRECTORY, pc->cb_info, &ret, &param);
1481
1482         if (ret != RECORDER_ERROR_NONE) {
1483                 LOGE("failed to set root directory %s", root_directory);
1484                 ret = RECORDER_ERROR_INVALID_OPERATION;
1485                 goto _ERR_RECORDER_AFTER_CREATE;
1486         }
1487
1488         free(root_directory);
1489         root_directory = NULL;
1490
1491         pc->remote_handle = handle;
1492         pc->cb_info->bufmgr = bufmgr;
1493
1494         LOGD("recorder[type %d] %p create success : remote handle 0x%x",
1495                 type, pc, pc->remote_handle);
1496
1497         *recorder = (recorder_h)pc;
1498
1499         LOGD("done");
1500
1501         return RECORDER_ERROR_NONE;
1502
1503 _ERR_RECORDER_AFTER_CREATE:
1504         _recorder_msg_send(MUSE_RECORDER_API_DESTROY, pc->cb_info, &destroy_ret);
1505         LOGE("destroy return 0x%x", destroy_ret);
1506
1507 _ERR_RECORDER_EXIT:
1508         tbm_bufmgr_deinit(bufmgr);
1509         bufmgr = NULL;
1510
1511         if (root_directory) {
1512                 free(root_directory);
1513                 root_directory = NULL;
1514         }
1515
1516         if (sock_fd > -1) {
1517                 muse_core_connection_close(sock_fd);
1518                 sock_fd = -1;
1519         }
1520
1521         if (pc) {
1522                 if (pc->cb_info) {
1523                         _recorder_client_callback_destroy(pc->cb_info);
1524                         pc->cb_info = NULL;
1525                 }
1526                 g_free(pc);
1527                 pc = NULL;
1528         }
1529
1530         return ret;
1531 }
1532
1533
1534 int recorder_create_videorecorder(camera_h camera, recorder_h *recorder)
1535 {
1536         return _recorder_create_common(recorder, MUSE_RECORDER_TYPE_VIDEO, camera);
1537 }
1538
1539
1540 int recorder_create_audiorecorder(recorder_h *recorder)
1541 {
1542         return _recorder_create_common(recorder, MUSE_RECORDER_TYPE_AUDIO, NULL);
1543 }
1544
1545
1546 int recorder_get_state(recorder_h recorder, recorder_state_e *state)
1547 {
1548         int ret = RECORDER_ERROR_NONE;
1549         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1550         muse_recorder_api_e api = MUSE_RECORDER_API_GET_STATE;
1551
1552         if (!pc || !pc->cb_info) {
1553                 LOGE("NULL handle");
1554                 return RECORDER_ERROR_INVALID_PARAMETER;
1555         }
1556
1557         if (state == NULL) {
1558                 LOGE("NULL pointer state");
1559                 return RECORDER_ERROR_INVALID_PARAMETER;
1560         }
1561
1562         LOGD("Enter, remote_handle : %x", pc->remote_handle);
1563
1564         _recorder_msg_send(api, pc->cb_info, &ret);
1565
1566         if (ret == RECORDER_ERROR_NONE)
1567                 *state = (recorder_state_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_STATE];
1568
1569         LOGD("ret : 0x%x, state : %d", ret, *state);
1570
1571         return ret;
1572 }
1573
1574
1575 int recorder_destroy(recorder_h recorder)
1576 {
1577         int ret = RECORDER_ERROR_NONE;
1578         muse_recorder_api_e api = MUSE_RECORDER_API_DESTROY;
1579         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1580
1581         if (!pc || !pc->cb_info) {
1582                 LOGE("NULL handle");
1583                 return RECORDER_ERROR_INVALID_PARAMETER;
1584         }
1585
1586         LOGD("ENTER");
1587
1588         if (pc->cb_info->is_server_connected)
1589                 _recorder_msg_send(api, pc->cb_info, &ret);
1590         else
1591                 LOGW("server disconnected. release resource without send message.");
1592
1593         if (ret == RECORDER_ERROR_NONE) {
1594                 _recorder_remove_idle_event_all(pc->cb_info);
1595                 _recorder_client_callback_destroy(pc->cb_info);
1596                 g_free(pc);
1597                 pc = NULL;
1598         }
1599
1600         LOGD("ret : 0x%x", ret);
1601
1602         return ret;
1603 }
1604
1605
1606 int recorder_prepare(recorder_h recorder)
1607 {
1608         int ret = RECORDER_ERROR_NONE;
1609         muse_recorder_api_e api = MUSE_RECORDER_API_PREPARE;
1610         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1611
1612         if (!pc || !pc->cb_info) {
1613                 LOGE("NULL handle");
1614                 return RECORDER_ERROR_INVALID_PARAMETER;
1615         }
1616
1617         LOGD("ENTER");
1618
1619         _recorder_msg_send(api, pc->cb_info, &ret);
1620
1621         LOGD("ret : 0x%x", ret);
1622
1623         if (ret == RECORDER_ERROR_NONE && pc->camera)
1624                 camera_start_evas_rendering(pc->camera);
1625
1626         return ret;
1627 }
1628
1629
1630 int recorder_unprepare(recorder_h recorder)
1631 {
1632         int ret = RECORDER_ERROR_NONE;
1633         muse_recorder_api_e api = MUSE_RECORDER_API_UNPREPARE;
1634         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1635         camera_state_e camera_state = CAMERA_STATE_NONE;
1636
1637         if (!pc || !pc->cb_info) {
1638                 LOGE("NULL handle");
1639                 return RECORDER_ERROR_INVALID_PARAMETER;
1640         }
1641
1642         LOGD("ENTER");
1643
1644         if (pc->camera) {
1645                 ret = camera_get_state(pc->camera, &camera_state);
1646                 if (ret != CAMERA_ERROR_NONE) {
1647                         LOGE("failed to get camera state 0x%x", ret);
1648                         return RECORDER_ERROR_INVALID_OPERATION;
1649                 }
1650
1651                 if (camera_state == CAMERA_STATE_PREVIEW) {
1652                         ret = camera_stop_evas_rendering(pc->camera, false);
1653                         if (ret != CAMERA_ERROR_NONE) {
1654                                 LOGE("camera_stop_evas_rendering failed 0x%x", ret);
1655                                 return RECORDER_ERROR_INVALID_OPERATION;
1656                         }
1657                 }
1658         }
1659
1660         _recorder_msg_send(api, pc->cb_info, &ret);
1661
1662         LOGD("ret : 0x%x", ret);
1663
1664         return ret;
1665 }
1666
1667
1668 int recorder_start(recorder_h recorder)
1669 {
1670         int ret = RECORDER_ERROR_NONE;
1671         muse_recorder_api_e api = MUSE_RECORDER_API_START;
1672         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1673         recorder_state_e current_state = RECORDER_STATE_NONE;
1674
1675         if (!pc || !pc->cb_info) {
1676                 LOGE("NULL handle");
1677                 return RECORDER_ERROR_INVALID_PARAMETER;
1678         }
1679
1680         LOGD("ENTER");
1681
1682         if (pc->camera) {
1683                 ret = recorder_get_state(recorder, &current_state);
1684                 if (ret != RECORDER_ERROR_NONE) {
1685                         LOGE("failed to get current state 0x%x", ret);
1686                         return RECORDER_ERROR_INVALID_OPERATION;
1687                 }
1688
1689                 if (current_state == RECORDER_STATE_READY) {
1690                         ret = camera_stop_evas_rendering(pc->camera, true);
1691                         if (ret != CAMERA_ERROR_NONE) {
1692                                 LOGE("camera_stop_evas_rendering failed 0x%x", ret);
1693                                 return RECORDER_ERROR_INVALID_OPERATION;
1694                         }
1695                 }
1696         }
1697
1698         _recorder_msg_send(api, pc->cb_info, &ret);
1699
1700         if (pc->camera && current_state == RECORDER_STATE_READY)
1701                 camera_start_evas_rendering(pc->camera);
1702
1703         LOGD("ret : 0x%x", ret);
1704
1705         return ret;
1706 }
1707
1708
1709 int recorder_pause(recorder_h recorder)
1710 {
1711         int ret = RECORDER_ERROR_NONE;
1712         muse_recorder_api_e api = MUSE_RECORDER_API_PAUSE;
1713         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1714
1715         if (!pc || !pc->cb_info) {
1716                 LOGE("NULL handle");
1717                 return RECORDER_ERROR_INVALID_PARAMETER;
1718         }
1719
1720         LOGD("ENTER");
1721
1722         _recorder_msg_send(api, pc->cb_info, &ret);
1723
1724         LOGD("ret : 0x%x", ret);
1725
1726         return ret;
1727 }
1728
1729
1730 int recorder_commit(recorder_h recorder)
1731 {
1732         int ret = RECORDER_ERROR_NONE;
1733         muse_recorder_api_e api = MUSE_RECORDER_API_COMMIT;
1734         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1735         recorder_state_e current_state = RECORDER_STATE_NONE;
1736
1737         if (!pc || !pc->cb_info) {
1738                 LOGE("NULL handle");
1739                 return RECORDER_ERROR_INVALID_PARAMETER;
1740         }
1741
1742         LOGD("ENTER");
1743
1744         if (pc->camera) {
1745                 ret = recorder_get_state(recorder, &current_state);
1746                 if (ret != RECORDER_ERROR_NONE) {
1747                         LOGE("failed to get current state 0x%x", ret);
1748                         return RECORDER_ERROR_INVALID_OPERATION;
1749                 }
1750
1751                 if (current_state >= RECORDER_STATE_RECORDING) {
1752                         ret = camera_stop_evas_rendering(pc->camera, true);
1753                         if (ret != CAMERA_ERROR_NONE) {
1754                                 LOGE("camera_stop_evas_rendering failed 0x%x", ret);
1755                                 return RECORDER_ERROR_INVALID_OPERATION;
1756                         }
1757                 }
1758         }
1759
1760         _recorder_msg_send(api, pc->cb_info, &ret);
1761
1762         if (pc->camera && current_state >= RECORDER_STATE_RECORDING)
1763                 camera_start_evas_rendering(pc->camera);
1764
1765         LOGD("ret : 0x%x", ret);
1766
1767         return ret;
1768 }
1769
1770
1771 int recorder_cancel(recorder_h recorder)
1772 {
1773         int ret = RECORDER_ERROR_NONE;
1774         muse_recorder_api_e api = MUSE_RECORDER_API_CANCEL;
1775         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1776         recorder_state_e current_state = RECORDER_STATE_NONE;
1777
1778         if (!pc || !pc->cb_info) {
1779                 LOGE("NULL handle");
1780                 return RECORDER_ERROR_INVALID_PARAMETER;
1781         }
1782
1783         LOGD("ENTER");
1784
1785         if (pc->camera) {
1786                 ret = recorder_get_state(recorder, &current_state);
1787                 if (ret != RECORDER_ERROR_NONE) {
1788                         LOGE("failed to get current state 0x%x", ret);
1789                         return RECORDER_ERROR_INVALID_OPERATION;
1790                 }
1791
1792                 if (current_state >= RECORDER_STATE_RECORDING) {
1793                         ret = camera_stop_evas_rendering(pc->camera, true);
1794                         if (ret != CAMERA_ERROR_NONE) {
1795                                 LOGE("camera_stop_evas_rendering failed 0x%x", ret);
1796                                 return RECORDER_ERROR_INVALID_OPERATION;
1797                         }
1798                 }
1799         }
1800
1801         _recorder_msg_send(api, pc->cb_info, &ret);
1802
1803         if (pc->camera && current_state >= RECORDER_STATE_RECORDING)
1804                 camera_start_evas_rendering(pc->camera);
1805
1806         LOGD("ret : 0x%x", ret);
1807
1808         return ret;
1809 }
1810
1811
1812 int recorder_set_video_resolution(recorder_h recorder, int width, int height)
1813 {
1814         int ret = RECORDER_ERROR_NONE;
1815         int send_ret = 0;
1816         char *send_msg = NULL;
1817         muse_recorder_api_e api = MUSE_RECORDER_API_SET_VIDEO_RESOLUTION;
1818         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1819
1820         if (!pc || !pc->cb_info) {
1821                 LOGE("NULL handle");
1822                 return RECORDER_ERROR_INVALID_PARAMETER;
1823         }
1824
1825         LOGD("ENTER");
1826
1827         send_msg = muse_core_msg_json_factory_new(api,
1828                 MUSE_TYPE_INT, "width", width,
1829                 MUSE_TYPE_INT, "height", height,
1830                 NULL);
1831         if (send_msg) {
1832                 if (pc->cb_info->is_server_connected)
1833                         send_ret = muse_core_ipc_send_msg(pc->cb_info->fd, send_msg);
1834
1835                 if (send_ret < 0) {
1836                         LOGE("message send failed");
1837                         ret = RECORDER_ERROR_INVALID_OPERATION;
1838                 } else {
1839                         ret = _recorder_client_wait_for_cb_return(api, pc->cb_info, RECORDER_CB_TIMEOUT);
1840                 }
1841
1842                 muse_core_msg_json_factory_free(send_msg);
1843         } else {
1844                 LOGE("failed to create msg");
1845                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
1846         }
1847
1848         LOGD("ret : 0x%x", ret);
1849
1850         return ret;
1851 }
1852
1853
1854 int recorder_get_video_resolution(recorder_h recorder, int *width, int *height)
1855 {
1856         int ret = RECORDER_ERROR_NONE;
1857         muse_recorder_api_e api = MUSE_RECORDER_API_GET_VIDEO_RESOLUTION;
1858         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1859
1860         if (!pc || !pc->cb_info) {
1861                 LOGE("NULL handle");
1862                 return RECORDER_ERROR_INVALID_PARAMETER;
1863         }
1864
1865         if (!width || !height) {
1866                 LOGE("NULL pointer width = [%p], height = [%p]", width, height);
1867                 return RECORDER_ERROR_INVALID_PARAMETER;
1868         }
1869
1870         LOGD("ENTER");
1871
1872         _recorder_msg_send(api, pc->cb_info, &ret);
1873
1874         if (ret == RECORDER_ERROR_NONE) {
1875                 *width = pc->cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_RESOLUTION] >> 16;
1876                 *height = (0x0000ffff & pc->cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_RESOLUTION]);
1877         }
1878
1879         LOGD("ret : 0x%x, %dx%d", ret, *width, *height);
1880
1881         return ret;
1882 }
1883
1884
1885 int recorder_foreach_supported_video_resolution(recorder_h recorder,
1886         recorder_supported_video_resolution_cb foreach_cb, void *user_data)
1887 {
1888         int ret = RECORDER_ERROR_NONE;
1889         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1890         muse_recorder_api_e api = MUSE_RECORDER_API_FOREACH_SUPPORTED_VIDEO_RESOLUTION;
1891
1892         if (!pc || !pc->cb_info || foreach_cb == NULL) {
1893                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
1894                 return RECORDER_ERROR_INVALID_PARAMETER;
1895         }
1896
1897         LOGD("Enter, handle :%x", pc->remote_handle);
1898
1899         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_RESOLUTION] = foreach_cb;
1900         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_RESOLUTION] = user_data;
1901
1902         _recorder_msg_send(api, pc->cb_info, &ret);
1903
1904         LOGD("ret : 0x%x", ret);
1905
1906         return ret;
1907 }
1908
1909
1910 int recorder_get_audio_level(recorder_h recorder, double *level)
1911 {
1912         int ret = RECORDER_ERROR_NONE;
1913         muse_recorder_api_e api = MUSE_RECORDER_API_GET_AUDIO_LEVEL;
1914         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1915
1916         if (!pc || !pc->cb_info || level == NULL) {
1917                 LOGE("NULL pointer %p %p", pc, level);
1918                 return RECORDER_ERROR_INVALID_PARAMETER;
1919         }
1920
1921         LOGD("ENTER");
1922
1923         _recorder_msg_send(api, pc->cb_info, &ret);
1924
1925         if (ret == RECORDER_ERROR_NONE)
1926                 *level = pc->cb_info->get_double_value[_RECORDER_GET_DOUBLE_AUDIO_LEVEL];
1927
1928         LOGD("ret : 0x%x, level %lf", ret, *level);
1929
1930         return ret;
1931 }
1932
1933
1934 int recorder_set_filename(recorder_h recorder,  const char *filename)
1935 {
1936         int ret = RECORDER_ERROR_NONE;
1937         muse_recorder_api_e api = MUSE_RECORDER_API_SET_FILENAME;
1938         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1939         recorder_msg_param param;
1940         char set_filename[RECORDER_FILENAME_MAX] = {0, };
1941
1942         if (!pc || !pc->cb_info) {
1943                 LOGE("NULL handle");
1944                 return RECORDER_ERROR_INVALID_PARAMETER;
1945         }
1946
1947         if (filename == NULL) {
1948                 LOGE("filename is NULL");
1949                 return RECORDER_ERROR_INVALID_PARAMETER;
1950         }
1951
1952         LOGD("ENTER");
1953
1954         if (storage_get_origin_internal_path(filename, RECORDER_FILENAME_MAX, set_filename) < 0) {
1955                 /* Cannot convert. Use original path. */
1956                 strncpy(set_filename, filename, strlen(filename));
1957         } else {
1958                 /* Converted. Use converted path. */
1959                 LOGD("Converted filename : %s -> %s", filename, set_filename);
1960         }
1961
1962         RECORDER_MSG_PARAM_SET(param, STRING, set_filename);
1963
1964         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
1965
1966         LOGD("ret : 0x%x", ret);
1967
1968         return ret;
1969 }
1970
1971
1972 int recorder_get_filename(recorder_h recorder,  char **filename)
1973 {
1974         int ret = RECORDER_ERROR_NONE;
1975         muse_recorder_api_e api = MUSE_RECORDER_API_GET_FILENAME;
1976         recorder_cli_s *pc = (recorder_cli_s *)recorder;
1977         char compat_filename[RECORDER_FILENAME_MAX] = {0, };
1978
1979         if (!pc || !pc->cb_info) {
1980                 LOGE("NULL handle");
1981                 return RECORDER_ERROR_INVALID_PARAMETER;
1982         }
1983
1984         if (filename == NULL) {
1985                 LOGE("filename is NULL");
1986                 return RECORDER_ERROR_INVALID_PARAMETER;
1987         }
1988
1989         LOGD("ENTER");
1990
1991         _recorder_msg_send(api, pc->cb_info, &ret);
1992
1993         if (ret == RECORDER_ERROR_NONE) {
1994                 if (storage_get_compat_internal_path(pc->cb_info->get_filename, RECORDER_FILENAME_MAX, compat_filename) < 0) {
1995                         /* Cannot convert. Use original path. */
1996                         *filename = pc->cb_info->get_filename;
1997                 } else {
1998                         /* Converted. Use converted path. */
1999                         LOGD("Converted filename : %s -> %s", pc->cb_info->get_filename, compat_filename);
2000                         *filename = strdup(compat_filename);
2001                         free(pc->cb_info->get_filename);
2002                 }
2003
2004                 pc->cb_info->get_filename = NULL;
2005         }
2006
2007         LOGD("ret : 0x%x, filename : [%s]", ret, (*filename) ? *filename : "NULL");
2008
2009         return ret;
2010 }
2011
2012
2013 int recorder_set_file_format(recorder_h recorder, recorder_file_format_e format)
2014 {
2015         int ret = RECORDER_ERROR_NONE;
2016         int set_format = (int)format;
2017         muse_recorder_api_e api = MUSE_RECORDER_API_SET_FILE_FORMAT;
2018         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2019         recorder_msg_param param;
2020
2021         if (!pc || !pc->cb_info) {
2022                 LOGE("NULL handle");
2023                 return RECORDER_ERROR_INVALID_PARAMETER;
2024         }
2025
2026         LOGD("ENTER, set_format : %d", set_format);
2027
2028         RECORDER_MSG_PARAM_SET(param, INT, set_format);
2029
2030         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2031
2032         LOGD("ret : 0x%x", ret);
2033         return ret;
2034 }
2035
2036
2037 int recorder_get_file_format(recorder_h recorder, recorder_file_format_e *format)
2038 {
2039         int ret = RECORDER_ERROR_NONE;
2040         muse_recorder_api_e api = MUSE_RECORDER_API_GET_FILE_FORMAT;
2041         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2042
2043         if (!pc || !pc->cb_info) {
2044                 LOGE("NULL handle");
2045                 return RECORDER_ERROR_INVALID_PARAMETER;
2046         }
2047
2048         if (format == NULL) {
2049                 LOGE("NULL pointer data");
2050                 return RECORDER_ERROR_INVALID_PARAMETER;
2051         }
2052
2053         LOGD("ENTER");
2054
2055         _recorder_msg_send(api, pc->cb_info, &ret);
2056
2057         if (ret == RECORDER_ERROR_NONE)
2058                 *format = (recorder_file_format_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_FILE_FORMAT];
2059
2060         LOGD("ret : 0x%x, format %d", ret, *format);
2061
2062         return ret;
2063 }
2064
2065
2066 int recorder_set_sound_stream_info(recorder_h recorder, sound_stream_info_h stream_info)
2067 {
2068         int ret = RECORDER_ERROR_NONE;
2069         muse_recorder_api_e api = MUSE_RECORDER_API_SET_SOUND_STREAM_INFO;
2070         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2071         bool is_available = false;
2072         int stream_index = 0;
2073         char *stream_type = NULL;
2074         char *send_msg = NULL;
2075         int send_ret = 0;
2076
2077         if (!pc || !pc->cb_info || stream_info == NULL) {
2078                 LOGE("NULL handle");
2079                 return RECORDER_ERROR_INVALID_PARAMETER;
2080         }
2081
2082         LOGD("ENTER");
2083
2084         ret = sound_manager_is_available_stream_information(stream_info, NATIVE_API_RECORDER, &is_available);
2085         if (ret != SOUND_MANAGER_ERROR_NONE) {
2086                 LOGE("stream info verification failed");
2087                 return RECORDER_ERROR_INVALID_OPERATION;
2088         }
2089
2090         if (is_available == false) {
2091                 LOGE("stream information is not available");
2092                 return RECORDER_ERROR_INVALID_OPERATION;
2093         }
2094
2095         ret = sound_manager_get_type_from_stream_information(stream_info, &stream_type);
2096         ret |= sound_manager_get_index_from_stream_information(stream_info, &stream_index);
2097
2098         LOGD("sound manager return [0x%x]", ret);
2099
2100         if (ret == SOUND_MANAGER_ERROR_NONE) {
2101                 send_msg = muse_core_msg_json_factory_new(api,
2102                         MUSE_TYPE_STRING, "stream_type", stream_type,
2103                         MUSE_TYPE_INT, "stream_index", stream_index,
2104                         NULL);
2105                 if (send_msg) {
2106                         if (pc->cb_info->is_server_connected)
2107                                 send_ret = muse_core_ipc_send_msg(pc->cb_info->fd, send_msg);
2108
2109                         if (send_ret < 0) {
2110                                 LOGE("message send failed");
2111                                 ret = RECORDER_ERROR_INVALID_OPERATION;
2112                         } else {
2113                                 ret = _recorder_client_wait_for_cb_return(api, pc->cb_info, RECORDER_CB_TIMEOUT);
2114                         }
2115
2116                         muse_core_msg_json_factory_free(send_msg);
2117                 } else {
2118                         LOGE("failed to create msg");
2119                         ret = RECORDER_ERROR_OUT_OF_MEMORY;
2120                 }
2121         } else {
2122                 ret = RECORDER_ERROR_INVALID_OPERATION;
2123         }
2124
2125         return ret;
2126 }
2127
2128
2129 int recorder_set_state_changed_cb(recorder_h recorder, recorder_state_changed_cb callback, void* user_data)
2130 {
2131         int ret = RECORDER_ERROR_NONE;
2132         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2133         muse_recorder_api_e api = MUSE_RECORDER_API_SET_STATE_CHANGED_CB;
2134
2135         if (!pc || !pc->cb_info || callback == NULL) {
2136                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2137                 return RECORDER_ERROR_INVALID_PARAMETER;
2138         }
2139
2140         LOGD("Enter, handle :%x", pc->remote_handle);
2141
2142         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_STATE_CHANGE] = callback;
2143         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_STATE_CHANGE] = user_data;
2144
2145         _recorder_msg_send(api, pc->cb_info, &ret);
2146
2147         LOGD("ret : 0x%x", ret);
2148
2149         return ret;
2150 }
2151
2152
2153 int recorder_unset_state_changed_cb(recorder_h recorder)
2154 {
2155         int ret = RECORDER_ERROR_NONE;
2156         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_STATE_CHANGED_CB;
2157         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2158
2159         if (!pc || !pc->cb_info) {
2160                 LOGE("NULL handle");
2161                 return RECORDER_ERROR_INVALID_PARAMETER;
2162         }
2163
2164         LOGD("ENTER");
2165
2166         _recorder_msg_send(api, pc->cb_info, &ret);
2167
2168         LOGD("ret : 0x%x", ret);
2169
2170         return ret;
2171 }
2172
2173
2174 int recorder_set_interrupted_cb(recorder_h recorder, recorder_interrupted_cb callback, void *user_data)
2175 {
2176         int ret = RECORDER_ERROR_NONE;
2177         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2178         muse_recorder_api_e api = MUSE_RECORDER_API_SET_INTERRUPTED_CB;
2179
2180         if (!pc || !pc->cb_info || callback == NULL) {
2181                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2182                 return RECORDER_ERROR_INVALID_PARAMETER;
2183         }
2184
2185         LOGD("Enter, handle :%x", pc->remote_handle);
2186
2187         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_INTERRUPTED] = callback;
2188         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_INTERRUPTED] = user_data;
2189
2190         _recorder_msg_send(api, pc->cb_info, &ret);
2191
2192         LOGD("ret : 0x%x", ret);
2193
2194         return ret;
2195 }
2196
2197
2198 int recorder_unset_interrupted_cb(recorder_h recorder)
2199 {
2200         int ret = RECORDER_ERROR_NONE;
2201         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_INTERRUPTED_CB;
2202         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2203
2204         if (!pc || !pc->cb_info) {
2205                 LOGE("NULL handle");
2206                 return RECORDER_ERROR_INVALID_PARAMETER;
2207         }
2208
2209         LOGD("ENTER");
2210
2211         _recorder_msg_send(api, pc->cb_info, &ret);
2212
2213         LOGD("ret : 0x%x", ret);
2214
2215         return ret;
2216 }
2217
2218
2219 int recorder_set_audio_stream_cb(recorder_h recorder, recorder_audio_stream_cb callback, void* user_data)
2220 {
2221         int ret = RECORDER_ERROR_NONE;
2222         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2223         muse_recorder_api_e api = MUSE_RECORDER_API_SET_AUDIO_STREAM_CB;
2224
2225         if (!pc || !pc->cb_info || callback == NULL) {
2226                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2227                 return RECORDER_ERROR_INVALID_PARAMETER;
2228         }
2229
2230         LOGD("Enter, handle :%x", pc->remote_handle);
2231
2232         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM] = callback;
2233         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_AUDIO_STREAM] = user_data;
2234
2235         _recorder_msg_send(api, pc->cb_info, &ret);
2236
2237         LOGD("ret : 0x%x", ret);
2238
2239         return ret;
2240 }
2241
2242
2243 int recorder_unset_audio_stream_cb(recorder_h recorder)
2244 {
2245         int ret = RECORDER_ERROR_NONE;
2246         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_AUDIO_STREAM_CB;
2247         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2248
2249         if (!pc || !pc->cb_info) {
2250                 LOGE("NULL handle");
2251                 return RECORDER_ERROR_INVALID_PARAMETER;
2252         }
2253
2254         LOGD("ENTER");
2255
2256         _recorder_msg_send(api, pc->cb_info, &ret);
2257
2258         LOGD("ret : 0x%x", ret);
2259
2260         return ret;
2261 }
2262
2263
2264 int recorder_set_error_cb(recorder_h recorder, recorder_error_cb callback, void *user_data)
2265 {
2266         int ret = RECORDER_ERROR_NONE;
2267         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2268         muse_recorder_api_e api = MUSE_RECORDER_API_SET_ERROR_CB;
2269
2270         if (!pc || !pc->cb_info || callback == NULL) {
2271                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2272                 return RECORDER_ERROR_INVALID_PARAMETER;
2273         }
2274
2275         LOGD("Enter, handle :%x", pc->remote_handle);
2276
2277         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_ERROR] = callback;
2278         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_ERROR] = user_data;
2279
2280         _recorder_msg_send(api, pc->cb_info, &ret);
2281
2282         LOGD("ret : 0x%x", ret);
2283
2284         return ret;
2285 }
2286
2287
2288 int recorder_unset_error_cb(recorder_h recorder)
2289 {
2290         int ret = RECORDER_ERROR_NONE;
2291         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_ERROR_CB;
2292         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2293
2294         if (!pc || !pc->cb_info) {
2295                 LOGE("NULL handle");
2296                 return RECORDER_ERROR_INVALID_PARAMETER;
2297         }
2298
2299         LOGD("ENTER");
2300
2301         _recorder_msg_send(api, pc->cb_info, &ret);
2302
2303         LOGD("ret : 0x%x", ret);
2304
2305         return ret;
2306 }
2307
2308
2309 int recorder_set_recording_status_cb(recorder_h recorder, recorder_recording_status_cb callback, void* user_data)
2310 {
2311         int ret = RECORDER_ERROR_NONE;
2312         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2313         muse_recorder_api_e api = MUSE_RECORDER_API_SET_RECORDING_STATUS_CB;
2314
2315         if (!pc || !pc->cb_info || callback == NULL) {
2316                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2317                 return RECORDER_ERROR_INVALID_PARAMETER;
2318         }
2319
2320         LOGD("Enter, handle :%x", pc->remote_handle);
2321
2322         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_RECORDING_STATUS] = callback;
2323         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_RECORDING_STATUS] = user_data;
2324
2325         _recorder_msg_send(api, pc->cb_info, &ret);
2326
2327         LOGD("ret : 0x%x", ret);
2328
2329         return ret;
2330 }
2331
2332
2333 int recorder_unset_recording_status_cb(recorder_h recorder)
2334 {
2335         int ret = RECORDER_ERROR_NONE;
2336         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_RECORDING_STATUS_CB;
2337         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2338
2339         if (!pc || !pc->cb_info) {
2340                 LOGE("NULL handle");
2341                 return RECORDER_ERROR_INVALID_PARAMETER;
2342         }
2343
2344         LOGD("ENTER");
2345
2346         _recorder_msg_send(api, pc->cb_info, &ret);
2347
2348         LOGD("ret : 0x%x", ret);
2349
2350         return ret;
2351 }
2352
2353
2354 int recorder_set_recording_limit_reached_cb(recorder_h recorder, recorder_recording_limit_reached_cb callback, void* user_data)
2355 {
2356         int ret = RECORDER_ERROR_NONE;
2357         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2358         muse_recorder_api_e api = MUSE_RECORDER_API_SET_RECORDING_LIMIT_REACHED_CB;
2359
2360         if (!pc || !pc->cb_info || callback == NULL) {
2361                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2362                 return RECORDER_ERROR_INVALID_PARAMETER;
2363         }
2364
2365         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_RECORDING_LIMITED] = callback;
2366         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_RECORDING_LIMITED] = user_data;
2367
2368         _recorder_msg_send(api, pc->cb_info, &ret);
2369
2370         LOGD("ret : 0x%x", ret);
2371
2372         return ret;
2373 }
2374
2375
2376 int recorder_unset_recording_limit_reached_cb(recorder_h recorder)
2377 {
2378         int ret = RECORDER_ERROR_NONE;
2379         muse_recorder_api_e api = MUSE_RECORDER_API_UNSET_RECORDING_LIMIT_REACHED_CB;
2380         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2381
2382         if (!pc || !pc->cb_info) {
2383                 LOGE("NULL handle");
2384                 return RECORDER_ERROR_INVALID_PARAMETER;
2385         }
2386
2387         LOGD("ENTER");
2388
2389         _recorder_msg_send(api, pc->cb_info, &ret);
2390
2391         LOGD("ret : 0x%x", ret);
2392
2393         return ret;
2394 }
2395
2396
2397 int recorder_foreach_supported_file_format(recorder_h recorder, recorder_supported_file_format_cb foreach_cb, void *user_data)
2398 {
2399         int ret = RECORDER_ERROR_NONE;
2400         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2401         muse_recorder_api_e api = MUSE_RECORDER_API_FOREACH_SUPPORTED_FILE_FORMAT;
2402
2403         if (!pc || !pc->cb_info || foreach_cb == NULL) {
2404                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2405                 return RECORDER_ERROR_INVALID_PARAMETER;
2406         }
2407
2408         LOGD("Enter, handle :%x", pc->remote_handle);
2409
2410         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_FILE_FORMAT] = foreach_cb;
2411         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_FILE_FORMAT] = user_data;
2412
2413         _recorder_msg_send(api, pc->cb_info, &ret);
2414
2415         LOGD("ret : 0x%x", ret);
2416
2417         return ret;
2418 }
2419
2420
2421 int recorder_attr_set_size_limit(recorder_h recorder, int kbyte)
2422 {
2423         int ret = RECORDER_ERROR_NONE;
2424         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_SIZE_LIMIT;
2425         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2426         recorder_msg_param param;
2427
2428         if (!pc || !pc->cb_info) {
2429                 LOGE("NULL handle");
2430                 return RECORDER_ERROR_INVALID_PARAMETER;
2431         }
2432
2433         LOGD("ENTER");
2434
2435         RECORDER_MSG_PARAM_SET(param, INT, kbyte);
2436
2437         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2438
2439         LOGD("ret : 0x%x", ret);
2440
2441         return ret;
2442 }
2443
2444
2445 int recorder_attr_set_time_limit(recorder_h recorder, int second)
2446 {
2447         int ret = RECORDER_ERROR_NONE;
2448         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_TIME_LIMIT;
2449         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2450         recorder_msg_param param;
2451
2452         if (!pc || !pc->cb_info) {
2453                 LOGE("NULL handle");
2454                 return RECORDER_ERROR_INVALID_PARAMETER;
2455         }
2456
2457         LOGD("ENTER");
2458
2459         RECORDER_MSG_PARAM_SET(param, INT, second);
2460
2461         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2462
2463         LOGD("ret : 0x%x", ret);
2464
2465         return ret;
2466 }
2467
2468
2469 int recorder_attr_set_audio_device(recorder_h recorder, recorder_audio_device_e device)
2470 {
2471         int ret = RECORDER_ERROR_NONE;
2472         int set_device = (int)device;
2473         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_AUDIO_DEVICE;
2474         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2475         recorder_msg_param param;
2476
2477         if (!pc || !pc->cb_info) {
2478                 LOGE("NULL handle");
2479                 return RECORDER_ERROR_INVALID_PARAMETER;
2480         };
2481
2482         LOGD("ENTER");
2483
2484         RECORDER_MSG_PARAM_SET(param, INT, set_device);
2485
2486         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2487
2488         LOGD("ret : 0x%x", ret);
2489
2490         return ret;
2491 }
2492
2493
2494 int recorder_set_audio_encoder(recorder_h recorder, recorder_audio_codec_e codec)
2495 {
2496         int ret = RECORDER_ERROR_NONE;
2497         int set_codec = (int)codec;
2498         muse_recorder_api_e api = MUSE_RECORDER_API_SET_AUDIO_ENCODER;
2499         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2500         recorder_msg_param param;
2501
2502         if (!pc || !pc->cb_info) {
2503                 LOGE("NULL handle");
2504                 return RECORDER_ERROR_INVALID_PARAMETER;
2505         }
2506
2507         LOGD("ENTER");
2508
2509         RECORDER_MSG_PARAM_SET(param, INT, set_codec);
2510
2511         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2512
2513         LOGD("ret : 0x%x", ret);
2514
2515         return ret;
2516 }
2517
2518
2519 int recorder_get_audio_encoder(recorder_h recorder, recorder_audio_codec_e *codec)
2520 {
2521         int ret = RECORDER_ERROR_NONE;
2522         muse_recorder_api_e api = MUSE_RECORDER_API_GET_AUDIO_ENCODER;
2523         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2524
2525         if (!pc || !pc->cb_info) {
2526                 LOGE("NULL handle");
2527                 return RECORDER_ERROR_INVALID_PARAMETER;
2528         }
2529
2530         if (codec == NULL) {
2531                 LOGE("codec is NULL");
2532                 return RECORDER_ERROR_INVALID_PARAMETER;
2533         }
2534
2535         LOGD("ENTER");
2536
2537         _recorder_msg_send(api, pc->cb_info, &ret);
2538
2539         if (ret == RECORDER_ERROR_NONE)
2540                 *codec = (recorder_audio_codec_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_ENCODER];
2541
2542         LOGD("ret : 0x%x, codec %d", ret, *codec);
2543
2544         return ret;
2545 }
2546
2547
2548 int recorder_set_video_encoder(recorder_h recorder, recorder_video_codec_e codec)
2549 {
2550         int ret = RECORDER_ERROR_NONE;
2551         int set_codec = (int)codec;
2552         muse_recorder_api_e api = MUSE_RECORDER_API_SET_VIDEO_ENCODER;
2553         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2554         recorder_msg_param param;
2555
2556         if (!pc || !pc->cb_info) {
2557                 LOGE("NULL handle");
2558                 return RECORDER_ERROR_INVALID_PARAMETER;
2559         }
2560
2561         LOGD("ENTER");
2562
2563         RECORDER_MSG_PARAM_SET(param, INT, set_codec);
2564
2565         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2566
2567         LOGD("ret : 0x%x", ret);
2568
2569         return ret;
2570 }
2571
2572
2573 int recorder_get_video_encoder(recorder_h recorder, recorder_video_codec_e *codec)
2574 {
2575         int ret = RECORDER_ERROR_NONE;
2576         muse_recorder_api_e api = MUSE_RECORDER_API_GET_VIDEO_ENCODER;
2577         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2578
2579         if (!pc || !pc->cb_info) {
2580                 LOGE("NULL handle");
2581                 return RECORDER_ERROR_INVALID_PARAMETER;
2582         }
2583
2584         if (codec == NULL) {
2585                 LOGE("codec is NULL");
2586                 return RECORDER_ERROR_INVALID_PARAMETER;
2587         }
2588
2589         LOGD("ENTER");
2590
2591         _recorder_msg_send(api, pc->cb_info, &ret);
2592
2593         if (ret == RECORDER_ERROR_NONE)
2594                 *codec = (recorder_video_codec_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_ENCODER];
2595
2596         LOGD("ret : 0x%x, codec %d", ret, *codec);
2597
2598         return ret;
2599 }
2600
2601
2602 int recorder_attr_set_audio_samplerate(recorder_h recorder, int samplerate)
2603 {
2604         int ret = RECORDER_ERROR_NONE;
2605         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_AUDIO_SAMPLERATE;
2606         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2607         recorder_msg_param param;
2608
2609         if (!pc || !pc->cb_info) {
2610                 LOGE("NULL handle");
2611                 return RECORDER_ERROR_INVALID_PARAMETER;
2612         }
2613
2614         LOGD("ENTER, samplerate : %d", samplerate);
2615
2616         RECORDER_MSG_PARAM_SET(param, INT, samplerate);
2617
2618         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2619
2620         LOGD("ret : 0x%x", ret);
2621
2622         return ret;
2623 }
2624
2625
2626 int recorder_attr_set_audio_encoder_bitrate(recorder_h recorder, int bitrate)
2627 {
2628         int ret = RECORDER_ERROR_NONE;
2629         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_AUDIO_ENCODER_BITRATE;
2630         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2631         recorder_msg_param param;
2632
2633         if (!pc || !pc->cb_info) {
2634                 LOGE("NULL handle");
2635                 return RECORDER_ERROR_INVALID_PARAMETER;
2636         }
2637
2638         LOGD("ENTER");
2639
2640         RECORDER_MSG_PARAM_SET(param, INT, bitrate);
2641
2642         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2643
2644         LOGD("ret : 0x%x", ret);
2645
2646         return ret;
2647 }
2648
2649
2650 int recorder_attr_set_video_encoder_bitrate(recorder_h recorder, int bitrate)
2651 {
2652         int ret = RECORDER_ERROR_NONE;
2653         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_VIDEO_ENCODER_BITRATE;
2654         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2655         recorder_msg_param param;
2656
2657         if (!pc || !pc->cb_info) {
2658                 LOGE("NULL handle");
2659                 return RECORDER_ERROR_INVALID_PARAMETER;
2660         }
2661
2662         LOGD("ENTER");
2663
2664         RECORDER_MSG_PARAM_SET(param, INT, bitrate);
2665
2666         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2667
2668         LOGD("ret : 0x%x", ret);
2669
2670         return ret;
2671 }
2672
2673
2674 int recorder_attr_get_size_limit(recorder_h recorder, int *kbyte)
2675 {
2676         int ret = RECORDER_ERROR_NONE;
2677         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_SIZE_LIMIT;
2678         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2679
2680         if (!pc || !pc->cb_info) {
2681                 LOGE("NULL handle");
2682                 return RECORDER_ERROR_INVALID_PARAMETER;
2683         }
2684
2685         if (kbyte == NULL) {
2686                 LOGE("NULL pointer kbyte");
2687                 return RECORDER_ERROR_INVALID_PARAMETER;
2688         }
2689
2690         LOGD("ENTER");
2691
2692         _recorder_msg_send(api, pc->cb_info, &ret);
2693
2694         if (ret == RECORDER_ERROR_NONE)
2695                 *kbyte = pc->cb_info->get_int_value[_RECORDER_GET_INT_SIZE_LIMIT];
2696
2697         LOGD("ret : 0x%x, %d kbyte", ret, *kbyte);
2698
2699         return ret;
2700 }
2701
2702
2703 int recorder_attr_get_time_limit(recorder_h recorder, int *second)
2704 {
2705         int ret = RECORDER_ERROR_NONE;
2706         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_TIME_LIMIT;
2707         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2708
2709         if (!pc || !pc->cb_info) {
2710                 LOGE("NULL handle");
2711                 return RECORDER_ERROR_INVALID_PARAMETER;
2712         }
2713
2714         if (second == NULL) {
2715                 LOGE("NULL pointer second");
2716                 return RECORDER_ERROR_INVALID_PARAMETER;
2717         }
2718
2719         LOGD("ENTER");
2720
2721         _recorder_msg_send(api, pc->cb_info, &ret);
2722
2723         if (ret == RECORDER_ERROR_NONE)
2724                 *second = pc->cb_info->get_int_value[_RECORDER_GET_INT_TIME_LIMIT];
2725
2726         LOGD("ret : 0x%x, %d second", ret, *second);
2727
2728         return ret;
2729 }
2730
2731
2732 int recorder_attr_get_audio_device(recorder_h recorder, recorder_audio_device_e *device)
2733 {
2734         int ret = RECORDER_ERROR_NONE;
2735         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_AUDIO_DEVICE;
2736         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2737
2738         if (!pc || !pc->cb_info) {
2739                 LOGE("NULL handle");
2740                 return RECORDER_ERROR_INVALID_PARAMETER;
2741         }
2742
2743         if (device == NULL) {
2744                 LOGE("NULL pointer device");
2745                 return RECORDER_ERROR_INVALID_PARAMETER;
2746         }
2747
2748         LOGD("ENTER");
2749
2750         _recorder_msg_send(api, pc->cb_info, &ret);
2751
2752         if (ret == RECORDER_ERROR_NONE)
2753                 *device = (recorder_audio_device_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_DEVICE];
2754
2755         LOGD("ret : 0x%x, device %d", ret, *device);
2756
2757         return ret;
2758 }
2759
2760
2761 int recorder_attr_get_audio_samplerate(recorder_h recorder, int *samplerate)
2762 {
2763         int ret = RECORDER_ERROR_NONE;
2764         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_AUDIO_SAMPLERATE;
2765         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2766
2767         if (!pc || !pc->cb_info) {
2768                 LOGE("NULL handle");
2769                 return RECORDER_ERROR_INVALID_PARAMETER;
2770         }
2771
2772         if (samplerate == NULL) {
2773                 LOGE("NULL pointer handle");
2774                 return RECORDER_ERROR_INVALID_PARAMETER;
2775         }
2776
2777         LOGD("ENTER");
2778
2779         _recorder_msg_send(api, pc->cb_info, &ret);
2780
2781         if (ret == RECORDER_ERROR_NONE)
2782                 *samplerate = pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_SAMPLERATE];
2783
2784         LOGD("ret : 0x%x, samplerate %d", ret, *samplerate);
2785
2786         return ret;
2787 }
2788
2789
2790 int recorder_attr_get_audio_encoder_bitrate(recorder_h recorder, int *bitrate)
2791 {
2792         int ret = RECORDER_ERROR_NONE;
2793         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_AUDIO_ENCODER_BITRATE;
2794         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2795
2796         if (!pc || !pc->cb_info) {
2797                 LOGE("NULL handle");
2798                 return RECORDER_ERROR_INVALID_PARAMETER;
2799         }
2800
2801         if (bitrate == NULL) {
2802                 LOGE("NULL pointer");
2803                 return RECORDER_ERROR_INVALID_PARAMETER;
2804         }
2805
2806         LOGD("ENTER");
2807
2808         _recorder_msg_send(api, pc->cb_info, &ret);
2809
2810         if (ret == RECORDER_ERROR_NONE)
2811                 *bitrate = pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_ENCODER_BITRATE];
2812
2813         LOGD("ret : 0x%x, bitrate %d", ret, *bitrate);
2814
2815         return ret;
2816 }
2817
2818
2819 int recorder_attr_get_video_encoder_bitrate(recorder_h recorder, int *bitrate)
2820 {
2821         int ret = RECORDER_ERROR_NONE;
2822         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_VIDEO_ENCODER_BITRATE;
2823         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2824
2825         if (!pc || !pc->cb_info) {
2826                 LOGE("NULL handle");
2827                 return RECORDER_ERROR_INVALID_PARAMETER;
2828         }
2829
2830         if (bitrate == NULL) {
2831                 LOGE("NULL pointer");
2832                 return RECORDER_ERROR_INVALID_PARAMETER;
2833         }
2834
2835         LOGD("ENTER");
2836
2837         _recorder_msg_send(api, pc->cb_info, &ret);
2838
2839         if (ret == RECORDER_ERROR_NONE)
2840                 *bitrate = pc->cb_info->get_int_value[_RECORDER_GET_INT_VIDEO_ENCODER_BITRATE];
2841
2842         LOGD("ret : 0x%x", ret);
2843
2844         return ret;
2845 }
2846
2847
2848 int recorder_foreach_supported_audio_encoder(recorder_h recorder, recorder_supported_audio_encoder_cb foreach_cb, void *user_data)
2849 {
2850         int ret = RECORDER_ERROR_NONE;
2851         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2852         muse_recorder_api_e api = MUSE_RECORDER_API_FOREACH_SUPPORTED_AUDIO_ENCODER;
2853
2854         if (!pc || !pc->cb_info || foreach_cb == NULL) {
2855                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2856                 return RECORDER_ERROR_INVALID_PARAMETER;
2857         }
2858
2859         LOGD("Enter, handle :%x", pc->remote_handle);
2860
2861         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_AUDIO_ENCODER] = foreach_cb;
2862         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_AUDIO_ENCODER] = user_data;
2863
2864         _recorder_msg_send(api, pc->cb_info, &ret);
2865
2866         LOGD("ret : 0x%x", ret);
2867
2868         return ret;
2869 }
2870
2871
2872 int recorder_foreach_supported_video_encoder(recorder_h recorder, recorder_supported_video_encoder_cb foreach_cb, void *user_data)
2873 {
2874         int ret = RECORDER_ERROR_NONE;
2875         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2876         muse_recorder_api_e api = MUSE_RECORDER_API_FOREACH_SUPPORTED_VIDEO_ENCODER;
2877
2878         if (!pc || !pc->cb_info || foreach_cb == NULL) {
2879                 LOGE("INVALID_PARAMETER(0x%08x)", RECORDER_ERROR_INVALID_PARAMETER);
2880                 return RECORDER_ERROR_INVALID_PARAMETER;
2881         }
2882
2883         LOGD("Enter, handle :%x", pc->remote_handle);
2884
2885         pc->cb_info->user_cb[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_ENCODER] = foreach_cb;
2886         pc->cb_info->user_data[MUSE_RECORDER_EVENT_TYPE_FOREACH_SUPPORTED_VIDEO_ENCODER] = user_data;
2887
2888         _recorder_msg_send(api, pc->cb_info, &ret);
2889
2890         LOGD("ret : 0x%x", ret);
2891
2892         return ret;
2893 }
2894
2895
2896 int recorder_attr_set_mute(recorder_h recorder, bool enable)
2897 {
2898         int ret = RECORDER_ERROR_NONE;
2899         int set_enable = (int)enable;
2900         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_MUTE;
2901         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2902         recorder_msg_param param;
2903
2904         if (!pc || !pc->cb_info) {
2905                 LOGE("NULL handle");
2906                 return RECORDER_ERROR_INVALID_PARAMETER;
2907         }
2908
2909         LOGD("ENTER");
2910
2911         RECORDER_MSG_PARAM_SET(param, INT, set_enable);
2912
2913         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2914
2915         LOGD("ret : 0x%x", ret);
2916
2917         return ret;
2918 }
2919
2920
2921 bool recorder_attr_is_muted(recorder_h recorder)
2922 {
2923         int ret = false;
2924         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_IS_MUTED;
2925         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2926
2927         if (!pc || !pc->cb_info) {
2928                 LOGE("NULL handle");
2929                 return false;
2930         }
2931
2932         LOGD("ENTER");
2933
2934         _recorder_msg_send(api, pc->cb_info, &ret);
2935
2936         if (ret == RECORDER_ERROR_SERVICE_DISCONNECTED)
2937                 ret = false;
2938
2939         LOGD("ret : %d", ret);
2940
2941         return (bool)ret;
2942 }
2943
2944
2945 int recorder_attr_set_recording_motion_rate(recorder_h recorder, double rate)
2946 {
2947         int ret = RECORDER_ERROR_NONE;
2948         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_RECORDING_MOTION_RATE;
2949         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2950         recorder_msg_param param;
2951
2952         if (!pc || !pc->cb_info) {
2953                 LOGE("NULL handle");
2954                 return RECORDER_ERROR_INVALID_PARAMETER;
2955         }
2956
2957         LOGD("ENTER - %.20lf", rate);
2958
2959         RECORDER_MSG_PARAM_SET(param, DOUBLE, rate);
2960
2961         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
2962
2963         LOGD("ret : 0x%x", ret);
2964
2965         return ret;
2966 }
2967
2968
2969 int recorder_attr_get_recording_motion_rate(recorder_h recorder, double *rate)
2970 {
2971         int ret = RECORDER_ERROR_NONE;
2972         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_RECORDING_MOTION_RATE;
2973         recorder_cli_s *pc = (recorder_cli_s *)recorder;
2974
2975         if (!pc || !pc->cb_info) {
2976                 LOGE("NULL handle");
2977                 return RECORDER_ERROR_INVALID_PARAMETER;
2978         }
2979
2980         if (rate == NULL) {
2981                 LOGE("rate is NULL");
2982                 return RECORDER_ERROR_INVALID_PARAMETER;
2983         }
2984
2985         LOGD("ENTER");
2986
2987         _recorder_msg_send(api, pc->cb_info, &ret);
2988         if (ret == RECORDER_ERROR_NONE)
2989                 *rate = pc->cb_info->get_double_value[_RECORDER_GET_DOUBLE_RECORDING_MOTION_RATE];
2990
2991         LOGD("ret : 0x%x - rate %.20lf", ret, *rate);
2992
2993         return ret;
2994 }
2995
2996
2997 int recorder_attr_set_audio_channel(recorder_h recorder, int channel_count)
2998 {
2999         int ret = RECORDER_ERROR_NONE;
3000         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_AUDIO_CHANNEL;
3001         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3002         recorder_msg_param param;
3003
3004         if (!pc || !pc->cb_info) {
3005                 LOGE("NULL handle");
3006                 return RECORDER_ERROR_INVALID_PARAMETER;
3007         }
3008
3009         LOGD("ENTER");
3010
3011         RECORDER_MSG_PARAM_SET(param, INT, channel_count);
3012
3013         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
3014
3015         LOGD("ret : 0x%x", ret);
3016
3017         return ret;
3018 }
3019
3020
3021 int recorder_attr_get_audio_channel(recorder_h recorder, int *channel_count)
3022 {
3023         int ret = RECORDER_ERROR_NONE;
3024         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_AUDIO_CHANNEL;
3025         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3026
3027         if (!pc || !pc->cb_info) {
3028                 LOGE("NULL handle");
3029                 return RECORDER_ERROR_INVALID_PARAMETER;
3030         }
3031
3032         if (channel_count == NULL) {
3033                 LOGE("channel_count is NULL");
3034                 return RECORDER_ERROR_INVALID_PARAMETER;
3035         }
3036
3037         LOGD("ENTER");
3038
3039         _recorder_msg_send(api, pc->cb_info, &ret);
3040
3041         if (ret == RECORDER_ERROR_NONE)
3042                 *channel_count = pc->cb_info->get_int_value[_RECORDER_GET_INT_AUDIO_CHANNEL];
3043
3044         LOGD("ret : 0x%x, channel count %d", ret, *channel_count);
3045
3046         return ret;
3047 }
3048
3049
3050 int recorder_attr_set_orientation_tag(recorder_h recorder, recorder_rotation_e orientation)
3051 {
3052         int ret = RECORDER_ERROR_NONE;
3053         int set_orientation = (int)orientation;
3054         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_SET_ORIENTATION_TAG;
3055         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3056         recorder_msg_param param;
3057
3058         if (!pc || !pc->cb_info) {
3059                 LOGE("NULL handle");
3060                 return RECORDER_ERROR_INVALID_PARAMETER;
3061         }
3062
3063         LOGD("ENTER");
3064
3065         RECORDER_MSG_PARAM_SET(param, INT, set_orientation);
3066
3067         _recorder_msg_send_param1(api, pc->cb_info, &ret, &param);
3068
3069         LOGD("ret : 0x%x", ret);
3070
3071         return ret;
3072 }
3073
3074
3075 int  recorder_attr_get_orientation_tag(recorder_h recorder, recorder_rotation_e *orientation)
3076 {
3077         int ret = RECORDER_ERROR_NONE;
3078         muse_recorder_api_e api = MUSE_RECORDER_API_ATTR_GET_ORIENTATION_TAG;
3079         recorder_cli_s *pc = (recorder_cli_s *)recorder;
3080
3081         if (!pc || !pc->cb_info) {
3082                 LOGE("NULL handle");
3083                 return RECORDER_ERROR_INVALID_PARAMETER;
3084         }
3085
3086         if (orientation == NULL) {
3087                 LOGE("orientation is NULL");
3088                 return RECORDER_ERROR_INVALID_PARAMETER;
3089         }
3090
3091         LOGD("ENTER");
3092
3093         _recorder_msg_send(api, pc->cb_info, &ret);
3094
3095         if (ret == RECORDER_ERROR_NONE)
3096                 *orientation = (recorder_rotation_e)pc->cb_info->get_int_value[_RECORDER_GET_INT_ORIENTATION_TAG];
3097
3098         LOGD("ret : 0x%x, orientation %d", ret, *orientation);
3099
3100         return ret;
3101 }
3102
3103
3104 int recorder_get_device_state(recorder_type_e type, recorder_device_state_e *state)
3105 {
3106         int ret = RECORDER_ERROR_NONE;
3107         int sock_fd = -1;
3108         int get_device_state = 0;
3109         char *send_msg = NULL;
3110         muse_recorder_api_e api = MUSE_RECORDER_API_GET_DEVICE_STATE;
3111         recorder_cb_info_s *cb_info = NULL;
3112
3113         if (!state) {
3114                 LOGE("NULL pointer");
3115                 return RECORDER_ERROR_INVALID_PARAMETER;
3116         }
3117
3118         LOGD("Enter - type %d", type);
3119
3120         sock_fd = muse_core_client_new();
3121         if (sock_fd < 0) {
3122                 LOGE("muse_core_client_new failed - returned fd %d", sock_fd);
3123                 ret = RECORDER_ERROR_INVALID_OPERATION;
3124                 goto _GET_DEVICE_STATE_EXIT;
3125         }
3126
3127         send_msg = muse_core_msg_json_factory_new(api,
3128                 MUSE_TYPE_INT, "module", MUSE_RECORDER,
3129                 MUSE_TYPE_INT, PARAM_RECORDER_TYPE, type,
3130                 NULL);
3131         if (!send_msg) {
3132                 LOGE("NULL msg");
3133                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
3134                 goto _GET_DEVICE_STATE_EXIT;
3135         }
3136
3137         LOGD("sock_fd : %d, msg : %s", sock_fd, send_msg);
3138
3139         ret = muse_core_ipc_send_msg(sock_fd, send_msg);
3140
3141         muse_core_msg_json_factory_free(send_msg);
3142         send_msg = NULL;
3143
3144         if (ret < 0) {
3145                 LOGE("send msg failed %d", errno);
3146                 ret = RECORDER_ERROR_INVALID_OPERATION;
3147                 goto _GET_DEVICE_STATE_EXIT;
3148         }
3149
3150         cb_info = _recorder_client_callback_new(sock_fd, false);
3151         if (cb_info == NULL) {
3152                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
3153                 goto _GET_DEVICE_STATE_EXIT;
3154         }
3155
3156         sock_fd = -1;
3157
3158         ret = _recorder_client_wait_for_cb_return(api, cb_info, RECORDER_CB_TIMEOUT);
3159         if (ret != RECORDER_ERROR_NONE) {
3160                 LOGE("API_CREATE failed 0x%x", ret);
3161                 goto _GET_DEVICE_STATE_EXIT;
3162         }
3163
3164         if (ret == RECORDER_ERROR_NONE) {
3165                 muse_recorder_msg_get(get_device_state, cb_info->recv_msg);
3166                 *state = (recorder_device_state_e)get_device_state;
3167                 LOGD("device state %d", *state);
3168         } else {
3169                 LOGE("failed 0x%x", ret);
3170         }
3171
3172 _GET_DEVICE_STATE_EXIT:
3173         if (cb_info) {
3174                 _recorder_client_callback_destroy(cb_info);
3175                 cb_info = NULL;
3176         }
3177
3178         if (sock_fd > -1) {
3179                 muse_core_connection_close(sock_fd);
3180                 sock_fd = -1;
3181         }
3182
3183         return ret;
3184 }
3185
3186
3187 int recorder_add_device_state_changed_cb(recorder_device_state_changed_cb callback, void *user_data, int *cb_id)
3188 {
3189         int ret = RECORDER_ERROR_NONE;
3190         recorder_device_state_e state = RECORDER_DEVICE_STATE_IDLE;
3191         recorder_cb_info *info = NULL;
3192
3193         if (!callback || !cb_id) {
3194                 LOGE("invalid pointer %p %p", callback, cb_id);
3195                 return RECORDER_ERROR_INVALID_PARAMETER;
3196         }
3197
3198         g_mutex_lock(&g_rec_dev_state_changed_cb_lock);
3199
3200         /* check recorder support */
3201         ret = recorder_get_device_state(RECORDER_TYPE_AUDIO, &state);
3202         if (ret != RECORDER_ERROR_NONE) {
3203                 LOGE("get device state failed");
3204                 g_mutex_unlock(&g_rec_dev_state_changed_cb_lock);
3205                 return ret;
3206         }
3207
3208         g_rec_dev_last_state = state;
3209
3210         info = g_new0(recorder_cb_info, 1);
3211         if (!info) {
3212                 LOGE("info failed");
3213                 ret = RECORDER_ERROR_OUT_OF_MEMORY;
3214                 goto _DONE;
3215         }
3216
3217         info->id = ++g_rec_dev_state_changed_cb_id;
3218         info->callback = (void *)callback;
3219         info->user_data = user_data;
3220
3221         *cb_id = info->id;
3222
3223         /* subscribe dbus signal for camera state change */
3224         if (!g_rec_dev_state_changed_cb_conn) {
3225                 g_rec_dev_state_changed_cb_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
3226                 if (!g_rec_dev_state_changed_cb_conn) {
3227                         LOGE("failed to get gdbus connection");
3228                         ret = RECORDER_ERROR_INVALID_OPERATION;
3229                         goto _DONE;
3230                 }
3231
3232                 LOGD("subscribe signal %s - %s - %s",
3233                         MM_CAMCORDER_DBUS_OBJECT,
3234                         MM_CAMCORDER_DBUS_INTERFACE_RECORDER,
3235                         MM_CAMCORDER_DBUS_SIGNAL_STATE_CHANGED);
3236
3237                 g_rec_dev_state_changed_cb_subscribe_id = g_dbus_connection_signal_subscribe(g_rec_dev_state_changed_cb_conn,
3238                         NULL, MM_CAMCORDER_DBUS_INTERFACE_RECORDER, MM_CAMCORDER_DBUS_SIGNAL_STATE_CHANGED, MM_CAMCORDER_DBUS_OBJECT, NULL,
3239                         G_DBUS_SIGNAL_FLAGS_NONE, (GDBusSignalCallback)__recorder_device_state_changed_cb, NULL, NULL);
3240                 if (!g_rec_dev_state_changed_cb_subscribe_id) {
3241                         LOGE("failed to get gdbus connection");
3242                         ret = RECORDER_ERROR_INVALID_OPERATION;
3243                         goto _DONE;
3244                 }
3245
3246                 LOGD("signal subscribe id %u", g_rec_dev_state_changed_cb_subscribe_id);
3247         }
3248
3249         g_rec_dev_state_changed_cb_list = g_list_prepend(g_rec_dev_state_changed_cb_list, (gpointer)info);
3250
3251         LOGD("callback id %d", info->id);
3252
3253 _DONE:
3254         if (ret != RECORDER_ERROR_NONE) {
3255                 if (info) {
3256                         g_free(info);
3257                         info = NULL;
3258                 }
3259
3260                 if (g_rec_dev_state_changed_cb_conn) {
3261                         g_object_unref(g_rec_dev_state_changed_cb_conn);
3262                         g_rec_dev_state_changed_cb_conn = NULL;
3263                 }
3264         }
3265
3266         g_mutex_unlock(&g_rec_dev_state_changed_cb_lock);
3267
3268         return ret;
3269 }
3270
3271
3272 int recorder_remove_device_state_changed_cb(int cb_id)
3273 {
3274         int ret = RECORDER_ERROR_NONE;
3275         recorder_device_state_e state = RECORDER_DEVICE_STATE_IDLE;
3276         GList *tmp_list = NULL;
3277         recorder_cb_info *info = NULL;
3278
3279         /* check recorder support */
3280         ret = recorder_get_device_state(RECORDER_TYPE_AUDIO, &state);
3281         if (ret != RECORDER_ERROR_NONE) {
3282                 LOGE("get device state failed");
3283                 return ret;
3284         }
3285
3286         g_mutex_lock(&g_rec_dev_state_changed_cb_lock);
3287
3288         if (!g_rec_dev_state_changed_cb_list) {
3289                 LOGE("there is no callback info");
3290                 ret = RECORDER_ERROR_INVALID_OPERATION;
3291                 goto _DONE;
3292         }
3293
3294         tmp_list = g_rec_dev_state_changed_cb_list;
3295
3296         do {
3297                 info = tmp_list->data;
3298                 tmp_list = tmp_list->next;
3299
3300                 if (!info) {
3301                         LOGW("NULL info");
3302                         continue;
3303                 }
3304
3305                 if (info->id == cb_id) {
3306                         g_rec_dev_state_changed_cb_list = g_list_remove(g_rec_dev_state_changed_cb_list, info);
3307
3308                         g_free(info);
3309                         info = NULL;
3310
3311                         if (!g_rec_dev_state_changed_cb_list) {
3312                                 /* no remained callback */
3313                                 if (g_rec_dev_state_changed_cb_conn) {
3314                                         /* unsubscribe signal */
3315                                         g_dbus_connection_signal_unsubscribe(g_rec_dev_state_changed_cb_conn, g_rec_dev_state_changed_cb_subscribe_id);
3316                                         g_rec_dev_state_changed_cb_subscribe_id = 0;
3317
3318                                         /* unref connection */
3319                                         g_object_unref(g_rec_dev_state_changed_cb_conn);
3320                                         g_rec_dev_state_changed_cb_conn = NULL;
3321                                 }
3322                         }
3323
3324                         LOGD("id %d callback removed", cb_id);
3325                         ret = RECORDER_ERROR_NONE;
3326
3327                         goto _DONE;
3328                 }
3329         } while (tmp_list);
3330
3331         LOGE("id %d callback not found", cb_id);
3332         ret = RECORDER_ERROR_INVALID_PARAMETER;
3333
3334 _DONE:
3335         g_mutex_unlock(&g_rec_dev_state_changed_cb_lock);
3336
3337         return ret;
3338 }