2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "cpp_audio_io.h"
19 #include <sound_manager_internal.h>
21 #include "CAudioIODef.h"
23 #include <system_info.h>
25 #define FEATURE_MICROPHONE "http://tizen.org/feature/microphone"
28 using namespace tizen_media_audio;
34 * Name : audio_io_interrupted_cb_s
35 * Declaration : Keeps user callback pointer and user data for delivering an interrupt event
37 typedef struct audio_io_interrupted_cb_s {
39 audio_io_interrupted_cb onInterrupt;
41 audio_io_interrupted_cb_s() : user_data(NULL), onInterrupt(NULL)
43 } audio_io_interrupted_cb_s;
48 * Name : audio_io_stream_cb_s
49 * Declaration : Keeps user callback pointer and user data for delivering an stream event
51 typedef struct audio_io_stream_cb_s {
53 audio_in_stream_cb onStream;
55 audio_io_stream_cb_s() : user_data(NULL), onStream(NULL)
57 } audio_io_stream_cb_s;
62 * Name : audio_io_state_changed_cb_s
63 * Declaration : Keeps user callback pointer and user data for delivering an state changed event
65 typedef struct audio_io_state_changed_cb_s {
67 audio_in_state_changed_cb onStateChanged;
69 audio_io_state_changed_cb_s() : user_data(NULL), onStateChanged(NULL)
71 } audio_io_state_changed_cb_s;
77 * Declaration : An handle of AudioIO
78 * The handle has two struct for user callback
79 * And the handle has a pointer of private audioIO object
80 * The CAudioIO is a abstract class object about Input and Output
82 typedef struct audio_io_s {
83 CAudioIO* audioIoHandle;
84 audio_io_interrupted_cb_s interrupt_callback;
85 audio_io_stream_cb_s stream_callback;
86 audio_io_state_changed_cb_s state_changed_callback;
88 audio_io_s() : audioIoHandle(NULL)
96 static audio_io_error_e __convert_CAudioError(CAudioError& error) {
97 audio_io_error_e ret = AUDIO_IO_ERROR_NONE;
98 CAudioError::EError err = error.getError();
101 case CAudioError::EError::ERROR_NONE:
102 ret = AUDIO_IO_ERROR_NONE;
104 case CAudioError::EError::ERROR_INVALID_ARGUMENT:
105 case CAudioError::EError::ERROR_INVALID_HANDLE:
106 case CAudioError::EError::ERROR_INVALID_SAMPLERATE:
107 case CAudioError::EError::ERROR_INVALID_CHANNEL:
108 case CAudioError::EError::ERROR_INVALID_FORMAT:
109 ret = AUDIO_IO_ERROR_INVALID_PARAMETER;
111 case CAudioError::EError::ERROR_DEVICE_NOT_OPENED:
112 ret = AUDIO_IO_ERROR_DEVICE_NOT_OPENED;
114 case CAudioError::EError::ERROR_DEVICE_NOT_CLOSED:
115 ret = AUDIO_IO_ERROR_DEVICE_NOT_CLOSED;
117 case CAudioError::EError::ERROR_PERMISSION_DENIED:
118 ret = AUDIO_IO_ERROR_PERMISSION_DENIED;
120 case CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION:
121 ret = AUDIO_IO_ERROR_DEVICE_POLICY_RESTRICTION;
123 case CAudioError::EError::ERROR_NOT_SUPPORTED:
124 ret = AUDIO_IO_ERROR_NOT_SUPPORTED;
126 case CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE:
127 ret = AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE;
129 case CAudioError::EError::ERROR_MAX:
130 case CAudioError::EError::ERROR_INTERNAL_OPERATION:
131 case CAudioError::EError::ERROR_NOT_INITIALIZED:
132 case CAudioError::EError::ERROR_FAILED_OPERATION:
133 case CAudioError::EError::ERROR_INVALID_OPERATION:
134 ret = AUDIO_IO_ERROR_INVALID_OPERATION;
136 case CAudioError::EError::ERROR_OUT_OF_MEMORY:
137 case CAudioError::EError::ERROR_INVALID_POINTER:
138 ret = AUDIO_IO_ERROR_INVALID_BUFFER;
140 case CAudioError::EError::ERROR_POLICY_BLOCKED:
141 case CAudioError::EError::ERROR_POLICY_INTERRUPTED:
142 case CAudioError::EError::ERROR_POLICY_DUPLICATED:
143 ret = AUDIO_IO_ERROR_SOUND_POLICY;
150 static void __convert_channel_2_audio_info_channel(const audio_channel_e& src_channel, CAudioInfo::EChannel& dst_channel) {
151 switch (src_channel) {
152 case AUDIO_CHANNEL_MONO:
153 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
155 case AUDIO_CHANNEL_STEREO:
156 dst_channel = CAudioInfo::EChannel::CHANNEL_STEREO;
159 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
163 static void __convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel, audio_channel_e& dst_channel) {
164 switch (src_channel) {
165 case CAudioInfo::EChannel::CHANNEL_MONO:
166 dst_channel = AUDIO_CHANNEL_MONO;
168 case CAudioInfo::EChannel::CHANNEL_STEREO:
169 dst_channel = AUDIO_CHANNEL_STEREO;
172 dst_channel = AUDIO_CHANNEL_MONO;
176 static void __convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type, CAudioInfo::ESampleType& dst_type) {
178 case AUDIO_SAMPLE_TYPE_U8:
179 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
181 case AUDIO_SAMPLE_TYPE_S16_LE:
182 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE;
185 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
189 static void __convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type, audio_sample_type_e& dst_type) {
191 case CAudioInfo::ESampleType::SAMPLE_TYPE_U8:
192 dst_type = AUDIO_SAMPLE_TYPE_U8;
194 case CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE:
195 dst_type = AUDIO_SAMPLE_TYPE_S16_LE;
198 dst_type = AUDIO_SAMPLE_TYPE_U8;
202 static void __convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type, CAudioInfo::EAudioType& dst_type) {
204 case SOUND_TYPE_SYSTEM:
205 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
207 case SOUND_TYPE_NOTIFICATION:
208 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION;
210 case SOUND_TYPE_ALARM:
211 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM;
213 case SOUND_TYPE_RINGTONE:
214 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP;
216 case SOUND_TYPE_MEDIA:
217 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
219 case SOUND_TYPE_CALL:
220 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
222 case SOUND_TYPE_VOIP:
223 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP;
225 case SOUND_TYPE_VOICE:
226 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION;
229 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
234 static void __convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type, sound_type_e& dst_type) {
236 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA:
237 dst_type = SOUND_TYPE_MEDIA;
239 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM:
240 dst_type = SOUND_TYPE_SYSTEM;
242 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM:
243 dst_type = SOUND_TYPE_ALARM;
245 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION:
246 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_EMERGENCY:
247 dst_type = SOUND_TYPE_NOTIFICATION;
249 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION:
250 dst_type = SOUND_TYPE_VOICE;
252 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP:
253 dst_type = SOUND_TYPE_RINGTONE;
255 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP:
256 dst_type = SOUND_TYPE_VOIP;
259 dst_type = SOUND_TYPE_MEDIA;
264 static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src_state) {
265 audio_io_state_e dst_state;
267 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE:
268 dst_state = AUDIO_IO_STATE_IDLE;
270 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE:
271 dst_state = AUDIO_IO_STATE_IDLE;
273 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING:
274 dst_state = AUDIO_IO_STATE_RUNNING;
276 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED:
277 dst_state = AUDIO_IO_STATE_PAUSED;
280 dst_state = AUDIO_IO_STATE_IDLE;
285 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) throw(CAudioError) {
286 if (sample_rate < 0) {
287 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate);
290 if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) {
291 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel);
294 if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE) {
295 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type);
299 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) throw(CAudioError) {
300 __check_audio_param(sample_rate, channel, type);
302 if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE) {
303 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type);
307 static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw(CAudioError) {
308 CAudioInfo::EChannel dstChannel;
309 CAudioInfo::ESampleType dstSampleType;
310 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
312 __convert_channel_2_audio_info_channel(channel, dstChannel);
313 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
315 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
318 static CAudioInfo __generate_audio_input_loopback_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw(CAudioError) {
319 CAudioInfo::EChannel dstChannel;
320 CAudioInfo::ESampleType dstSampleType;
321 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_LOOPBACK;
323 __convert_channel_2_audio_info_channel(channel, dstChannel);
324 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
326 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
329 static CAudioInfo __generate_audio_output_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type, sound_type_e sound_type) throw(CAudioError) {
330 CAudioInfo::EChannel dstChannel;
331 CAudioInfo::ESampleType dstSampleType;
332 CAudioInfo::EAudioType dstAudioType;
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);
338 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
341 static audio_io_interrupted_code_e __convert_interrupted_code(IAudioSessionEventListener::EInterruptCode code) {
343 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED:
344 return AUDIO_IO_INTERRUPTED_COMPLETED;
345 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_CALL:
346 return AUDIO_IO_INTERRUPTED_BY_CALL;
347 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EARJACK_UNPLUG:
348 return AUDIO_IO_INTERRUPTED_BY_EARJACK_UNPLUG;
349 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_RESOURCE_CONFLICT:
350 return AUDIO_IO_INTERRUPTED_BY_RESOURCE_CONFLICT;
351 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_ALARM:
352 return AUDIO_IO_INTERRUPTED_BY_ALARM;
353 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EMERGENCY:
354 return AUDIO_IO_INTERRUPTED_BY_EMERGENCY;
355 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_NOTIFICATION:
356 return AUDIO_IO_INTERRUPTED_BY_NOTIFICATION;
357 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_MEDIA:
358 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_MAX:
360 return AUDIO_IO_INTERRUPTED_BY_MEDIA;
365 * Implements CAPI functions
367 int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_in_h *input) {
368 audio_io_s* handle = NULL;
369 bool mic_enable = false;
373 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
376 __check_audio_param(sample_rate, channel, type);
378 /* If MIC is not supported, return NOT_SUPPORTED error */
379 ret = system_info_get_platform_bool(FEATURE_MICROPHONE, &mic_enable);
380 AUDIO_IO_LOGD("system_info_platform [%s]=[%d], ret[%d]", FEATURE_MICROPHONE, mic_enable, ret);
381 if (ret != SYSTEM_INFO_ERROR_NONE || !mic_enable) {
382 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "System doesn't support microphone!");
385 handle = new audio_io_s;
386 if (handle == NULL) {
387 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
390 CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type);
392 handle->audioIoHandle = new CAudioInput(audioInfo);
393 if (handle->audioIoHandle == NULL) {
394 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
397 handle->audioIoHandle->initialize();
400 } catch (CAudioError e) {
401 AUDIO_IO_LOGE("%s", e.getErrorMsg());
403 VALID_POINTER_START(handle)
404 SAFE_FINALIZE(handle->audioIoHandle);
405 SAFE_DELETE(handle->audioIoHandle);
409 VALID_POINTER_START(input)
413 return __convert_CAudioError(e);
416 return AUDIO_IO_ERROR_NONE;
419 int cpp_audio_in_create_loopback(int sample_rate, audio_channel_e channel, audio_sample_type_e type , audio_in_h* input) {
420 audio_io_s* handle = NULL;
423 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
426 __check_audio_param(sample_rate, channel, type);
428 handle = new audio_io_s;
429 if (handle == NULL) {
430 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
433 CAudioInfo audioInfo = __generate_audio_input_loopback_info(sample_rate, channel, type);
435 handle->audioIoHandle = new CAudioInput(audioInfo);
436 if (handle->audioIoHandle == NULL) {
437 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
440 handle->audioIoHandle->initialize();
443 } catch (CAudioError e) {
444 AUDIO_IO_LOGE("%s", e.getErrorMsg());
446 VALID_POINTER_START(handle)
447 SAFE_FINALIZE(handle->audioIoHandle);
448 SAFE_DELETE(handle->audioIoHandle);
452 VALID_POINTER_START(input)
456 return __convert_CAudioError(e);
459 return AUDIO_IO_ERROR_NONE;
462 int cpp_audio_in_destroy(audio_in_h input) {
463 audio_io_s* handle = static_cast<audio_io_s*>(input);
466 if (handle == NULL) {
467 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
470 assert(handle->audioIoHandle);
472 SAFE_FINALIZE(handle->audioIoHandle);
473 SAFE_DELETE(handle->audioIoHandle);
475 } catch (CAudioError e) {
476 AUDIO_IO_LOGE("%s", e.getErrorMsg());
477 return __convert_CAudioError(e);
480 return AUDIO_IO_ERROR_NONE;
483 int cpp_audio_in_set_stream_info(audio_in_h input, sound_stream_info_h stream_info) {
484 audio_io_s* handle = static_cast<audio_io_s*>(input);
487 if (handle == NULL || stream_info == NULL) {
488 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, stream_info:%p", input, stream_info);
491 assert(handle->audioIoHandle);
493 int errorCode = SOUND_MANAGER_ERROR_NONE;
494 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
499 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
500 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
504 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
505 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
507 handle->audioIoHandle->getAudioInfo().convertInputStreamType2AudioType(type, &audioType);
508 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
510 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
511 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
513 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
515 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported");
517 } catch (CAudioError e) {
518 AUDIO_IO_LOGE("%s", e.getErrorMsg());
519 return __convert_CAudioError(e);
522 return AUDIO_IO_ERROR_NONE;
525 int cpp_audio_in_prepare(audio_in_h input) {
526 audio_io_s* handle = static_cast<audio_io_s*>(input);
529 if (handle == NULL) {
530 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
533 assert(handle->audioIoHandle);
535 handle->audioIoHandle->prepare();
536 } catch (CAudioError e) {
537 AUDIO_IO_LOGE("%s", e.getErrorMsg());
538 return __convert_CAudioError(e);
541 return AUDIO_IO_ERROR_NONE;
544 int cpp_audio_in_unprepare(audio_in_h input) {
545 audio_io_s* handle = static_cast<audio_io_s*>(input);
548 if (handle == NULL) {
549 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
552 assert(handle->audioIoHandle);
554 handle->audioIoHandle->unprepare();
555 } catch (CAudioError e) {
556 AUDIO_IO_LOGE("%s", e.getErrorMsg());
557 return __convert_CAudioError(e);
560 return AUDIO_IO_ERROR_NONE;
563 int cpp_audio_in_pause(audio_in_h input) {
564 audio_io_s* handle = static_cast<audio_io_s*>(input);
567 if (handle == NULL) {
568 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
571 assert(handle->audioIoHandle);
573 handle->audioIoHandle->pause();
574 } catch (CAudioError e) {
575 AUDIO_IO_LOGE("%s", e.getErrorMsg());
576 return __convert_CAudioError(e);
579 return AUDIO_IO_ERROR_NONE;
582 int cpp_audio_in_resume(audio_in_h input) {
583 audio_io_s* handle = static_cast<audio_io_s*>(input);
586 if (handle == NULL) {
587 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
590 assert(handle->audioIoHandle);
592 handle->audioIoHandle->resume();
593 } catch (CAudioError e) {
594 AUDIO_IO_LOGE("%s", e.getErrorMsg());
595 return __convert_CAudioError(e);
598 return AUDIO_IO_ERROR_NONE;
601 int cpp_audio_in_drain(audio_in_h input) {
602 audio_io_s* handle = static_cast<audio_io_s*>(input);
605 if (handle == NULL) {
606 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
609 assert(handle->audioIoHandle);
611 handle->audioIoHandle->drain();
612 } catch (CAudioError e) {
613 AUDIO_IO_LOGE("%s", e.getErrorMsg());
614 return __convert_CAudioError(e);
617 return AUDIO_IO_ERROR_NONE;
620 int cpp_audio_in_flush(audio_in_h input) {
621 audio_io_s* handle = static_cast<audio_io_s*>(input);
624 if (handle == NULL) {
625 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
628 assert(handle->audioIoHandle);
630 handle->audioIoHandle->flush();
631 } catch (CAudioError e) {
632 AUDIO_IO_LOGE("%s", e.getErrorMsg());
633 return __convert_CAudioError(e);
636 return AUDIO_IO_ERROR_NONE;
639 int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) {
640 audio_io_s* handle = static_cast<audio_io_s*>(input);
644 if (handle == NULL || buffer == NULL) {
645 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
648 assert(handle->audioIoHandle);
650 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
651 if (inputHandle == NULL) {
652 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
654 size_t readn = inputHandle->read(buffer, static_cast<size_t>(length));
655 ret = static_cast<int>(readn);
656 #ifdef _AUDIO_IO_DEBUG_TIMING_
657 AUDIO_IO_LOGD("readn:%d", readn);
659 } catch (CAudioError e) {
660 AUDIO_IO_LOGE("%s", e.getErrorMsg());
661 return __convert_CAudioError(e);
667 int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) {
668 audio_io_s* handle = static_cast<audio_io_s*>(input);
671 if (handle == NULL || size == NULL) {
672 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, size:%p", input, size);
675 assert(handle->audioIoHandle);
677 CAudioIO* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
678 if (inputHandle == NULL) {
679 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
681 *size = inputHandle->getBufferSize();
682 } catch (CAudioError e) {
683 AUDIO_IO_LOGE("%s", e.getErrorMsg());
684 return __convert_CAudioError(e);
687 return AUDIO_IO_ERROR_NONE;
690 int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) {
691 audio_io_s* handle = static_cast<audio_io_s*>(input);
694 if (handle == NULL || sample_rate == NULL) {
695 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
698 assert(handle->audioIoHandle);
699 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
700 } catch (CAudioError e) {
701 AUDIO_IO_LOGE("%s", e.getErrorMsg());
702 return __convert_CAudioError(e);
705 return AUDIO_IO_ERROR_NONE;
708 int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
709 audio_io_s* handle = static_cast<audio_io_s*>(input);
712 if (handle == NULL || channel == NULL) {
713 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, channel:%p", input, channel);
716 assert(handle->audioIoHandle);
718 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
719 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
720 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
722 *channel = dstChannel;
723 } catch (CAudioError e) {
724 AUDIO_IO_LOGE("%s", e.getErrorMsg());
725 return __convert_CAudioError(e);
728 return AUDIO_IO_ERROR_NONE;
731 int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
732 audio_io_s* handle = static_cast<audio_io_s*>(input);
735 if (handle == NULL || type == NULL) {
736 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, type:%p", input, type);
739 assert(handle->audioIoHandle);
741 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
742 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
743 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
745 *type = dstSampleType;
746 } catch (CAudioError e) {
747 AUDIO_IO_LOGE("%s", e.getErrorMsg());
748 return __convert_CAudioError(e);
751 return AUDIO_IO_ERROR_NONE;
754 static void __interrupt_cb_internal(IAudioSessionEventListener::EInterruptCode _code, void* user_data) {
755 audio_io_s* handle = static_cast<audio_io_s*>(user_data);
756 audio_io_interrupted_code_e code = __convert_interrupted_code(_code);
760 if (handle->interrupt_callback.onInterrupt != NULL) {
761 handle->interrupt_callback.onInterrupt(code, handle->interrupt_callback.user_data);
765 int cpp_audio_in_set_interrupted_cb(audio_in_h input, audio_io_interrupted_cb callback, void *user_data) {
766 audio_io_s* handle = static_cast<audio_io_s*>(input);
769 if (handle == NULL || callback == NULL) {
770 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
773 assert(handle->audioIoHandle);
775 handle->interrupt_callback.onInterrupt = callback;
776 handle->interrupt_callback.user_data = user_data;
778 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
779 cb.mUserData = static_cast<void*>(handle);
780 cb.onInterrupt = __interrupt_cb_internal;
782 handle->audioIoHandle->setInterruptCallback(cb);
783 } catch (CAudioError e) {
784 AUDIO_IO_LOGE("%s", e.getErrorMsg());
785 return __convert_CAudioError(e);
788 return AUDIO_IO_ERROR_NONE;
791 int cpp_audio_in_unset_interrupted_cb(audio_in_h input) {
792 audio_io_s* handle = static_cast<audio_io_s*>(input);
795 if (handle == NULL) {
796 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
799 assert(handle->audioIoHandle);
801 handle->interrupt_callback.onInterrupt = NULL;
802 handle->interrupt_callback.user_data = NULL;
804 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
806 cb.onInterrupt = NULL;
808 handle->audioIoHandle->setInterruptCallback(cb);
809 } catch (CAudioError e) {
810 AUDIO_IO_LOGE("%s", e.getErrorMsg());
811 return __convert_CAudioError(e);
814 return AUDIO_IO_ERROR_NONE;
817 int cpp_audio_in_ignore_session(audio_in_h input) {
818 audio_io_s* handle = static_cast<audio_io_s*>(input);
821 if (handle == NULL) {
822 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
825 if (handle->stream_callback.onStream) {
826 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
829 assert(handle->audioIoHandle);
831 handle->audioIoHandle->ignoreSession();
832 } catch (CAudioError e) {
833 AUDIO_IO_LOGE("%s", e.getErrorMsg());
834 return __convert_CAudioError(e);
837 return AUDIO_IO_ERROR_NONE;
840 static void __stream_cb_internal(size_t nbytes, void *user_data) {
841 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
844 if (audioIo->stream_callback.onStream != NULL) {
845 audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data);
849 static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState state_prev, bool by_policy, void *user_data) {
850 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
853 if (audioIo->state_changed_callback.onStateChanged != NULL) {
854 audioIo->state_changed_callback.onStateChanged(audioIo, __convert_state_type(state_prev), __convert_state_type(state), by_policy, audioIo->state_changed_callback.user_data);
858 int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) {
859 audio_io_s* handle = static_cast<audio_io_s*>(input);
862 if (handle == NULL || callback == NULL) {
863 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
866 assert(handle->audioIoHandle);
868 handle->stream_callback.onStream = callback;
869 handle->stream_callback.user_data = user_data;
871 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
872 cb.mUserData = static_cast<void*>(handle);
873 cb.onStream = __stream_cb_internal;
875 handle->audioIoHandle->setStreamCallback(cb);
876 } catch (CAudioError e) {
877 AUDIO_IO_LOGE("%s", e.getErrorMsg());
878 return __convert_CAudioError(e);
881 return AUDIO_IO_ERROR_NONE;
884 int cpp_audio_in_unset_stream_cb(audio_in_h input) {
885 audio_io_s* handle = static_cast<audio_io_s*>(input);
888 if (handle == NULL) {
889 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
892 assert(handle->audioIoHandle);
894 handle->stream_callback.onStream = NULL;
895 handle->stream_callback.user_data = NULL;
897 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
901 handle->audioIoHandle->setStreamCallback(cb);
902 } catch (CAudioError e) {
903 AUDIO_IO_LOGE("%s", e.getErrorMsg());
904 return __convert_CAudioError(e);
907 return AUDIO_IO_ERROR_NONE;
910 int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) {
911 audio_io_s* handle = static_cast<audio_io_s*>(input);
915 if (handle == NULL || buffer == NULL) {
916 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
919 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
922 inputHandle->peek(buffer, &_length);
923 } catch (CAudioError e) {
924 AUDIO_IO_LOGE("%s", e.getErrorMsg());
925 return __convert_CAudioError(e);
928 *length = (unsigned int)_length;
930 return AUDIO_IO_ERROR_NONE;
933 int cpp_audio_in_drop(audio_in_h input) {
934 audio_io_s* handle = static_cast<audio_io_s*>(input);
937 if (handle == NULL) {
938 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
941 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
945 } catch (CAudioError e) {
946 AUDIO_IO_LOGE("%s", e.getErrorMsg());
947 return __convert_CAudioError(e);
950 return AUDIO_IO_ERROR_NONE;
953 int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) {
954 audio_io_s* handle = static_cast<audio_io_s*>(input);
957 if (handle == NULL || callback == NULL) {
958 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
961 assert(handle->audioIoHandle);
963 handle->state_changed_callback.onStateChanged = callback;
964 handle->state_changed_callback.user_data = user_data;
966 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
967 cb.mUserData = static_cast<void*>(handle);
968 cb.onStateChanged = __state_changed_cb_internal;
970 handle->audioIoHandle->setStateChangedCallback(cb);
971 } catch (CAudioError e) {
972 AUDIO_IO_LOGE("%s", e.getErrorMsg());
973 return __convert_CAudioError(e);
976 return AUDIO_IO_ERROR_NONE;
979 int cpp_audio_in_unset_state_changed_cb(audio_in_h input) {
980 audio_io_s* handle = static_cast<audio_io_s*>(input);
983 if (handle == NULL) {
984 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", input);
987 assert(handle->audioIoHandle);
989 handle->state_changed_callback.onStateChanged = NULL;
990 handle->state_changed_callback.user_data = NULL;
992 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
994 cb.onStateChanged = NULL;
996 handle->audioIoHandle->setStateChangedCallback(cb);
997 } catch (CAudioError e) {
998 AUDIO_IO_LOGE("%s", e.getErrorMsg());
999 return __convert_CAudioError(e);
1002 return AUDIO_IO_ERROR_NONE;
1009 int cpp_audio_out_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type, audio_out_h *output) {
1010 audio_io_s* handle = NULL;
1012 if (output == NULL) {
1013 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1016 __check_audio_param(sample_rate, channel, type, sound_type);
1018 handle = new audio_io_s;
1019 if (handle == NULL) {
1020 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1023 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, sound_type);
1025 handle->audioIoHandle = new CAudioOutput(audioInfo);
1026 if (handle == NULL) {
1027 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1030 handle->audioIoHandle->initialize();
1033 } catch (CAudioError e) {
1034 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1036 VALID_POINTER_START(handle)
1037 SAFE_FINALIZE(handle->audioIoHandle);
1038 SAFE_DELETE(handle->audioIoHandle);
1039 SAFE_DELETE(handle);
1042 VALID_POINTER_START(output)
1046 return __convert_CAudioError(e);
1049 return AUDIO_IO_ERROR_NONE;
1052 int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) {
1053 audio_io_s* handle = NULL;
1055 if (output == NULL) {
1056 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1059 __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */);
1061 handle = new audio_io_s;
1062 if (handle == NULL) {
1063 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1066 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA /* default sound_type */);
1068 handle->audioIoHandle = new CAudioOutput(audioInfo);
1069 if (handle->audioIoHandle == NULL) {
1070 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1073 handle->audioIoHandle->initialize();
1076 } catch (CAudioError e) {
1077 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1079 VALID_POINTER_START(handle)
1080 SAFE_FINALIZE(handle->audioIoHandle);
1081 SAFE_DELETE(handle->audioIoHandle);
1082 SAFE_DELETE(handle);
1085 VALID_POINTER_START(output)
1089 return __convert_CAudioError(e);
1092 return AUDIO_IO_ERROR_NONE;
1095 int cpp_audio_out_destroy(audio_out_h output) {
1096 audio_io_s* handle = static_cast<audio_io_s*>(output);
1099 if (handle == NULL) {
1100 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1103 assert(handle->audioIoHandle);
1105 SAFE_FINALIZE(handle->audioIoHandle);
1106 SAFE_DELETE(handle->audioIoHandle);
1107 SAFE_DELETE(handle);
1108 } catch (CAudioError e) {
1109 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1110 return __convert_CAudioError(e);
1113 return AUDIO_IO_ERROR_NONE;
1116 int cpp_audio_out_set_stream_info(audio_out_h output, sound_stream_info_h stream_info) {
1117 audio_io_s* handle = static_cast<audio_io_s*>(output);
1120 if (handle == NULL || stream_info == NULL) {
1121 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, stream_info:%p", output, stream_info);
1124 assert(handle->audioIoHandle);
1126 int errorCode = SOUND_MANAGER_ERROR_NONE;
1127 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
1132 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
1133 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
1137 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
1138 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
1140 handle->audioIoHandle->getAudioInfo().convertOutputStreamType2AudioType(type, &audioType);
1141 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
1143 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
1144 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
1146 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
1148 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Output stream is not supported");
1150 } catch (CAudioError e) {
1151 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1152 return __convert_CAudioError(e);
1155 return AUDIO_IO_ERROR_NONE;
1158 int cpp_audio_out_prepare(audio_out_h output) {
1159 audio_io_s* handle = static_cast<audio_io_s*>(output);
1162 if (handle == NULL) {
1163 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1166 assert(handle->audioIoHandle);
1168 handle->audioIoHandle->prepare();
1169 } catch (CAudioError e) {
1170 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1171 return __convert_CAudioError(e);
1174 return AUDIO_IO_ERROR_NONE;
1177 int cpp_audio_out_unprepare(audio_out_h output) {
1178 audio_io_s* handle = static_cast<audio_io_s*>(output);
1181 if (handle == NULL) {
1182 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1185 assert(handle->audioIoHandle);
1187 handle->audioIoHandle->unprepare();
1188 } catch (CAudioError e) {
1189 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1190 return __convert_CAudioError(e);
1193 return AUDIO_IO_ERROR_NONE;
1196 int cpp_audio_out_pause(audio_out_h output) {
1197 audio_io_s* handle = static_cast<audio_io_s*>(output);
1200 if (handle == NULL) {
1201 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1204 assert(handle->audioIoHandle);
1206 handle->audioIoHandle->pause();
1207 } catch (CAudioError e) {
1208 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1209 return __convert_CAudioError(e);
1212 return AUDIO_IO_ERROR_NONE;
1215 int cpp_audio_out_resume(audio_out_h output) {
1216 audio_io_s* handle = static_cast<audio_io_s*>(output);
1219 if (handle == NULL) {
1220 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1223 assert(handle->audioIoHandle);
1225 handle->audioIoHandle->resume();
1226 } catch (CAudioError e) {
1227 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1228 return __convert_CAudioError(e);
1231 return AUDIO_IO_ERROR_NONE;
1234 int cpp_audio_out_drain(audio_out_h output) {
1235 audio_io_s* handle = static_cast<audio_io_s*>(output);
1238 if (handle == NULL) {
1239 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1242 assert(handle->audioIoHandle);
1244 handle->audioIoHandle->drain();
1245 } catch (CAudioError e) {
1246 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1247 return __convert_CAudioError(e);
1250 return AUDIO_IO_ERROR_NONE;
1253 int cpp_audio_out_flush(audio_out_h output) {
1254 audio_io_s* handle = static_cast<audio_io_s*>(output);
1257 if (handle == NULL) {
1258 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1261 assert(handle->audioIoHandle);
1263 handle->audioIoHandle->flush();
1264 } catch (CAudioError e) {
1265 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1266 return __convert_CAudioError(e);
1269 return AUDIO_IO_ERROR_NONE;
1272 int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) {
1273 audio_io_s* handle = static_cast<audio_io_s*>(output);
1277 if (handle == NULL || buffer == NULL) {
1278 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p, buffer:%p", output, buffer);
1281 assert(handle->audioIoHandle);
1283 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1284 if (outputHandle == NULL) {
1285 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1287 size_t written = outputHandle->write(buffer, static_cast<size_t>(length));
1288 ret = static_cast<int>(written);
1289 #ifdef _AUDIO_IO_DEBUG_TIMING_
1290 AUDIO_IO_LOGD("written:%d", written);
1292 } catch (CAudioError e) {
1293 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1294 return __convert_CAudioError(e);
1300 int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) {
1301 audio_io_s* handle = static_cast<audio_io_s*>(output);
1304 if (handle == NULL || size == NULL) {
1305 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, size:%p", output, size);
1308 assert(handle->audioIoHandle);
1310 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1311 if (outputHandle == NULL) {
1312 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1314 *size = outputHandle->getBufferSize();
1315 } catch (CAudioError e) {
1316 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1317 return __convert_CAudioError(e);
1320 return AUDIO_IO_ERROR_NONE;
1323 int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
1324 audio_io_s* handle = static_cast<audio_io_s*>(output);
1327 if (handle == NULL || sample_rate == NULL) {
1328 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
1331 assert(handle->audioIoHandle);
1332 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
1333 } catch (CAudioError e) {
1334 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1335 return __convert_CAudioError(e);
1338 return AUDIO_IO_ERROR_NONE;
1341 int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
1342 audio_io_s* handle = static_cast<audio_io_s*>(output);
1345 if (handle == NULL || channel == NULL) {
1346 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, channel:%p", output, channel);
1349 assert(handle->audioIoHandle);
1351 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
1352 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
1353 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
1355 *channel = dstChannel;
1356 } catch (CAudioError e) {
1357 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1358 return __convert_CAudioError(e);
1361 return AUDIO_IO_ERROR_NONE;
1364 int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) {
1365 audio_io_s* handle = static_cast<audio_io_s*>(output);
1368 if (handle == NULL || type == NULL) {
1369 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1372 assert(handle->audioIoHandle);
1374 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
1375 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
1376 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
1378 *type = dstSampleType;
1379 } catch (CAudioError e) {
1380 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1381 return __convert_CAudioError(e);
1384 return AUDIO_IO_ERROR_NONE;
1387 int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) {
1388 audio_io_s* handle = static_cast<audio_io_s*>(output);
1391 if (handle == NULL || type == NULL) {
1392 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1395 assert(handle->audioIoHandle);
1397 const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType();
1398 sound_type_e dstSoundType = SOUND_TYPE_MEDIA;
1399 __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType);
1401 *type = dstSoundType;
1402 } catch (CAudioError e) {
1403 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1404 return __convert_CAudioError(e);
1407 return AUDIO_IO_ERROR_NONE;
1410 int cpp_audio_out_set_interrupted_cb(audio_out_h output, audio_io_interrupted_cb callback, void *user_data) {
1411 audio_io_s* handle = static_cast<audio_io_s*>(output);
1414 if (handle == NULL || callback == NULL) {
1415 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1418 assert(handle->audioIoHandle);
1420 handle->interrupt_callback.onInterrupt = callback;
1421 handle->interrupt_callback.user_data = user_data;
1423 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1424 cb.mUserData = static_cast<void*>(handle);
1425 cb.onInterrupt = __interrupt_cb_internal;
1427 handle->audioIoHandle->setInterruptCallback(cb);
1428 } catch (CAudioError e) {
1429 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1430 return __convert_CAudioError(e);
1433 return AUDIO_IO_ERROR_NONE;
1436 int cpp_audio_out_unset_interrupted_cb(audio_out_h output) {
1437 audio_io_s* handle = static_cast<audio_io_s*>(output);
1440 if (handle == NULL) {
1441 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1444 assert(handle->audioIoHandle);
1446 handle->interrupt_callback.onInterrupt = NULL;
1447 handle->interrupt_callback.user_data = NULL;
1449 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1450 cb.mUserData = NULL;
1451 cb.onInterrupt = NULL;
1453 handle->audioIoHandle->setInterruptCallback(cb);
1454 } catch (CAudioError e) {
1455 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1456 return __convert_CAudioError(e);
1459 return AUDIO_IO_ERROR_NONE;
1462 int cpp_audio_out_ignore_session(audio_out_h output) {
1463 audio_io_s* handle = static_cast<audio_io_s*>(output);
1466 if (handle == NULL) {
1467 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1470 if (handle->stream_callback.onStream) {
1471 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
1474 assert(handle->audioIoHandle);
1476 handle->audioIoHandle->ignoreSession();
1477 } catch (CAudioError e) {
1478 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1479 return __convert_CAudioError(e);
1482 return AUDIO_IO_ERROR_NONE;
1485 int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) {
1486 audio_io_s* handle = static_cast<audio_io_s*>(output);
1489 if (handle == NULL || callback == NULL) {
1490 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1493 assert(handle->audioIoHandle);
1495 handle->stream_callback.onStream = callback;
1496 handle->stream_callback.user_data = user_data;
1498 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1499 cb.mUserData = static_cast<void*>(handle);
1500 cb.onStream = __stream_cb_internal;
1502 handle->audioIoHandle->setStreamCallback(cb);
1503 } catch (CAudioError e) {
1504 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1505 return __convert_CAudioError(e);
1508 return AUDIO_IO_ERROR_NONE;
1511 int cpp_audio_out_unset_stream_cb(audio_out_h output) {
1512 audio_io_s* handle = static_cast<audio_io_s*>(output);
1515 if (handle == NULL) {
1516 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1519 assert(handle->audioIoHandle);
1521 handle->stream_callback.onStream = NULL;
1522 handle->stream_callback.user_data = NULL;
1524 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1525 cb.mUserData = NULL;
1528 handle->audioIoHandle->setStreamCallback(cb);
1529 } catch (CAudioError e) {
1530 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1531 return __convert_CAudioError(e);
1534 return AUDIO_IO_ERROR_NONE;
1537 int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) {
1538 audio_io_s* handle = static_cast<audio_io_s*>(output);
1541 if (handle == NULL || callback == NULL) {
1542 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1545 assert(handle->audioIoHandle);
1547 handle->state_changed_callback.onStateChanged = callback;
1548 handle->state_changed_callback.user_data = user_data;
1550 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1551 cb.mUserData = static_cast<void*>(handle);
1552 cb.onStateChanged = __state_changed_cb_internal;
1554 handle->audioIoHandle->setStateChangedCallback(cb);
1555 } catch (CAudioError e) {
1556 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1557 return __convert_CAudioError(e);
1560 return AUDIO_IO_ERROR_NONE;
1563 int cpp_audio_out_unset_state_changed_cb(audio_out_h output) {
1564 audio_io_s* handle = static_cast<audio_io_s*>(output);
1567 if (handle == NULL) {
1568 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1571 assert(handle->audioIoHandle);
1573 handle->state_changed_callback.onStateChanged = NULL;
1574 handle->state_changed_callback.user_data = NULL;
1576 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1577 cb.mUserData = NULL;
1578 cb.onStateChanged = NULL;
1580 handle->audioIoHandle->setStateChangedCallback(cb);
1581 } catch (CAudioError e) {
1582 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1583 return __convert_CAudioError(e);
1586 return AUDIO_IO_ERROR_NONE;