diff --git a/distributedaudio.gni b/distributedaudio.gni index 0d4c51c1715e22d41a5c6545d6d89770b4a51b4a..f4f64c2379d33a114c2adb6296974702f5ed4e01 100644 --- a/distributedaudio.gni +++ b/distributedaudio.gni @@ -11,11 +11,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +distributedaudio_ext_path = + "//foundation/distributedhardware/distributed_audio_ext" distributedaudio_path = "//foundation/distributedhardware/distributed_audio" -distributedhardwarefwk_path = "//foundation/distributedhardware/distributed_hardware_fwk" +distributedhardwarefwk_path = + "//foundation/distributedhardware/distributed_hardware_fwk" mediastandard_path = "//foundation/multimedia/player_framework" -mediastandardfwk_path = "//foundation/multimedia/audio_framework/interfaces/inner_api/native" -audio_framework_path = "//foundation/multimedia/audio_framework/frameworks/native" +mediastandardfwk_path = + "//foundation/multimedia/audio_framework/interfaces/inner_api/native" +audio_framework_path = + "//foundation/multimedia/audio_framework/frameworks/native" common_path = "${distributedaudio_path}/common" services_path = "${distributedaudio_path}/services" @@ -26,7 +31,8 @@ audio_processor_path = "${services_path}/audioprocessor" audio_transport_path = "${services_path}/audiotransport" softbusadapter_path = "${services_path}/softbusadapter" interfaces_path = "${distributedaudio_path}/interfaces" -hdf_interfaces_path = "${distributedaudio_path}/hdf_interfaces/distributed_audio" +hdf_interfaces_path = + "${distributedaudio_path}/hdf_interfaces/distributed_audio" hdf_service_path = "${distributedaudio_path}/hdf_service/distributed_audio" hdf_ser_aud_path = "${hdf_service_path}/hdi_service/audio/v1_0" hdf_ser_aud_ext_path = "${hdf_service_path}/hdi_service/audio_ext/v1_0" @@ -36,4 +42,8 @@ fwk_common_path = "${distributedhardwarefwk_path}/common" fwk_services_path = "${distributedhardwarefwk_path}/services" innerkits_path = "${distributedaudio_path}/interfaces/inner_kits" -build_flags = ["-Werror"] +build_flags = [ "-Werror" ] + +declare_args() { + distributed_audio_extension = false +} diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/BUILD.gn b/hdf_service/distributed_audio/hdi_service/audio/v1_0/BUILD.gn index 8db4eef5148e5990fd93babda0f4ed3a4e4f3779..6fbd906bb9006b8514f907243f898e5f6687d860 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/BUILD.gn +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/BUILD.gn @@ -23,6 +23,11 @@ ohos_shared_library("libaudio_manager_daudio_primary_service_1.0") { "${hdf_service_path}/hdi_service/common/utils/include", ] + if (distributed_audio_extension) { + include_dirs += + [ "${distributedaudio_ext_path}/hdf_service_extension/include/" ] + } + sources = [ "./src/audio_adapter_interface_impl.cpp", "./src/audio_capture_interface_impl.cpp", @@ -30,6 +35,13 @@ ohos_shared_library("libaudio_manager_daudio_primary_service_1.0") { "./src/audio_render_interface_impl.cpp", ] + if (distributed_audio_extension) { + sources += [ + "${distributedaudio_ext_path}/hdf_service_extension/src/audio_capture_ext_impl.cpp", + "${distributedaudio_ext_path}/hdf_service_extension/src/audio_render_ext_impl.cpp", + ] + } + public_deps = [ "${hdf_interfaces_path}/audio/v1_0:libdaudio_stub_1.0", "${hdf_interfaces_path}/audioext/v1_0:libdaudioext_stub_1.0", @@ -44,6 +56,10 @@ ohos_shared_library("libaudio_manager_daudio_primary_service_1.0") { "ipc:ipc_single", ] + if (distributed_audio_extension) { + cflags = [ "-DDAUDIO_SUPPORT_EXTENSION" ] + } + defines = [ "HI_LOG_ENABLE", "LOG_DOMAIN=0xD004100", diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_adapter_interface_impl.h b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_adapter_interface_impl.h index 294df6c3cb85d241bf3adad8acbbf0fed7bb369a..cee31f40caa666312a3b1f7d05a3bc906aa18865 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_adapter_interface_impl.h +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_adapter_interface_impl.h @@ -28,6 +28,10 @@ #include "audio_capture_interface_impl_base.h" #include "audio_render_interface_impl.h" #include "audio_render_interface_impl_base.h" +#ifdef DAUDIO_SUPPORT_EXTENSION +#include "audio_capture_ext_impl.h" +#include "audio_render_ext_impl.h" +#endif namespace OHOS { namespace HDI { @@ -155,14 +159,6 @@ private: const std::string NOT_MUTE_STATUS = "0"; const std::string IS_MUTE_STATUS = "1"; - - AudioCaptureInterfaceImplBase *(*GetCaptureImplExt_)() = nullptr; - AudioRenderInterfaceImplBase *(*GetRenderImplExt_)() = nullptr; -#ifdef __aarch64__ - char resolvedPath_[100] = "/system/lib64/libdaudio_ext_hdf_service.z.so"; -#else - char resolvedPath_[100] = "/system/lib/libdaudio_ext_hdf_service.z.so"; -#endif }; } // V1_0 } // Audio diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp index 7bbc5411dc005a512896f1e42daa2abbfdc233cc..25780ced9f29141c68b3aec1529c0e92540bc15b 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_adapter_interface_impl.cpp @@ -87,18 +87,14 @@ int32_t AudioAdapterInterfaceImpl::CreateRender(const AudioDeviceDescriptor &des } renderFlags_ = Audioext::V1_0::NORMAL_MODE; audioRender_ = new AudioRenderInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extSpkCallback_); - void *handle = dlopen(resolvedPath_, RTLD_LAZY); - if (attrs.type == AUDIO_MMAP_NOIRQ && handle != nullptr) { +#ifdef DAUDIO_SUPPORT_EXTENSION + if (attrs.type == AUDIO_MMAP_NOIRQ) { DHLOGI("Try to mmap mode."); - GetRenderImplExt_ = (AudioRenderInterfaceImplBase *(*)())(dlsym(handle, "GetRenderImplExt")); - if (GetRenderImplExt_ == nullptr) { - DHLOGE("Dlsym GetRenderImplExt error."); - return HDF_FAILURE; - } - audioRender_ = GetRenderImplExt_(); + audioRender_ = new AudioRenderExtImpl(); audioRender_->SetAttrs(adpDescriptor_.adapterName, desc, attrs, extSpkCallback_); renderFlags_ = Audioext::V1_0::MMAP_MODE; } +#endif if (audioRender_ == nullptr) { DHLOGE("Create render failed."); return HDF_FAILURE; @@ -151,18 +147,14 @@ int32_t AudioAdapterInterfaceImpl::CreateCapture(const AudioDeviceDescriptor &de } capturerFlags_ = Audioext::V1_0::NORMAL_MODE; audioCapture_ = new AudioCaptureInterfaceImpl(adpDescriptor_.adapterName, desc, attrs, extMicCallback_); - void *handle = dlopen(resolvedPath_, RTLD_LAZY); - if (attrs.type == AUDIO_MMAP_NOIRQ && handle != nullptr) { +#ifdef DAUDIO_SUPPORT_EXTENSION + if (attrs.type == AUDIO_MMAP_NOIRQ) { DHLOGI("Try to mmap mode."); - GetCaptureImplExt_ = (AudioCaptureInterfaceImplBase *(*)())(dlsym(handle, "GetCaptureImplExt")); - if (GetCaptureImplExt_ == nullptr) { - DHLOGE("Dlsym GetCaptureImplExt error."); - return HDF_FAILURE; - } - audioCapture_ = GetCaptureImplExt_(); + audioCapture_ = new AudioCaptureExtImpl(); audioCapture_->SetAttrs(adpDescriptor_.adapterName, desc, attrs, extMicCallback_); capturerFlags_ = Audioext::V1_0::MMAP_MODE; } +#endif if (audioCapture_ == nullptr) { DHLOGE("Create capture failed."); return HDF_FAILURE; diff --git a/hdf_service/distributed_audio/mfc_test/BUILD.gn b/hdf_service/distributed_audio/mfc_test/BUILD.gn deleted file mode 100644 index 6429b2d2ce2d9636141ddd82420c0d6607ba34ad..0000000000000000000000000000000000000000 --- a/hdf_service/distributed_audio/mfc_test/BUILD.gn +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright (c) 2022 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. - -import("//build/ohos.gni") -import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni") -import("//drivers/peripheral/adapter/base/hidl_adapter.gni") - -ohos_executable("audio_test_distributed_fifo") { - include_dirs = [ - "./include", - "//drivers/peripheral/audio/interfaces/include", - "./../../../services/hdfaudioclient/include", - "./../../../hdf_service/distributed_audio/hdi_service/common/include", - ] - - sources = [ "distributedaudiotest.cpp" ] - - deps = [ - "$hdf_uhdf_path/utils:libhdf_utils", - "./../../../services/hdfaudioclient:daudio_client", - ] - - external_deps = [ - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", - ] - - defines = [ - "HI_LOG_ENABLE", - "DH_LOG_TAG=\"daudioTest\"", - "LOG_DOMAIN=0xD004100", - ] - - install_enable = true - install_images = [ chipset_base_dir ] - subsystem_name = "distributedhardware" - part_name = "distributed_audio" -} diff --git a/hdf_service/distributed_audio/mfc_test/distributedaudiotest.cpp b/hdf_service/distributed_audio/mfc_test/distributedaudiotest.cpp deleted file mode 100644 index 742c5ab2b1054152d8ccf9738b1e2a588dda3a38..0000000000000000000000000000000000000000 --- a/hdf_service/distributed_audio/mfc_test/distributedaudiotest.cpp +++ /dev/null @@ -1,715 +0,0 @@ -/* - * Copyright (c) 2022-2023 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 -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include "unistd.h" -#include "distributedaudiotest.h" - -static const std::string SERVER_FIFO = "/data/seqnum_sv"; -static const std::string CLIENT_FIFO_TEMPLATE = "/data/seqnum_cl.%ld"; -const size_t CLIENT_FIFO_NAME_LEN = (sizeof(CLIENT_FIFO_TEMPLATE) + 20); - -struct Request { - pid_t pid; - int commandNum; - char reqString[70]; - int seqLen; -}; - -struct Response { - int seqNum; - char resString[CMD_EXECUTING_RETURN_LENGHT_MAX]; -}; - -using namespace OHOS::DistributedHardware; -static AudioManager *g_manager = nullptr; -static AudioAdapter *g_adapter = nullptr; -static AudioRender *g_render = nullptr; -static AudioCapture *g_capture = nullptr; -static AudioAdapterDescriptor *g_devices = nullptr; - -static constexpr const char* PLAY_THREAD = "playThread"; -static constexpr const char* CAPTURE_THREAD = "captureThread"; - -int32_t g_deviceNum = 0; -int32_t g_frameNum = 0; -int32_t g_frameIndex = 0; -int32_t g_micFrameNum = 0; -bool g_isInitRenderData = false; -static std::vector renderData; - -static DeviceStatus g_spkStatus = DEVICE_IDLE; -static DeviceStatus g_micStatus = DEVICE_IDLE; - -static std::thread g_palyingThread; -static std::thread g_capingThread; -FILE *g_micFile = nullptr; - -static std::string CloseSpk(); -static std::string CloseMic(); - -static int64_t GetNowTimeUs() -{ - std::chrono::microseconds nowUs = - std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); - return nowUs.count(); -} - -static int32_t InitTestDemo() -{ - std::cout << "**********************************************************************************" << std::endl; - std::cout << "Distributed Audio Test Demo Bin v1.3." << std::endl; - std::cout << "**********************************************************************************" << std::endl; - std::cout << std::endl; - std::cout << "Init distributed audio hdf service." << std::endl; - g_manager = GetAudioManagerFuncs(); - if (g_manager == nullptr) { - std::cout << "Distributed audio manager is null, quit!" << std::endl; - return ERR_DH_AUDIO_HDF_FAIL; - } - std::cout << "Load audio manager success." << std::endl; - return DH_SUCCESS; -} - -static std::string FindAudioDevice() -{ - if (g_manager == nullptr) { - return "false"; - } - int32_t ret = g_manager->GetAllAdapters(g_manager, &g_devices, &g_deviceNum); - if (ret != DH_SUCCESS) { - std::cout << "Get audio devices failed!"; - return "false"; - } - std::string res = "true;"; - std::cout << "Get audio devices success, adapter size: " << g_deviceNum << std::endl; - for (int32_t index = 0; index < g_deviceNum; index++) { - const AudioAdapterDescriptor &desc = g_devices[index]; - std::cout << "Device[" << index << "] ID: " << desc.adapterName << std::endl; - res = res + desc.adapterName; - if (index != g_deviceNum - 1) { - res = res + ";"; - } - std::cout << "pin list: "; - for (uint32_t i = 0; i < desc.portNum; i++) { - std::cout << desc.ports[i].portId << ", "; - } - std::cout << std::endl; - } - return res; -} - -static void HandleDevError(const char *condition, const char *value) -{ - if (condition[TYPE_OFFSET] == DEV_TYPE_SPK && g_spkStatus != DEVICE_IDLE) { - CloseSpk(); - } - - if (condition[TYPE_OFFSET] == DEV_TYPE_MIC && g_micStatus == DEVICE_IDLE) { - CloseMic(); - } - - std::cout << "Receive abnormal event, Demo quit." << std::endl; -} - -static int32_t ParamEventCallback(enum AudioExtParamKey key, const char *condition, const char *value, int8_t &reserved, - int8_t &cookie) -{ - std::string val(value); - std::string con(condition); - std::cout << std::endl; - std::cout << "**********************************************************************************" << std::endl; - std::cout << "Event recived: " << key << std::endl; - std::cout << "Condition: " << con << std::endl; - std::cout << "Value: " << val << std::endl; - std::cout << "**********************************************************************************" << std::endl; - std::cout << std::endl; - - if (key == AudioExtParamKey::AUDIO_EXT_PARAM_KEY_STATUS && con.rfind("ERR_EVENT", 0) == 0) { - HandleDevError(condition, value); - } - return DH_SUCCESS; -} - -static int32_t LoadSpkDev(const std::string &devId) -{ - std::cout << "Open SPK device , device Id:" << devId << std::endl; - - struct AudioAdapterDescriptor *dev = nullptr; - for (int32_t index = 0; index < g_deviceNum; index++) { - struct AudioAdapterDescriptor &desc = g_devices[index]; - if (desc.adapterName == devId) { - dev = &desc; - break; - } - } - if (dev == nullptr) { - std::cout << "Input device id is wrong." << std::endl; - FindAudioDevice(); - return ERR_DH_AUDIO_HDF_FAIL; - } - if (g_manager == nullptr) { - return ERR_DH_AUDIO_HDF_FAIL; - } - if (g_adapter == nullptr) { - int32_t ret = g_manager->LoadAdapter(g_manager, dev, &g_adapter); - if (ret != DH_SUCCESS || g_adapter == nullptr) { - std::cout << "Load audio device failed, ret: " << ret << std::endl; - return ERR_DH_AUDIO_HDF_FAIL; - } - } - std::cout << "Load audio device success." << std::endl; - return DH_SUCCESS; -} - -static std::string OpenSpk(const std::string &devId) -{ - if (g_spkStatus != DEVICE_IDLE) { - std::cout << "Speaker device is already opened." << std::endl; - return "true"; - } - if (LoadSpkDev(devId) != DH_SUCCESS) { - return "false"; - } - ParamCallback callback = ParamEventCallback; - int32_t ret = g_adapter->RegExtraParamObserver(g_adapter, callback, nullptr); - if (ret != DH_SUCCESS) { - std::cout << "Register observer failed, ret: " << ret << std::endl; - return "false"; - } - - struct AudioDeviceDescriptor renderDesc; - renderDesc.pins = AudioPortPin::PIN_OUT_SPEAKER; - renderDesc.desc = nullptr; - AudioSampleAttributes g_rattrs = {}; - g_rattrs.type = AUDIO_IN_MEDIA; - g_rattrs.interleaved = RENDER_INTER_LEAVED; - g_rattrs.streamId = RENDER_STREAM_ID; - g_rattrs.channelCount = RENDER_CHANNEL_MASK; - g_rattrs.sampleRate = AUDIO_SAMPLE_RATE; - g_rattrs.format = AudioFormat::AUDIO_FORMAT_TYPE_PCM_16_BIT; - ret = g_adapter->CreateRender(g_adapter, &renderDesc, &g_rattrs, &g_render); - if (ret != DH_SUCCESS || g_render == nullptr) { - std::cout << "Open SPK device failed, ret: " << ret << std::endl; - return "false"; - } - g_spkStatus = DEVICE_OPEN; - std::cout << "Open SPK device success." << std::endl; - return "true"; -} - -static void WriteStreamWait(const int64_t &startTime) -{ - int64_t endTime = GetNowTimeUs(); - int64_t passTime = endTime - startTime; - - if (passTime > AUDIO_FRAME_TIME_INTERFAL_DEFAULT) { - return; - } - int64_t remainTime = AUDIO_FRAME_TIME_INTERFAL_DEFAULT - passTime; - std::this_thread::sleep_for(std::chrono::microseconds(remainTime)); -} - -static void Play() -{ - if (g_render == nullptr) { - std::cout << "SPK device is null." << std::endl; - return; - } - if (pthread_setname_np(pthread_self(), PLAY_THREAD) != DH_SUCCESS) { - std::cout << "Play thread setname failed." << std::endl; - } - std::cout << "Playing thread started." << std::endl; - g_render->control.Start((AudioHandle)g_render); - g_spkStatus = DEVICE_START; - - uint64_t size = 0; - while (g_spkStatus == DEVICE_START) { - int64_t startTime = GetNowTimeUs(); - int32_t ret = g_render->RenderFrame(g_render, renderData[g_frameIndex], RENDER_FRAME_SIZE, &size); - if (ret != DH_SUCCESS) { - std::cout<<"RenderFrame failed, index: "<< g_frameIndex << ", ret: " << ret << std::endl; - } - g_frameIndex++; - if (g_frameNum != 0 && g_frameIndex == g_frameNum) { - g_frameIndex = 0; - } - WriteStreamWait(startTime); - } - std::cout << "Playing thread stopped." << std::endl; -} - -static std::string StartRender() -{ - if (g_spkStatus == DEVICE_IDLE) { - return "Speaker device is not opened, start render failed."; - } - - if (g_spkStatus == DEVICE_OPEN) { - WavHdr wavHeader; - size_t headerSize = sizeof(WavHdr); - if (!g_isInitRenderData) { - struct stat statbuf; - stat(SPK_FILE_PATH, &statbuf); - int32_t size = statbuf.st_size; - g_frameNum = (size - headerSize) / RENDER_FRAME_SIZE; - std::cout << "Audio file frame num: " << g_frameNum << std::endl; - for (int32_t j = 0; j < g_frameNum; j++) { - uint8_t *frame = new uint8_t[RENDER_FRAME_SIZE](); - renderData.push_back(frame); - } - g_isInitRenderData = true; - } - FILE *wavFile = fopen(SPK_FILE_PATH, "rb"); - fread(&wavHeader, 1, headerSize, wavFile); - for (int32_t i = 0; i < g_frameNum; i++) { - fread(renderData[i], 1, RENDER_FRAME_SIZE, wavFile); - } - fclose(wavFile); - g_frameIndex = 0; - g_palyingThread = std::thread(Play); - return "true"; - } - if (g_spkStatus == DEVICE_START) { - return "Speaker device is started."; - } - if (g_spkStatus == DEVICE_STOP) { - g_palyingThread = std::thread(Play); - } - return "true"; -} - -static std::string StopRender() -{ - if (g_render == nullptr) { - return "SPK device is null."; - } - - if (g_spkStatus == DEVICE_IDLE) { - return "Speaker device is not opened."; - } - - if (g_spkStatus == DEVICE_OPEN) { - return "Speaker device is not started."; - } - - if (g_spkStatus == DEVICE_STOP) { - return "Speaker device is already stoped."; - } - - g_spkStatus = DEVICE_STOP; - if (g_palyingThread.joinable()) { - g_palyingThread.join(); - } - g_render->control.Stop((AudioHandle)g_render); - return "true"; -} - -static std::string CloseSpk() -{ - if (g_spkStatus == DEVICE_IDLE) { - return "Speaker device is not opened."; - } - - if (g_spkStatus == DEVICE_START) { - StopRender(); - } - - int32_t ret = g_adapter->DestroyRender(g_adapter, g_render); - if (ret != DH_SUCCESS) { - return "Close speaker failed"; - } - if (g_micStatus == DEVICE_IDLE) { - g_manager->UnloadAdapter(g_manager, g_adapter); - g_adapter = nullptr; - } - g_spkStatus = DEVICE_IDLE; - - if (g_isInitRenderData) { - for (auto &p : renderData) { - delete[] p; - } - renderData.clear(); - g_isInitRenderData = false; - } - return "true"; -} - -static int32_t LoadMicDev(const std::string &devId) -{ - std::cout << "Open MIC device ,input device Id:" << devId << std::endl; - - struct AudioAdapterDescriptor *dev = nullptr; - for (int32_t index = 0; index < g_deviceNum; index++) { - struct AudioAdapterDescriptor &desc = g_devices[index]; - if (desc.adapterName == devId) { - dev = &desc; - break; - } - } - if (dev == nullptr) { - std::cout << "Input device id is wrong." << std::endl; - FindAudioDevice(); - return ERR_DH_AUDIO_HDF_FAIL; - } - if (g_manager == nullptr) { - return ERR_DH_AUDIO_HDF_FAIL; - } - if (g_adapter == nullptr) { - int32_t ret = g_manager->LoadAdapter(g_manager, dev, &g_adapter); - if (ret != DH_SUCCESS || g_adapter == nullptr) { - std::cout << "Load audio device failed, ret: " << ret << std::endl; - return ERR_DH_AUDIO_HDF_FAIL; - } - } - std::cout << "Load audio device success." << std::endl; - return DH_SUCCESS; -} - -static std::string OpenMic(const std::string &devId) -{ - if (g_micStatus != DEVICE_IDLE) { - return "Mic device is already opened."; - } - if (LoadMicDev(devId) != DH_SUCCESS) { - return "Load audio device failed."; - } - - AudioDeviceDescriptor captureDesc; - captureDesc.pins = AudioPortPin::PIN_IN_MIC; - captureDesc.desc = nullptr; - AudioSampleAttributes captureAttr; - captureAttr.type = AUDIO_IN_MEDIA; - captureAttr.interleaved = CAPTURE_INTER_LEAVED; - captureAttr.streamId = CAPTURE_STREAM_ID; - captureAttr.channelCount = CAPTURE_CHANNEL_MASK; - captureAttr.sampleRate = AUDIO_SAMPLE_RATE; - captureAttr.format = AudioFormat::AUDIO_FORMAT_TYPE_PCM_16_BIT; - int32_t ret = g_adapter->CreateCapture(g_adapter, &captureDesc, &captureAttr, &g_capture); - if (ret != DH_SUCCESS || g_capture == nullptr) { - return "Open MIC device failed."; - } - g_micStatus = DEVICE_OPEN; - return "true"; -} - -static void ReadStreamWait(const int64_t &startTime) -{ - int64_t endTime = GetNowTimeUs(); - int32_t passTime = endTime - startTime; - - if (passTime > AUDIO_FRAME_TIME_INTERFAL_DEFAULT) { - return; - } - int64_t remainTime = AUDIO_FRAME_TIME_INTERFAL_DEFAULT - passTime; - std::this_thread::sleep_for(std::chrono::microseconds(remainTime)); -} - -static void Capture() -{ - if (g_capture == nullptr) { - std::cout << "MIC device is null." << std::endl; - return; - } - if (pthread_setname_np(pthread_self(), CAPTURE_THREAD) != DH_SUCCESS) { - std::cout << "Capture thread setname failed." << std::endl; - } - std::cout << "Capturing thread started." << std::endl; - g_capture->control.Start((AudioHandle)g_capture); - g_micStatus = DEVICE_START; - - uint64_t size = 0; - while (g_micStatus == DEVICE_START) { - uint8_t *data[RENDER_FRAME_SIZE]; - int64_t startTime = GetNowTimeUs(); - int32_t ret = g_capture->CaptureFrame(g_capture, data, RENDER_FRAME_SIZE, &size); - if (ret != DH_SUCCESS) { - std::cout << "CaptureFrame failed, ret: " << ret << std::endl; - return; - } - fwrite(data, 1, RENDER_FRAME_SIZE, g_micFile); - g_micFrameNum++; - ReadStreamWait(startTime); - } - std::cout << "Capturing thread stopped." << std::endl; -} - -static std::string StartCapture() -{ - if (g_micStatus == DEVICE_IDLE) { - return "Mic device is not opened, start capture failed."; - } - - if (g_micStatus == DEVICE_OPEN) { - g_micFile = fopen(MIC_FILE_PATH, "ab+"); - if (g_micFile == nullptr) { - return "Open pcm file failed."; - } - g_capingThread = std::thread(Capture); - return "true"; - } - - if (g_micStatus == DEVICE_START) { - return "Mic device is already started."; - } - - if (g_micStatus == DEVICE_STOP) { - g_capingThread = std::thread(Capture); - } - return "true"; -} - -static std::string StopCapture() -{ - if (g_capture == nullptr) { - return "MIC device is null."; - } - - if (g_micStatus == DEVICE_IDLE) { - return "Mic device is not opened."; - } - - if (g_micStatus == DEVICE_OPEN) { - return "Mic device is not started."; - } - - if (g_micStatus == DEVICE_STOP) { - return "Mic device is already started."; - } - - g_micStatus = DEVICE_STOP; - if (g_capingThread.joinable()) { - g_capingThread.join(); - } - g_capture->control.Stop((AudioHandle)g_capture); - return "true"; -} - -static std::string CloseMic() -{ - if (g_micStatus == DEVICE_IDLE) { - return "Mic device is not opened."; - } - - if (g_micStatus == DEVICE_START) { - StopCapture(); - } - - int32_t ret = g_adapter->DestroyCapture(g_adapter, g_capture); - if (ret != DH_SUCCESS) { - return "Close mic failed."; - } - if (g_spkStatus == DEVICE_IDLE) { - g_manager->UnloadAdapter(g_manager, g_adapter); - g_adapter = nullptr; - } - if (g_micFile != nullptr) { - fclose(g_micFile); - g_micFile = nullptr; - } - g_micStatus = DEVICE_IDLE; - return "true"; -} - -static std::string SetVolume(std::string vol) -{ - if (g_spkStatus == DEVICE_IDLE) { - return "Speaker is not opened, can not set volume."; - } - int32_t volInt = std::stoi(vol); - if (volInt < VOLUME_MIN || volInt > VOLUME_MAX) { - return "Volume is invalid"; - } - enum AudioExtParamKey key = AudioExtParamKey::AUDIO_EXT_PARAM_KEY_VOLUME; - std::string condition = "EVENT_TYPE=1;VOLUME_GROUP_ID=1;AUDIO_VOLUME_TYPE=1;"; - int32_t ret = g_adapter->SetExtraParams(g_adapter, key, condition.c_str(), vol.c_str()); - if (ret != DH_SUCCESS) { - return "Set volume failed"; - } - return "true"; -} - -static std::string GetVolume() -{ - if (g_spkStatus == DEVICE_IDLE) { - return "Speaker is not opened, can not get volume."; - } - enum AudioExtParamKey key = AudioExtParamKey::AUDIO_EXT_PARAM_KEY_VOLUME; - std::string condition = "EVENT_TYPE=1;VOLUME_GROUP_ID=1;AUDIO_VOLUME_TYPE=1;"; - char vol[VOLUME_BIT]; - int32_t ret = g_adapter->GetExtraParams(g_adapter, key, condition.c_str(), vol, VOLUME_BIT); - if (ret != DH_SUCCESS) { - return "Get Volume failed."; - } - std::string volStr(vol); - return "true;"+volStr; -} - -static std::string SpkMicPlayEvent(const std::string &cmd) -{ - if (cmd == CMD_START_SPK || cmd == CMD_START_SPK_EXT) { - return StartRender(); - } - if (cmd == CMD_STOP_SPK || cmd == CMD_STOP_SPK_EXT) { - return StopRender(); - } - if (cmd == CMD_CLOSE_SPK || cmd == CMD_CLOSE_SPK_EXT) { - return CloseSpk(); - } - - if (cmd == CMD_START_MIC || cmd == CMD_START_MIC_EXT) { - return StartCapture(); - } - if (cmd == CMD_STOP_MIC || cmd == CMD_STOP_MIC_EXT) { - return StopCapture(); - } - if (cmd == CMD_CLOSE_MIC || cmd == CMD_CLOSE_MIC_EXT) { - return CloseMic(); - } - return ""; -} - -static void HandleAudioEvent(const std::string& cmd, std::string& cmdResString, struct Request& req) -{ - // find audio device - if (cmd == CMD_FIND || cmd == CMD_FIND_EXT) { - cmdResString = FindAudioDevice(); - return; - } - // Speaker open、play、pause and close - if (cmd == CMD_OPEN_SPK || cmd == CMD_OPEN_SPK_EXT) { - std::string reqdevId(req.reqString); - cmdResString = OpenSpk(reqdevId); - return; - } - cmdResString = SpkMicPlayEvent(cmd); - - // Mic open、record、pause and close - if (cmd == CMD_OPEN_MIC || cmd == CMD_OPEN_MIC_EXT) { - std::string reqdevId(req.reqString); - cmdResString = OpenMic(reqdevId); - return; - } - cmdResString = SpkMicPlayEvent(cmd); - - // set Speaker volume - if (cmd == CMD_SET_VOL || cmd == CMD_SET_VOL_EXT) { - std::string vol(req.reqString); - cmdResString = SetVolume(vol); - return; - } - // get Speaker volume - if (cmd == CMD_GET_VOL || cmd == CMD_GET_VOL_EXT) { - cmdResString = GetVolume(); - } -} - -static int GenerateFifoName(int& clientFd, char clientFifo[], struct Response& resp, - std::string& cmdResString, struct Request& req) -{ - if (snprintf_s(clientFifo, CLIENT_FIFO_NAME_LEN, CLIENT_FIFO_NAME_LEN, - CLIENT_FIFO_TEMPLATE.c_str(), static_cast(req.pid)) < 0) { - return -1; - } - clientFd = open(clientFifo, O_WRONLY); - if (clientFd == -1) { - perror("open"); - return -1; - } - if (strcpy_s(resp.resString, sizeof(resp.resString), cmdResString.c_str()) != 0) { - return -1; - } - - std::cout << "Write resString: " << resp.resString << std::endl; - int writeLen = write(clientFd, &resp, sizeof(struct Response)); - std::cout << "Write Len: " << writeLen << "sizeof Response: " << sizeof(struct Response) << std::endl; - if (writeLen != sizeof(struct Response)) { - std::cout << "Write Len: " << writeLen << "sizeof Response: " << sizeof(struct Response) << std::endl; - } - if (close(clientFd) == -1) { - perror("close"); - } - - return 0; -} - -int main(int argc, char *argv[]) -{ - int serverFd, dummyFd, clientFd, seqNum = 0; - char clientFifo[CLIENT_FIFO_NAME_LEN]; - struct Request req; - struct Response resp; - - if (InitTestDemo() != DH_SUCCESS) { - return ERR_DH_AUDIO_HDF_FAIL; - } - umask(0); - if (mkfifo(SERVER_FIFO.c_str(), S_IRUSR | S_IWUSR | S_IWGRP) == -1 && errno != EEXIST) { - perror("mkfifo"); - } - serverFd = open(SERVER_FIFO.c_str(), O_RDONLY); - if (serverFd == -1) { - perror("open"); - } - dummyFd = open(SERVER_FIFO.c_str(), O_WRONLY); - if (dummyFd == -1) { - perror("open"); - } - if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { - perror("signal"); - } - bool testRunning = true; - std::string cmd = ""; - while (testRunning) { - int reLen = read(serverFd, &req, sizeof(struct Request)); - if (reLen != sizeof(struct Request)) { - std::cout << "Read Len: " << reLen << "sizeof Request: " << sizeof(struct Request) << std::endl; - continue; - } - - std::cout << req.reqString << std::endl; - std::cout << "Read Len: " << reLen << "sizeof Request: " << sizeof(struct Request) << std::endl; - printf("receive msg from client pid:%d, commandNum: %d\n", req.pid, req.commandNum); - cmd = std::to_string(req.commandNum); - std::string cmdResString = ""; - - // quit_test_demo - if (cmd == CMD_QUIT || cmd == CMD_QUIT_EXT) { - CloseSpk(); - CloseMic(); - testRunning = false; - continue; - } - HandleAudioEvent(cmd, cmdResString, req); - if (GenerateFifoName(clientFd, clientFifo, resp, cmdResString, req) != 0) { - continue; - } - - resp.seqNum = seqNum; - seqNum += req.seqLen; /* Update our sequence number */ - } -} diff --git a/hdf_service/distributed_audio/mfc_test/distributedaudiotest.h b/hdf_service/distributed_audio/mfc_test/distributedaudiotest.h deleted file mode 100644 index ab10feea7ba01a398c49fd06d183b37faadefd79..0000000000000000000000000000000000000000 --- a/hdf_service/distributed_audio/mfc_test/distributedaudiotest.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2022 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. - */ - -#ifndef DISTRIBUTED_AUDIO_TEST_H -#define DISTRIBUTED_AUDIO_TEST_H - -#include -#include -#include -#include -#include -#include -#include - -#include "audio_adapter.h" -#include "audio_manager.h" -#include "audio_types.h" -#include "daudio_errcode.h" - -const std::string CMD_QUIT = "quit"; -const std::string CMD_FIND = "find"; -const std::string CMD_OPEN_SPK = "openspk"; -const std::string CMD_CLOSE_SPK = "closespk"; -const std::string CMD_START_SPK = "startspk"; -const std::string CMD_STOP_SPK = "stopspk"; -const std::string CMD_OPEN_MIC = "openmic"; -const std::string CMD_CLOSE_MIC = "closemic"; -const std::string CMD_START_MIC = "startmic"; -const std::string CMD_STOP_MIC = "stopmic"; -const std::string CMD_SET_VOL = "setvol"; -const std::string CMD_GET_VOL = "getvol"; - -const std::string CMD_QUIT_EXT = "0"; -const std::string CMD_FIND_EXT = "9"; -const std::string CMD_OPEN_SPK_EXT = "1"; -const std::string CMD_CLOSE_SPK_EXT = "2"; -const std::string CMD_START_SPK_EXT = "3"; -const std::string CMD_STOP_SPK_EXT = "4"; -const std::string CMD_OPEN_MIC_EXT = "5"; -const std::string CMD_CLOSE_MIC_EXT = "6"; -const std::string CMD_START_MIC_EXT = "7"; -const std::string CMD_STOP_MIC_EXT = "8"; -const std::string CMD_SET_VOL_EXT = "11"; -const std::string CMD_GET_VOL_EXT = "12"; - -const char DEV_TYPE_SPK = '1'; -const char DEV_TYPE_MIC = '2'; -const char SPK_FILE_PATH[128] = "/data/test.wav"; -const char MIC_FILE_PATH[128] = "/data/mic.pcm"; -constexpr int32_t TYPE_OFFSET = 12; -constexpr int32_t AUDIO_SAMPLE_RATE = 48000; -constexpr int32_t VOLUME_MIN = 0; -constexpr int32_t VOLUME_MAX = 15; -constexpr int32_t VOLUME_BIT = 3; -constexpr int32_t RENDER_FRAME_SIZE = 4096; -constexpr int32_t RENDER_INTER_LEAVED = 1; -constexpr int32_t RENDER_STREAM_ID = 0; -constexpr int32_t RENDER_CHANNEL_MASK = 2; -constexpr int32_t CAPTURE_INTER_LEAVED = 1; -constexpr int32_t CAPTURE_STREAM_ID = 2; -constexpr int32_t CAPTURE_CHANNEL_MASK = 2; -constexpr int32_t MILLISECOND_PER_SECOND = 1000; -constexpr int64_t AUDIO_FRAME_TIME_INTERFAL_DEFAULT = 21333; -constexpr int32_t CMD_EXECUTING_RETURN_LENGHT_MAX = 500; - -typedef enum { - DEVICE_IDLE = 0, - DEVICE_OPEN = 1, - DEVICE_START = 2, - DEVICE_STOP = 3, -} DeviceStatus; - -struct WAV_HEADER { - /* RIFF Chunk Descriptor */ - uint8_t riff[4] = {'R', 'I', 'F', 'F'}; - uint32_t chunkSize = 0; - uint8_t wave[4] = {'W', 'A', 'V', 'E'}; - /* "fmt" sub-chunk */ - uint8_t fmt[4] = {'f', 'm', 't', ' '}; - uint32_t subchunk1Size = 16; - uint16_t audioFormat = 1; - uint16_t numOfChan = 2; - uint32_t samplesPerSec = 44100; - uint32_t bytesPerSec = 176400; - uint16_t blockAlign = 2; - uint16_t bitsPerSample = 16; - /* "data" sub-chunk */ - uint8_t subchunk2ID[4] = {'d', 'a', 't', 'a'}; - uint32_t subchunk2Size = 0; -}; -using WavHdr = struct WAV_HEADER; - -#endif \ No newline at end of file diff --git a/services/audiomanager/managersink/include/daudio_sink_dev.h b/services/audiomanager/managersink/include/daudio_sink_dev.h index 10ad859c2e3afd11d46e8e1dd1a4d2345f640d49..8e5d07efa396afb8fcfd7ce822cae8dfa34b6db0 100644 --- a/services/audiomanager/managersink/include/daudio_sink_dev.h +++ b/services/audiomanager/managersink/include/daudio_sink_dev.h @@ -29,6 +29,10 @@ #include "imic_client.h" #include "ispk_client.h" #include "iaudio_event_callback.h" +#ifdef DDAUDIO_SUPPORT_DIRECT +#include "direct_dmic_client.h" +#include "direct_dspeaker_client.h" +#endif using json = nlohmann::json; @@ -96,14 +100,6 @@ private: using DAudioSinkDevFunc = int32_t (DAudioSinkDev::*)(const AudioEvent &audioEvent); std::map memberFuncMap_; - - ISpkClient *(*GetDirectSpkClient_)() = nullptr; - IMicClient *(*GetDirectMicClient_)() = nullptr; -#ifdef __aarch64__ - char resolvedPath_[100] = "/system/lib64/libdistributed_direct_client.z.so"; -#else - char resolvedPath_[100] = "/system/lib/libdistributed_direct_client.z.so"; -#endif }; } // DistributedHardware } // OHOS diff --git a/services/audiomanager/managersink/src/daudio_sink_dev.cpp b/services/audiomanager/managersink/src/daudio_sink_dev.cpp index 14aefed6ec0528f0e6914d8fcdca58fdee9a082b..566e508b079a87f5a782c29cc02bc9ce1a8b12d3 100644 --- a/services/audiomanager/managersink/src/daudio_sink_dev.cpp +++ b/services/audiomanager/managersink/src/daudio_sink_dev.cpp @@ -394,17 +394,13 @@ int32_t DAudioSinkDev::TaskOpenDSpeaker(const std::string &args) if (speakerClient_ == nullptr) { speakerClient_ = std::make_shared(devId_, shared_from_this()); } - void *handle = dlopen(resolvedPath_, RTLD_LAZY); - if (audioParam.renderOpts.renderFlags == MMAP_MODE && handle != nullptr) { +#ifdef DDAUDIO_SUPPORT_DIRECT + if (audioParam.renderOpts.renderFlags == MMAP_MODE) { DHLOGI("Try to mmap mode."); - GetDirectSpkClient_ = (ISpkClient *(*)())(dlsym(handle, "GetDirectSpkClient")); - if (GetDirectSpkClient_ == nullptr) { - DHLOGE("Dlsym GetDirectSpkClient error."); - return ERR_DH_AUDIO_FAILED; - } - speakerClient_ = std::shared_ptr(GetDirectSpkClient_()); + speakerClient_ = std::shared_ptr(); speakerClient_->SetAttrs(devId_, shared_from_this()); } +#endif ret = speakerClient_->SetUp(audioParam); if (ret != DH_SUCCESS) { DHLOGE("Setup speaker failed, ret: %d.", ret); @@ -468,17 +464,13 @@ int32_t DAudioSinkDev::TaskOpenDMic(const std::string &args) if (micClient_ == nullptr) { micClient_ = std::make_shared(devId_, shared_from_this()); } - void *handle = dlopen(resolvedPath_, RTLD_LAZY); - if (audioParam.captureOpts.capturerFlags == MMAP_MODE && handle != nullptr) { +#ifdef DDAUDIO_SUPPORT_DIRECT + if (audioParam.captureOpts.capturerFlags == MMAP_MODE) { DHLOGI("Try to mmap mode."); - GetDirectMicClient_ = (IMicClient *(*)())(dlsym(handle, "GetDirectMicClient")); - if (GetDirectMicClient_ == nullptr) { - DHLOGE("Dlsym GetDirectMicClient error."); - return ERR_DH_AUDIO_FAILED; - } - micClient_ = std::shared_ptr(GetDirectMicClient_()); + micClient_ = std::make_shared(); micClient_->SetAttrs(devId_, shared_from_this()); } +#endif ret = micClient_->SetUp(audioParam); if (ret != DH_SUCCESS) { DHLOGE("Set up mic failed, ret: %d.", ret); diff --git a/services/audiomanager/servicesink/BUILD.gn b/services/audiomanager/servicesink/BUILD.gn index 3a6f127afdcc1c2b210639235dc4a91c082d66d5..912a3cd23bf848cee485c6a4f1df04a5fa479670 100755 --- a/services/audiomanager/servicesink/BUILD.gn +++ b/services/audiomanager/servicesink/BUILD.gn @@ -53,6 +53,13 @@ ohos_shared_library("distributed_audio_sink") { "${softbusadapter_path}/include", ] + if (distributed_audio_extension) { + include_dirs += [ + "${distributedaudio_ext_path}/services/audioclient/directmicclient/include", + "${distributedaudio_ext_path}/services/audioclient/directspkclient/include", + ] + } + sources = [ "${audio_control_path}/controlsink/src/daudio_sink_dev_ctrl_manager.cpp", "${innerkits_path}/native_cpp/audio_sink/src/daudio_sink_handler.cpp", @@ -65,6 +72,13 @@ ohos_shared_library("distributed_audio_sink") { "src/daudio_sink_stub.cpp", ] + if (distributed_audio_extension) { + sources += [ + "${distributedaudio_ext_path}/services/audioclient/directmicclient/src/direct_dmic_client.cpp", + "${distributedaudio_ext_path}/services/audioclient/directspkclient/src/direct_dspeaker_client.cpp", + ] + } + deps = [ "${audio_transport_path}/audioctrltransport:distributed_audio_control", "${audio_transport_path}/decodetransport:distributed_audio_decode_transport", @@ -88,6 +102,10 @@ ohos_shared_library("distributed_audio_sink") { "samgr:samgr_proxy", ] + if (distributed_audio_extension) { + cflags = [ "-DDAUDIO_SUPPORT_DIRECT" ] + } + defines = [ "HI_LOG_ENABLE", "LOG_DOMAIN=0xD004100",