Merge branch 'tizen' into tizen_line_coverage
[platform/core/api/audio-io.git] / src / cpp / cpp_audio_io.cpp
1 /*
2  * Copyright (c) 2015 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
18 #include <new>
19
20 #include "cpp_audio_io.h"
21 #include "audio_io.h"
22 #include "CAudioIODef.h"
23
24 #include <system_info.h>
25
26 #define FEATURE_MICROPHONE          "http://tizen.org/feature/microphone"
27
28 using namespace std;
29 using namespace tizen_media_audio;
30
31 /**
32  * Defines Structures
33  * type : struct
34  * Name : audio_io_stream_cb_s
35  * Declaration : Keeps user callback pointer and user data for delivering an stream event
36  */
37 typedef struct audio_io_stream_cb_s {
38     void* user_data;
39     audio_in_stream_cb onStream;
40
41     audio_io_stream_cb_s() : user_data(NULL), onStream(NULL)
42     {/* Empty Body */}
43 }   audio_io_stream_cb_s;
44
45 /**
46  * Defines Structures
47  * type : struct
48  * Name : audio_io_state_changed_cb_s
49  * Declaration : Keeps user callback pointer and user data for delivering an state changed event
50  */
51 typedef struct audio_io_state_changed_cb_s {
52     void* user_data;
53     audio_in_state_changed_cb onStateChanged;
54
55     audio_io_state_changed_cb_s() : user_data(NULL), onStateChanged(NULL)
56     {/* Empty Body */}
57 }   audio_io_state_changed_cb_s;
58
59 /**
60  * Defines Structures
61  * type : struct
62  * Name : audio_io_s
63  * Declaration : An handle of AudioIO
64  * The handle has two struct for user callback
65  * And the handle has a pointer of private audioIO object
66  * The CAudioIO is a abstract class object about Input and Output
67  */
68 typedef struct audio_io_s {
69     CAudioIO* audioIoHandle;
70     audio_io_stream_cb_s stream_callback;
71     audio_io_state_changed_cb_s state_changed_callback;
72
73     audio_io_s() : audioIoHandle(NULL)
74     {/* Empty Body */}
75 }   audio_io_s;
76
77
78 /**
79  * Internal functions
80  */
81 static audio_io_error_e __convert_CAudioError(CAudioError& error) {
82     audio_io_error_e ret = AUDIO_IO_ERROR_NONE;
83     CAudioError::EError err = error.getError();
84
85     switch (err) {
86     case CAudioError::EError::ERROR_NONE:
87         ret = AUDIO_IO_ERROR_NONE;
88         break;
89     case CAudioError::EError::ERROR_INVALID_ARGUMENT:
90     case CAudioError::EError::ERROR_INVALID_HANDLE:
91     case CAudioError::EError::ERROR_INVALID_SAMPLERATE:
92     case CAudioError::EError::ERROR_INVALID_CHANNEL:
93     case CAudioError::EError::ERROR_INVALID_FORMAT:
94         ret = AUDIO_IO_ERROR_INVALID_PARAMETER;
95         break;
96     case CAudioError::EError::ERROR_DEVICE_NOT_OPENED:
97         ret = AUDIO_IO_ERROR_DEVICE_NOT_OPENED;
98         break;
99     case CAudioError::EError::ERROR_DEVICE_NOT_CLOSED:
100         ret = AUDIO_IO_ERROR_DEVICE_NOT_CLOSED;
101         break;
102     case CAudioError::EError::ERROR_PERMISSION_DENIED:
103         ret = AUDIO_IO_ERROR_PERMISSION_DENIED;
104         break;
105     case CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION:
106         ret = AUDIO_IO_ERROR_DEVICE_POLICY_RESTRICTION;
107         break;
108     case CAudioError::EError::ERROR_NOT_SUPPORTED:
109         ret = AUDIO_IO_ERROR_NOT_SUPPORTED;
110         break;
111     case CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE:
112         ret = AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE;
113         break;
114     case CAudioError::EError::ERROR_MAX:
115     case CAudioError::EError::ERROR_INTERNAL_OPERATION:
116     case CAudioError::EError::ERROR_NOT_INITIALIZED:
117     case CAudioError::EError::ERROR_FAILED_OPERATION:
118     case CAudioError::EError::ERROR_INVALID_OPERATION:
119         ret = AUDIO_IO_ERROR_INVALID_OPERATION;
120         break;
121     case CAudioError::EError::ERROR_INVALID_STATE:
122         ret = AUDIO_IO_ERROR_INVALID_STATE;
123         break;
124     case CAudioError::EError::ERROR_OUT_OF_MEMORY:
125     case CAudioError::EError::ERROR_INVALID_POINTER:
126         ret = AUDIO_IO_ERROR_INVALID_BUFFER;
127         break;
128     case CAudioError::EError::ERROR_POLICY_BLOCKED:
129     case CAudioError::EError::ERROR_POLICY_INTERRUPTED:
130     case CAudioError::EError::ERROR_POLICY_DUPLICATED:
131         ret = AUDIO_IO_ERROR_SOUND_POLICY;
132         break;
133     }
134
135     return ret;
136 }
137
138 static void __convert_channel_2_audio_info_channel(const audio_channel_e& src_channel,
139                                                    CAudioInfo::EChannel& dst_channel) {
140     switch (src_channel) {
141     case AUDIO_CHANNEL_MONO:
142         dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
143         break;
144     case AUDIO_CHANNEL_STEREO:
145         dst_channel = CAudioInfo::EChannel::CHANNEL_STEREO;
146         break;
147     default:
148         dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
149         break;
150     }
151 }
152
153 static void __convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel,
154                                                    audio_channel_e& dst_channel) {
155     switch (src_channel) {
156     case CAudioInfo::EChannel::CHANNEL_MONO:
157         dst_channel = AUDIO_CHANNEL_MONO;
158         break;
159     case CAudioInfo::EChannel::CHANNEL_STEREO:
160         dst_channel = AUDIO_CHANNEL_STEREO;
161         break;
162     default:
163         dst_channel = AUDIO_CHANNEL_MONO;
164         break;
165     }
166 }
167
168 static void __convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type,
169                                                            CAudioInfo::ESampleType& dst_type) {
170     switch (src_type) {
171     case AUDIO_SAMPLE_TYPE_U8:
172         dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
173         break;
174     case AUDIO_SAMPLE_TYPE_S16_LE:
175         dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE;
176         break;
177     case AUDIO_SAMPLE_TYPE_S24_LE:
178         dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S24_LE;
179         break;
180     case AUDIO_SAMPLE_TYPE_S24_32_LE:
181         dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S24_32_LE;
182         break;
183     default:
184         dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
185         break;
186     }
187 }
188
189 static void __convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type,
190                                                            audio_sample_type_e& dst_type) {
191     switch (src_type) {
192     case CAudioInfo::ESampleType::SAMPLE_TYPE_U8:
193         dst_type = AUDIO_SAMPLE_TYPE_U8;
194         break;
195     case CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE:
196         dst_type = AUDIO_SAMPLE_TYPE_S16_LE;
197         break;
198     case CAudioInfo::ESampleType::SAMPLE_TYPE_S24_LE:
199         dst_type = AUDIO_SAMPLE_TYPE_S24_LE;
200         break;
201     case CAudioInfo::ESampleType::SAMPLE_TYPE_S24_32_LE:
202         dst_type = AUDIO_SAMPLE_TYPE_S24_32_LE;
203         break;
204     default:
205         dst_type = AUDIO_SAMPLE_TYPE_U8;
206         break;
207     }
208 }
209
210 static void __convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type,
211                                                          CAudioInfo::EAudioType& dst_type) {
212     switch (src_type) {
213     case SOUND_TYPE_SYSTEM:
214         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
215         break;
216     case SOUND_TYPE_NOTIFICATION:
217         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION;
218         break;
219     case SOUND_TYPE_ALARM:
220         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM;
221         break;
222     case SOUND_TYPE_RINGTONE:
223         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP;
224         break;
225     case SOUND_TYPE_MEDIA:
226         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
227         break;
228     case SOUND_TYPE_CALL:
229         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
230         break;
231     case SOUND_TYPE_VOIP:
232         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP;
233         break;
234     case SOUND_TYPE_VOICE:
235         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION;
236         break;
237     default:
238         dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
239         break;
240     }
241 }
242
243 static void __convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type,
244                                                          sound_type_e& dst_type) {
245     switch (src_type) {
246     case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA:
247         dst_type = SOUND_TYPE_MEDIA;
248         break;
249     case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM:
250         dst_type = SOUND_TYPE_SYSTEM;
251         break;
252     case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM:
253         dst_type = SOUND_TYPE_ALARM;
254         break;
255     case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION:
256     case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_EMERGENCY:
257         dst_type = SOUND_TYPE_NOTIFICATION;
258         break;
259     case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION:
260         dst_type = SOUND_TYPE_VOICE;
261         break;
262     case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP:
263         dst_type = SOUND_TYPE_RINGTONE;
264         break;
265     case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP:
266         dst_type = SOUND_TYPE_VOIP;
267         break;
268     default:
269         dst_type = SOUND_TYPE_MEDIA;
270         break;
271     }
272 }
273
274 static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src_state) {
275     audio_io_state_e dst_state;
276
277     switch (src_state) {
278     case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE:
279         dst_state = AUDIO_IO_STATE_IDLE;
280         break;
281     case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE:
282         dst_state = AUDIO_IO_STATE_IDLE;
283         break;
284     case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING:
285         dst_state = AUDIO_IO_STATE_RUNNING;
286         break;
287     case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED:
288         dst_state = AUDIO_IO_STATE_PAUSED;
289         break;
290     default:
291         dst_state = AUDIO_IO_STATE_IDLE;
292         break;
293     }
294     return dst_state;
295 }
296
297 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) {
298     if (sample_rate < 0)
299         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate);
300
301     if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO)
302         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel);
303
304     if (type != AUDIO_SAMPLE_TYPE_U8 &&
305         type != AUDIO_SAMPLE_TYPE_S16_LE &&
306         type != AUDIO_SAMPLE_TYPE_S24_LE &&
307         type != AUDIO_SAMPLE_TYPE_S24_32_LE)
308         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type);
309 }
310
311 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) {
312     __check_audio_param(sample_rate, channel, type);
313
314     if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE)
315         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type);
316 }
317
318 static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) {
319     CAudioInfo::EChannel dstChannel;
320     CAudioInfo::ESampleType dstSampleType;
321     CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
322
323     __convert_channel_2_audio_info_channel(channel, dstChannel);
324     __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
325
326     return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
327 }
328
329 static CAudioInfo __generate_audio_output_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type, sound_type_e sound_type) {
330     CAudioInfo::EChannel dstChannel;
331     CAudioInfo::ESampleType dstSampleType;
332     CAudioInfo::EAudioType dstAudioType;
333
334     __convert_channel_2_audio_info_channel(channel, dstChannel);
335     __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
336     __convert_sound_type_2_audio_info_audio_type(sound_type, dstAudioType);
337
338     return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
339 }
340
341 static void __handle_safe_free(audio_io_s* handle, void *obj, bool is_output) {
342     VALID_POINTER_START(handle)
343         SAFE_FINALIZE(handle->audioIoHandle);
344         SAFE_DELETE(handle->audioIoHandle);
345         SAFE_DELETE(handle);
346     VALID_POINTER_END
347
348     VALID_POINTER_START(obj)
349         if (is_output)
350             *(audio_out_h *)obj = NULL;
351         else
352             *(audio_in_h *)obj = NULL;
353     VALID_POINTER_END
354 }
355
356 /**
357  * Implements CAPI functions
358  */
359 int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_in_h *input) {
360     audio_io_s* handle = NULL;
361     bool mic_enable = false;
362     int ret = 0;
363     try {
364         if (input == NULL) {
365             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
366                                   "Parameters are NULL input:%p", input);
367         }
368
369         __check_audio_param(sample_rate, channel, type);
370
371         AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x]", sample_rate, channel, type);
372
373         /* If MIC is not supported, return NOT_SUPPORTED error */
374         ret = system_info_get_platform_bool(FEATURE_MICROPHONE, &mic_enable);
375         AUDIO_IO_LOGD("system_info_platform [%s]=[%d], ret[%d]", FEATURE_MICROPHONE, mic_enable, ret);
376         if (ret != SYSTEM_INFO_ERROR_NONE || !mic_enable) {
377             THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "System doesn't support microphone!");
378         }
379
380         CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type);
381
382         handle = new audio_io_s;
383         handle->audioIoHandle = new CAudioInput(audioInfo);
384         handle->audioIoHandle->initialize();
385
386         AUDIO_IO_LOGD("[%p] created", handle);
387         *input = handle;
388     } catch (CAudioError& e) {
389         AUDIO_IO_LOGE("%s", e.getErrorMsg());
390         __handle_safe_free(handle, (void *)input, false);
391         return __convert_CAudioError(e);
392     } catch (const std::bad_alloc&) {
393 //LCOV_EXCL_START
394         CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY;
395         AUDIO_IO_LOGE("Failed to allocate handle");
396         __handle_safe_free(handle, (void *)input, false);
397         return __convert_CAudioError(e);
398 //LCOV_EXCL_STOP
399     }
400
401     return AUDIO_IO_ERROR_NONE;
402 }
403
404 int cpp_audio_in_destroy(audio_in_h input) {
405     audio_io_s* handle = static_cast<audio_io_s*>(input);
406
407     try {
408         if (handle == NULL)
409             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
410                                   "Parameters are NULL input:%p", input);
411         assert(handle->audioIoHandle);
412         AUDIO_IO_LOGD("[%p]", handle);
413
414         /* Internal unprepare for backward compatibility */
415         handle->audioIoHandle->unprepare();
416
417         SAFE_FINALIZE(handle->audioIoHandle);
418         SAFE_DELETE(handle->audioIoHandle);
419         SAFE_DELETE(handle);
420     } catch (CAudioError& e) {
421         AUDIO_IO_LOGE("%s", e.getErrorMsg());
422         return __convert_CAudioError(e);
423     }
424
425     AUDIO_IO_LOGD("destroyed");
426
427     return AUDIO_IO_ERROR_NONE;
428 }
429
430 int cpp_audio_in_set_sound_stream_info(audio_in_h input, sound_stream_info_h stream_info) {
431     audio_io_s* handle = static_cast<audio_io_s*>(input);
432
433     try {
434         if (handle == NULL || stream_info == NULL)
435             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
436                                    "Parameters are NULL input:%p, stream_info:%p", input, stream_info);
437         assert(handle->audioIoHandle);
438         AUDIO_IO_LOGD("[%p], stream_info:[%p]", handle, stream_info);
439
440         handle->audioIoHandle->setStreamInfo(stream_info);
441     } catch (CAudioError& e) {
442         AUDIO_IO_LOGE("%s", e.getErrorMsg());
443         return __convert_CAudioError(e);
444     }
445
446     AUDIO_IO_LOGD("[%p] done", handle);
447
448     return AUDIO_IO_ERROR_NONE;
449 }
450
451 int cpp_audio_in_prepare(audio_in_h input) {
452     audio_io_s* handle = static_cast<audio_io_s*>(input);
453
454     try {
455         if (handle == NULL)
456             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
457                                    "Parameters are NULL input:%p", input);
458         assert(handle->audioIoHandle);
459         AUDIO_IO_LOGD("[%p]", handle);
460
461         handle->audioIoHandle->prepare();
462     } catch (CAudioError& e) {
463         AUDIO_IO_LOGE("%s", e.getErrorMsg());
464         return __convert_CAudioError(e);
465     }
466
467     AUDIO_IO_LOGD("[%p] prepared", handle);
468
469     return AUDIO_IO_ERROR_NONE;
470 }
471
472 int cpp_audio_in_unprepare(audio_in_h input) {
473     audio_io_s* handle = static_cast<audio_io_s*>(input);
474
475     try {
476         if (handle == NULL)
477             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
478                                    "Parameters are NULL input:%p", input);
479         assert(handle->audioIoHandle);
480         AUDIO_IO_LOGD("[%p]", handle);
481
482         handle->audioIoHandle->unprepare();
483     } catch (CAudioError& e) {
484         AUDIO_IO_LOGE("%s", e.getErrorMsg());
485         return __convert_CAudioError(e);
486     }
487
488     AUDIO_IO_LOGD("[%p] unprepared", handle);
489
490     return AUDIO_IO_ERROR_NONE;
491 }
492
493 int cpp_audio_in_pause(audio_in_h input) {
494     audio_io_s* handle = static_cast<audio_io_s*>(input);
495
496     try {
497         if (handle == NULL)
498             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
499                                    "Parameters are NULL input:%p", input);
500         assert(handle->audioIoHandle);
501         AUDIO_IO_LOGD("[%p]", handle);
502
503         handle->audioIoHandle->pause();
504     } catch (CAudioError& e) {
505         AUDIO_IO_LOGE("%s", e.getErrorMsg());
506         return __convert_CAudioError(e);
507     }
508
509     AUDIO_IO_LOGD("[%p] paused", handle);
510
511     return AUDIO_IO_ERROR_NONE;
512 }
513
514 int cpp_audio_in_resume(audio_in_h input) {
515     audio_io_s* handle = static_cast<audio_io_s*>(input);
516
517     try {
518         if (handle == NULL)
519             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
520                                    "Parameters are NULL input:%p", input);
521         assert(handle->audioIoHandle);
522         AUDIO_IO_LOGD("[%p]", handle);
523
524         handle->audioIoHandle->resume();
525     } catch (CAudioError& e) {
526         AUDIO_IO_LOGE("%s", e.getErrorMsg());
527         return __convert_CAudioError(e);
528     }
529
530     AUDIO_IO_LOGD("[%p] resumed", handle);
531
532     return AUDIO_IO_ERROR_NONE;
533 }
534
535 int cpp_audio_in_flush(audio_in_h input) {
536     audio_io_s* handle = static_cast<audio_io_s*>(input);
537
538     try {
539         if (handle == NULL)
540             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
541                                    "Parameters are NULL input:%p", input);
542         assert(handle->audioIoHandle);
543         AUDIO_IO_LOGD("[%p]", handle);
544
545         handle->audioIoHandle->flush();
546     } catch (CAudioError& e) {
547         AUDIO_IO_LOGE("%s", e.getErrorMsg());
548         return __convert_CAudioError(e);
549     }
550
551     AUDIO_IO_LOGD("[%p] flushed", handle);
552
553     return AUDIO_IO_ERROR_NONE;
554 }
555
556 int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) {
557     audio_io_s* handle = static_cast<audio_io_s*>(input);
558     int ret = 0;
559
560     try {
561         if (handle == NULL || buffer == NULL)
562             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
563                                    "Parameters are NULL input:%p, buffer:%p", input, buffer);
564         assert(handle->audioIoHandle);
565
566         CAudioInput* inputHandle = static_cast<CAudioInput*>(handle->audioIoHandle);
567         if (inputHandle == NULL) {
568             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
569         }
570
571         size_t readn = inputHandle->read(buffer, static_cast<size_t>(length));
572         ret = static_cast<int>(readn);
573 #ifdef _AUDIO_IO_DEBUG_TIMING_
574         AUDIO_IO_LOGD("readn:%d", readn);
575 #endif
576     } catch (CAudioError& e) {
577         AUDIO_IO_LOGE("%s", e.getErrorMsg());
578         return __convert_CAudioError(e);
579     }
580
581     return ret;
582 }
583
584 int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) {
585     audio_io_s* handle = static_cast<audio_io_s*>(input);
586
587     try {
588         if (handle == NULL || size == NULL)
589             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
590                                    "Parameters are NULL input:%p, size:%p", input, size);
591         assert(handle->audioIoHandle);
592
593         CAudioIO* inputHandle = static_cast<CAudioInput*>(handle->audioIoHandle);
594         if (inputHandle == NULL) {
595             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
596         }
597         *size = inputHandle->getBufferSize();
598     } catch (CAudioError& e) {
599         AUDIO_IO_LOGE("%s", e.getErrorMsg());
600         return __convert_CAudioError(e);
601     }
602
603     return AUDIO_IO_ERROR_NONE;
604 }
605
606 int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) {
607     audio_io_s* handle = static_cast<audio_io_s*>(input);
608
609     try {
610         if (handle == NULL || sample_rate == NULL)
611             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
612                                    "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
613         assert(handle->audioIoHandle);
614
615         *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
616     } catch (CAudioError& e) {
617         AUDIO_IO_LOGE("%s", e.getErrorMsg());
618         return __convert_CAudioError(e);
619     }
620
621     return AUDIO_IO_ERROR_NONE;
622 }
623
624 int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
625     audio_io_s* handle = static_cast<audio_io_s*>(input);
626
627     try {
628         if (handle == NULL || channel == NULL)
629             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
630                                    "Parameters are NULL input:%p, channel:%p", input, channel);
631         assert(handle->audioIoHandle);
632
633         const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
634         audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
635         __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
636
637         *channel = dstChannel;
638     } catch (CAudioError& e) {
639         AUDIO_IO_LOGE("%s", e.getErrorMsg());
640         return __convert_CAudioError(e);
641     }
642
643     return AUDIO_IO_ERROR_NONE;
644 }
645
646 int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
647     audio_io_s* handle = static_cast<audio_io_s*>(input);
648
649     try {
650         if (handle == NULL || type == NULL)
651             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
652                                    "Parameters are NULL input:%p, type:%p", input, type);
653         assert(handle->audioIoHandle);
654
655         const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
656         audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
657         __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
658
659         *type = dstSampleType;
660     } catch (CAudioError& e) {
661         AUDIO_IO_LOGE("%s", e.getErrorMsg());
662         return __convert_CAudioError(e);
663     }
664
665     return AUDIO_IO_ERROR_NONE;
666 }
667
668 static void __stream_cb_internal(size_t nbytes, void *user_data) {
669     audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
670     assert(audioIo);
671
672     if (audioIo->stream_callback.onStream)
673         audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data);
674 }
675
676 //LCOV_EXCL_START
677 static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state,
678                                         CAudioInfo::EAudioIOState state_prev,
679                                         bool by_policy,
680                                         void *user_data) {
681     audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
682     assert(audioIo);
683
684     if (audioIo->state_changed_callback.onStateChanged)
685         audioIo->state_changed_callback.onStateChanged(audioIo, __convert_state_type(state_prev),
686                                                        __convert_state_type(state), by_policy,
687                                                        audioIo->state_changed_callback.user_data);
688 }
689 //LCOV_EXCL_STOP
690
691 int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) {
692     audio_io_s* handle = static_cast<audio_io_s*>(input);
693
694     try {
695         if (handle == NULL || callback == NULL)
696             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
697                                    "Parameters are NULL input:%p, callback:%p", input, callback);
698         assert(handle->audioIoHandle);
699         AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data);
700
701         handle->stream_callback.onStream = callback;
702         handle->stream_callback.user_data = user_data;
703
704         CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
705         cb.mUserData = static_cast<void*>(handle);
706         cb.onStream  = __stream_cb_internal;
707
708         handle->audioIoHandle->setStreamCallback(cb);
709     } catch (CAudioError& e) {
710         AUDIO_IO_LOGE("%s", e.getErrorMsg());
711         return __convert_CAudioError(e);
712     }
713
714     AUDIO_IO_LOGD("[%p] done", handle);
715
716     return AUDIO_IO_ERROR_NONE;
717 }
718
719 int cpp_audio_in_unset_stream_cb(audio_in_h input) {
720     audio_io_s* handle = static_cast<audio_io_s*>(input);
721
722     try {
723         if (handle == NULL)
724             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
725                                    "Parameters are NULL input:%p", input);
726         assert(handle->audioIoHandle);
727         AUDIO_IO_LOGD("[%p]", handle);
728
729         handle->stream_callback.onStream = NULL;
730         handle->stream_callback.user_data = NULL;
731
732         CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
733         cb.mUserData = NULL;
734         cb.onStream  = NULL;
735
736         handle->audioIoHandle->setStreamCallback(cb);
737     } catch (CAudioError& e) {
738         AUDIO_IO_LOGE("%s", e.getErrorMsg());
739         return __convert_CAudioError(e);
740     }
741
742     AUDIO_IO_LOGD("[%p] done", handle);
743
744     return AUDIO_IO_ERROR_NONE;
745 }
746
747 int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) {
748     audio_io_s* handle = static_cast<audio_io_s*>(input);
749     size_t _length = 0;
750
751     try {
752         if (handle == NULL || buffer == NULL)
753             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
754                                    "Parameters are NULL input:%p, buffer:%p", input, buffer);
755
756         CAudioInput* inputHandle = static_cast<CAudioInput*>(handle->audioIoHandle);
757         if (inputHandle == NULL)
758             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
759
760         inputHandle->peek(buffer, &_length);
761     } catch (CAudioError& e) {
762         AUDIO_IO_LOGE("%s", e.getErrorMsg());
763         return __convert_CAudioError(e);
764     }
765
766     *length = (unsigned int)_length;
767
768     return AUDIO_IO_ERROR_NONE;
769 }
770
771 int cpp_audio_in_drop(audio_in_h input) {
772     audio_io_s* handle = static_cast<audio_io_s*>(input);
773
774     try {
775         if (handle == NULL)
776             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
777                                    "Parameters are NULL input:%p", input);
778
779         CAudioInput* inputHandle = static_cast<CAudioInput*>(handle->audioIoHandle);
780         if (inputHandle == NULL)
781             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
782
783         inputHandle->drop();
784     } catch (CAudioError& e) {
785         AUDIO_IO_LOGE("%s", e.getErrorMsg());
786         return __convert_CAudioError(e);
787     }
788
789     return AUDIO_IO_ERROR_NONE;
790 }
791
792 int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) {
793     audio_io_s* handle = static_cast<audio_io_s*>(input);
794
795     try {
796         if (handle == NULL || callback == NULL)
797             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
798                                    "Parameters are NULL input:%p, callback:%p", input, callback);
799         assert(handle->audioIoHandle);
800         AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data);
801
802         handle->state_changed_callback.onStateChanged = callback;
803         handle->state_changed_callback.user_data = user_data;
804
805         CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
806         cb.mUserData = static_cast<void*>(handle);
807         cb.onStateChanged = __state_changed_cb_internal;
808
809         handle->audioIoHandle->setStateChangedCallback(cb);
810     } catch (CAudioError& e) {
811         AUDIO_IO_LOGE("%s", e.getErrorMsg());
812         return __convert_CAudioError(e);
813     }
814
815     AUDIO_IO_LOGD("[%p] done", handle);
816
817     return AUDIO_IO_ERROR_NONE;
818 }
819
820 int cpp_audio_in_unset_state_changed_cb(audio_in_h input) {
821     audio_io_s* handle = static_cast<audio_io_s*>(input);
822
823     try {
824         if (handle == NULL)
825             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
826                                    "Parameters are NULL output:%p", input);
827         assert(handle->audioIoHandle);
828         AUDIO_IO_LOGD("[%p]", handle);
829
830         handle->state_changed_callback.onStateChanged = NULL;
831         handle->state_changed_callback.user_data = NULL;
832
833         CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
834         cb.mUserData = NULL;
835         cb.onStateChanged  = NULL;
836
837         handle->audioIoHandle->setStateChangedCallback(cb);
838     } catch (CAudioError& e) {
839         AUDIO_IO_LOGE("%s", e.getErrorMsg());
840         return __convert_CAudioError(e);
841     }
842
843     AUDIO_IO_LOGD("[%p] done", handle);
844
845     return AUDIO_IO_ERROR_NONE;
846 }
847
848
849 /**
850  * Audio Out
851  */
852 int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) {
853     audio_io_s* handle = NULL;
854     try {
855         if (output == NULL)
856             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
857                                    "Parameters are NULL output:%p", output);
858
859         __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */);
860
861         AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x]", sample_rate, channel, type);
862         CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA);
863
864         handle = new audio_io_s;
865         handle->audioIoHandle = new CAudioOutput(audioInfo);
866         handle->audioIoHandle->initialize();
867
868         AUDIO_IO_LOGD("[%p] created", handle);
869         *output = handle;
870     } catch (CAudioError& e) {
871         AUDIO_IO_LOGE("%s", e.getErrorMsg());
872         __handle_safe_free(handle, (void *)output, true);
873         return __convert_CAudioError(e);
874     } catch (const std::bad_alloc&) {
875 //LCOV_EXCL_START
876         CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY;
877         AUDIO_IO_LOGE("Failed to allocate handle");
878         __handle_safe_free(handle, (void *)output, true);
879         return __convert_CAudioError(e);
880 //LCOV_EXCL_STOP
881     }
882
883     return AUDIO_IO_ERROR_NONE;
884 }
885
886 int cpp_audio_out_destroy(audio_out_h output) {
887     audio_io_s* handle = static_cast<audio_io_s*>(output);
888
889     try {
890         if (handle == NULL)
891             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
892                                    "Parameter is NULL output:%p", output);
893         assert(handle->audioIoHandle);
894         AUDIO_IO_LOGD("[%p]", handle);
895
896         /* Internal unprepare for backward compatibility */
897         handle->audioIoHandle->unprepare();
898
899         SAFE_FINALIZE(handle->audioIoHandle);
900         SAFE_DELETE(handle->audioIoHandle);
901         SAFE_DELETE(handle);
902     } catch (CAudioError& e) {
903         AUDIO_IO_LOGE("%s", e.getErrorMsg());
904         return __convert_CAudioError(e);
905     }
906
907     AUDIO_IO_LOGD("destroyed");
908
909     return AUDIO_IO_ERROR_NONE;
910 }
911
912 int cpp_audio_out_set_sound_stream_info(audio_out_h output, sound_stream_info_h stream_info) {
913     audio_io_s* handle = static_cast<audio_io_s*>(output);
914
915     try {
916         if (handle == NULL || stream_info == NULL)
917             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
918                                    "Parameters are NULL output:%p, stream_info:%p", output, stream_info);
919         assert(handle->audioIoHandle);
920         AUDIO_IO_LOGD("[%p], stream_info:[%p]", handle, stream_info);
921
922         handle->audioIoHandle->setStreamInfo(stream_info);
923     } catch (CAudioError& e) {
924         AUDIO_IO_LOGE("%s", e.getErrorMsg());
925         return __convert_CAudioError(e);
926     }
927
928     AUDIO_IO_LOGD("[%p] done", handle);
929
930     return AUDIO_IO_ERROR_NONE;
931 }
932
933 int cpp_audio_out_prepare(audio_out_h output) {
934     audio_io_s* handle = static_cast<audio_io_s*>(output);
935
936     try {
937         if (handle == NULL)
938             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
939                                    "Parameter is NULL output:%p", output);
940         assert(handle->audioIoHandle);
941         AUDIO_IO_LOGD("[%p]", handle);
942
943         handle->audioIoHandle->prepare();
944     } catch (CAudioError& e) {
945         AUDIO_IO_LOGE("%s", e.getErrorMsg());
946         return __convert_CAudioError(e);
947     }
948
949     AUDIO_IO_LOGD("[%p] prepared", handle);
950
951     return AUDIO_IO_ERROR_NONE;
952 }
953
954 int cpp_audio_out_unprepare(audio_out_h output) {
955     audio_io_s* handle = static_cast<audio_io_s*>(output);
956
957     try {
958         if (handle == NULL)
959             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
960                                    "Parameter is NULL output:%p", output);
961         assert(handle->audioIoHandle);
962         AUDIO_IO_LOGD("[%p]", handle);
963
964         handle->audioIoHandle->unprepare();
965     } catch (CAudioError& e) {
966         AUDIO_IO_LOGE("%s", e.getErrorMsg());
967         return __convert_CAudioError(e);
968     }
969
970     AUDIO_IO_LOGD("[%p] unprepared", handle);
971
972     return AUDIO_IO_ERROR_NONE;
973 }
974
975 int cpp_audio_out_pause(audio_out_h output) {
976     audio_io_s* handle = static_cast<audio_io_s*>(output);
977
978     try {
979         if (handle == NULL)
980             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
981                                    "Parameter is NULL output:%p", output);
982         assert(handle->audioIoHandle);
983         AUDIO_IO_LOGD("[%p]", handle);
984
985         handle->audioIoHandle->pause();
986     } catch (CAudioError& e) {
987         AUDIO_IO_LOGE("%s", e.getErrorMsg());
988         return __convert_CAudioError(e);
989     }
990
991     AUDIO_IO_LOGD("[%p] paused", handle);
992
993     return AUDIO_IO_ERROR_NONE;
994 }
995
996 int cpp_audio_out_resume(audio_out_h output) {
997     audio_io_s* handle = static_cast<audio_io_s*>(output);
998
999     try {
1000         if (handle == NULL)
1001             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1002                                    "Parameter is NULL output:%p", output);
1003         assert(handle->audioIoHandle);
1004         AUDIO_IO_LOGD("[%p]", handle);
1005
1006         handle->audioIoHandle->resume();
1007     } catch (CAudioError& e) {
1008         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1009         return __convert_CAudioError(e);
1010     }
1011
1012     AUDIO_IO_LOGD("[%p] resumed", handle);
1013
1014     return AUDIO_IO_ERROR_NONE;
1015 }
1016
1017 int cpp_audio_out_drain(audio_out_h output) {
1018     audio_io_s* handle = static_cast<audio_io_s*>(output);
1019
1020     try {
1021         if (handle == NULL)
1022             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1023                                    "Parameter is NULL output:%p", output);
1024         assert(handle->audioIoHandle);
1025         AUDIO_IO_LOGD("[%p]", handle);
1026
1027         handle->audioIoHandle->drain();
1028     } catch (CAudioError& e) {
1029         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1030         return __convert_CAudioError(e);
1031     }
1032
1033     AUDIO_IO_LOGD("[%p] drained", handle);
1034
1035     return AUDIO_IO_ERROR_NONE;
1036 }
1037
1038 int cpp_audio_out_flush(audio_out_h output) {
1039     audio_io_s* handle = static_cast<audio_io_s*>(output);
1040
1041     try {
1042         if (handle == NULL)
1043             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1044                                    "Parameter is NULL output:%p", output);
1045         assert(handle->audioIoHandle);
1046         AUDIO_IO_LOGD("[%p]", handle);
1047
1048         handle->audioIoHandle->flush();
1049     } catch (CAudioError& e) {
1050         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1051         return __convert_CAudioError(e);
1052     }
1053
1054     AUDIO_IO_LOGD("[%p] flushed", handle);
1055
1056     return AUDIO_IO_ERROR_NONE;
1057 }
1058
1059 int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) {
1060     audio_io_s* handle = static_cast<audio_io_s*>(output);
1061     int ret = 0;
1062
1063     try {
1064         if (handle == NULL || buffer == NULL)
1065             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1066                                    "Parameter is NULL output:%p, buffer:%p", output, buffer);
1067         assert(handle->audioIoHandle);
1068
1069         CAudioOutput* outputHandle = static_cast<CAudioOutput*>(handle->audioIoHandle);
1070         if (outputHandle == NULL)
1071             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1072
1073         size_t written = outputHandle->write(buffer, static_cast<size_t>(length));
1074         ret = static_cast<int>(written);
1075 #ifdef _AUDIO_IO_DEBUG_TIMING_
1076         AUDIO_IO_LOGD("written:%d", written);
1077 #endif
1078     } catch (CAudioError& e) {
1079         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1080         return __convert_CAudioError(e);
1081     }
1082
1083     return ret;
1084 }
1085
1086 int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) {
1087     audio_io_s* handle = static_cast<audio_io_s*>(output);
1088
1089     try {
1090         if (handle == NULL || size == NULL)
1091             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1092                                    "Parameters are NULL output:%p, size:%p", output, size);
1093         assert(handle->audioIoHandle);
1094
1095         CAudioOutput* outputHandle = static_cast<CAudioOutput*>(handle->audioIoHandle);
1096         if (outputHandle == NULL)
1097             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1098
1099         *size = outputHandle->getBufferSize();
1100     } catch (CAudioError& e) {
1101         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1102         return __convert_CAudioError(e);
1103     }
1104
1105     return AUDIO_IO_ERROR_NONE;
1106 }
1107
1108 int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
1109     audio_io_s* handle = static_cast<audio_io_s*>(output);
1110
1111     try {
1112         if (handle == NULL || sample_rate == NULL)
1113             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1114                                    "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
1115         assert(handle->audioIoHandle);
1116
1117         *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
1118     } catch (CAudioError& e) {
1119         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1120         return __convert_CAudioError(e);
1121     }
1122
1123     return AUDIO_IO_ERROR_NONE;
1124 }
1125
1126 int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
1127     audio_io_s* handle = static_cast<audio_io_s*>(output);
1128
1129     try {
1130         if (handle == NULL || channel == NULL)
1131             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1132                                    "Parameters are NULL output:%p, channel:%p", output, channel);
1133         assert(handle->audioIoHandle);
1134
1135         const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
1136         audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
1137         __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
1138
1139         *channel = dstChannel;
1140     } catch (CAudioError& e) {
1141         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1142         return __convert_CAudioError(e);
1143     }
1144
1145     return AUDIO_IO_ERROR_NONE;
1146 }
1147
1148 int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) {
1149     audio_io_s* handle = static_cast<audio_io_s*>(output);
1150
1151     try {
1152         if (handle == NULL || type == NULL)
1153             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1154                                    "Parameters are NULL output:%p, type:%p", output, type);
1155         assert(handle->audioIoHandle);
1156
1157         const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
1158         audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
1159         __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
1160
1161         *type = dstSampleType;
1162     } catch (CAudioError& e) {
1163         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1164         return __convert_CAudioError(e);
1165     }
1166
1167     return AUDIO_IO_ERROR_NONE;
1168 }
1169
1170 int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) {
1171     audio_io_s* handle = static_cast<audio_io_s*>(output);
1172
1173     try {
1174         if (handle == NULL || type == NULL)
1175             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1176                                    "Parameters are NULL output:%p, type:%p", output, type);
1177         assert(handle->audioIoHandle);
1178
1179         const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType();
1180         sound_type_e dstSoundType = SOUND_TYPE_MEDIA;
1181         __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType);
1182
1183         *type = dstSoundType;
1184     } catch (CAudioError& e) {
1185         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1186         return __convert_CAudioError(e);
1187     }
1188
1189     return AUDIO_IO_ERROR_NONE;
1190 }
1191
1192 int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) {
1193     audio_io_s* handle = static_cast<audio_io_s*>(output);
1194
1195     try {
1196         if (handle == NULL || callback == NULL)
1197             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1198                                    "Parameters are NULL output:%p, callback:%p", output, callback);
1199         assert(handle->audioIoHandle);
1200         AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data);
1201
1202         handle->stream_callback.onStream = callback;
1203         handle->stream_callback.user_data = user_data;
1204
1205         CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1206         cb.mUserData = static_cast<void*>(handle);
1207         cb.onStream = __stream_cb_internal;
1208
1209         handle->audioIoHandle->setStreamCallback(cb);
1210     } catch (CAudioError& e) {
1211         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1212         return __convert_CAudioError(e);
1213     }
1214
1215     AUDIO_IO_LOGD("[%p] done", handle);
1216
1217     return AUDIO_IO_ERROR_NONE;
1218 }
1219
1220 int cpp_audio_out_unset_stream_cb(audio_out_h output) {
1221     audio_io_s* handle = static_cast<audio_io_s*>(output);
1222
1223     try {
1224         if (handle == NULL)
1225             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1226                                    "Parameters are NULL output:%p", output);
1227         assert(handle->audioIoHandle);
1228         AUDIO_IO_LOGD("[%p]", handle);
1229
1230         handle->stream_callback.onStream = NULL;
1231         handle->stream_callback.user_data = NULL;
1232
1233         CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1234         cb.mUserData = NULL;
1235         cb.onStream = NULL;
1236
1237         handle->audioIoHandle->setStreamCallback(cb);
1238     } catch (CAudioError& e) {
1239         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1240         return __convert_CAudioError(e);
1241     }
1242
1243     AUDIO_IO_LOGD("[%p] done", handle);
1244
1245     return AUDIO_IO_ERROR_NONE;
1246 }
1247
1248 int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) {
1249     audio_io_s* handle = static_cast<audio_io_s*>(output);
1250
1251     try {
1252         if (handle == NULL || callback == NULL)
1253             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1254                                    "Parameters are NULL output:%p, callback:%p", output, callback);
1255         assert(handle->audioIoHandle);
1256         AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data);
1257
1258         handle->state_changed_callback.onStateChanged = callback;
1259         handle->state_changed_callback.user_data = user_data;
1260
1261         CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1262         cb.mUserData = static_cast<void*>(handle);
1263         cb.onStateChanged = __state_changed_cb_internal;
1264
1265         handle->audioIoHandle->setStateChangedCallback(cb);
1266     } catch (CAudioError& e) {
1267         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1268         return __convert_CAudioError(e);
1269     }
1270
1271     AUDIO_IO_LOGD("[%p] done", handle);
1272
1273     return AUDIO_IO_ERROR_NONE;
1274 }
1275
1276 int cpp_audio_out_unset_state_changed_cb(audio_out_h output) {
1277     audio_io_s* handle = static_cast<audio_io_s*>(output);
1278
1279     try {
1280         if (handle == NULL)
1281             THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1282                                    "Parameters are NULL output:%p", output);
1283         assert(handle->audioIoHandle);
1284         AUDIO_IO_LOGD("[%p]", handle);
1285
1286         handle->state_changed_callback.onStateChanged = NULL;
1287         handle->state_changed_callback.user_data = NULL;
1288
1289         CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1290         cb.mUserData = NULL;
1291         cb.onStateChanged = NULL;
1292
1293         handle->audioIoHandle->setStateChangedCallback(cb);
1294     } catch (CAudioError& e) {
1295         AUDIO_IO_LOGE("%s", e.getErrorMsg());
1296         return __convert_CAudioError(e);
1297     }
1298
1299     AUDIO_IO_LOGD("[%p] done", handle);
1300
1301     return AUDIO_IO_ERROR_NONE;
1302 }