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.
19 #include "cpp_audio_io.h"
20 #include "CAudioIODef.h"
21 #include <sound_manager_internal.h>
25 using namespace tizen_media_audio;
31 * Name : audio_io_interrupted_cb_s
32 * Declaration : Keeps user callback pointer and user data for delivering an interrupt event
34 typedef struct audio_io_interrupted_cb_s {
36 audio_io_interrupted_cb onInterrupt;
38 audio_io_interrupted_cb_s() : user_data(NULL), onInterrupt(NULL)
40 } audio_io_interrupted_cb_s;
45 * Name : audio_io_stream_cb_s
46 * Declaration : Keeps user callback pointer and user data for delivering an stream event
48 typedef struct audio_io_stream_cb_s {
50 audio_in_stream_cb onStream;
52 audio_io_stream_cb_s() : user_data(NULL), onStream(NULL)
54 } audio_io_stream_cb_s;
59 * Name : audio_io_state_changed_cb_s
60 * Declaration : Keeps user callback pointer and user data for delivering an state changed event
62 typedef struct audio_io_state_changed_cb_s {
64 audio_in_state_changed_cb onStateChanged;
66 audio_io_state_changed_cb_s() : user_data(NULL), onStateChanged(NULL)
68 } audio_io_state_changed_cb_s;
74 * Declaration : An handle of AudioIO
75 * The handle has two struct for user callback
76 * And the handle has a pointer of private audioIO object
77 * The CAudioIO is a abstract class object about Input and Output
79 typedef struct audio_io_s {
80 CAudioIO* audioIoHandle;
81 audio_io_interrupted_cb_s interrupt_callback;
82 audio_io_stream_cb_s stream_callback;
83 audio_io_state_changed_cb_s state_changed_callback;
85 audio_io_s() : audioIoHandle(NULL)
93 static audio_io_error_e _convert_CAudioError(CAudioError& error) {
94 audio_io_error_e ret = AUDIO_IO_ERROR_NONE;
95 CAudioError::EError err = error.getError();
99 case CAudioError::ERROR_NONE:
100 ret = AUDIO_IO_ERROR_NONE;
103 case CAudioError::ERROR_INVALID_ARGUMENT:
104 case CAudioError::ERROR_INVALID_HANDLE:
105 case CAudioError::ERROR_INVALID_SAMPLERATE:
106 case CAudioError::ERROR_INVALID_CHANNEL:
107 case CAudioError::ERROR_INVALID_FORMAT:
108 ret = AUDIO_IO_ERROR_INVALID_PARAMETER;
111 case CAudioError::ERROR_DEVICE_NOT_OPENED:
112 ret = AUDIO_IO_ERROR_DEVICE_NOT_OPENED;
115 case CAudioError::ERROR_DEVICE_NOT_CLOSED:
116 ret = AUDIO_IO_ERROR_DEVICE_NOT_CLOSED;
119 case CAudioError::ERROR_PERMISSION_DENIED:
120 ret = AUDIO_IO_ERROR_PERMISSION_DENIED;
123 case CAudioError::ERROR_NOT_SUPPORTED:
124 ret = AUDIO_IO_ERROR_NOT_SUPPORTED;
127 case CAudioError::ERROR_MAX:
128 case CAudioError::ERROR_INTERNAL_OPERATION:
129 case CAudioError::ERROR_NOT_INITIALIZED:
130 case CAudioError::ERROR_FAILED_OPERATION:
131 case CAudioError::ERROR_INVALID_OPERATION:
132 ret = AUDIO_IO_ERROR_INVALID_OPERATION;
135 case CAudioError::ERROR_OUT_OF_MEMORY:
136 case CAudioError::ERROR_INVALID_POINTER:
137 ret = AUDIO_IO_ERROR_INVALID_BUFFER;
140 case CAudioError::ERROR_POLICY_BLOCKED:
141 case CAudioError::ERROR_POLICY_INTERRUPTED:
142 case CAudioError::ERROR_POLICY_DUPLICATED:
143 ret = AUDIO_IO_ERROR_SOUND_POLICY;
150 static void _convert_channel_2_audio_info_channel(const audio_channel_e& src_channel, CAudioInfo::EChannel& dst_channel) {
151 switch (src_channel) {
152 case AUDIO_CHANNEL_MONO:
153 dst_channel = CAudioInfo::CHANNEL_MONO;
155 case AUDIO_CHANNEL_STEREO:
156 dst_channel = CAudioInfo::CHANNEL_STEREO;
159 dst_channel = CAudioInfo::CHANNEL_MONO;
163 static void _convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel, audio_channel_e& dst_channel) {
164 switch (src_channel) {
165 case CAudioInfo::CHANNEL_MONO:
166 dst_channel = AUDIO_CHANNEL_MONO;
168 case CAudioInfo::CHANNEL_STEREO:
169 dst_channel = AUDIO_CHANNEL_STEREO;
172 dst_channel = AUDIO_CHANNEL_MONO;
176 static void _convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type, CAudioInfo::ESampleType& dst_type) {
178 case AUDIO_SAMPLE_TYPE_U8:
179 dst_type = CAudioInfo::SAMPLE_TYPE_U8;
181 case AUDIO_SAMPLE_TYPE_S16_LE:
182 dst_type = CAudioInfo::SAMPLE_TYPE_S16_LE;
185 dst_type = CAudioInfo::SAMPLE_TYPE_U8;
189 static void _convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type, audio_sample_type_e& dst_type) {
191 case CAudioInfo::SAMPLE_TYPE_U8:
192 dst_type = AUDIO_SAMPLE_TYPE_U8;
194 case CAudioInfo::SAMPLE_TYPE_S16_LE:
195 dst_type = AUDIO_SAMPLE_TYPE_S16_LE;
198 dst_type = AUDIO_SAMPLE_TYPE_U8;
202 static void _convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type, CAudioInfo::EAudioType& dst_type) {
204 case SOUND_TYPE_SYSTEM:
205 dst_type = CAudioInfo::AUDIO_OUT_TYPE_SYSTEM;
207 case SOUND_TYPE_NOTIFICATION:
208 dst_type = CAudioInfo::AUDIO_OUT_TYPE_NOTIFICATION;
210 case SOUND_TYPE_ALARM:
211 dst_type = CAudioInfo::AUDIO_OUT_TYPE_ALARM;
213 case SOUND_TYPE_RINGTONE:
214 dst_type = CAudioInfo::AUDIO_OUT_TYPE_RINGTONE_VOIP;
216 case SOUND_TYPE_MEDIA:
217 dst_type = CAudioInfo::AUDIO_OUT_TYPE_MEDIA;
219 case SOUND_TYPE_CALL:
220 dst_type = CAudioInfo::AUDIO_OUT_TYPE_SYSTEM;
222 case SOUND_TYPE_VOIP:
223 dst_type = CAudioInfo::AUDIO_OUT_TYPE_VOIP;
225 case SOUND_TYPE_VOICE:
226 dst_type = CAudioInfo::AUDIO_OUT_TYPE_VOICE_INFORMATION;
229 dst_type = CAudioInfo::AUDIO_OUT_TYPE_MEDIA;
234 static void _convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type, sound_type_e& dst_type) {
236 case CAudioInfo::AUDIO_OUT_TYPE_MEDIA:
237 dst_type = SOUND_TYPE_MEDIA;
239 case CAudioInfo::AUDIO_OUT_TYPE_SYSTEM:
240 dst_type = SOUND_TYPE_SYSTEM;
242 case CAudioInfo::AUDIO_OUT_TYPE_ALARM:
243 dst_type = SOUND_TYPE_ALARM;
245 case CAudioInfo::AUDIO_OUT_TYPE_NOTIFICATION:
246 case CAudioInfo::AUDIO_OUT_TYPE_EMERGENCY:
247 dst_type = SOUND_TYPE_NOTIFICATION;
249 case CAudioInfo::AUDIO_OUT_TYPE_VOICE_INFORMATION:
250 case CAudioInfo::AUDIO_OUT_TYPE_VOICE_RECOGNITION:
251 dst_type = SOUND_TYPE_VOICE;
253 case CAudioInfo::AUDIO_OUT_TYPE_RINGTONE_VOIP:
254 dst_type = SOUND_TYPE_RINGTONE;
256 case CAudioInfo::AUDIO_OUT_TYPE_VOIP:
257 dst_type = SOUND_TYPE_VOIP;
260 dst_type = SOUND_TYPE_MEDIA;
265 static audio_io_state_e _convert_state_type(const CAudioInfo::EAudioIOState src_state) {
266 audio_io_state_e dst_state;
268 case CAudioInfo::AUDIO_IO_STATE_NONE:
269 dst_state = AUDIO_IO_STATE_IDLE;
271 case CAudioInfo::AUDIO_IO_STATE_IDLE:
272 dst_state = AUDIO_IO_STATE_IDLE;
274 case CAudioInfo::AUDIO_IO_STATE_RUNNING:
275 dst_state = AUDIO_IO_STATE_RUNNING;
277 case CAudioInfo::AUDIO_IO_STATE_PAUSED:
278 dst_state = AUDIO_IO_STATE_PAUSED;
281 dst_state = AUDIO_IO_STATE_IDLE;
286 static void _check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) throw (CAudioError) {
288 if (sample_rate < 0) {
289 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate);
292 if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) {
293 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel);
296 if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE) {
297 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type);
301 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::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::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::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::INTERRUPT_COMPLETED:
347 return AUDIO_IO_INTERRUPTED_COMPLETED;
348 case IAudioSessionEventListener::INTERRUPT_BY_CALL:
349 return AUDIO_IO_INTERRUPTED_BY_CALL;
350 case IAudioSessionEventListener::INTERRUPT_BY_EARJACK_UNPLUG:
351 return AUDIO_IO_INTERRUPTED_BY_EARJACK_UNPLUG;
352 case IAudioSessionEventListener::INTERRUPT_BY_RESOURCE_CONFLICT:
353 return AUDIO_IO_INTERRUPTED_BY_RESOURCE_CONFLICT;
354 case IAudioSessionEventListener::INTERRUPT_BY_ALARM:
355 return AUDIO_IO_INTERRUPTED_BY_ALARM;
356 case IAudioSessionEventListener::INTERRUPT_BY_EMERGENCY:
357 return AUDIO_IO_INTERRUPTED_BY_EMERGENCY;
358 case IAudioSessionEventListener::INTERRUPT_BY_NOTIFICATION:
359 return AUDIO_IO_INTERRUPTED_BY_NOTIFICATION;
360 case IAudioSessionEventListener::INTERRUPT_BY_MEDIA:
361 case IAudioSessionEventListener::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;
374 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
377 _check_audio_param(sample_rate, channel, type);
379 handle = new audio_io_s;
380 if (handle == NULL) {
381 THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
384 CAudioInfo audioInfo = _generate_audio_input_info(sample_rate, channel, type);
386 handle->audioIoHandle = new CAudioInput(audioInfo);
387 if (handle == NULL) {
388 THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
391 handle->audioIoHandle->initialize();
394 } catch (CAudioError e) {
395 AUDIO_IO_LOGE("%s", e.getErrorMsg());
397 VALID_POINTER_START(handle)
398 SAFE_FINALIZE(handle->audioIoHandle);
399 SAFE_DELETE(handle->audioIoHandle);
403 VALID_POINTER_START(input)
407 return _convert_CAudioError(e);
410 return AUDIO_IO_ERROR_NONE;
414 int cpp_audio_in_create_loopback(int sample_rate, audio_channel_e channel, audio_sample_type_e type , audio_in_h* input) {
415 audio_io_s* handle = NULL;
418 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
421 handle = new audio_io_s;
422 if (handle == NULL) {
423 THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
426 CAudioInfo audioInfo = _generate_audio_input_loopback_info(sample_rate, channel, type);
428 handle->audioIoHandle = new CAudioInput(audioInfo);
429 if (handle == NULL) {
430 THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
433 handle->audioIoHandle->initialize();
436 } catch (CAudioError e) {
437 AUDIO_IO_LOGE("%s", e.getErrorMsg());
439 VALID_POINTER_START(handle)
440 SAFE_FINALIZE(handle->audioIoHandle);
441 SAFE_DELETE(handle->audioIoHandle);
445 VALID_POINTER_START(input)
449 return _convert_CAudioError(e);
452 return AUDIO_IO_ERROR_NONE;
455 int cpp_audio_in_destroy(audio_in_h input) {
456 audio_io_s* handle = static_cast<audio_io_s*>(input);
459 if (handle == NULL) {
460 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
463 assert(handle->audioIoHandle);
465 SAFE_FINALIZE(handle->audioIoHandle);
466 SAFE_DELETE(handle->audioIoHandle);
468 } catch (CAudioError e) {
469 AUDIO_IO_LOGE("%s", e.getErrorMsg());
470 return _convert_CAudioError(e);
473 return AUDIO_IO_ERROR_NONE;
476 int cpp_audio_in_set_stream_info(audio_in_h input, sound_stream_info_h stream_info) {
477 audio_io_s* handle = static_cast<audio_io_s*>(input);
480 if (handle == NULL || stream_info == NULL) {
481 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, stream_info:%p", input, stream_info);
484 assert(handle->audioIoHandle);
486 int errorCode = SOUND_MANAGER_ERROR_NONE;
487 CAudioInfo::EAudioType AudioType = CAudioInfo::AUDIO_IN_TYPE_MEDIA;
491 if ((errorCode = sound_manager_get_type_from_stream_information (stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
492 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
494 handle->audioIoHandle->getAudioInfo().convertStreamType2AudioType(type, &AudioType);
495 handle->audioIoHandle->getAudioInfo().setAudioType(AudioType);
497 if ((errorCode = sound_manager_get_index_from_stream_information (stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
498 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
500 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
501 } catch (CAudioError e) {
502 AUDIO_IO_LOGE("%s", e.getErrorMsg());
503 return _convert_CAudioError(e);
506 return AUDIO_IO_ERROR_NONE;
509 int cpp_audio_in_prepare(audio_in_h input) {
510 audio_io_s* handle = static_cast<audio_io_s*>(input);
513 if (handle == NULL) {
514 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
517 assert(handle->audioIoHandle);
519 handle->audioIoHandle->prepare();
520 } catch (CAudioError e) {
521 AUDIO_IO_LOGE("%s", e.getErrorMsg());
522 return _convert_CAudioError(e);
525 return AUDIO_IO_ERROR_NONE;
528 int cpp_audio_in_unprepare(audio_in_h input) {
529 audio_io_s* handle = static_cast<audio_io_s*>(input);
532 if (handle == NULL) {
533 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
536 assert(handle->audioIoHandle);
538 handle->audioIoHandle->unprepare();
539 } catch (CAudioError e) {
540 AUDIO_IO_LOGE("%s", e.getErrorMsg());
541 return _convert_CAudioError(e);
544 return AUDIO_IO_ERROR_NONE;
547 int cpp_audio_in_pause(audio_in_h input) {
548 audio_io_s* handle = static_cast<audio_io_s*>(input);
551 if (handle == NULL) {
552 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
555 assert(handle->audioIoHandle);
557 handle->audioIoHandle->pause();
558 } catch (CAudioError e) {
559 AUDIO_IO_LOGE("%s", e.getErrorMsg());
560 return _convert_CAudioError(e);
563 return AUDIO_IO_ERROR_NONE;
566 int cpp_audio_in_resume(audio_in_h input) {
567 audio_io_s* handle = static_cast<audio_io_s*>(input);
570 if (handle == NULL) {
571 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
574 assert(handle->audioIoHandle);
576 handle->audioIoHandle->resume();
577 } catch (CAudioError e) {
578 AUDIO_IO_LOGE("%s", e.getErrorMsg());
579 return _convert_CAudioError(e);
582 return AUDIO_IO_ERROR_NONE;
585 int cpp_audio_in_drain(audio_in_h input) {
586 audio_io_s* handle = static_cast<audio_io_s*>(input);
589 if (handle == NULL) {
590 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
593 assert(handle->audioIoHandle);
595 handle->audioIoHandle->drain();
596 } catch (CAudioError e) {
597 AUDIO_IO_LOGE("%s", e.getErrorMsg());
598 return _convert_CAudioError(e);
601 return AUDIO_IO_ERROR_NONE;
604 int cpp_audio_in_flush(audio_in_h input) {
605 audio_io_s* handle = static_cast<audio_io_s*>(input);
608 if (handle == NULL) {
609 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
612 assert(handle->audioIoHandle);
614 handle->audioIoHandle->flush();
615 } catch (CAudioError e) {
616 AUDIO_IO_LOGE("%s", e.getErrorMsg());
617 return _convert_CAudioError(e);
620 return AUDIO_IO_ERROR_NONE;
623 int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) {
624 audio_io_s* handle = static_cast<audio_io_s*>(input);
628 if (handle == NULL) {
629 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
632 assert(handle->audioIoHandle);
634 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
635 ret = inputHandle->read(buffer, length);
636 #ifdef _AUDIO_IO_DEBUG_TIMING_
637 AUDIO_IO_LOGD("ret:%d", ret);
639 } catch (CAudioError e) {
640 AUDIO_IO_LOGE("%s", e.getErrorMsg());
641 return _convert_CAudioError(e);
647 int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) {
648 audio_io_s* handle = static_cast<audio_io_s*>(input);
651 if (handle == NULL || size == NULL) {
652 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, size:%p", input, size);
655 assert(handle->audioIoHandle);
657 CAudioIO* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
658 *size = inputHandle->getBufferSize();
659 } catch (CAudioError e) {
660 AUDIO_IO_LOGE("%s", e.getErrorMsg());
661 return _convert_CAudioError(e);
664 return AUDIO_IO_ERROR_NONE;
667 int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) {
668 audio_io_s* handle = static_cast<audio_io_s*>(input);
671 if (handle == NULL || sample_rate == NULL) {
672 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
675 assert(handle->audioIoHandle);
676 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
677 } catch (CAudioError e) {
678 AUDIO_IO_LOGE("%s", e.getErrorMsg());
679 return _convert_CAudioError(e);
682 return AUDIO_IO_ERROR_NONE;
685 int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
686 audio_io_s* handle = static_cast<audio_io_s*>(input);
689 if (handle == NULL || channel == NULL) {
690 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, channel:%p", input, channel);
693 assert(handle->audioIoHandle);
695 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
696 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
697 _convert_audio_info_channel_2_channel(srcChannel, dstChannel);
699 *channel = dstChannel;
700 } catch (CAudioError e) {
701 AUDIO_IO_LOGE("%s", e.getErrorMsg());
702 return _convert_CAudioError(e);
705 return AUDIO_IO_ERROR_NONE;
708 int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
709 audio_io_s* handle = static_cast<audio_io_s*>(input);
712 if (handle == NULL || type == NULL) {
713 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, type:%p", input, type);
716 assert(handle->audioIoHandle);
718 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
719 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
720 _convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
722 *type = dstSampleType;
723 } catch (CAudioError e) {
724 AUDIO_IO_LOGE("%s", e.getErrorMsg());
725 return _convert_CAudioError(e);
728 return AUDIO_IO_ERROR_NONE;
731 static void _interrupt_cb_internal(IAudioSessionEventListener::EInterruptCode _code, void* user_data) {
732 audio_io_s* handle = static_cast<audio_io_s*>(user_data);
733 audio_io_interrupted_code_e code = _convert_interrupted_code(_code);
737 if (handle->interrupt_callback.onInterrupt != NULL) {
738 handle->interrupt_callback.onInterrupt(code, handle->interrupt_callback.user_data);
742 int cpp_audio_in_set_interrupted_cb(audio_in_h input, audio_io_interrupted_cb callback, void *user_data) {
743 audio_io_s* handle = static_cast<audio_io_s*>(input);
746 if (handle == NULL || callback == NULL) {
747 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
750 assert(handle->audioIoHandle);
752 handle->interrupt_callback.onInterrupt = callback;
753 handle->interrupt_callback.user_data = user_data;
755 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
756 cb.mUserData = static_cast<void*>(handle);
757 cb.onInterrupt = _interrupt_cb_internal;
759 handle->audioIoHandle->setInterruptCallback(cb);
760 } catch (CAudioError e) {
761 AUDIO_IO_LOGE("%s", e.getErrorMsg());
762 return _convert_CAudioError(e);
765 return AUDIO_IO_ERROR_NONE;
768 int cpp_audio_in_unset_interrupted_cb(audio_in_h input) {
769 audio_io_s* handle = static_cast<audio_io_s*>(input);
772 if (handle == NULL) {
773 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
776 assert(handle->audioIoHandle);
778 handle->interrupt_callback.onInterrupt = NULL;
779 handle->interrupt_callback.user_data = NULL;
781 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
783 cb.onInterrupt = NULL;
785 handle->audioIoHandle->setInterruptCallback(cb);
786 } catch (CAudioError e) {
787 AUDIO_IO_LOGE("%s", e.getErrorMsg());
788 return _convert_CAudioError(e);
791 return AUDIO_IO_ERROR_NONE;
794 int cpp_audio_in_ignore_session(audio_in_h input) {
795 audio_io_s* handle = static_cast<audio_io_s*>(input);
798 if (handle == NULL) {
799 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
802 if (handle->stream_callback.onStream) {
803 THROW_ERROR_MSG(CAudioError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
806 assert(handle->audioIoHandle);
808 handle->audioIoHandle->ignoreSession();
809 } catch (CAudioError e) {
810 AUDIO_IO_LOGE("%s", e.getErrorMsg());
811 return _convert_CAudioError(e);
814 return AUDIO_IO_ERROR_NONE;
817 static void _stream_cb_internal(size_t nbytes, void *user_data) {
818 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
821 if (audioIo->stream_callback.onStream != NULL) {
822 audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data);
826 static void _state_changed_cb_internal(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState state_prev, bool by_policy, void *user_data) {
827 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
830 if (audioIo->state_changed_callback.onStateChanged != NULL) {
831 audioIo->state_changed_callback.onStateChanged(audioIo, _convert_state_type(state_prev), _convert_state_type(state), by_policy, audioIo->state_changed_callback.user_data);
835 int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) {
836 audio_io_s* handle = static_cast<audio_io_s*>(input);
839 if (handle == NULL || callback == NULL) {
840 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback);
843 assert(handle->audioIoHandle);
845 handle->stream_callback.onStream = callback;
846 handle->stream_callback.user_data = user_data;
848 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
849 cb.mUserData = static_cast<void*>(handle);
850 cb.onStream = _stream_cb_internal;
852 handle->audioIoHandle->setStreamCallback(cb);
853 } catch (CAudioError e) {
854 AUDIO_IO_LOGE("%s", e.getErrorMsg());
855 return _convert_CAudioError(e);
858 return AUDIO_IO_ERROR_NONE;
861 int cpp_audio_in_unset_stream_cb(audio_in_h input) {
862 audio_io_s* handle = static_cast<audio_io_s*>(input);
865 if (handle == NULL) {
866 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
869 assert(handle->audioIoHandle);
871 handle->stream_callback.onStream = NULL;
872 handle->stream_callback.user_data = NULL;
874 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
878 handle->audioIoHandle->setStreamCallback(cb);
879 } catch (CAudioError e) {
880 AUDIO_IO_LOGE("%s", e.getErrorMsg());
881 return _convert_CAudioError(e);
884 return AUDIO_IO_ERROR_NONE;
887 int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) {
888 audio_io_s* handle = static_cast<audio_io_s*>(input);
891 if (handle == NULL) {
892 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
895 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
898 inputHandle->peek(buffer, length);
899 } catch (CAudioError e) {
900 AUDIO_IO_LOGE("%s", e.getErrorMsg());
901 return _convert_CAudioError(e);
904 return AUDIO_IO_ERROR_NONE;
907 int cpp_audio_in_drop(audio_in_h input) {
908 audio_io_s* handle = static_cast<audio_io_s*>(input);
911 if (handle == NULL) {
912 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input);
915 CAudioInput* inputHandle = dynamic_cast<CAudioInput*>(handle->audioIoHandle);
919 } catch (CAudioError e) {
920 AUDIO_IO_LOGE("%s", e.getErrorMsg());
921 return _convert_CAudioError(e);
924 return AUDIO_IO_ERROR_NONE;
927 int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) {
928 audio_io_s* handle = static_cast<audio_io_s*>(input);
931 if (handle == NULL) {
932 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", input, callback);
935 assert(handle->audioIoHandle);
937 handle->state_changed_callback.onStateChanged = callback;
938 handle->state_changed_callback.user_data = user_data;
940 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
941 cb.mUserData = static_cast<void*>(handle);
942 cb.onStateChanged = _state_changed_cb_internal;
944 handle->audioIoHandle->setStateChangedCallback(cb);
945 } catch (CAudioError e) {
946 AUDIO_IO_LOGE("%s", e.getErrorMsg());
947 return _convert_CAudioError(e);
950 return AUDIO_IO_ERROR_NONE;
953 int cpp_audio_in_unset_state_changed_cb(audio_in_h input) {
954 audio_io_s* handle = static_cast<audio_io_s*>(input);
957 if (handle == NULL) {
958 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", input);
961 assert(handle->audioIoHandle);
963 handle->state_changed_callback.onStateChanged = NULL;
964 handle->state_changed_callback.user_data = NULL;
966 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
968 cb.onStateChanged = NULL;
970 handle->audioIoHandle->setStateChangedCallback(cb);
971 } catch (CAudioError e) {
972 AUDIO_IO_LOGE("%s", e.getErrorMsg());
973 return _convert_CAudioError(e);
976 return AUDIO_IO_ERROR_NONE;
983 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) {
984 audio_io_s* handle = NULL;
986 handle = new audio_io_s;
987 if (handle == NULL) {
988 THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
991 _check_audio_param(sample_rate, channel, type, sound_type);
993 CAudioInfo audioInfo = _generate_audio_output_info(sample_rate, channel, type, sound_type);
995 handle->audioIoHandle = new CAudioOutput(audioInfo);
996 if (handle == NULL) {
997 THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1000 handle->audioIoHandle->initialize();
1003 } catch (CAudioError e) {
1004 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1006 VALID_POINTER_START(handle)
1007 SAFE_FINALIZE(handle->audioIoHandle);
1008 SAFE_DELETE(handle->audioIoHandle);
1009 SAFE_DELETE(handle);
1012 VALID_POINTER_START(output)
1016 return _convert_CAudioError(e);
1019 return AUDIO_IO_ERROR_NONE;
1022 int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) {
1023 audio_io_s* handle = NULL;
1025 handle = new audio_io_s;
1026 if (handle == NULL) {
1027 THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed allocation handle");
1030 CAudioInfo audioInfo = _generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA /* default sound_tyoe */);
1032 handle->audioIoHandle = new CAudioOutput(audioInfo);
1033 if (handle == NULL) {
1034 THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed allocation internal handle");
1037 handle->audioIoHandle->initialize();
1040 } catch (CAudioError e) {
1041 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1043 VALID_POINTER_START(handle)
1044 SAFE_FINALIZE(handle->audioIoHandle);
1045 SAFE_DELETE(handle->audioIoHandle);
1046 SAFE_DELETE(handle);
1049 VALID_POINTER_START(output)
1053 return _convert_CAudioError(e);
1056 return AUDIO_IO_ERROR_NONE;
1059 int cpp_audio_out_destroy(audio_out_h output) {
1060 audio_io_s* handle = static_cast<audio_io_s*>(output);
1063 if (handle == NULL) {
1064 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1067 assert(handle->audioIoHandle);
1069 SAFE_FINALIZE(handle->audioIoHandle);
1070 SAFE_DELETE(handle->audioIoHandle);
1071 SAFE_DELETE(handle);
1072 } catch (CAudioError e) {
1073 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1074 return _convert_CAudioError(e);
1077 return AUDIO_IO_ERROR_NONE;
1080 int cpp_audio_out_set_stream_info(audio_out_h output, sound_stream_info_h stream_info) {
1081 audio_io_s* handle = static_cast<audio_io_s*>(output);
1084 if (handle == NULL || stream_info == NULL) {
1085 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, stream_info:%p", output, stream_info);
1088 assert(handle->audioIoHandle);
1090 int errorCode = SOUND_MANAGER_ERROR_NONE;
1091 CAudioInfo::EAudioType AudioType = CAudioInfo::AUDIO_OUT_TYPE_MEDIA;
1095 if ((errorCode = sound_manager_get_type_from_stream_information (stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) {
1096 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode);
1098 handle->audioIoHandle->getAudioInfo().convertStreamType2AudioType(type, &AudioType);
1099 handle->audioIoHandle->getAudioInfo().setAudioType(AudioType);
1101 if ((errorCode = sound_manager_get_index_from_stream_information (stream_info, &index)) != SOUND_MANAGER_ERROR_NONE) {
1102 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode);
1104 handle->audioIoHandle->getAudioInfo().setAudioIndex(index);
1105 } catch (CAudioError e) {
1106 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1107 return _convert_CAudioError(e);
1110 return AUDIO_IO_ERROR_NONE;
1113 int cpp_audio_out_prepare(audio_out_h output) {
1114 audio_io_s* handle = static_cast<audio_io_s*>(output);
1117 if (handle == NULL) {
1118 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1121 assert(handle->audioIoHandle);
1123 handle->audioIoHandle->prepare();
1124 } catch (CAudioError e) {
1125 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1126 return _convert_CAudioError(e);
1129 return AUDIO_IO_ERROR_NONE;
1132 int cpp_audio_out_unprepare(audio_out_h output) {
1133 audio_io_s* handle = static_cast<audio_io_s*>(output);
1136 if (handle == NULL) {
1137 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1140 assert(handle->audioIoHandle);
1142 handle->audioIoHandle->unprepare();
1143 } catch (CAudioError e) {
1144 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1145 return _convert_CAudioError(e);
1148 return AUDIO_IO_ERROR_NONE;
1151 int cpp_audio_out_pause(audio_out_h output) {
1152 audio_io_s* handle = static_cast<audio_io_s*>(output);
1155 if (handle == NULL) {
1156 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1159 assert(handle->audioIoHandle);
1161 handle->audioIoHandle->pause();
1162 } catch (CAudioError e) {
1163 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1164 return _convert_CAudioError(e);
1167 return AUDIO_IO_ERROR_NONE;
1170 int cpp_audio_out_resume(audio_out_h output) {
1171 audio_io_s* handle = static_cast<audio_io_s*>(output);
1174 if (handle == NULL) {
1175 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1178 assert(handle->audioIoHandle);
1180 handle->audioIoHandle->resume();
1181 } catch (CAudioError e) {
1182 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1183 return _convert_CAudioError(e);
1186 return AUDIO_IO_ERROR_NONE;
1189 int cpp_audio_out_drain(audio_out_h output) {
1190 audio_io_s* handle = static_cast<audio_io_s*>(output);
1193 if (handle == NULL) {
1194 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1197 assert(handle->audioIoHandle);
1199 handle->audioIoHandle->drain();
1200 } catch (CAudioError e) {
1201 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1202 return _convert_CAudioError(e);
1205 return AUDIO_IO_ERROR_NONE;
1208 int cpp_audio_out_flush(audio_out_h output) {
1209 audio_io_s* handle = static_cast<audio_io_s*>(output);
1212 if (handle == NULL) {
1213 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1216 assert(handle->audioIoHandle);
1218 handle->audioIoHandle->flush();
1219 } catch (CAudioError e) {
1220 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1221 return _convert_CAudioError(e);
1224 return AUDIO_IO_ERROR_NONE;
1227 int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) {
1228 audio_io_s* handle = static_cast<audio_io_s*>(output);
1232 if (handle == NULL) {
1233 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output);
1236 assert(handle->audioIoHandle);
1238 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1239 ret = outputHandle->write(buffer, length);
1240 #ifdef _AUDIO_IO_DEBUG_TIMING_
1241 AUDIO_IO_LOGD("ret:%d", ret);
1243 } catch (CAudioError e) {
1244 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1245 return _convert_CAudioError(e);
1251 int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) {
1252 audio_io_s* handle = static_cast<audio_io_s*>(output);
1255 if (handle == NULL || size == NULL) {
1256 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, size:%p", output, size);
1259 assert(handle->audioIoHandle);
1261 CAudioOutput* outputHandle = dynamic_cast<CAudioOutput*>(handle->audioIoHandle);
1262 *size = outputHandle->getBufferSize();
1263 } catch (CAudioError e) {
1264 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1265 return _convert_CAudioError(e);
1268 return AUDIO_IO_ERROR_NONE;
1271 int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
1272 audio_io_s* handle = static_cast<audio_io_s*>(output);
1275 if (handle == NULL || sample_rate == NULL) {
1276 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
1279 assert(handle->audioIoHandle);
1280 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
1281 } catch (CAudioError e) {
1282 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1283 return _convert_CAudioError(e);
1286 return AUDIO_IO_ERROR_NONE;
1289 int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
1290 audio_io_s* handle = static_cast<audio_io_s*>(output);
1293 if (handle == NULL || channel == NULL) {
1294 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, channel:%p", output, channel);
1297 assert(handle->audioIoHandle);
1299 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
1300 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
1301 _convert_audio_info_channel_2_channel(srcChannel, dstChannel);
1303 *channel = dstChannel;
1304 } catch (CAudioError e) {
1305 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1306 return _convert_CAudioError(e);
1309 return AUDIO_IO_ERROR_NONE;
1312 int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) {
1313 audio_io_s* handle = static_cast<audio_io_s*>(output);
1316 if (handle == NULL || type == NULL) {
1317 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1320 assert(handle->audioIoHandle);
1322 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
1323 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
1324 _convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
1326 *type = dstSampleType;
1327 } catch (CAudioError e) {
1328 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1329 return _convert_CAudioError(e);
1332 return AUDIO_IO_ERROR_NONE;
1335 int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) {
1336 audio_io_s* handle = static_cast<audio_io_s*>(output);
1339 if (handle == NULL || type == NULL) {
1340 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type);
1343 assert(handle->audioIoHandle);
1345 const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType();
1346 sound_type_e dstSoundType = SOUND_TYPE_MEDIA;
1347 _convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType);
1349 *type = dstSoundType;
1350 } catch (CAudioError e) {
1351 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1352 return _convert_CAudioError(e);
1355 return AUDIO_IO_ERROR_NONE;
1358 int cpp_audio_out_set_interrupted_cb(audio_out_h output, audio_io_interrupted_cb callback, void *user_data) {
1359 audio_io_s* handle = static_cast<audio_io_s*>(output);
1362 if (handle == NULL || callback == NULL) {
1363 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1366 assert(handle->audioIoHandle);
1368 handle->interrupt_callback.onInterrupt = callback;
1369 handle->interrupt_callback.user_data = user_data;
1371 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1372 cb.mUserData = static_cast<void*>(handle);
1373 cb.onInterrupt = _interrupt_cb_internal;
1375 handle->audioIoHandle->setInterruptCallback(cb);
1376 } catch (CAudioError e) {
1377 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1378 return _convert_CAudioError(e);
1381 return AUDIO_IO_ERROR_NONE;
1384 int cpp_audio_out_unset_interrupted_cb(audio_out_h output) {
1385 audio_io_s* handle = static_cast<audio_io_s*>(output);
1388 if (handle == NULL) {
1389 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1392 assert(handle->audioIoHandle);
1394 handle->interrupt_callback.onInterrupt = NULL;
1395 handle->interrupt_callback.user_data = NULL;
1397 CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback();
1398 cb.mUserData = NULL;
1399 cb.onInterrupt = NULL;
1401 handle->audioIoHandle->setInterruptCallback(cb);
1402 } catch (CAudioError e) {
1403 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1404 return _convert_CAudioError(e);
1407 return AUDIO_IO_ERROR_NONE;
1410 int cpp_audio_out_ignore_session(audio_out_h output) {
1411 audio_io_s* handle = static_cast<audio_io_s*>(output);
1414 if (handle == NULL) {
1415 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1418 if (handle->stream_callback.onStream) {
1419 THROW_ERROR_MSG(CAudioError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode");
1422 assert(handle->audioIoHandle);
1424 handle->audioIoHandle->ignoreSession();
1425 } catch (CAudioError e) {
1426 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1427 return _convert_CAudioError(e);
1430 return AUDIO_IO_ERROR_NONE;
1433 int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) {
1434 audio_io_s* handle = static_cast<audio_io_s*>(output);
1437 if (handle == NULL) {
1438 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1441 assert(handle->audioIoHandle);
1443 handle->stream_callback.onStream = callback;
1444 handle->stream_callback.user_data = user_data;
1446 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1447 cb.mUserData = static_cast<void*>(handle);
1448 cb.onStream = _stream_cb_internal;
1450 handle->audioIoHandle->setStreamCallback(cb);
1451 } catch (CAudioError e) {
1452 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1453 return _convert_CAudioError(e);
1456 return AUDIO_IO_ERROR_NONE;
1459 int cpp_audio_out_unset_stream_cb(audio_out_h output) {
1460 audio_io_s* handle = static_cast<audio_io_s*>(output);
1463 if (handle == NULL) {
1464 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1467 assert(handle->audioIoHandle);
1469 handle->stream_callback.onStream = NULL;
1470 handle->stream_callback.user_data = NULL;
1472 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1473 cb.mUserData = NULL;
1476 handle->audioIoHandle->setStreamCallback(cb);
1477 } catch (CAudioError e) {
1478 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1479 return _convert_CAudioError(e);
1482 return AUDIO_IO_ERROR_NONE;
1485 int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) {
1486 audio_io_s* handle = static_cast<audio_io_s*>(output);
1489 if (handle == NULL) {
1490 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback);
1493 assert(handle->audioIoHandle);
1495 handle->state_changed_callback.onStateChanged = callback;
1496 handle->state_changed_callback.user_data = user_data;
1498 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1499 cb.mUserData = static_cast<void*>(handle);
1500 cb.onStateChanged = _state_changed_cb_internal;
1502 handle->audioIoHandle->setStateChangedCallback(cb);
1503 } catch (CAudioError e) {
1504 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1505 return _convert_CAudioError(e);
1508 return AUDIO_IO_ERROR_NONE;
1511 int cpp_audio_out_unset_state_changed_cb(audio_out_h output) {
1512 audio_io_s* handle = static_cast<audio_io_s*>(output);
1515 if (handle == NULL) {
1516 THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output);
1519 assert(handle->audioIoHandle);
1521 handle->state_changed_callback.onStateChanged = NULL;
1522 handle->state_changed_callback.user_data = NULL;
1524 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1525 cb.mUserData = NULL;
1526 cb.onStateChanged = NULL;
1528 handle->audioIoHandle->setStateChangedCallback(cb);
1529 } catch (CAudioError e) {
1530 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1531 return _convert_CAudioError(e);
1534 return AUDIO_IO_ERROR_NONE;