1 /* 2 * Copyright (c) 2025 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 DIRECT_PLAYBACK_ENGINE_H 17 #define DIRECT_PLAYBACK_ENGINE_H 18 19 #include <mutex> 20 #include <atomic> 21 #include "audio_playback_engine.h" 22 #include "common/hdi_adapter_info.h" 23 24 namespace OHOS { 25 namespace AudioStandard { 26 class DirectPlayBackEngine : public AudioPlaybackEngine { 27 public: 28 DirectPlayBackEngine(); 29 ~DirectPlayBackEngine() override; 30 31 int32_t Init(const AudioDeviceDescriptor &type, bool isVoip) override; 32 int32_t Start() override; 33 int32_t Stop() override; 34 int32_t Pause() override; 35 int32_t Flush() override; 36 37 int32_t AddRenderer(const std::shared_ptr<IRendererStream> &stream) override; 38 void RemoveRenderer(const std::shared_ptr<IRendererStream> &stream) override; 39 bool IsPlaybackEngineRunning() const noexcept override; 40 41 uint64_t GetLatency() noexcept override; 42 43 protected: 44 void MixStreams() override; 45 46 private: 47 int32_t InitSink(const AudioStreamInfo &clientStreamInfo); 48 int32_t InitSink(uint32_t channel, AudioSampleFormat format, uint32_t rate, AudioChannelLayout layout); 49 int32_t StopAudioSink(); 50 void DoRenderFrame(std::vector<char> &audioBufferConverted, int32_t index, int32_t appUid); 51 void DirectCallback(const RenderCallbackType type); 52 int32_t RegisterWriteCallback(); 53 int32_t GetDirectFormatByteSize(AudioSampleFormat format); 54 55 private: 56 bool isStart_; 57 bool isInit_; 58 AudioDeviceDescriptor device_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO); 59 std::atomic<uint32_t> failedCount_; 60 uint64_t latency_; 61 std::shared_ptr<IRendererStream> stream_; 62 uint32_t uChannel_; 63 int32_t format_; 64 uint32_t uSampleRate_; 65 }; 66 } // namespace AudioStandard 67 } // namespace OHOS 68 #endif 69