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 #include "cpp/mutex.h" 26 #include "media_dfx.h" 27 28 namespace OHOS { 29 namespace Media { 30 class StreamIDManager : public std::enable_shared_from_this<StreamIDManager> { 31 public: 32 StreamIDManager(int32_t maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo); 33 ~StreamIDManager(); 34 35 int32_t Play(std::shared_ptr<SoundParser> soundParser, PlayParams playParameters); 36 37 std::shared_ptr<CacheBuffer> FindCacheBuffer(const int32_t streamID); 38 39 int32_t GetStreamIDBySoundID(const int32_t soundID); 40 41 int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback); 42 43 int32_t SetFrameWriteCallback(const std::shared_ptr<ISoundPoolFrameWriteCallback> &callback); 44 45 int32_t ReorderStream(int32_t streamID, int32_t priority); 46 47 int32_t ClearStreamIDInDeque(int32_t streamID); 48 49 private: 50 class CacheBufferCallBack : public ISoundPoolCallback { 51 public: CacheBufferCallBack(const std::weak_ptr<StreamIDManager> streamIDManager)52 explicit CacheBufferCallBack(const std::weak_ptr<StreamIDManager> streamIDManager) 53 : streamIDManagerInner_(streamIDManager) 54 { 55 (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN_SOUNDPOOL, "SoundPool", 56 "Construction StreamIDManager::CacheBufferCallBack"); 57 } 58 virtual ~CacheBufferCallBack() = default; OnLoadCompleted(int32_t soundID)59 void OnLoadCompleted(int32_t soundID) 60 { 61 (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN_SOUNDPOOL, "SoundPool", 62 "StreamIDManager::CacheBufferCallBack OnLoadCompleted"); 63 } OnPlayFinished()64 void OnPlayFinished() 65 { 66 if (!streamIDManagerInner_.expired()) { 67 streamIDManagerInner_.lock()->OnPlayFinished(); 68 (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN_SOUNDPOOL, "SoundPool", 69 "StreamIDManager::CacheBufferCallBack OnPlayFinished"); 70 } 71 } OnError(int32_t errorCode)72 void OnError(int32_t errorCode) 73 { 74 (void)HILOG_IMPL(LOG_CORE, LOG_INFO, LOG_DOMAIN_SOUNDPOOL, "SoundPool", 75 "StreamIDManager::CacheBufferCallBack OnError"); 76 } 77 78 private: 79 std::weak_ptr<StreamIDManager> streamIDManagerInner_; 80 }; 81 // audio render max concurrency count. 82 static constexpr int32_t MAX_PLAY_STREAMS_NUMBER = 32; 83 static constexpr int32_t MIN_PLAY_STREAMS_NUMBER = 1; 84 85 struct StreamIDAndPlayParamsInfo { 86 int32_t streamID; 87 PlayParams playParameters; 88 }; 89 90 int32_t InitThreadPool(); 91 int32_t SetPlay(const int32_t soundID, const int32_t streamID, const PlayParams playParameters); 92 int32_t AddPlayTask(const int32_t streamID, const PlayParams playParameters); 93 int32_t DoPlay(const int32_t streamID); 94 int32_t GetFreshStreamID(const int32_t soundID, PlayParams playParameters); 95 void OnPlayFinished(); 96 void QueueAndSortPlayingStreamID(int32_t streamID); 97 void QueueAndSortWillPlayStreamID(StreamIDAndPlayParamsInfo freshStreamIDAndPlayParamsInfo); 98 99 std::shared_ptr<ISoundPoolCallback> cacheBufferCallback_ = nullptr; 100 AudioStandard::AudioRendererInfo audioRendererInfo_; 101 ffrt::mutex streamIDManagerLock_; 102 std::shared_ptr<ISoundPoolCallback> callback_ = nullptr; 103 std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr; 104 std::map<int32_t, std::shared_ptr<CacheBuffer>> cacheBuffers_; 105 int32_t nextStreamID_ = 0; 106 int32_t maxStreams_ = MIN_PLAY_STREAMS_NUMBER; 107 108 std::atomic<bool> isStreamPlayingThreadPoolStarted_ = false; 109 std::unique_ptr<ThreadPool> streamPlayingThreadPool_; 110 111 std::deque<int32_t> streamIDs_; 112 std::deque<StreamIDAndPlayParamsInfo> willPlayStreamInfos_; 113 std::deque<int32_t> playingStreamIDs_; 114 }; 115 } // namespace Media 116 } // namespace OHOS 117 #endif // STREAM_ID_MANAGER_H 118