1 /*
2  * Copyright (c) 2021-2021 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 HISTREAMER_HDI_SINK_H
17 #define HISTREAMER_HDI_SINK_H
18 
19 #include <atomic>
20 #include <vector>
21 #include "audio_manager.h"
22 #include "audio_types.h"
23 #include "foundation/osal/thread/condition_variable.h"
24 #include "foundation/osal/thread/mutex.h"
25 #include "foundation/osal/thread/task.h"
26 #include "plugin/interface/audio_sink_plugin.h"
27 
28 struct AudioAdapter;
29 struct AudioRender;
30 struct AudioPort;
31 namespace OHOS {
32 namespace Media {
33 namespace Plugin {
34 namespace HosLite {
35 class RingBuffer;
36 
37 class HdiSink : public std::enable_shared_from_this<HdiSink>, public AudioSinkPlugin {
38 public:
39     explicit HdiSink(std::string name);
40 
41     ~HdiSink() override = default;
42 
43     Status Init() override;
44 
45     Status Deinit() override;
46 
47     Status Prepare() override;
48 
49     Status Reset() override;
50 
51     Status Start() override;
52 
53     Status Stop() override;
54 
55     Status GetParameter(Tag tag, ValueType& value) override;
56 
57     Status SetParameter(Tag tag, const ValueType& value) override;
58 
59     std::shared_ptr<Allocator> GetAllocator() override;
60 
61     Status SetCallback(Callback* cb) override;
62 
63     Status GetMute(bool& mute) override;
64 
65     Status SetMute(bool mute) override;
66 
67     Status GetVolume(float& volume) override;
68 
69     Status SetVolume(float volume) override;
70 
71     Status GetSpeed(float& speed) override;
72 
73     Status SetSpeed(float speed) override;
74 
75     Status Pause() override;
76 
77     Status Resume() override;
78 
79     Status GetLatency(uint64_t& hstTime) override;
80 
81     Status GetFrameSize(size_t& size) override;
82 
83     Status GetFrameCount(uint32_t& count) override;
84 
85     Status Write(const std::shared_ptr<Buffer>& input) override;
86 
87     Status Flush() override;
88 
89     Status Drain() override;
90 
91 private:
92     Status ReleaseRender();
93 
94     void Deinterleave8(uint8_t* inData, uint8_t* outData, int32_t frameCnt);
95 
96     void Deinterleave16(uint8_t* inData, uint8_t* outData, int32_t frameCnt);
97 
98     void Deinterleave32(uint8_t* inData, uint8_t* outData, int32_t frameCnt);
99 
100     bool HandleInterleaveData(uint8_t* origData, int32_t frameCnt);
101 
102     void RenderFrame(const std::shared_ptr<Buffer>& input);
103 
104     Status ProcessInputSampleFormat(const ValueType& value);
105 
106 private:
107     OSAL::Mutex renderMutex_ {};
108     AudioManager* audioManager_ {nullptr};
109     AudioAdapterDescriptor adapterDescriptor_ {};
110     AudioAdapter* audioAdapter_ {nullptr};
111     AudioRender* audioRender_ {nullptr};
112     AudioDeviceDescriptor deviceDescriptor_ {};
113     AudioSampleAttributes sampleAttributes_ {};
114     bool isInputInterleaved_{false};
115     AudioChannelMask channelMask_{AUDIO_CHANNEL_MONO};
116     Callback* eventCallback_ {};
117     std::vector<uint8_t> cacheData_;
118     bool usingDefaultInCaps_ {true}; // if true pass hdi with S16P pcm data and convert input into non-interleaved
119     std::atomic<bool> processing_;
120     OSAL::ConditionVariable renderCond_;
121 };
122 } // namespace HosLite
123 } // namespace Plugin
124 } // namespace Media
125 } // namespace OHOS
126 #endif
127