• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
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 PLAYER_MEM_MANAGE_H
16 #define PLAYER_MEM_MANAGE_H
17 
18 #include <mutex>
19 #include <vector>
20 #include <unordered_map>
21 #include <chrono>
22 #include "task_queue.h"
23 #include "app_state_listener.h"
24 
25 namespace OHOS {
26 namespace Media {
27 using ResetForMemManageRecall = std::function<void()>;
28 using RecoverByMemManageRecall = std::function<void()>;
29 struct MemManageRecall {
30     ResetForMemManageRecall resetFrontGroundRecall;
31     ResetForMemManageRecall resetBackGroundRecall;
32     ResetForMemManageRecall resetMemmgrRecall;
33     RecoverByMemManageRecall recoverRecall;
34     void *signAddr;
35 };
36 class PlayerMemManage {
37 public:
38     virtual ~PlayerMemManage();
39 
40     static PlayerMemManage& GetInstance();
41     int32_t RegisterPlayerServer(int32_t uid, int32_t pid, const MemManageRecall &memRecallStruct);
42     int32_t DeregisterPlayerServer(const MemManageRecall &memRecallStruct);
43     int32_t HandleForceReclaim(int32_t uid, int32_t pid);
44     int32_t HandleOnTrim(Memory::SystemMemoryLevel level);
45     int32_t RecordAppState(int32_t uid, int32_t pid, int32_t state);
46     void HandleOnConnected();
47     void HandleOnDisconnected();
48     void HandleOnRemoteDied(const wptr<IRemoteObject> &object);
49 
50 private:
51     struct AppPlayerInfo {
52         std::vector<MemManageRecall> memRecallStructVec;
53         int32_t appState;
54         std::chrono::steady_clock::time_point appEnterFrontTime;
55         std::chrono::steady_clock::time_point appEnterBackTime;
56     };
57     PlayerMemManage();
58     bool Init();
59     void ProbeTask();
60     void HandleOnTrimLevelLow();
61     void FindBackGroundPlayerFromVec(AppPlayerInfo &appPlayerInfo);
62     void FindFrontGroundPlayerFromVec(AppPlayerInfo &appPlayerInfo);
63     void FindProbeTaskPlayer();
64     void FindDeregisterPlayerFromVec(bool &isFind, AppPlayerInfo &appPlayerInfo,
65         const MemManageRecall &memRecallStruct);
66     void AwakeFrontGroundAppMedia(AppPlayerInfo &appPlayerInfo);
67     void SetAppPlayerInfo(AppPlayerInfo &appPlayerInfo, int32_t state);
68     void RemoteDieAgainRegisterActiveApps();
69     static void WritePurgeableEvent(int32_t level, int32_t useTime);
70 
71     bool isParsed_ = false;
72     std::recursive_mutex recMutex_;
73     std::recursive_mutex recTaskMutex_;
74     std::shared_ptr<AppStateListener> appStateListener_;
75     bool isAppStateListenerConnected_ = false;
76     bool isAppStateListenerRemoteDied_ = false;
77     using PidPlayersInfo = std::unordered_map<int32_t, AppPlayerInfo>;
78     std::unordered_map<int32_t, PidPlayersInfo> playerManage_;
79     std::unique_ptr<TaskQueue> probeTaskQueue_;
80     bool isProbeTaskCreated_ = false;
81 };
82 }
83 }
84 #endif // PLAYER_MEM_MANAGE_H
85