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 AudioDeviceDescriptor& deviceInfo, 49 const AudioStreamDeviceChangeReason reason) override; 50 51 private: 52 AudioAdapterDeviceChangeReason GetChangeReason(AudioStreamDeviceChangeReason reason); 53 std::shared_ptr<AudioOutputChangeCallbackAdapter> cb_ = nullptr; 54 }; 55 #endif 56 57 class AudioRendererAdapterImpl : public AudioRendererAdapter { 58 public: 59 AudioRendererAdapterImpl() = default; 60 61 ~AudioRendererAdapterImpl() override = default; 62 63 int32_t Create(const std::shared_ptr<AudioRendererOptionsAdapter> rendererOptions, 64 std::string cachePath = std::string()) override; 65 66 bool Start() override; 67 68 bool Pause() override; 69 70 bool Stop() override; 71 72 bool Release() override; 73 74 int32_t Write(uint8_t* buffer, size_t bufferSize) override; 75 76 int32_t GetLatency(uint64_t& latency) override; 77 78 int32_t SetVolume(float volume) override; 79 80 float GetVolume() override; 81 82 int32_t SetAudioRendererCallback(const std::shared_ptr<AudioRendererCallbackAdapter>& callback) override; 83 84 void SetInterruptMode(bool audioExclusive) override; 85 86 bool IsRendererStateRunning() override; 87 88 int32_t SetAudioOutputChangeCallback(const std::shared_ptr<AudioOutputChangeCallbackAdapter>& callback) override; 89 90 void SetAudioSilentMode(bool isSilentMode) override; 91 92 bool Flush() override; 93 94 #if defined(NWEB_AUDIO_ENABLE) 95 static AudioSamplingRate GetAudioSamplingRate(AudioAdapterSamplingRate samplingRate); 96 97 static AudioEncodingType GetAudioEncodingType(AudioAdapterEncodingType encodingType); 98 99 static AudioSampleFormat GetAudioSampleFormat(AudioAdapterSampleFormat sampleFormat); 100 101 static AudioChannel GetAudioChannel(AudioAdapterChannel channel); 102 103 static ContentType GetAudioContentType(AudioAdapterContentType contentType); 104 105 static StreamUsage GetAudioStreamUsage(AudioAdapterStreamUsage streamUsage); 106 107 static void TransformToAudioRendererOptions( 108 AudioRendererOptions& out, const std::shared_ptr<AudioRendererOptionsAdapter>& in); 109 110 static AudioSessionStrategy GetAudioAudioStrategy(AudioAdapterConcurrencyMode concurrencyMode); 111 112 private: 113 std::unique_ptr<AudioRenderer> audio_renderer_; 114 std::shared_ptr<AudioRendererCallbackImpl> callback_; 115 std::shared_ptr<AudioOutputChangeCallbackImpl> ouputChangeCallback_; 116 #endif 117 }; 118 } // namespace OHOS::NWeb 119 120 #endif // AUDIO_RENDERER_ADAPTER_IMPL_H 121