• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 PARALLEL_STREAM_MANAGER_H
16 #define PARALLEL_STREAM_MANAGER_H
17 
18 #include <atomic>
19 #include <thread>
20 #include <vector>
21 #include "cache_buffer.h"
22 #include "stream.h"
23 #include "isoundpool.h"
24 #include "sound_parser.h"
25 #include "thread_pool.h"
26 #include "cpp/mutex.h"
27 #include "media_dfx.h"
28 
29 namespace OHOS {
30 namespace Media {
31 class Stream;
32 
33 class ParallelStreamManager : public std::enable_shared_from_this<ParallelStreamManager> {
34 public:
35     ParallelStreamManager(int32_t maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo);
36     ~ParallelStreamManager();
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<SoundParser> soundParser, PlayParams &playParameters);
44     int32_t UnloadStream(int32_t soundId);
45     void ReorderStream();
46     std::shared_ptr<Stream> FindStreamLock(const int32_t streamId);
47     int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
48     int32_t SetFrameWriteCallback(const std::shared_ptr<ISoundPoolFrameWriteCallback> &callback);
49 
50 private:
51     class StreamCallBack : public ISoundPoolCallback {
52     public:
StreamCallBack(const std::weak_ptr<ParallelStreamManager> & parallelStreamManager)53         explicit StreamCallBack(const std::weak_ptr<ParallelStreamManager> &parallelStreamManager)
54             : parallelStreamManagerInner_(parallelStreamManager) {}
55         virtual ~StreamCallBack() = default;
56         void OnLoadCompleted(int32_t soundId);
57         void OnPlayFinished(int32_t streamId);
58         void OnError(int32_t errorCode);
59 
60     private:
61         std::weak_ptr<ParallelStreamManager> parallelStreamManagerInner_;
62     };
63 
64     int32_t PreparePlay(std::shared_ptr<Stream> stream, bool waitQueueFlag);
65     int32_t DoPlay(int32_t streamID);
66     void DealQueueAndAddTask(int32_t streamId, std::shared_ptr<Stream> stream, bool waitQueueFlag);
67     void AddToPlayingDeque(int32_t streamID, std::shared_ptr<Stream> stream);
68     void AddToWaitingDeque(int32_t streamID, std::shared_ptr<Stream> stream);
69     void RemoveFromWaitingDeque(int32_t streamId);
70     void OnPlayFinished(int32_t streamID);
71     std::shared_ptr<Stream> FindStream(const int32_t streamId);
72 
73     // pair<int32_t, int32_t> is mapping between SoundId and GlobalId
74     std::vector<std::pair<int32_t, int32_t>> globalIdVector_;
75     std::mutex globalIdMutex_;
76     AudioStandard::AudioRendererInfo audioRendererInfo_;
77     int32_t maxStreams_ = 1;
78     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
79     std::shared_ptr<ISoundPoolFrameWriteCallback> frameWriteCallback_ = nullptr;
80     std::deque<std::pair<int32_t, std::shared_ptr<Stream>>> playingStream_;
81     std::deque<std::pair<int32_t, std::shared_ptr<Stream>>> waitingStream_;
82     int32_t nextStreamId_ = 0;
83     ffrt::mutex parallelStreamManagerLock_;
84     std::shared_ptr<ThreadPool> streamPlayThreadPool_;
85     std::shared_ptr<ThreadPool> streamStopThreadPool_;
86     std::shared_ptr<ThreadPool> waitPlayThreadPool_;
87 };
88 } // namespace Media
89 } // namespace OHOS
90 #endif // PARALLEL_STREAM_MANAGER_H
91