• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "nlohmann/json.hpp"
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 #include "daudio_hdi_handler.h"
30 #include "daudio_io_dev.h"
31 #include "iaudio_data_transport.h"
32 #include "iaudio_datatrans_callback.h"
33 #include "iaudio_event_callback.h"
34 #include "idaudio_hdi_callback.h"
35 
36 using json = nlohmann::json;
37 
38 namespace OHOS {
39 namespace DistributedHardware {
40 class DMicDev : public DAudioIoDev,
41     public IAudioDataTransCallback,
42     public AVReceiverTransportCallback,
43     public std::enable_shared_from_this<DMicDev> {
44 public:
DMicDev(const std::string & devId,std::shared_ptr<IAudioEventCallback> callback)45     DMicDev(const std::string &devId, std::shared_ptr<IAudioEventCallback> callback)
46         : DAudioIoDev(devId), audioEventCallback_(callback) {};
47     ~DMicDev() override = default;
48 
49     void OnEngineTransEvent(const AVTransEvent &event) override;
50     void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override;
51     void OnEngineTransDataAvailable(const std::shared_ptr<AudioData> &audioData) override;
52 
53     int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override;
54     int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override;
55 
56     int32_t EnableDevice(const int32_t dhId, const std::string &capability) override;
57     int32_t DisableDevice(const int32_t dhId) override;
58     int32_t OpenDevice(const std::string &devId, const int32_t dhId) override;
59     int32_t CloseDevice(const std::string &devId, const int32_t dhId) override;
60     int32_t SetParameters(const std::string &devId, const int32_t dhId, const AudioParamHDF &param) override;
61     int32_t WriteStreamData(const std::string &devId, const int32_t dhId, std::shared_ptr<AudioData> &data) override;
62     int32_t ReadStreamData(const std::string &devId, const int32_t dhId, std::shared_ptr<AudioData> &data) override;
63     int32_t NotifyEvent(const std::string &devId, const int32_t dhId, const AudioEvent &event) override;
64     int32_t ReadMmapPosition(const std::string &devId, const int32_t dhId,
65         uint64_t &frames, CurrentTimeHDF &time) override;
66     int32_t RefreshAshmemInfo(const std::string &devId, const int32_t dhId,
67         int32_t fd, int32_t ashmemLength, int32_t lengthPerTrans) override;
68 
69     int32_t MmapStart() override;
70     int32_t MmapStop() override;
71 
72     int32_t SetUp() override;
73     int32_t Start() override;
74     int32_t Pause() override;
75     int32_t Restart() override;
76     int32_t Stop() override;
77     int32_t Release() override;
78     bool IsOpened() override;
79     int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override;
80 
81     AudioParam GetAudioParam() const override;
82     int32_t NotifyHdfAudioEvent(const AudioEvent &event, const int32_t portId) override;
83 
84     int32_t OnStateChange(const AudioEventType type) override;
85     int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override;
86 
87 private:
88     void EnqueueThread();
89     void FillJitterQueue();
90 
91 private:
92     static constexpr uint8_t CHANNEL_WAIT_SECONDS = 5;
93     static constexpr size_t DATA_QUEUE_MAX_SIZE = 10;
94     static constexpr size_t DATA_QUEUE_HALF_SIZE = DATA_QUEUE_MAX_SIZE >> 1U;
95     static constexpr size_t LOW_LATENCY_DATA_QUEUE_MAX_SIZE = 30;
96     static constexpr size_t LOW_LATENCY_DATA_QUEUE_HALF_SIZE = 10;
97     static constexpr uint32_t MMAP_WAIT_FRAME_US = 5000;
98     static constexpr const char* ENQUEUE_THREAD = "micEnqueueTh";
99     const std::string MIC_DEV_FILENAME = DUMP_FILE_PATH + "/source_mic_read_from_trans.pcm";
100     const std::string MIC_LOWLATENCY_FILENAME = DUMP_FILE_PATH + "/source_mic_write_to_ashmem.pcm";
101 
102     std::weak_ptr<IAudioEventCallback> audioEventCallback_;
103     std::mutex dataQueueMtx_;
104     std::mutex channelWaitMutex_;
105     std::condition_variable channelWaitCond_;
106     int32_t curPort_ = 0;
107     std::atomic<bool> isTransReady_ = false;
108     std::atomic<bool> isOpened_ = false;
109     std::atomic<bool> dumpFlag_ = false;
110     std::shared_ptr<IAudioDataTransport> micTrans_ = nullptr;
111     std::queue<std::shared_ptr<AudioData>> dataQueue_;
112     AudioStatus curStatus_ = AudioStatus::STATUS_IDLE;
113     // Mic capture parameters
114     AudioParamHDF paramHDF_;
115     AudioParam param_;
116 
117     uint32_t timeInterval_ = 5;
118     uint32_t insertFrameCnt_ = 0;
119     std::atomic<bool> isExistedEmpty_ = false;
120     size_t dataQueSize_ = 0;
121     sptr<Ashmem> ashmem_ = nullptr;
122     std::atomic<bool> isEnqueueRunning_ = false;
123     int32_t ashmemLength_ = -1;
124     int32_t lengthPerTrans_ = -1;
125     int32_t writeIndex_ = -1;
126     int64_t frameIndex_ = 0;
127     int64_t startTime_ = 0;
128     uint64_t writeNum_ = 0;
129     int64_t writeTvSec_ = 0;
130     int64_t writeTvNSec_ = 0;
131     int64_t lastReadStartTime_ = 0;
132     std::thread enqueueDataThread_;
133     std::mutex writeAshmemMutex_;
134     std::condition_variable dataQueueCond_;
135     int32_t dhId_ = -1;
136 };
137 } // DistributedHardware
138 } // OHOS
139 #endif // OHOS_DAUDIO_DMIC_DEV_H
140