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