• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 AUDIO_SINK_SV_IMPL_H
17 #define AUDIO_SINK_SV_IMPL_H
18 
19 #include "audio_sink.h"
20 #include "audio_renderer.h"
21 #include "audio_system_manager.h"
22 #include "audio_errors.h"
23 #include "task_queue.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class AudioRendererMediaCallback : public AudioStandard::AudioRendererCallback {
28 public:
29     using InterruptCbFunc = std::function<void(GstBaseSink *, guint, guint, guint)>;
30     using StateCbFunc = std::function<void(GstBaseSink *, guint)>;
31     explicit AudioRendererMediaCallback(GstBaseSink *audioSink);
32     ~AudioRendererMediaCallback();
33     void SaveInterruptCallback(InterruptCbFunc interruptCb);
34     void SaveStateCallback(StateCbFunc stateCb);
35     void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) override;
36     void OnStateChange(const AudioStandard::RendererState state,
37         const AudioStandard::StateChangeCmdType cmdType) override;
38 private:
39     GstBaseSink *audioSink_ = nullptr;
40     InterruptCbFunc interruptCb_ = nullptr;
41     StateCbFunc stateCb_ = nullptr;
42     TaskQueue taskQue_;
43 };
44 
45 
46 class AudioSinkSvImpl : public AudioSink {
47 public:
48     explicit AudioSinkSvImpl(GstBaseSink *audioSink);
49     virtual ~AudioSinkSvImpl();
50 
51     GstCaps *GetCaps() override;
52     int32_t SetVolume(float volume) override;
53     int32_t GetVolume(float &volume) override;
54     int32_t GetMaxVolume(float &volume) override;
55     int32_t GetMinVolume(float &volume) override;
56     int32_t Prepare(int32_t appUid, int32_t appPid) override;
57     int32_t Start() override;
58     int32_t Stop() override;
59     int32_t Pause() override;
60     int32_t Drain() override;
61     int32_t Flush() override;
62     int32_t Release() override;
63     int32_t SetParameters(uint32_t bitsPerSample, uint32_t channels, uint32_t sampleRate) override;
64     int32_t GetParameters(uint32_t &bitsPerSample, uint32_t &channels, uint32_t &sampleRate) override;
65     int32_t GetMinimumBufferSize(uint32_t &bufferSize) override;
66     int32_t GetMinimumFrameCount(uint32_t &frameCount) override;
67     int32_t Write(uint8_t *buffer, size_t size) override;
68     int32_t GetAudioTime(uint64_t &time) override;
69     int32_t GetLatency(uint64_t &latency) const override;
70     int32_t SetRendererInfo(int32_t desc, int32_t rendererFlags) override;
71     void SetAudioInterruptMode(int32_t interruptMode) override;
72     void SetAudioSinkCb(void (*interruptCb)(GstBaseSink *, guint, guint, guint),
73                         void (*stateCb)(GstBaseSink *, guint),
74                         void (*errorCb)(GstBaseSink *, const std::string &)) override;
75     bool Writeable() const override;
76 
77 private:
78     void OnError(std::string errMsg);
79     using ErrorCbFunc = std::function<void(GstBaseSink *, const std::string &)>;
80     ErrorCbFunc errorCb_ = nullptr;
81     GstBaseSink *audioSink_ = nullptr;
82     std::unique_ptr<OHOS::AudioStandard::AudioRenderer> audioRenderer_ = nullptr;
83     AudioStandard::AudioRendererOptions rendererOptions_ = {};
84     void InitChannelRange(GstCaps *caps) const;
85     void InitRateRange(GstCaps *caps) const;
86     void SetMuteVolumeBySysParam();
87     bool isMute_ = false;
88     std::shared_ptr<AudioRendererMediaCallback> audioRendererMediaCallback_ = nullptr;
89 };
90 
91 class AudioSinkBypass : public AudioSinkSvImpl {
92 public:
AudioSinkBypass(GstBaseSink * audioSink)93     explicit AudioSinkBypass(GstBaseSink *audioSink) : AudioSinkSvImpl(audioSink) {}
94     virtual ~AudioSinkBypass() = default;
95 
SetVolume(float volume)96     int32_t SetVolume(float volume) override
97     {
98         (void)volume;
99         return MSERR_OK;
100     }
GetVolume(float & volume)101     int32_t GetVolume(float &volume) override
102     {
103         volume = 1.0; // default volume 1.0
104         return MSERR_OK;
105     }
GetMaxVolume(float & volume)106     int32_t GetMaxVolume(float &volume) override
107     {
108         volume = 1.0; // default max volume 1.0
109         return MSERR_OK;
110     }
GetMinVolume(float & volume)111     int32_t GetMinVolume(float &volume) override
112     {
113         volume = 0.0; // default min volume 0.0
114         return MSERR_OK;
115     }
Prepare(int32_t appUid,int32_t appPid)116     int32_t Prepare(int32_t appUid, int32_t appPid) override
117     {
118         (void)appUid;
119         (void)appPid;
120         return MSERR_OK;
121     }
Start()122     int32_t Start() override
123     {
124         return MSERR_OK;
125     }
Stop()126     int32_t Stop() override
127     {
128         return MSERR_OK;
129     }
Pause()130     int32_t Pause() override
131     {
132         return MSERR_OK;
133     }
Drain()134     int32_t Drain() override
135     {
136         return MSERR_OK;
137     }
Flush()138     int32_t Flush() override
139     {
140         return MSERR_OK;
141     }
Release()142     int32_t Release() override
143     {
144         return MSERR_OK;
145     }
SetParameters(uint32_t bitsPerSample,uint32_t channels,uint32_t sampleRate)146     int32_t SetParameters(uint32_t bitsPerSample, uint32_t channels, uint32_t sampleRate) override
147     {
148         (void)bitsPerSample;
149         (void)channels;
150         (void)sampleRate;
151         return MSERR_OK;
152     }
GetParameters(uint32_t & bitsPerSample,uint32_t & channels,uint32_t & sampleRate)153     int32_t GetParameters(uint32_t &bitsPerSample, uint32_t &channels, uint32_t &sampleRate) override
154     {
155         bitsPerSample = 16; // default bitsPerSample is 16
156         channels = 2; // default channels is 2
157         sampleRate = 44100; // default sampleRate is 44100
158         return MSERR_OK;
159     }
GetMinimumBufferSize(uint32_t & bufferSize)160     int32_t GetMinimumBufferSize(uint32_t &bufferSize) override
161     {
162         bufferSize = 3528; // default size is 3528
163         return MSERR_OK;
164     }
GetMinimumFrameCount(uint32_t & frameCount)165     int32_t GetMinimumFrameCount(uint32_t &frameCount) override
166     {
167         frameCount = 882; // default count is 882
168         return MSERR_OK;
169     }
Write(uint8_t * buffer,size_t size)170     int32_t Write(uint8_t *buffer, size_t size) override
171     {
172         (void)buffer;
173         (void)size;
174         return MSERR_OK;
175     }
GetAudioTime(uint64_t & time)176     int32_t GetAudioTime(uint64_t &time) override
177     {
178         (void)time;
179         return MSERR_OK;
180     }
GetLatency(uint64_t & latency)181     int32_t GetLatency(uint64_t &latency) const override
182     {
183         latency = 0;
184         return MSERR_OK;
185     }
SetRendererInfo(int32_t desc,int32_t rendererFlags)186     int32_t SetRendererInfo(int32_t desc, int32_t rendererFlags) override
187     {
188         (void)desc;
189         (void)rendererFlags;
190         return MSERR_OK;
191     }
SetAudioInterruptMode(int32_t interruptMode)192     void SetAudioInterruptMode(int32_t interruptMode) override
193     {
194         (void)interruptMode;
195     }
SetAudioSinkCb(void (* interruptCb)(GstBaseSink *,guint,guint,guint),void (* stateCb)(GstBaseSink *,guint),void (* errorCb)(GstBaseSink *,const std::string &))196     void SetAudioSinkCb(void (*interruptCb)(GstBaseSink *, guint, guint, guint),
197                         void (*stateCb)(GstBaseSink *, guint),
198                         void (*errorCb)(GstBaseSink *, const std::string &)) override
199     {
200         (void)interruptCb;
201         (void)stateCb;
202         (void)errorCb;
203     }
Writeable()204     bool Writeable() const override
205     {
206         return true;
207     }
208 };
209 } // namespace Media
210 } // namespace OHOS
211 #endif // AUDIO_SINK_SV_IMPL_H
212