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 void SetInstanceInfoByInstanceId(int32_t instanceId, const InstanceInfo &info); 51 52 private: 53 AVCodecServerManager(); 54 55 #ifdef SUPPORT_CODEC 56 int32_t CreateCodecStubObject(sptr<IRemoteObject> &object); 57 #endif 58 #ifdef SUPPORT_CODECLIST 59 int32_t CreateCodecListStubObject(sptr<IRemoteObject> &object); 60 #endif 61 62 void EraseObject(std::map<sptr<IRemoteObject>, pid_t> &stubMap, pid_t pid); 63 void EraseCodecObjectByPid(pid_t pid); 64 void Init(); 65 66 class AsyncExecutor { 67 public: 68 AsyncExecutor() = default; 69 virtual ~AsyncExecutor() = default; 70 void Commit(sptr<IRemoteObject> obj); 71 void Clear(); 72 73 private: 74 void HandleAsyncExecution(); 75 std::list<sptr<IRemoteObject>> freeList_; 76 std::mutex listMutex_; 77 }; 78 79 std::unordered_multimap<pid_t, CodecInstance> codecStubMap_; 80 std::map<sptr<IRemoteObject>, pid_t> codecListStubMap_; 81 AsyncExecutor executor_; 82 pid_t pid_ = 0; 83 std::shared_mutex mutex_; 84 using NotifyProcessStatusFunc = int32_t(*)(int32_t pid, int32_t type, int32_t status, int32_t saId); 85 using SetCriticalFunc = int32_t(*)(int32_t pid, bool critical, int32_t saId); 86 static constexpr char LIB_PATH[] = "libmemmgrclient.z.so"; 87 static constexpr char NOTIFY_STATUS_FUNC_NAME[] = "notify_process_status"; 88 static constexpr char SET_CRITICAL_FUNC_NAME[] = "set_critical"; 89 std::shared_ptr<void> libMemMgrClientHandle_ = nullptr; 90 NotifyProcessStatusFunc notifyProcessStatusFunc_ = nullptr; 91 SetCriticalFunc setCriticalFunc_ = nullptr; 92 std::atomic<bool> memMgrStarted_ = false; 93 }; 94 } // namespace MediaAVCodec 95 } // namespace OHOS 96 #endif // AVCODEC_SERVER_MANAGER_H