• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::AVSession {
26 class AVSession;
27 };
28 
29 namespace OHOS::NWeb {
30 #if defined(NWEB_AUDIO_ENABLE)
31 using namespace OHOS::AudioStandard;
32 
33 class AudioRendererCallbackImpl : public AudioRendererCallback {
34 public:
35     AudioRendererCallbackImpl(std::shared_ptr<AudioRendererCallbackAdapter> cb);
36 
37     ~AudioRendererCallbackImpl() override = default;
38 
39     void OnInterrupt(const InterruptEvent &interruptEvent) override;
40 
41     void OnStateChange(const RendererState state, const StateChangeCmdType cmdType = CMD_FROM_CLIENT) override;
42 
43 private:
44     std::shared_ptr<AudioRendererCallbackAdapter> cb_ = nullptr;
45 };
46 #endif
47 
48 class AudioRendererAdapterImpl : public AudioRendererAdapter {
49 public:
50     AudioRendererAdapterImpl() = default;
51 
52     ~AudioRendererAdapterImpl();
53 
54     int32_t Create(const AudioAdapterRendererOptions &rendererOptions,
55         std::string cachePath = std::string()) override;
56 
57     bool Start() override;
58 
59     bool Pause() override;
60 
61     bool Stop() override;
62 
63     bool Release() override;
64 
65     int32_t Write(uint8_t *buffer, size_t bufferSize) override;
66 
67     int32_t GetLatency(uint64_t &latency) const override;
68 
69     int32_t SetVolume(float volume) const override;
70 
71     float GetVolume() const override;
72 
73     int32_t SetAudioRendererCallback(const std::shared_ptr<AudioRendererCallbackAdapter> &callback) override;
74 
75     void SetInterruptMode(bool audioExclusive) override;
76 
77     bool IsRendererStateRunning() override;
78 
79 #if defined(NWEB_AUDIO_ENABLE)
80     static AudioSamplingRate GetAudioSamplingRate(AudioAdapterSamplingRate samplingRate);
81 
82     static AudioEncodingType GetAudioEncodingType(AudioAdapterEncodingType encodingType);
83 
84     static AudioSampleFormat GetAudioSampleFormat(AudioAdapterSampleFormat sampleFormat);
85 
86     static AudioChannel GetAudioChannel(AudioAdapterChannel channel);
87 
88     static ContentType GetAudioContentType(AudioAdapterContentType contentType);
89 
90     static StreamUsage GetAudioStreamUsage(AudioAdapterStreamUsage streamUsage);
91 
92 private:
93     std::unique_ptr<AudioRenderer> audio_renderer_;
94     std::shared_ptr<AudioRendererCallbackImpl> callback_;
95     static std::shared_ptr<OHOS::AVSession::AVSession> avsession_;
96 #endif
97 };
98 }  // namespace OHOS::NWeb
99 
100 #endif // AUDIO_RENDERER_ADAPTER_IMPL_H