1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DMIC_CLIENT_H 17 #define OHOS_DMIC_CLIENT_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <cstdint> 22 #include <memory> 23 #include <queue> 24 #include <sstream> 25 #include <string> 26 #include <thread> 27 28 #include "audio_capturer.h" 29 #include "audio_info.h" 30 31 #include "audio_data.h" 32 #include "audio_event.h" 33 #include "audio_param.h" 34 #include "audio_status.h" 35 #include "av_sender_engine_transport.h" 36 #include "daudio_constants.h" 37 #include "daudio_errorcode.h" 38 #include "daudio_log.h" 39 #include "daudio_sink_ctrl_trans.h" 40 #include "iaudio_data_transport.h" 41 #include "iaudio_datatrans_callback.h" 42 #include "iaudio_event_callback.h" 43 #include "imic_client.h" 44 45 namespace OHOS { 46 namespace DistributedHardware { 47 class DMicClient : public IAudioDataTransCallback, 48 public AudioStandard::AudioCapturerReadCallback, 49 public IMicClient, public AVSenderTransportCallback, 50 public IAudioCtrlTransCallback, 51 public std::enable_shared_from_this<DMicClient> { 52 public: DMicClient(const std::string & devId,const int32_t dhId,const std::shared_ptr<IAudioEventCallback> & callback)53 DMicClient(const std::string &devId, const int32_t dhId, const std::shared_ptr<IAudioEventCallback> &callback) 54 : devId_(devId), dhId_(dhId), eventCallback_(callback) {}; 55 ~DMicClient() override; 56 int32_t OnStateChange(const AudioEventType type) override; 57 int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override; 58 void OnEngineTransEvent(const AVTransEvent &event) override; 59 void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 60 void OnCtrlTransEvent(const AVTransEvent &event) override; 61 void OnCtrlTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 62 int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override; 63 int32_t InitCtrlTrans() override; 64 int32_t SetUp(const AudioParam ¶m) override; 65 int32_t Release() override; 66 int32_t StartCapture() override; 67 int32_t StopCapture() override; 68 void SetAttrs(const std::string &devId, const std::shared_ptr<IAudioEventCallback> &callback) override; 69 int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override; 70 71 void OnReadData(size_t length) override; 72 int32_t PauseCapture(); 73 int32_t ResumeCapture(); 74 private: 75 void CaptureThreadRunning(); 76 77 int32_t AudioFwkClientSetUp(); 78 int32_t TransSetUp(); 79 void AudioFwkCaptureData(); 80 private: 81 constexpr static uint8_t CHANNEL_WAIT_SECONDS = 5; 82 static constexpr const char* CAPTURETHREAD = "captureThread"; 83 const std::string DUMP_DAUDIO_MIC_BEFORE_TRANS_NAME = "dump_sink_mic_before_trans.pcm"; 84 85 std::string devId_; 86 int32_t dhId_; 87 std::thread captureDataThread_; 88 AudioParam audioParam_; 89 std::atomic<bool> isBlocking_ = false; 90 std::atomic<bool> isCaptureReady_ = false; 91 std::mutex devMtx_; 92 AudioStatus clientStatus_ = AudioStatus::STATUS_IDLE; 93 94 std::weak_ptr<IAudioEventCallback> eventCallback_; 95 std::unique_ptr<AudioStandard::AudioCapturer> audioCapturer_ = nullptr; 96 std::shared_ptr<IAudioDataTransport> micTrans_ = nullptr; 97 std::shared_ptr<IAudioCtrlTransport> micCtrlTrans_ = nullptr; 98 int64_t lastCaptureStartTime_ = 0; 99 int64_t lastTransStartTime_ = 0; 100 std::atomic<bool> isPauseStatus_ = false; 101 FILE *dumpFile_ = nullptr; 102 }; 103 } // DistributedHardware 104 } // OHOS 105 #endif // OHOS_DMIC_CLIENT_H 106