• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DEV_H
17 #define OHOS_DMIC_DEV_H
18 
19 #include <queue>
20 #include <set>
21 #include <thread>
22 #include "cJSON.h"
23 
24 #include "audio_param.h"
25 #include "audio_status.h"
26 #include "av_receiver_engine_transport.h"
27 #include "ashmem.h"
28 #include "daudio_constants.h"
29 #ifdef ECHO_CANNEL_ENABLE
30 #include "daudio_echo_cannel_manager.h"
31 #endif
32 #include "daudio_hdi_handler.h"
33 #include "daudio_io_dev.h"
34 #include "daudio_source_ctrl_trans.h"
35 #include "daudio_ringbuffer.h"
36 #include "iaudio_data_transport.h"
37 #include "iaudio_datatrans_callback.h"
38 #include "iaudio_event_callback.h"
39 #include "idaudio_hdi_callback.h"
40 
41 namespace OHOS {
42 namespace DistributedHardware {
43 class DMicDev : public DAudioIoDev,
44     public IAudioDataTransCallback,
45     public IAudioCtrlTransCallback,
46     public AVReceiverTransportCallback,
47     public std::enable_shared_from_this<DMicDev> {
48 public:
DMicDev(const std::string & devId,std::shared_ptr<IAudioEventCallback> callback)49     DMicDev(const std::string &devId, std::shared_ptr<IAudioEventCallback> callback)
50         : DAudioIoDev(devId), audioEventCallback_(callback) {};
51     ~DMicDev() override = default;
52 
53     void OnEngineTransEvent(const AVTransEvent &event) override;
54     void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override;
55     void OnEngineTransDataAvailable(const std::shared_ptr<AudioData> &audioData) override;
56 
57     void OnCtrlTransEvent(const AVTransEvent &event) override;
58     void OnCtrlTransMessage(const std::shared_ptr<AVTransMessage> &message) override;
59 
60     int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override;
61     int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override;
62     int32_t InitCtrlTrans() override;
63 
64     int32_t EnableDevice(const int32_t dhId, const std::string &capability) override;
65     int32_t DisableDevice(const int32_t dhId) override;
66     int32_t CreateStream(const int32_t streamId) override;
67     int32_t DestroyStream(const int32_t streamId) override;
68     int32_t SetParameters(const int32_t streamId, const AudioParamHDF &param) override;
69     int32_t WriteStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override;
70     int32_t ReadStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override;
71     int32_t NotifyEvent(const int32_t streamId, const AudioEvent &event) override;
72     int32_t ReadMmapPosition(const int32_t streamId, uint64_t &frames, CurrentTimeHDF &time) override;
73     int32_t RefreshAshmemInfo(const int32_t streamId,
74         int32_t fd, int32_t ashmemLength, int32_t lengthPerTrans) override;
75 
76     int32_t MmapStart() override;
77     int32_t MmapStop() override;
78 
79     int32_t SetUp() override;
80     int32_t Start() override;
81     int32_t Pause() override;
82     int32_t Restart() override;
83     int32_t Stop() override;
84     int32_t Release() override;
85     bool IsOpened() override;
86     int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override;
87 
88     AudioParam GetAudioParam() const override;
89     int32_t NotifyHdfAudioEvent(const AudioEvent &event, const int32_t portId) override;
90 
91     int32_t OnStateChange(const AudioEventType type) override;
92     int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override;
93 
94 private:
95     void EnqueueThread();
96     void FillJitterQueue();
97     void ReadFromRingbuffer();
98     void SendToProcess(const std::shared_ptr<AudioData> &audioData);
99 
100 private:
101     static constexpr uint8_t CHANNEL_WAIT_SECONDS = 5;
102     static constexpr uint8_t RINGBUFFER_WAIT_SECONDS = 5;
103     static constexpr size_t DATA_QUEUE_MAX_SIZE = 10;
104     static constexpr size_t DATA_QUEUE_HALF_SIZE = DATA_QUEUE_MAX_SIZE >> 1U;
105     static constexpr uint32_t LOW_LATENCY_JITTER_MAX_TIME_MS = 150;
106     static constexpr uint32_t LOW_LATENCY_JITTER_TIME_MS = 50;
107     static constexpr uint8_t MMAP_NORMAL_PERIOD = 5;
108     static constexpr uint8_t MMAP_VOIP_PERIOD = 20;
109     static constexpr uint32_t MMAP_WAIT_FRAME_US = 5000;
110     static constexpr const char* ENQUEUE_THREAD = "micEnqueueTh";
111     const std::string DUMP_DAUDIO_MIC_READ_FROM_BUF_NAME = "dump_source_mic_read_from_trans.pcm";
112     const std::string DUMP_DAUDIO_LOWLATENCY_MIC_FROM_BUF_NAME = "dump_source_mic_write_to_ashmem.pcm";
113     const int32_t ASHMEM_MAX_LEN = 2 * 4096;
114 
115     std::weak_ptr<IAudioEventCallback> audioEventCallback_;
116     std::mutex dataQueueMtx_;
117     std::mutex channelWaitMutex_;
118     std::condition_variable channelWaitCond_;
119     int32_t curPort_ = 0;
120     int32_t streamId_ = 100;
121     std::atomic<bool> isTransReady_ = false;
122     std::atomic<bool> isOpened_ = false;
123     std::shared_ptr<IAudioDataTransport> micTrans_ = nullptr;
124     std::shared_ptr<IAudioCtrlTransport> micCtrlTrans_ = nullptr;
125 #ifdef ECHO_CANNEL_ENABLE
126     std::shared_ptr<DAudioEchoCannelManager> echoManager_ = nullptr;
127 #endif
128     std::queue<std::shared_ptr<AudioData>> dataQueue_;
129     AudioStatus curStatus_ = AudioStatus::STATUS_IDLE;
130     // Mic capture parameters
131     AudioParamHDF paramHDF_;
132     AudioParam param_;
133 
134     std::atomic<bool> isExistedEmpty_ = false;
135     std::atomic<bool> isNeedCodec_ = true;
136     size_t dataQueSize_ = 0;
137     sptr<Ashmem> ashmem_ = nullptr;
138     std::atomic<bool> isEnqueueRunning_ = false;
139     int32_t ashmemLength_ = -1;
140     int32_t lengthPerTrans_ = -1;
141     int32_t writeIndex_ = -1;
142     int64_t frameIndex_ = 0;
143     int64_t startTime_ = 0;
144     uint64_t writeNum_ = 0;
145     int64_t writeTvSec_ = 0;
146     int64_t writeTvNSec_ = 0;
147     int64_t lastReadStartTime_ = 0;
148     std::thread enqueueDataThread_;
149     std::mutex writeAshmemMutex_;
150     std::condition_variable dataQueueCond_;
151     int32_t dhId_ = -1;
152     bool echoCannelOn_ = false;
153     FILE *dumpFileCommn_ = nullptr;
154     FILE *dumpFileFast_ = nullptr;
155     uint32_t lowLatencyHalfSize_ = 0;
156     uint32_t lowLatencyMaxfSize_ = 0;
157     std::unique_ptr<DaudioRingBuffer> ringBuffer_ = nullptr;
158     uint8_t *frameData_ = nullptr;
159     int32_t frameSize_ = 0;
160     std::thread ringbufferThread_;
161     std::atomic<bool> isRingbufferOn_ = false;
162     std::mutex ringbufferMutex_;
163 };
164 } // DistributedHardware
165 } // OHOS
166 #endif // OHOS_DAUDIO_DMIC_DEV_H
167