• 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 
16 #ifndef OHOS_MEDIA_NOTIFY_TASK_WORKER_H
17 #define OHOS_MEDIA_NOTIFY_TASK_WORKER_H
18 
19 #include "notify_info_inner.h"
20 #include "media_change_info.h"
21 #include "notify_info.h"
22 #include "media_change_info.h"
23 
24 #include <atomic>
25 #include <mutex>
26 #include <string>
27 #include <unordered_map>
28 #include <vector>
29 #include <condition_variable>
30 
31 namespace OHOS {
32 namespace Media {
33 namespace Notification {
34 #define EXPORT __attribute__ ((visibility ("default")))
35 struct NotifyTaskInfo {
36     int32_t loopedCnt_{0};
37     std::vector<NotifyInfoInner> notifyInfos;
38 };
39 
40 class NotifyTaskWorker {
41 public:
42     EXPORT NotifyTaskWorker();
43     EXPORT ~NotifyTaskWorker();
44     EXPORT static std::shared_ptr<NotifyTaskWorker> GetInstance();
45     EXPORT void StartWorker();
46     EXPORT void AddTaskInfo(NotifyInfoInner notifyInfoInner);
47     EXPORT bool IsRunning();
48 
49 private:
50     EXPORT void HandleNotifyTaskPeriod();
51     EXPORT void HandleNotifyTask();
52     std::vector<MediaChangeInfo> ClassifyNotifyInfo(std::vector<NotifyTaskInfo> &notifyTaskInfos);
53     EXPORT void WaitForTask();
54     EXPORT bool IsTaskInfosEmpty();
55     EXPORT std::vector<NotifyTaskInfo> GetCurrentNotifyMap();
56     EXPORT std::vector<NotifyInfo> MergeNotifyInfo(std::vector<MediaChangeInfo> changeInfos);
57     EXPORT void DistributeNotifyInfo(std::vector<NotifyInfo> notifyInfos);
58 
59 private:
60     EXPORT static std::unordered_map<int32_t, NotifyTaskInfo> taskInfos_;
61     EXPORT static std::shared_ptr<NotifyTaskWorker> notifyTaskWorker_;
62     EXPORT static std::mutex instanceMtx_;
63     EXPORT std::atomic<bool> isThreadRunning_{false};
64     EXPORT std::mutex workLock_;
65     EXPORT int32_t noTaskTims_ {0};
66     EXPORT std::condition_variable workCv_;
67     EXPORT static std::mutex mapMutex_;
68 };
69 } // namespace Notification
70 } // namespace Media
71 } // namespace OHOS
72 #endif // OHOS_MEDIA_NOTIFY_H