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