X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fcpp%2Fcpp_audio_io.cpp;h=86fbf78fbd20539ff96337856be52558c09f01eb;hb=aa24b179dee00e98df93d29da0cf46e334b62c87;hp=182d92931ab9c8c53f2862c631a04f2333a3a118;hpb=907fc08da90dfa673c95794b5bf7b7980e8c3d76;p=platform%2Fcore%2Fapi%2Faudio-io.git diff --git a/src/cpp/cpp_audio_io.cpp b/src/cpp/cpp_audio_io.cpp index 182d929..86fbf78 100644 --- a/src/cpp/cpp_audio_io.cpp +++ b/src/cpp/cpp_audio_io.cpp @@ -22,6 +22,7 @@ #include "CAudioIODef.h" #include +#include #define FEATURE_MICROPHONE "http://tizen.org/feature/microphone" @@ -38,8 +39,17 @@ typedef struct audio_io_stream_cb_s { void* user_data; audio_in_stream_cb onStream; - audio_io_stream_cb_s() : user_data(NULL), onStream(NULL) - {/* Empty Body */} + audio_io_stream_cb_s() : user_data(nullptr), onStream(nullptr) { } + + void set(audio_in_stream_cb callback, void* userdata) { + onStream = callback; + user_data = userdata; + } + + void unset() { + onStream = nullptr; + user_data = nullptr; + } } audio_io_stream_cb_s; /** @@ -52,8 +62,18 @@ typedef struct audio_io_state_changed_cb_s { void* user_data; audio_in_state_changed_cb onStateChanged; - audio_io_state_changed_cb_s() : user_data(NULL), onStateChanged(NULL) - {/* Empty Body */} + audio_io_state_changed_cb_s() : user_data(nullptr), onStateChanged(nullptr) { } + + void set(audio_in_state_changed_cb callback, void* userdata) { + onStateChanged = callback; + user_data = userdata; + } + + void unset() { + onStateChanged = nullptr; + user_data = nullptr; + } + } audio_io_state_changed_cb_s; /** @@ -70,272 +90,172 @@ typedef struct audio_io_s { audio_io_stream_cb_s stream_callback; audio_io_state_changed_cb_s state_changed_callback; - audio_io_s() : audioIoHandle(NULL) - {/* Empty Body */} + audio_io_s() : audioIoHandle(nullptr) { } } audio_io_s; /** * Internal functions */ -static audio_io_error_e __convert_CAudioError(CAudioError& error) { - audio_io_error_e ret = AUDIO_IO_ERROR_NONE; - CAudioError::EError err = error.getError(); - - switch (err) { +static audio_io_error_e __convert_audio_io_error(CAudioError::EError error) { + switch (error) { case CAudioError::EError::ERROR_NONE: - ret = AUDIO_IO_ERROR_NONE; - break; + return AUDIO_IO_ERROR_NONE; case CAudioError::EError::ERROR_INVALID_ARGUMENT: case CAudioError::EError::ERROR_INVALID_HANDLE: - case CAudioError::EError::ERROR_INVALID_SAMPLERATE: - case CAudioError::EError::ERROR_INVALID_CHANNEL: - case CAudioError::EError::ERROR_INVALID_FORMAT: - ret = AUDIO_IO_ERROR_INVALID_PARAMETER; - break; + return AUDIO_IO_ERROR_INVALID_PARAMETER; case CAudioError::EError::ERROR_DEVICE_NOT_OPENED: - ret = AUDIO_IO_ERROR_DEVICE_NOT_OPENED; - break; + return AUDIO_IO_ERROR_DEVICE_NOT_OPENED; case CAudioError::EError::ERROR_DEVICE_NOT_CLOSED: - ret = AUDIO_IO_ERROR_DEVICE_NOT_CLOSED; - break; + return AUDIO_IO_ERROR_DEVICE_NOT_CLOSED; case CAudioError::EError::ERROR_PERMISSION_DENIED: - ret = AUDIO_IO_ERROR_PERMISSION_DENIED; - break; + return AUDIO_IO_ERROR_PERMISSION_DENIED; case CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION: - ret = AUDIO_IO_ERROR_DEVICE_POLICY_RESTRICTION; - break; + return AUDIO_IO_ERROR_DEVICE_POLICY_RESTRICTION; case CAudioError::EError::ERROR_NOT_SUPPORTED: - ret = AUDIO_IO_ERROR_NOT_SUPPORTED; - break; + return AUDIO_IO_ERROR_NOT_SUPPORTED; case CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE: - ret = AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE; - break; + return AUDIO_IO_ERROR_NOT_SUPPORTED_TYPE; case CAudioError::EError::ERROR_MAX: case CAudioError::EError::ERROR_INTERNAL_OPERATION: case CAudioError::EError::ERROR_NOT_INITIALIZED: case CAudioError::EError::ERROR_FAILED_OPERATION: case CAudioError::EError::ERROR_INVALID_OPERATION: - ret = AUDIO_IO_ERROR_INVALID_OPERATION; - break; + return AUDIO_IO_ERROR_INVALID_OPERATION; case CAudioError::EError::ERROR_INVALID_STATE: - ret = AUDIO_IO_ERROR_INVALID_STATE; - break; + return AUDIO_IO_ERROR_INVALID_STATE; case CAudioError::EError::ERROR_OUT_OF_MEMORY: case CAudioError::EError::ERROR_INVALID_POINTER: - ret = AUDIO_IO_ERROR_INVALID_BUFFER; - break; + return AUDIO_IO_ERROR_INVALID_BUFFER; case CAudioError::EError::ERROR_POLICY_BLOCKED: case CAudioError::EError::ERROR_POLICY_INTERRUPTED: case CAudioError::EError::ERROR_POLICY_DUPLICATED: - ret = AUDIO_IO_ERROR_SOUND_POLICY; - break; + return AUDIO_IO_ERROR_SOUND_POLICY; + default: + return AUDIO_IO_ERROR_NONE; } - - return ret; } -static void __convert_channel_2_audio_info_channel(const audio_channel_e& src_channel, - CAudioInfo::EChannel& dst_channel) { - switch (src_channel) { - case AUDIO_CHANNEL_MONO: - dst_channel = CAudioInfo::EChannel::CHANNEL_MONO; - break; - case AUDIO_CHANNEL_STEREO: - dst_channel = CAudioInfo::EChannel::CHANNEL_STEREO; - break; - default: - dst_channel = CAudioInfo::EChannel::CHANNEL_MONO; - break; - } +static CAudioInfo::EChannel __convert_channel_to_audio_info_channel(const audio_channel_e &src_channel) { + if (src_channel < AUDIO_CHANNEL_MONO || + src_channel > AUDIO_CHANNEL_MULTI_16) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid src_channel:%d", src_channel); + + return static_cast(src_channel - AUDIO_CHANNEL_MONO + 1); } -static void __convert_audio_info_channel_2_channel(const CAudioInfo::EChannel& src_channel, - audio_channel_e& dst_channel) { - switch (src_channel) { - case CAudioInfo::EChannel::CHANNEL_MONO: - dst_channel = AUDIO_CHANNEL_MONO; - break; - case CAudioInfo::EChannel::CHANNEL_STEREO: - dst_channel = AUDIO_CHANNEL_STEREO; - break; - default: - dst_channel = AUDIO_CHANNEL_MONO; - break; - } +static audio_channel_e __convert_audio_info_channel_to_channel(const CAudioInfo::EChannel& src_channel) { + if (src_channel < CAudioInfo::EChannel::CHANNEL_MONO || + src_channel >= CAudioInfo::EChannel::CHANNEL_MAX) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid src_channel:%d", static_cast(src_channel)); + + return static_cast(static_cast(src_channel) + AUDIO_CHANNEL_MONO - 1); } -static void __convert_sample_type_2_audio_info_sample_type(const audio_sample_type_e& src_type, - CAudioInfo::ESampleType& dst_type) { - switch (src_type) { - case AUDIO_SAMPLE_TYPE_U8: - dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8; - break; - case AUDIO_SAMPLE_TYPE_S16_LE: - dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE; - break; - case AUDIO_SAMPLE_TYPE_S24_LE: - dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S24_LE; - break; - case AUDIO_SAMPLE_TYPE_S24_32_LE: - dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_S24_32_LE; - break; - default: - dst_type = CAudioInfo::ESampleType::SAMPLE_TYPE_U8; - break; - } +static CAudioInfo::ESampleType __convert_sample_type_to_audio_info_sample_type(const audio_sample_type_e& src_type) { + if (src_type < AUDIO_SAMPLE_TYPE_U8 || + src_type > AUDIO_SAMPLE_TYPE_S32_LE) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid src_type:%d", src_type); + + return static_cast(static_cast(src_type) - AUDIO_SAMPLE_TYPE_U8 + 1); } -static void __convert_audio_info_sample_type_2_sample_type(const CAudioInfo::ESampleType& src_type, - audio_sample_type_e& dst_type) { - switch (src_type) { - case CAudioInfo::ESampleType::SAMPLE_TYPE_U8: - dst_type = AUDIO_SAMPLE_TYPE_U8; - break; - case CAudioInfo::ESampleType::SAMPLE_TYPE_S16_LE: - dst_type = AUDIO_SAMPLE_TYPE_S16_LE; - break; - case CAudioInfo::ESampleType::SAMPLE_TYPE_S24_LE: - dst_type = AUDIO_SAMPLE_TYPE_S24_LE; - break; - case CAudioInfo::ESampleType::SAMPLE_TYPE_S24_32_LE: - dst_type = AUDIO_SAMPLE_TYPE_S24_32_LE; - break; - default: - dst_type = AUDIO_SAMPLE_TYPE_U8; - break; - } +static audio_sample_type_e __convert_audio_info_sample_type_to_sample_type(const CAudioInfo::ESampleType &src_type) { + if (src_type < CAudioInfo::ESampleType::SAMPLE_TYPE_U8 || + src_type >= CAudioInfo::ESampleType::SAMPLE_TYPE_MAX) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid src_type:%d", static_cast(src_type)); + + return static_cast(static_cast(src_type) + AUDIO_SAMPLE_TYPE_U8 - 1); } -static void __convert_sound_type_2_audio_info_audio_type(const sound_type_e& src_type, - CAudioInfo::EAudioType& dst_type) { +static CAudioInfo::EAudioType __convert_sound_type_to_audio_info_audio_type(const sound_type_e &src_type) { switch (src_type) { case SOUND_TYPE_SYSTEM: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM; case SOUND_TYPE_NOTIFICATION: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION; case SOUND_TYPE_ALARM: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM; case SOUND_TYPE_RINGTONE: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP; case SOUND_TYPE_MEDIA: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA; case SOUND_TYPE_CALL: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM; case SOUND_TYPE_VOIP: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP; case SOUND_TYPE_VOICE: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION; default: - dst_type = CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA; - break; + return CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA; } } -static void __convert_audio_info_audio_type_2_sound_type(const CAudioInfo::EAudioType& src_type, - sound_type_e& dst_type) { +static sound_type_e __convert_audio_info_audio_type_to_sound_type(const CAudioInfo::EAudioType &src_type) { switch (src_type) { case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA: - dst_type = SOUND_TYPE_MEDIA; - break; + return SOUND_TYPE_MEDIA; case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_SYSTEM: - dst_type = SOUND_TYPE_SYSTEM; - break; + return SOUND_TYPE_SYSTEM; case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_ALARM: - dst_type = SOUND_TYPE_ALARM; - break; + return SOUND_TYPE_ALARM; case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_NOTIFICATION: case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_EMERGENCY: - dst_type = SOUND_TYPE_NOTIFICATION; - break; + return SOUND_TYPE_NOTIFICATION; case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOICE_INFORMATION: - dst_type = SOUND_TYPE_VOICE; - break; + return SOUND_TYPE_VOICE; case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_RINGTONE_VOIP: - dst_type = SOUND_TYPE_RINGTONE; - break; + return SOUND_TYPE_RINGTONE; case CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP: - dst_type = SOUND_TYPE_VOIP; - break; + return SOUND_TYPE_VOIP; default: - dst_type = SOUND_TYPE_MEDIA; - break; + return SOUND_TYPE_MEDIA; } } static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src_state) { - audio_io_state_e dst_state; - switch (src_state) { case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE: - dst_state = AUDIO_IO_STATE_IDLE; - break; + return AUDIO_IO_STATE_IDLE; case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE: - dst_state = AUDIO_IO_STATE_IDLE; - break; + return AUDIO_IO_STATE_IDLE; case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING: - dst_state = AUDIO_IO_STATE_RUNNING; - break; + return AUDIO_IO_STATE_RUNNING; case CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED: - dst_state = AUDIO_IO_STATE_PAUSED; - break; + return AUDIO_IO_STATE_PAUSED; default: - dst_state = AUDIO_IO_STATE_IDLE; - break; + return AUDIO_IO_STATE_IDLE; } - return dst_state; } -static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) { - if (sample_rate < 0) +static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, bool is_output) { + if (sample_rate < CAudioInfo::MIN_SYSTEM_SAMPLERATE || + sample_rate > CAudioInfo::MAX_SYSTEM_SAMPLERATE) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate); - if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) + if (channel < AUDIO_CHANNEL_MONO || + channel > ((is_output) ? AUDIO_CHANNEL_STEREO : AUDIO_CHANNEL_MULTI_16)) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel); - if (type != AUDIO_SAMPLE_TYPE_U8 && - type != AUDIO_SAMPLE_TYPE_S16_LE && - type != AUDIO_SAMPLE_TYPE_S24_LE && - type != AUDIO_SAMPLE_TYPE_S24_32_LE) - THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type); -} - -static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) { - __check_audio_param(sample_rate, channel, type); - - if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE) - THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type); + if (type < AUDIO_SAMPLE_TYPE_U8 || + type > AUDIO_SAMPLE_TYPE_S32_LE) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :0x%x", type); } static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) { - CAudioInfo::EChannel dstChannel; - CAudioInfo::ESampleType dstSampleType; - CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA; - - __convert_channel_2_audio_info_channel(channel, dstChannel); - __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType); - - return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1); + return CAudioInfo(sampleRate, + __convert_channel_to_audio_info_channel(channel), + __convert_sample_type_to_audio_info_sample_type(sample_type), + CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA, + -1); } static CAudioInfo __generate_audio_output_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type, sound_type_e sound_type) { - CAudioInfo::EChannel dstChannel; - CAudioInfo::ESampleType dstSampleType; - CAudioInfo::EAudioType dstAudioType; - - __convert_channel_2_audio_info_channel(channel, dstChannel); - __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType); - __convert_sound_type_2_audio_info_audio_type(sound_type, dstAudioType); - - return CAudioInfo(sampleRate, dstChannel, dstSampleType, dstAudioType, -1); + return CAudioInfo(sampleRate, + __convert_channel_to_audio_info_channel(channel), + __convert_sample_type_to_audio_info_sample_type(sample_type), + __convert_sound_type_to_audio_info_audio_type(sound_type), + -1); } static void __handle_safe_free(audio_io_s* handle, void *obj, bool is_output) { @@ -347,9 +267,9 @@ static void __handle_safe_free(audio_io_s* handle, void *obj, bool is_output) { VALID_POINTER_START(obj) if (is_output) - *(audio_out_h *)obj = NULL; + *(audio_out_h *)obj = nullptr; else - *(audio_in_h *)obj = NULL; + *(audio_in_h *)obj = nullptr; VALID_POINTER_END } @@ -357,25 +277,23 @@ static void __handle_safe_free(audio_io_s* handle, void *obj, bool is_output) { * Implements CAPI functions */ int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_in_h *input) { - audio_io_s* handle = NULL; + audio_io_s* handle = nullptr; bool mic_enable = false; - int ret = 0; + try { - if (input == NULL) { + if (!input) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } - __check_audio_param(sample_rate, channel, type); + __check_audio_param(sample_rate, channel, type, false); AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x]", sample_rate, channel, type); /* If MIC is not supported, return NOT_SUPPORTED error */ - ret = system_info_get_platform_bool(FEATURE_MICROPHONE, &mic_enable); + int ret = system_info_get_platform_bool(FEATURE_MICROPHONE, &mic_enable); AUDIO_IO_LOGD("system_info_platform [%s]=[%d], ret[%d]", FEATURE_MICROPHONE, mic_enable, ret); - if (ret != SYSTEM_INFO_ERROR_NONE || !mic_enable) { + if (ret != SYSTEM_INFO_ERROR_NONE || !mic_enable) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "System doesn't support microphone!"); - } CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type); @@ -385,16 +303,15 @@ int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_t AUDIO_IO_LOGD("[%p] created", handle); *input = handle; - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); __handle_safe_free(handle, (void *)input, false); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } catch (const std::bad_alloc&) { //LCOV_EXCL_START - CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY; AUDIO_IO_LOGE("Failed to allocate handle"); __handle_safe_free(handle, (void *)input, false); - return __convert_CAudioError(e); + return __convert_audio_io_error(CAudioError::EError::ERROR_OUT_OF_MEMORY); //LCOV_EXCL_STOP } @@ -402,24 +319,26 @@ int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_t } int cpp_audio_in_destroy(audio_in_h input) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); assert(handle->audioIoHandle); - AUDIO_IO_LOGD("[%p]", handle); + AUDIO_IO_LOGD("unpreparing [%p]", handle); /* Internal unprepare for backward compatibility */ handle->audioIoHandle->unprepare(); + AUDIO_IO_LOGD("try to destroy [%p]", handle); + SAFE_FINALIZE(handle->audioIoHandle); SAFE_DELETE(handle->audioIoHandle); SAFE_DELETE(handle); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("destroyed"); @@ -428,19 +347,19 @@ int cpp_audio_in_destroy(audio_in_h input) { } int cpp_audio_in_set_sound_stream_info(audio_in_h input, sound_stream_info_h stream_info) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL || stream_info == NULL) + if (!handle || !stream_info) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, stream_info:%p", input, stream_info); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p], stream_info:[%p]", handle, stream_info); handle->audioIoHandle->setStreamInfo(stream_info); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -449,19 +368,19 @@ int cpp_audio_in_set_sound_stream_info(audio_in_h input, sound_stream_info_h str } int cpp_audio_in_prepare(audio_in_h input) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->prepare(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] prepared", handle); @@ -470,19 +389,19 @@ int cpp_audio_in_prepare(audio_in_h input) { } int cpp_audio_in_unprepare(audio_in_h input) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->unprepare(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] unprepared", handle); @@ -491,19 +410,19 @@ int cpp_audio_in_unprepare(audio_in_h input) { } int cpp_audio_in_pause(audio_in_h input) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->pause(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] paused", handle); @@ -512,19 +431,19 @@ int cpp_audio_in_pause(audio_in_h input) { } int cpp_audio_in_resume(audio_in_h input) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->resume(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] resumed", handle); @@ -532,43 +451,20 @@ int cpp_audio_in_resume(audio_in_h input) { return AUDIO_IO_ERROR_NONE; } -//LCOV_EXCL_START -int cpp_audio_in_drain(audio_in_h input) { - audio_io_s* handle = static_cast(input); - - try { - if (handle == NULL) - THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, - "Parameters are NULL input:%p", input); - assert(handle->audioIoHandle); - AUDIO_IO_LOGD("[%p]", handle); - - handle->audioIoHandle->drain(); - } catch (CAudioError& e) { - AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); - } - - AUDIO_IO_LOGD("[%p] drained", handle); - - return AUDIO_IO_ERROR_NONE; -} -//LCOV_EXCL_STOP - int cpp_audio_in_flush(audio_in_h input) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->flush(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] flushed", handle); @@ -577,119 +473,106 @@ int cpp_audio_in_flush(audio_in_h input) { } int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) { - audio_io_s* handle = static_cast(input); int ret = 0; try { - if (handle == NULL || buffer == NULL) + auto handle = static_cast(input); + if (!handle || !buffer) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer); assert(handle->audioIoHandle); - CAudioInput* inputHandle = static_cast(handle->audioIoHandle); - if (inputHandle == NULL) { + auto inputHandle = static_cast(handle->audioIoHandle); + if (!inputHandle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); - } - size_t readn = inputHandle->read(buffer, static_cast(length)); + auto readn = inputHandle->read(buffer, static_cast(length)); ret = static_cast(readn); #ifdef _AUDIO_IO_DEBUG_TIMING_ - AUDIO_IO_LOGD("readn:%d", readn); + AUDIO_IO_LOGD("readn:%zu", readn); #endif - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return ret; } int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) { - audio_io_s* handle = static_cast(input); - try { - if (handle == NULL || size == NULL) + auto handle = static_cast(input); + if (!handle || !size) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, size:%p", input, size); assert(handle->audioIoHandle); - CAudioIO* inputHandle = static_cast(handle->audioIoHandle); - if (inputHandle == NULL) { + auto inputHandle = static_cast(handle->audioIoHandle); + if (!inputHandle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); - } + *size = inputHandle->getBufferSize(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) { - audio_io_s* handle = static_cast(input); - try { - if (handle == NULL || sample_rate == NULL) + auto handle = static_cast(input); + if (!handle || !sample_rate) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate); assert(handle->audioIoHandle); *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) { - audio_io_s* handle = static_cast(input); - try { - if (handle == NULL || channel == NULL) + auto handle = static_cast(input); + if (!handle || !channel) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, channel:%p", input, channel); assert(handle->audioIoHandle); - const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel(); - audio_channel_e dstChannel = AUDIO_CHANNEL_MONO; - __convert_audio_info_channel_2_channel(srcChannel, dstChannel); - - *channel = dstChannel; - } catch (CAudioError& e) { + *channel = __convert_audio_info_channel_to_channel(handle->audioIoHandle->getAudioInfo().getChannel()); + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) { - audio_io_s* handle = static_cast(input); - try { - if (handle == NULL || type == NULL) + auto handle = static_cast(input); + if (!handle || !type) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, type:%p", input, type); assert(handle->audioIoHandle); - const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType(); - audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8; - __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType); - - *type = dstSampleType; - } catch (CAudioError& e) { + *type = __convert_audio_info_sample_type_to_sample_type(handle->audioIoHandle->getAudioInfo().getSampleType()); + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } static void __stream_cb_internal(size_t nbytes, void *user_data) { - audio_io_s* audioIo = static_cast(user_data); + auto audioIo = static_cast(user_data); assert(audioIo); if (audioIo->stream_callback.onStream) @@ -701,7 +584,7 @@ static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, CAudioInfo::EAudioIOState state_prev, bool by_policy, void *user_data) { - audio_io_s* audioIo = static_cast(user_data); + auto audioIo = static_cast(user_data); assert(audioIo); if (audioIo->state_changed_callback.onStateChanged) @@ -712,26 +595,24 @@ static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, //LCOV_EXCL_STOP int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL || callback == NULL) + if (!handle || !callback) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); - handle->stream_callback.onStream = callback; - handle->stream_callback.user_data = user_data; + handle->stream_callback.set(callback, user_data); - CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback(); - cb.mUserData = static_cast(handle); - cb.onStream = __stream_cb_internal; + auto cb = handle->audioIoHandle->getStreamCallback(); + cb.set(__stream_cb_internal, static_cast(handle)); handle->audioIoHandle->setStreamCallback(cb); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -740,26 +621,23 @@ int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, vo } int cpp_audio_in_unset_stream_cb(audio_in_h input) { - audio_io_s* handle = static_cast(input); - + auto handle = static_cast(input); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); - handle->stream_callback.onStream = NULL; - handle->stream_callback.user_data = NULL; + handle->stream_callback.unset(); - CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback(); - cb.mUserData = NULL; - cb.onStream = NULL; + auto cb = handle->audioIoHandle->getStreamCallback(); + cb.unset(); handle->audioIoHandle->setStreamCallback(cb); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -768,22 +646,22 @@ int cpp_audio_in_unset_stream_cb(audio_in_h input) { } int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *length) { - audio_io_s* handle = static_cast(input); size_t _length = 0; try { - if (handle == NULL || buffer == NULL) + auto handle = static_cast(input); + if (!handle || !buffer) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer); - CAudioInput* inputHandle = static_cast(handle->audioIoHandle); - if (inputHandle == NULL) - THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); + auto inputHandle = static_cast(handle->audioIoHandle); + if (!inputHandle) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); // LCOV_EXCL_LINE inputHandle->peek(buffer, &_length); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } *length = (unsigned int)_length; @@ -792,47 +670,44 @@ int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *lengt } int cpp_audio_in_drop(audio_in_h input) { - audio_io_s* handle = static_cast(input); - try { - if (handle == NULL) + auto handle = static_cast(input); + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - CAudioInput* inputHandle = static_cast(handle->audioIoHandle); - if (inputHandle == NULL) - THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); + auto inputHandle = static_cast(handle->audioIoHandle); + if (!inputHandle) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); // LCOV_EXCL_LINE inputHandle->drop(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_cb callback, void* user_data) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL || callback == NULL) + if (!handle || !callback) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); - handle->state_changed_callback.onStateChanged = callback; - handle->state_changed_callback.user_data = user_data; + handle->state_changed_callback.set(callback, user_data); - CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback(); - cb.mUserData = static_cast(handle); - cb.onStateChanged = __state_changed_cb_internal; + auto cb = handle->audioIoHandle->getStateChangedCallback(); + cb.set(__state_changed_cb_internal, static_cast(handle)); handle->audioIoHandle->setStateChangedCallback(cb); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -841,26 +716,49 @@ int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_c } int cpp_audio_in_unset_state_changed_cb(audio_in_h input) { - audio_io_s* handle = static_cast(input); + auto handle = static_cast(input); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, - "Parameters are NULL output:%p", input); + "Parameters are NULL input:%p", input); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); - handle->state_changed_callback.onStateChanged = NULL; - handle->state_changed_callback.user_data = NULL; + handle->state_changed_callback.unset(); - CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback(); - cb.mUserData = NULL; - cb.onStateChanged = NULL; + auto cb = handle->audioIoHandle->getStateChangedCallback(); + cb.unset(); handle->audioIoHandle->setStateChangedCallback(cb); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { + AUDIO_IO_LOGE("%s", e.getErrorMsg()); + return __convert_audio_io_error(e.getError()); + } + + AUDIO_IO_LOGD("[%p] done", handle); + + return AUDIO_IO_ERROR_NONE; +} + +int cpp_audio_in_get_volume(audio_in_h input, double *volume) { + auto handle = static_cast(input); + + try { + if (!handle || !volume) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, + "Parameters are NULL input:%p, volume:%p", input, volume); + assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); + + auto input_handle = dynamic_cast(handle->audioIoHandle); + if (input_handle == nullptr) + return __convert_audio_io_error(CAudioError::EError::ERROR_INVALID_HANDLE); + + *volume = input_handle->getVolume(); + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -868,18 +766,46 @@ int cpp_audio_in_unset_state_changed_cb(audio_in_h input) { return AUDIO_IO_ERROR_NONE; } +int cpp_audio_in_set_volume(audio_in_h input, double volume) { + auto handle = static_cast(input); + + try { + if (!handle) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, + "Parameters are NULL input:%p", input); + + if (volume < CAudioInfo::MIN_RECORD_VOLUME || volume > CAudioInfo::MAX_RECORD_VOLUME) + THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid volume: %f", volume); + + assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); + + auto input_handle = dynamic_cast(handle->audioIoHandle); + if (input_handle == nullptr) + return __convert_audio_io_error(CAudioError::EError::ERROR_INVALID_HANDLE); + + input_handle->setVolume(volume); + } catch (const CAudioError& e) { + AUDIO_IO_LOGE("%s", e.getErrorMsg()); + return __convert_audio_io_error(e.getError()); + } + + AUDIO_IO_LOGD("[%p] done", handle); + + return AUDIO_IO_ERROR_NONE; +} /** * Audio Out */ int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) { - audio_io_s* handle = NULL; + audio_io_s* handle = nullptr; try { - if (output == NULL) + if (!output) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); - __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */); + __check_audio_param(sample_rate, channel, type, true); AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x]", sample_rate, channel, type); CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA); @@ -890,16 +816,15 @@ int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sam AUDIO_IO_LOGD("[%p] created", handle); *output = handle; - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); __handle_safe_free(handle, (void *)output, true); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } catch (const std::bad_alloc&) { //LCOV_EXCL_START - CAudioError e = CAudioError::EError::ERROR_OUT_OF_MEMORY; AUDIO_IO_LOGE("Failed to allocate handle"); __handle_safe_free(handle, (void *)output, true); - return __convert_CAudioError(e); + return __convert_audio_io_error(CAudioError::EError::ERROR_OUT_OF_MEMORY); //LCOV_EXCL_STOP } @@ -907,24 +832,26 @@ int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sam } int cpp_audio_out_destroy(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); assert(handle->audioIoHandle); - AUDIO_IO_LOGD("[%p]", handle); + AUDIO_IO_LOGD("unpreparing [%p]", handle); /* Internal unprepare for backward compatibility */ handle->audioIoHandle->unprepare(); + AUDIO_IO_LOGD("try to destroy [%p]", handle); + SAFE_FINALIZE(handle->audioIoHandle); SAFE_DELETE(handle->audioIoHandle); SAFE_DELETE(handle); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("destroyed"); @@ -933,19 +860,19 @@ int cpp_audio_out_destroy(audio_out_h output) { } int cpp_audio_out_set_sound_stream_info(audio_out_h output, sound_stream_info_h stream_info) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL || stream_info == NULL) + if (!handle || !stream_info) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, stream_info:%p", output, stream_info); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p], stream_info:[%p]", handle, stream_info); handle->audioIoHandle->setStreamInfo(stream_info); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -954,19 +881,19 @@ int cpp_audio_out_set_sound_stream_info(audio_out_h output, sound_stream_info_h } int cpp_audio_out_prepare(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->prepare(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] prepared", handle); @@ -975,19 +902,19 @@ int cpp_audio_out_prepare(audio_out_h output) { } int cpp_audio_out_unprepare(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->unprepare(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] unprepared", handle); @@ -996,19 +923,19 @@ int cpp_audio_out_unprepare(audio_out_h output) { } int cpp_audio_out_pause(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->pause(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] paused", handle); @@ -1017,19 +944,19 @@ int cpp_audio_out_pause(audio_out_h output) { } int cpp_audio_out_resume(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->resume(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] resumed", handle); @@ -1038,19 +965,23 @@ int cpp_audio_out_resume(audio_out_h output) { } int cpp_audio_out_drain(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); - handle->audioIoHandle->drain(); - } catch (CAudioError& e) { + auto output_handle = dynamic_cast(handle->audioIoHandle); + if (output_handle == nullptr) + return __convert_audio_io_error(CAudioError::EError::ERROR_INVALID_HANDLE); + + output_handle->drain(); + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] drained", handle); @@ -1059,19 +990,19 @@ int cpp_audio_out_drain(audio_out_h output) { } int cpp_audio_out_flush(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->flush(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] flushed", handle); @@ -1080,159 +1011,141 @@ int cpp_audio_out_flush(audio_out_h output) { } int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) { - audio_io_s* handle = static_cast(output); int ret = 0; try { - if (handle == NULL || buffer == NULL) + auto handle = static_cast(output); + if (!handle || !buffer) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p, buffer:%p", output, buffer); assert(handle->audioIoHandle); - CAudioOutput* outputHandle = static_cast(handle->audioIoHandle); - if (outputHandle == NULL) + auto outputHandle = static_cast(handle->audioIoHandle); + if (!outputHandle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); - size_t written = outputHandle->write(buffer, static_cast(length)); + auto written = outputHandle->write(buffer, static_cast(length)); ret = static_cast(written); #ifdef _AUDIO_IO_DEBUG_TIMING_ - AUDIO_IO_LOGD("written:%d", written); + AUDIO_IO_LOGD("written:%zu", written); #endif - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return ret; } int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) { - audio_io_s* handle = static_cast(output); - try { - if (handle == NULL || size == NULL) + auto handle = static_cast(output); + if (!handle || !size) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, size:%p", output, size); assert(handle->audioIoHandle); - CAudioOutput* outputHandle = static_cast(handle->audioIoHandle); - if (outputHandle == NULL) + auto outputHandle = static_cast(handle->audioIoHandle); + if (!outputHandle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); *size = outputHandle->getBufferSize(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) { - audio_io_s* handle = static_cast(output); - try { - if (handle == NULL || sample_rate == NULL) + auto handle = static_cast(output); + if (!handle || !sample_rate) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate); assert(handle->audioIoHandle); *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) { - audio_io_s* handle = static_cast(output); - try { - if (handle == NULL || channel == NULL) + auto handle = static_cast(output); + if (!handle || !channel) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, channel:%p", output, channel); assert(handle->audioIoHandle); - const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel(); - audio_channel_e dstChannel = AUDIO_CHANNEL_MONO; - __convert_audio_info_channel_2_channel(srcChannel, dstChannel); - - *channel = dstChannel; - } catch (CAudioError& e) { + *channel = __convert_audio_info_channel_to_channel(handle->audioIoHandle->getAudioInfo().getChannel()); + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) { - audio_io_s* handle = static_cast(output); - try { - if (handle == NULL || type == NULL) + auto handle = static_cast(output); + if (!handle || !type) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type); assert(handle->audioIoHandle); - const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType(); - audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8; - __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType); - - *type = dstSampleType; - } catch (CAudioError& e) { + *type = __convert_audio_info_sample_type_to_sample_type(handle->audioIoHandle->getAudioInfo().getSampleType()); + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL || type == NULL) + if (!handle || !type) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type); assert(handle->audioIoHandle); - const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType(); - sound_type_e dstSoundType = SOUND_TYPE_MEDIA; - __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType); - - *type = dstSoundType; - } catch (CAudioError& e) { + *type = __convert_audio_info_audio_type_to_sound_type(handle->audioIoHandle->getAudioInfo().getAudioType()); + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } return AUDIO_IO_ERROR_NONE; } int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback, void* user_data) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL || callback == NULL) + if (!handle || !callback) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); - handle->stream_callback.onStream = callback; - handle->stream_callback.user_data = user_data; + handle->stream_callback.set(callback, user_data); - CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback(); - cb.mUserData = static_cast(handle); - cb.onStream = __stream_cb_internal; + auto cb = handle->audioIoHandle->getStreamCallback(); + cb.set(__stream_cb_internal, static_cast(handle)); handle->audioIoHandle->setStreamCallback(cb); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -1241,26 +1154,24 @@ int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback } int cpp_audio_out_unset_stream_cb(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); - handle->stream_callback.onStream = NULL; - handle->stream_callback.user_data = NULL; + handle->stream_callback.unset(); - CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback(); - cb.mUserData = NULL; - cb.onStream = NULL; + auto cb = handle->audioIoHandle->getStreamCallback(); + cb.unset(); handle->audioIoHandle->setStreamCallback(cb); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -1269,26 +1180,24 @@ int cpp_audio_out_unset_stream_cb(audio_out_h output) { } int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_changed_cb callback, void* user_data) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL || callback == NULL) + if (!handle || !callback) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); - handle->state_changed_callback.onStateChanged = callback; - handle->state_changed_callback.user_data = user_data; + handle->state_changed_callback.set(callback, user_data); - CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback(); - cb.mUserData = static_cast(handle); - cb.onStateChanged = __state_changed_cb_internal; + auto cb = handle->audioIoHandle->getStateChangedCallback(); + cb.set(__state_changed_cb_internal, static_cast(handle)); handle->audioIoHandle->setStateChangedCallback(cb); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle); @@ -1297,26 +1206,24 @@ int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_change } int cpp_audio_out_unset_state_changed_cb(audio_out_h output) { - audio_io_s* handle = static_cast(output); + auto handle = static_cast(output); try { - if (handle == NULL) + if (!handle) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); assert(handle->audioIoHandle); AUDIO_IO_LOGD("[%p]", handle); - handle->state_changed_callback.onStateChanged = NULL; - handle->state_changed_callback.user_data = NULL; + handle->state_changed_callback.unset(); - CAudioIO::SStateChangedCallback cb = handle->audioIoHandle->getStateChangedCallback(); - cb.mUserData = NULL; - cb.onStateChanged = NULL; + auto cb = handle->audioIoHandle->getStateChangedCallback(); + cb.unset(); handle->audioIoHandle->setStateChangedCallback(cb); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); - return __convert_CAudioError(e); + return __convert_audio_io_error(e.getError()); } AUDIO_IO_LOGD("[%p] done", handle);