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