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_DEVICE_POLICY_RESTRICTION:
118 ret = AUDIO_IO_ERROR_DEVICE_POLICY_RESTRICTION;
120 case CAudioError::EError::ERROR_NOT_SUPPORTED:
121 ret = AUDIO_IO_ERROR_NOT_SUPPORTED;
123 case CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE:
124 ret = AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE;
126 case CAudioError::EError::ERROR_MAX:
127 case CAudioError::EError::ERROR_INTERNAL_OPERATION:
128 case CAudioError::EError::ERROR_NOT_INITIALIZED:
129 case CAudioError::EError::ERROR_FAILED_OPERATION:
130 case CAudioError::EError::ERROR_INVALID_OPERATION:
131 ret = AUDIO_IO_ERROR_INVALID_OPERATION;
133 case CAudioError::EError::ERROR_OUT_OF_MEMORY:
134 case CAudioError::EError::ERROR_INVALID_POINTER:
135 ret = AUDIO_IO_ERROR_INVALID_BUFFER;
137 case CAudioError::EError::ERROR_POLICY_BLOCKED:
138 case CAudioError::EError::ERROR_POLICY_INTERRUPTED:
139 case CAudioError::EError::ERROR_POLICY_DUPLICATED:
140 ret = AUDIO_IO_ERROR_SOUND_POLICY;
147 static void __convert_channel_2_audio_info_channel(const audio_channel_e& src_channel, CAudioInfo::EChannel& dst_channel) {
148 switch (src_channel) {
149 case AUDIO_CHANNEL_MONO:
150 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
152 case AUDIO_CHANNEL_STEREO:
153 dst_channel = CAudioInfo::EChannel::CHANNEL_STEREO;
156 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
160 static void __convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel, audio_channel_e& dst_channel) {
161 switch (src_channel) {
162 case CAudioInfo::EChannel::CHANNEL_MONO:
163 dst_channel = AUDIO_CHANNEL_MONO;
165 case CAudioInfo::EChannel::CHANNEL_STEREO:
166 dst_channel = AUDIO_CHANNEL_STEREO;
169 dst_channel = AUDIO_CHANNEL_MONO;
173 static void __convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type, CAudioInfo::ESampleType& dst_type) {
175 case AUDIO_SAMPLE_TYPE_U8:
176 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
178 case AUDIO_SAMPLE_TYPE_S16_LE:
179 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE;
182 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
186 static void __convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type, audio_sample_type_e& dst_type) {
188 case CAudioInfo::ESampleType::SAMPLE_TYPE_U8:
189 dst_type = AUDIO_SAMPLE_TYPE_U8;
191 case CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE:
192 dst_type = AUDIO_SAMPLE_TYPE_S16_LE;
195 dst_type = AUDIO_SAMPLE_TYPE_U8;
199 static void __convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type, CAudioInfo::EAudioType& dst_type) {
201 case SOUND_TYPE_SYSTEM:
202 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
204 case SOUND_TYPE_NOTIFICATION:
205 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION;
207 case SOUND_TYPE_ALARM:
208 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM;
210 case SOUND_TYPE_RINGTONE:
211 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP;
213 case SOUND_TYPE_MEDIA:
214 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
216 case SOUND_TYPE_CALL:
217 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
219 case SOUND_TYPE_VOIP:
220 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP;
222 case SOUND_TYPE_VOICE:
223 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION;
226 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
231 static void __convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type, sound_type_e& dst_type) {
233 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA:
234 dst_type = SOUND_TYPE_MEDIA;
236 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM:
237 dst_type = SOUND_TYPE_SYSTEM;
239 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM:
240 dst_type = SOUND_TYPE_ALARM;
242 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION:
243 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_EMERGENCY:
244 dst_type = SOUND_TYPE_NOTIFICATION;
246 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION:
247 dst_type = SOUND_TYPE_VOICE;
249 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP:
250 dst_type = SOUND_TYPE_RINGTONE;
252 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP:
253 dst_type = SOUND_TYPE_VOIP;
256 dst_type = SOUND_TYPE_MEDIA;
261 static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src_state) {
262 audio_io_state_e dst_state;
264 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE:
265 dst_state = AUDIO_IO_STATE_IDLE;
267 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE:
268 dst_state = AUDIO_IO_STATE_IDLE;
270 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING:
271 dst_state = AUDIO_IO_STATE_RUNNING;
273 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED:
274 dst_state = AUDIO_IO_STATE_PAUSED;
277 dst_state = AUDIO_IO_STATE_IDLE;
282 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) throw(CAudioError) {
283 if (sample_rate < 0) {
284 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate);
287 if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) {
288 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel);
291 if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE) {
292 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type);
296 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) throw(CAudioError) {
297 __check_audio_param(sample_rate, channel, type);
299 if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE) {
300 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type);
304 static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw(CAudioError) {
305 CAudioInfo::EChannel dstChannel;
306 CAudioInfo::ESampleType dstSampleType;
307 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
309 __convert_channel_2_audio_info_channel(channel, dstChannel);
310 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
312 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
315 static CAudioInfo __generate_audio_input_loopback_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw(CAudioError) {
316 CAudioInfo::EChannel dstChannel;
317 CAudioInfo::ESampleType dstSampleType;
318 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_LOOPBACK;
320 __convert_channel_2_audio_info_channel(channel, dstChannel);
321 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
323 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
326 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) {
327 CAudioInfo::EChannel dstChannel;
328 CAudioInfo::ESampleType dstSampleType;
329 CAudioInfo::EAudioType dstAudioType;
331 __convert_channel_2_audio_info_channel(channel, dstChannel);
332 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
333 __convert_sound_type_2_audio_info_audio_type(sound_type, dstAudioType);
335 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
338 static audio_io_interrupted_code_e __convert_interrupted_code(IAudioSessionEventListener::EInterruptCode code) {
340 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED:
341 return AUDIO_IO_INTERRUPTED_COMPLETED;
342 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_CALL:
343 return AUDIO_IO_INTERRUPTED_BY_CALL;
344 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EARJACK_UNPLUG:
345 return AUDIO_IO_INTERRUPTED_BY_EARJACK_UNPLUG;
346 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_RESOURCE_CONFLICT:
347 return AUDIO_IO_INTERRUPTED_BY_RESOURCE_CONFLICT;
348 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_ALARM:
349 return AUDIO_IO_INTERRUPTED_BY_ALARM;
350 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EMERGENCY:
351 return AUDIO_IO_INTERRUPTED_BY_EMERGENCY;
352 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_NOTIFICATION:
353 return AUDIO_IO_INTERRUPTED_BY_NOTIFICATION;
354 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_MEDIA:
355 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_MAX:
357 return AUDIO_IO_INTERRUPTED_BY_MEDIA;
362 * Implements CAPI functions
364 int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_in_h *input) {
365 audio_io_s* handle = NULL;
368 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
371 __check_audio_param(sample_rate, channel, type);
373 handle = new audio_io_s;
374 if (handle == NULL) {
375 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
378 CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type);
380 handle->audioIoHandle = new CAudioInput(audioInfo);
381 if (handle == NULL) {
382 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
385 handle->audioIoHandle->initialize();
388 } catch (CAudioError e) {
389 AUDIO_IO_LOGE("%s", e.getErrorMsg());
391 VALID_POINTER_START(handle)
392 SAFE_FINALIZE(handle->audioIoHandle);
393 SAFE_DELETE(handle->audioIoHandle);
397 VALID_POINTER_START(input)
401 return __convert_CAudioError(e);
404 return AUDIO_IO_ERROR_NONE;
407 int cpp_audio_in_create_loopback(int sample_rate, audio_channel_e channel, audio_sample_type_e type , audio_in_h* input) {
408 audio_io_s* handle = NULL;
411 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
414 __check_audio_param(sample_rate, channel, type);
416 handle = new audio_io_s;
417 if (handle == NULL) {
418 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
421 CAudioInfo audioInfo = __generate_audio_input_loopback_info(sample_rate, channel, type);
423 handle->audioIoHandle = new CAudioInput(audioInfo);
424 if (handle == NULL) {
425 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
428 handle->audioIoHandle->initialize();
431 } catch (CAudioError e) {
432 AUDIO_IO_LOGE("%s", e.getErrorMsg());
434 VALID_POINTER_START(handle)
435 SAFE_FINALIZE(handle->audioIoHandle);
436 SAFE_DELETE(handle->audioIoHandle);
440 VALID_POINTER_START(input)
444 return __convert_CAudioError(e);
447 return AUDIO_IO_ERROR_NONE;
450 int cpp_audio_in_destroy(audio_in_h input) {
451 audio_io_s* handle = static_cast<audio_io_s*>(input);
454 if (handle == NULL) {
455 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
458 assert(handle->audioIoHandle);
460 SAFE_FINALIZE(handle->audioIoHandle);
461 SAFE_DELETE(handle->audioIoHandle);
463 } catch (CAudioError e) {
464 AUDIO_IO_LOGE("%s", e.getErrorMsg());
465 return __convert_CAudioError(e);
468 return AUDIO_IO_ERROR_NONE;
471 int cpp_audio_in_set_stream_info(audio_in_h input, sound_stream_info_h stream_info) {
472 audio_io_s* handle = static_cast<audio_io_s*>(input);
475 if (handle == NULL || stream_info == NULL) {
476 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, stream_info:%p", input, stream_info);
479 assert(handle->audioIoHandle);
481 int errorCode = SOUND_MANAGER_ERROR_NONE;
482 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
487 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
488 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
492 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
493 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
495 handle->audioIoHandle->getAudioInfo().convertInputStreamType2AudioType(type, &audioType);
496 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
498 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
499 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
501 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
503 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported");
505 } catch (CAudioError e) {
506 AUDIO_IO_LOGE("%s", e.getErrorMsg());
507 return __convert_CAudioError(e);
510 return AUDIO_IO_ERROR_NONE;
513 int cpp_audio_in_prepare(audio_in_h input) {
514 audio_io_s* handle = static_cast<audio_io_s*>(input);
517 if (handle == NULL) {
518 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
521 assert(handle->audioIoHandle);
523 handle->audioIoHandle->prepare();
524 } catch (CAudioError e) {
525 AUDIO_IO_LOGE("%s", e.getErrorMsg());
526 return __convert_CAudioError(e);
529 return AUDIO_IO_ERROR_NONE;
532 int cpp_audio_in_unprepare(audio_in_h input) {
533 audio_io_s* handle = static_cast<audio_io_s*>(input);
536 if (handle == NULL) {
537 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
540 assert(handle->audioIoHandle);
542 handle->audioIoHandle->unprepare();
543 } catch (CAudioError e) {
544 AUDIO_IO_LOGE("%s", e.getErrorMsg());
545 return __convert_CAudioError(e);
548 return AUDIO_IO_ERROR_NONE;
551 int cpp_audio_in_pause(audio_in_h input) {
552 audio_io_s* handle = static_cast<audio_io_s*>(input);
555 if (handle == NULL) {
556 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
559 assert(handle->audioIoHandle);
561 handle->audioIoHandle->pause();
562 } catch (CAudioError e) {
563 AUDIO_IO_LOGE("%s", e.getErrorMsg());
564 return __convert_CAudioError(e);
567 return AUDIO_IO_ERROR_NONE;
570 int cpp_audio_in_resume(audio_in_h input) {
571 audio_io_s* handle = static_cast<audio_io_s*>(input);
574 if (handle == NULL) {
575 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
578 assert(handle->audioIoHandle);
580 handle->audioIoHandle->resume();
581 } catch (CAudioError e) {
582 AUDIO_IO_LOGE("%s", e.getErrorMsg());
583 return __convert_CAudioError(e);
586 return AUDIO_IO_ERROR_NONE;
589 int cpp_audio_in_drain(audio_in_h input) {
590 audio_io_s* handle = static_cast<audio_io_s*>(input);
593 if (handle == NULL) {
594 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
597 assert(handle->audioIoHandle);
599 handle->audioIoHandle->drain();
600 } catch (CAudioError e) {
601 AUDIO_IO_LOGE("%s", e.getErrorMsg());
602 return __convert_CAudioError(e);
605 return AUDIO_IO_ERROR_NONE;
608 int cpp_audio_in_flush(audio_in_h input) {
609 audio_io_s* handle = static_cast<audio_io_s*>(input);
612 if (handle == NULL) {
613 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
616 assert(handle->audioIoHandle);
618 handle->audioIoHandle->flush();
619 } catch (CAudioError e) {
620 AUDIO_IO_LOGE("%s", e.getErrorMsg());
621 return __convert_CAudioError(e);
624 return AUDIO_IO_ERROR_NONE;
627 int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) {
628 audio_io_s* handle = static_cast<audio_io_s*>(input);
632 if (handle == NULL || buffer == NULL) {
633 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
636 assert(handle->audioIoHandle);
638 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
639 if (inputHandle == NULL) {
640 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
642 size_t readn = inputHandle->read(buffer, static_cast<size_t>(length));
643 ret = static_cast<int>(readn);
644 #ifdef _AUDIO_IO_DEBUG_TIMING_
645 AUDIO_IO_LOGD("readn:%d", readn);
647 } catch (CAudioError e) {
648 AUDIO_IO_LOGE("%s", e.getErrorMsg());
649 return __convert_CAudioError(e);
655 int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) {
656 audio_io_s* handle = static_cast<audio_io_s*>(input);
659 if (handle == NULL || size == NULL) {
660 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, size:%p", input, size);
663 assert(handle->audioIoHandle);
665 CAudioIO* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
666 if (inputHandle == NULL) {
667 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
669 *size = inputHandle->getBufferSize();
670 } catch (CAudioError e) {
671 AUDIO_IO_LOGE("%s", e.getErrorMsg());
672 return __convert_CAudioError(e);
675 return AUDIO_IO_ERROR_NONE;
678 int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) {
679 audio_io_s* handle = static_cast<audio_io_s*>(input);
682 if (handle == NULL || sample_rate == NULL) {
683 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
686 assert(handle->audioIoHandle);
687 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
688 } catch (CAudioError e) {
689 AUDIO_IO_LOGE("%s", e.getErrorMsg());
690 return __convert_CAudioError(e);
693 return AUDIO_IO_ERROR_NONE;
696 int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
697 audio_io_s* handle = static_cast<audio_io_s*>(input);
700 if (handle == NULL || channel == NULL) {
701 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, channel:%p", input, channel);
704 assert(handle->audioIoHandle);
706 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
707 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
708 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
710 *channel = dstChannel;
711 } catch (CAudioError e) {
712 AUDIO_IO_LOGE("%s", e.getErrorMsg());
713 return __convert_CAudioError(e);
716 return AUDIO_IO_ERROR_NONE;
719 int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
720 audio_io_s* handle = static_cast<audio_io_s*>(input);
723 if (handle == NULL || type == NULL) {
724 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, type:%p", input, type);
727 assert(handle->audioIoHandle);
729 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
730 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
731 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
733 *type = dstSampleType;
734 } catch (CAudioError e) {
735 AUDIO_IO_LOGE("%s", e.getErrorMsg());
736 return __convert_CAudioError(e);
739 return AUDIO_IO_ERROR_NONE;
742 static void __interrupt_cb_internal(IAudioSessionEventListener::EInterruptCode _code, void* user_data) {
743 audio_io_s* handle = static_cast<audio_io_s*>(user_data);
744 audio_io_interrupted_code_e code = __convert_interrupted_code(_code);
748 if (handle->interrupt_callback.onInterrupt != NULL) {
749 handle->interrupt_callback.onInterrupt(code, handle->interrupt_callback.user_data);
753 int cpp_audio_in_set_interrupted_cb(audio_in_h input, audio_io_interrupted_cb callback, void *user_data) {
754 audio_io_s* handle = static_cast<audio_io_s*>(input);
757 if (handle == NULL || callback == NULL) {
758 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
761 assert(handle->audioIoHandle);
763 handle->interrupt_callback.onInterrupt = callback;
764 handle->interrupt_callback.user_data = user_data;
766 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
767 cb.mUserData = static_cast<void*>(handle);
768 cb.onInterrupt = __interrupt_cb_internal;
770 handle->audioIoHandle->setInterruptCallback(cb);
771 } catch (CAudioError e) {
772 AUDIO_IO_LOGE("%s", e.getErrorMsg());
773 return __convert_CAudioError(e);
776 return AUDIO_IO_ERROR_NONE;
779 int cpp_audio_in_unset_interrupted_cb(audio_in_h input) {
780 audio_io_s* handle = static_cast<audio_io_s*>(input);
783 if (handle == NULL) {
784 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
787 assert(handle->audioIoHandle);
789 handle->interrupt_callback.onInterrupt = NULL;
790 handle->interrupt_callback.user_data = NULL;
792 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
794 cb.onInterrupt = NULL;
796 handle->audioIoHandle->setInterruptCallback(cb);
797 } catch (CAudioError e) {
798 AUDIO_IO_LOGE("%s", e.getErrorMsg());
799 return __convert_CAudioError(e);
802 return AUDIO_IO_ERROR_NONE;
805 int cpp_audio_in_ignore_session(audio_in_h input) {
806 audio_io_s* handle = static_cast<audio_io_s*>(input);
809 if (handle == NULL) {
810 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
813 if (handle->stream_callback.onStream) {
814 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
817 assert(handle->audioIoHandle);
819 handle->audioIoHandle->ignoreSession();
820 } catch (CAudioError e) {
821 AUDIO_IO_LOGE("%s", e.getErrorMsg());
822 return __convert_CAudioError(e);
825 return AUDIO_IO_ERROR_NONE;
828 static void __stream_cb_internal(size_t nbytes, void *user_data) {
829 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
832 if (audioIo->stream_callback.onStream != NULL) {
833 audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data);
837 static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState state_prev, bool by_policy, void *user_data) {
838 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
841 if (audioIo->state_changed_callback.onStateChanged != NULL) {
842 audioIo->state_changed_callback.onStateChanged(audioIo, __convert_state_type(state_prev), __convert_state_type(state), by_policy, audioIo->state_changed_callback.user_data);
846 int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) {
847 audio_io_s* handle = static_cast<audio_io_s*>(input);
850 if (handle == NULL || callback == NULL) {
851 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
854 assert(handle->audioIoHandle);
856 handle->stream_callback.onStream = callback;
857 handle->stream_callback.user_data = user_data;
859 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
860 cb.mUserData = static_cast<void*>(handle);
861 cb.onStream = __stream_cb_internal;
863 handle->audioIoHandle->setStreamCallback(cb);
864 } catch (CAudioError e) {
865 AUDIO_IO_LOGE("%s", e.getErrorMsg());
866 return __convert_CAudioError(e);
869 return AUDIO_IO_ERROR_NONE;
872 int cpp_audio_in_unset_stream_cb(audio_in_h input) {
873 audio_io_s* handle = static_cast<audio_io_s*>(input);
876 if (handle == NULL) {
877 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
880 assert(handle->audioIoHandle);
882 handle->stream_callback.onStream = NULL;
883 handle->stream_callback.user_data = NULL;
885 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
889 handle->audioIoHandle->setStreamCallback(cb);
890 } catch (CAudioError e) {
891 AUDIO_IO_LOGE("%s", e.getErrorMsg());
892 return __convert_CAudioError(e);
895 return AUDIO_IO_ERROR_NONE;
898 int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) {
899 audio_io_s* handle = static_cast<audio_io_s*>(input);
903 if (handle == NULL || buffer == NULL) {
904 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
907 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
910 inputHandle->peek(buffer, &_length);
911 } catch (CAudioError e) {
912 AUDIO_IO_LOGE("%s", e.getErrorMsg());
913 return __convert_CAudioError(e);
916 *length = (unsigned int)_length;
918 return AUDIO_IO_ERROR_NONE;
921 int cpp_audio_in_drop(audio_in_h input) {
922 audio_io_s* handle = static_cast<audio_io_s*>(input);
925 if (handle == NULL) {
926 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
929 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
933 } catch (CAudioError e) {
934 AUDIO_IO_LOGE("%s", e.getErrorMsg());
935 return __convert_CAudioError(e);
938 return AUDIO_IO_ERROR_NONE;
941 int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) {
942 audio_io_s* handle = static_cast<audio_io_s*>(input);
945 if (handle == NULL || callback == NULL) {
946 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
949 assert(handle->audioIoHandle);
951 handle->state_changed_callback.onStateChanged = callback;
952 handle->state_changed_callback.user_data = user_data;
954 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
955 cb.mUserData = static_cast<void*>(handle);
956 cb.onStateChanged = __state_changed_cb_internal;
958 handle->audioIoHandle->setStateChangedCallback(cb);
959 } catch (CAudioError e) {
960 AUDIO_IO_LOGE("%s", e.getErrorMsg());
961 return __convert_CAudioError(e);
964 return AUDIO_IO_ERROR_NONE;
967 int cpp_audio_in_unset_state_changed_cb(audio_in_h input) {
968 audio_io_s* handle = static_cast<audio_io_s*>(input);
971 if (handle == NULL) {
972 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", input);
975 assert(handle->audioIoHandle);
977 handle->state_changed_callback.onStateChanged = NULL;
978 handle->state_changed_callback.user_data = NULL;
980 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
982 cb.onStateChanged = NULL;
984 handle->audioIoHandle->setStateChangedCallback(cb);
985 } catch (CAudioError e) {
986 AUDIO_IO_LOGE("%s", e.getErrorMsg());
987 return __convert_CAudioError(e);
990 return AUDIO_IO_ERROR_NONE;
997 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) {
998 audio_io_s* handle = NULL;
1000 if (output == NULL) {
1001 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1004 __check_audio_param(sample_rate, channel, type, sound_type);
1006 handle = new audio_io_s;
1007 if (handle == NULL) {
1008 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1011 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, sound_type);
1013 handle->audioIoHandle = new CAudioOutput(audioInfo);
1014 if (handle == NULL) {
1015 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1018 handle->audioIoHandle->initialize();
1021 } catch (CAudioError e) {
1022 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1024 VALID_POINTER_START(handle)
1025 SAFE_FINALIZE(handle->audioIoHandle);
1026 SAFE_DELETE(handle->audioIoHandle);
1027 SAFE_DELETE(handle);
1030 VALID_POINTER_START(output)
1034 return __convert_CAudioError(e);
1037 return AUDIO_IO_ERROR_NONE;
1040 int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) {
1041 audio_io_s* handle = NULL;
1043 if (output == NULL) {
1044 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1047 __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */);
1049 handle = new audio_io_s;
1050 if (handle == NULL) {
1051 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1054 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA /* default sound_type */);
1056 handle->audioIoHandle = new CAudioOutput(audioInfo);
1057 if (handle == NULL) {
1058 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1061 handle->audioIoHandle->initialize();
1064 } catch (CAudioError e) {
1065 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1067 VALID_POINTER_START(handle)
1068 SAFE_FINALIZE(handle->audioIoHandle);
1069 SAFE_DELETE(handle->audioIoHandle);
1070 SAFE_DELETE(handle);
1073 VALID_POINTER_START(output)
1077 return __convert_CAudioError(e);
1080 return AUDIO_IO_ERROR_NONE;
1083 int cpp_audio_out_destroy(audio_out_h output) {
1084 audio_io_s* handle = static_cast<audio_io_s*>(output);
1087 if (handle == NULL) {
1088 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1091 assert(handle->audioIoHandle);
1093 SAFE_FINALIZE(handle->audioIoHandle);
1094 SAFE_DELETE(handle->audioIoHandle);
1095 SAFE_DELETE(handle);
1096 } catch (CAudioError e) {
1097 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1098 return __convert_CAudioError(e);
1101 return AUDIO_IO_ERROR_NONE;
1104 int cpp_audio_out_set_stream_info(audio_out_h output, sound_stream_info_h stream_info) {
1105 audio_io_s* handle = static_cast<audio_io_s*>(output);
1108 if (handle == NULL || stream_info == NULL) {
1109 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, stream_info:%p", output, stream_info);
1112 assert(handle->audioIoHandle);
1114 int errorCode = SOUND_MANAGER_ERROR_NONE;
1115 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
1120 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
1121 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
1125 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
1126 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
1128 handle->audioIoHandle->getAudioInfo().convertOutputStreamType2AudioType(type, &audioType);
1129 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
1131 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
1132 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
1134 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
1136 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Output stream is not supported");
1138 } catch (CAudioError e) {
1139 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1140 return __convert_CAudioError(e);
1143 return AUDIO_IO_ERROR_NONE;
1146 int cpp_audio_out_prepare(audio_out_h output) {
1147 audio_io_s* handle = static_cast<audio_io_s*>(output);
1150 if (handle == NULL) {
1151 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1154 assert(handle->audioIoHandle);
1156 handle->audioIoHandle->prepare();
1157 } catch (CAudioError e) {
1158 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1159 return __convert_CAudioError(e);
1162 return AUDIO_IO_ERROR_NONE;
1165 int cpp_audio_out_unprepare(audio_out_h output) {
1166 audio_io_s* handle = static_cast<audio_io_s*>(output);
1169 if (handle == NULL) {
1170 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1173 assert(handle->audioIoHandle);
1175 handle->audioIoHandle->unprepare();
1176 } catch (CAudioError e) {
1177 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1178 return __convert_CAudioError(e);
1181 return AUDIO_IO_ERROR_NONE;
1184 int cpp_audio_out_pause(audio_out_h output) {
1185 audio_io_s* handle = static_cast<audio_io_s*>(output);
1188 if (handle == NULL) {
1189 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1192 assert(handle->audioIoHandle);
1194 handle->audioIoHandle->pause();
1195 } catch (CAudioError e) {
1196 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1197 return __convert_CAudioError(e);
1200 return AUDIO_IO_ERROR_NONE;
1203 int cpp_audio_out_resume(audio_out_h output) {
1204 audio_io_s* handle = static_cast<audio_io_s*>(output);
1207 if (handle == NULL) {
1208 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1211 assert(handle->audioIoHandle);
1213 handle->audioIoHandle->resume();
1214 } catch (CAudioError e) {
1215 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1216 return __convert_CAudioError(e);
1219 return AUDIO_IO_ERROR_NONE;
1222 int cpp_audio_out_drain(audio_out_h output) {
1223 audio_io_s* handle = static_cast<audio_io_s*>(output);
1226 if (handle == NULL) {
1227 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1230 assert(handle->audioIoHandle);
1232 handle->audioIoHandle->drain();
1233 } catch (CAudioError e) {
1234 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1235 return __convert_CAudioError(e);
1238 return AUDIO_IO_ERROR_NONE;
1241 int cpp_audio_out_flush(audio_out_h output) {
1242 audio_io_s* handle = static_cast<audio_io_s*>(output);
1245 if (handle == NULL) {
1246 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1249 assert(handle->audioIoHandle);
1251 handle->audioIoHandle->flush();
1252 } catch (CAudioError e) {
1253 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1254 return __convert_CAudioError(e);
1257 return AUDIO_IO_ERROR_NONE;
1260 int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) {
1261 audio_io_s* handle = static_cast<audio_io_s*>(output);
1265 if (handle == NULL || buffer == NULL) {
1266 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p, buffer:%p", output, buffer);
1269 assert(handle->audioIoHandle);
1271 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1272 if (outputHandle == NULL) {
1273 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1275 size_t writen = outputHandle->write(buffer, static_cast<size_t>(length));
1276 ret = static_cast<int>(writen);
1277 #ifdef _AUDIO_IO_DEBUG_TIMING_
1278 AUDIO_IO_LOGD("writen:%d", writen);
1280 } catch (CAudioError e) {
1281 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1282 return __convert_CAudioError(e);
1288 int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) {
1289 audio_io_s* handle = static_cast<audio_io_s*>(output);
1292 if (handle == NULL || size == NULL) {
1293 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, size:%p", output, size);
1296 assert(handle->audioIoHandle);
1298 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1299 if (outputHandle == NULL) {
1300 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1302 *size = outputHandle->getBufferSize();
1303 } catch (CAudioError e) {
1304 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1305 return __convert_CAudioError(e);
1308 return AUDIO_IO_ERROR_NONE;
1311 int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
1312 audio_io_s* handle = static_cast<audio_io_s*>(output);
1315 if (handle == NULL || sample_rate == NULL) {
1316 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
1319 assert(handle->audioIoHandle);
1320 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
1321 } catch (CAudioError e) {
1322 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1323 return __convert_CAudioError(e);
1326 return AUDIO_IO_ERROR_NONE;
1329 int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
1330 audio_io_s* handle = static_cast<audio_io_s*>(output);
1333 if (handle == NULL || channel == NULL) {
1334 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, channel:%p", output, channel);
1337 assert(handle->audioIoHandle);
1339 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
1340 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
1341 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
1343 *channel = dstChannel;
1344 } catch (CAudioError e) {
1345 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1346 return __convert_CAudioError(e);
1349 return AUDIO_IO_ERROR_NONE;
1352 int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) {
1353 audio_io_s* handle = static_cast<audio_io_s*>(output);
1356 if (handle == NULL || type == NULL) {
1357 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1360 assert(handle->audioIoHandle);
1362 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
1363 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
1364 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
1366 *type = dstSampleType;
1367 } catch (CAudioError e) {
1368 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1369 return __convert_CAudioError(e);
1372 return AUDIO_IO_ERROR_NONE;
1375 int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) {
1376 audio_io_s* handle = static_cast<audio_io_s*>(output);
1379 if (handle == NULL || type == NULL) {
1380 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1383 assert(handle->audioIoHandle);
1385 const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType();
1386 sound_type_e dstSoundType = SOUND_TYPE_MEDIA;
1387 __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType);
1389 *type = dstSoundType;
1390 } catch (CAudioError e) {
1391 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1392 return __convert_CAudioError(e);
1395 return AUDIO_IO_ERROR_NONE;
1398 int cpp_audio_out_set_interrupted_cb(audio_out_h output, audio_io_interrupted_cb callback, void *user_data) {
1399 audio_io_s* handle = static_cast<audio_io_s*>(output);
1402 if (handle == NULL || callback == NULL) {
1403 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1406 assert(handle->audioIoHandle);
1408 handle->interrupt_callback.onInterrupt = callback;
1409 handle->interrupt_callback.user_data = user_data;
1411 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1412 cb.mUserData = static_cast<void*>(handle);
1413 cb.onInterrupt = __interrupt_cb_internal;
1415 handle->audioIoHandle->setInterruptCallback(cb);
1416 } catch (CAudioError e) {
1417 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1418 return __convert_CAudioError(e);
1421 return AUDIO_IO_ERROR_NONE;
1424 int cpp_audio_out_unset_interrupted_cb(audio_out_h output) {
1425 audio_io_s* handle = static_cast<audio_io_s*>(output);
1428 if (handle == NULL) {
1429 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1432 assert(handle->audioIoHandle);
1434 handle->interrupt_callback.onInterrupt = NULL;
1435 handle->interrupt_callback.user_data = NULL;
1437 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1438 cb.mUserData = NULL;
1439 cb.onInterrupt = NULL;
1441 handle->audioIoHandle->setInterruptCallback(cb);
1442 } catch (CAudioError e) {
1443 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1444 return __convert_CAudioError(e);
1447 return AUDIO_IO_ERROR_NONE;
1450 int cpp_audio_out_ignore_session(audio_out_h output) {
1451 audio_io_s* handle = static_cast<audio_io_s*>(output);
1454 if (handle == NULL) {
1455 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1458 if (handle->stream_callback.onStream) {
1459 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
1462 assert(handle->audioIoHandle);
1464 handle->audioIoHandle->ignoreSession();
1465 } catch (CAudioError e) {
1466 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1467 return __convert_CAudioError(e);
1470 return AUDIO_IO_ERROR_NONE;
1473 int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) {
1474 audio_io_s* handle = static_cast<audio_io_s*>(output);
1477 if (handle == NULL || callback == NULL) {
1478 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1481 assert(handle->audioIoHandle);
1483 handle->stream_callback.onStream = callback;
1484 handle->stream_callback.user_data = user_data;
1486 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1487 cb.mUserData = static_cast<void*>(handle);
1488 cb.onStream = __stream_cb_internal;
1490 handle->audioIoHandle->setStreamCallback(cb);
1491 } catch (CAudioError e) {
1492 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1493 return __convert_CAudioError(e);
1496 return AUDIO_IO_ERROR_NONE;
1499 int cpp_audio_out_unset_stream_cb(audio_out_h output) {
1500 audio_io_s* handle = static_cast<audio_io_s*>(output);
1503 if (handle == NULL) {
1504 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1507 assert(handle->audioIoHandle);
1509 handle->stream_callback.onStream = NULL;
1510 handle->stream_callback.user_data = NULL;
1512 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1513 cb.mUserData = NULL;
1516 handle->audioIoHandle->setStreamCallback(cb);
1517 } catch (CAudioError e) {
1518 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1519 return __convert_CAudioError(e);
1522 return AUDIO_IO_ERROR_NONE;
1525 int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) {
1526 audio_io_s* handle = static_cast<audio_io_s*>(output);
1529 if (handle == NULL || callback == NULL) {
1530 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1533 assert(handle->audioIoHandle);
1535 handle->state_changed_callback.onStateChanged = callback;
1536 handle->state_changed_callback.user_data = user_data;
1538 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1539 cb.mUserData = static_cast<void*>(handle);
1540 cb.onStateChanged = __state_changed_cb_internal;
1542 handle->audioIoHandle->setStateChangedCallback(cb);
1543 } catch (CAudioError e) {
1544 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1545 return __convert_CAudioError(e);
1548 return AUDIO_IO_ERROR_NONE;
1551 int cpp_audio_out_unset_state_changed_cb(audio_out_h output) {
1552 audio_io_s* handle = static_cast<audio_io_s*>(output);
1555 if (handle == NULL) {
1556 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1559 assert(handle->audioIoHandle);
1561 handle->state_changed_callback.onStateChanged = NULL;
1562 handle->state_changed_callback.user_data = NULL;
1564 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1565 cb.mUserData = NULL;
1566 cb.onStateChanged = NULL;
1568 handle->audioIoHandle->setStateChangedCallback(cb);
1569 } catch (CAudioError e) {
1570 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1571 return __convert_CAudioError(e);
1574 return AUDIO_IO_ERROR_NONE;