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 DeviceInfo &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 protected:
39     void MixStreams() override;
40 
41 private:
42     void StandbySleep();
43     int32_t InitSink(const AudioStreamInfo &streamInfo);
44     int32_t InitSink(uint32_t channel, HdiAdapterFormat format, uint32_t rate);
45     int32_t SwitchSink(const AudioStreamInfo &streamInfo, bool isVoip);
46     void PauseAsync();
47     int32_t StopAudioSink();
48     void DoFadeinOut(bool isFadeOut, char* buffer, size_t bufferSize);
49 
50     int32_t GetDirectFormatByteSize(HdiAdapterFormat format);
51 
52     AudioSamplingRate GetDirectSampleRate(AudioSamplingRate sampleRate);
53     AudioSamplingRate GetDirectVoipSampleRate(AudioSamplingRate sampleRate);
54     HdiAdapterFormat GetDirectDeviceFormate(AudioSampleFormat format);
55 
56 private:
57     bool isVoip_;
58     bool isStart_;
59     bool isInit_;
60     DeviceInfo device_;
61     std::atomic<uint32_t> failedCount_;
62     uint64_t writeCount_;
63     uint64_t fwkSyncTime_;
64     std::shared_ptr<IRendererStream> stream_;
65 
66     std::mutex startMutex;
67 
68     std::mutex fadingMutex_;
69     std::condition_variable cvFading_;
70     std::atomic<bool> startFadein_;
71     std::atomic<bool> startFadeout_;
72     uint32_t uChannel_;
73     int32_t uFormat_;
74     uint32_t uSampleRate_;
75 };
76 } // namespace AudioStandard
77 } // namespace OHOS
78 #endif
79