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