• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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_AU_SERVER_SINK_PLUGIN_H
17 #define HISTREAMER_AU_SERVER_SINK_PLUGIN_H
18 
19 #include <atomic>
20 #include <memory>
21 
22 #include "audio_info.h"
23 #include "audio_renderer.h"
24 #include "foundation/osal/thread/mutex.h"
25 #include "plugin/common/plugin_audio_tags.h"
26 #include "plugin/convert/ffmpeg_convert.h"
27 #include "plugin/interface/audio_sink_plugin.h"
28 #include "plugins/ffmpeg_adapter/utils/ffmpeg_utils.h"
29 #include "timestamp.h"
30 
31 namespace OHOS {
32 namespace Media {
33 namespace Plugin {
34 namespace AuSrSinkPlugin {
35 class AudioServerSinkPlugin : public Plugin::AudioSinkPlugin {
36 public:
37     explicit AudioServerSinkPlugin(std::string name);
38 
39     ~AudioServerSinkPlugin() override;
40 
41     Status Init() override;
42 
43     Status Deinit() override;
44 
45     Status Prepare() override;
46 
47     Status Reset() override;
48 
49     Status Start() override;
50 
51     Status Stop() override;
52 
53     Status GetParameter(Tag tag, ValueType& para) override;
54 
55     Status SetParameter(Tag tag, const ValueType& para) override;
56 
GetAllocator()57     std::shared_ptr<Allocator> GetAllocator() override
58     {
59         return nullptr;
60     }
61 
SetCallback(Callback * cb)62     Status SetCallback(Callback* cb) override
63     {
64         callback_ = cb;
65         return Status::OK;
66     }
67 
GetMute(bool & mute)68     Status GetMute(bool& mute) override
69     {
70         return Status::OK;
71     }
72 
SetMute(bool mute)73     Status SetMute(bool mute) override
74     {
75         return Status::OK;
76     }
77 
78     Status GetVolume(float& volume) override;
79 
80     Status SetVolume(float volume) override;
81 
GetSpeed(float & speed)82     Status GetSpeed(float& speed) override
83     {
84         return Status::OK;
85     }
86 
SetSpeed(float speed)87     Status SetSpeed(float speed) override
88     {
89         return Status::OK;
90     }
91 
92     Status Pause() override;
93 
94     Status Resume() override;
95 
96     Status GetLatency(uint64_t& hstTime) override;
97 
GetFrameSize(size_t & size)98     Status GetFrameSize(size_t& size) override
99     {
100         return Status::OK;
101     }
102 
GetFrameCount(uint32_t & count)103     Status GetFrameCount(uint32_t& count) override
104     {
105         return Status::OK;
106     }
107 
108     Status Write(const std::shared_ptr<Buffer>& input) override;
109 
110     Status Flush() override;
111 
112     Status Drain() override;
113 private:
114     class AudioRendererCallbackImpl : public OHOS::AudioStandard::AudioRendererCallback {
115     public:
116         AudioRendererCallbackImpl(Callback* cb, bool& isPaused);
117         void OnInterrupt(const OHOS::AudioStandard::InterruptEvent& interruptEvent) override;
118         void OnStateChange(const OHOS::AudioStandard::RendererState state,
119             const OHOS::AudioStandard::StateChangeCmdType cmdType) override;
120     private:
121         Callback* callback_ {};
122         bool isPaused_ {false};
123     };
124     void ReleaseRender();
125     bool StopRender();
126     bool AssignSampleRateIfSupported(uint32_t sampleRate);
127     bool AssignChannelNumIfSupported(uint32_t channelNum);
128     bool AssignSampleFmtIfSupported(AudioSampleFormat sampleFormat);
129     void SetInterruptMode(AudioStandard::InterruptMode interruptMode);
130 
131     OSAL::Mutex renderMutex_ {};
132     Callback* callback_ {};
133     AudioRenderInfo audioRenderInfo_ {};
134     AudioStandard::AudioRendererOptions rendererOptions_ {};
135     AudioStandard::InterruptMode audioInterruptMode_ {AudioStandard::InterruptMode::SHARE_MODE};
136     std::unique_ptr<AudioStandard::AudioRenderer> audioRenderer_ {nullptr};
137     std::shared_ptr<OHOS::AudioStandard::AudioRendererCallback> audioRendererCallback_ {nullptr};
138     AudioStandard::AudioRendererParams rendererParams_ {};
139 
140     bool fmtSupported_ {false};
141     bool isForcePaused_ {false};
142     AVSampleFormat reSrcFfFmt_ {AV_SAMPLE_FMT_NONE};
143     const AudioStandard::AudioSampleFormat reStdDestFmt_ {AudioStandard::AudioSampleFormat::SAMPLE_S16LE};
144     const AVSampleFormat reFfDestFmt_ {AV_SAMPLE_FMT_S16};
145     AudioChannelLayout channelLayout_ {};
146     uint32_t channels_ {};
147     uint32_t samplesPerFrame_ {};
148     uint32_t bitsPerSample_ {0};
149     uint32_t sampleRate_ {};
150     int64_t bitRate_ {0};
151     int32_t appPid_ {0};
152     int32_t appUid_ {0};
153     bool needReformat_ {false};
154     Plugin::Seekable seekable_ {Plugin::Seekable::INVALID};
155     std::shared_ptr<Ffmpeg::Resample> resample_ {nullptr};
156 };
157 } // AuSrSinkPlugin
158 } // Plugin
159 } // Media
160 } // OHOS
161 #endif // HISTREAMER_AU_SERVER_SINK_PLUGIN_H
162