• 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_hdi_handler.h"
28 #include "iaudio_event_callback.h"
29 #include "iaudio_data_transport.h"
30 #include "iaudio_datatrans_callback.h"
31 #include "idaudio_hdi_callback.h"
32 
33 using json = nlohmann::json;
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 class DSpeakerDev : public IDAudioHdiCallback,
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         : devId_(devId), audioEventCallback_(callback) {};
44     ~DSpeakerDev() override = default;
45     void OnEngineTransEvent(const AVTransEvent &event) override;
46     void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override;
47     int32_t InitSenderEngine(IAVEngineProvider *providerPtr);
48 
49     int32_t EnableDSpeaker(const int32_t dhId, const std::string& capability);
50     int32_t DisableDSpeaker(const int32_t dhId);
51 
52     int32_t OpenDevice(const std::string &devId, const int32_t dhId) override;
53     int32_t CloseDevice(const std::string &devId, const int32_t dhId) override;
54     int32_t SetParameters(const std::string &devId, const int32_t dhId, const AudioParamHDF &param) override;
55     int32_t WriteStreamData(const std::string &devId, const int32_t dhId, std::shared_ptr<AudioData> &data) override;
56     int32_t ReadStreamData(const std::string &devId, const int32_t dhId, std::shared_ptr<AudioData> &data) override;
57     int32_t NotifyEvent(const std::string &devId, const int32_t dhId, const AudioEvent &event) override;
58     int32_t ReadMmapPosition(const std::string &devId, const int32_t dhId,
59         uint64_t &frames, CurrentTimeHDF &time) override;
60     int32_t RefreshAshmemInfo(const std::string &devId, const int32_t dhId,
61         int32_t fd, int32_t ashmemLength, int32_t lengthPerTrans) override;
62     int32_t MmapStart();
63     int32_t MmapStop();
64 
65     int32_t OnStateChange(const AudioEventType type) override;
66     int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override;
67 
68     int32_t SetUp();
69     int32_t Start();
70     int32_t Stop();
71     int32_t Release();
72     bool IsOpened();
73     int32_t Pause();
74     int32_t Restart();
75     int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId);
76 
77     AudioParam GetAudioParam() const;
78     int32_t NotifyHdfAudioEvent(const AudioEvent &event);
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 
85 private:
86     static constexpr const char* ENQUEUE_THREAD = "spkEnqueueTh";
87 
88     std::string devId_;
89     std::weak_ptr<IAudioEventCallback> audioEventCallback_;
90     std::mutex channelWaitMutex_;
91     std::condition_variable channelWaitCond_;
92     std::atomic<bool> isTransReady_ = false;
93     std::atomic<bool> isOpened_ = false;
94     int32_t curPort_ = 0;
95     std::shared_ptr<IAudioDataTransport> speakerTrans_ = nullptr;
96     std::set<int32_t> enabledPorts_;
97 
98     // Speaker render parameters
99     AudioParamHDF paramHDF_;
100     AudioParam param_;
101 
102     uint32_t timeInterval_ = 5;
103     sptr<Ashmem> ashmem_ = nullptr;
104     std::atomic<bool> isEnqueueRunning_ = false;
105     int32_t ashmemLength_ = -1;
106     int32_t lengthPerTrans_ = -1;
107     int32_t readIndex_ = -1;
108     int64_t frameIndex_ = 0;
109     int64_t startTime_ = 0;
110     uint64_t readNum_ = 0;
111     int64_t readTvSec_ = 0;
112     int64_t readTvNSec_ = 0;
113     std::thread enqueueDataThread_;
114 };
115 } // DistributedHardware
116 } // OHOS
117 #endif // OHOS_DAUDIO_DSPEAKER_DEV_H