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.
20 #include "cpp_audio_io.h"
22 #include "CAudioIODef.h"
24 #include <system_info.h>
26 #define FEATURE_MICROPHONE "http://tizen.org/feature/microphone"
29 using namespace tizen_media_audio;
34 * Name : audio_io_stream_cb_s
35 * Declaration : Keeps user callback pointer and user data for delivering an stream event
37 typedef struct audio_io_stream_cb_s {
39 audio_in_stream_cb onStream;
41 audio_io_stream_cb_s() : user_data(NULL), onStream(NULL)
43 } audio_io_stream_cb_s;
48 * Name : audio_io_state_changed_cb_s
49 * Declaration : Keeps user callback pointer and user data for delivering an state changed event
51 typedef struct audio_io_state_changed_cb_s {
53 audio_in_state_changed_cb onStateChanged;
55 audio_io_state_changed_cb_s() : user_data(NULL), onStateChanged(NULL)
57 } audio_io_state_changed_cb_s;
63 * Declaration : An handle of AudioIO
64 * The handle has two struct for user callback
65 * And the handle has a pointer of private audioIO object
66 * The CAudioIO is a abstract class object about Input and Output
68 typedef struct audio_io_s {
69 CAudioIO* audioIoHandle;
70 audio_io_stream_cb_s stream_callback;
71 audio_io_state_changed_cb_s state_changed_callback;
73 audio_io_s() : audioIoHandle(NULL)
81 static audio_io_error_e __convert_CAudioError(CAudioError& error) {
82 audio_io_error_e ret = AUDIO_IO_ERROR_NONE;
83 CAudioError::EError err = error.getError();
86 case CAudioError::EError::ERROR_NONE:
87 ret = AUDIO_IO_ERROR_NONE;
89 case CAudioError::EError::ERROR_INVALID_ARGUMENT:
90 case CAudioError::EError::ERROR_INVALID_HANDLE:
91 case CAudioError::EError::ERROR_INVALID_SAMPLERATE:
92 case CAudioError::EError::ERROR_INVALID_CHANNEL:
93 case CAudioError::EError::ERROR_INVALID_FORMAT:
94 ret = AUDIO_IO_ERROR_INVALID_PARAMETER;
96 case CAudioError::EError::ERROR_DEVICE_NOT_OPENED:
97 ret = AUDIO_IO_ERROR_DEVICE_NOT_OPENED;
99 case CAudioError::EError::ERROR_DEVICE_NOT_CLOSED:
100 ret = AUDIO_IO_ERROR_DEVICE_NOT_CLOSED;
102 case CAudioError::EError::ERROR_PERMISSION_DENIED:
103 ret = AUDIO_IO_ERROR_PERMISSION_DENIED;
105 case CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION:
106 ret = AUDIO_IO_ERROR_DEVICE_POLICY_RESTRICTION;
108 case CAudioError::EError::ERROR_NOT_SUPPORTED:
109 ret = AUDIO_IO_ERROR_NOT_SUPPORTED;
111 case CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE:
112 ret = AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE;
114 case CAudioError::EError::ERROR_MAX:
115 case CAudioError::EError::ERROR_INTERNAL_OPERATION:
116 case CAudioError::EError::ERROR_NOT_INITIALIZED:
117 case CAudioError::EError::ERROR_FAILED_OPERATION:
118 case CAudioError::EError::ERROR_INVALID_OPERATION:
119 ret = AUDIO_IO_ERROR_INVALID_OPERATION;
121 case CAudioError::EError::ERROR_INVALID_STATE:
122 ret = AUDIO_IO_ERROR_INVALID_STATE;
124 case CAudioError::EError::ERROR_OUT_OF_MEMORY:
125 case CAudioError::EError::ERROR_INVALID_POINTER:
126 ret = AUDIO_IO_ERROR_INVALID_BUFFER;
128 case CAudioError::EError::ERROR_POLICY_BLOCKED:
129 case CAudioError::EError::ERROR_POLICY_INTERRUPTED:
130 case CAudioError::EError::ERROR_POLICY_DUPLICATED:
131 ret = AUDIO_IO_ERROR_SOUND_POLICY;
138 static void __convert_channel_2_audio_info_channel(const audio_channel_e& src_channel,
139 CAudioInfo::EChannel& dst_channel) {
140 switch (src_channel) {
141 case AUDIO_CHANNEL_MONO:
142 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
144 case AUDIO_CHANNEL_STEREO:
145 dst_channel = CAudioInfo::EChannel::CHANNEL_STEREO;
148 dst_channel = CAudioInfo::EChannel::CHANNEL_MONO;
153 static void __convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel,
154 audio_channel_e& dst_channel) {
155 switch (src_channel) {
156 case CAudioInfo::EChannel::CHANNEL_MONO:
157 dst_channel = AUDIO_CHANNEL_MONO;
159 case CAudioInfo::EChannel::CHANNEL_STEREO:
160 dst_channel = AUDIO_CHANNEL_STEREO;
163 dst_channel = AUDIO_CHANNEL_MONO;
168 static void __convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type,
169 CAudioInfo::ESampleType& dst_type) {
171 case AUDIO_SAMPLE_TYPE_U8:
172 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
174 case AUDIO_SAMPLE_TYPE_S16_LE:
175 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE;
178 dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8;
183 static void __convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type,
184 audio_sample_type_e& dst_type) {
186 case CAudioInfo::ESampleType::SAMPLE_TYPE_U8:
187 dst_type = AUDIO_SAMPLE_TYPE_U8;
189 case CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE:
190 dst_type = AUDIO_SAMPLE_TYPE_S16_LE;
193 dst_type = AUDIO_SAMPLE_TYPE_U8;
198 static void __convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type,
199 CAudioInfo::EAudioType& dst_type) {
201 case SOUND_TYPE_SYSTEM:
202 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
204 case SOUND_TYPE_NOTIFICATION:
205 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION;
207 case SOUND_TYPE_ALARM:
208 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM;
210 case SOUND_TYPE_RINGTONE:
211 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP;
213 case SOUND_TYPE_MEDIA:
214 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
216 case SOUND_TYPE_CALL:
217 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM;
219 case SOUND_TYPE_VOIP:
220 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP;
222 case SOUND_TYPE_VOICE:
223 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION;
226 dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
231 static void __convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type,
232 sound_type_e& dst_type) {
234 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA:
235 dst_type = SOUND_TYPE_MEDIA;
237 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM:
238 dst_type = SOUND_TYPE_SYSTEM;
240 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM:
241 dst_type = SOUND_TYPE_ALARM;
243 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION:
244 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_EMERGENCY:
245 dst_type = SOUND_TYPE_NOTIFICATION;
247 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION:
248 dst_type = SOUND_TYPE_VOICE;
250 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP:
251 dst_type = SOUND_TYPE_RINGTONE;
253 case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP:
254 dst_type = SOUND_TYPE_VOIP;
257 dst_type = SOUND_TYPE_MEDIA;
262 static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src_state) {
263 audio_io_state_e dst_state;
266 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE:
267 dst_state = AUDIO_IO_STATE_IDLE;
269 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE:
270 dst_state = AUDIO_IO_STATE_IDLE;
272 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING:
273 dst_state = AUDIO_IO_STATE_RUNNING;
275 case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED:
276 dst_state = AUDIO_IO_STATE_PAUSED;
279 dst_state = AUDIO_IO_STATE_IDLE;
285 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) {
287 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate);
289 if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO)
290 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel);
292 if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE)
293 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type);
296 static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) {
297 __check_audio_param(sample_rate, channel, type);
299 if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE)
300 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type);
303 static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) {
304 CAudioInfo::EChannel dstChannel;
305 CAudioInfo::ESampleType dstSampleType;
306 CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
308 __convert_channel_2_audio_info_channel(channel, dstChannel);
309 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
311 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
314 static CAudioInfo __generate_audio_output_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type, sound_type_e sound_type) {
315 CAudioInfo::EChannel dstChannel;
316 CAudioInfo::ESampleType dstSampleType;
317 CAudioInfo::EAudioType dstAudioType;
319 __convert_channel_2_audio_info_channel(channel, dstChannel);
320 __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType);
321 __convert_sound_type_2_audio_info_audio_type(sound_type, dstAudioType);
323 return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1);
326 static void __handle_safe_free(audio_io_s* handle, void *obj, bool is_output) {
327 VALID_POINTER_START(handle)
328 SAFE_FINALIZE(handle->audioIoHandle);
329 SAFE_DELETE(handle->audioIoHandle);
333 VALID_POINTER_START(obj)
335 *(audio_out_h *)obj = NULL;
337 *(audio_in_h *)obj = NULL;
342 * Implements CAPI functions
344 int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_in_h *input) {
345 audio_io_s* handle = NULL;
346 bool mic_enable = false;
350 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
351 "Parameters are NULL input:%p", input);
354 __check_audio_param(sample_rate, channel, type);
356 AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x]", sample_rate, channel, type);
358 /* If MIC is not supported, return NOT_SUPPORTED error */
359 ret = system_info_get_platform_bool(FEATURE_MICROPHONE, &mic_enable);
360 AUDIO_IO_LOGD("system_info_platform [%s]=[%d], ret[%d]", FEATURE_MICROPHONE, mic_enable, ret);
361 if (ret != SYSTEM_INFO_ERROR_NONE || !mic_enable) {
362 THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "System doesn't support microphone!");
365 CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type);
367 handle = new audio_io_s;
368 handle->audioIoHandle = new CAudioInput(audioInfo);
369 handle->audioIoHandle->initialize();
371 AUDIO_IO_LOGD("[%p] created", handle);
373 } catch (CAudioError& e) {
374 AUDIO_IO_LOGE("%s", e.getErrorMsg());
375 __handle_safe_free(handle, (void *)input, false);
376 return __convert_CAudioError(e);
377 } catch (const std::bad_alloc&) {
378 CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY;
379 AUDIO_IO_LOGE("Failed to allocate handle");
380 __handle_safe_free(handle, (void *)input, false);
381 return __convert_CAudioError(e);
384 return AUDIO_IO_ERROR_NONE;
387 int cpp_audio_in_destroy(audio_in_h input) {
388 audio_io_s* handle = static_cast<audio_io_s*>(input);
392 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
393 "Parameters are NULL input:%p", input);
394 assert(handle->audioIoHandle);
395 AUDIO_IO_LOGD("[%p]", handle);
397 /* Internal unprepare for backward compatibility */
398 handle->audioIoHandle->unprepare();
400 SAFE_FINALIZE(handle->audioIoHandle);
401 SAFE_DELETE(handle->audioIoHandle);
403 } catch (CAudioError& e) {
404 AUDIO_IO_LOGE("%s", e.getErrorMsg());
405 return __convert_CAudioError(e);
408 AUDIO_IO_LOGD("destroyed");
410 return AUDIO_IO_ERROR_NONE;
413 int cpp_audio_in_set_sound_stream_info(audio_in_h input, sound_stream_info_h stream_info) {
414 audio_io_s* handle = static_cast<audio_io_s*>(input);
417 if (handle == NULL || stream_info == NULL)
418 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
419 "Parameters are NULL input:%p, stream_info:%p", input, stream_info);
420 assert(handle->audioIoHandle);
421 AUDIO_IO_LOGD("[%p], stream_info:[%p]", handle, stream_info);
423 handle->audioIoHandle->setStreamInfo(stream_info);
424 } catch (CAudioError& e) {
425 AUDIO_IO_LOGE("%s", e.getErrorMsg());
426 return __convert_CAudioError(e);
429 AUDIO_IO_LOGD("[%p] done", handle);
431 return AUDIO_IO_ERROR_NONE;
434 int cpp_audio_in_prepare(audio_in_h input) {
435 audio_io_s* handle = static_cast<audio_io_s*>(input);
439 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
440 "Parameters are NULL input:%p", input);
441 assert(handle->audioIoHandle);
442 AUDIO_IO_LOGD("[%p]", handle);
444 handle->audioIoHandle->prepare();
445 } catch (CAudioError& e) {
446 AUDIO_IO_LOGE("%s", e.getErrorMsg());
447 return __convert_CAudioError(e);
450 AUDIO_IO_LOGD("[%p] prepared", handle);
452 return AUDIO_IO_ERROR_NONE;
455 int cpp_audio_in_unprepare(audio_in_h input) {
456 audio_io_s* handle = static_cast<audio_io_s*>(input);
460 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
461 "Parameters are NULL input:%p", input);
462 assert(handle->audioIoHandle);
463 AUDIO_IO_LOGD("[%p]", handle);
465 handle->audioIoHandle->unprepare();
466 } catch (CAudioError& e) {
467 AUDIO_IO_LOGE("%s", e.getErrorMsg());
468 return __convert_CAudioError(e);
471 AUDIO_IO_LOGD("[%p] unprepared", handle);
473 return AUDIO_IO_ERROR_NONE;
476 int cpp_audio_in_pause(audio_in_h input) {
477 audio_io_s* handle = static_cast<audio_io_s*>(input);
481 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
482 "Parameters are NULL input:%p", input);
483 assert(handle->audioIoHandle);
484 AUDIO_IO_LOGD("[%p]", handle);
486 handle->audioIoHandle->pause();
487 } catch (CAudioError& e) {
488 AUDIO_IO_LOGE("%s", e.getErrorMsg());
489 return __convert_CAudioError(e);
492 AUDIO_IO_LOGD("[%p] paused", handle);
494 return AUDIO_IO_ERROR_NONE;
497 int cpp_audio_in_resume(audio_in_h input) {
498 audio_io_s* handle = static_cast<audio_io_s*>(input);
502 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
503 "Parameters are NULL input:%p", input);
504 assert(handle->audioIoHandle);
505 AUDIO_IO_LOGD("[%p]", handle);
507 handle->audioIoHandle->resume();
508 } catch (CAudioError& e) {
509 AUDIO_IO_LOGE("%s", e.getErrorMsg());
510 return __convert_CAudioError(e);
513 AUDIO_IO_LOGD("[%p] resumed", handle);
515 return AUDIO_IO_ERROR_NONE;
518 int cpp_audio_in_drain(audio_in_h input) {
519 audio_io_s* handle = static_cast<audio_io_s*>(input);
523 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
524 "Parameters are NULL input:%p", input);
525 assert(handle->audioIoHandle);
526 AUDIO_IO_LOGD("[%p]", handle);
528 handle->audioIoHandle->drain();
529 } catch (CAudioError& e) {
530 AUDIO_IO_LOGE("%s", e.getErrorMsg());
531 return __convert_CAudioError(e);
534 AUDIO_IO_LOGD("[%p] drained", handle);
536 return AUDIO_IO_ERROR_NONE;
539 int cpp_audio_in_flush(audio_in_h input) {
540 audio_io_s* handle = static_cast<audio_io_s*>(input);
544 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
545 "Parameters are NULL input:%p", input);
546 assert(handle->audioIoHandle);
547 AUDIO_IO_LOGD("[%p]", handle);
549 handle->audioIoHandle->flush();
550 } catch (CAudioError& e) {
551 AUDIO_IO_LOGE("%s", e.getErrorMsg());
552 return __convert_CAudioError(e);
555 AUDIO_IO_LOGD("[%p] flushed", handle);
557 return AUDIO_IO_ERROR_NONE;
560 int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) {
561 audio_io_s* handle = static_cast<audio_io_s*>(input);
565 if (handle == NULL || buffer == NULL)
566 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
567 "Parameters are NULL input:%p, buffer:%p", input, buffer);
568 assert(handle->audioIoHandle);
570 CAudioInput* inputHandle = static_cast<CAudioInput*>(handle->audioIoHandle);
571 if (inputHandle == NULL) {
572 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
575 size_t readn = inputHandle->read(buffer, static_cast<size_t>(length));
576 ret = static_cast<int>(readn);
577 #ifdef _AUDIO_IO_DEBUG_TIMING_
578 AUDIO_IO_LOGD("readn:%d", readn);
580 } catch (CAudioError& e) {
581 AUDIO_IO_LOGE("%s", e.getErrorMsg());
582 return __convert_CAudioError(e);
588 int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) {
589 audio_io_s* handle = static_cast<audio_io_s*>(input);
592 if (handle == NULL || size == NULL)
593 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
594 "Parameters are NULL input:%p, size:%p", input, size);
595 assert(handle->audioIoHandle);
597 CAudioIO* inputHandle = static_cast<CAudioInput*>(handle->audioIoHandle);
598 if (inputHandle == NULL) {
599 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
601 *size = inputHandle->getBufferSize();
602 } catch (CAudioError& e) {
603 AUDIO_IO_LOGE("%s", e.getErrorMsg());
604 return __convert_CAudioError(e);
607 return AUDIO_IO_ERROR_NONE;
610 int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) {
611 audio_io_s* handle = static_cast<audio_io_s*>(input);
614 if (handle == NULL || sample_rate == NULL)
615 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
616 "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
617 assert(handle->audioIoHandle);
619 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
620 } catch (CAudioError& e) {
621 AUDIO_IO_LOGE("%s", e.getErrorMsg());
622 return __convert_CAudioError(e);
625 return AUDIO_IO_ERROR_NONE;
628 int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
629 audio_io_s* handle = static_cast<audio_io_s*>(input);
632 if (handle == NULL || channel == NULL)
633 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
634 "Parameters are NULL input:%p, channel:%p", input, channel);
635 assert(handle->audioIoHandle);
637 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
638 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
639 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
641 *channel = dstChannel;
642 } catch (CAudioError& e) {
643 AUDIO_IO_LOGE("%s", e.getErrorMsg());
644 return __convert_CAudioError(e);
647 return AUDIO_IO_ERROR_NONE;
650 int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
651 audio_io_s* handle = static_cast<audio_io_s*>(input);
654 if (handle == NULL || type == NULL)
655 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
656 "Parameters are NULL input:%p, type:%p", input, type);
657 assert(handle->audioIoHandle);
659 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
660 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
661 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
663 *type = dstSampleType;
664 } catch (CAudioError& e) {
665 AUDIO_IO_LOGE("%s", e.getErrorMsg());
666 return __convert_CAudioError(e);
669 return AUDIO_IO_ERROR_NONE;
672 static void __stream_cb_internal(size_t nbytes, void *user_data) {
673 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
676 if (audioIo->stream_callback.onStream)
677 audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data);
680 static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state,
681 CAudioInfo::EAudioIOState state_prev,
684 audio_io_s* audioIo = static_cast<audio_io_s*>(user_data);
687 if (audioIo->state_changed_callback.onStateChanged)
688 audioIo->state_changed_callback.onStateChanged(audioIo, __convert_state_type(state_prev),
689 __convert_state_type(state), by_policy,
690 audioIo->state_changed_callback.user_data);
693 int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) {
694 audio_io_s* handle = static_cast<audio_io_s*>(input);
697 if (handle == NULL || callback == NULL)
698 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
699 "Parameters are NULL input:%p, callback:%p", input, callback);
700 assert(handle->audioIoHandle);
701 AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data);
703 handle->stream_callback.onStream = callback;
704 handle->stream_callback.user_data = user_data;
706 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
707 cb.mUserData = static_cast<void*>(handle);
708 cb.onStream = __stream_cb_internal;
710 handle->audioIoHandle->setStreamCallback(cb);
711 } catch (CAudioError& e) {
712 AUDIO_IO_LOGE("%s", e.getErrorMsg());
713 return __convert_CAudioError(e);
716 AUDIO_IO_LOGD("[%p] done", handle);
718 return AUDIO_IO_ERROR_NONE;
721 int cpp_audio_in_unset_stream_cb(audio_in_h input) {
722 audio_io_s* handle = static_cast<audio_io_s*>(input);
726 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
727 "Parameters are NULL input:%p", input);
728 assert(handle->audioIoHandle);
729 AUDIO_IO_LOGD("[%p]", handle);
731 handle->stream_callback.onStream = NULL;
732 handle->stream_callback.user_data = NULL;
734 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
738 handle->audioIoHandle->setStreamCallback(cb);
739 } catch (CAudioError& e) {
740 AUDIO_IO_LOGE("%s", e.getErrorMsg());
741 return __convert_CAudioError(e);
744 AUDIO_IO_LOGD("[%p] done", handle);
746 return AUDIO_IO_ERROR_NONE;
749 int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) {
750 audio_io_s* handle = static_cast<audio_io_s*>(input);
754 if (handle == NULL || buffer == NULL)
755 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
756 "Parameters are NULL input:%p, buffer:%p", input, buffer);
758 CAudioInput* inputHandle = static_cast<CAudioInput*>(handle->audioIoHandle);
759 if (inputHandle == NULL)
760 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
762 inputHandle->peek(buffer, &_length);
763 } catch (CAudioError& e) {
764 AUDIO_IO_LOGE("%s", e.getErrorMsg());
765 return __convert_CAudioError(e);
768 *length = (unsigned int)_length;
770 return AUDIO_IO_ERROR_NONE;
773 int cpp_audio_in_drop(audio_in_h input) {
774 audio_io_s* handle = static_cast<audio_io_s*>(input);
778 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
779 "Parameters are NULL input:%p", input);
781 CAudioInput* inputHandle = static_cast<CAudioInput*>(handle->audioIoHandle);
782 if (inputHandle == NULL)
783 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
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_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) {
795 audio_io_s* handle = static_cast<audio_io_s*>(input);
798 if (handle == NULL || callback == NULL)
799 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
800 "Parameters are NULL input:%p, callback:%p", input, callback);
801 assert(handle->audioIoHandle);
802 AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data);
804 handle->state_changed_callback.onStateChanged = callback;
805 handle->state_changed_callback.user_data = user_data;
807 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
808 cb.mUserData = static_cast<void*>(handle);
809 cb.onStateChanged = __state_changed_cb_internal;
811 handle->audioIoHandle->setStateChangedCallback(cb);
812 } catch (CAudioError& e) {
813 AUDIO_IO_LOGE("%s", e.getErrorMsg());
814 return __convert_CAudioError(e);
817 AUDIO_IO_LOGD("[%p] done", handle);
819 return AUDIO_IO_ERROR_NONE;
822 int cpp_audio_in_unset_state_changed_cb(audio_in_h input) {
823 audio_io_s* handle = static_cast<audio_io_s*>(input);
827 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
828 "Parameters are NULL output:%p", input);
829 assert(handle->audioIoHandle);
830 AUDIO_IO_LOGD("[%p]", handle);
832 handle->state_changed_callback.onStateChanged = NULL;
833 handle->state_changed_callback.user_data = NULL;
835 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
837 cb.onStateChanged = NULL;
839 handle->audioIoHandle->setStateChangedCallback(cb);
840 } catch (CAudioError& e) {
841 AUDIO_IO_LOGE("%s", e.getErrorMsg());
842 return __convert_CAudioError(e);
845 AUDIO_IO_LOGD("[%p] done", handle);
847 return AUDIO_IO_ERROR_NONE;
854 int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) {
855 audio_io_s* handle = NULL;
858 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
859 "Parameters are NULL output:%p", output);
861 __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */);
863 AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x]", sample_rate, channel, type);
864 CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA);
866 handle = new audio_io_s;
867 handle->audioIoHandle = new CAudioOutput(audioInfo);
868 handle->audioIoHandle->initialize();
870 AUDIO_IO_LOGD("[%p] created", handle);
872 } catch (CAudioError& e) {
873 AUDIO_IO_LOGE("%s", e.getErrorMsg());
874 __handle_safe_free(handle, (void *)output, true);
875 return __convert_CAudioError(e);
876 } catch (const std::bad_alloc&) {
877 CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY;
878 AUDIO_IO_LOGE("Failed to allocate handle");
879 __handle_safe_free(handle, (void *)output, true);
880 return __convert_CAudioError(e);
883 return AUDIO_IO_ERROR_NONE;
886 int cpp_audio_out_destroy(audio_out_h output) {
887 audio_io_s* handle = static_cast<audio_io_s*>(output);
891 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
892 "Parameter is NULL output:%p", output);
893 assert(handle->audioIoHandle);
894 AUDIO_IO_LOGD("[%p]", handle);
896 /* Internal unprepare for backward compatibility */
897 handle->audioIoHandle->unprepare();
899 SAFE_FINALIZE(handle->audioIoHandle);
900 SAFE_DELETE(handle->audioIoHandle);
902 } catch (CAudioError& e) {
903 AUDIO_IO_LOGE("%s", e.getErrorMsg());
904 return __convert_CAudioError(e);
907 AUDIO_IO_LOGD("destroyed");
909 return AUDIO_IO_ERROR_NONE;
912 int cpp_audio_out_set_sound_stream_info(audio_out_h output, sound_stream_info_h stream_info) {
913 audio_io_s* handle = static_cast<audio_io_s*>(output);
916 if (handle == NULL || stream_info == NULL)
917 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
918 "Parameters are NULL output:%p, stream_info:%p", output, stream_info);
919 assert(handle->audioIoHandle);
920 AUDIO_IO_LOGD("[%p], stream_info:[%p]", handle, stream_info);
922 handle->audioIoHandle->setStreamInfo(stream_info);
923 } catch (CAudioError& e) {
924 AUDIO_IO_LOGE("%s", e.getErrorMsg());
925 return __convert_CAudioError(e);
928 AUDIO_IO_LOGD("[%p] done", handle);
930 return AUDIO_IO_ERROR_NONE;
933 int cpp_audio_out_prepare(audio_out_h output) {
934 audio_io_s* handle = static_cast<audio_io_s*>(output);
938 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
939 "Parameter is NULL output:%p", output);
940 assert(handle->audioIoHandle);
941 AUDIO_IO_LOGD("[%p]", handle);
943 handle->audioIoHandle->prepare();
944 } catch (CAudioError& e) {
945 AUDIO_IO_LOGE("%s", e.getErrorMsg());
946 return __convert_CAudioError(e);
949 AUDIO_IO_LOGD("[%p] prepared", handle);
951 return AUDIO_IO_ERROR_NONE;
954 int cpp_audio_out_unprepare(audio_out_h output) {
955 audio_io_s* handle = static_cast<audio_io_s*>(output);
959 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
960 "Parameter is NULL output:%p", output);
961 assert(handle->audioIoHandle);
962 AUDIO_IO_LOGD("[%p]", handle);
964 handle->audioIoHandle->unprepare();
965 } catch (CAudioError& e) {
966 AUDIO_IO_LOGE("%s", e.getErrorMsg());
967 return __convert_CAudioError(e);
970 AUDIO_IO_LOGD("[%p] unprepared", handle);
972 return AUDIO_IO_ERROR_NONE;
975 int cpp_audio_out_pause(audio_out_h output) {
976 audio_io_s* handle = static_cast<audio_io_s*>(output);
980 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
981 "Parameter is NULL output:%p", output);
982 assert(handle->audioIoHandle);
983 AUDIO_IO_LOGD("[%p]", handle);
985 handle->audioIoHandle->pause();
986 } catch (CAudioError& e) {
987 AUDIO_IO_LOGE("%s", e.getErrorMsg());
988 return __convert_CAudioError(e);
991 AUDIO_IO_LOGD("[%p] paused", handle);
993 return AUDIO_IO_ERROR_NONE;
996 int cpp_audio_out_resume(audio_out_h output) {
997 audio_io_s* handle = static_cast<audio_io_s*>(output);
1001 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1002 "Parameter is NULL output:%p", output);
1003 assert(handle->audioIoHandle);
1004 AUDIO_IO_LOGD("[%p]", handle);
1006 handle->audioIoHandle->resume();
1007 } catch (CAudioError& e) {
1008 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1009 return __convert_CAudioError(e);
1012 AUDIO_IO_LOGD("[%p] resumed", handle);
1014 return AUDIO_IO_ERROR_NONE;
1017 int cpp_audio_out_drain(audio_out_h output) {
1018 audio_io_s* handle = static_cast<audio_io_s*>(output);
1022 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1023 "Parameter is NULL output:%p", output);
1024 assert(handle->audioIoHandle);
1025 AUDIO_IO_LOGD("[%p]", handle);
1027 handle->audioIoHandle->drain();
1028 } catch (CAudioError& e) {
1029 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1030 return __convert_CAudioError(e);
1033 AUDIO_IO_LOGD("[%p] drained", handle);
1035 return AUDIO_IO_ERROR_NONE;
1038 int cpp_audio_out_flush(audio_out_h output) {
1039 audio_io_s* handle = static_cast<audio_io_s*>(output);
1043 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1044 "Parameter is NULL output:%p", output);
1045 assert(handle->audioIoHandle);
1046 AUDIO_IO_LOGD("[%p]", handle);
1048 handle->audioIoHandle->flush();
1049 } catch (CAudioError& e) {
1050 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1051 return __convert_CAudioError(e);
1054 AUDIO_IO_LOGD("[%p] flushed", handle);
1056 return AUDIO_IO_ERROR_NONE;
1059 int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) {
1060 audio_io_s* handle = static_cast<audio_io_s*>(output);
1064 if (handle == NULL || buffer == NULL)
1065 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1066 "Parameter is NULL output:%p, buffer:%p", output, buffer);
1067 assert(handle->audioIoHandle);
1069 CAudioOutput* outputHandle = static_cast<CAudioOutput*>(handle->audioIoHandle);
1070 if (outputHandle == NULL)
1071 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1073 size_t written = outputHandle->write(buffer, static_cast<size_t>(length));
1074 ret = static_cast<int>(written);
1075 #ifdef _AUDIO_IO_DEBUG_TIMING_
1076 AUDIO_IO_LOGD("written:%d", written);
1078 } catch (CAudioError& e) {
1079 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1080 return __convert_CAudioError(e);
1086 int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) {
1087 audio_io_s* handle = static_cast<audio_io_s*>(output);
1090 if (handle == NULL || size == NULL)
1091 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1092 "Parameters are NULL output:%p, size:%p", output, size);
1093 assert(handle->audioIoHandle);
1095 CAudioOutput* outputHandle = static_cast<CAudioOutput*>(handle->audioIoHandle);
1096 if (outputHandle == NULL)
1097 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL");
1099 *size = outputHandle->getBufferSize();
1100 } catch (CAudioError& e) {
1101 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1102 return __convert_CAudioError(e);
1105 return AUDIO_IO_ERROR_NONE;
1108 int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
1109 audio_io_s* handle = static_cast<audio_io_s*>(output);
1112 if (handle == NULL || sample_rate == NULL)
1113 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1114 "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
1115 assert(handle->audioIoHandle);
1117 *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
1118 } catch (CAudioError& e) {
1119 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1120 return __convert_CAudioError(e);
1123 return AUDIO_IO_ERROR_NONE;
1126 int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
1127 audio_io_s* handle = static_cast<audio_io_s*>(output);
1130 if (handle == NULL || channel == NULL)
1131 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1132 "Parameters are NULL output:%p, channel:%p", output, channel);
1133 assert(handle->audioIoHandle);
1135 const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel();
1136 audio_channel_e dstChannel = AUDIO_CHANNEL_MONO;
1137 __convert_audio_info_channel_2_channel(srcChannel, dstChannel);
1139 *channel = dstChannel;
1140 } catch (CAudioError& e) {
1141 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1142 return __convert_CAudioError(e);
1145 return AUDIO_IO_ERROR_NONE;
1148 int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) {
1149 audio_io_s* handle = static_cast<audio_io_s*>(output);
1152 if (handle == NULL || type == NULL)
1153 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1154 "Parameters are NULL output:%p, type:%p", output, type);
1155 assert(handle->audioIoHandle);
1157 const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType();
1158 audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8;
1159 __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType);
1161 *type = dstSampleType;
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_get_sound_type(audio_out_h output, sound_type_e *type) {
1171 audio_io_s* handle = static_cast<audio_io_s*>(output);
1174 if (handle == NULL || type == NULL)
1175 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1176 "Parameters are NULL output:%p, type:%p", output, type);
1177 assert(handle->audioIoHandle);
1179 const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType();
1180 sound_type_e dstSoundType = SOUND_TYPE_MEDIA;
1181 __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType);
1183 *type = dstSoundType;
1184 } catch (CAudioError& e) {
1185 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1186 return __convert_CAudioError(e);
1189 return AUDIO_IO_ERROR_NONE;
1192 int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) {
1193 audio_io_s* handle = static_cast<audio_io_s*>(output);
1196 if (handle == NULL || callback == NULL)
1197 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1198 "Parameters are NULL output:%p, callback:%p", output, callback);
1199 assert(handle->audioIoHandle);
1200 AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data);
1202 handle->stream_callback.onStream = callback;
1203 handle->stream_callback.user_data = user_data;
1205 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1206 cb.mUserData = static_cast<void*>(handle);
1207 cb.onStream = __stream_cb_internal;
1209 handle->audioIoHandle->setStreamCallback(cb);
1210 } catch (CAudioError& e) {
1211 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1212 return __convert_CAudioError(e);
1215 AUDIO_IO_LOGD("[%p] done", handle);
1217 return AUDIO_IO_ERROR_NONE;
1220 int cpp_audio_out_unset_stream_cb(audio_out_h output) {
1221 audio_io_s* handle = static_cast<audio_io_s*>(output);
1225 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1226 "Parameters are NULL output:%p", output);
1227 assert(handle->audioIoHandle);
1228 AUDIO_IO_LOGD("[%p]", handle);
1230 handle->stream_callback.onStream = NULL;
1231 handle->stream_callback.user_data = NULL;
1233 CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback();
1234 cb.mUserData = NULL;
1237 handle->audioIoHandle->setStreamCallback(cb);
1238 } catch (CAudioError& e) {
1239 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1240 return __convert_CAudioError(e);
1243 AUDIO_IO_LOGD("[%p] done", handle);
1245 return AUDIO_IO_ERROR_NONE;
1248 int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) {
1249 audio_io_s* handle = static_cast<audio_io_s*>(output);
1252 if (handle == NULL || callback == NULL)
1253 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1254 "Parameters are NULL output:%p, callback:%p", output, callback);
1255 assert(handle->audioIoHandle);
1256 AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data);
1258 handle->state_changed_callback.onStateChanged = callback;
1259 handle->state_changed_callback.user_data = user_data;
1261 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1262 cb.mUserData = static_cast<void*>(handle);
1263 cb.onStateChanged = __state_changed_cb_internal;
1265 handle->audioIoHandle->setStateChangedCallback(cb);
1266 } catch (CAudioError& e) {
1267 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1268 return __convert_CAudioError(e);
1271 AUDIO_IO_LOGD("[%p] done", handle);
1273 return AUDIO_IO_ERROR_NONE;
1276 int cpp_audio_out_unset_state_changed_cb(audio_out_h output) {
1277 audio_io_s* handle = static_cast<audio_io_s*>(output);
1281 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
1282 "Parameters are NULL output:%p", output);
1283 assert(handle->audioIoHandle);
1284 AUDIO_IO_LOGD("[%p]", handle);
1286 handle->state_changed_callback.onStateChanged = NULL;
1287 handle->state_changed_callback.user_data = NULL;
1289 CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback();
1290 cb.mUserData = NULL;
1291 cb.onStateChanged = NULL;
1293 handle->audioIoHandle->setStateChangedCallback(cb);
1294 } catch (CAudioError& e) {
1295 AUDIO_IO_LOGE("%s", e.getErrorMsg());
1296 return __convert_CAudioError(e);
1299 AUDIO_IO_LOGD("[%p] done", handle);
1301 return AUDIO_IO_ERROR_NONE;