1 /*
2  * Copyright (C) 2023 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 AVCODEC_SERVER_MANAGER_H
17 #define AVCODEC_SERVER_MANAGER_H
18 
19 #include <memory>
20 #include <functional>
21 #include <map>
22 #include <list>
23 #include "iremote_object.h"
24 #include "ipc_skeleton.h"
25 #include "nocopyable.h"
26 
27 namespace OHOS {
28 namespace MediaAVCodec {
29 class AVCodecServerManager : public NoCopyable {
30 public:
31     static AVCodecServerManager& GetInstance();
32     ~AVCodecServerManager();
33 
34     enum StubType { CODECLIST, CODEC };
35     int32_t CreateStubObject(StubType type, sptr<IRemoteObject> &object);
36     void DestroyStubObject(StubType type, sptr<IRemoteObject> object);
37     void DestroyStubObjectForPid(pid_t pid);
38     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args);
39     void NotifyProcessStatus(const int32_t status);
40     void SetMemMgrStatus(const bool isStarted);
41     void SetCritical(const bool isKeyService);
42 
43 private:
44     AVCodecServerManager();
45 
46 #ifdef SUPPORT_CODEC
47     int32_t CreateCodecStubObject(sptr<IRemoteObject> &object);
48 #endif
49 #ifdef SUPPORT_CODECLIST
50     int32_t CreateCodecListStubObject(sptr<IRemoteObject> &object);
51 #endif
52 
53     void EraseObject(std::map<sptr<IRemoteObject>, pid_t>::iterator& iter,
54                      std::map<sptr<IRemoteObject>, pid_t>& stubMap,
55                      pid_t pid,
56                      const std::string& stubName);
57     void EraseObject(std::map<sptr<IRemoteObject>, pid_t>& stubMap, pid_t pid);
58     void Init();
59 
60     class AsyncExecutor {
61     public:
62         AsyncExecutor() = default;
63         virtual ~AsyncExecutor() = default;
64         void Commit(sptr<IRemoteObject> obj);
65         void Clear();
66 
67     private:
68         void HandleAsyncExecution();
69         std::list<sptr<IRemoteObject>> freeList_;
70         std::mutex listMutex_;
71     };
72 
73     std::map<sptr<IRemoteObject>, pid_t> codecStubMap_;
74     std::map<sptr<IRemoteObject>, pid_t> codecListStubMap_;
75     AsyncExecutor executor_;
76     pid_t pid_ = 0;
77     std::mutex mutex_;
78     using NotifyProcessStatusFunc = int32_t(*)(int32_t pid, int32_t type, int32_t status, int32_t saId);
79     using SetCriticalFunc = int32_t(*)(int32_t pid, bool critical, int32_t saId);
80     static constexpr char LIB_PATH[] = "libmemmgrclient.z.so";
81     static constexpr char NOTIFY_STATUS_FUNC_NAME[] = "notify_process_status";
82     static constexpr char SET_CRITICAL_FUNC_NAME[] = "set_critical";
83     std::shared_ptr<void> libMemMgrClientHandle_ = nullptr;
84     NotifyProcessStatusFunc notifyProcessStatusFunc_ = nullptr;
85     SetCriticalFunc setCriticalFunc_ = nullptr;
86     std::atomic<bool> memMgrStarted_ = false;
87 };
88 } // namespace MediaAVCodec
89 } // namespace OHOS
90 #endif // AVCODEC_SERVER_MANAGER_H