• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 &param) 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     void GetCodecCaps(const std::string &capability);
91     void AddToVec(std::vector<AudioCodecType> &container, const AudioCodecType value);
92     bool IsMimeSupported(const AudioCodecType coder);
93 
94 private:
95     static constexpr const char* ENQUEUE_THREAD = "spkEnqueueTh";
96     const std::string SPK_DEV_FILENAME = "dump_source_spk_write_to_trans.pcm";
97     const std::string SPK_LOWLATENCY_FILENAME = "dump_source_spk_fast_read_from_ashmem.pcm";
98     const int32_t ASHMEM_MAX_LEN = 2 * 4096;
99 
100     std::weak_ptr<IAudioEventCallback> audioEventCallback_;
101     std::mutex channelWaitMutex_;
102     std::condition_variable channelWaitCond_;
103     std::atomic<bool> isTransReady_ = false;
104     std::atomic<bool> isOpened_ = false;
105     int32_t curPort_ = 0;
106     int32_t streamId_ = 0;
107     std::shared_ptr<IAudioDataTransport> speakerTrans_ = nullptr;
108     std::shared_ptr<IAudioCtrlTransport> speakerCtrlTrans_ = nullptr;
109 
110     // Speaker render parameters
111     AudioParamHDF paramHDF_;
112     AudioParam param_;
113 
114     sptr<Ashmem> ashmem_ = nullptr;
115     std::atomic<bool> isEnqueueRunning_ = false;
116     int32_t ashmemLength_ = -1;
117     int32_t lengthPerTrans_ = -1;
118     int32_t readIndex_ = -1;
119     int64_t frameIndex_ = 0;
120     int64_t startTime_ = 0;
121     uint64_t readNum_ = 0;
122     int64_t readTvSec_ = 0;
123     int64_t readTvNSec_ = 0;
124     std::thread enqueueDataThread_;
125     int64_t lastwriteStartTime_ = 0;
126     int32_t dhId_ = -1;
127     FILE *dumpFileCommn_ = nullptr;
128     FILE *dumpFileFast_ = nullptr;
129     std::vector<AudioCodecType> codec_;
130 };
131 } // DistributedHardware
132 } // OHOS
133 #endif // OHOS_DAUDIO_DSPEAKER_DEV_H