/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "multimedia_audio_ffi.h" #include "audio_log.h" #include "multimedia_audio_capturer_callback.h" #include "multimedia_audio_capturer_impl.h" #include "multimedia_audio_error.h" #include "multimedia_audio_manager_impl.h" #include "multimedia_audio_routing_manager_callback.h" #include "multimedia_audio_routing_manager_impl.h" #include "multimedia_audio_stream_manager_callback.h" #include "multimedia_audio_stream_manager_impl.h" #include "multimedia_audio_volume_group_manager_impl.h" #include "multimedia_audio_volume_manager_impl.h" using namespace OHOS::FFI; namespace OHOS { namespace AudioStandard { extern "C" { // Audio Capturer int64_t FfiMMACreateAudioCapturer(CAudioCapturerOptions options, int32_t *errorCode) { auto capturer = FFIData::Create(); if (!capturer) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("Create MMAAudioCapturerImpl error"); return CJ_ERR_INVALID_RETURN_VALUE; } auto ret = capturer->CreateAudioCapturer(options); if (ret != SUCCESS_CODE) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("CreateAudioCapturer error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return capturer->GetID(); } int32_t FfiMMAAudioCapturerGetState(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("get state failed, invalid id of AudioCapturer"); return CJ_ERR_INVALID_RETURN_VALUE; } return capturer->GetState(); } uint32_t FfiMMAAudioCapturerGetStreamId(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("get stream id failed, invalid id of AudioCapturer"); return CJ_ERR_INVALID_RETURN_VALUE; } return capturer->GetStreamId(errorCode); } int64_t FfiMMAAudioCapturerGetAudioTime(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("get audio time failed, invalid id of AudioCapturer"); return CJ_ERR_INVALID_RETURN_VALUE; } return capturer->GetAudioTime(errorCode); } uint32_t FfiMMAAudioCapturerGetBufferSize(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("get buffer size failed, invalid id of AudioCapturer"); return CJ_ERR_INVALID_RETURN_VALUE; } return capturer->GetBufferSize(errorCode); } uint32_t FfiMMAAudioCapturerGetOverflowCount(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("get OverflowCount failed, invalid id of AudioCapturer"); return CJ_ERR_INVALID_RETURN_VALUE; } return capturer->GetOverflowCount(); } void FfiMMAAudioCapturerStart(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("AudioCapturer start failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; } auto isSuccess = capturer->Start(); if (isSuccess != SUCCESS_CODE) { *errorCode = isSuccess; } } void FfiMMAAudioCapturerStop(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("AudioCapturer stop failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; } auto isSuccess = capturer->Stop(); if (isSuccess != SUCCESS_CODE) { *errorCode = isSuccess; } } void FfiMMAAudioCapturerRelease(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("AudioCapturer release failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; } auto isSuccess = capturer->Release(); if (isSuccess != SUCCESS_CODE) { *errorCode = isSuccess; } } CAudioCapturerChangeInfo FfiMMAAudioCapturerGetAudioCapturerChangeInfo(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("Get AudioCapturerChangeInfo failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; return CAudioCapturerChangeInfo(); } auto info = capturer->GetAudioCapturerChangeInfo(errorCode); return info; } CArrDeviceDescriptor FfiMMAAudioCapturerGetInputDevices(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("Get InputDevices failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; return CArrDeviceDescriptor(); } auto devices = capturer->GetInputDevices(errorCode); return devices; } CAudioCapturerInfo FfiMMAAudioCapturerGetCapturerInfo(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("Get AudioCapturerInfo failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; return CAudioCapturerInfo(); } auto info = capturer->GetCurrentCapturerInfo(errorCode); return info; } CAudioStreamInfo FfiMMAAudioCapturerGetStreamInfo(int64_t id, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("Get StreamInfo failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; return CAudioStreamInfo(); } auto info = capturer->GetStreamInfo(errorCode); return info; } void FfiMMAAudioCapturerOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("register failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; } capturer->RegisterCallback(callbackType, callback, errorCode); } void FfiMMAAudioCapturerOnWithFrame(int64_t id, int32_t callbackType, void (*callback)(), int64_t frame, int32_t *errorCode) { auto capturer = FFIData::GetData(id); if (!capturer) { AUDIO_ERR_LOG("register failed, invalid id of AudioCapturer"); *errorCode = CJ_ERR_SYSTEM; } capturer->RegisterCallbackWithFrame(callbackType, callback, frame, errorCode); } // Audio Manager int64_t FfiMMACreateAudioManager(int32_t *errorCode) { auto mgr = FFIData::Create(); if (!mgr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("Create AudioManager error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return mgr->GetID(); } int64_t FfiMMAAudioManagerGetRoutingManager(int64_t id, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("Get RoutingManager failed, invalid id of AudioManager"); *errorCode = CJ_ERR_SYSTEM; return CJ_ERR_INVALID_RETURN_VALUE; } return mgr->GetRoutingManager(errorCode); } int64_t FfiMMAAudioManagerGetStreamManager(int64_t id, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("Get StreamManager failed, invalid id of AudioManager"); *errorCode = CJ_ERR_SYSTEM; return CJ_ERR_INVALID_RETURN_VALUE; } return mgr->GetStreamManger(errorCode); } int32_t FfiMMAAudioManagerGetAudioScene(int64_t id, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("Get AudioScene failed, invalid id of AudioManager"); *errorCode = CJ_ERR_SYSTEM; return CJ_ERR_INVALID_RETURN_VALUE; } return mgr->GetAudioScene(); } int64_t FfiMMAAudioManagerGetVolumeManager(int64_t id, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (mgr == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("FfiMMAAudioManagerGetVolumeManager failed."); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return mgr->GetVolumeManager(errorCode); } // Audio Stream Manager bool FfiMMAASMIsActive(int64_t id, int32_t volumeType, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("IsActive failed, invalid id of AudioStreamManager"); *errorCode = CJ_ERR_SYSTEM; return false; } return mgr->IsActive(volumeType); } CArrAudioCapturerChangeInfo FfiMMAASMGetCurrentAudioCapturerInfoArray(int64_t id, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("Get CurrentAudioCapturerInfoArray failed, invalid id of AudioStreamManager"); *errorCode = CJ_ERR_SYSTEM; return CArrAudioCapturerChangeInfo(); } return mgr->GetAudioCapturerInfoArray(errorCode); } CArrI32 FfiMMAASMGetAudioEffectInfoArray(int64_t id, int32_t usage, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("Get AudioEffectInfoArray failed, invalid id of AudioStreamManager"); *errorCode = CJ_ERR_SYSTEM; return CArrI32(); } return mgr->GetAudioEffectInfoArray(usage, errorCode); } void FfiMMAASMOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("register failed, invalid id of AudioStreamManager"); *errorCode = CJ_ERR_SYSTEM; } mgr->RegisterCallback(callbackType, callback, errorCode); } // Audio Routing Manager void FfiMMAARMSetCommunicationDevice(int64_t id, int32_t deviceType, bool active, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("SetCommunicationDevice failed, invalid id of AudioRoutingManager"); *errorCode = CJ_ERR_SYSTEM; } *errorCode = mgr->SetCommunicationDevice(deviceType, active); } bool FfiMMAARMIsCommunicationDeviceActive(int64_t id, int32_t deviceType, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("IsCommunicationDeviceActive failed, invalid id of AudioRoutingManager"); *errorCode = CJ_ERR_SYSTEM; return false; } return mgr->IsCommunicationDeviceActive(deviceType); } CArrDeviceDescriptor FfiMMAARMGetDevices(int64_t id, int32_t deviceFlag, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("GetDevices failed, invalid id of AudioRoutingManager"); *errorCode = CJ_ERR_SYSTEM; return CArrDeviceDescriptor(); } return mgr->GetDevices(deviceFlag, errorCode); } CArrDeviceDescriptor FfiMMAARMGetPreferredInputDeviceForCapturerInfo(int64_t id, CAudioCapturerInfo capturerInfo, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("GetPreferredInputDeviceForCapturerInfo failed, invalid id of AudioRoutingManager"); *errorCode = CJ_ERR_SYSTEM; return CArrDeviceDescriptor(); } return mgr->GetPreferredInputDeviceForCapturerInfo(capturerInfo, errorCode); } void FfiMMAARMOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("register failed, invalid id of AudioRoutingManager"); *errorCode = CJ_ERR_SYSTEM; } mgr->RegisterCallback(callbackType, callback, errorCode); } void FfiMMAARMOnWithFlags(int64_t id, int32_t callbackType, void (*callback)(), int32_t flags, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("register failed, invalid id of AudioRoutingManager"); *errorCode = CJ_ERR_SYSTEM; } mgr->RegisterDeviceChangeCallback(callbackType, callback, flags, errorCode); } void FfiMMAARMWOnithCapturerInfo(int64_t id, int32_t callbackType, void (*callback)(), CAudioCapturerInfo capturerInfo, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (!mgr) { AUDIO_ERR_LOG("register failed, invalid id of AudioRoutingManager"); *errorCode = CJ_ERR_SYSTEM; } mgr->RegisterPreferredInputDeviceChangeCallback(callbackType, callback, capturerInfo, errorCode); } /* Audio Volume Manager */ int64_t FfiMMAAVMGetVolumeGroupManager(int64_t id, int32_t groupId, int32_t *errorCode) { auto mgr = FFIData::GetData(id); if (mgr == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("FfiMMAAVMGetVolumeGroupManager failed."); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return mgr->GetVolumeGroupManager(groupId, errorCode); } /* Audio Volumne Group Manager */ int32_t FfiMMAAVGMGetMaxVolume(int64_t id, int32_t volumeType, int32_t *errorCode) { auto inst = FFIData::GetData(id); if (inst == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("FfiMMAAVGMGetMaxVolume error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return inst->GetMaxVolume(volumeType); } int32_t FfiMMAAVGMGetMinVolume(int64_t id, int32_t volumeType, int32_t *errorCode) { auto inst = FFIData::GetData(id); if (inst == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("FfiMMAAVGMGetMinVolume error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return inst->GetMinVolume(volumeType); } int32_t FfiMMAAVGMGetRingerMode(int64_t id, int32_t *errorCode) { auto inst = FFIData::GetData(id); if (inst == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("FfiMMAAVGMGetRingerMode error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return inst->GetRingerMode(); } float FfiMMAAVGMGetSystemVolumeInDb(int64_t id, int32_t volumeType, int32_t volumeLevel, int32_t device, int32_t *errorCode) { auto inst = FFIData::GetData(id); if (inst == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("FfiMMAAVGMGetSystemVolumeInDb error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return inst->GetSystemVolumeInDb(volumeType, volumeLevel, device); } int32_t FfiMMAAVGMGetVolume(int64_t id, int32_t volumeType, int32_t *errorCode) { auto inst = FFIData::GetData(id); if (inst == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("FfiMMAAVGMGetVolume error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return inst->GetVolume(volumeType); } bool FfiMMAAVGMIsMicrophoneMute(int64_t id, int32_t *errorCode) { auto inst = FFIData::GetData(id); if (inst == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("Get FfiMMAAVGMIsMicrophoneMute error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return inst->IsMicrophoneMute(); } bool FfiMMAAVGMIsMute(int64_t id, int32_t volumeType, int32_t *errorCode) { auto inst = FFIData::GetData(id); if (inst == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("Get FfiMMAAVGMIsMute error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return inst->IsMute(volumeType); } bool FfiMMAAVGMIsVolumeUnadjustable(int64_t id, int32_t *errorCode) { auto inst = FFIData::GetData(id); if (inst == nullptr) { *errorCode = CJ_ERR_SYSTEM; AUDIO_ERR_LOG("Get FfiMMAAVGMIsVolumeUnadjustable error"); return CJ_ERR_INVALID_RETURN_VALUE; } *errorCode = SUCCESS_CODE; return inst->IsVolumeUnadjustable(); } } } // namespace AudioStandard } // namespace OHOS