150a07fd2Sopenharmony_ci/*
250a07fd2Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
350a07fd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
450a07fd2Sopenharmony_ci * you may not use this file except in compliance with the License.
550a07fd2Sopenharmony_ci * You may obtain a copy of the License at
650a07fd2Sopenharmony_ci *
750a07fd2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
850a07fd2Sopenharmony_ci *
950a07fd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1050a07fd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1150a07fd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1250a07fd2Sopenharmony_ci * See the License for the specific language governing permissions and
1350a07fd2Sopenharmony_ci * limitations under the License.
1450a07fd2Sopenharmony_ci */
1550a07fd2Sopenharmony_ci
1650a07fd2Sopenharmony_ci#ifndef OHOS_DMIC_DEV_H
1750a07fd2Sopenharmony_ci#define OHOS_DMIC_DEV_H
1850a07fd2Sopenharmony_ci
1950a07fd2Sopenharmony_ci#include <queue>
2050a07fd2Sopenharmony_ci#include <set>
2150a07fd2Sopenharmony_ci#include <thread>
2250a07fd2Sopenharmony_ci#include "cJSON.h"
2350a07fd2Sopenharmony_ci
2450a07fd2Sopenharmony_ci#include "audio_param.h"
2550a07fd2Sopenharmony_ci#include "audio_status.h"
2650a07fd2Sopenharmony_ci#include "av_receiver_engine_transport.h"
2750a07fd2Sopenharmony_ci#include "ashmem.h"
2850a07fd2Sopenharmony_ci#include "daudio_constants.h"
2950a07fd2Sopenharmony_ci#ifdef ECHO_CANNEL_ENABLE
3050a07fd2Sopenharmony_ci#include "daudio_echo_cannel_manager.h"
3150a07fd2Sopenharmony_ci#endif
3250a07fd2Sopenharmony_ci#include "daudio_hdi_handler.h"
3350a07fd2Sopenharmony_ci#include "daudio_io_dev.h"
3450a07fd2Sopenharmony_ci#include "iaudio_data_transport.h"
3550a07fd2Sopenharmony_ci#include "iaudio_datatrans_callback.h"
3650a07fd2Sopenharmony_ci#include "iaudio_event_callback.h"
3750a07fd2Sopenharmony_ci#include "idaudio_hdi_callback.h"
3850a07fd2Sopenharmony_ci
3950a07fd2Sopenharmony_cinamespace OHOS {
4050a07fd2Sopenharmony_cinamespace DistributedHardware {
4150a07fd2Sopenharmony_ciclass DMicDev : public DAudioIoDev,
4250a07fd2Sopenharmony_ci    public IAudioDataTransCallback,
4350a07fd2Sopenharmony_ci    public AVReceiverTransportCallback,
4450a07fd2Sopenharmony_ci    public std::enable_shared_from_this<DMicDev> {
4550a07fd2Sopenharmony_cipublic:
4650a07fd2Sopenharmony_ci    DMicDev(const std::string &devId, std::shared_ptr<IAudioEventCallback> callback)
4750a07fd2Sopenharmony_ci        : DAudioIoDev(devId), audioEventCallback_(callback) {};
4850a07fd2Sopenharmony_ci    ~DMicDev() override = default;
4950a07fd2Sopenharmony_ci
5050a07fd2Sopenharmony_ci    void OnEngineTransEvent(const AVTransEvent &event) override;
5150a07fd2Sopenharmony_ci    void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override;
5250a07fd2Sopenharmony_ci    void OnEngineTransDataAvailable(const std::shared_ptr<AudioData> &audioData) override;
5350a07fd2Sopenharmony_ci
5450a07fd2Sopenharmony_ci    int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override;
5550a07fd2Sopenharmony_ci    int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override;
5650a07fd2Sopenharmony_ci
5750a07fd2Sopenharmony_ci    int32_t EnableDevice(const int32_t dhId, const std::string &capability) override;
5850a07fd2Sopenharmony_ci    int32_t DisableDevice(const int32_t dhId) override;
5950a07fd2Sopenharmony_ci    int32_t CreateStream(const int32_t streamId) override;
6050a07fd2Sopenharmony_ci    int32_t DestroyStream(const int32_t streamId) override;
6150a07fd2Sopenharmony_ci    int32_t SetParameters(const int32_t streamId, const AudioParamHDF &param) override;
6250a07fd2Sopenharmony_ci    int32_t WriteStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override;
6350a07fd2Sopenharmony_ci    int32_t ReadStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override;
6450a07fd2Sopenharmony_ci    int32_t NotifyEvent(const int32_t streamId, const AudioEvent &event) override;
6550a07fd2Sopenharmony_ci    int32_t ReadMmapPosition(const int32_t streamId, uint64_t &frames, CurrentTimeHDF &time) override;
6650a07fd2Sopenharmony_ci    int32_t RefreshAshmemInfo(const int32_t streamId,
6750a07fd2Sopenharmony_ci        int32_t fd, int32_t ashmemLength, int32_t lengthPerTrans) override;
6850a07fd2Sopenharmony_ci
6950a07fd2Sopenharmony_ci    int32_t MmapStart() override;
7050a07fd2Sopenharmony_ci    int32_t MmapStop() override;
7150a07fd2Sopenharmony_ci
7250a07fd2Sopenharmony_ci    int32_t SetUp() override;
7350a07fd2Sopenharmony_ci    int32_t Start() override;
7450a07fd2Sopenharmony_ci    int32_t Pause() override;
7550a07fd2Sopenharmony_ci    int32_t Restart() override;
7650a07fd2Sopenharmony_ci    int32_t Stop() override;
7750a07fd2Sopenharmony_ci    int32_t Release() override;
7850a07fd2Sopenharmony_ci    bool IsOpened() override;
7950a07fd2Sopenharmony_ci    int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override;
8050a07fd2Sopenharmony_ci
8150a07fd2Sopenharmony_ci    AudioParam GetAudioParam() const override;
8250a07fd2Sopenharmony_ci    int32_t NotifyHdfAudioEvent(const AudioEvent &event, const int32_t portId) override;
8350a07fd2Sopenharmony_ci
8450a07fd2Sopenharmony_ci    int32_t OnStateChange(const AudioEventType type) override;
8550a07fd2Sopenharmony_ci    int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override;
8650a07fd2Sopenharmony_ci
8750a07fd2Sopenharmony_ciprivate:
8850a07fd2Sopenharmony_ci    void EnqueueThread();
8950a07fd2Sopenharmony_ci    void FillJitterQueue();
9050a07fd2Sopenharmony_ci
9150a07fd2Sopenharmony_ciprivate:
9250a07fd2Sopenharmony_ci    static constexpr uint8_t CHANNEL_WAIT_SECONDS = 5;
9350a07fd2Sopenharmony_ci    static constexpr size_t DATA_QUEUE_MAX_SIZE = 10;
9450a07fd2Sopenharmony_ci    static constexpr size_t DATA_QUEUE_HALF_SIZE = DATA_QUEUE_MAX_SIZE >> 1U;
9550a07fd2Sopenharmony_ci    static constexpr uint32_t LOW_LATENCY_JITTER_MAX_TIME_MS = 150;
9650a07fd2Sopenharmony_ci    static constexpr uint32_t LOW_LATENCY_JITTER_TIME_MS = 50;
9750a07fd2Sopenharmony_ci    static constexpr uint8_t MMAP_NORMAL_PERIOD = 5;
9850a07fd2Sopenharmony_ci    static constexpr uint8_t MMAP_VOIP_PERIOD = 20;
9950a07fd2Sopenharmony_ci    static constexpr uint32_t MMAP_WAIT_FRAME_US = 5000;
10050a07fd2Sopenharmony_ci    static constexpr const char* ENQUEUE_THREAD = "micEnqueueTh";
10150a07fd2Sopenharmony_ci    const std::string DUMP_DAUDIO_MIC_READ_FROM_BUF_NAME = "dump_source_mic_read_from_trans.pcm";
10250a07fd2Sopenharmony_ci    const std::string DUMP_DAUDIO_LOWLATENCY_MIC_FROM_BUF_NAME = "dump_source_mic_write_to_ashmem.pcm";
10350a07fd2Sopenharmony_ci    const int32_t ASHMEM_MAX_LEN = 2 * 4096;
10450a07fd2Sopenharmony_ci
10550a07fd2Sopenharmony_ci    std::weak_ptr<IAudioEventCallback> audioEventCallback_;
10650a07fd2Sopenharmony_ci    std::mutex dataQueueMtx_;
10750a07fd2Sopenharmony_ci    std::mutex channelWaitMutex_;
10850a07fd2Sopenharmony_ci    std::condition_variable channelWaitCond_;
10950a07fd2Sopenharmony_ci    int32_t curPort_ = 0;
11050a07fd2Sopenharmony_ci    int32_t streamId_ = 100;
11150a07fd2Sopenharmony_ci    std::atomic<bool> isTransReady_ = false;
11250a07fd2Sopenharmony_ci    std::atomic<bool> isOpened_ = false;
11350a07fd2Sopenharmony_ci    std::shared_ptr<IAudioDataTransport> micTrans_ = nullptr;
11450a07fd2Sopenharmony_ci#ifdef ECHO_CANNEL_ENABLE
11550a07fd2Sopenharmony_ci    std::shared_ptr<DAudioEchoCannelManager> echoManager_ = nullptr;
11650a07fd2Sopenharmony_ci#endif
11750a07fd2Sopenharmony_ci    std::queue<std::shared_ptr<AudioData>> dataQueue_;
11850a07fd2Sopenharmony_ci    AudioStatus curStatus_ = AudioStatus::STATUS_IDLE;
11950a07fd2Sopenharmony_ci    // Mic capture parameters
12050a07fd2Sopenharmony_ci    AudioParamHDF paramHDF_;
12150a07fd2Sopenharmony_ci    AudioParam param_;
12250a07fd2Sopenharmony_ci
12350a07fd2Sopenharmony_ci    uint32_t insertFrameCnt_ = 0;
12450a07fd2Sopenharmony_ci    std::atomic<bool> isExistedEmpty_ = false;
12550a07fd2Sopenharmony_ci    size_t dataQueSize_ = 0;
12650a07fd2Sopenharmony_ci    sptr<Ashmem> ashmem_ = nullptr;
12750a07fd2Sopenharmony_ci    std::atomic<bool> isEnqueueRunning_ = false;
12850a07fd2Sopenharmony_ci    int32_t ashmemLength_ = -1;
12950a07fd2Sopenharmony_ci    int32_t lengthPerTrans_ = -1;
13050a07fd2Sopenharmony_ci    int32_t writeIndex_ = -1;
13150a07fd2Sopenharmony_ci    int64_t frameIndex_ = 0;
13250a07fd2Sopenharmony_ci    int64_t startTime_ = 0;
13350a07fd2Sopenharmony_ci    uint64_t writeNum_ = 0;
13450a07fd2Sopenharmony_ci    int64_t writeTvSec_ = 0;
13550a07fd2Sopenharmony_ci    int64_t writeTvNSec_ = 0;
13650a07fd2Sopenharmony_ci    int64_t lastReadStartTime_ = 0;
13750a07fd2Sopenharmony_ci    std::thread enqueueDataThread_;
13850a07fd2Sopenharmony_ci    std::mutex writeAshmemMutex_;
13950a07fd2Sopenharmony_ci    std::condition_variable dataQueueCond_;
14050a07fd2Sopenharmony_ci    int32_t dhId_ = -1;
14150a07fd2Sopenharmony_ci    bool echoCannelOn_ = false;
14250a07fd2Sopenharmony_ci    FILE *dumpFileCommn_ = nullptr;
14350a07fd2Sopenharmony_ci    FILE *dumpFileFast_ = nullptr;
14450a07fd2Sopenharmony_ci    uint32_t lowLatencyHalfSize_ = 0;
14550a07fd2Sopenharmony_ci    uint32_t lowLatencyMaxfSize_ = 0;
14650a07fd2Sopenharmony_ci};
14750a07fd2Sopenharmony_ci} // DistributedHardware
14850a07fd2Sopenharmony_ci} // OHOS
14950a07fd2Sopenharmony_ci#endif // OHOS_DAUDIO_DMIC_DEV_H
150