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