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 <unordered_set> 24 #include "iremote_object.h" 25 #include "ipc_skeleton.h" 26 #include "nocopyable.h" 27 28 namespace OHOS { 29 namespace MediaAVCodec { 30 using DumperEntry = std::function<int32_t(int32_t)>; 31 struct Dumper { 32 pid_t pid_; 33 pid_t uid_; 34 DumperEntry entry_; 35 sptr<IRemoteObject> remoteObject_; 36 }; 37 38 class AVCodecServerManager : public NoCopyable { 39 public: 40 static AVCodecServerManager& GetInstance(); 41 ~AVCodecServerManager(); 42 43 enum StubType { CODECLIST, CODEC, MUXER, DEMUXER, SOURCE }; 44 sptr<IRemoteObject> CreateStubObject(StubType type); 45 void DestroyStubObject(StubType type, sptr<IRemoteObject> object); 46 void DestroyStubObjectForPid(pid_t pid); 47 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args); 48 void DestroyDumper(StubType type, sptr<IRemoteObject> object); 49 void DestroyDumperForPid(pid_t pid); 50 51 private: 52 AVCodecServerManager(); 53 void PrintDumpMenu(int32_t fd); 54 void DumpServer(int32_t fd, StubType stubType, std::unordered_set<std::u16string> &argSets); 55 56 #ifdef SUPPORT_DEMUXER 57 sptr<IRemoteObject> CreateDemuxerStubObject(); 58 #endif 59 60 #ifdef SUPPORT_CODEC 61 sptr<IRemoteObject> CreateCodecStubObject(); 62 #endif 63 #ifdef SUPPORT_CODECLIST 64 sptr<IRemoteObject> CreateCodecListStubObject(); 65 #endif 66 67 #ifdef SUPPORT_SOURCE 68 sptr<IRemoteObject> CreateSourceStubObject(); 69 #endif 70 void EraseObject(std::map<sptr<IRemoteObject>, pid_t>::iterator& iter, 71 std::map<sptr<IRemoteObject>, pid_t>& stubMap, 72 pid_t pid, 73 const std::string& stubName); 74 void EraseObject(std::map<sptr<IRemoteObject>, pid_t>& stubMap, pid_t pid); 75 76 class AsyncExecutor { 77 public: 78 AsyncExecutor() = default; 79 virtual ~AsyncExecutor() = default; 80 void Commit(sptr<IRemoteObject> obj); 81 void Clear(); 82 83 private: 84 void HandleAsyncExecution(); 85 std::list<sptr<IRemoteObject>> freeList_; 86 std::mutex listMutex_; 87 }; 88 89 std::map<sptr<IRemoteObject>, pid_t> demuxerStubMap_; 90 std::map<sptr<IRemoteObject>, pid_t> codecStubMap_; 91 std::map<sptr<IRemoteObject>, pid_t> codecListStubMap_; 92 std::map<sptr<IRemoteObject>, pid_t> sourceStubMap_; 93 std::map<StubType, std::vector<Dumper>> dumperTbl_; 94 AsyncExecutor executor_; 95 96 std::mutex mutex_; 97 }; 98 } // namespace MediaAVCodec 99 } // namespace OHOS 100 #endif // AVCODEC_SERVER_MANAGER_H