1 /* 2 * Copyright (c) 2024 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 #ifndef NONE_MIX_ENGINE_H 16 #define NONE_MIX_ENGINE_H 17 #include <mutex> 18 #include <atomic> 19 #include "audio_playback_engine.h" 20 21 namespace OHOS { 22 namespace AudioStandard { 23 class NoneMixEngine : public AudioPlaybackEngine { 24 public: 25 NoneMixEngine(); 26 ~NoneMixEngine() override; 27 28 int32_t Init(const AudioDeviceDescriptor &type, bool isVoip) override; 29 int32_t Start() override; 30 int32_t Stop() override; 31 int32_t Pause() override; 32 int32_t Flush() override; 33 34 int32_t AddRenderer(const std::shared_ptr<IRendererStream> &stream) override; 35 void RemoveRenderer(const std::shared_ptr<IRendererStream> &stream) override; 36 bool IsPlaybackEngineRunning() const noexcept override; 37 38 uint64_t GetLatency() noexcept override; 39 40 protected: 41 void MixStreams() override; 42 void AdjustVoipVolume(); 43 44 private: 45 void StandbySleep(); 46 int32_t InitSink(const AudioStreamInfo &clientStreamInfo); 47 int32_t InitSink(uint32_t channel, AudioSampleFormat format, uint32_t rate); 48 int32_t SwitchSink(const AudioStreamInfo &streamInfo, bool isVoip); 49 void PauseAsync(); 50 int32_t StopAudioSink(); 51 void DoFadeinOut(bool isFadeOut, char* buffer, size_t bufferSize); 52 void DoRenderFrame(std::vector<char> &audioBufferConverted, int32_t index, int32_t appUid); 53 54 AudioSamplingRate GetDirectSampleRate(AudioSamplingRate sampleRate); 55 AudioSamplingRate GetDirectVoipSampleRate(AudioSamplingRate sampleRate); 56 AudioSampleFormat GetDirectDeviceFormat(AudioSampleFormat format); 57 AudioSampleFormat GetDirectVoipDeviceFormat(AudioSampleFormat format); 58 int32_t GetDirectFormatByteSize(AudioSampleFormat format); 59 60 void GetTargetSinkStreamInfo(const AudioStreamInfo &clientStreamInfo, uint32_t &targetSampleRate, 61 uint32_t &targetChannel, AudioSampleFormat &targetFormat, bool &isVoip); 62 63 private: 64 bool isVoip_; 65 bool isStart_; 66 bool isInit_; 67 AudioDeviceDescriptor device_ = AudioDeviceDescriptor(AudioDeviceDescriptor::DEVICE_INFO); 68 std::atomic<uint32_t> failedCount_; 69 uint64_t writeCount_; 70 uint64_t fwkSyncTime_; 71 uint64_t latency_; 72 std::shared_ptr<IRendererStream> stream_; 73 74 std::mutex startMutex; 75 76 std::mutex fadingMutex_; 77 std::condition_variable cvFading_; 78 std::atomic<bool> startFadein_; 79 std::atomic<bool> startFadeout_; 80 uint32_t uChannel_; 81 int32_t uFormat_; 82 uint32_t uSampleRate_; 83 bool firstSetVolume_; 84 }; 85 } // namespace AudioStandard 86 } // namespace OHOS 87 #endif 88