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