1 2 /* 3 * Copyright (C) 2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef STREAM_ID_MANAGER_H 17 #define STREAM_ID_MANAGER_H 18 19 #include <atomic> 20 #include <thread> 21 #include "cache_buffer.h" 22 #include "isoundpool.h" 23 #include "sound_parser.h" 24 #include "thread_pool.h" 25 26 namespace OHOS { 27 namespace Media { 28 class StreamIDManager : public std::enable_shared_from_this<StreamIDManager> { 29 public: 30 StreamIDManager(int32_t maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo); 31 ~StreamIDManager(); 32 33 int32_t Play(std::shared_ptr<SoundParser> soundParser, PlayParams playParameters); 34 35 std::shared_ptr<CacheBuffer> FindCacheBuffer(const int32_t streamID); 36 37 int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 38 39 int32_t ReorderStream(int32_t streamID, int32_t priority); 40 41 private: 42 class CacheBufferCallBack : public ISoundPoolCallback { 43 public: CacheBufferCallBack(const std::weak_ptr<StreamIDManager> streamIDManager)44 explicit CacheBufferCallBack(const std::weak_ptr<StreamIDManager> streamIDManager) 45 : streamIDManagerInner_(streamIDManager) 46 { 47 MEDIA_INFO_LOG("Construction StreamIDManager::SoundPoolCallBack"); 48 } 49 virtual ~CacheBufferCallBack() = default; OnLoadCompleted(int32_t soundID)50 void OnLoadCompleted(int32_t soundID) 51 { 52 MEDIA_INFO_LOG("StreamIDManager::SoundPoolCallBack OnLoadCompleted"); 53 } OnPlayFinished()54 void OnPlayFinished() 55 { 56 if (!streamIDManagerInner_.expired()) { 57 streamIDManagerInner_.lock()->OnPlayFinished(); 58 } 59 } OnError(int32_t errorCode)60 void OnError(int32_t errorCode) 61 { 62 MEDIA_INFO_LOG("StreamIDManager::SoundPoolCallBack OnError"); 63 } 64 65 private: 66 std::weak_ptr<StreamIDManager> streamIDManagerInner_; 67 }; 68 // audio render max concurrency count. 69 static constexpr int32_t MAX_PLAY_STREAMS_NUMBER = 16; 70 static constexpr int32_t MIN_PLAY_STREAMS_NUMBER = 1; 71 72 struct StreamIDAndPlayParamsInfo { 73 int32_t streamID; 74 PlayParams playParameters; 75 }; 76 77 int32_t InitThreadPool(); 78 int32_t SetPlay(const int32_t soundID, const int32_t streamID, const PlayParams playParameters); 79 int32_t AddPlayTask(const int32_t streamID, const PlayParams playParameters); 80 int32_t DoPlay(const int32_t streamID); 81 int32_t GetFreshStreamID(const int32_t soundID, PlayParams playParameters); 82 void OnPlayFinished(); 83 void QueueAndSortPlayingStreamID(int32_t streamID); 84 void QueueAndSortWillPlayStreamID(StreamIDAndPlayParamsInfo freshStreamIDAndPlayParamsInfo); 85 86 std::shared_ptr<ISoundPoolCallback> cacheBufferCallback_ = nullptr; 87 AudioStandard::AudioRendererInfo audioRendererInfo_; 88 std::mutex streamIDManagerLock_; 89 std::shared_ptr<ISoundPoolCallback> callback_ = nullptr; 90 std::map<int32_t, std::shared_ptr<CacheBuffer>> cacheBuffers_; 91 int32_t nextStreamID_ = 0; 92 int32_t maxStreams_ = MIN_PLAY_STREAMS_NUMBER; 93 size_t currentTaskNum_ = 0; 94 95 std::atomic<bool> isStreamPlayingThreadPoolStarted_ = false; 96 std::unique_ptr<ThreadPool> streamPlayingThreadPool_; 97 98 std::deque<int32_t> streamIDs_; 99 std::deque<StreamIDAndPlayParamsInfo> willPlayStreamInfos_; 100 std::deque<int32_t> playingStreamIDs_; 101 }; 102 } // namespace Media 103 } // namespace OHOS 104 #endif // STREAM_ID_MANAGER_H 105