1 /*
2  * Copyright (c) 2022-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 "iaudio_data_transport.h"
40 #include "iaudio_datatrans_callback.h"
41 #include "iaudio_event_callback.h"
42 #include "imic_client.h"
43 
44 namespace OHOS {
45 namespace DistributedHardware {
46 class DMicClient : public IAudioDataTransCallback,
47     public AudioStandard::AudioCapturerReadCallback,
48     public IMicClient, public AVSenderTransportCallback,
49     public std::enable_shared_from_this<DMicClient> {
50 public:
DMicClient(const std::string & devId,const int32_t dhId,const std::shared_ptr<IAudioEventCallback> & callback)51     DMicClient(const std::string &devId, const int32_t dhId, const std::shared_ptr<IAudioEventCallback> &callback)
52         : devId_(devId), dhId_(dhId), eventCallback_(callback) {};
53     ~DMicClient() override;
54     int32_t OnStateChange(const AudioEventType type) override;
55     int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override;
56     void OnEngineTransEvent(const AVTransEvent &event) override;
57     void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override;
58     int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override;
59     int32_t SetUp(const AudioParam &param) override;
60     int32_t Release() override;
61     int32_t StartCapture() override;
62     int32_t StopCapture() override;
63     void SetAttrs(const std::string &devId, const std::shared_ptr<IAudioEventCallback> &callback) override;
64     int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override;
65 
66     void OnReadData(size_t length) override;
67     int32_t PauseCapture();
68     int32_t ResumeCapture();
69 private:
70     void CaptureThreadRunning();
71 
72     int32_t AudioFwkClientSetUp();
73     int32_t TransSetUp();
74     void AudioFwkCaptureData();
75 private:
76     constexpr static uint8_t CHANNEL_WAIT_SECONDS = 5;
77     static constexpr const char* CAPTURETHREAD = "captureThread";
78     const std::string DUMP_DAUDIO_MIC_BEFORE_TRANS_NAME = "dump_sink_mic_before_trans.pcm";
79 
80     std::string devId_;
81     int32_t dhId_;
82     std::thread captureDataThread_;
83     AudioParam audioParam_;
84     std::atomic<bool> isBlocking_ = false;
85     std::atomic<bool> isCaptureReady_ = false;
86     std::mutex devMtx_;
87     AudioStatus clientStatus_ = AudioStatus::STATUS_IDLE;
88 
89     std::weak_ptr<IAudioEventCallback> eventCallback_;
90     std::unique_ptr<AudioStandard::AudioCapturer> audioCapturer_ = nullptr;
91     std::shared_ptr<IAudioDataTransport> micTrans_ = nullptr;
92     int64_t lastCaptureStartTime_ = 0;
93     int64_t lastTransStartTime_ = 0;
94     std::atomic<bool> isPauseStatus_ = false;
95     FILE *dumpFile_ = nullptr;
96 };
97 } // DistributedHardware
98 } // OHOS
99 #endif // OHOS_DMIC_CLIENT_H
100