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"
25 using namespace tizen_media_audio;
31 * Name : audio_io_interrupted_cb_s
32 * Declaration : Keeps user callback pointer and user data for delivering an interrupt event
34 typedef struct audio_io_interrupted_cb_s {
36 audio_io_interrupted_cb onInterrupt;
38 audio_io_interrupted_cb_s() : user_data(NULL), onInterrupt(NULL)
40 } audio_io_interrupted_cb_s;
45 * Name : audio_io_stream_cb_s
46 * Declaration : Keeps user callback pointer and user data for delivering an stream event
48 typedef struct audio_io_stream_cb_s {
50 audio_in_stream_cb onStream;
52 audio_io_stream_cb_s() : user_data(NULL), onStream(NULL)
54 } audio_io_stream_cb_s;
59 * Name : audio_io_state_changed_cb_s
60 * Declaration : Keeps user callback pointer and user data for delivering an state changed event
62 typedef struct audio_io_state_changed_cb_s {
64 audio_in_state_changed_cb onStateChanged;
66 audio_io_state_changed_cb_s() : user_data(NULL), onStateChanged(NULL)
68 } audio_io_state_changed_cb_s;
74 * Declaration : An handle of AudioIO
75 * The handle has two struct for user callback
76 * And the handle has a pointer of private audioIO object
77 * The CAudioIO is a abstract class object about Input and Output
79 typedef struct audio_io_s {
80 CAudioIO* audioIoHandle;
81 audio_io_interrupted_cb_s interrupt_callback;
82 audio_io_stream_cb_s stream_callback;
83 audio_io_state_changed_cb_s state_changed_callback;
85 audio_io_s() : audioIoHandle(NULL)
93 static audio_io_error_e __convert_CAudioError(CAudioError& error) {
94 audio_io_error_e ret = AUDIO_IO_ERROR_NONE;
95 CAudioError::EError err = error.getError();
99 case CAudioError::EError::ERROR_NONE:
100 ret = AUDIO_IO_ERROR_NONE;
102 case CAudioError::EError::ERROR_INVALID_ARGUMENT:
103 case CAudioError::EError::ERROR_INVALID_HANDLE:
104 case CAudioError::EError::ERROR_INVALID_SAMPLERATE:
105 case CAudioError::EError::ERROR_INVALID_CHANNEL:
106 case CAudioError::EError::ERROR_INVALID_FORMAT:
107 ret = AUDIO_IO_ERROR_INVALID_PARAMETER;
109 case CAudioError::EError::ERROR_DEVICE_NOT_OPENED:
110 ret = AUDIO_IO_ERROR_DEVICE_NOT_OPENED;
112 case CAudioError::EError::ERROR_DEVICE_NOT_CLOSED:
113 ret = AUDIO_IO_ERROR_DEVICE_NOT_CLOSED;
115 case CAudioError::EError::ERROR_PERMISSION_DENIED:
116 ret = AUDIO_IO_ERROR_PERMISSION_DENIED;
118 case CAudioError::EError::ERROR_NOT_SUPPORTED:
119 ret = AUDIO_IO_ERROR_NOT_SUPPORTED;
121 case CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE:
122 ret = AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE;
124 case CAudioError::EError::ERROR_MAX:
125 case CAudioError::EError::ERROR_INTERNAL_OPERATION:
126 case CAudioError::EError::ERROR_NOT_INITIALIZED:
127 case CAudioError::EError::ERROR_FAILED_OPERATION:
128 case CAudioError::EError::ERROR_INVALID_OPERATION:
129 ret = AUDIO_IO_ERROR_INVALID_OPERATION;
131 case CAudioError::EError::ERROR_OUT_OF_MEMORY:
132 case CAudioError::EError::ERROR_INVALID_POINTER:
133 ret = AUDIO_IO_ERROR_INVALID_BUFFER;
135 case CAudioError::EError::ERROR_POLICY_BLOCKED:
136 case CAudioError::EError::ERROR_POLICY_INTERRUPTED:
137 case CAudioError::EError::ERROR_POLICY_DUPLICATED:
138 ret = AUDIO_IO_ERROR_SOUND_POLICY;
145 static void __convert_channel_2_audio_info_channel(const audio_channel_e& src_channel, CAudioInfo::EChannel& dst_channel) {
146 switch (src_channel) {
147 case AUDIO_CHANNEL_MONO:
148 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
150 case AUDIO_CHANNEL_STEREO:
151 dst_channel = CAudioInfo::EChannel::CHANNEL_STEREO;
154 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
158 static void __convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel, audio_channel_e& dst_channel) {
159 switch (src_channel) {
160 case CAudioInfo::EChannel::CHANNEL_MONO:
161 dst_channel = AUDIO_CHANNEL_MONO;
163 case CAudioInfo::EChannel::CHANNEL_STEREO:
164 dst_channel = AUDIO_CHANNEL_STEREO;
167 dst_channel = AUDIO_CHANNEL_MONO;
171 static void __convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type, CAudioInfo::ESampleType& dst_type) {
173 case AUDIO_SAMPLE_TYPE_U8:
174 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
176 case AUDIO_SAMPLE_TYPE_S16_LE:
177 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE;
180 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
184 static void __convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type, audio_sample_type_e& dst_type) {
186 case CAudioInfo::ESampleType::SAMPLE_TYPE_U8:
187 dst_type = AUDIO_SAMPLE_TYPE_U8;
189 case CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE:
190 dst_type = AUDIO_SAMPLE_TYPE_S16_LE;
193 dst_type = AUDIO_SAMPLE_TYPE_U8;
197 static void __convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type, CAudioInfo::EAudioType& dst_type) {
199 case SOUND_TYPE_SYSTEM:
200 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
202 case SOUND_TYPE_NOTIFICATION:
203 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION;
205 case SOUND_TYPE_ALARM:
206 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM;
208 case SOUND_TYPE_RINGTONE:
209 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP;
211 case SOUND_TYPE_MEDIA:
212 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
214 case SOUND_TYPE_CALL:
215 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
217 case SOUND_TYPE_VOIP:
218 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP;
220 case SOUND_TYPE_VOICE:
221 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION;
224 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
229 static void __convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type, sound_type_e& dst_type) {
231 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA:
232 dst_type = SOUND_TYPE_MEDIA;
234 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM:
235 dst_type = SOUND_TYPE_SYSTEM;
237 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM:
238 dst_type = SOUND_TYPE_ALARM;
240 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION:
241 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_EMERGENCY:
242 dst_type = SOUND_TYPE_NOTIFICATION;
244 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION:
245 dst_type = SOUND_TYPE_VOICE;
247 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP:
248 dst_type = SOUND_TYPE_RINGTONE;
250 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP:
251 dst_type = SOUND_TYPE_VOIP;
254 dst_type = SOUND_TYPE_MEDIA;
259 static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src_state) {
260 audio_io_state_e dst_state;
262 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE:
263 dst_state = AUDIO_IO_STATE_IDLE;
265 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE:
266 dst_state = AUDIO_IO_STATE_IDLE;
268 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING:
269 dst_state = AUDIO_IO_STATE_RUNNING;
271 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED:
272 dst_state = AUDIO_IO_STATE_PAUSED;
275 dst_state = AUDIO_IO_STATE_IDLE;
280 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) throw (CAudioError) {
281 if (sample_rate < 0) {
282 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate);
285 if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) {
286 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel);
289 if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE) {
290 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type);
294 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) throw (CAudioError) {
295 __check_audio_param(sample_rate, channel, type);
297 if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE) {
298 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type);
302 static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw (CAudioError) {
303 CAudioInfo::EChannel dstChannel;
304 CAudioInfo::ESampleType dstSampleType;
305 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
307 __convert_channel_2_audio_info_channel(channel, dstChannel);
308 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
310 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
313 static CAudioInfo __generate_audio_input_loopback_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw (CAudioError) {
314 CAudioInfo::EChannel dstChannel;
315 CAudioInfo::ESampleType dstSampleType;
316 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_LOOPBACK;
318 __convert_channel_2_audio_info_channel(channel, dstChannel);
319 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
321 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
324 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) {
325 CAudioInfo::EChannel dstChannel;
326 CAudioInfo::ESampleType dstSampleType;
327 CAudioInfo::EAudioType dstAudioType;
329 __convert_channel_2_audio_info_channel(channel, dstChannel);
330 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
331 __convert_sound_type_2_audio_info_audio_type(sound_type, dstAudioType);
333 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
336 static audio_io_interrupted_code_e __convert_interrupted_code(IAudioSessionEventListener::EInterruptCode code) {
338 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED:
339 return AUDIO_IO_INTERRUPTED_COMPLETED;
340 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_CALL:
341 return AUDIO_IO_INTERRUPTED_BY_CALL;
342 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EARJACK_UNPLUG:
343 return AUDIO_IO_INTERRUPTED_BY_EARJACK_UNPLUG;
344 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_RESOURCE_CONFLICT:
345 return AUDIO_IO_INTERRUPTED_BY_RESOURCE_CONFLICT;
346 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_ALARM:
347 return AUDIO_IO_INTERRUPTED_BY_ALARM;
348 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EMERGENCY:
349 return AUDIO_IO_INTERRUPTED_BY_EMERGENCY;
350 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_NOTIFICATION:
351 return AUDIO_IO_INTERRUPTED_BY_NOTIFICATION;
352 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_MEDIA:
353 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_MAX:
355 return AUDIO_IO_INTERRUPTED_BY_MEDIA;
360 * Implements CAPI functions
362 int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_in_h *input) {
363 audio_io_s* handle = NULL;
366 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
369 __check_audio_param(sample_rate, channel, type);
371 handle = new audio_io_s;
372 if (handle == NULL) {
373 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
376 CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type);
378 handle->audioIoHandle = new CAudioInput(audioInfo);
379 if (handle == NULL) {
380 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
383 handle->audioIoHandle->initialize();
386 } catch (CAudioError e) {
387 AUDIO_IO_LOGE("%s", e.getErrorMsg());
389 VALID_POINTER_START(handle)
390 SAFE_FINALIZE(handle->audioIoHandle);
391 SAFE_DELETE(handle->audioIoHandle);
395 VALID_POINTER_START(input)
399 return __convert_CAudioError(e);
402 return AUDIO_IO_ERROR_NONE;
406 int cpp_audio_in_create_loopback(int sample_rate, audio_channel_e channel, audio_sample_type_e type , audio_in_h* input) {
407 audio_io_s* handle = NULL;
410 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
413 __check_audio_param(sample_rate, channel, type);
415 handle = new audio_io_s;
416 if (handle == NULL) {
417 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
420 CAudioInfo audioInfo = __generate_audio_input_loopback_info(sample_rate, channel, type);
422 handle->audioIoHandle = new CAudioInput(audioInfo);
423 if (handle == NULL) {
424 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
427 handle->audioIoHandle->initialize();
430 } catch (CAudioError e) {
431 AUDIO_IO_LOGE("%s", e.getErrorMsg());
433 VALID_POINTER_START(handle)
434 SAFE_FINALIZE(handle->audioIoHandle);
435 SAFE_DELETE(handle->audioIoHandle);
439 VALID_POINTER_START(input)
443 return __convert_CAudioError(e);
446 return AUDIO_IO_ERROR_NONE;
449 int cpp_audio_in_destroy(audio_in_h input) {
450 audio_io_s* handle = static_cast<audio_io_s*>(input);
453 if (handle == NULL) {
454 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
457 assert(handle->audioIoHandle);
459 SAFE_FINALIZE(handle->audioIoHandle);
460 SAFE_DELETE(handle->audioIoHandle);
462 } catch (CAudioError e) {
463 AUDIO_IO_LOGE("%s", e.getErrorMsg());
464 return __convert_CAudioError(e);
467 return AUDIO_IO_ERROR_NONE;
470 int cpp_audio_in_set_stream_info(audio_in_h input, sound_stream_info_h stream_info) {
471 audio_io_s* handle = static_cast<audio_io_s*>(input);
474 if (handle == NULL || stream_info == NULL) {
475 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, stream_info:%p", input, stream_info);
478 assert(handle->audioIoHandle);
480 int errorCode = SOUND_MANAGER_ERROR_NONE;
481 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
486 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
487 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
491 if ((errorCode = sound_manager_get_type_from_stream_information (stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
492 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
494 handle->audioIoHandle->getAudioInfo().convertInputStreamType2AudioType(type, &audioType);
495 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
497 if ((errorCode = sound_manager_get_index_from_stream_information (stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
498 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
500 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
502 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported");
504 } catch (CAudioError e) {
505 AUDIO_IO_LOGE("%s", e.getErrorMsg());
506 return __convert_CAudioError(e);
509 return AUDIO_IO_ERROR_NONE;
512 int cpp_audio_in_prepare(audio_in_h input) {
513 audio_io_s* handle = static_cast<audio_io_s*>(input);
516 if (handle == NULL) {
517 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
520 assert(handle->audioIoHandle);
522 handle->audioIoHandle->prepare();
523 } catch (CAudioError e) {
524 AUDIO_IO_LOGE("%s", e.getErrorMsg());
525 return __convert_CAudioError(e);
528 return AUDIO_IO_ERROR_NONE;
531 int cpp_audio_in_unprepare(audio_in_h input) {
532 audio_io_s* handle = static_cast<audio_io_s*>(input);
535 if (handle == NULL) {
536 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
539 assert(handle->audioIoHandle);
541 handle->audioIoHandle->unprepare();
542 } catch (CAudioError e) {
543 AUDIO_IO_LOGE("%s", e.getErrorMsg());
544 return __convert_CAudioError(e);
547 return AUDIO_IO_ERROR_NONE;
550 int cpp_audio_in_pause(audio_in_h input) {
551 audio_io_s* handle = static_cast<audio_io_s*>(input);
554 if (handle == NULL) {
555 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
558 assert(handle->audioIoHandle);
560 handle->audioIoHandle->pause();
561 } catch (CAudioError e) {
562 AUDIO_IO_LOGE("%s", e.getErrorMsg());
563 return __convert_CAudioError(e);
566 return AUDIO_IO_ERROR_NONE;
569 int cpp_audio_in_resume(audio_in_h input) {
570 audio_io_s* handle = static_cast<audio_io_s*>(input);
573 if (handle == NULL) {
574 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
577 assert(handle->audioIoHandle);
579 handle->audioIoHandle->resume();
580 } catch (CAudioError e) {
581 AUDIO_IO_LOGE("%s", e.getErrorMsg());
582 return __convert_CAudioError(e);
585 return AUDIO_IO_ERROR_NONE;
588 int cpp_audio_in_drain(audio_in_h input) {
589 audio_io_s* handle = static_cast<audio_io_s*>(input);
592 if (handle == NULL) {
593 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
596 assert(handle->audioIoHandle);
598 handle->audioIoHandle->drain();
599 } catch (CAudioError e) {
600 AUDIO_IO_LOGE("%s", e.getErrorMsg());
601 return __convert_CAudioError(e);
604 return AUDIO_IO_ERROR_NONE;
607 int cpp_audio_in_flush(audio_in_h input) {
608 audio_io_s* handle = static_cast<audio_io_s*>(input);
611 if (handle == NULL) {
612 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
615 assert(handle->audioIoHandle);
617 handle->audioIoHandle->flush();
618 } catch (CAudioError e) {
619 AUDIO_IO_LOGE("%s", e.getErrorMsg());
620 return __convert_CAudioError(e);
623 return AUDIO_IO_ERROR_NONE;
626 int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) {
627 audio_io_s* handle = static_cast<audio_io_s*>(input);
631 if (handle == NULL || buffer == NULL) {
632 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
635 assert(handle->audioIoHandle);
637 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
638 size_t readn = inputHandle->read(buffer, static_cast<size_t>(length));
639 ret = static_cast<int>(readn);
640 #ifdef _AUDIO_IO_DEBUG_TIMING_
641 AUDIO_IO_LOGD("readn:%d", readn);
643 } catch (CAudioError e) {
644 AUDIO_IO_LOGE("%s", e.getErrorMsg());
645 return __convert_CAudioError(e);
651 int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) {
652 audio_io_s* handle = static_cast<audio_io_s*>(input);
655 if (handle == NULL || size == NULL) {
656 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, size:%p", input, size);
659 assert(handle->audioIoHandle);
661 CAudioIO* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
662 *size = inputHandle->getBufferSize();
663 } catch (CAudioError e) {
664 AUDIO_IO_LOGE("%s", e.getErrorMsg());
665 return __convert_CAudioError(e);
668 return AUDIO_IO_ERROR_NONE;
671 int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) {
672 audio_io_s* handle = static_cast<audio_io_s*>(input);
675 if (handle == NULL || sample_rate == NULL) {
676 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
679 assert(handle->audioIoHandle);
680 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
681 } catch (CAudioError e) {
682 AUDIO_IO_LOGE("%s", e.getErrorMsg());
683 return __convert_CAudioError(e);
686 return AUDIO_IO_ERROR_NONE;
689 int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
690 audio_io_s* handle = static_cast<audio_io_s*>(input);
693 if (handle == NULL || channel == NULL) {
694 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, channel:%p", input, channel);
697 assert(handle->audioIoHandle);
699 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
700 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
701 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
703 *channel = dstChannel;
704 } catch (CAudioError e) {
705 AUDIO_IO_LOGE("%s", e.getErrorMsg());
706 return __convert_CAudioError(e);
709 return AUDIO_IO_ERROR_NONE;
712 int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
713 audio_io_s* handle = static_cast<audio_io_s*>(input);
716 if (handle == NULL || type == NULL) {
717 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, type:%p", input, type);
720 assert(handle->audioIoHandle);
722 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
723 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
724 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
726 *type = dstSampleType;
727 } catch (CAudioError e) {
728 AUDIO_IO_LOGE("%s", e.getErrorMsg());
729 return __convert_CAudioError(e);
732 return AUDIO_IO_ERROR_NONE;
735 static void __interrupt_cb_internal(IAudioSessionEventListener::EInterruptCode _code, void* user_data) {
736 audio_io_s* handle = static_cast<audio_io_s*>(user_data);
737 audio_io_interrupted_code_e code = __convert_interrupted_code(_code);
741 if (handle->interrupt_callback.onInterrupt != NULL) {
742 handle->interrupt_callback.onInterrupt(code, handle->interrupt_callback.user_data);
746 int cpp_audio_in_set_interrupted_cb(audio_in_h input, audio_io_interrupted_cb callback, void *user_data) {
747 audio_io_s* handle = static_cast<audio_io_s*>(input);
750 if (handle == NULL || callback == NULL) {
751 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
754 assert(handle->audioIoHandle);
756 handle->interrupt_callback.onInterrupt = callback;
757 handle->interrupt_callback.user_data = user_data;
759 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
760 cb.mUserData = static_cast<void*>(handle);
761 cb.onInterrupt = __interrupt_cb_internal;
763 handle->audioIoHandle->setInterruptCallback(cb);
764 } catch (CAudioError e) {
765 AUDIO_IO_LOGE("%s", e.getErrorMsg());
766 return __convert_CAudioError(e);
769 return AUDIO_IO_ERROR_NONE;
772 int cpp_audio_in_unset_interrupted_cb(audio_in_h input) {
773 audio_io_s* handle = static_cast<audio_io_s*>(input);
776 if (handle == NULL) {
777 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
780 assert(handle->audioIoHandle);
782 handle->interrupt_callback.onInterrupt = NULL;
783 handle->interrupt_callback.user_data = NULL;
785 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
787 cb.onInterrupt = NULL;
789 handle->audioIoHandle->setInterruptCallback(cb);
790 } catch (CAudioError e) {
791 AUDIO_IO_LOGE("%s", e.getErrorMsg());
792 return __convert_CAudioError(e);
795 return AUDIO_IO_ERROR_NONE;
798 int cpp_audio_in_ignore_session(audio_in_h input) {
799 audio_io_s* handle = static_cast<audio_io_s*>(input);
802 if (handle == NULL) {
803 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
806 if (handle->stream_callback.onStream) {
807 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
810 assert(handle->audioIoHandle);
812 handle->audioIoHandle->ignoreSession();
813 } catch (CAudioError e) {
814 AUDIO_IO_LOGE("%s", e.getErrorMsg());
815 return __convert_CAudioError(e);
818 return AUDIO_IO_ERROR_NONE;
821 static void __stream_cb_internal(size_t nbytes, void *user_data) {
822 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
825 if (audioIo->stream_callback.onStream != NULL) {
826 audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data);
830 static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState state_prev, bool by_policy, void *user_data) {
831 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
834 if (audioIo->state_changed_callback.onStateChanged != NULL) {
835 audioIo->state_changed_callback.onStateChanged(audioIo, __convert_state_type(state_prev), __convert_state_type(state), by_policy, audioIo->state_changed_callback.user_data);
839 int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) {
840 audio_io_s* handle = static_cast<audio_io_s*>(input);
843 if (handle == NULL || callback == NULL) {
844 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
847 assert(handle->audioIoHandle);
849 handle->stream_callback.onStream = callback;
850 handle->stream_callback.user_data = user_data;
852 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
853 cb.mUserData = static_cast<void*>(handle);
854 cb.onStream = __stream_cb_internal;
856 handle->audioIoHandle->setStreamCallback(cb);
857 } catch (CAudioError e) {
858 AUDIO_IO_LOGE("%s", e.getErrorMsg());
859 return __convert_CAudioError(e);
862 return AUDIO_IO_ERROR_NONE;
865 int cpp_audio_in_unset_stream_cb(audio_in_h input) {
866 audio_io_s* handle = static_cast<audio_io_s*>(input);
869 if (handle == NULL) {
870 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
873 assert(handle->audioIoHandle);
875 handle->stream_callback.onStream = NULL;
876 handle->stream_callback.user_data = NULL;
878 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
882 handle->audioIoHandle->setStreamCallback(cb);
883 } catch (CAudioError e) {
884 AUDIO_IO_LOGE("%s", e.getErrorMsg());
885 return __convert_CAudioError(e);
888 return AUDIO_IO_ERROR_NONE;
891 int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) {
892 audio_io_s* handle = static_cast<audio_io_s*>(input);
896 if (handle == NULL || buffer == NULL) {
897 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
900 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
903 inputHandle->peek(buffer, &_length);
904 } catch (CAudioError e) {
905 AUDIO_IO_LOGE("%s", e.getErrorMsg());
906 return __convert_CAudioError(e);
909 *length = (unsigned int)_length;
911 return AUDIO_IO_ERROR_NONE;
914 int cpp_audio_in_drop(audio_in_h input) {
915 audio_io_s* handle = static_cast<audio_io_s*>(input);
918 if (handle == NULL) {
919 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
922 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
926 } catch (CAudioError e) {
927 AUDIO_IO_LOGE("%s", e.getErrorMsg());
928 return __convert_CAudioError(e);
931 return AUDIO_IO_ERROR_NONE;
934 int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) {
935 audio_io_s* handle = static_cast<audio_io_s*>(input);
938 if (handle == NULL || callback == NULL) {
939 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
942 assert(handle->audioIoHandle);
944 handle->state_changed_callback.onStateChanged = callback;
945 handle->state_changed_callback.user_data = user_data;
947 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
948 cb.mUserData = static_cast<void*>(handle);
949 cb.onStateChanged = __state_changed_cb_internal;
951 handle->audioIoHandle->setStateChangedCallback(cb);
952 } catch (CAudioError e) {
953 AUDIO_IO_LOGE("%s", e.getErrorMsg());
954 return __convert_CAudioError(e);
957 return AUDIO_IO_ERROR_NONE;
960 int cpp_audio_in_unset_state_changed_cb(audio_in_h input) {
961 audio_io_s* handle = static_cast<audio_io_s*>(input);
964 if (handle == NULL) {
965 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", input);
968 assert(handle->audioIoHandle);
970 handle->state_changed_callback.onStateChanged = NULL;
971 handle->state_changed_callback.user_data = NULL;
973 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
975 cb.onStateChanged = NULL;
977 handle->audioIoHandle->setStateChangedCallback(cb);
978 } catch (CAudioError e) {
979 AUDIO_IO_LOGE("%s", e.getErrorMsg());
980 return __convert_CAudioError(e);
983 return AUDIO_IO_ERROR_NONE;
990 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) {
991 audio_io_s* handle = NULL;
993 if (output == NULL) {
994 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
997 __check_audio_param(sample_rate, channel, type, sound_type);
999 handle = new audio_io_s;
1000 if (handle == NULL) {
1001 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1004 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, sound_type);
1006 handle->audioIoHandle = new CAudioOutput(audioInfo);
1007 if (handle == NULL) {
1008 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1011 handle->audioIoHandle->initialize();
1014 } catch (CAudioError e) {
1015 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1017 VALID_POINTER_START(handle)
1018 SAFE_FINALIZE(handle->audioIoHandle);
1019 SAFE_DELETE(handle->audioIoHandle);
1020 SAFE_DELETE(handle);
1023 VALID_POINTER_START(output)
1027 return __convert_CAudioError(e);
1030 return AUDIO_IO_ERROR_NONE;
1033 int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) {
1034 audio_io_s* handle = NULL;
1036 if (output == NULL) {
1037 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1040 __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */);
1042 handle = new audio_io_s;
1043 if (handle == NULL) {
1044 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1047 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA /* default sound_type */);
1049 handle->audioIoHandle = new CAudioOutput(audioInfo);
1050 if (handle == NULL) {
1051 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1054 handle->audioIoHandle->initialize();
1057 } catch (CAudioError e) {
1058 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1060 VALID_POINTER_START(handle)
1061 SAFE_FINALIZE(handle->audioIoHandle);
1062 SAFE_DELETE(handle->audioIoHandle);
1063 SAFE_DELETE(handle);
1066 VALID_POINTER_START(output)
1070 return __convert_CAudioError(e);
1073 return AUDIO_IO_ERROR_NONE;
1076 int cpp_audio_out_destroy(audio_out_h output) {
1077 audio_io_s* handle = static_cast<audio_io_s*>(output);
1080 if (handle == NULL) {
1081 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1084 assert(handle->audioIoHandle);
1086 SAFE_FINALIZE(handle->audioIoHandle);
1087 SAFE_DELETE(handle->audioIoHandle);
1088 SAFE_DELETE(handle);
1089 } catch (CAudioError e) {
1090 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1091 return __convert_CAudioError(e);
1094 return AUDIO_IO_ERROR_NONE;
1097 int cpp_audio_out_set_stream_info(audio_out_h output, sound_stream_info_h stream_info) {
1098 audio_io_s* handle = static_cast<audio_io_s*>(output);
1101 if (handle == NULL || stream_info == NULL) {
1102 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, stream_info:%p", output, stream_info);
1105 assert(handle->audioIoHandle);
1107 int errorCode = SOUND_MANAGER_ERROR_NONE;
1108 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
1113 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
1114 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
1118 if ((errorCode = sound_manager_get_type_from_stream_information (stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
1119 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
1121 handle->audioIoHandle->getAudioInfo().convertOutputStreamType2AudioType(type, &audioType);
1122 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
1124 if ((errorCode = sound_manager_get_index_from_stream_information (stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
1125 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
1127 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
1129 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Output stream is not supported");
1131 } catch (CAudioError e) {
1132 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1133 return __convert_CAudioError(e);
1136 return AUDIO_IO_ERROR_NONE;
1139 int cpp_audio_out_prepare(audio_out_h output) {
1140 audio_io_s* handle = static_cast<audio_io_s*>(output);
1143 if (handle == NULL) {
1144 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1147 assert(handle->audioIoHandle);
1149 handle->audioIoHandle->prepare();
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_unprepare(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->unprepare();
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_pause(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->pause();
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_resume(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->resume();
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_drain(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->drain();
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_flush(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->flush();
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_write(audio_out_h output, void *buffer, unsigned int length) {
1254 audio_io_s* handle = static_cast<audio_io_s*>(output);
1258 if (handle == NULL || buffer == NULL) {
1259 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p, buffer:%p", output, buffer);
1262 assert(handle->audioIoHandle);
1264 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1265 size_t writen = outputHandle->write(buffer, static_cast<size_t>(length));
1266 ret = static_cast<int>(writen);
1267 #ifdef _AUDIO_IO_DEBUG_TIMING_
1268 AUDIO_IO_LOGD("writen:%d", writen);
1270 } catch (CAudioError e) {
1271 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1272 return __convert_CAudioError(e);
1278 int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) {
1279 audio_io_s* handle = static_cast<audio_io_s*>(output);
1282 if (handle == NULL || size == NULL) {
1283 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, size:%p", output, size);
1286 assert(handle->audioIoHandle);
1288 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1289 *size = outputHandle->getBufferSize();
1290 } catch (CAudioError e) {
1291 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1292 return __convert_CAudioError(e);
1295 return AUDIO_IO_ERROR_NONE;
1298 int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
1299 audio_io_s* handle = static_cast<audio_io_s*>(output);
1302 if (handle == NULL || sample_rate == NULL) {
1303 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
1306 assert(handle->audioIoHandle);
1307 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
1308 } catch (CAudioError e) {
1309 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1310 return __convert_CAudioError(e);
1313 return AUDIO_IO_ERROR_NONE;
1316 int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
1317 audio_io_s* handle = static_cast<audio_io_s*>(output);
1320 if (handle == NULL || channel == NULL) {
1321 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, channel:%p", output, channel);
1324 assert(handle->audioIoHandle);
1326 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
1327 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
1328 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
1330 *channel = dstChannel;
1331 } catch (CAudioError e) {
1332 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1333 return __convert_CAudioError(e);
1336 return AUDIO_IO_ERROR_NONE;
1339 int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) {
1340 audio_io_s* handle = static_cast<audio_io_s*>(output);
1343 if (handle == NULL || type == NULL) {
1344 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1347 assert(handle->audioIoHandle);
1349 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
1350 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
1351 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
1353 *type = dstSampleType;
1354 } catch (CAudioError e) {
1355 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1356 return __convert_CAudioError(e);
1359 return AUDIO_IO_ERROR_NONE;
1362 int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) {
1363 audio_io_s* handle = static_cast<audio_io_s*>(output);
1366 if (handle == NULL || type == NULL) {
1367 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1370 assert(handle->audioIoHandle);
1372 const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType();
1373 sound_type_e dstSoundType = SOUND_TYPE_MEDIA;
1374 __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType);
1376 *type = dstSoundType;
1377 } catch (CAudioError e) {
1378 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1379 return __convert_CAudioError(e);
1382 return AUDIO_IO_ERROR_NONE;
1385 int cpp_audio_out_set_interrupted_cb(audio_out_h output, audio_io_interrupted_cb callback, void *user_data) {
1386 audio_io_s* handle = static_cast<audio_io_s*>(output);
1389 if (handle == NULL || callback == NULL) {
1390 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1393 assert(handle->audioIoHandle);
1395 handle->interrupt_callback.onInterrupt = callback;
1396 handle->interrupt_callback.user_data = user_data;
1398 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1399 cb.mUserData = static_cast<void*>(handle);
1400 cb.onInterrupt = __interrupt_cb_internal;
1402 handle->audioIoHandle->setInterruptCallback(cb);
1403 } catch (CAudioError e) {
1404 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1405 return __convert_CAudioError(e);
1408 return AUDIO_IO_ERROR_NONE;
1411 int cpp_audio_out_unset_interrupted_cb(audio_out_h output) {
1412 audio_io_s* handle = static_cast<audio_io_s*>(output);
1415 if (handle == NULL) {
1416 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1419 assert(handle->audioIoHandle);
1421 handle->interrupt_callback.onInterrupt = NULL;
1422 handle->interrupt_callback.user_data = NULL;
1424 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1425 cb.mUserData = NULL;
1426 cb.onInterrupt = NULL;
1428 handle->audioIoHandle->setInterruptCallback(cb);
1429 } catch (CAudioError e) {
1430 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1431 return __convert_CAudioError(e);
1434 return AUDIO_IO_ERROR_NONE;
1437 int cpp_audio_out_ignore_session(audio_out_h output) {
1438 audio_io_s* handle = static_cast<audio_io_s*>(output);
1441 if (handle == NULL) {
1442 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1445 if (handle->stream_callback.onStream) {
1446 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
1449 assert(handle->audioIoHandle);
1451 handle->audioIoHandle->ignoreSession();
1452 } catch (CAudioError e) {
1453 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1454 return __convert_CAudioError(e);
1457 return AUDIO_IO_ERROR_NONE;
1460 int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) {
1461 audio_io_s* handle = static_cast<audio_io_s*>(output);
1464 if (handle == NULL || callback == NULL) {
1465 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1468 assert(handle->audioIoHandle);
1470 handle->stream_callback.onStream = callback;
1471 handle->stream_callback.user_data = user_data;
1473 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1474 cb.mUserData = static_cast<void*>(handle);
1475 cb.onStream = __stream_cb_internal;
1477 handle->audioIoHandle->setStreamCallback(cb);
1478 } catch (CAudioError e) {
1479 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1480 return __convert_CAudioError(e);
1483 return AUDIO_IO_ERROR_NONE;
1486 int cpp_audio_out_unset_stream_cb(audio_out_h output) {
1487 audio_io_s* handle = static_cast<audio_io_s*>(output);
1490 if (handle == NULL) {
1491 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1494 assert(handle->audioIoHandle);
1496 handle->stream_callback.onStream = NULL;
1497 handle->stream_callback.user_data = NULL;
1499 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1500 cb.mUserData = NULL;
1503 handle->audioIoHandle->setStreamCallback(cb);
1504 } catch (CAudioError e) {
1505 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1506 return __convert_CAudioError(e);
1509 return AUDIO_IO_ERROR_NONE;
1512 int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) {
1513 audio_io_s* handle = static_cast<audio_io_s*>(output);
1516 if (handle == NULL || callback == NULL) {
1517 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1520 assert(handle->audioIoHandle);
1522 handle->state_changed_callback.onStateChanged = callback;
1523 handle->state_changed_callback.user_data = user_data;
1525 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1526 cb.mUserData = static_cast<void*>(handle);
1527 cb.onStateChanged = __state_changed_cb_internal;
1529 handle->audioIoHandle->setStateChangedCallback(cb);
1530 } catch (CAudioError e) {
1531 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1532 return __convert_CAudioError(e);
1535 return AUDIO_IO_ERROR_NONE;
1538 int cpp_audio_out_unset_state_changed_cb(audio_out_h output) {
1539 audio_io_s* handle = static_cast<audio_io_s*>(output);
1542 if (handle == NULL) {
1543 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1546 assert(handle->audioIoHandle);
1548 handle->state_changed_callback.onStateChanged = NULL;
1549 handle->state_changed_callback.user_data = NULL;
1551 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1552 cb.mUserData = NULL;
1553 cb.onStateChanged = NULL;
1555 handle->audioIoHandle->setStateChangedCallback(cb);
1556 } catch (CAudioError e) {
1557 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1558 return __convert_CAudioError(e);
1561 return AUDIO_IO_ERROR_NONE;