From a81d51d70f6f3728f7eea465a3ee34f662ecfcaf Mon Sep 17 00:00:00 2001 From: w30042960 Date: Tue, 26 Aug 2025 14:05:33 +0800 Subject: [PATCH 1/2] add timestamp Signed-off-by: w30042960 --- common/include/daudio_ipc_interface_code.h | 1 + .../include/daudio_source_handler.h | 2 + .../include/daudio_source_proxy.h | 2 + .../audio_source/include/idaudio_source.h | 2 + .../src/daudio_source_handler.cpp | 13 ++ .../audio_source/src/daudio_source_proxy.cpp | 21 +++ .../include/daudio_ipc_common.h | 6 + .../src/daudio_source_handler_test.cpp | 27 +++ .../src/daudio_source_proxy_test.cpp | 23 +++ .../micclient/include/dmic_client.h | 10 ++ .../audioclient/micclient/src/dmic_client.cpp | 20 +++ .../micclient/src/dmic_client_test.cpp | 13 ++ .../managersource/include/daudio_io_dev.h | 9 + .../managersource/include/daudio_source_dev.h | 1 + .../include/daudio_source_manager.h | 1 + .../managersource/include/dmic_dev.h | 39 +++- .../managersource/include/dspeaker_dev.h | 2 + .../managersource/src/daudio_source_dev.cpp | 14 ++ .../src/daudio_source_manager.cpp | 20 +++ .../managersource/src/dmic_dev.cpp | 170 ++++++++++++++++-- .../managersource/src/dspeaker_dev.cpp | 9 + .../include/daudio_source_service.h | 2 + .../include/daudio_source_stub.h | 1 + .../src/daudio_source_service.cpp | 14 ++ .../servicesource/src/daudio_source_stub.cpp | 23 +++ .../src/daudio_source_dev_test.cpp | 2 +- .../sourcedevice/src/dmic_dev_test.cpp | 85 ++++++++- .../src/daudio_source_mgr_test.cpp | 15 ++ .../src/av_receiver_engine_transport.cpp | 2 + .../src/av_sender_engine_adapter.cpp | 1 + .../common/audiodata/include/audio_data.h | 6 + services/common/audiodata/src/audio_data.cpp | 20 +++ 32 files changed, 548 insertions(+), 28 deletions(-) diff --git a/common/include/daudio_ipc_interface_code.h b/common/include/daudio_ipc_interface_code.h index a18c0683..b37acfac 100644 --- a/common/include/daudio_ipc_interface_code.h +++ b/common/include/daudio_ipc_interface_code.h @@ -28,6 +28,7 @@ enum class IDAudioSourceInterfaceCode : uint32_t { UNREGISTER_DISTRIBUTED_HARDWARE = 3, CONFIG_DISTRIBUTED_HARDWARE = 4, DAUDIO_NOTIFY = 5, + UPDATE_WORKMODE = 6, }; /* SAID: 4806 */ diff --git a/interfaces/inner_kits/native_cpp/audio_source/include/daudio_source_handler.h b/interfaces/inner_kits/native_cpp/audio_source/include/daudio_source_handler.h index ca187db3..771608b6 100644 --- a/interfaces/inner_kits/native_cpp/audio_source/include/daudio_source_handler.h +++ b/interfaces/inner_kits/native_cpp/audio_source/include/daudio_source_handler.h @@ -43,6 +43,8 @@ public: int32_t UnLoadDistributedHDF() override; void OnRemoteSourceSvrDied(const wptr &remote); void FinishStartSA(const std::string ¶m, const sptr &remoteObject); + int32_t UpdateDistributedHardwareWorkMode(const std::string &networkId, const std::string &dhId, + const WorkModeParam ¶m) override; private: class DAudioSourceSvrRecipient : public IRemoteObject::DeathRecipient { diff --git a/interfaces/inner_kits/native_cpp/audio_source/include/daudio_source_proxy.h b/interfaces/inner_kits/native_cpp/audio_source/include/daudio_source_proxy.h index 4cb89acb..531c15d4 100644 --- a/interfaces/inner_kits/native_cpp/audio_source/include/daudio_source_proxy.h +++ b/interfaces/inner_kits/native_cpp/audio_source/include/daudio_source_proxy.h @@ -37,6 +37,8 @@ public: const std::string &value) override; void DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, const std::string &eventContent) override; + int32_t UpdateDistributedHardwareWorkMode(const std::string &networkId, const std::string &dhId, + const WorkModeParam ¶m) override; private: static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_kits/native_cpp/audio_source/include/idaudio_source.h b/interfaces/inner_kits/native_cpp/audio_source/include/idaudio_source.h index 33f65844..c3c6def4 100644 --- a/interfaces/inner_kits/native_cpp/audio_source/include/idaudio_source.h +++ b/interfaces/inner_kits/native_cpp/audio_source/include/idaudio_source.h @@ -37,6 +37,8 @@ public: const std::string &value) = 0; virtual void DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, const std::string &eventContent) = 0; + virtual int32_t UpdateDistributedHardwareWorkMode(const std::string &devId, const std::string &dhId, + const WorkModeParam ¶m) = 0; }; } // DistributedHardware } // OHOS diff --git a/interfaces/inner_kits/native_cpp/audio_source/src/daudio_source_handler.cpp b/interfaces/inner_kits/native_cpp/audio_source/src/daudio_source_handler.cpp index f5a1b94f..c1b22c74 100644 --- a/interfaces/inner_kits/native_cpp/audio_source/src/daudio_source_handler.cpp +++ b/interfaces/inner_kits/native_cpp/audio_source/src/daudio_source_handler.cpp @@ -224,6 +224,19 @@ void DAudioSourceHandler::DAudioSourceSvrRecipient::OnRemoteDied(const wptr lock(sourceProxyMutex_); + CHECK_NULL_RETURN(dAudioSourceProxy_, ERR_DH_AUDIO_SA_PROXY_NOT_INIT); + if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN) { + return ERR_DH_AUDIO_SA_DEVID_ILLEGAL; + } + return dAudioSourceProxy_->UpdateDistributedHardwareWorkMode(devId, dhId, param); +} + IDistributedHardwareSource *GetSourceHardwareHandler() { DHLOGI("Get source hardware handler."); diff --git a/interfaces/inner_kits/native_cpp/audio_source/src/daudio_source_proxy.cpp b/interfaces/inner_kits/native_cpp/audio_source/src/daudio_source_proxy.cpp index 77fb57c8..1617bf45 100644 --- a/interfaces/inner_kits/native_cpp/audio_source/src/daudio_source_proxy.cpp +++ b/interfaces/inner_kits/native_cpp/audio_source/src/daudio_source_proxy.cpp @@ -171,5 +171,26 @@ void DAudioSourceProxy::DAudioNotify(const std::string &devId, const std::string Remote()->SendRequest(static_cast(IDAudioSourceInterfaceCode::DAUDIO_NOTIFY), data, reply, option); } + +int32_t DAudioSourceProxy::UpdateDistributedHardwareWorkMode(const std::string &devId, const std::string &dhId, + const WorkModeParam ¶m) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + CHECK_AND_RETURN_RET_LOG(!data.WriteInterfaceToken(GetDescriptor()), + ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED, "Write InterfaceToken failed"); + CHECK_AND_RETURN_RET_LOG(devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN, + ERR_DH_AUDIO_SA_DEVID_ILLEGAL, "Id is illegal."); + CHECK_AND_RETURN_RET_LOG(!data.WriteString(devId) || !data.WriteString(dhId) || + !data.WriteFileDescriptor(param.fd) || !data.WriteInt32(param.sharedMemLen) || + !data.WriteUint32(param.scene) || !data.WriteInt32(param.isAVsync), + ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED, "Write params failed."); + CHECK_AND_RETURN_RET_LOG(Remote() == nullptr, ERR_DH_AUDIO_NULLPTR, "Remote service is null."); + Remote()->SendRequest(static_cast(IDAudioSourceInterfaceCode::UPDATE_WORKMODE), + data, reply, option); + int32_t ret = reply.ReadInt32(); + return ret; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/include/daudio_ipc_common.h b/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/include/daudio_ipc_common.h index e81bd6f8..7b560667 100644 --- a/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/include/daudio_ipc_common.h +++ b/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/include/daudio_ipc_common.h @@ -111,6 +111,12 @@ public: void DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType, const std::string &eventContent) {} + + int32_t UpdateDistributedHardwareWorkMode(const std::string &devId, const std::string &dhId, + const WorkModeParam ¶m) + { + return DH_SUCCESS; + } }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/src/daudio_source_handler_test.cpp b/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/src/daudio_source_handler_test.cpp index 7f41fb1e..922a9574 100644 --- a/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/src/daudio_source_handler_test.cpp +++ b/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/src/daudio_source_handler_test.cpp @@ -221,5 +221,32 @@ HWTEST_F(DAudioSourceHandlerTest, ConfigDistributedHardware_003, TestSize.Level1 int32_t ret = DAudioSourceHandler::GetInstance().ConfigDistributedHardware(devId, dhId, key, value); EXPECT_EQ(DH_SUCCESS, ret); } + +/** + * @tc.name: UpdateDistributedHardwareWorkMode_001 + * @tc.desc: Verify the UpdateDistributedHardwareWorkMode function. + * @tc.type: FUNC + * @tc.require: AR000H0E5F + */ +HWTEST_F(DAudioSourceHandlerTest, UpdateDistributedHardwareWorkMode_001, TestSize.Level1) +{ + std::string devId = "devId"; + std::string dhId = "dhId"; + WorkModeParam params(-1, 0, 0, 0); + DAudioSourceHandler::GetInstance().dAudioSourceProxy_ = sptr(new MockIDAudioSource()); + int32_t ret = DAudioSourceHandler::GetInstance().UpdateDistributedHardwareWorkMode(devId, dhId, params); + EXPECT_EQ(DH_SUCCESS, ret); + size_t legalLen = 10; + size_t illegalLen = 101; + devId.resize(illegalLen); + ret = DAudioSourceHandler::GetInstance().UpdateDistributedHardwareWorkMode(devId, dhId, params); + EXPECT_EQ(ERR_DH_AUDIO_SA_DEVID_ILLEGAL, ret); + dhId.resize(illegalLen); + ret = DAudioSourceHandler::GetInstance().UpdateDistributedHardwareWorkMode(devId, dhId, params); + EXPECT_EQ(ERR_DH_AUDIO_SA_DEVID_ILLEGAL, ret); + devId.resize(legalLen); + ret = DAudioSourceHandler::GetInstance().UpdateDistributedHardwareWorkMode(devId, dhId, params); + EXPECT_EQ(ERR_DH_AUDIO_SA_DEVID_ILLEGAL, ret); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/src/daudio_source_proxy_test.cpp b/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/src/daudio_source_proxy_test.cpp index a0135595..5da4c59f 100644 --- a/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/src/daudio_source_proxy_test.cpp +++ b/interfaces/inner_kits/native_cpp/test/unittest/audiosourcetest/src/daudio_source_proxy_test.cpp @@ -168,5 +168,28 @@ HWTEST_F(DAudioSourceProxyTest, ConfigDistributedHardware_002, TestSize.Level1) ret = dAudioProxy->ConfigDistributedHardware(devId, dhId, key, value); EXPECT_EQ(ERR_DH_AUDIO_SA_DEVID_ILLEGAL, ret); } + +/** + * @tc.name: UpdateDistributedHardwareWorkMode_001 + * @tc.desc: Verify the UpdateDistributedHardwareWorkMode function. + * @tc.type: FUNC + * @tc.require: AR000H0E5F + */ +HWTEST_F(DAudioSourceProxyTest, UpdateDistributedHardwareWorkMode_001, TestSize.Level1) +{ + ASSERT_TRUE(dAudioProxy != nullptr); + size_t DAUDIO_MAX_DEVICE_ID_LEN = 101; + size_t DAUDIO_LEGAL_DEVICE_ID_LEN = 10; + WorkModeParam params(-1, 0, 0, 0); + std::string devId; + devId.resize(DAUDIO_MAX_DEVICE_ID_LEN); + std::string dhId = "dhId"; + int32_t ret = dAudioProxy->UpdateDistributedHardwareWorkMode(devId, dhId, params); + EXPECT_EQ(ERR_DH_AUDIO_SA_DEVID_ILLEGAL, ret); + devId.resize(DAUDIO_LEGAL_DEVICE_ID_LEN); + dhId.resize(DAUDIO_MAX_DEVICE_ID_LEN); + ret = dAudioProxy->UpdateDistributedHardwareWorkMode(devId, dhId, params); + EXPECT_EQ(ERR_DH_AUDIO_SA_DEVID_ILLEGAL, ret); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/audioclient/micclient/include/dmic_client.h b/services/audioclient/micclient/include/dmic_client.h index 5488621e..4efe0c62 100644 --- a/services/audioclient/micclient/include/dmic_client.h +++ b/services/audioclient/micclient/include/dmic_client.h @@ -77,6 +77,7 @@ private: int32_t AudioFwkClientSetUp(); int32_t TransSetUp(); void AudioFwkCaptureData(); + void CalcMicDataPts(); private: constexpr static uint8_t CHANNEL_WAIT_SECONDS = 5; static constexpr const char* CAPTURETHREAD = "captureThread"; @@ -99,6 +100,15 @@ private: int64_t lastTransStartTime_ = 0; std::atomic isPauseStatus_ = false; FILE *dumpFile_ = nullptr; + std::atomic micDataPts_ = 0; + constexpr static int64_t AUDIO_FRAME_INTERVAL_US = 20000; + constexpr static int64_t TIME_CONVERSION_NTOU = 1000; + constexpr static int64_t TIME_CONVERSION_STOU = 1000000; + constexpr static int32_t REVISE_PER_FIFTY_FRAME = 50; + constexpr static int32_t LENGTH_PER_TRANS = 960; + constexpr static int32_t AUDIO_FRAME_INTERVAL_MS = 20; + std::atomic getAudioTimeCounter_ = 0; + int64_t frameIndex_ = 0; }; } // DistributedHardware } // OHOS diff --git a/services/audioclient/micclient/src/dmic_client.cpp b/services/audioclient/micclient/src/dmic_client.cpp index 7ad9589d..b1bf9440 100644 --- a/services/audioclient/micclient/src/dmic_client.cpp +++ b/services/audioclient/micclient/src/dmic_client.cpp @@ -274,6 +274,21 @@ int32_t DMicClient::StartCapture() return DH_SUCCESS; } +void DMicClient::CalcMicDataPts() +{ + micDataPts_.fetch_add(AUDIO_FRAME_INTERVAL_US); + if (getAudioTimeCounter_ % REVISE_PER_FIFTY_FRAME == 0) { + AudioStandard::Timestamp timestamp; + audioCapturer_->GetTimeStampInfo(timestamp, AudioStandard::Timestamp::Timestampbase::MONOTONIC); + int64_t ptsOrigin = timestamp.time.tv_sec * TIME_CONVERSION_STOU + + timestamp.time.tv_nsec / TIME_CONVERSION_NTOU; + int64_t frameNumOfPts = static_cast(timestamp.framePosition) / LENGTH_PER_TRANS; + int64_t ptsFinal = ptsOrigin - (frameNumOfPts - frameIndex_) * AUDIO_FRAME_INTERVAL_MS; + micDataPts_.store(ptsFinal); + getAudioTimeCounter_ = 0; + } +} + void DMicClient::AudioFwkCaptureData() { std::shared_ptr audioData = std::make_shared(audioParam_.comParam.frameSize); @@ -302,6 +317,11 @@ void DMicClient::AudioFwkCaptureData() DHLOGE("Bytes read failed."); return; } + CalcMicDataPts(); + getAudioTimeCounter_++; + frameIndex_++; + DHLOGI("micDataPts_: %{public}" PRId64, micDataPts_.load()); + audioData->SetPts(micDataPts_.load()); if (isPauseStatus_.load()) { memset_s(audioData->Data(), audioData->Size(), 0, audioData->Size()); } diff --git a/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp b/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp index ab5978b7..1d0a5597 100644 --- a/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp +++ b/services/audioclient/test/unittest/micclient/src/dmic_client_test.cpp @@ -231,5 +231,18 @@ HWTEST_F(DMicClientTest, TransSetUp_001, TestSize.Level0) actual = micClient_->TransSetUp(); EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, actual); } + +/** + * @tc.name: CalcMicDataPts_001 + * @tc.desc: Verify the CalcMicDataPts function. + * @tc.type: FUNC + * @tc.require: AR000H0E6G + */ +HWTEST_F(DMicClientTest, CalcMicDataPts_001, TestSize.Level0) +{ + micClient_->getAudioTimeCounter_ = 1; + micClient_->CalcMicDataPts(); + EXPECT_NE(micClient_->getAudioTimeCounter_, 0); +} } // DistributedHardware } // OHOS diff --git a/services/audiomanager/managersource/include/daudio_io_dev.h b/services/audiomanager/managersource/include/daudio_io_dev.h index 74a4b63a..05c3512c 100644 --- a/services/audiomanager/managersource/include/daudio_io_dev.h +++ b/services/audiomanager/managersource/include/daudio_io_dev.h @@ -32,6 +32,13 @@ namespace OHOS { namespace DistributedHardware { +struct AudioAsyncParam { + int32_t fd; + int32_t sharedMemLen; + uint32_t scene; + bool isAVsync; +}; + class DAudioIoDev : public IDAudioHdiCallback { public: explicit DAudioIoDev(const std::string &devId) @@ -58,6 +65,8 @@ public: virtual AudioParam GetAudioParam() const = 0; virtual int32_t NotifyHdfAudioEvent(const AudioEvent &event, const int32_t portId) = 0; + virtual int32_t UpdateWorkModeParam(const std::string &devId, const std::string &dhId, + const AudioAsyncParam ¶m) = 0; protected: const std::string devId_; diff --git a/services/audiomanager/managersource/include/daudio_source_dev.h b/services/audiomanager/managersource/include/daudio_source_dev.h index 19a08371..a5d4ed20 100644 --- a/services/audiomanager/managersource/include/daudio_source_dev.h +++ b/services/audiomanager/managersource/include/daudio_source_dev.h @@ -58,6 +58,7 @@ public: bool GetThreadStatusFlag(); void NotifyEvent(const AudioEvent &event) override; void SetTokenId(uint64_t value); + int32_t UpdateWorkModeParam(const std::string &devId, const std::string &dhId, const AudioAsyncParam ¶m); private: int32_t EnableDSpeaker(const int32_t dhId, const std::string &attrs); diff --git a/services/audiomanager/managersource/include/daudio_source_manager.h b/services/audiomanager/managersource/include/daudio_source_manager.h index d4dbda94..e4846030 100644 --- a/services/audiomanager/managersource/include/daudio_source_manager.h +++ b/services/audiomanager/managersource/include/daudio_source_manager.h @@ -54,6 +54,7 @@ public: IAVEngineProvider *getSenderProvider(); IAVEngineProvider *getReceiverProvider(); void SetCallerTokenId(uint64_t tokenId); + int32_t UpdateWorkModeParam(const std::string &devId, const std::string &dhId, const AudioAsyncParam ¶m); private: DAudioSourceManager(); diff --git a/services/audiomanager/managersource/include/dmic_dev.h b/services/audiomanager/managersource/include/dmic_dev.h index 46a42261..78d50f2f 100644 --- a/services/audiomanager/managersource/include/dmic_dev.h +++ b/services/audiomanager/managersource/include/dmic_dev.h @@ -16,7 +16,7 @@ #ifndef OHOS_DMIC_DEV_H #define OHOS_DMIC_DEV_H -#include +#include #include #include #include "cJSON.h" @@ -90,6 +90,10 @@ public: int32_t OnStateChange(const AudioEventType type) override; int32_t OnDecodeTransDataDone(const std::shared_ptr &audioData) override; + int32_t UpdateWorkModeParam(const std::string &devId, const std::string &dhId, + const AudioAsyncParam ¶m) override; + int32_t AVsyncRefreshAshmem(int32_t fd, int32_t ashmemLength); + void AVsyncDeintAshmem(); private: void EnqueueThread(); @@ -99,10 +103,12 @@ private: void GetCodecCaps(const std::string &capability); void AddToVec(std::vector &container, const AudioCodecType value); bool IsMimeSupported(const AudioCodecType coder); + int32_t GetAudioDataFromQueue(std::shared_ptr &data); private: static constexpr uint8_t CHANNEL_WAIT_SECONDS = 5; static constexpr uint8_t RINGBUFFER_WAIT_SECONDS = 5; + static constexpr uint8_t SCENE_WAIT_SECONDS = 5; static constexpr size_t DATA_QUEUE_MAX_SIZE = 10; static constexpr size_t DATA_QUEUE_HALF_SIZE = DATA_QUEUE_MAX_SIZE >> 1U; static constexpr uint32_t LOW_LATENCY_JITTER_MAX_TIME_MS = 150; @@ -110,6 +116,8 @@ private: static constexpr uint8_t MMAP_NORMAL_PERIOD = 5; static constexpr uint8_t MMAP_VOIP_PERIOD = 20; static constexpr uint32_t MMAP_WAIT_FRAME_US = 5000; + constexpr static int64_t TIMESTAMP_COMPENSATION = 1333; + constexpr static int64_t ONE_FRAME_COMPENSATION = 20000; static constexpr const char* ENQUEUE_THREAD = "micEnqueueTh"; const std::string DUMP_DAUDIO_MIC_READ_FROM_BUF_NAME = "dump_source_mic_read_from_trans.pcm"; const std::string DUMP_DAUDIO_LOWLATENCY_MIC_FROM_BUF_NAME = "dump_source_mic_write_to_ashmem.pcm"; @@ -128,7 +136,7 @@ private: #ifdef ECHO_CANNEL_ENABLE std::shared_ptr echoManager_ = nullptr; #endif - std::queue> dataQueue_; + std::deque> dataQueue_; AudioStatus curStatus_ = AudioStatus::STATUS_IDLE; // Mic capture parameters AudioParamHDF paramHDF_; @@ -163,6 +171,33 @@ private: std::atomic isRingbufferOn_ = false; std::mutex ringbufferMutex_; std::vector codec_; + + uint64_t frameInIndex_ = 0; + uint64_t frameOutIndex_ = 0; + uint64_t framnum_ = 0; + uint64_t indexFlag_ = 15; + uint64_t frameOutIndexFlag_ = 16; + std::map ptsMap_; + std::mutex ptsMutex_; + AudioAsyncParam avSyncParam_ {}; + std::mutex avSyncMutex_; + uint32_t scene_ = 0; + std::atomic isFirstCaptureFrame_ = true; + sptr avsyncAshmem_ = nullptr; + constexpr static int64_t TIME_CONVERSION_NTOU = 1000; + constexpr static int64_t TIME_CONVERSION_STOU = 1000000; + struct AVsyncShareData { + volatile int lock = 1; + uint64_t audio_current_pts = 0; + uint64_t audio_update_clock = 0; + float audio_speed = 1.0f; + uint64_t video_current_pts = 0; + uint64_t video_update_clock = 0; + float video_speed = 1.0f; + uint64_t sync_strategy = 1; + bool reset = false; + }; + std::shared_ptr avsyncShareData_ = nullptr; }; } // DistributedHardware } // OHOS diff --git a/services/audiomanager/managersource/include/dspeaker_dev.h b/services/audiomanager/managersource/include/dspeaker_dev.h index 68d2eaea..cf4ba849 100644 --- a/services/audiomanager/managersource/include/dspeaker_dev.h +++ b/services/audiomanager/managersource/include/dspeaker_dev.h @@ -84,6 +84,8 @@ public: AudioParam GetAudioParam() const override; int32_t NotifyHdfAudioEvent(const AudioEvent &event, const int32_t portId) override; + int32_t UpdateWorkModeParam(const std::string &devId, const std::string &dhId, + const AudioAsyncParam ¶m) override; private: void EnqueueThread(); diff --git a/services/audiomanager/managersource/src/daudio_source_dev.cpp b/services/audiomanager/managersource/src/daudio_source_dev.cpp index eb18089a..4fc3f904 100644 --- a/services/audiomanager/managersource/src/daudio_source_dev.cpp +++ b/services/audiomanager/managersource/src/daudio_source_dev.cpp @@ -1561,6 +1561,20 @@ int32_t DAudioSourceDev::CheckOsType(std::string &networkId, bool &isInvalid) return DH_SUCCESS; } +int32_t DAudioSourceDev::UpdateWorkModeParam(const std::string &devId, const std::string &dhId, + const AudioAsyncParam ¶m) +{ + int32_t dhIdNum = ConvertString2Int(dhId); + CHECK_AND_RETURN_RET_LOG(dhIdNum == ERR_DH_AUDIO_FAILED, ERR_DH_AUDIO_FAILED, "dhId is invalid."); + std::lock_guard devLck(ioDevMtx_); + CHECK_AND_RETURN_RET_LOG(deviceMap_.find(dhIdNum) == deviceMap_.end(), ERR_DH_AUDIO_FAILED, "map not found."); + auto ioDev = deviceMap_[dhIdNum]; + CHECK_NULL_RETURN(ioDev, ERR_DH_AUDIO_FAILED); + int32_t ret = ioDev->UpdateWorkModeParam(devId, dhId, param); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ERR_DH_AUDIO_FAILED, "UpdateWorkModeParam failed."); + return DH_SUCCESS; +} + DAudioSourceDev::SourceEventHandler::SourceEventHandler(const std::shared_ptr &runner, const std::shared_ptr &dev) : AppExecFwk::EventHandler(runner), sourceDev_(dev) { diff --git a/services/audiomanager/managersource/src/daudio_source_manager.cpp b/services/audiomanager/managersource/src/daudio_source_manager.cpp index 607c9a0f..58ccc6c4 100644 --- a/services/audiomanager/managersource/src/daudio_source_manager.cpp +++ b/services/audiomanager/managersource/src/daudio_source_manager.cpp @@ -574,6 +574,26 @@ void DAudioSourceManager::SetCallerTokenId(uint64_t tokenId) callerTokenId_ = tokenId; } +int32_t DAudioSourceManager::UpdateWorkModeParam(const std::string &devId, const std::string &dhId, + const AudioAsyncParam ¶m) +{ + std::shared_ptr sourceDev = nullptr; + { + std::lock_guard lock(devMapMtx_); + auto device = audioDevMap_.find(devId); + if (device == audioDevMap_.end()) { + if (CreateAudioDevice(devId) != DH_SUCCESS) { + return ERR_DH_AUDIO_FAILED; + } + } + sourceDev = audioDevMap_[devId].dev; + } + CHECK_NULL_RETURN(sourceDev, ERR_DH_AUDIO_FAILED); + int32_t ret = sourceDev->UpdateWorkModeParam(devId, dhId, param); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "UpdateWorkModeParam failed, ret: %{public}d", ret); + return DH_SUCCESS; +} + IAVEngineProvider *DAudioSourceManager::getSenderProvider() { return sendProviderPtr_; diff --git a/services/audiomanager/managersource/src/dmic_dev.cpp b/services/audiomanager/managersource/src/dmic_dev.cpp index be316ad5..ca843234 100644 --- a/services/audiomanager/managersource/src/dmic_dev.cpp +++ b/services/audiomanager/managersource/src/dmic_dev.cpp @@ -16,6 +16,7 @@ #include "dmic_dev.h" #include +#include #include #include #include @@ -59,9 +60,20 @@ void DMicDev::OnEngineTransDataAvailable(const std::shared_ptr &audio { std::lock_guard lock(ringbufferMutex_); CHECK_NULL_VOID(ringBuffer_); + CHECK_NULL_VOID(audioData); CHECK_AND_RETURN_LOG(ringBuffer_->RingBufferInsert(audioData->Data(), static_cast(audioData->Capacity())) != DH_SUCCESS, "RingBufferInsert failed."); - DHLOGD("ringbuffer insert one"); + DHLOGD("Ringbuffer insert one"); + int64_t timestamp = audioData->GetPts(); + std::lock_guard timeLock(ptsMutex_); + ptsMap_[frameInIndex_] = timestamp; + frameInIndex_++; + if (frameInIndex_ == indexFlag_) { + timestamp = audioData->GetPtsSpecial(); + ptsMap_[frameInIndex_] = timestamp; + DHLOGD("audioData->GetPtsSpecial(): %{public}" PRId64", index: %{public}" PRIu64, timestamp, frameInIndex_); + frameInIndex_ = 0; + } } void DMicDev::ReadFromRingbuffer() @@ -96,6 +108,26 @@ void DMicDev::ReadFromRingbuffer() void DMicDev::SendToProcess(const std::shared_ptr &audioData) { DHLOGD("On Engine Data available"); + CHECK_NULL_VOID(audioData); + int64_t pts = 0; + { + std::lock_guard lock(ptsMutex_); + auto iter = ptsMap_.find(frameOutIndex_); + if (iter != ptsMap_.end()) { + pts = iter->second; + audioData->SetPts(pts); + ptsMap_.erase(iter); + } else { + DHLOGI("iter == ptsMap_.end()"); + } + frameOutIndex_++; + if (frameOutIndex_ == frameOutIndexFlag_) { + frameOutIndex_ = 0; + } + } + DHLOGI("Set timestamp %{public}" PRId64 " for frame index %{public}" PRIu64, audioData->GetPts(), frameOutIndex_); + framnum_++; + DHLOGD("current frame index: %{public}" PRIu64, framnum_); if (echoCannelOn_) { #ifdef ECHO_CANNEL_ENABLE CHECK_NULL_VOID(echoManager_); @@ -321,6 +353,7 @@ int32_t DMicDev::NotifyEvent(const int32_t streamId, const AudioEvent &event) case AudioEventType::AUDIO_START: curStatus_ = AudioStatus::STATUS_START; isExistedEmpty_.store(false); + isFirstCaptureFrame_.store(true); break; case AudioEventType::AUDIO_STOP: curStatus_ = AudioStatus::STATUS_STOP; @@ -358,6 +391,7 @@ int32_t DMicDev::SetUp() ptr->ReadFromRingbuffer(); }); echoCannelOn_ = false; + DHLOGI("echoCannelOn_: %{public}d", echoCannelOn_); #ifdef ECHO_CANNEL_ENABLE if (echoCannelOn_ && echoManager_ == nullptr) { echoManager_ = std::make_shared(); @@ -443,6 +477,11 @@ int32_t DMicDev::Release() } CHECK_NULL_RETURN(micTrans_, DH_SUCCESS); + AVsyncDeintAshmem(); + avSyncParam_.fd = -1; + avSyncParam_.sharedMemLen = 0; + DHLOGI("AVsync mode closed"); + int32_t ret = micTrans_->Release(); DaudioRadar::GetInstance().ReportMicCloseProgress("Release", MicClose::RELEASE_TRANS, ret); if (ret != DH_SUCCESS) { @@ -484,13 +523,15 @@ int32_t DMicDev::WriteStreamData(const int32_t streamId, std::shared_ptr &data) +int32_t DMicDev::GetAudioDataFromQueue(std::shared_ptr &data) { - int64_t startTime = GetNowTimeUs(); - if (curStatus_ != AudioStatus::STATUS_START) { - DHLOGE("Distributed audio is not starting status."); - return ERR_DH_AUDIO_FAILED; + DHLOGI("Data queue size %{public}zu", dataQueue_.size()); + if (dataQueue_.size() < scene_ && isFirstCaptureFrame_.load()) { + DHLOGI("Data queue size less than scene_"); + data = std::make_shared(param_.comParam.frameSize); + return DH_SUCCESS; } + isFirstCaptureFrame_.store(false); std::lock_guard lock(dataQueueMtx_); uint32_t queSize = dataQueue_.size(); if (queSize == 0) { @@ -499,9 +540,50 @@ int32_t DMicDev::ReadStreamData(const int32_t streamId, std::shared_ptr(param_.comParam.frameSize); } else { data = dataQueue_.front(); - dataQueue_.pop(); + dataQueue_.pop_front(); + } + return DH_SUCCESS; +} + +int32_t DMicDev::ReadStreamData(const int32_t streamId, std::shared_ptr &data) +{ + int64_t startTime = GetNowTimeUs(); + if (curStatus_ != AudioStatus::STATUS_START) { + DHLOGE("Distributed audio is not starting status."); + return ERR_DH_AUDIO_FAILED; } - CHECK_NULL_RETURN(data, ERR_DH_AUDIO_NULLPTR); + int32_t ret = GetAudioDataFromQueue(data); + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS && data ==nullptr, ERR_DH_AUDIO_NULLPTR, "GetAudioData failed"); + bool isAVsync = false; + { + std::lock_guardlock(avSyncMutex_); + isAVsync = avSyncParam_.isAVsync; + } + if (isAVsync && avsyncAshmem_ != nullptr) { + auto syncData = avsyncAshmem_->ReadFromAshmem(avSyncParam_.sharedMemLen, 0); + AVsyncShareData *readSyncSharedData = reinterpret_cast(const_cast(syncData)); + while (!readSyncSharedData->lock) { + DHLOGE("readSyncSharedData->lock == false"); + syncData = avsyncAshmem_->ReadFromAshmem(avSyncParam_.sharedMemLen, 0); + readSyncSharedData = reinterpret_cast(const_cast(syncData)); + } + avsyncShareData_ = std::make_shared(); + avsyncShareData_->lock = 0; + ret = avsyncAshmem_->WriteToAshmem(static_cast(avsyncShareData_.get()), sizeof(AVsyncShareData), 0); + CHECK_AND_RETURN_RET_LOG(!ret, ERR_DH_AUDIO_FAILED, "Write avsnc data failed"); + struct timespec time = {0, 0}; + clock_gettime(CLOCK_REALTIME, &time); + int64_t updatePts = static_cast(time.tv_sec) * TIME_CONVERSION_STOU + + static_cast(time.tv_nsec) / TIME_CONVERSION_NTOU; + avsyncShareData_->audio_current_pts = data->GetPts(); + avsyncShareData_->audio_update_clock = updatePts; + avsyncShareData_->lock = 1; + ret = avsyncAshmem_->WriteToAshmem(static_cast(avsyncShareData_.get()), sizeof(AVsyncShareData), 0); + CHECK_AND_RETURN_RET_LOG(!ret, ERR_DH_AUDIO_FAILED, "Write avsync data failed."); + DHLOGI("avsyncShareData_->audio_current_pts: %{public}" PRId64", avsyncShareData_->audio_update_clock: " + "%{public}" PRId64, avsyncShareData_->audio_current_pts, avsyncShareData_->audio_update_clock); + } + DumpFileUtil::WriteDumpFile(dumpFileCommn_, static_cast(data->Data()), data->Size()); int64_t endTime = GetNowTimeUs(); if (IsOutDurationRange(startTime, endTime, lastReadStartTime_)) { @@ -586,7 +668,8 @@ void DMicDev::EnqueueThread() audioData = std::make_shared(param_.comParam.frameSize); } else { audioData = dataQueue_.front(); - dataQueue_.pop(); + DHLOGI("shared_ptr audioData: %{public}" PRId64, audioData->GetPts()); + dataQueue_.pop_front(); } if (audioData == nullptr) { DHLOGD("The audioData is nullptr."); @@ -694,23 +777,78 @@ int32_t DMicDev::SendMessage(uint32_t type, std::string content, std::string dst int32_t DMicDev::OnDecodeTransDataDone(const std::shared_ptr &audioData) { + DHLOGD("On Decode Trans Data Done."); + int32_t scene = DATA_QUEUE_HALF_SIZE; + int32_t sceneMax = DATA_QUEUE_MAX_SIZE; + { + std::lock_guard lock(avSyncMutex_); + if (avSyncParam_.isAVsync) { + scene = scene_; + sceneMax = scene_ + scene_; + } + } CHECK_NULL_RETURN(audioData, ERR_DH_AUDIO_NULLPTR); std::lock_guard lock(dataQueueMtx_); dataQueSize_ = curStatus_ != AudioStatus::STATUS_START ? - (param_.captureOpts.capturerFlags == MMAP_MODE ? lowLatencyHalfSize_ : DATA_QUEUE_HALF_SIZE) : - (param_.captureOpts.capturerFlags == MMAP_MODE ? lowLatencyMaxfSize_ : DATA_QUEUE_MAX_SIZE); + (param_.captureOpts.capturerFlags == MMAP_MODE ? lowLatencyHalfSize_ : scene) : + (param_.captureOpts.capturerFlags == MMAP_MODE ? lowLatencyMaxfSize_ : sceneMax); if (isExistedEmpty_.load()) { dataQueSize_ = param_.captureOpts.capturerFlags == MMAP_MODE ? dataQueSize_ : DATA_QUEUE_EXT_SIZE; } uint64_t queueSize; - while (dataQueue_.size() > dataQueSize_) { + while (dataQueue_.size() > scene_) { queueSize = static_cast(dataQueue_.size()); - DHLOGD("Data queue overflow. buf current size: %{public}" PRIu64, queueSize); - dataQueue_.pop(); + DHLOGI("Data queue overflow. buf current size: %{public}" PRIu64, queueSize); + dataQueue_.pop_front(); + } + std::shared_ptr writeAudioData = std::make_shared(param_.comParam.frameSize); + if (memcpy_s(writeAudioData->Data(), writeAudioData->Capacity(), audioData->Data(), audioData->Capacity()) != EOK) { + DHLOGE("Copy audio data failed"); } - dataQueue_.push(audioData); + writeAudioData->SetPts(audioData->GetPts()); + dataQueue_.push_back(writeAudioData); queueSize = static_cast(dataQueue_.size()); - DHLOGD("Push new mic data, buf len: %{public}" PRIu64, queueSize); + DHLOGI("Push new mic data, buf len: %{public}" PRIu64, queueSize); + return DH_SUCCESS; +} +int32_t DMicDev::AVsyncRefreshAshmem(int32_t fd, int32_t ashmemLength) +{ + DHLOGD("AVsync mode: fd:%{public}d, ashmemLength: %{public}d", fd, ashmemLength); + CHECK_AND_RETURN_RET_LOG(avsyncAshmem_ != nullptr, DH_SUCCESS, "AvsyncAshmem_ is not nullptr"); + if (ashmemLength < ASHMEM_MAX_LEN && fd > 0) { + avsyncAshmem_ = sptr(new Ashmem(fd, ashmemLength)); + ashmemLength_ = ashmemLength; + DHLOGD("Create ashmem success. fd:%{public}d, ashmem length: %{public}d", fd, ashmemLength_); + bool mapRet = avsyncAshmem_->MapReadAndWriteAshmem(); + CHECK_AND_RETURN_RET_LOG(!mapRet, ERR_DH_AUDIO_NULLPTR, "Mmap ashmem failed."); + } + return DH_SUCCESS; +} +void DMicDev::AVsyncDeintAshmem() +{ + if (avsyncAshmem_ != nullptr) { + avsyncAshmem_->UnmapAshmem(); + avsyncAshmem_->CloseAshmem(); + avsyncAshmem_ = nullptr; + } +} + +int32_t DMicDev::UpdateWorkModeParam(const std::string &devId, const std::string &dhId, const AudioAsyncParam ¶m) +{ + std::lock_guard lock(avSyncMutex_); + avSyncParam_ = param; + if (avSyncParam_.isAVsync) { + auto ret = AVsyncRefreshAshmem(avSyncParam_.fd, avSyncParam_.sharedMemLen); + scene_ = indexFlag_; + CHECK_AND_RETURN_RET_LOG(ret != DH_SUCCESS, ret, "AVsyncRefreshAshmemInfo failed"); + DHLOGI("AVsync mode opened"); + } else { + AVsyncDeintAshmem(); + avSyncParam_.fd = -1; + avSyncParam_.sharedMemLen = 0; + scene_ = indexFlag_; + DHLOGI("AVsync mode closed"); + } return DH_SUCCESS; } } // DistributedHardware diff --git a/services/audiomanager/managersource/src/dspeaker_dev.cpp b/services/audiomanager/managersource/src/dspeaker_dev.cpp index 18ee5172..7457e0cf 100644 --- a/services/audiomanager/managersource/src/dspeaker_dev.cpp +++ b/services/audiomanager/managersource/src/dspeaker_dev.cpp @@ -552,5 +552,14 @@ int32_t DSpeakerDev::OnDecodeTransDataDone(const std::shared_ptr &aud (void) audioData; return DH_SUCCESS; } + +int32_t DSpeakerDev::UpdateWorkModeParam(const std::string &devId, const std::string &dhId, + const AudioAsyncParam ¶m) +{ + (void)devId; + (void)dhId; + (void)param; + return DH_SUCCESS; +} } // DistributedHardware } // OHOS diff --git a/services/audiomanager/servicesource/include/daudio_source_service.h b/services/audiomanager/servicesource/include/daudio_source_service.h index 6bf5a4ef..04c94a06 100644 --- a/services/audiomanager/servicesource/include/daudio_source_service.h +++ b/services/audiomanager/servicesource/include/daudio_source_service.h @@ -45,6 +45,8 @@ public: 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; + int32_t UpdateDistributedHardwareWorkMode(const std::string &devId, const std::string &dhId, + const WorkModeParam ¶m) override; protected: void OnStart() override; diff --git a/services/audiomanager/servicesource/include/daudio_source_stub.h b/services/audiomanager/servicesource/include/daudio_source_stub.h index 7cef6bb2..e2399369 100644 --- a/services/audiomanager/servicesource/include/daudio_source_stub.h +++ b/services/audiomanager/servicesource/include/daudio_source_stub.h @@ -38,6 +38,7 @@ private: int32_t ConfigDistributedHardwareInner(MessageParcel &data, MessageParcel &reply, MessageOption &option); int32_t DAudioNotifyInner(MessageParcel &data, MessageParcel &reply, MessageOption &option); bool VerifyPermission(); + int32_t UpdateDAudioWorkModeInner(MessageParcel &data, MessageParcel &reply, MessageOption &option); using DAudioSourceServiceFunc = int32_t (DAudioSourceStub::*)(MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/services/audiomanager/servicesource/src/daudio_source_service.cpp b/services/audiomanager/servicesource/src/daudio_source_service.cpp index e77ff184..32acfd8f 100644 --- a/services/audiomanager/servicesource/src/daudio_source_service.cpp +++ b/services/audiomanager/servicesource/src/daudio_source_service.cpp @@ -157,5 +157,19 @@ int DAudioSourceService::Dump(int32_t fd, const std::vector& arg return DH_SUCCESS; } + +int32_t DAudioSourceService::UpdateDistributedHardwareWorkMode(const std::string &devId, const std::string &dhId, + const WorkModeParam ¶m) +{ + DHLOGI("update workmode, devId: %{public}s, dhId: %{public}s.", GetAnonyString(devId).c_str(), + GetAnonyString(dhId).c_str()); + AudioAsyncParam audioAsyncParam = { 0 }; + audioAsyncParam.fd = param.fd; + audioAsyncParam.sharedMemLen = param.sharedMemLen; + audioAsyncParam.scene = param.scene; + audioAsyncParam.isAVsync = param.isAVsync; + DAudioSourceManager::GetInstance().UpdateWorkModeParam(devId, dhId, audioAsyncParam); + return DH_SUCCESS; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/audiomanager/servicesource/src/daudio_source_stub.cpp b/services/audiomanager/servicesource/src/daudio_source_stub.cpp index 93491e4d..3ddd23c2 100644 --- a/services/audiomanager/servicesource/src/daudio_source_stub.cpp +++ b/services/audiomanager/servicesource/src/daudio_source_stub.cpp @@ -44,6 +44,8 @@ DAudioSourceStub::DAudioSourceStub() : IRemoteStub(true) &DAudioSourceStub::ConfigDistributedHardwareInner; memberFuncMap_[static_cast(IDAudioSourceInterfaceCode::DAUDIO_NOTIFY)] = &DAudioSourceStub::DAudioNotifyInner; + memberFuncMap_[static_cast(IDAudioSourceInterfaceCode::UPDATE_WORKMODE)] = + &DAudioSourceStub::UpdateDAudioWorkModeInner; } int32_t DAudioSourceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, @@ -70,6 +72,8 @@ int32_t DAudioSourceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Me return ConfigDistributedHardwareInner(data, reply, option); case static_cast(IDAudioSourceInterfaceCode::DAUDIO_NOTIFY): return DAudioNotifyInner(data, reply, option); + case static_cast(IDAudioSourceInterfaceCode::UPDATE_WORKMODE): + return UpdateDAudioWorkModeInner(data, reply, option); default: DHLOGE("Invalid request code."); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -177,5 +181,24 @@ int32_t DAudioSourceStub::DAudioNotifyInner(MessageParcel &data, MessageParcel & DAudioNotify(networkId, dhId, eventType, eventContent); return DH_SUCCESS; } + +int32_t DAudioSourceStub::UpdateDAudioWorkModeInner(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + int32_t ret = 0; + do { + std::string devId = data.ReadString(); + std::string dhId = data.ReadString(); + WorkModeParam params(-1, 0, 0, 0); + params.fd = data.ReadFileDescriptor(); + + params.sharedMemLen = data.ReadInt32(); + params.scene = data.ReadUint32(); + params.isAVsync = data.ReadInt32(); + ret = UpdateDistributedHardwareWorkMode(devId, dhId, params); + DHLOGI("DistributedAudioSourceStub UpdateDistributedHardwareWorkMode %{public}d", ret); + } while (0); + reply.WriteInt32(ret); + return DH_SUCCESS; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp b/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp index d4230f80..b240564e 100644 --- a/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp +++ b/services/audiomanager/test/unittest/sourcedevice/src/daudio_source_dev_test.cpp @@ -970,7 +970,7 @@ HWTEST_F(DAudioSourceDevTest, TaskMicMmapStart_001, TestSize.Level1) EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, sourceDev_->TaskMicMmapStart(ARGS)); std::shared_ptr data = std::make_shared(AUDIO_DATA_CAP); for (size_t i = 0; i < TASK_QUEUE_LEN; i++) { - mic->dataQueue_.push(data); + mic->dataQueue_.push_back(data); } EXPECT_EQ(DH_SUCCESS, sourceDev_->deviceMap_[dhId]->MmapStop()); } diff --git a/services/audiomanager/test/unittest/sourcedevice/src/dmic_dev_test.cpp b/services/audiomanager/test/unittest/sourcedevice/src/dmic_dev_test.cpp index 93802e4a..332f9ac1 100644 --- a/services/audiomanager/test/unittest/sourcedevice/src/dmic_dev_test.cpp +++ b/services/audiomanager/test/unittest/sourcedevice/src/dmic_dev_test.cpp @@ -77,6 +77,26 @@ HWTEST_F(DMicDevTest, InitReceiverEngine_001, TestSize.Level1) EXPECT_EQ(DH_SUCCESS, mic_->InitReceiverEngine(providerPtr)); } +/** + * @tc.name: SendToProcess_001 + * @tc.desc: Verify SendToProcess and EnableDevice function. + * @tc.type: FUNC + * @tc.require: AR000H0E5F + */ +HWTEST_F(DMicDevTest, SendToProcess_001, TestSize.Level1) +{ + mic_->SendToProcess(nullptr); + const size_t capacity = 1; + auto writeData = std::make_shared(capacity); + mic_->SendToProcess(writeData); + mic_->frameIndex_ = 10; + mic_->ptsMap_[mic_->frameIndex_] = 10; + mic_->SendToProcess(writeData); + mic_->frameOutIndexFlag_ = 13; + mic_->SendToProcess(writeData); + EXPECT_NE(nullptr, writeData); +} + /** * @tc.name: EnableDMic_001 * @tc.desc: Verify EnableDMic and EnableDevice function. @@ -193,7 +213,7 @@ HWTEST_F(DMicDevTest, NotifyEvent_001, TestSize.Level1) for (int32_t i = 0; i < NOTIFY_WAIT_FRAMES; i++) { size_t size = 4096; auto audioData = std::make_shared(size); - mic_->dataQueue_.push(audioData); + mic_->dataQueue_.push_back(audioData); } event.type = AUDIO_START; EXPECT_EQ(DH_SUCCESS, mic_->NotifyEvent(streamId_, event)); @@ -324,6 +344,28 @@ HWTEST_F(DMicDevTest, Release_001, TestSize.Level1) EXPECT_EQ(DH_SUCCESS, mic_->RefreshAshmemInfo(streamId, fd, ashmemLength, lengthPerTrans)); } +/** + * @tc.name: GetAudioDataFromQueue_001 + * @tc.desc: Verify GetAudioDataFromQueue function. + * @tc.type: FUNC + * @tc.require: AR000H0E5F + */ +HWTEST_F(DMicDevTest, GetAudioDataFromQueue_001, TestSize.Level1) +{ + mic_->scene_ = 2; + mic_->isFirstCaptureFrame_ = true; + const size_t capacity = 1; + auto writeData = std::make_shared(capacity); + mic_->dataQueue_.push_back(writeData); + std::shared_ptr readData = nullptr; + EXPECT_EQ(DH_SUCCESS, mic_->GetAudioDataFromQueue(readData)); + mic_->dataQueue_.push_back(writeData); + EXPECT_EQ(DH_SUCCESS, mic_->GetAudioDataFromQueue(readData)); + mic_->isFirstCaptureFrame_ = false; + EXPECT_EQ(DH_SUCCESS, mic_->GetAudioDataFromQueue(readData)); + mic_->scene_ = 3; + EXPECT_EQ(DH_SUCCESS, mic_->GetAudioDataFromQueue(readData)); +} /** * @tc.name: ReadStreamData_001 @@ -340,11 +382,11 @@ HWTEST_F(DMicDevTest, ReadStreamData_001, TestSize.Level1) EXPECT_EQ(DH_SUCCESS, mic_->WriteStreamData(streamId_, writeData)); std::shared_ptr readData = nullptr; - mic_->dataQueue_.push(writeData); + mic_->dataQueue_.push_back(writeData); EXPECT_EQ(DH_SUCCESS, mic_->ReadStreamData(streamId_, readData)); for (size_t i = 0; i < 11; ++i) { auto data = std::make_shared(DEFAULT_AUDIO_DATA_SIZE); - mic_->dataQueue_.push(data); + mic_->dataQueue_.push_back(data); } mic_->isEnqueueRunning_ = true; mic_->FillJitterQueue(); @@ -360,7 +402,9 @@ HWTEST_F(DMicDevTest, ReadStreamData_001, TestSize.Level1) mic_->curStatus_ = AudioStatus::STATUS_START; EXPECT_EQ(DH_SUCCESS, mic_->ReadStreamData(streamId_, readData1)); - + mic_->avSyncParam_.isAVsync = 1; + EXPECT_EQ(DH_SUCCESS, mic_->ReadStreamData(streamId_, readData1)); + mic_->avSyncParam_.isAVsync = 0; EXPECT_EQ(DH_SUCCESS, mic_->ReadStreamData(streamId_, readData1)); } @@ -412,6 +456,10 @@ HWTEST_F(DMicDevTest, OnDecodeTransDataDone_001, TestSize.Level1) { std::shared_ptr data = nullptr; EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, mic_->OnDecodeTransDataDone(data)); + mic_->avSyncParam_.isAVsync = 0; + EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, mic_->OnDecodeTransDataDone(data)); + mic_->avSyncParam_.isAVsync = 1; + EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, mic_->OnDecodeTransDataDone(data)); const size_t capacity = 1; data = std::make_shared(capacity); @@ -510,20 +558,39 @@ HWTEST_F(DMicDevTest, ReadMmapPosition001, TestSize.Level1) int32_t streamId = 0; uint64_t frames = 0; CurrentTimeHDF time; + EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, mic_->MmapStart()); EXPECT_EQ(DH_SUCCESS, mic_->ReadMmapPosition(streamId, frames, time)); } /** - * @tc.name: MmapStart_001 - * @tc.desc: Verify MmapStart function. + * @tc.name: AVsyncRefreshAshmem001 + * @tc.desc: Verify AVsyncRefreshAshmem function. * @tc.type: FUNC * @tc.require: AR000H0E5F */ -HWTEST_F(DMicDevTest, MmapStart_001, TestSize.Level1) +HWTEST_F(DMicDevTest, AVsyncRefreshAshmem001, TestSize.Level1) { - mic_->ashmem_ = nullptr; - EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, mic_->MmapStart()); + int32_t fd = 10; + int32_t ashmemLength = 10; + EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, mic_->AVsyncRefreshAshmem(fd, ashmemLength)); + mic_->AVsyncDeintAshmem(); + EXPECT_EQ(nullptr, mic_->avsyncAshmem_); } +/** + * @tc.name: UpdateWorkModeParam001 + * @tc.desc: Verify UpdateWorkModeParam function. + * @tc.type: FUNC + * @tc.require: AR000H0E5F + */ +HWTEST_F(DMicDevTest, UpdateWorkModeParam001, TestSize.Level1) +{ + std::string devId = "devId"; + std::string dhId = "dhId"; + AudioAsyncParam param1{-1, 0, 0, 0}; + EXPECT_EQ(DH_SUCCESS, mic_->UpdateWorkModeParam(devId, dhId, param1)); + AudioAsyncParam param2{-1, 0, 0, 1}; + EXPECT_EQ(DH_SUCCESS, mic_->UpdateWorkModeParam(devId, dhId, param2)); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/audiomanager/test/unittest/sourcemanager/src/daudio_source_mgr_test.cpp b/services/audiomanager/test/unittest/sourcemanager/src/daudio_source_mgr_test.cpp index ac2dc5f8..a4e00c34 100644 --- a/services/audiomanager/test/unittest/sourcemanager/src/daudio_source_mgr_test.cpp +++ b/services/audiomanager/test/unittest/sourcemanager/src/daudio_source_mgr_test.cpp @@ -371,5 +371,20 @@ HWTEST_F(DAudioSourceMgrTest, LoadAVReceiverEngineProvider_001, TestSize.Level1) EXPECT_EQ(DH_SUCCESS, sourceMgr.LoadAVSenderEngineProvider()); EXPECT_EQ(DH_SUCCESS, sourceMgr.UnloadAVSenderEngineProvider()); } + +/** + * @tc.name: UpdateWorkModeParam_001 + * @tc.desc: Verify the UpdateWorkModeParam function. + * @tc.type: FUNC + * @tc.require: AR000H0E5F + */ +HWTEST_F(DAudioSourceMgrTest, UpdateWorkModeParam_001, TestSize.Level1) +{ + AudioAsyncParam param {-1, 0, 0, 0}; + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.UpdateWorkModeParam(DEV_ID, DH_ID_SPK, param)); + DAudioSourceManager::AudioDevice device = { DEV_ID, nullptr }; + sourceMgr.audioDevMap_[DEV_ID] = device; + EXPECT_EQ(ERR_DH_AUDIO_FAILED, sourceMgr.UpdateWorkModeParam(DEV_ID, DH_ID_SPK, param)); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/audiotransport/receiverengine/src/av_receiver_engine_transport.cpp b/services/audiotransport/receiverengine/src/av_receiver_engine_transport.cpp index 036dd8f0..5e98c712 100644 --- a/services/audiotransport/receiverengine/src/av_receiver_engine_transport.cpp +++ b/services/audiotransport/receiverengine/src/av_receiver_engine_transport.cpp @@ -138,6 +138,8 @@ void AVTransReceiverTransport::OnEngineDataAvailable(const std::shared_ptrSetPts(buffer->GetPts()); + audioData->SetPtsSpecial(buffer->GetPtsSpecial()); auto sourceDevObj = transCallback_.lock(); CHECK_NULL_VOID(sourceDevObj); sourceDevObj->OnEngineTransDataAvailable(audioData); diff --git a/services/audiotransport/senderengine/src/av_sender_engine_adapter.cpp b/services/audiotransport/senderengine/src/av_sender_engine_adapter.cpp index fd0205f2..0f7aee30 100644 --- a/services/audiotransport/senderengine/src/av_sender_engine_adapter.cpp +++ b/services/audiotransport/senderengine/src/av_sender_engine_adapter.cpp @@ -112,6 +112,7 @@ int32_t AVTransSenderAdapter::PushData(std::shared_ptr &audioData) CHECK_NULL_RETURN(bufferData, ERR_DH_AUDIO_NULLPTR); bufferData->Write(audioData->Data(), audioData->Size()); + transBuffer->SetPts(audioData->GetPts()); return senderEngine_->PushData(transBuffer); } diff --git a/services/common/audiodata/include/audio_data.h b/services/common/audiodata/include/audio_data.h index 33361607..0dd6594e 100644 --- a/services/common/audiodata/include/audio_data.h +++ b/services/common/audiodata/include/audio_data.h @@ -33,6 +33,10 @@ public: size_t Capacity() const; uint8_t *Data() const; int32_t SetRange(size_t offset, size_t size); + void SetPts(int64_t pts); + int64_t GetPts(); + void SetPtsSpecial(int64_t pts); + int64_t GetPtsSpecial(); void SetInt64(const string name, int64_t value); bool FindInt32(const string &name, int32_t &value); @@ -45,6 +49,8 @@ private: size_t rangeOffset_ = 0; size_t rangeLength_ = 0; uint8_t *data_ = nullptr; + int64_t pts_ = 0; + int64_t ptsSpecial_ = 0; map int32Map_; map int64Map_; diff --git a/services/common/audiodata/src/audio_data.cpp b/services/common/audiodata/src/audio_data.cpp index 8f5b2a1a..5698561d 100644 --- a/services/common/audiodata/src/audio_data.cpp +++ b/services/common/audiodata/src/audio_data.cpp @@ -55,6 +55,26 @@ int32_t AudioData::SetRange(size_t offset, size_t size) return DH_SUCCESS; } +void AudioData::SetPts(int64_t pts) +{ + pts_ = pts; +} + +int64_t AudioData::GetPts() +{ + return pts_; +} + +void AudioData::SetPtsSpecial(int64_t pts) +{ + ptsSpecial_ = pts; +} + +int64_t AudioData::GetPtsSpecial() +{ + return ptsSpecial_; +} + void AudioData::SetInt64(const string name, int64_t value) { int64Map_[name] = value; -- Gitee From 6e3dd1e39574e9c583543374a9ddd7e16ba3ed41 Mon Sep 17 00:00:00 2001 From: wangyupan Date: Sat, 30 Aug 2025 12:13:07 +0000 Subject: [PATCH 2/2] update services/audiomanager/servicesource/src/daudio_source_stub.cpp. Signed-off-by: wangyupan --- .../audiomanager/servicesource/src/daudio_source_stub.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/audiomanager/servicesource/src/daudio_source_stub.cpp b/services/audiomanager/servicesource/src/daudio_source_stub.cpp index 3ddd23c2..35b37fb0 100644 --- a/services/audiomanager/servicesource/src/daudio_source_stub.cpp +++ b/services/audiomanager/servicesource/src/daudio_source_stub.cpp @@ -184,6 +184,10 @@ int32_t DAudioSourceStub::DAudioNotifyInner(MessageParcel &data, MessageParcel & int32_t DAudioSourceStub::UpdateDAudioWorkModeInner(MessageParcel &data, MessageParcel &reply, MessageOption &option) { + if (!VerifyPermission()) { + DHLOGE("Permission verification fail."); + return ERR_DH_AUDIO_SA_PERMISSION_FAIED; + } int32_t ret = 0; do { std::string devId = data.ReadString(); -- Gitee