1 /*
2  * Copyright (c) 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 AUDIO_RENDERER_ADAPTER_IMPL_H
17 #define AUDIO_RENDERER_ADAPTER_IMPL_H
18 
19 #include "audio_renderer_adapter.h"
20 
21 #if defined(NWEB_AUDIO_ENABLE)
22 #include "audio_renderer.h"
23 #endif
24 
25 namespace OHOS::NWeb {
26 #if defined(NWEB_AUDIO_ENABLE)
27 using namespace OHOS::AudioStandard;
28 
29 class AudioRendererCallbackImpl : public AudioRendererCallback {
30 public:
31     AudioRendererCallbackImpl(std::shared_ptr<AudioRendererCallbackAdapter> cb);
32 
33     ~AudioRendererCallbackImpl() override = default;
34 
35     void OnInterrupt(const InterruptEvent& interruptEvent) override;
36 
37     void OnStateChange(const RendererState state, const StateChangeCmdType cmdType = CMD_FROM_CLIENT) override;
38 
39 private:
40     std::shared_ptr<AudioRendererCallbackAdapter> cb_ = nullptr;
41 };
42 class AudioOutputChangeCallbackImpl : public AudioRendererOutputDeviceChangeCallback {
43 public:
44     AudioOutputChangeCallbackImpl(std::shared_ptr<AudioOutputChangeCallbackAdapter> cb);
45 
46     ~AudioOutputChangeCallbackImpl() override = default;
47 
48     void OnOutputDeviceChange(const DeviceInfo& deviceInfo, const AudioStreamDeviceChangeReason reason) override;
49 
50 private:
51     AudioAdapterDeviceChangeReason GetChangeReason(AudioStreamDeviceChangeReason reason);
52     std::shared_ptr<AudioOutputChangeCallbackAdapter> cb_ = nullptr;
53 };
54 #endif
55 
56 class AudioRendererAdapterImpl : public AudioRendererAdapter {
57 public:
58     AudioRendererAdapterImpl() = default;
59 
60     ~AudioRendererAdapterImpl() override = default;
61 
62     int32_t Create(const std::shared_ptr<AudioRendererOptionsAdapter> rendererOptions,
63         std::string cachePath = std::string()) override;
64 
65     bool Start() override;
66 
67     bool Pause() override;
68 
69     bool Stop() override;
70 
71     bool Release() override;
72 
73     int32_t Write(uint8_t* buffer, size_t bufferSize) override;
74 
75     int32_t GetLatency(uint64_t& latency) override;
76 
77     int32_t SetVolume(float volume) override;
78 
79     float GetVolume() override;
80 
81     int32_t SetAudioRendererCallback(const std::shared_ptr<AudioRendererCallbackAdapter>& callback) override;
82 
83     void SetInterruptMode(bool audioExclusive) override;
84 
85     bool IsRendererStateRunning() override;
86 
87     int32_t SetAudioOutputChangeCallback(const std::shared_ptr<AudioOutputChangeCallbackAdapter>& callback) override;
88 
89     void SetAudioSilentMode(bool isSilentMode) override;
90 
91 #if defined(NWEB_AUDIO_ENABLE)
92     static AudioSamplingRate GetAudioSamplingRate(AudioAdapterSamplingRate samplingRate);
93 
94     static AudioEncodingType GetAudioEncodingType(AudioAdapterEncodingType encodingType);
95 
96     static AudioSampleFormat GetAudioSampleFormat(AudioAdapterSampleFormat sampleFormat);
97 
98     static AudioChannel GetAudioChannel(AudioAdapterChannel channel);
99 
100     static ContentType GetAudioContentType(AudioAdapterContentType contentType);
101 
102     static StreamUsage GetAudioStreamUsage(AudioAdapterStreamUsage streamUsage);
103 
104     static void TransformToAudioRendererOptions(
105         AudioRendererOptions& out, const std::shared_ptr<AudioRendererOptionsAdapter>& in);
106 
107     static AudioSessionStrategy GetAudioAudioStrategy(AudioAdapterConcurrencyMode concurrencyMode);
108 
109 private:
110     std::unique_ptr<AudioRenderer> audio_renderer_;
111     std::shared_ptr<AudioRendererCallbackImpl> callback_;
112     std::shared_ptr<AudioOutputChangeCallbackImpl> ouputChangeCallback_;
113 #endif
114 };
115 } // namespace OHOS::NWeb
116 
117 #endif // AUDIO_RENDERER_ADAPTER_IMPL_H
118