1 /* 2 * Copyright (C) 2023 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 SOUNDPOOL_MOCK_H 16 #define SOUNDPOOL_MOCK_H 17 18 #include <fcntl.h> 19 #include <cstdio> 20 #include <mutex> 21 #include <cstdlib> 22 #include <thread> 23 #include <string> 24 #include <vector> 25 #include <memory> 26 #include <atomic> 27 #include <iostream> 28 #include "gtest/gtest.h" 29 #include "unittest_log.h" 30 #include "media_errors.h" 31 #include "nocopyable.h" 32 #include "isoundpool.h" 33 34 namespace OHOS { 35 namespace Media { 36 using namespace std; 37 using namespace AudioStandard; 38 class SoundPoolMock { 39 public: 40 SoundPoolMock() = default; 41 ~SoundPoolMock() = default; 42 bool CreateSoundPool(int maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo); 43 int32_t Load(std::string url); 44 int32_t Load(int32_t fd, int64_t offset, int64_t length); 45 int32_t Play(int32_t soundID, PlayParams playParameters); 46 int32_t Stop(int32_t streamID); 47 int32_t SetLoop(int32_t streamID, int32_t loop); 48 int32_t SetPriority(int32_t streamID, int32_t priority); 49 int32_t SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate); 50 int32_t SetVolume(int32_t streamID, float leftVolume, float rigthVolume); 51 int32_t Unload(int32_t soundID); 52 int32_t Release(); 53 int32_t SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback); 54 size_t GetFileSize(const std::string& fileName); 55 private: 56 std::shared_ptr<ISoundPool> soundPool_ = nullptr; 57 std::atomic<bool> isExit_ { false }; 58 }; 59 60 class SoundPoolParallelMock { 61 public: 62 SoundPoolParallelMock() = default; 63 ~SoundPoolParallelMock() = default; 64 bool CreateParallelSoundPool(int maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo); 65 int32_t Load(std::string url); 66 int32_t Load(int32_t fd, int64_t offset, int64_t length); 67 int32_t Play(int32_t soundID, PlayParams playParameters); 68 int32_t Stop(int32_t streamID); 69 int32_t SetLoop(int32_t streamID, int32_t loop); 70 int32_t SetPriority(int32_t streamID, int32_t priority); 71 int32_t SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate); 72 int32_t SetVolume(int32_t streamID, float leftVolume, float rigthVolume); 73 int32_t Unload(int32_t soundID); 74 int32_t Release(); 75 int32_t SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback); 76 size_t GetFileSize(const std::string& fileName); 77 private: 78 std::shared_ptr<ISoundPool> soundPoolParallel_ = nullptr; 79 }; 80 81 class NumberCounter { 82 public: Increment()83 void Increment() 84 { 85 std::unique_lock<mutex> lock(numberMtx_); 86 number_++; 87 if (number_ >= targetNumber_) { 88 isNumReached_ = true; 89 numberCv_.notify_all(); 90 } 91 } Reset()92 void Reset() 93 { 94 std::unique_lock<mutex> lock(numberMtx_); 95 number_ = 0; 96 } GetNumber()97 int32_t GetNumber() 98 { 99 std::unique_lock<mutex> lock(numberMtx_); 100 return number_; 101 } WaitForCounter(int32_t target,std::chrono::seconds timeoutSec)102 bool WaitForCounter(int32_t target, std::chrono::seconds timeoutSec) 103 { 104 targetNumber_ = target; 105 std::unique_lock<mutex> lock(numberMtx_); 106 return numberCv_.wait_for(lock, timeoutSec, [this, target] { 107 return isNumReached_ || number_ >= target; 108 }); 109 } 110 private: 111 std::mutex numberMtx_; 112 std::condition_variable numberCv_; 113 int32_t number_ = 0; 114 int32_t targetNumber_ = INT32_MAX; 115 bool isNumReached_ = false; 116 }; 117 118 class SoundPoolCallbackTest : public ISoundPoolCallback, public NoCopyable { 119 public: 120 static const int32_t DEFAULT_MAX_WAIT_SECONDS = 20; SoundPoolCallbackTest(std::shared_ptr<SoundPoolMock> soundPool)121 SoundPoolCallbackTest(std::shared_ptr<SoundPoolMock> soundPool) 122 { 123 soundPool_ = soundPool; 124 } SoundPoolCallbackTest(std::shared_ptr<SoundPoolParallelMock> soundPoolParallel)125 SoundPoolCallbackTest(std::shared_ptr<SoundPoolParallelMock> soundPoolParallel) 126 { 127 soundPoolParallel_ = soundPoolParallel; 128 } ~SoundPoolCallbackTest()129 ~SoundPoolCallbackTest() 130 { 131 soundPool_ = nullptr; 132 soundPoolParallel_ = nullptr; 133 } 134 bool WaitLoadedSoundNum(int32_t target, int32_t seconds = DEFAULT_MAX_WAIT_SECONDS) 135 { 136 return soundCounter_.WaitForCounter(target, std::chrono::seconds(seconds)); 137 } GetHaveLoadedSoundNum()138 int32_t GetHaveLoadedSoundNum() 139 { 140 return soundCounter_.GetNumber(); 141 } ResetHaveLoadedSoundNum()142 void ResetHaveLoadedSoundNum() 143 { 144 cout << "Before ResetHaveLoadedSoundNum :" << GetHaveLoadedSoundNum() << endl; 145 soundCounter_.Reset(); 146 } 147 GetHavePlayedSoundNum()148 int32_t GetHavePlayedSoundNum() 149 { 150 cout << "GetHavePlayedSoundNum havePlayedSoundNumInner_:" << havePlayedSoundNumInner_ << endl; 151 return havePlayedSoundNumInner_; 152 } ResetHavePlayedSoundNum()153 void ResetHavePlayedSoundNum() 154 { 155 cout << "Before ResetHavePlayedSoundNum havePlayedSoundNumInner_:" << havePlayedSoundNumInner_ << endl; 156 havePlayedSoundNumInner_ = 0; 157 cout << "After ResetHavePlayedSoundNum havePlayedSoundNumInner_:" << havePlayedSoundNumInner_ << endl; 158 } 159 GetHavePlayedStreamID()160 std::vector<int32_t> GetHavePlayedStreamID() 161 { 162 return vector_; 163 } 164 ResetHavePlayedStreamID()165 void ResetHavePlayedStreamID() 166 { 167 vector_.clear(); 168 } 169 std::shared_ptr<SoundPoolMock> soundPool_ = nullptr; 170 std::shared_ptr<SoundPoolParallelMock> soundPoolParallel_ = nullptr; 171 NumberCounter soundCounter_; 172 void OnLoadCompleted(int32_t soundId) override; 173 void OnPlayFinished(int32_t streamID) override; 174 void OnError(int32_t errorCode) override; 175 176 private: 177 int32_t havePlayedSoundNumInner_ = 0; 178 std::vector<int32_t> vector_; 179 }; 180 181 class SoundPoolFrameWriteCallbackTest : public ISoundPoolFrameWriteCallback { OnFirstAudioFrameWritingCallback(uint64_t & latency)182 void OnFirstAudioFrameWritingCallback(uint64_t &latency) override 183 {} 184 }; 185 186 } // namespace Media 187 } // namespace OHOS 188 #endif