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