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