2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "cpp_audio_io.h"
19 #include <sound_manager_internal.h>
21 #include "CAudioIODef.h"
23 #include <system_info.h>
25 #define FEATURE_MICROPHONE "http://tizen.org/feature/microphone"
28 using namespace tizen_media_audio;
34 * Name : audio_io_interrupted_cb_s
35 * Declaration : Keeps user callback pointer and user data for delivering an interrupt event
37 typedef struct audio_io_interrupted_cb_s {
39 audio_io_interrupted_cb onInterrupt;
41 audio_io_interrupted_cb_s() : user_data(NULL), onInterrupt(NULL)
43 } audio_io_interrupted_cb_s;
48 * Name : audio_io_stream_cb_s
49 * Declaration : Keeps user callback pointer and user data for delivering an stream event
51 typedef struct audio_io_stream_cb_s {
53 audio_in_stream_cb onStream;
55 audio_io_stream_cb_s() : user_data(NULL), onStream(NULL)
57 } audio_io_stream_cb_s;
62 * Name : audio_io_state_changed_cb_s
63 * Declaration : Keeps user callback pointer and user data for delivering an state changed event
65 typedef struct audio_io_state_changed_cb_s {
67 audio_in_state_changed_cb onStateChanged;
69 audio_io_state_changed_cb_s() : user_data(NULL), onStateChanged(NULL)
71 } audio_io_state_changed_cb_s;
77 * Declaration : An handle of AudioIO
78 * The handle has two struct for user callback
79 * And the handle has a pointer of private audioIO object
80 * The CAudioIO is a abstract class object about Input and Output
82 typedef struct audio_io_s {
83 CAudioIO* audioIoHandle;
84 audio_io_interrupted_cb_s interrupt_callback;
85 audio_io_stream_cb_s stream_callback;
86 audio_io_state_changed_cb_s state_changed_callback;
88 audio_io_s() : audioIoHandle(NULL)
96 static audio_io_error_e __convert_CAudioError(CAudioError& error) {
97 audio_io_error_e ret = AUDIO_IO_ERROR_NONE;
98 CAudioError::EError err = error.getError();
101 case CAudioError::EError::ERROR_NONE:
102 ret = AUDIO_IO_ERROR_NONE;
104 case CAudioError::EError::ERROR_INVALID_ARGUMENT:
105 case CAudioError::EError::ERROR_INVALID_HANDLE:
106 case CAudioError::EError::ERROR_INVALID_SAMPLERATE:
107 case CAudioError::EError::ERROR_INVALID_CHANNEL:
108 case CAudioError::EError::ERROR_INVALID_FORMAT:
109 ret = AUDIO_IO_ERROR_INVALID_PARAMETER;
111 case CAudioError::EError::ERROR_DEVICE_NOT_OPENED:
112 ret = AUDIO_IO_ERROR_DEVICE_NOT_OPENED;
114 case CAudioError::EError::ERROR_DEVICE_NOT_CLOSED:
115 ret = AUDIO_IO_ERROR_DEVICE_NOT_CLOSED;
117 case CAudioError::EError::ERROR_PERMISSION_DENIED:
118 ret = AUDIO_IO_ERROR_PERMISSION_DENIED;
120 case CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION:
121 ret = AUDIO_IO_ERROR_DEVICE_POLICY_RESTRICTION;
123 case CAudioError::EError::ERROR_NOT_SUPPORTED:
124 ret = AUDIO_IO_ERROR_NOT_SUPPORTED;
126 case CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE:
127 ret = AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE;
129 case CAudioError::EError::ERROR_MAX:
130 case CAudioError::EError::ERROR_INTERNAL_OPERATION:
131 case CAudioError::EError::ERROR_NOT_INITIALIZED:
132 case CAudioError::EError::ERROR_FAILED_OPERATION:
133 case CAudioError::EError::ERROR_INVALID_OPERATION:
134 ret = AUDIO_IO_ERROR_INVALID_OPERATION;
136 case CAudioError::EError::ERROR_INVALID_STATE:
137 ret = AUDIO_IO_ERROR_INVALID_STATE;
139 case CAudioError::EError::ERROR_OUT_OF_MEMORY:
140 case CAudioError::EError::ERROR_INVALID_POINTER:
141 ret = AUDIO_IO_ERROR_INVALID_BUFFER;
143 case CAudioError::EError::ERROR_POLICY_BLOCKED:
144 case CAudioError::EError::ERROR_POLICY_INTERRUPTED:
145 case CAudioError::EError::ERROR_POLICY_DUPLICATED:
146 ret = AUDIO_IO_ERROR_SOUND_POLICY;
153 static void __convert_channel_2_audio_info_channel(const audio_channel_e& src_channel, CAudioInfo::EChannel& dst_channel) {
154 switch (src_channel) {
155 case AUDIO_CHANNEL_MONO:
156 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
158 case AUDIO_CHANNEL_STEREO:
159 dst_channel = CAudioInfo::EChannel::CHANNEL_STEREO;
162 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
166 static void __convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel, audio_channel_e& dst_channel) {
167 switch (src_channel) {
168 case CAudioInfo::EChannel::CHANNEL_MONO:
169 dst_channel = AUDIO_CHANNEL_MONO;
171 case CAudioInfo::EChannel::CHANNEL_STEREO:
172 dst_channel = AUDIO_CHANNEL_STEREO;
175 dst_channel = AUDIO_CHANNEL_MONO;
179 static void __convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type, CAudioInfo::ESampleType& dst_type) {
181 case AUDIO_SAMPLE_TYPE_U8:
182 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
184 case AUDIO_SAMPLE_TYPE_S16_LE:
185 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE;
188 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
192 static void __convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type, audio_sample_type_e& dst_type) {
194 case CAudioInfo::ESampleType::SAMPLE_TYPE_U8:
195 dst_type = AUDIO_SAMPLE_TYPE_U8;
197 case CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE:
198 dst_type = AUDIO_SAMPLE_TYPE_S16_LE;
201 dst_type = AUDIO_SAMPLE_TYPE_U8;
205 static void __convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type, CAudioInfo::EAudioType& dst_type) {
207 case SOUND_TYPE_SYSTEM:
208 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
210 case SOUND_TYPE_NOTIFICATION:
211 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION;
213 case SOUND_TYPE_ALARM:
214 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM;
216 case SOUND_TYPE_RINGTONE:
217 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP;
219 case SOUND_TYPE_MEDIA:
220 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
222 case SOUND_TYPE_CALL:
223 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
225 case SOUND_TYPE_VOIP:
226 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP;
228 case SOUND_TYPE_VOICE:
229 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION;
232 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
237 static void __convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type, sound_type_e& dst_type) {
239 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA:
240 dst_type = SOUND_TYPE_MEDIA;
242 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM:
243 dst_type = SOUND_TYPE_SYSTEM;
245 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM:
246 dst_type = SOUND_TYPE_ALARM;
248 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION:
249 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_EMERGENCY:
250 dst_type = SOUND_TYPE_NOTIFICATION;
252 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION:
253 dst_type = SOUND_TYPE_VOICE;
255 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP:
256 dst_type = SOUND_TYPE_RINGTONE;
258 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP:
259 dst_type = SOUND_TYPE_VOIP;
262 dst_type = SOUND_TYPE_MEDIA;
267 static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src_state) {
268 audio_io_state_e dst_state;
270 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE:
271 dst_state = AUDIO_IO_STATE_IDLE;
273 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE:
274 dst_state = AUDIO_IO_STATE_IDLE;
276 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING:
277 dst_state = AUDIO_IO_STATE_RUNNING;
279 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED:
280 dst_state = AUDIO_IO_STATE_PAUSED;
283 dst_state = AUDIO_IO_STATE_IDLE;
288 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) throw(CAudioError) {
289 if (sample_rate < 0) {
290 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate);
293 if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) {
294 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel);
297 if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE) {
298 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type);
302 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) throw(CAudioError) {
303 __check_audio_param(sample_rate, channel, type);
305 if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE) {
306 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type);
310 static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw(CAudioError) {
311 CAudioInfo::EChannel dstChannel;
312 CAudioInfo::ESampleType dstSampleType;
313 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
315 __convert_channel_2_audio_info_channel(channel, dstChannel);
316 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
318 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
321 static CAudioInfo __generate_audio_input_loopback_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) throw(CAudioError) {
322 CAudioInfo::EChannel dstChannel;
323 CAudioInfo::ESampleType dstSampleType;
324 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_LOOPBACK;
326 __convert_channel_2_audio_info_channel(channel, dstChannel);
327 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
329 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
332 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) {
333 CAudioInfo::EChannel dstChannel;
334 CAudioInfo::ESampleType dstSampleType;
335 CAudioInfo::EAudioType dstAudioType;
337 __convert_channel_2_audio_info_channel(channel, dstChannel);
338 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
339 __convert_sound_type_2_audio_info_audio_type(sound_type, dstAudioType);
341 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
344 static audio_io_interrupted_code_e __convert_interrupted_code(IAudioSessionEventListener::EInterruptCode code) {
346 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED:
347 return AUDIO_IO_INTERRUPTED_COMPLETED;
348 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_CALL:
349 return AUDIO_IO_INTERRUPTED_BY_CALL;
350 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EARJACK_UNPLUG:
351 return AUDIO_IO_INTERRUPTED_BY_EARJACK_UNPLUG;
352 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_RESOURCE_CONFLICT:
353 return AUDIO_IO_INTERRUPTED_BY_RESOURCE_CONFLICT;
354 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_ALARM:
355 return AUDIO_IO_INTERRUPTED_BY_ALARM;
356 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_EMERGENCY:
357 return AUDIO_IO_INTERRUPTED_BY_EMERGENCY;
358 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_NOTIFICATION:
359 return AUDIO_IO_INTERRUPTED_BY_NOTIFICATION;
360 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_BY_MEDIA:
361 case IAudioSessionEventListener::EInterruptCode::INTERRUPT_MAX:
363 return AUDIO_IO_INTERRUPTED_BY_MEDIA;
368 * Implements CAPI functions
370 int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_in_h *input) {
371 audio_io_s* handle = NULL;
372 bool mic_enable = false;
376 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
379 __check_audio_param(sample_rate, channel, type);
381 /* If MIC is not supported, return NOT_SUPPORTED error */
382 ret = system_info_get_platform_bool(FEATURE_MICROPHONE, &mic_enable);
383 AUDIO_IO_LOGD("system_info_platform [%s]=[%d], ret[%d]", FEATURE_MICROPHONE, mic_enable, ret);
384 if (ret != SYSTEM_INFO_ERROR_NONE || !mic_enable) {
385 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "System doesn't support microphone!");
388 handle = new audio_io_s;
389 if (handle == NULL) {
390 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
393 CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type);
395 handle->audioIoHandle = new CAudioInput(audioInfo);
396 if (handle->audioIoHandle == NULL) {
397 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
400 handle->audioIoHandle->initialize();
403 } catch (CAudioError e) {
404 AUDIO_IO_LOGE("%s", e.getErrorMsg());
406 VALID_POINTER_START(handle)
407 SAFE_FINALIZE(handle->audioIoHandle);
408 SAFE_DELETE(handle->audioIoHandle);
412 VALID_POINTER_START(input)
416 return __convert_CAudioError(e);
419 return AUDIO_IO_ERROR_NONE;
422 int cpp_audio_in_create_loopback(int sample_rate, audio_channel_e channel, audio_sample_type_e type , audio_in_h* input) {
423 audio_io_s* handle = NULL;
426 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
429 __check_audio_param(sample_rate, channel, type);
431 handle = new audio_io_s;
432 if (handle == NULL) {
433 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
436 CAudioInfo audioInfo = __generate_audio_input_loopback_info(sample_rate, channel, type);
438 handle->audioIoHandle = new CAudioInput(audioInfo);
439 if (handle->audioIoHandle == NULL) {
440 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
443 handle->audioIoHandle->initialize();
446 } catch (CAudioError e) {
447 AUDIO_IO_LOGE("%s", e.getErrorMsg());
449 VALID_POINTER_START(handle)
450 SAFE_FINALIZE(handle->audioIoHandle);
451 SAFE_DELETE(handle->audioIoHandle);
455 VALID_POINTER_START(input)
459 return __convert_CAudioError(e);
462 return AUDIO_IO_ERROR_NONE;
465 int cpp_audio_in_destroy(audio_in_h input) {
466 audio_io_s* handle = static_cast<audio_io_s*>(input);
469 if (handle == NULL) {
470 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
473 assert(handle->audioIoHandle);
475 /* Internal unprepare for backward compatibility */
476 handle->audioIoHandle->unprepare();
478 SAFE_FINALIZE(handle->audioIoHandle);
479 SAFE_DELETE(handle->audioIoHandle);
481 } catch (CAudioError e) {
482 AUDIO_IO_LOGE("%s", e.getErrorMsg());
483 return __convert_CAudioError(e);
486 return AUDIO_IO_ERROR_NONE;
489 int cpp_audio_in_set_sound_stream_info(audio_in_h input, sound_stream_info_h stream_info) {
490 audio_io_s* handle = static_cast<audio_io_s*>(input);
493 if (handle == NULL || stream_info == NULL) {
494 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, stream_info:%p", input, stream_info);
497 assert(handle->audioIoHandle);
499 int errorCode = SOUND_MANAGER_ERROR_NONE;
500 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
505 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
506 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
510 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
511 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
513 handle->audioIoHandle->getAudioInfo().convertInputStreamType2AudioType(type, &audioType);
514 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
516 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
517 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
519 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
521 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported");
523 } catch (CAudioError e) {
524 AUDIO_IO_LOGE("%s", e.getErrorMsg());
525 return __convert_CAudioError(e);
528 return AUDIO_IO_ERROR_NONE;
531 int cpp_audio_in_prepare(audio_in_h input) {
532 audio_io_s* handle = static_cast<audio_io_s*>(input);
535 if (handle == NULL) {
536 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
539 assert(handle->audioIoHandle);
541 handle->audioIoHandle->prepare();
542 } catch (CAudioError e) {
543 AUDIO_IO_LOGE("%s", e.getErrorMsg());
544 return __convert_CAudioError(e);
547 return AUDIO_IO_ERROR_NONE;
550 int cpp_audio_in_unprepare(audio_in_h input) {
551 audio_io_s* handle = static_cast<audio_io_s*>(input);
554 if (handle == NULL) {
555 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
558 assert(handle->audioIoHandle);
560 handle->audioIoHandle->unprepare();
561 } catch (CAudioError e) {
562 AUDIO_IO_LOGE("%s", e.getErrorMsg());
563 return __convert_CAudioError(e);
566 return AUDIO_IO_ERROR_NONE;
569 int cpp_audio_in_pause(audio_in_h input) {
570 audio_io_s* handle = static_cast<audio_io_s*>(input);
573 if (handle == NULL) {
574 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
577 assert(handle->audioIoHandle);
579 handle->audioIoHandle->pause();
580 } catch (CAudioError e) {
581 AUDIO_IO_LOGE("%s", e.getErrorMsg());
582 return __convert_CAudioError(e);
585 return AUDIO_IO_ERROR_NONE;
588 int cpp_audio_in_resume(audio_in_h input) {
589 audio_io_s* handle = static_cast<audio_io_s*>(input);
592 if (handle == NULL) {
593 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
596 assert(handle->audioIoHandle);
598 handle->audioIoHandle->resume();
599 } catch (CAudioError e) {
600 AUDIO_IO_LOGE("%s", e.getErrorMsg());
601 return __convert_CAudioError(e);
604 return AUDIO_IO_ERROR_NONE;
607 int cpp_audio_in_drain(audio_in_h input) {
608 audio_io_s* handle = static_cast<audio_io_s*>(input);
611 if (handle == NULL) {
612 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
615 assert(handle->audioIoHandle);
617 handle->audioIoHandle->drain();
618 } catch (CAudioError e) {
619 AUDIO_IO_LOGE("%s", e.getErrorMsg());
620 return __convert_CAudioError(e);
623 return AUDIO_IO_ERROR_NONE;
626 int cpp_audio_in_flush(audio_in_h input) {
627 audio_io_s* handle = static_cast<audio_io_s*>(input);
630 if (handle == NULL) {
631 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
634 assert(handle->audioIoHandle);
636 handle->audioIoHandle->flush();
637 } catch (CAudioError e) {
638 AUDIO_IO_LOGE("%s", e.getErrorMsg());
639 return __convert_CAudioError(e);
642 return AUDIO_IO_ERROR_NONE;
645 int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) {
646 audio_io_s* handle = static_cast<audio_io_s*>(input);
650 if (handle == NULL || buffer == NULL) {
651 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
654 assert(handle->audioIoHandle);
656 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
657 if (inputHandle == NULL) {
658 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
660 size_t readn = inputHandle->read(buffer, static_cast<size_t>(length));
661 ret = static_cast<int>(readn);
662 #ifdef _AUDIO_IO_DEBUG_TIMING_
663 AUDIO_IO_LOGD("readn:%d", readn);
665 } catch (CAudioError e) {
666 AUDIO_IO_LOGE("%s", e.getErrorMsg());
667 return __convert_CAudioError(e);
673 int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) {
674 audio_io_s* handle = static_cast<audio_io_s*>(input);
677 if (handle == NULL || size == NULL) {
678 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, size:%p", input, size);
681 assert(handle->audioIoHandle);
683 CAudioIO* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
684 if (inputHandle == NULL) {
685 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
687 *size = inputHandle->getBufferSize();
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_sample_rate(audio_in_h input, int *sample_rate) {
697 audio_io_s* handle = static_cast<audio_io_s*>(input);
700 if (handle == NULL || sample_rate == NULL) {
701 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
704 assert(handle->audioIoHandle);
705 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
706 } catch (CAudioError e) {
707 AUDIO_IO_LOGE("%s", e.getErrorMsg());
708 return __convert_CAudioError(e);
711 return AUDIO_IO_ERROR_NONE;
714 int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
715 audio_io_s* handle = static_cast<audio_io_s*>(input);
718 if (handle == NULL || channel == NULL) {
719 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, channel:%p", input, channel);
722 assert(handle->audioIoHandle);
724 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
725 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
726 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
728 *channel = dstChannel;
729 } catch (CAudioError e) {
730 AUDIO_IO_LOGE("%s", e.getErrorMsg());
731 return __convert_CAudioError(e);
734 return AUDIO_IO_ERROR_NONE;
737 int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
738 audio_io_s* handle = static_cast<audio_io_s*>(input);
741 if (handle == NULL || type == NULL) {
742 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, type:%p", input, type);
745 assert(handle->audioIoHandle);
747 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
748 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
749 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
751 *type = dstSampleType;
752 } catch (CAudioError e) {
753 AUDIO_IO_LOGE("%s", e.getErrorMsg());
754 return __convert_CAudioError(e);
757 return AUDIO_IO_ERROR_NONE;
760 static void __interrupt_cb_internal(IAudioSessionEventListener::EInterruptCode _code, void* user_data) {
761 audio_io_s* handle = static_cast<audio_io_s*>(user_data);
762 audio_io_interrupted_code_e code = __convert_interrupted_code(_code);
766 if (handle->interrupt_callback.onInterrupt != NULL) {
767 handle->interrupt_callback.onInterrupt(code, handle->interrupt_callback.user_data);
771 int cpp_audio_in_set_interrupted_cb(audio_in_h input, audio_io_interrupted_cb callback, void *user_data) {
772 audio_io_s* handle = static_cast<audio_io_s*>(input);
775 if (handle == NULL || callback == NULL) {
776 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
779 assert(handle->audioIoHandle);
781 handle->interrupt_callback.onInterrupt = callback;
782 handle->interrupt_callback.user_data = user_data;
784 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
785 cb.mUserData = static_cast<void*>(handle);
786 cb.onInterrupt = __interrupt_cb_internal;
788 handle->audioIoHandle->setInterruptCallback(cb);
789 } catch (CAudioError e) {
790 AUDIO_IO_LOGE("%s", e.getErrorMsg());
791 return __convert_CAudioError(e);
794 return AUDIO_IO_ERROR_NONE;
797 int cpp_audio_in_unset_interrupted_cb(audio_in_h input) {
798 audio_io_s* handle = static_cast<audio_io_s*>(input);
801 if (handle == NULL) {
802 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
805 assert(handle->audioIoHandle);
807 handle->interrupt_callback.onInterrupt = NULL;
808 handle->interrupt_callback.user_data = NULL;
810 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
812 cb.onInterrupt = NULL;
814 handle->audioIoHandle->setInterruptCallback(cb);
815 } catch (CAudioError e) {
816 AUDIO_IO_LOGE("%s", e.getErrorMsg());
817 return __convert_CAudioError(e);
820 return AUDIO_IO_ERROR_NONE;
823 int cpp_audio_in_ignore_session(audio_in_h input) {
824 audio_io_s* handle = static_cast<audio_io_s*>(input);
827 if (handle == NULL) {
828 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
831 if (handle->stream_callback.onStream) {
832 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
835 assert(handle->audioIoHandle);
837 handle->audioIoHandle->ignoreSession();
838 } catch (CAudioError e) {
839 AUDIO_IO_LOGE("%s", e.getErrorMsg());
840 return __convert_CAudioError(e);
843 return AUDIO_IO_ERROR_NONE;
846 static void __stream_cb_internal(size_t nbytes, void *user_data) {
847 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
850 if (audioIo->stream_callback.onStream != NULL) {
851 audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data);
855 static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState state_prev, bool by_policy, void *user_data) {
856 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
859 if (audioIo->state_changed_callback.onStateChanged != NULL) {
860 audioIo->state_changed_callback.onStateChanged(audioIo, __convert_state_type(state_prev), __convert_state_type(state), by_policy, audioIo->state_changed_callback.user_data);
864 int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) {
865 audio_io_s* handle = static_cast<audio_io_s*>(input);
868 if (handle == NULL || callback == NULL) {
869 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
872 assert(handle->audioIoHandle);
874 handle->stream_callback.onStream = callback;
875 handle->stream_callback.user_data = user_data;
877 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
878 cb.mUserData = static_cast<void*>(handle);
879 cb.onStream = __stream_cb_internal;
881 handle->audioIoHandle->setStreamCallback(cb);
882 } catch (CAudioError e) {
883 AUDIO_IO_LOGE("%s", e.getErrorMsg());
884 return __convert_CAudioError(e);
887 return AUDIO_IO_ERROR_NONE;
890 int cpp_audio_in_unset_stream_cb(audio_in_h input) {
891 audio_io_s* handle = static_cast<audio_io_s*>(input);
894 if (handle == NULL) {
895 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
898 assert(handle->audioIoHandle);
900 handle->stream_callback.onStream = NULL;
901 handle->stream_callback.user_data = NULL;
903 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
907 handle->audioIoHandle->setStreamCallback(cb);
908 } catch (CAudioError e) {
909 AUDIO_IO_LOGE("%s", e.getErrorMsg());
910 return __convert_CAudioError(e);
913 return AUDIO_IO_ERROR_NONE;
916 int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) {
917 audio_io_s* handle = static_cast<audio_io_s*>(input);
921 if (handle == NULL || buffer == NULL) {
922 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer);
925 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
928 inputHandle->peek(buffer, &_length);
929 } catch (CAudioError e) {
930 AUDIO_IO_LOGE("%s", e.getErrorMsg());
931 return __convert_CAudioError(e);
934 *length = (unsigned int)_length;
936 return AUDIO_IO_ERROR_NONE;
939 int cpp_audio_in_drop(audio_in_h input) {
940 audio_io_s* handle = static_cast<audio_io_s*>(input);
943 if (handle == NULL) {
944 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
947 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
951 } catch (CAudioError e) {
952 AUDIO_IO_LOGE("%s", e.getErrorMsg());
953 return __convert_CAudioError(e);
956 return AUDIO_IO_ERROR_NONE;
959 int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) {
960 audio_io_s* handle = static_cast<audio_io_s*>(input);
963 if (handle == NULL || callback == NULL) {
964 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
967 assert(handle->audioIoHandle);
969 handle->state_changed_callback.onStateChanged = callback;
970 handle->state_changed_callback.user_data = user_data;
972 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
973 cb.mUserData = static_cast<void*>(handle);
974 cb.onStateChanged = __state_changed_cb_internal;
976 handle->audioIoHandle->setStateChangedCallback(cb);
977 } catch (CAudioError e) {
978 AUDIO_IO_LOGE("%s", e.getErrorMsg());
979 return __convert_CAudioError(e);
982 return AUDIO_IO_ERROR_NONE;
985 int cpp_audio_in_unset_state_changed_cb(audio_in_h input) {
986 audio_io_s* handle = static_cast<audio_io_s*>(input);
989 if (handle == NULL) {
990 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", input);
993 assert(handle->audioIoHandle);
995 handle->state_changed_callback.onStateChanged = NULL;
996 handle->state_changed_callback.user_data = NULL;
998 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1000 cb.onStateChanged = NULL;
1002 handle->audioIoHandle->setStateChangedCallback(cb);
1003 } catch (CAudioError e) {
1004 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1005 return __convert_CAudioError(e);
1008 return AUDIO_IO_ERROR_NONE;
1015 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) {
1016 audio_io_s* handle = NULL;
1018 if (output == NULL) {
1019 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1022 __check_audio_param(sample_rate, channel, type, sound_type);
1024 handle = new audio_io_s;
1025 if (handle == NULL) {
1026 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1029 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, sound_type);
1031 handle->audioIoHandle = new CAudioOutput(audioInfo);
1032 if (handle->audioIoHandle == NULL) {
1033 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1036 handle->audioIoHandle->initialize();
1039 } catch (CAudioError e) {
1040 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1042 VALID_POINTER_START(handle)
1043 SAFE_FINALIZE(handle->audioIoHandle);
1044 SAFE_DELETE(handle->audioIoHandle);
1045 SAFE_DELETE(handle);
1048 VALID_POINTER_START(output)
1052 return __convert_CAudioError(e);
1055 return AUDIO_IO_ERROR_NONE;
1058 int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) {
1059 audio_io_s* handle = NULL;
1061 if (output == NULL) {
1062 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1065 __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */);
1067 handle = new audio_io_s;
1068 if (handle == NULL) {
1069 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1072 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA /* default sound_type */);
1074 handle->audioIoHandle = new CAudioOutput(audioInfo);
1075 if (handle->audioIoHandle == NULL) {
1076 THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1079 handle->audioIoHandle->initialize();
1082 } catch (CAudioError e) {
1083 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1085 VALID_POINTER_START(handle)
1086 SAFE_FINALIZE(handle->audioIoHandle);
1087 SAFE_DELETE(handle->audioIoHandle);
1088 SAFE_DELETE(handle);
1091 VALID_POINTER_START(output)
1095 return __convert_CAudioError(e);
1098 return AUDIO_IO_ERROR_NONE;
1101 int cpp_audio_out_destroy(audio_out_h output) {
1102 audio_io_s* handle = static_cast<audio_io_s*>(output);
1105 if (handle == NULL) {
1106 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1109 assert(handle->audioIoHandle);
1111 /* Internal unprepare for backward compatibility */
1112 handle->audioIoHandle->unprepare();
1114 SAFE_FINALIZE(handle->audioIoHandle);
1115 SAFE_DELETE(handle->audioIoHandle);
1116 SAFE_DELETE(handle);
1117 } catch (CAudioError e) {
1118 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1119 return __convert_CAudioError(e);
1122 return AUDIO_IO_ERROR_NONE;
1125 int cpp_audio_out_set_sound_stream_info(audio_out_h output, sound_stream_info_h stream_info) {
1126 audio_io_s* handle = static_cast<audio_io_s*>(output);
1129 if (handle == NULL || stream_info == NULL) {
1130 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, stream_info:%p", output, stream_info);
1133 assert(handle->audioIoHandle);
1135 int errorCode = SOUND_MANAGER_ERROR_NONE;
1136 CAudioInfo::EAudioType audioType = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
1141 if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) {
1142 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode);
1146 if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
1147 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
1149 handle->audioIoHandle->getAudioInfo().convertOutputStreamType2AudioType(type, &audioType);
1150 handle->audioIoHandle->getAudioInfo().setAudioType(audioType);
1152 if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
1153 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
1155 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
1157 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Output stream is not supported");
1159 } catch (CAudioError e) {
1160 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1161 return __convert_CAudioError(e);
1164 return AUDIO_IO_ERROR_NONE;
1167 int cpp_audio_out_prepare(audio_out_h output) {
1168 audio_io_s* handle = static_cast<audio_io_s*>(output);
1171 if (handle == NULL) {
1172 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1175 assert(handle->audioIoHandle);
1177 handle->audioIoHandle->prepare();
1178 } catch (CAudioError e) {
1179 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1180 return __convert_CAudioError(e);
1183 return AUDIO_IO_ERROR_NONE;
1186 int cpp_audio_out_unprepare(audio_out_h output) {
1187 audio_io_s* handle = static_cast<audio_io_s*>(output);
1190 if (handle == NULL) {
1191 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1194 assert(handle->audioIoHandle);
1196 handle->audioIoHandle->unprepare();
1197 } catch (CAudioError e) {
1198 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1199 return __convert_CAudioError(e);
1202 return AUDIO_IO_ERROR_NONE;
1205 int cpp_audio_out_pause(audio_out_h output) {
1206 audio_io_s* handle = static_cast<audio_io_s*>(output);
1209 if (handle == NULL) {
1210 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1213 assert(handle->audioIoHandle);
1215 handle->audioIoHandle->pause();
1216 } catch (CAudioError e) {
1217 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1218 return __convert_CAudioError(e);
1221 return AUDIO_IO_ERROR_NONE;
1224 int cpp_audio_out_resume(audio_out_h output) {
1225 audio_io_s* handle = static_cast<audio_io_s*>(output);
1228 if (handle == NULL) {
1229 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1232 assert(handle->audioIoHandle);
1234 handle->audioIoHandle->resume();
1235 } catch (CAudioError e) {
1236 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1237 return __convert_CAudioError(e);
1240 return AUDIO_IO_ERROR_NONE;
1243 int cpp_audio_out_drain(audio_out_h output) {
1244 audio_io_s* handle = static_cast<audio_io_s*>(output);
1247 if (handle == NULL) {
1248 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1251 assert(handle->audioIoHandle);
1253 handle->audioIoHandle->drain();
1254 } catch (CAudioError e) {
1255 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1256 return __convert_CAudioError(e);
1259 return AUDIO_IO_ERROR_NONE;
1262 int cpp_audio_out_flush(audio_out_h output) {
1263 audio_io_s* handle = static_cast<audio_io_s*>(output);
1266 if (handle == NULL) {
1267 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1270 assert(handle->audioIoHandle);
1272 handle->audioIoHandle->flush();
1273 } catch (CAudioError e) {
1274 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1275 return __convert_CAudioError(e);
1278 return AUDIO_IO_ERROR_NONE;
1281 int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) {
1282 audio_io_s* handle = static_cast<audio_io_s*>(output);
1286 if (handle == NULL || buffer == NULL) {
1287 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p, buffer:%p", output, buffer);
1290 assert(handle->audioIoHandle);
1292 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1293 if (outputHandle == NULL) {
1294 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1296 size_t written = outputHandle->write(buffer, static_cast<size_t>(length));
1297 ret = static_cast<int>(written);
1298 #ifdef _AUDIO_IO_DEBUG_TIMING_
1299 AUDIO_IO_LOGD("written:%d", written);
1301 } catch (CAudioError e) {
1302 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1303 return __convert_CAudioError(e);
1309 int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) {
1310 audio_io_s* handle = static_cast<audio_io_s*>(output);
1313 if (handle == NULL || size == NULL) {
1314 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, size:%p", output, size);
1317 assert(handle->audioIoHandle);
1319 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1320 if (outputHandle == NULL) {
1321 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1323 *size = outputHandle->getBufferSize();
1324 } catch (CAudioError e) {
1325 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1326 return __convert_CAudioError(e);
1329 return AUDIO_IO_ERROR_NONE;
1332 int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
1333 audio_io_s* handle = static_cast<audio_io_s*>(output);
1336 if (handle == NULL || sample_rate == NULL) {
1337 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
1340 assert(handle->audioIoHandle);
1341 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
1342 } catch (CAudioError e) {
1343 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1344 return __convert_CAudioError(e);
1347 return AUDIO_IO_ERROR_NONE;
1350 int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
1351 audio_io_s* handle = static_cast<audio_io_s*>(output);
1354 if (handle == NULL || channel == NULL) {
1355 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, channel:%p", output, channel);
1358 assert(handle->audioIoHandle);
1360 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
1361 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
1362 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
1364 *channel = dstChannel;
1365 } catch (CAudioError e) {
1366 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1367 return __convert_CAudioError(e);
1370 return AUDIO_IO_ERROR_NONE;
1373 int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) {
1374 audio_io_s* handle = static_cast<audio_io_s*>(output);
1377 if (handle == NULL || type == NULL) {
1378 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1381 assert(handle->audioIoHandle);
1383 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
1384 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
1385 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
1387 *type = dstSampleType;
1388 } catch (CAudioError e) {
1389 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1390 return __convert_CAudioError(e);
1393 return AUDIO_IO_ERROR_NONE;
1396 int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) {
1397 audio_io_s* handle = static_cast<audio_io_s*>(output);
1400 if (handle == NULL || type == NULL) {
1401 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1404 assert(handle->audioIoHandle);
1406 const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType();
1407 sound_type_e dstSoundType = SOUND_TYPE_MEDIA;
1408 __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType);
1410 *type = dstSoundType;
1411 } catch (CAudioError e) {
1412 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1413 return __convert_CAudioError(e);
1416 return AUDIO_IO_ERROR_NONE;
1419 int cpp_audio_out_set_interrupted_cb(audio_out_h output, audio_io_interrupted_cb callback, void *user_data) {
1420 audio_io_s* handle = static_cast<audio_io_s*>(output);
1423 if (handle == NULL || callback == NULL) {
1424 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1427 assert(handle->audioIoHandle);
1429 handle->interrupt_callback.onInterrupt = callback;
1430 handle->interrupt_callback.user_data = user_data;
1432 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1433 cb.mUserData = static_cast<void*>(handle);
1434 cb.onInterrupt = __interrupt_cb_internal;
1436 handle->audioIoHandle->setInterruptCallback(cb);
1437 } catch (CAudioError e) {
1438 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1439 return __convert_CAudioError(e);
1442 return AUDIO_IO_ERROR_NONE;
1445 int cpp_audio_out_unset_interrupted_cb(audio_out_h output) {
1446 audio_io_s* handle = static_cast<audio_io_s*>(output);
1449 if (handle == NULL) {
1450 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1453 assert(handle->audioIoHandle);
1455 handle->interrupt_callback.onInterrupt = NULL;
1456 handle->interrupt_callback.user_data = NULL;
1458 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1459 cb.mUserData = NULL;
1460 cb.onInterrupt = NULL;
1462 handle->audioIoHandle->setInterruptCallback(cb);
1463 } catch (CAudioError e) {
1464 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1465 return __convert_CAudioError(e);
1468 return AUDIO_IO_ERROR_NONE;
1471 int cpp_audio_out_ignore_session(audio_out_h output) {
1472 audio_io_s* handle = static_cast<audio_io_s*>(output);
1475 if (handle == NULL) {
1476 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1479 if (handle->stream_callback.onStream) {
1480 THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
1483 assert(handle->audioIoHandle);
1485 handle->audioIoHandle->ignoreSession();
1486 } catch (CAudioError e) {
1487 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1488 return __convert_CAudioError(e);
1491 return AUDIO_IO_ERROR_NONE;
1494 int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) {
1495 audio_io_s* handle = static_cast<audio_io_s*>(output);
1498 if (handle == NULL || callback == NULL) {
1499 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1502 assert(handle->audioIoHandle);
1504 handle->stream_callback.onStream = callback;
1505 handle->stream_callback.user_data = user_data;
1507 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1508 cb.mUserData = static_cast<void*>(handle);
1509 cb.onStream = __stream_cb_internal;
1511 handle->audioIoHandle->setStreamCallback(cb);
1512 } catch (CAudioError e) {
1513 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1514 return __convert_CAudioError(e);
1517 return AUDIO_IO_ERROR_NONE;
1520 int cpp_audio_out_unset_stream_cb(audio_out_h output) {
1521 audio_io_s* handle = static_cast<audio_io_s*>(output);
1524 if (handle == NULL) {
1525 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1528 assert(handle->audioIoHandle);
1530 handle->stream_callback.onStream = NULL;
1531 handle->stream_callback.user_data = NULL;
1533 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1534 cb.mUserData = NULL;
1537 handle->audioIoHandle->setStreamCallback(cb);
1538 } catch (CAudioError e) {
1539 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1540 return __convert_CAudioError(e);
1543 return AUDIO_IO_ERROR_NONE;
1546 int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) {
1547 audio_io_s* handle = static_cast<audio_io_s*>(output);
1550 if (handle == NULL || callback == NULL) {
1551 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1554 assert(handle->audioIoHandle);
1556 handle->state_changed_callback.onStateChanged = callback;
1557 handle->state_changed_callback.user_data = user_data;
1559 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1560 cb.mUserData = static_cast<void*>(handle);
1561 cb.onStateChanged = __state_changed_cb_internal;
1563 handle->audioIoHandle->setStateChangedCallback(cb);
1564 } catch (CAudioError e) {
1565 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1566 return __convert_CAudioError(e);
1569 return AUDIO_IO_ERROR_NONE;
1572 int cpp_audio_out_unset_state_changed_cb(audio_out_h output) {
1573 audio_io_s* handle = static_cast<audio_io_s*>(output);
1576 if (handle == NULL) {
1577 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1580 assert(handle->audioIoHandle);
1582 handle->state_changed_callback.onStateChanged = NULL;
1583 handle->state_changed_callback.user_data = NULL;
1585 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1586 cb.mUserData = NULL;
1587 cb.onStateChanged = NULL;
1589 handle->audioIoHandle->setStateChangedCallback(cb);
1590 } catch (CAudioError e) {
1591 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1592 return __convert_CAudioError(e);
1595 return AUDIO_IO_ERROR_NONE;