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();
98 case CAudioError::EError::ERROR_NONE:
99 ret = AUDIO_IO_ERROR_NONE;
101 case CAudioError::EError::ERROR_INVALID_ARGUMENT:
102 case CAudioError::EError::ERROR_INVALID_HANDLE:
103 case CAudioError::EError::ERROR_INVALID_SAMPLERATE:
104 case CAudioError::EError::ERROR_INVALID_CHANNEL:
105 case CAudioError::EError::ERROR_INVALID_FORMAT:
106 ret = AUDIO_IO_ERROR_INVALID_PARAMETER;
108 case CAudioError::EError::ERROR_DEVICE_NOT_OPENED:
109 ret = AUDIO_IO_ERROR_DEVICE_NOT_OPENED;
111 case CAudioError::EError::ERROR_DEVICE_NOT_CLOSED:
112 ret = AUDIO_IO_ERROR_DEVICE_NOT_CLOSED;
114 case CAudioError::EError::ERROR_PERMISSION_DENIED:
115 ret = AUDIO_IO_ERROR_PERMISSION_DENIED;
117 case CAudioError::EError::ERROR_NOT_SUPPORTED:
118 ret = AUDIO_IO_ERROR_NOT_SUPPORTED;
120 case CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE:
121 ret = AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE;
123 case CAudioError::EError::ERROR_MAX:
124 case CAudioError::EError::ERROR_INTERNAL_OPERATION:
125 case CAudioError::EError::ERROR_NOT_INITIALIZED:
126 case CAudioError::EError::ERROR_FAILED_OPERATION:
127 case CAudioError::EError::ERROR_INVALID_OPERATION:
128 ret = AUDIO_IO_ERROR_INVALID_OPERATION;
130 case CAudioError::EError::ERROR_OUT_OF_MEMORY:
131 case CAudioError::EError::ERROR_INVALID_POINTER:
132 ret = AUDIO_IO_ERROR_INVALID_BUFFER;
134 case CAudioError::EError::ERROR_POLICY_BLOCKED:
135 case CAudioError::EError::ERROR_POLICY_INTERRUPTED:
136 case CAudioError::EError::ERROR_POLICY_DUPLICATED:
137 ret = AUDIO_IO_ERROR_SOUND_POLICY;
144 static void __convert_channel_2_audio_info_channel(const audio_channel_e& src_channel, CAudioInfo::EChannel& dst_channel) {
145 switch (src_channel) {
146 case AUDIO_CHANNEL_MONO:
147 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
149 case AUDIO_CHANNEL_STEREO:
150 dst_channel = CAudioInfo::EChannel::CHANNEL_STEREO;
153 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
157 static void __convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel, audio_channel_e& dst_channel) {
158 switch (src_channel) {
159 case CAudioInfo::EChannel::CHANNEL_MONO:
160 dst_channel = AUDIO_CHANNEL_MONO;
162 case CAudioInfo::EChannel::CHANNEL_STEREO:
163 dst_channel = AUDIO_CHANNEL_STEREO;
166 dst_channel = AUDIO_CHANNEL_MONO;
170 static void __convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type, CAudioInfo::ESampleType& dst_type) {
172 case AUDIO_SAMPLE_TYPE_U8:
173 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
175 case AUDIO_SAMPLE_TYPE_S16_LE:
176 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE;
179 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
183 static void __convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type, audio_sample_type_e& dst_type) {
185 case CAudioInfo::ESampleType::SAMPLE_TYPE_U8:
186 dst_type = AUDIO_SAMPLE_TYPE_U8;
188 case CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE:
189 dst_type = AUDIO_SAMPLE_TYPE_S16_LE;
192 dst_type = AUDIO_SAMPLE_TYPE_U8;
196 static void __convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type, CAudioInfo::EAudioType& dst_type) {
198 case SOUND_TYPE_SYSTEM:
199 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
201 case SOUND_TYPE_NOTIFICATION:
202 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION;
204 case SOUND_TYPE_ALARM:
205 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM;
207 case SOUND_TYPE_RINGTONE:
208 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP;
210 case SOUND_TYPE_MEDIA:
211 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
213 case SOUND_TYPE_CALL:
214 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
216 case SOUND_TYPE_VOIP:
217 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP;
219 case SOUND_TYPE_VOICE:
220 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION;
223 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
228 static void __convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type, sound_type_e& dst_type) {
230 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA:
231 dst_type = SOUND_TYPE_MEDIA;
233 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM:
234 dst_type = SOUND_TYPE_SYSTEM;
236 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM:
237 dst_type = SOUND_TYPE_ALARM;
239 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION:
240 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_EMERGENCY:
241 dst_type = SOUND_TYPE_NOTIFICATION;
243 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION:
244 dst_type = SOUND_TYPE_VOICE;
246 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP:
247 dst_type = SOUND_TYPE_RINGTONE;
249 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP:
250 dst_type = SOUND_TYPE_VOIP;
253 dst_type = SOUND_TYPE_MEDIA;
258 static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src_state) {
259 audio_io_state_e dst_state;
261 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE:
262 dst_state = AUDIO_IO_STATE_IDLE;
264 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE:
265 dst_state = AUDIO_IO_STATE_IDLE;
267 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING:
268 dst_state = AUDIO_IO_STATE_RUNNING;
270 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED:
271 dst_state = AUDIO_IO_STATE_PAUSED;
274 dst_state = AUDIO_IO_STATE_IDLE;
279 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) throw(CAudioError) {
280 if (sample_rate < 0) {
281 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate);
284 if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) {
285 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel);
288 if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE) {
289 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type);
293 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) throw(CAudioError) {
294 __check_audio_param(sample_rate, channel, type);
296 if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE) {
297 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type);
301 static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw(CAudioError) {
302 CAudioInfo::EChannel dstChannel;
303 CAudioInfo::ESampleType dstSampleType;
304 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
306 __convert_channel_2_audio_info_channel(channel, dstChannel);
307 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
309 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
312 static CAudioInfo __generate_audio_input_loopback_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw(CAudioError) {
313 CAudioInfo::EChannel dstChannel;
314 CAudioInfo::ESampleType dstSampleType;
315 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_LOOPBACK;
317 __convert_channel_2_audio_info_channel(channel, dstChannel);
318 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
320 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
323 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) {
324 CAudioInfo::EChannel dstChannel;
325 CAudioInfo::ESampleType dstSampleType;
326 CAudioInfo::EAudioType dstAudioType;
328 __convert_channel_2_audio_info_channel(channel, dstChannel);
329 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
330 __convert_sound_type_2_audio_info_audio_type(sound_type, dstAudioType);
332 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
335 static audio_io_interrupted_code_e __convert_interrupted_code(IAudioSessionEventListener::EInterruptCode code) {
337 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED:
338 return AUDIO_IO_INTERRUPTED_COMPLETED;
339 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_CALL:
340 return AUDIO_IO_INTERRUPTED_BY_CALL;
341 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EARJACK_UNPLUG:
342 return AUDIO_IO_INTERRUPTED_BY_EARJACK_UNPLUG;
343 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_RESOURCE_CONFLICT:
344 return AUDIO_IO_INTERRUPTED_BY_RESOURCE_CONFLICT;
345 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_ALARM:
346 return AUDIO_IO_INTERRUPTED_BY_ALARM;
347 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EMERGENCY:
348 return AUDIO_IO_INTERRUPTED_BY_EMERGENCY;
349 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_NOTIFICATION:
350 return AUDIO_IO_INTERRUPTED_BY_NOTIFICATION;
351 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_MEDIA:
352 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_MAX:
354 return AUDIO_IO_INTERRUPTED_BY_MEDIA;
359 * Implements CAPI functions
361 int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_in_h *input) {
362 audio_io_s* handle = NULL;
365 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
368 __check_audio_param(sample_rate, channel, type);
370 handle = new audio_io_s;
371 if (handle == NULL) {
372 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
375 CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type);
377 handle->audioIoHandle = new CAudioInput(audioInfo);
378 if (handle == NULL) {
379 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
382 handle->audioIoHandle->initialize();
385 } catch (CAudioError e) {
386 AUDIO_IO_LOGE("%s", e.getErrorMsg());
388 VALID_POINTER_START(handle)
389 SAFE_FINALIZE(handle->audioIoHandle);
390 SAFE_DELETE(handle->audioIoHandle);
394 VALID_POINTER_START(input)
398 return __convert_CAudioError(e);
401 return AUDIO_IO_ERROR_NONE;
404 int cpp_audio_in_create_loopback(int sample_rate, audio_channel_e channel, audio_sample_type_e type , audio_in_h* input) {
405 audio_io_s* handle = NULL;
408 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
411 __check_audio_param(sample_rate, channel, type);
413 handle = new audio_io_s;
414 if (handle == NULL) {
415 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
418 CAudioInfo audioInfo = __generate_audio_input_loopback_info(sample_rate, channel, type);
420 handle->audioIoHandle = new CAudioInput(audioInfo);
421 if (handle == NULL) {
422 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
425 handle->audioIoHandle->initialize();
428 } catch (CAudioError e) {
429 AUDIO_IO_LOGE("%s", e.getErrorMsg());
431 VALID_POINTER_START(handle)
432 SAFE_FINALIZE(handle->audioIoHandle);
433 SAFE_DELETE(handle->audioIoHandle);
437 VALID_POINTER_START(input)
441 return __convert_CAudioError(e);
444 return AUDIO_IO_ERROR_NONE;
447 int cpp_audio_in_destroy(audio_in_h input) {
448 audio_io_s* handle = static_cast<audio_io_s*>(input);
451 if (handle == NULL) {
452 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
455 assert(handle->audioIoHandle);
457 SAFE_FINALIZE(handle->audioIoHandle);
458 SAFE_DELETE(handle->audioIoHandle);
460 } catch (CAudioError e) {
461 AUDIO_IO_LOGE("%s", e.getErrorMsg());
462 return __convert_CAudioError(e);
465 return AUDIO_IO_ERROR_NONE;
468 int cpp_audio_in_set_stream_info(audio_in_h input, sound_stream_info_h stream_info) {
469 audio_io_s* handle = static_cast<audio_io_s*>(input);
472 if (handle == NULL || stream_info == NULL) {
473 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, stream_info:%p", input, stream_info);
476 assert(handle->audioIoHandle);
478 int errorCode = SOUND_MANAGER_ERROR_NONE;
479 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
484 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
485 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
489 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
490 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
492 handle->audioIoHandle->getAudioInfo().convertInputStreamType2AudioType(type, &audioType);
493 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
495 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
496 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
498 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
500 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported");
502 } catch (CAudioError e) {
503 AUDIO_IO_LOGE("%s", e.getErrorMsg());
504 return __convert_CAudioError(e);
507 return AUDIO_IO_ERROR_NONE;
510 int cpp_audio_in_prepare(audio_in_h input) {
511 audio_io_s* handle = static_cast<audio_io_s*>(input);
514 if (handle == NULL) {
515 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
518 assert(handle->audioIoHandle);
520 handle->audioIoHandle->prepare();
521 } catch (CAudioError e) {
522 AUDIO_IO_LOGE("%s", e.getErrorMsg());
523 return __convert_CAudioError(e);
526 return AUDIO_IO_ERROR_NONE;
529 int cpp_audio_in_unprepare(audio_in_h input) {
530 audio_io_s* handle = static_cast<audio_io_s*>(input);
533 if (handle == NULL) {
534 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
537 assert(handle->audioIoHandle);
539 handle->audioIoHandle->unprepare();
540 } catch (CAudioError e) {
541 AUDIO_IO_LOGE("%s", e.getErrorMsg());
542 return __convert_CAudioError(e);
545 return AUDIO_IO_ERROR_NONE;
548 int cpp_audio_in_pause(audio_in_h input) {
549 audio_io_s* handle = static_cast<audio_io_s*>(input);
552 if (handle == NULL) {
553 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
556 assert(handle->audioIoHandle);
558 handle->audioIoHandle->pause();
559 } catch (CAudioError e) {
560 AUDIO_IO_LOGE("%s", e.getErrorMsg());
561 return __convert_CAudioError(e);
564 return AUDIO_IO_ERROR_NONE;
567 int cpp_audio_in_resume(audio_in_h input) {
568 audio_io_s* handle = static_cast<audio_io_s*>(input);
571 if (handle == NULL) {
572 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
575 assert(handle->audioIoHandle);
577 handle->audioIoHandle->resume();
578 } catch (CAudioError e) {
579 AUDIO_IO_LOGE("%s", e.getErrorMsg());
580 return __convert_CAudioError(e);
583 return AUDIO_IO_ERROR_NONE;
586 int cpp_audio_in_drain(audio_in_h input) {
587 audio_io_s* handle = static_cast<audio_io_s*>(input);
590 if (handle == NULL) {
591 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
594 assert(handle->audioIoHandle);
596 handle->audioIoHandle->drain();
597 } catch (CAudioError e) {
598 AUDIO_IO_LOGE("%s", e.getErrorMsg());
599 return __convert_CAudioError(e);
602 return AUDIO_IO_ERROR_NONE;
605 int cpp_audio_in_flush(audio_in_h input) {
606 audio_io_s* handle = static_cast<audio_io_s*>(input);
609 if (handle == NULL) {
610 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
613 assert(handle->audioIoHandle);
615 handle->audioIoHandle->flush();
616 } catch (CAudioError e) {
617 AUDIO_IO_LOGE("%s", e.getErrorMsg());
618 return __convert_CAudioError(e);
621 return AUDIO_IO_ERROR_NONE;
624 int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) {
625 audio_io_s* handle = static_cast<audio_io_s*>(input);
629 if (handle == NULL || buffer == NULL) {
630 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
633 assert(handle->audioIoHandle);
635 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
636 size_t readn = inputHandle->read(buffer, static_cast<size_t>(length));
637 ret = static_cast<int>(readn);
638 #ifdef _AUDIO_IO_DEBUG_TIMING_
639 AUDIO_IO_LOGD("readn:%d", readn);
641 } catch (CAudioError e) {
642 AUDIO_IO_LOGE("%s", e.getErrorMsg());
643 return __convert_CAudioError(e);
649 int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) {
650 audio_io_s* handle = static_cast<audio_io_s*>(input);
653 if (handle == NULL || size == NULL) {
654 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, size:%p", input, size);
657 assert(handle->audioIoHandle);
659 CAudioIO* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
660 *size = inputHandle->getBufferSize();
661 } catch (CAudioError e) {
662 AUDIO_IO_LOGE("%s", e.getErrorMsg());
663 return __convert_CAudioError(e);
666 return AUDIO_IO_ERROR_NONE;
669 int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) {
670 audio_io_s* handle = static_cast<audio_io_s*>(input);
673 if (handle == NULL || sample_rate == NULL) {
674 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
677 assert(handle->audioIoHandle);
678 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
679 } catch (CAudioError e) {
680 AUDIO_IO_LOGE("%s", e.getErrorMsg());
681 return __convert_CAudioError(e);
684 return AUDIO_IO_ERROR_NONE;
687 int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
688 audio_io_s* handle = static_cast<audio_io_s*>(input);
691 if (handle == NULL || channel == NULL) {
692 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, channel:%p", input, channel);
695 assert(handle->audioIoHandle);
697 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
698 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
699 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
701 *channel = dstChannel;
702 } catch (CAudioError e) {
703 AUDIO_IO_LOGE("%s", e.getErrorMsg());
704 return __convert_CAudioError(e);
707 return AUDIO_IO_ERROR_NONE;
710 int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
711 audio_io_s* handle = static_cast<audio_io_s*>(input);
714 if (handle == NULL || type == NULL) {
715 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, type:%p", input, type);
718 assert(handle->audioIoHandle);
720 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
721 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
722 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
724 *type = dstSampleType;
725 } catch (CAudioError e) {
726 AUDIO_IO_LOGE("%s", e.getErrorMsg());
727 return __convert_CAudioError(e);
730 return AUDIO_IO_ERROR_NONE;
733 static void __interrupt_cb_internal(IAudioSessionEventListener::EInterruptCode _code, void* user_data) {
734 audio_io_s* handle = static_cast<audio_io_s*>(user_data);
735 audio_io_interrupted_code_e code = __convert_interrupted_code(_code);
739 if (handle->interrupt_callback.onInterrupt != NULL) {
740 handle->interrupt_callback.onInterrupt(code, handle->interrupt_callback.user_data);
744 int cpp_audio_in_set_interrupted_cb(audio_in_h input, audio_io_interrupted_cb callback, void *user_data) {
745 audio_io_s* handle = static_cast<audio_io_s*>(input);
748 if (handle == NULL || callback == NULL) {
749 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
752 assert(handle->audioIoHandle);
754 handle->interrupt_callback.onInterrupt = callback;
755 handle->interrupt_callback.user_data = user_data;
757 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
758 cb.mUserData = static_cast<void*>(handle);
759 cb.onInterrupt = __interrupt_cb_internal;
761 handle->audioIoHandle->setInterruptCallback(cb);
762 } catch (CAudioError e) {
763 AUDIO_IO_LOGE("%s", e.getErrorMsg());
764 return __convert_CAudioError(e);
767 return AUDIO_IO_ERROR_NONE;
770 int cpp_audio_in_unset_interrupted_cb(audio_in_h input) {
771 audio_io_s* handle = static_cast<audio_io_s*>(input);
774 if (handle == NULL) {
775 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
778 assert(handle->audioIoHandle);
780 handle->interrupt_callback.onInterrupt = NULL;
781 handle->interrupt_callback.user_data = NULL;
783 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
785 cb.onInterrupt = NULL;
787 handle->audioIoHandle->setInterruptCallback(cb);
788 } catch (CAudioError e) {
789 AUDIO_IO_LOGE("%s", e.getErrorMsg());
790 return __convert_CAudioError(e);
793 return AUDIO_IO_ERROR_NONE;
796 int cpp_audio_in_ignore_session(audio_in_h input) {
797 audio_io_s* handle = static_cast<audio_io_s*>(input);
800 if (handle == NULL) {
801 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
804 if (handle->stream_callback.onStream) {
805 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
808 assert(handle->audioIoHandle);
810 handle->audioIoHandle->ignoreSession();
811 } catch (CAudioError e) {
812 AUDIO_IO_LOGE("%s", e.getErrorMsg());
813 return __convert_CAudioError(e);
816 return AUDIO_IO_ERROR_NONE;
819 static void __stream_cb_internal(size_t nbytes, void *user_data) {
820 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
823 if (audioIo->stream_callback.onStream != NULL) {
824 audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data);
828 static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState state_prev, bool by_policy, void *user_data) {
829 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
832 if (audioIo->state_changed_callback.onStateChanged != NULL) {
833 audioIo->state_changed_callback.onStateChanged(audioIo, __convert_state_type(state_prev), __convert_state_type(state), by_policy, audioIo->state_changed_callback.user_data);
837 int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) {
838 audio_io_s* handle = static_cast<audio_io_s*>(input);
841 if (handle == NULL || callback == NULL) {
842 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
845 assert(handle->audioIoHandle);
847 handle->stream_callback.onStream = callback;
848 handle->stream_callback.user_data = user_data;
850 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
851 cb.mUserData = static_cast<void*>(handle);
852 cb.onStream = __stream_cb_internal;
854 handle->audioIoHandle->setStreamCallback(cb);
855 } catch (CAudioError e) {
856 AUDIO_IO_LOGE("%s", e.getErrorMsg());
857 return __convert_CAudioError(e);
860 return AUDIO_IO_ERROR_NONE;
863 int cpp_audio_in_unset_stream_cb(audio_in_h input) {
864 audio_io_s* handle = static_cast<audio_io_s*>(input);
867 if (handle == NULL) {
868 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
871 assert(handle->audioIoHandle);
873 handle->stream_callback.onStream = NULL;
874 handle->stream_callback.user_data = NULL;
876 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
880 handle->audioIoHandle->setStreamCallback(cb);
881 } catch (CAudioError e) {
882 AUDIO_IO_LOGE("%s", e.getErrorMsg());
883 return __convert_CAudioError(e);
886 return AUDIO_IO_ERROR_NONE;
889 int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) {
890 audio_io_s* handle = static_cast<audio_io_s*>(input);
894 if (handle == NULL || buffer == NULL) {
895 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
898 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
901 inputHandle->peek(buffer, &_length);
902 } catch (CAudioError e) {
903 AUDIO_IO_LOGE("%s", e.getErrorMsg());
904 return __convert_CAudioError(e);
907 *length = (unsigned int)_length;
909 return AUDIO_IO_ERROR_NONE;
912 int cpp_audio_in_drop(audio_in_h input) {
913 audio_io_s* handle = static_cast<audio_io_s*>(input);
916 if (handle == NULL) {
917 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
920 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
924 } catch (CAudioError e) {
925 AUDIO_IO_LOGE("%s", e.getErrorMsg());
926 return __convert_CAudioError(e);
929 return AUDIO_IO_ERROR_NONE;
932 int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) {
933 audio_io_s* handle = static_cast<audio_io_s*>(input);
936 if (handle == NULL || callback == NULL) {
937 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
940 assert(handle->audioIoHandle);
942 handle->state_changed_callback.onStateChanged = callback;
943 handle->state_changed_callback.user_data = user_data;
945 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
946 cb.mUserData = static_cast<void*>(handle);
947 cb.onStateChanged = __state_changed_cb_internal;
949 handle->audioIoHandle->setStateChangedCallback(cb);
950 } catch (CAudioError e) {
951 AUDIO_IO_LOGE("%s", e.getErrorMsg());
952 return __convert_CAudioError(e);
955 return AUDIO_IO_ERROR_NONE;
958 int cpp_audio_in_unset_state_changed_cb(audio_in_h input) {
959 audio_io_s* handle = static_cast<audio_io_s*>(input);
962 if (handle == NULL) {
963 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", input);
966 assert(handle->audioIoHandle);
968 handle->state_changed_callback.onStateChanged = NULL;
969 handle->state_changed_callback.user_data = NULL;
971 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
973 cb.onStateChanged = NULL;
975 handle->audioIoHandle->setStateChangedCallback(cb);
976 } catch (CAudioError e) {
977 AUDIO_IO_LOGE("%s", e.getErrorMsg());
978 return __convert_CAudioError(e);
981 return AUDIO_IO_ERROR_NONE;
988 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) {
989 audio_io_s* handle = NULL;
991 if (output == NULL) {
992 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
995 __check_audio_param(sample_rate, channel, type, sound_type);
997 handle = new audio_io_s;
998 if (handle == NULL) {
999 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1002 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, sound_type);
1004 handle->audioIoHandle = new CAudioOutput(audioInfo);
1005 if (handle == NULL) {
1006 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1009 handle->audioIoHandle->initialize();
1012 } catch (CAudioError e) {
1013 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1015 VALID_POINTER_START(handle)
1016 SAFE_FINALIZE(handle->audioIoHandle);
1017 SAFE_DELETE(handle->audioIoHandle);
1018 SAFE_DELETE(handle);
1021 VALID_POINTER_START(output)
1025 return __convert_CAudioError(e);
1028 return AUDIO_IO_ERROR_NONE;
1031 int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) {
1032 audio_io_s* handle = NULL;
1034 if (output == NULL) {
1035 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1038 __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */);
1040 handle = new audio_io_s;
1041 if (handle == NULL) {
1042 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1045 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA /* default sound_type */);
1047 handle->audioIoHandle = new CAudioOutput(audioInfo);
1048 if (handle == NULL) {
1049 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1052 handle->audioIoHandle->initialize();
1055 } catch (CAudioError e) {
1056 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1058 VALID_POINTER_START(handle)
1059 SAFE_FINALIZE(handle->audioIoHandle);
1060 SAFE_DELETE(handle->audioIoHandle);
1061 SAFE_DELETE(handle);
1064 VALID_POINTER_START(output)
1068 return __convert_CAudioError(e);
1071 return AUDIO_IO_ERROR_NONE;
1074 int cpp_audio_out_destroy(audio_out_h output) {
1075 audio_io_s* handle = static_cast<audio_io_s*>(output);
1078 if (handle == NULL) {
1079 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1082 assert(handle->audioIoHandle);
1084 SAFE_FINALIZE(handle->audioIoHandle);
1085 SAFE_DELETE(handle->audioIoHandle);
1086 SAFE_DELETE(handle);
1087 } catch (CAudioError e) {
1088 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1089 return __convert_CAudioError(e);
1092 return AUDIO_IO_ERROR_NONE;
1095 int cpp_audio_out_set_stream_info(audio_out_h output, sound_stream_info_h stream_info) {
1096 audio_io_s* handle = static_cast<audio_io_s*>(output);
1099 if (handle == NULL || stream_info == NULL) {
1100 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, stream_info:%p", output, stream_info);
1103 assert(handle->audioIoHandle);
1105 int errorCode = SOUND_MANAGER_ERROR_NONE;
1106 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
1111 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
1112 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
1116 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
1117 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
1119 handle->audioIoHandle->getAudioInfo().convertOutputStreamType2AudioType(type, &audioType);
1120 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
1122 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
1123 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
1125 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
1127 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Output stream is not supported");
1129 } catch (CAudioError e) {
1130 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1131 return __convert_CAudioError(e);
1134 return AUDIO_IO_ERROR_NONE;
1137 int cpp_audio_out_prepare(audio_out_h output) {
1138 audio_io_s* handle = static_cast<audio_io_s*>(output);
1141 if (handle == NULL) {
1142 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1145 assert(handle->audioIoHandle);
1147 handle->audioIoHandle->prepare();
1148 } catch (CAudioError e) {
1149 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1150 return __convert_CAudioError(e);
1153 return AUDIO_IO_ERROR_NONE;
1156 int cpp_audio_out_unprepare(audio_out_h output) {
1157 audio_io_s* handle = static_cast<audio_io_s*>(output);
1160 if (handle == NULL) {
1161 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1164 assert(handle->audioIoHandle);
1166 handle->audioIoHandle->unprepare();
1167 } catch (CAudioError e) {
1168 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1169 return __convert_CAudioError(e);
1172 return AUDIO_IO_ERROR_NONE;
1175 int cpp_audio_out_pause(audio_out_h output) {
1176 audio_io_s* handle = static_cast<audio_io_s*>(output);
1179 if (handle == NULL) {
1180 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1183 assert(handle->audioIoHandle);
1185 handle->audioIoHandle->pause();
1186 } catch (CAudioError e) {
1187 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1188 return __convert_CAudioError(e);
1191 return AUDIO_IO_ERROR_NONE;
1194 int cpp_audio_out_resume(audio_out_h output) {
1195 audio_io_s* handle = static_cast<audio_io_s*>(output);
1198 if (handle == NULL) {
1199 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1202 assert(handle->audioIoHandle);
1204 handle->audioIoHandle->resume();
1205 } catch (CAudioError e) {
1206 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1207 return __convert_CAudioError(e);
1210 return AUDIO_IO_ERROR_NONE;
1213 int cpp_audio_out_drain(audio_out_h output) {
1214 audio_io_s* handle = static_cast<audio_io_s*>(output);
1217 if (handle == NULL) {
1218 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1221 assert(handle->audioIoHandle);
1223 handle->audioIoHandle->drain();
1224 } catch (CAudioError e) {
1225 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1226 return __convert_CAudioError(e);
1229 return AUDIO_IO_ERROR_NONE;
1232 int cpp_audio_out_flush(audio_out_h output) {
1233 audio_io_s* handle = static_cast<audio_io_s*>(output);
1236 if (handle == NULL) {
1237 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1240 assert(handle->audioIoHandle);
1242 handle->audioIoHandle->flush();
1243 } catch (CAudioError e) {
1244 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1245 return __convert_CAudioError(e);
1248 return AUDIO_IO_ERROR_NONE;
1251 int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) {
1252 audio_io_s* handle = static_cast<audio_io_s*>(output);
1256 if (handle == NULL || buffer == NULL) {
1257 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p, buffer:%p", output, buffer);
1260 assert(handle->audioIoHandle);
1262 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1263 size_t writen = outputHandle->write(buffer, static_cast<size_t>(length));
1264 ret = static_cast<int>(writen);
1265 #ifdef _AUDIO_IO_DEBUG_TIMING_
1266 AUDIO_IO_LOGD("writen:%d", writen);
1268 } catch (CAudioError e) {
1269 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1270 return __convert_CAudioError(e);
1276 int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) {
1277 audio_io_s* handle = static_cast<audio_io_s*>(output);
1280 if (handle == NULL || size == NULL) {
1281 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, size:%p", output, size);
1284 assert(handle->audioIoHandle);
1286 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1287 *size = outputHandle->getBufferSize();
1288 } catch (CAudioError e) {
1289 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1290 return __convert_CAudioError(e);
1293 return AUDIO_IO_ERROR_NONE;
1296 int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
1297 audio_io_s* handle = static_cast<audio_io_s*>(output);
1300 if (handle == NULL || sample_rate == NULL) {
1301 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
1304 assert(handle->audioIoHandle);
1305 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
1306 } catch (CAudioError e) {
1307 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1308 return __convert_CAudioError(e);
1311 return AUDIO_IO_ERROR_NONE;
1314 int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
1315 audio_io_s* handle = static_cast<audio_io_s*>(output);
1318 if (handle == NULL || channel == NULL) {
1319 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, channel:%p", output, channel);
1322 assert(handle->audioIoHandle);
1324 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
1325 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
1326 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
1328 *channel = dstChannel;
1329 } catch (CAudioError e) {
1330 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1331 return __convert_CAudioError(e);
1334 return AUDIO_IO_ERROR_NONE;
1337 int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) {
1338 audio_io_s* handle = static_cast<audio_io_s*>(output);
1341 if (handle == NULL || type == NULL) {
1342 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1345 assert(handle->audioIoHandle);
1347 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
1348 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
1349 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
1351 *type = dstSampleType;
1352 } catch (CAudioError e) {
1353 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1354 return __convert_CAudioError(e);
1357 return AUDIO_IO_ERROR_NONE;
1360 int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) {
1361 audio_io_s* handle = static_cast<audio_io_s*>(output);
1364 if (handle == NULL || type == NULL) {
1365 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1368 assert(handle->audioIoHandle);
1370 const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType();
1371 sound_type_e dstSoundType = SOUND_TYPE_MEDIA;
1372 __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType);
1374 *type = dstSoundType;
1375 } catch (CAudioError e) {
1376 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1377 return __convert_CAudioError(e);
1380 return AUDIO_IO_ERROR_NONE;
1383 int cpp_audio_out_set_interrupted_cb(audio_out_h output, audio_io_interrupted_cb callback, void *user_data) {
1384 audio_io_s* handle = static_cast<audio_io_s*>(output);
1387 if (handle == NULL || callback == NULL) {
1388 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1391 assert(handle->audioIoHandle);
1393 handle->interrupt_callback.onInterrupt = callback;
1394 handle->interrupt_callback.user_data = user_data;
1396 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1397 cb.mUserData = static_cast<void*>(handle);
1398 cb.onInterrupt = __interrupt_cb_internal;
1400 handle->audioIoHandle->setInterruptCallback(cb);
1401 } catch (CAudioError e) {
1402 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1403 return __convert_CAudioError(e);
1406 return AUDIO_IO_ERROR_NONE;
1409 int cpp_audio_out_unset_interrupted_cb(audio_out_h output) {
1410 audio_io_s* handle = static_cast<audio_io_s*>(output);
1413 if (handle == NULL) {
1414 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1417 assert(handle->audioIoHandle);
1419 handle->interrupt_callback.onInterrupt = NULL;
1420 handle->interrupt_callback.user_data = NULL;
1422 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1423 cb.mUserData = NULL;
1424 cb.onInterrupt = NULL;
1426 handle->audioIoHandle->setInterruptCallback(cb);
1427 } catch (CAudioError e) {
1428 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1429 return __convert_CAudioError(e);
1432 return AUDIO_IO_ERROR_NONE;
1435 int cpp_audio_out_ignore_session(audio_out_h output) {
1436 audio_io_s* handle = static_cast<audio_io_s*>(output);
1439 if (handle == NULL) {
1440 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1443 if (handle->stream_callback.onStream) {
1444 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
1447 assert(handle->audioIoHandle);
1449 handle->audioIoHandle->ignoreSession();
1450 } catch (CAudioError e) {
1451 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1452 return __convert_CAudioError(e);
1455 return AUDIO_IO_ERROR_NONE;
1458 int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) {
1459 audio_io_s* handle = static_cast<audio_io_s*>(output);
1462 if (handle == NULL || callback == NULL) {
1463 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1466 assert(handle->audioIoHandle);
1468 handle->stream_callback.onStream = callback;
1469 handle->stream_callback.user_data = user_data;
1471 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1472 cb.mUserData = static_cast<void*>(handle);
1473 cb.onStream = __stream_cb_internal;
1475 handle->audioIoHandle->setStreamCallback(cb);
1476 } catch (CAudioError e) {
1477 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1478 return __convert_CAudioError(e);
1481 return AUDIO_IO_ERROR_NONE;
1484 int cpp_audio_out_unset_stream_cb(audio_out_h output) {
1485 audio_io_s* handle = static_cast<audio_io_s*>(output);
1488 if (handle == NULL) {
1489 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1492 assert(handle->audioIoHandle);
1494 handle->stream_callback.onStream = NULL;
1495 handle->stream_callback.user_data = NULL;
1497 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1498 cb.mUserData = NULL;
1501 handle->audioIoHandle->setStreamCallback(cb);
1502 } catch (CAudioError e) {
1503 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1504 return __convert_CAudioError(e);
1507 return AUDIO_IO_ERROR_NONE;
1510 int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) {
1511 audio_io_s* handle = static_cast<audio_io_s*>(output);
1514 if (handle == NULL || callback == NULL) {
1515 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1518 assert(handle->audioIoHandle);
1520 handle->state_changed_callback.onStateChanged = callback;
1521 handle->state_changed_callback.user_data = user_data;
1523 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1524 cb.mUserData = static_cast<void*>(handle);
1525 cb.onStateChanged = __state_changed_cb_internal;
1527 handle->audioIoHandle->setStateChangedCallback(cb);
1528 } catch (CAudioError e) {
1529 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1530 return __convert_CAudioError(e);
1533 return AUDIO_IO_ERROR_NONE;
1536 int cpp_audio_out_unset_state_changed_cb(audio_out_h output) {
1537 audio_io_s* handle = static_cast<audio_io_s*>(output);
1540 if (handle == NULL) {
1541 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1544 assert(handle->audioIoHandle);
1546 handle->state_changed_callback.onStateChanged = NULL;
1547 handle->state_changed_callback.user_data = NULL;
1549 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1550 cb.mUserData = NULL;
1551 cb.onStateChanged = NULL;
1553 handle->audioIoHandle->setStateChangedCallback(cb);
1554 } catch (CAudioError e) {
1555 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1556 return __convert_CAudioError(e);
1559 return AUDIO_IO_ERROR_NONE;