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