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