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_DSPEAKER_DEV_H 17 #define OHOS_DSPEAKER_DEV_H 18 19 #include <condition_variable> 20 #include <set> 21 #include <thread> 22 #include "cJSON.h" 23 24 #include "audio_param.h" 25 #include "ashmem.h" 26 #include "av_sender_engine_transport.h" 27 #include "daudio_constants.h" 28 #include "daudio_hdi_handler.h" 29 #include "daudio_io_dev.h" 30 #include "daudio_source_ctrl_trans.h" 31 #include "iaudio_event_callback.h" 32 #include "iaudio_data_transport.h" 33 #include "iaudio_datatrans_callback.h" 34 #include "idaudio_hdi_callback.h" 35 36 namespace OHOS { 37 namespace DistributedHardware { 38 class DSpeakerDev : public DAudioIoDev, 39 public IAudioDataTransCallback, 40 public AVSenderTransportCallback, 41 public IAudioCtrlTransCallback, 42 public std::enable_shared_from_this<DSpeakerDev> { 43 public: DSpeakerDev(const std::string & devId,std::shared_ptr<IAudioEventCallback> callback)44 DSpeakerDev(const std::string &devId, std::shared_ptr<IAudioEventCallback> callback) 45 : DAudioIoDev(devId), audioEventCallback_(callback) {}; 46 ~DSpeakerDev() override = default; 47 48 void OnEngineTransEvent(const AVTransEvent &event) override; 49 void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 50 51 void OnCtrlTransEvent(const AVTransEvent &event) override; 52 void OnCtrlTransMessage(const std::shared_ptr<AVTransMessage> &message) override; 53 54 int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override; 55 int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override; 56 int32_t InitCtrlTrans() override; 57 58 int32_t EnableDevice(const int32_t dhId, const std::string &capability) override; 59 int32_t DisableDevice(const int32_t dhId) override; 60 int32_t CreateStream(const int32_t streamId) override; 61 int32_t DestroyStream(const int32_t streamId) override; 62 int32_t SetParameters(const int32_t streamId, const AudioParamHDF ¶m) override; 63 int32_t WriteStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override; 64 int32_t ReadStreamData(const int32_t streamId, std::shared_ptr<AudioData> &data) override; 65 int32_t NotifyEvent(const int32_t streamId, const AudioEvent &event) override; 66 int32_t ReadMmapPosition(const int32_t streamId, uint64_t &frames, CurrentTimeHDF &time) override; 67 int32_t RefreshAshmemInfo(const int32_t streamId, 68 int32_t fd, int32_t ashmemLength, int32_t lengthPerTrans) override; 69 70 int32_t MmapStart() override; 71 int32_t MmapStop() override; 72 73 int32_t OnStateChange(const AudioEventType type) override; 74 int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override; 75 76 int32_t SetUp() override; 77 int32_t Start() override; 78 int32_t Stop() override; 79 int32_t Release() override; 80 bool IsOpened() override; 81 int32_t Pause() override; 82 int32_t Restart() override; 83 int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override; 84 85 AudioParam GetAudioParam() const override; 86 int32_t NotifyHdfAudioEvent(const AudioEvent &event, const int32_t portId) override; 87 88 private: 89 void EnqueueThread(); 90 91 private: 92 static constexpr const char* ENQUEUE_THREAD = "spkEnqueueTh"; 93 const std::string SPK_DEV_FILENAME = "dump_source_spk_write_to_trans.pcm"; 94 const std::string SPK_LOWLATENCY_FILENAME = "dump_source_spk_fast_read_from_ashmem.pcm"; 95 const int32_t ASHMEM_MAX_LEN = 2 * 4096; 96 97 std::weak_ptr<IAudioEventCallback> audioEventCallback_; 98 std::mutex channelWaitMutex_; 99 std::condition_variable channelWaitCond_; 100 std::atomic<bool> isTransReady_ = false; 101 std::atomic<bool> isOpened_ = false; 102 int32_t curPort_ = 0; 103 int32_t streamId_ = 0; 104 std::shared_ptr<IAudioDataTransport> speakerTrans_ = nullptr; 105 std::shared_ptr<IAudioCtrlTransport> speakerCtrlTrans_ = nullptr; 106 107 // Speaker render parameters 108 AudioParamHDF paramHDF_; 109 AudioParam param_; 110 111 sptr<Ashmem> ashmem_ = nullptr; 112 std::atomic<bool> isEnqueueRunning_ = false; 113 std::atomic<bool> isNeedCodec_ = true; 114 int32_t ashmemLength_ = -1; 115 int32_t lengthPerTrans_ = -1; 116 int32_t readIndex_ = -1; 117 int64_t frameIndex_ = 0; 118 int64_t startTime_ = 0; 119 uint64_t readNum_ = 0; 120 int64_t readTvSec_ = 0; 121 int64_t readTvNSec_ = 0; 122 std::thread enqueueDataThread_; 123 int64_t lastwriteStartTime_ = 0; 124 int32_t dhId_ = -1; 125 FILE *dumpFileCommn_ = nullptr; 126 FILE *dumpFileFast_ = nullptr; 127 }; 128 } // DistributedHardware 129 } // OHOS 130 #endif // OHOS_DAUDIO_DSPEAKER_DEV_H