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         virtual ~CacheBufferCallBack() = default;
55         void OnLoadCompleted(int32_t soundID);
56         void OnPlayFinished();
57         void OnError(int32_t errorCode);
58 
59     private:
60         std::weak_ptr<StreamIDManager> streamIDManagerInner_;
61     };
62     // audio render max concurrency count.
63     static constexpr int32_t MAX_PLAY_STREAMS_NUMBER = 32;
64     static constexpr int32_t MIN_PLAY_STREAMS_NUMBER = 1;
65     static constexpr int32_t CACHE_BUFFER_THREAD_NUMBER = 1;
66 
67     struct StreamIDAndPlayParamsInfo {
68         int32_t streamID;
69         PlayParams playParameters;
70     };
71 
72     int32_t InitThreadPool();
73     int32_t SetPlay(const int32_t soundID, const int32_t streamID, const PlayParams playParameters);
74     int32_t AddPlayTask(const int32_t streamID, const PlayParams playParameters);
75     int32_t DoPlay(const int32_t streamID);
76     int32_t GetFreshStreamID(const int32_t soundID, PlayParams playParameters);
77     void OnPlayFinished();
78     void QueueAndSortPlayingStreamID(int32_t streamID);
79     void QueueAndSortWillPlayStreamID(StreamIDAndPlayParamsInfo freshStreamIDAndPlayParamsInfo);
80 
81     std::shared_ptr<ISoundPoolCallback> cacheBufferCallback_ = nullptr;
82     AudioStandard::AudioRendererInfo audioRendererInfo_;
83     ffrt::mutex streamIDManagerLock_;
84     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
85     std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr;
86     std::map<int32_t, std::shared_ptr<CacheBuffer>> cacheBuffers_;
87     int32_t nextStreamID_ = 0;
88     int32_t maxStreams_ = MIN_PLAY_STREAMS_NUMBER;
89 
90     std::atomic<bool> isStreamPlayingThreadPoolStarted_ = false;
91     std::unique_ptr<ThreadPool> streamPlayingThreadPool_;
92     std::atomic<bool> isCacheBufferStopThreadPoolStarted_ = false;
93     std::shared_ptr<ThreadPool> cacheBufferStopThreadPool_;
94 
95     std::deque<int32_t> streamIDs_;
96     std::deque<StreamIDAndPlayParamsInfo> willPlayStreamInfos_;
97     std::deque<int32_t> playingStreamIDs_;
98 };
99 } // namespace Media
100 } // namespace OHOS
101 #endif // STREAM_ID_MANAGER_H
102