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_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 "iaudio_data_transport.h"
35#include "iaudio_datatrans_callback.h"
36#include "iaudio_event_callback.h"
37#include "idaudio_hdi_callback.h"
38
39namespace OHOS {
40namespace DistributedHardware {
41class DMicDev : public DAudioIoDev,
42    public IAudioDataTransCallback,
43    public AVReceiverTransportCallback,
44    public std::enable_shared_from_this<DMicDev> {
45public:
46    DMicDev(const std::string &devId, std::shared_ptr<IAudioEventCallback> callback)
47        : DAudioIoDev(devId), audioEventCallback_(callback) {};
48    ~DMicDev() override = default;
49
50    void OnEngineTransEvent(const AVTransEvent &event) override;
51    void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override;
52    void OnEngineTransDataAvailable(const std::shared_ptr<AudioData> &audioData) override;
53
54    int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override;
55    int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override;
56
57    int32_t EnableDevice(const int32_t dhId, const std::string &capability) override;
58    int32_t DisableDevice(const int32_t dhId) override;
59    int32_t CreateStream(const int32_t streamId) override;
60    int32_t DestroyStream(const int32_t streamId) override;
61    int32_t SetParameters(const int32_t streamId, const AudioParamHDF &param) override;
62    int32_t WriteStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override;
63    int32_t ReadStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override;
64    int32_t NotifyEvent(const int32_t streamId, const AudioEvent &event) override;
65    int32_t ReadMmapPosition(const int32_t streamId, uint64_t &frames, CurrentTimeHDF &time) override;
66    int32_t RefreshAshmemInfo(const int32_t streamId,
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
87private:
88    void EnqueueThread();
89    void FillJitterQueue();
90
91private:
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 uint32_t LOW_LATENCY_JITTER_MAX_TIME_MS = 150;
96    static constexpr uint32_t LOW_LATENCY_JITTER_TIME_MS = 50;
97    static constexpr uint8_t MMAP_NORMAL_PERIOD = 5;
98    static constexpr uint8_t MMAP_VOIP_PERIOD = 20;
99    static constexpr uint32_t MMAP_WAIT_FRAME_US = 5000;
100    static constexpr const char* ENQUEUE_THREAD = "micEnqueueTh";
101    const std::string DUMP_DAUDIO_MIC_READ_FROM_BUF_NAME = "dump_source_mic_read_from_trans.pcm";
102    const std::string DUMP_DAUDIO_LOWLATENCY_MIC_FROM_BUF_NAME = "dump_source_mic_write_to_ashmem.pcm";
103    const int32_t ASHMEM_MAX_LEN = 2 * 4096;
104
105    std::weak_ptr<IAudioEventCallback> audioEventCallback_;
106    std::mutex dataQueueMtx_;
107    std::mutex channelWaitMutex_;
108    std::condition_variable channelWaitCond_;
109    int32_t curPort_ = 0;
110    int32_t streamId_ = 100;
111    std::atomic<bool> isTransReady_ = false;
112    std::atomic<bool> isOpened_ = false;
113    std::shared_ptr<IAudioDataTransport> micTrans_ = nullptr;
114#ifdef ECHO_CANNEL_ENABLE
115    std::shared_ptr<DAudioEchoCannelManager> echoManager_ = nullptr;
116#endif
117    std::queue<std::shared_ptr<AudioData>> dataQueue_;
118    AudioStatus curStatus_ = AudioStatus::STATUS_IDLE;
119    // Mic capture parameters
120    AudioParamHDF paramHDF_;
121    AudioParam param_;
122
123    uint32_t insertFrameCnt_ = 0;
124    std::atomic<bool> isExistedEmpty_ = false;
125    size_t dataQueSize_ = 0;
126    sptr<Ashmem> ashmem_ = nullptr;
127    std::atomic<bool> isEnqueueRunning_ = false;
128    int32_t ashmemLength_ = -1;
129    int32_t lengthPerTrans_ = -1;
130    int32_t writeIndex_ = -1;
131    int64_t frameIndex_ = 0;
132    int64_t startTime_ = 0;
133    uint64_t writeNum_ = 0;
134    int64_t writeTvSec_ = 0;
135    int64_t writeTvNSec_ = 0;
136    int64_t lastReadStartTime_ = 0;
137    std::thread enqueueDataThread_;
138    std::mutex writeAshmemMutex_;
139    std::condition_variable dataQueueCond_;
140    int32_t dhId_ = -1;
141    bool echoCannelOn_ = false;
142    FILE *dumpFileCommn_ = nullptr;
143    FILE *dumpFileFast_ = nullptr;
144    uint32_t lowLatencyHalfSize_ = 0;
145    uint32_t lowLatencyMaxfSize_ = 0;
146};
147} // DistributedHardware
148} // OHOS
149#endif // OHOS_DAUDIO_DMIC_DEV_H
150