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