• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DSPEAKER_DEV_H
17 #define OHOS_DSPEAKER_DEV_H
18 
19 #include <condition_variable>
20 #include <set>
21 #include <thread>
22 #include "nlohmann/json.hpp"
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 using json = nlohmann::json;
36 
37 namespace OHOS {
38 namespace DistributedHardware {
39 class DSpeakerDev : public DAudioIoDev,
40     public IAudioDataTransCallback,
41     public AVSenderTransportCallback,
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     int32_t InitReceiverEngine(IAVEngineProvider *providerPtr) override;
52     int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override;
53 
54     int32_t EnableDevice(const int32_t dhId, const std::string &capability) override;
55     int32_t DisableDevice(const int32_t dhId) override;
56     int32_t OpenDevice(const std::string &devId, const int32_t dhId) override;
57     int32_t CloseDevice(const std::string &devId, const int32_t dhId) override;
58     int32_t SetParameters(const std::string &devId, const int32_t dhId, const AudioParamHDF &param) override;
59     int32_t WriteStreamData(const std::string &devId, const int32_t dhId, std::shared_ptr<AudioData> &data) override;
60     int32_t ReadStreamData(const std::string &devId, const int32_t dhId, std::shared_ptr<AudioData> &data) override;
61     int32_t NotifyEvent(const std::string &devId, const int32_t dhId, const AudioEvent &event) override;
62     int32_t ReadMmapPosition(const std::string &devId, const int32_t dhId,
63         uint64_t &frames, CurrentTimeHDF &time) override;
64     int32_t RefreshAshmemInfo(const std::string &devId, const int32_t dhId,
65         int32_t fd, int32_t ashmemLength, int32_t lengthPerTrans) override;
66 
67     int32_t MmapStart() override;
68     int32_t MmapStop() override;
69 
70     int32_t OnStateChange(const AudioEventType type) override;
71     int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override;
72 
73     int32_t SetUp() override;
74     int32_t Start() override;
75     int32_t Stop() override;
76     int32_t Release() override;
77     bool IsOpened() override;
78     int32_t Pause() override;
79     int32_t Restart() override;
80     int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override;
81 
82     AudioParam GetAudioParam() const override;
83     int32_t NotifyHdfAudioEvent(const AudioEvent &event, const int32_t portId) override;
84 
85 private:
86     void EnqueueThread();
87 
88 private:
89     static constexpr const char* ENQUEUE_THREAD = "spkEnqueueTh";
90     const std::string SPK_DEV_FILENAME = DUMP_FILE_PATH + "/source_spk_write_to_trans.pcm";
91     const std::string SPK_LOWLATENCY_FILENAME = DUMP_FILE_PATH + "/source_spk_read_from_ashmem.pcm";
92 
93     std::weak_ptr<IAudioEventCallback> audioEventCallback_;
94     std::mutex channelWaitMutex_;
95     std::condition_variable channelWaitCond_;
96     std::atomic<bool> isTransReady_ = false;
97     std::atomic<bool> isOpened_ = false;
98     std::atomic<bool> dumpFlag_ = false;
99     int32_t curPort_ = 0;
100     std::shared_ptr<IAudioDataTransport> speakerTrans_ = nullptr;
101 
102     // Speaker render parameters
103     AudioParamHDF paramHDF_;
104     AudioParam param_;
105 
106     uint32_t timeInterval_ = 5;
107     sptr<Ashmem> ashmem_ = nullptr;
108     std::atomic<bool> isEnqueueRunning_ = false;
109     int32_t ashmemLength_ = -1;
110     int32_t lengthPerTrans_ = -1;
111     int32_t readIndex_ = -1;
112     int64_t frameIndex_ = 0;
113     int64_t startTime_ = 0;
114     uint64_t readNum_ = 0;
115     int64_t readTvSec_ = 0;
116     int64_t readTvNSec_ = 0;
117     std::thread enqueueDataThread_;
118     int64_t lastwriteStartTime_ = 0;
119     int32_t dhId_ = -1;
120 };
121 } // DistributedHardware
122 } // OHOS
123 #endif // OHOS_DAUDIO_DSPEAKER_DEV_H