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