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 SOUND_ID_MANAGER_H
17 #define SOUND_ID_MANAGER_H
18 #include <atomic>
19 #include <map>
20 #include <mutex>
21 #include <deque>
22 #include "thread_pool.h"
23 #include "isoundpool.h"
24 #include "sound_parser.h"
25 
26 namespace OHOS {
27 namespace Media {
28 class SoundIDManager {
29 public:
30     SoundIDManager();
31     ~SoundIDManager();
32 
33     int32_t Load(std::string url);
34 
35     int32_t Load(int32_t fd, int64_t offset, int64_t length);
36     int32_t DoLoad(int32_t soundID);
37     int32_t DoParser();
38 
39     int32_t Unload(int32_t soundID);
40 
41     int32_t SetCallback(const std::shared_ptr<ISoundPoolCallback> &callback);
42 
43     std::shared_ptr<SoundParser> FindSoundParser(int32_t soundID) const;
44 
45 private:
46     int32_t InitThreadPool();
47 
48     std::mutex soundManagerLock_;
49     std::shared_ptr<ISoundPoolCallback> callback_ = nullptr;
50     int32_t nextSoundID_ = 0;
51     std::map<int32_t, std::shared_ptr<SoundParser>> soundParsers_;
52 
53     std::atomic<bool> isParsingThreadPoolStarted_;
54     std::unique_ptr<ThreadPool> soundParserThreadPool_;
55 
56     std::condition_variable queueSpaceValid_;
57     std::condition_variable queueDataValid_;
58     std::deque<int32_t> soundIDs_;
59     bool quitQueue_;
60 
61     static const int32_t invalidSoundIDFlag = -1;
62     static constexpr int32_t MAX_SOUND_ID_QUEUE = 128;
63     static constexpr int32_t WAIT_TIME_BEFORE_CLOSE_MS = 1000;
64     static constexpr size_t MAX_LOAD_NUM = 32;
65 };
66 } // namespace Media
67 } // namespace OHOS
68 #endif // SOUND_ID_MANAGER_H
69