From fcb5b935a80f3ec47fe9d05f74b45b90ea265bcf Mon Sep 17 00:00:00 2001 From: w30042960 Date: Wed, 23 Aug 2023 20:34:20 +0800 Subject: [PATCH] add hidumper Signed-off-by: w30042960 --- common/dfx_utils/include/daudio_hidumper.h | 4 + common/dfx_utils/src/daudio_hidumper.cpp | 22 +++- common/include/daudio_util.h | 2 + common/src/daudio_util.cpp | 11 ++ .../include/audio_capture_interface_impl.h | 3 + .../audio_capture_interface_impl_base.h | 1 + .../include/audio_render_interface_impl.h | 3 + .../audio_render_interface_impl_base.h | 1 + .../v1_0/src/audio_adapter_interface_impl.cpp | 13 +- .../v1_0/src/audio_capture_interface_impl.cpp | 9 +- .../v1_0/src/audio_render_interface_impl.cpp | 8 ++ .../common/include/daudio_events.h | 2 + .../common/utils/include/daudio_utils.h | 3 + .../common/utils/src/daudio_utils.cpp | 10 ++ .../micclient/include/dmic_client.h | 1 + .../audioclient/micclient/src/dmic_client.cpp | 4 + .../spkclient/include/dspeaker_client.h | 1 + .../spkclient/src/dspeaker_client.cpp | 4 + .../include/daudio_hdi_handler.h | 2 + .../audiohdiproxy/src/daudio_hdi_handler.cpp | 29 ++-- .../managersource/include/dmic_dev.h | 2 + .../managersource/include/dspeaker_dev.h | 2 + .../managersource/src/dmic_dev.cpp | 10 ++ .../managersource/src/dspeaker_dev.cpp | 10 ++ services/audiomanager/servicesink/BUILD.gn | 1 + .../include/daudio_sink_hidumper.h | 52 ++++++++ .../servicesink/include/daudio_sink_service.h | 2 + .../servicesink/src/daudio_sink_hidumper.cpp | 124 ++++++++++++++++++ .../servicesink/src/daudio_sink_service.cpp | 23 ++++ services/common/audioparam/audio_event.h | 4 + 30 files changed, 351 insertions(+), 12 deletions(-) create mode 100644 services/audiomanager/servicesink/include/daudio_sink_hidumper.h create mode 100644 services/audiomanager/servicesink/src/daudio_sink_hidumper.cpp diff --git a/common/dfx_utils/include/daudio_hidumper.h b/common/dfx_utils/include/daudio_hidumper.h index 49236b9d..b10c8df4 100644 --- a/common/dfx_utils/include/daudio_hidumper.h +++ b/common/dfx_utils/include/daudio_hidumper.h @@ -35,12 +35,14 @@ enum class HidumpFlag { GET_SOURCE_DEVID, GET_SINK_INFO, GET_ABILITY, + DUMP_AUDIO_DATA, }; class DaudioHidumper { DECLARE_SINGLE_INSTANCE_BASE(DaudioHidumper); public: bool Dump(const std::vector &args, std::string &result); + bool GetFlagStatus(); DaudioHidumper(); ~DaudioHidumper(); @@ -52,11 +54,13 @@ private: int32_t GetSourceDevId(std::string &result); int32_t GetSinkInfo(std::string &result); int32_t GetAbilityInfo(std::string &result); + int32_t DumpAudioData(std::string &result); private: std::string g_sourceDevId_ = ""; AudioManager *g_manager = nullptr; AudioAdapterDescriptor *g_devices = nullptr; + bool HidumperFlag_ = false; int32_t g_deviceNum = 0; std::string spkDefault = "1"; std::string micDefault = "134217729"; diff --git a/common/dfx_utils/src/daudio_hidumper.cpp b/common/dfx_utils/src/daudio_hidumper.cpp index 424ff9d2..07be7f80 100644 --- a/common/dfx_utils/src/daudio_hidumper.cpp +++ b/common/dfx_utils/src/daudio_hidumper.cpp @@ -31,12 +31,14 @@ const std::string ARGS_HELP = "-h"; const std::string ARGS_SOURCE_DEVID = "--sourceDevId"; const std::string ARGS_SINK_INFO = "--sinkInfo"; const std::string ARGS_ABILITY = "--ability"; +const std::string ARGS_DUMP_AUDIO_DATA = "--dumpAudioData"; const std::map ARGS_MAP = { { ARGS_HELP, HidumpFlag::GET_HELP }, { ARGS_SOURCE_DEVID, HidumpFlag::GET_SOURCE_DEVID }, { ARGS_SINK_INFO, HidumpFlag::GET_SINK_INFO }, { ARGS_ABILITY, HidumpFlag::GET_ABILITY }, + { ARGS_DUMP_AUDIO_DATA, HidumpFlag::DUMP_AUDIO_DATA }, }; } @@ -94,6 +96,9 @@ int32_t DaudioHidumper::ProcessDump(const std::string &args, std::string &result case HidumpFlag::GET_ABILITY: { return GetAbilityInfo(result); } + case HidumpFlag::DUMP_AUDIO_DATA: { + return DumpAudioData(result); + } default: { return ShowIllegalInfomation(result); } @@ -151,6 +156,19 @@ int32_t DaudioHidumper::GetAbilityInfo(std::string &result) return DH_SUCCESS; } +int32_t DaudioHidumper::DumpAudioData(std::string &result) +{ + DHLOGI("Dump audio data."); + result.append("dump..."); + HidumperFlag_ = true; + return DH_SUCCESS; +} + +bool DaudioHidumper::GetFlagStatus() +{ + return HidumperFlag_; +} + void DaudioHidumper::ShowHelp(std::string &result) { DHLOGI("Show help."); @@ -163,7 +181,9 @@ void DaudioHidumper::ShowHelp(std::string &result) .append("--sinkInfo ") .append(": dump sink info in the system\n") .append("--ability ") - .append(": dump current ability of the audio in the system\n"); + .append(": dump current ability of the audio in the system\n") + .append("--dumpAudioData") + .append(": dump audio data in the system\n"); } int32_t DaudioHidumper::ShowIllegalInfomation(std::string &result) diff --git a/common/include/daudio_util.h b/common/include/daudio_util.h index abb1f60e..33d43922 100644 --- a/common/include/daudio_util.h +++ b/common/include/daudio_util.h @@ -17,6 +17,7 @@ #define OHOS_DAUDIO_UTIL_H #include +#include #include #include "nlohmann/json.hpp" @@ -48,6 +49,7 @@ void GetCurrentTime(int64_t &tvSec, int64_t &tvNSec); bool CheckIsNum(const std::string &jsonString); bool CheckDevIdIsLegal(const std::string &devId); bool IsOutDurationRange(int64_t startTime, int64_t endTime, int64_t lastStartTime); +void SaveFile(std::string fileName, uint8_t *audioData, int32_t size); template bool GetSysPara(const char *key, T &value); diff --git a/common/src/daudio_util.cpp b/common/src/daudio_util.cpp index 471704c8..cd89c020 100644 --- a/common/src/daudio_util.cpp +++ b/common/src/daudio_util.cpp @@ -365,5 +365,16 @@ bool IsParamEnabled(const std::string &key, bool &isEnabled) isEnabled = false; return false; } + +void SaveFile(std::string fileName, uint8_t *audioData, int32_t size) +{ + std::ofstream ofs(fileName, std::ios::binary | std::ios::out | std::ios::app); + if (!ofs.is_open()) { + DHLOGE("open file failed"); + return; + } + ofs.write((const char*)(audioData), size); + ofs.close(); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl.h b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl.h index 6e087be6..cd9e25ba 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl.h +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl.h @@ -75,12 +75,15 @@ public: const AudioDeviceDescriptor &GetCaptureDesc() override; void SetAttrs(const std::string &adpName, const AudioDeviceDescriptor &desc, const AudioSampleAttributes &attrs, const sptr &callback) override; + void SetDumpFlagInner() override; private: static constexpr int64_t AUDIO_OFFSET_FRAME_NUM = 10; + const std::string FILE_NAME = "/data/hdf_captureframe.pcm"; std::string adapterName_; AudioDeviceDescriptor devDesc_; AudioSampleAttributes devAttrs_; + bool dumpFlag_ = false; uint32_t timeInterval_ = 5; int64_t frameIndex_ = 0; int64_t framePeriodNs_ = 0; diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl_base.h b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl_base.h index 4e3c4912..8cb3cb3c 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl_base.h +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_capture_interface_impl_base.h @@ -45,6 +45,7 @@ public: virtual const AudioDeviceDescriptor &GetCaptureDesc() = 0; virtual void SetAttrs(const std::string &adpName, const AudioDeviceDescriptor &desc, const AudioSampleAttributes &attrs, const sptr &callback) = 0; + virtual void SetDumpFlagInner() = 0; }; } // V1_0 } // Audio diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl.h b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl.h index 3cfa5899..ff2d7743 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl.h +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl.h @@ -93,6 +93,7 @@ public: uint32_t GetMinVolumeInner() override; void SetAttrs(const std::string &adpName, const AudioDeviceDescriptor &desc, const AudioSampleAttributes &attrs, const sptr &callback) override; + void SetDumpFlagInner() override; private: float GetFadeRate(uint32_t currentIndex, const uint32_t durationIndex); @@ -100,6 +101,7 @@ private: private: static constexpr int64_t AUDIO_OFFSET_FRAME_NUM = 10; + const std::string FILE_NAME = "/data/hdf_renderframe.pcm"; std::string adapterName_; AudioDeviceDescriptor devDesc_; @@ -108,6 +110,7 @@ private: std::mutex renderMtx_; std::mutex volMtx_; bool firstOpenFlag_ = true; + bool dumpFlag_ = false; uint32_t timeInterval_ = 5; uint32_t currentFrame_ = 0; uint32_t vol_ = 0; diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl_base.h b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl_base.h index 5929d137..3675951b 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl_base.h +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/include/audio_render_interface_impl_base.h @@ -53,6 +53,7 @@ public: virtual uint32_t GetMinVolumeInner() = 0; virtual void SetAttrs(const std::string &adpName, const AudioDeviceDescriptor &desc, const AudioSampleAttributes &attrs, const sptr &callback) = 0; + virtual void SetDumpFlagInner() = 0; }; } // 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 6fdb2734..e5583255 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 @@ -47,7 +47,6 @@ AudioAdapterInterfaceImpl::~AudioAdapterInterfaceImpl() DHLOGD("Distributed audio adapter destructed, name(%s).", GetAnonyString(adpDescriptor_.adapterName).c_str()); } - void AudioAdapterInterfaceImpl::SetSpeakerCallback(const sptr &spkCallback) { if (spkCallback == nullptr) { @@ -373,6 +372,8 @@ int32_t AudioAdapterInterfaceImpl::Notify(const uint32_t devId, const DAudioEven case HDF_AUDIO_EVENT_CLOSE_SPK_RESULT: case HDF_AUDIO_EVENT_OPEN_MIC_RESULT: case HDF_AUDIO_EVENT_CLOSE_MIC_RESULT: + case HDF_AUDIO_EVENT_SPK_DUMP: + case HDF_AUDIO_EVENT_MIC_DUMP: return HandleSANotifyEvent(event); case HDF_AUDIO_EVENT_SPK_CLOSED: case HDF_AUDIO_EVENT_MIC_CLOSED: @@ -771,6 +772,16 @@ int32_t AudioAdapterInterfaceImpl::HandleSANotifyEvent(const DAudioEvent &event) micNotifyFlag_ = true; micWaitCond_.notify_all(); break; + case HDF_AUDIO_EVENT_SPK_DUMP: + if (audioRender_ != nullptr) { + audioRender_->SetDumpFlagInner(); + } + break; + case HDF_AUDIO_EVENT_MIC_DUMP: + if (audioCapture_ != nullptr) { + audioCapture_->SetDumpFlagInner(); + } + break; default: DHLOGE("Notify not support event type %d, event content: %s.", event.type, event.content.c_str()); return ERR_DH_AUDIO_HDF_FAIL; diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_capture_interface_impl.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_capture_interface_impl.cpp index 8188c279..083282ca 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_capture_interface_impl.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_capture_interface_impl.cpp @@ -84,7 +84,9 @@ int32_t AudioCaptureInterfaceImpl::CaptureFrame(std::vector &frame, uint DHLOGE("Read stream data failed."); return HDF_FAILURE; } - + if (dumpFlag_) { + SaveFile(FILE_NAME, reinterpret_cast(audioData.data.data()), audioData.data.size()); + } frame.resize(devAttrs_.frameSize); ret = memcpy_s(frame.data(), frame.size(), audioData.data.data(), audioData.data.size()); if (ret != EOK) { @@ -334,6 +336,11 @@ void AudioCaptureInterfaceImpl::SetAttrs(const std::string &adpName, const Audio DHLOGI("Set attrs, not support yet."); } +void AudioCaptureInterfaceImpl::SetDumpFlagInner() +{ + dumpFlag_ = true; +} + const AudioDeviceDescriptor &AudioCaptureInterfaceImpl::GetCaptureDesc() { return devDesc_; diff --git a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_render_interface_impl.cpp b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_render_interface_impl.cpp index 13f297ae..7b1b9f9f 100644 --- a/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_render_interface_impl.cpp +++ b/hdf_service/distributed_audio/hdi_service/audio/v1_0/src/audio_render_interface_impl.cpp @@ -101,6 +101,9 @@ int32_t AudioRenderInterfaceImpl::RenderFrame(const std::vector &frame, AudioParameter param = { devAttrs_.format, devAttrs_.channelCount, devAttrs_.sampleRate, 0, devAttrs_.frameSize}; AudioData data = { param, frame }; + if (dumpFlag_) { + SaveFile(FILE_NAME, reinterpret_cast(data.data.data()), frame.size()); + } FadeInProcess(DURATION_FRAMES, data.data.data(), frame.size()); if (audioExtCallback_ == nullptr) { DHLOGE("Callback is nullptr."); @@ -443,6 +446,11 @@ void AudioRenderInterfaceImpl::SetAttrs(const std::string &adpName, const AudioD { DHLOGI("Set attrs, not support yet."); } + +void AudioRenderInterfaceImpl::SetDumpFlagInner() +{ + dumpFlag_ = true; +} } // V1_0 } // Audio } // Distributedaudio diff --git a/hdf_service/distributed_audio/hdi_service/common/include/daudio_events.h b/hdf_service/distributed_audio/hdi_service/common/include/daudio_events.h index b8ef0121..c4355bfe 100644 --- a/hdf_service/distributed_audio/hdi_service/common/include/daudio_events.h +++ b/hdf_service/distributed_audio/hdi_service/common/include/daudio_events.h @@ -65,6 +65,8 @@ typedef enum AudioExtParamEvent { HDF_AUDIO_EVENT_MMAP_STOP_MIC = 17, HDF_AUDIO_EVENT_START = 18, HDF_AUDIO_EVENT_STOP = 19, + HDF_AUDIO_EVENT_SPK_DUMP = 20, + HDF_AUDIO_EVENT_MIC_DUMP = 21, } EXT_PARAM_EVENT; typedef enum AudioVolumeEvent { diff --git a/hdf_service/distributed_audio/hdi_service/common/utils/include/daudio_utils.h b/hdf_service/distributed_audio/hdi_service/common/utils/include/daudio_utils.h index 5caefc5d..0bde1ba6 100644 --- a/hdf_service/distributed_audio/hdi_service/common/utils/include/daudio_utils.h +++ b/hdf_service/distributed_audio/hdi_service/common/utils/include/daudio_utils.h @@ -16,6 +16,7 @@ #ifndef OHOS_DAUDIO_UTILS_H #define OHOS_DAUDIO_UTILS_H +#include #include #define AUDIO_MS_PER_SECOND 1000 @@ -53,6 +54,8 @@ int64_t CalculateOffset(const int64_t frameindex, const int64_t framePeriodNs, c int64_t UpdateTimeOffset(const int64_t frameIndex, const int64_t framePeriodNs, int64_t &startTime); bool IsOutDurationRange(int64_t startTime, int64_t endTime, int64_t lastStartTime); + +void SaveFile(std::string fileName, uint8_t *audioData, int32_t size); } // DistributedHardware } // OHOS #endif \ No newline at end of file diff --git a/hdf_service/distributed_audio/hdi_service/common/utils/src/daudio_utils.cpp b/hdf_service/distributed_audio/hdi_service/common/utils/src/daudio_utils.cpp index c83a95d6..02211a6b 100644 --- a/hdf_service/distributed_audio/hdi_service/common/utils/src/daudio_utils.cpp +++ b/hdf_service/distributed_audio/hdi_service/common/utils/src/daudio_utils.cpp @@ -175,5 +175,15 @@ bool IsOutDurationRange(int64_t startTime, int64_t endTime, int64_t lastStartTim int64_t twiceInterval = startTime - lastStartTime; return (currentInterval > MAX_TIME_INTERVAL_US || twiceInterval > MAX_TIME_INTERVAL_US) ? true : false; } + +void SaveFile(std::string fileName, uint8_t *audioData, int32_t size) +{ + std::ofstream ofs(fileName, std::ios::binary | std::ios::out | std::ios::app); + if (!ofs.is_open()) { + return; + } + ofs.write((const char*)(audioData), size); + ofs.close(); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/audioclient/micclient/include/dmic_client.h b/services/audioclient/micclient/include/dmic_client.h index d3a0dd0c..d205e268 100644 --- a/services/audioclient/micclient/include/dmic_client.h +++ b/services/audioclient/micclient/include/dmic_client.h @@ -69,6 +69,7 @@ private: private: constexpr static uint8_t CHANNEL_WAIT_SECONDS = 5; static constexpr const char* CAPTURETHREAD = "captureThread"; + const std::string FILE_NAME = "/data/sink_mic_send.pcm"; std::string devId_; std::thread captureDataThread_; diff --git a/services/audioclient/micclient/src/dmic_client.cpp b/services/audioclient/micclient/src/dmic_client.cpp index 87bfc54c..bfb86718 100644 --- a/services/audioclient/micclient/src/dmic_client.cpp +++ b/services/audioclient/micclient/src/dmic_client.cpp @@ -19,6 +19,7 @@ #include "daudio_constants.h" #include "daudio_hisysevent.h" +#include "daudio_sink_hidumper.h" #include "daudio_sink_manager.h" #undef DH_LOG_TAG @@ -252,6 +253,9 @@ void DMicClient::CaptureThreadRunning() DHLOGE("Bytes read failed."); break; } + if (DaudioSinkHidumper::GetInstance().GetFlagStatus()) { + SaveFile(FILE_NAME, const_cast(audioData->Data()), audioData->Size()); + } int64_t startTransTime = GetNowTimeUs(); int32_t ret = micTrans_->FeedAudioData(audioData); if (ret != DH_SUCCESS) { diff --git a/services/audioclient/spkclient/include/dspeaker_client.h b/services/audioclient/spkclient/include/dspeaker_client.h index 1f6efbd0..f38be3cb 100644 --- a/services/audioclient/spkclient/include/dspeaker_client.h +++ b/services/audioclient/spkclient/include/dspeaker_client.h @@ -93,6 +93,7 @@ private: constexpr static size_t DATA_QUEUE_SIZE = 8; constexpr static size_t SLEEP_TIME = 5000; static constexpr const char* RENDERTHREAD = "renderThread"; + const std::string FILE_NAME = "/data/sink_spk_recv.pcm"; std::string devId_; std::thread renderDataThread_; diff --git a/services/audioclient/spkclient/src/dspeaker_client.cpp b/services/audioclient/spkclient/src/dspeaker_client.cpp index 9ae280e6..9d7fa5b2 100644 --- a/services/audioclient/spkclient/src/dspeaker_client.cpp +++ b/services/audioclient/spkclient/src/dspeaker_client.cpp @@ -17,6 +17,7 @@ #include "daudio_constants.h" #include "daudio_hisysevent.h" +#include "daudio_sink_hidumper.h" #include "daudio_util.h" #include "daudio_sink_manager.h" @@ -290,6 +291,9 @@ void DSpeakerClient::PlayThreadRunning() dataQueue_.pop(); DHLOGD("Pop spk data, dataqueue size: %d.", dataQueue_.size()); } + if (DaudioSinkHidumper::GetInstance().GetFlagStatus()) { + SaveFile(FILE_NAME, const_cast(audioData->Data()), audioData->Size()); + } int32_t writeOffSet = 0; while (writeOffSet < static_cast(audioData->Capacity())) { int32_t writeLen = audioRenderer_->Write(audioData->Data() + writeOffSet, diff --git a/services/audiohdiproxy/include/daudio_hdi_handler.h b/services/audiohdiproxy/include/daudio_hdi_handler.h index 8a004e60..d5d15ac2 100644 --- a/services/audiohdiproxy/include/daudio_hdi_handler.h +++ b/services/audiohdiproxy/include/daudio_hdi_handler.h @@ -30,6 +30,7 @@ namespace OHOS { namespace DistributedHardware { +using OHOS::HDI::DistributedAudio::Audioext::V1_0::DAudioEvent; using OHOS::HDI::DistributedAudio::Audioext::V1_0::IDAudioCallback; using OHOS::HDI::DistributedAudio::Audioext::V1_0::IDAudioManager; class DAudioHdiHandler { @@ -50,6 +51,7 @@ public: private: DAudioHdiHandler(); ~DAudioHdiHandler(); + void ProcessEventMsg(const AudioEvent &audioEvent, DAudioEvent &newEvent); const std::string HDF_AUDIO_SERVICE_NAME = "daudio_ext_service"; std::mutex devMapMtx_; diff --git a/services/audiohdiproxy/src/daudio_hdi_handler.cpp b/services/audiohdiproxy/src/daudio_hdi_handler.cpp index 8c07edc9..ba5f08d9 100644 --- a/services/audiohdiproxy/src/daudio_hdi_handler.cpp +++ b/services/audiohdiproxy/src/daudio_hdi_handler.cpp @@ -162,16 +162,8 @@ int32_t DAudioHdiHandler::UnRegisterAudioDevice(const std::string &devId, const return DH_SUCCESS; } -int32_t DAudioHdiHandler::NotifyEvent(const std::string &devId, const int32_t dhId, - const AudioEvent &audioEvent) +void DAudioHdiHandler::ProcessEventMsg(const AudioEvent &audioEvent, DAudioEvent &newEvent) { - DHLOGD("Notify event adpname: %s, dhId: %d, event type: %d, event content: %s.", - GetAnonyString(devId).c_str(), dhId, audioEvent.type, audioEvent.content.c_str()); - if (audioSrvHdf_ == nullptr) { - DHLOGE("Audio hdi proxy not init"); - return ERR_DH_AUDIO_HDI_PROXY_NOT_INIT; - } - OHOS::HDI::DistributedAudio::Audioext::V1_0::DAudioEvent newEvent = {AUDIO_EVENT_UNKNOWN, audioEvent.content}; switch (audioEvent.type) { case AudioEventType::NOTIFY_OPEN_SPEAKER_RESULT: newEvent.type = AUDIO_EVENT_OPEN_SPK_RESULT; @@ -200,10 +192,29 @@ int32_t DAudioHdiHandler::NotifyEvent(const std::string &devId, const int32_t dh case AudioEventType::AUDIO_RENDER_STATE_CHANGE: newEvent.type = AUDIO_EVENT_RENDER_STATE_CHANGE; break; + case AudioEventType::NOTIFY_HDF_SPK_DUMP: + newEvent.type = AUDIO_EVENT_SPK_DUMP; + break; + case AudioEventType::NOTIFY_HDF_MIC_DUMP: + newEvent.type = AUDIO_EVENT_MIC_DUMP; + break; default: DHLOGE("Unsupport audio event."); break; } +} + +int32_t DAudioHdiHandler::NotifyEvent(const std::string &devId, const int32_t dhId, + const AudioEvent &audioEvent) +{ + DHLOGD("Notify event adpname: %s, dhId: %d, event type: %d, event content: %s.", + GetAnonyString(devId).c_str(), dhId, audioEvent.type, audioEvent.content.c_str()); + if (audioSrvHdf_ == nullptr) { + DHLOGE("Audio hdi proxy not init"); + return ERR_DH_AUDIO_HDI_PROXY_NOT_INIT; + } + DAudioEvent newEvent = {AUDIO_EVENT_UNKNOWN, audioEvent.content}; + ProcessEventMsg(audioEvent, newEvent); int32_t res = audioSrvHdf_->NotifyEvent(devId, dhId, newEvent); if (res != HDF_SUCCESS) { diff --git a/services/audiomanager/managersource/include/dmic_dev.h b/services/audiomanager/managersource/include/dmic_dev.h index 254d1c7a..68d49948 100644 --- a/services/audiomanager/managersource/include/dmic_dev.h +++ b/services/audiomanager/managersource/include/dmic_dev.h @@ -91,6 +91,7 @@ private: static constexpr size_t LOW_LATENCY_DATA_QUEUE_HALF_SIZE = 10; static constexpr uint32_t MMAP_WAIT_FRAME_US = 5000; static constexpr const char* ENQUEUE_THREAD = "micEnqueueTh"; + const std::string FILE_NAME = "/data/source_mic_read.pcm"; std::string devId_; std::weak_ptr audioEventCallback_; @@ -100,6 +101,7 @@ private: int32_t curPort_ = 0; std::atomic isTransReady_ = false; std::atomic isOpened_ = false; + std::atomic dumpFlag_ = false; std::shared_ptr micTrans_ = nullptr; std::queue> dataQueue_; std::set enabledPorts_; diff --git a/services/audiomanager/managersource/include/dspeaker_dev.h b/services/audiomanager/managersource/include/dspeaker_dev.h index 2c5472b9..0b2f8a5e 100644 --- a/services/audiomanager/managersource/include/dspeaker_dev.h +++ b/services/audiomanager/managersource/include/dspeaker_dev.h @@ -84,6 +84,7 @@ private: private: static constexpr const char* ENQUEUE_THREAD = "spkEnqueueTh"; + const std::string FILE_NAME = "/data/source_spk_write.pcm"; std::string devId_; std::weak_ptr audioEventCallback_; @@ -91,6 +92,7 @@ private: std::condition_variable channelWaitCond_; std::atomic isTransReady_ = false; std::atomic isOpened_ = false; + std::atomic dumpFlag_ = false; int32_t curPort_ = 0; std::shared_ptr speakerTrans_ = nullptr; std::set enabledPorts_; diff --git a/services/audiomanager/managersource/src/dmic_dev.cpp b/services/audiomanager/managersource/src/dmic_dev.cpp index 01fa37e6..2cf8d093 100644 --- a/services/audiomanager/managersource/src/dmic_dev.cpp +++ b/services/audiomanager/managersource/src/dmic_dev.cpp @@ -23,6 +23,7 @@ #include "audio_decode_transport.h" #include "daudio_constants.h" #include "daudio_errorcode.h" +#include "daudio_hidumper.h" #include "daudio_hisysevent.h" #include "daudio_hitrace.h" #include "daudio_log.h" @@ -302,6 +303,7 @@ int32_t DMicDev::Release() DHLOGE("Release mic trans failed, ret: %d.", ret); return ret; } + dumpFlag_.store(false); return DH_SUCCESS; } @@ -341,6 +343,14 @@ int32_t DMicDev::ReadStreamData(const std::string &devId, const int32_t dhId, st data = dataQueue_.front(); dataQueue_.pop(); } + if (DaudioHidumper::GetInstance().GetFlagStatus()) { + if (!dumpFlag_) { + AudioEvent event(NOTIFY_HDF_MIC_DUMP, ""); + NotifyHdfAudioEvent(event); + dumpFlag_.store(true); + } + SaveFile(FILE_NAME, const_cast(data->Data()), data->Size()); + } int64_t endTime = GetNowTimeUs(); if (IsOutDurationRange(startTime, endTime, lastReadStartTime_)) { DHLOGE("This time read data spend: %lld, The interval of read data this time and the last time: %lld", diff --git a/services/audiomanager/managersource/src/dspeaker_dev.cpp b/services/audiomanager/managersource/src/dspeaker_dev.cpp index 9c540ed2..479e5b08 100644 --- a/services/audiomanager/managersource/src/dspeaker_dev.cpp +++ b/services/audiomanager/managersource/src/dspeaker_dev.cpp @@ -25,6 +25,7 @@ #include "audio_encode_transport.h" #include "daudio_constants.h" #include "daudio_errorcode.h" +#include "daudio_hidumper.h" #include "daudio_hisysevent.h" #include "daudio_hitrace.h" #include "daudio_log.h" @@ -285,6 +286,7 @@ int32_t DSpeakerDev::Release() if (ret != DH_SUCCESS) { DHLOGE("Release speaker trans failed, ret: %d.", ret); } + dumpFlag_.store(false); return DH_SUCCESS; } @@ -344,6 +346,14 @@ int32_t DSpeakerDev::WriteStreamData(const std::string &devId, const int32_t dhI DHLOGE("Write stream data, speaker trans is null."); return ERR_DH_AUDIO_SA_SPEAKER_TRANS_NULL; } + if (DaudioHidumper::GetInstance().GetFlagStatus()) { + if (!dumpFlag_) { + AudioEvent event(NOTIFY_HDF_SPK_DUMP, ""); + NotifyHdfAudioEvent(event); + dumpFlag_.store(true); + } + SaveFile(FILE_NAME, const_cast(data->Data()), data->Size()); + } int32_t ret = speakerTrans_->FeedAudioData(data); if (ret != DH_SUCCESS) { DHLOGE("Write stream data failed, ret: %d.", ret); diff --git a/services/audiomanager/servicesink/BUILD.gn b/services/audiomanager/servicesink/BUILD.gn index 2a605183..37fb9543 100755 --- a/services/audiomanager/servicesink/BUILD.gn +++ b/services/audiomanager/servicesink/BUILD.gn @@ -70,6 +70,7 @@ ohos_shared_library("distributed_audio_sink") { "${innerkits_path}/native_cpp/audio_source/src/daudio_source_proxy.cpp", "${services_path}/audiomanager/managersink/src/daudio_sink_dev.cpp", "${services_path}/audiomanager/managersink/src/daudio_sink_manager.cpp", + "src/daudio_sink_hidumper.cpp", "src/daudio_sink_service.cpp", "src/daudio_sink_stub.cpp", ] diff --git a/services/audiomanager/servicesink/include/daudio_sink_hidumper.h b/services/audiomanager/servicesink/include/daudio_sink_hidumper.h new file mode 100644 index 00000000..24cee047 --- /dev/null +++ b/services/audiomanager/servicesink/include/daudio_sink_hidumper.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 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. + */ + +#ifndef OHOS_DISTRIBUTED_AUDIO_SINK_HIDUMPER_H +#define OHOS_DISTRIBUTED_AUDIO_SINK_HIDUMPER_H + +#include +#include + +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +enum class HidumpFlag { + UNKNOWN = 0, + GET_HELP, + DUMP_SINK_AUDIO_DATA, +}; +class DaudioSinkHidumper { + DECLARE_SINGLE_INSTANCE_BASE(DaudioSinkHidumper); + +public: + bool Dump(const std::vector &args, std::string &result); + bool GetFlagStatus(); + DaudioSinkHidumper(); + ~DaudioSinkHidumper(); + +private: + void ShowHelp(std::string &result); + int32_t ShowIllegalInfomation(std::string &result); + int32_t ProcessDump(const std::string &args, std::string &result); + + int32_t DumpAudioData(std::string &result); + +private: + bool HidumperFlag_ = false; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_AUDIO_SINK_HIDUMPER_H \ No newline at end of file diff --git a/services/audiomanager/servicesink/include/daudio_sink_service.h b/services/audiomanager/servicesink/include/daudio_sink_service.h index b20cc9a1..633d3934 100644 --- a/services/audiomanager/servicesink/include/daudio_sink_service.h +++ b/services/audiomanager/servicesink/include/daudio_sink_service.h @@ -19,6 +19,7 @@ #include "system_ability.h" #include "ipc_object_stub.h" +#include "daudio_sink_hidumper.h" #include "daudio_sink_stub.h" namespace OHOS { @@ -36,6 +37,7 @@ public: int32_t UnsubscribeLocalHardware(const std::string &dhId) override; void DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, const std::string &eventContent) override; + int Dump(int32_t fd, const std::vector& args) override; protected: void OnStart() override; diff --git a/services/audiomanager/servicesink/src/daudio_sink_hidumper.cpp b/services/audiomanager/servicesink/src/daudio_sink_hidumper.cpp new file mode 100644 index 00000000..41fdfdee --- /dev/null +++ b/services/audiomanager/servicesink/src/daudio_sink_hidumper.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 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 "daudio_sink_hidumper.h" + +#include "daudio_errorcode.h" +#include "daudio_log.h" +#include "daudio_util.h" + +#undef DH_LOG_TAG +#define DH_LOG_TAG "DaudioSinkHidumper" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DaudioSinkHidumper); + +namespace { +const std::string ARGS_HELP = "-h"; +const std::string ARGS_DUMP_SINK_AUDIO_DATA = "--dumpSinkAudioData"; + +const std::map ARGS_MAP = { + { ARGS_HELP, HidumpFlag::GET_HELP }, + { ARGS_DUMP_SINK_AUDIO_DATA, HidumpFlag::DUMP_SINK_AUDIO_DATA }, +}; +} + +DaudioSinkHidumper::DaudioSinkHidumper() +{ + DHLOGI("Distributed audio hidumper constructed."); +} + +DaudioSinkHidumper::~DaudioSinkHidumper() +{ + DHLOGI("Distributed audio hidumper deconstructed."); +} + +bool DaudioSinkHidumper::Dump(const std::vector &args, std::string &result) +{ + DHLOGI("Distributed audio hidumper dump args.size():%d.", args.size()); + result.clear(); + int32_t argsSize = static_cast(args.size()); + for (int32_t i = 0; i < argsSize; i++) { + DHLOGI("Distributed audio hidumper dump args[%d]: %s.", i, args.at(i).c_str()); + } + + if (args.empty()) { + ShowHelp(result); + return true; + } else if (args.size() > 1) { + ShowIllegalInfomation(result); + return true; + } + + return ProcessDump(args[0], result) == DH_SUCCESS; +} + +int32_t DaudioSinkHidumper::ProcessDump(const std::string &args, std::string &result) +{ + DHLOGI("Process dump."); + HidumpFlag hf = HidumpFlag::UNKNOWN; + auto operatorIter = ARGS_MAP.find(args); + if (operatorIter != ARGS_MAP.end()) { + hf = operatorIter->second; + } + + if (hf == HidumpFlag::GET_HELP) { + ShowHelp(result); + return DH_SUCCESS; + } + result.clear(); + switch (hf) { + case HidumpFlag::DUMP_SINK_AUDIO_DATA: { + return DumpAudioData(result); + } + default: { + return ShowIllegalInfomation(result); + } + } +} + +int32_t DaudioSinkHidumper::DumpAudioData(std::string &result) +{ + DHLOGI("Dump audio data."); + result.append("Dump..."); + HidumperFlag_ = true; + return DH_SUCCESS; +} + +bool DaudioSinkHidumper::GetFlagStatus() +{ + return HidumperFlag_; +} + +void DaudioSinkHidumper::ShowHelp(std::string &result) +{ + DHLOGI("Show help."); + result.append("Usage:dump [options]\n") + .append("Description:\n") + .append("-h ") + .append(": show help\n") + .append("--dumpSinkAudioData ") + .append(": dump sink audio data\n"); +} + +int32_t DaudioSinkHidumper::ShowIllegalInfomation(std::string &result) +{ + DHLOGI("Show illegal information."); + result.append("unknown command, -h for help."); + return DH_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/audiomanager/servicesink/src/daudio_sink_service.cpp b/services/audiomanager/servicesink/src/daudio_sink_service.cpp index fe17dae7..dac62574 100644 --- a/services/audiomanager/servicesink/src/daudio_sink_service.cpp +++ b/services/audiomanager/servicesink/src/daudio_sink_service.cpp @@ -116,5 +116,28 @@ void DAudioSinkService::DAudioNotify(const std::string &devId, const std::string dhId.c_str(), eventType); DAudioSinkManager::GetInstance().HandleDAudioNotify(devId, dhId, eventType, eventContent); } + +int DAudioSinkService::Dump(int32_t fd, const std::vector &args) +{ + DHLOGD("Distributed audio sink service dump."); + std::string result; + std::vector argsStr; + + std::transform(args.cbegin(), args.cend(), std::back_inserter(argsStr), + [](const std::u16string& item) { return Str16ToStr8(item); }); + + if (!DaudioSinkHidumper::GetInstance().Dump(argsStr, result)) { + DHLOGE("Hidump error"); + return ERR_DH_AUDIO_BAD_VALUE; + } + + int ret = dprintf(fd, "%s\n", result.c_str()); + if (ret < 0) { + DHLOGE("Dprintf error"); + return ERR_DH_AUDIO_BAD_VALUE; + } + + return DH_SUCCESS; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/common/audioparam/audio_event.h b/services/common/audioparam/audio_event.h index 3a6178e8..5660ff40 100644 --- a/services/common/audioparam/audio_event.h +++ b/services/common/audioparam/audio_event.h @@ -37,6 +37,8 @@ typedef enum { SPEAKER_CLOSED = 14, NOTIFY_OPEN_SPEAKER_RESULT = 15, NOTIFY_CLOSE_SPEAKER_RESULT = 16, + NOTIFY_HDF_SPK_DUMP = 17, + NOTIFY_HDF_MIC_DUMP = 18, OPEN_MIC = 21, CLOSE_MIC = 22, @@ -92,6 +94,8 @@ typedef enum { AUDIO_EVENT_MMAP_STOP_MIC = 17, AUDIO_EVENT_START = 18, AUDIO_EVENT_STOP = 19, + AUDIO_EVENT_SPK_DUMP = 20, + AUDIO_EVENT_MIC_DUMP = 21, } AudioEventHDF; class AudioEvent { public: -- Gitee