1 /*
2  * Copyright (C) 2021 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 MEDIA_SERVER_MANAGER_H
17 #define MEDIA_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 Media {
29 using DumperEntry = std::function<int32_t(int32_t)>;
30 struct Dumper {
31     pid_t pid_;
32     pid_t uid_;
33     DumperEntry entry_;
34     sptr<IRemoteObject> remoteObject_;
35 };
36 
37 class MediaServerManager : public NoCopyable {
38 public:
39     static MediaServerManager &GetInstance();
40     ~MediaServerManager();
41 
42     enum StubType {
43         RECORDER = 0,
44         PLAYER,
45         AVMETADATAHELPER,
46         AVCODECLIST,
47         AVCODEC,
48         RECORDERPROFILES,
49         MONITOR,
50         SCREEN_CAPTURE,
51         SCREEN_CAPTURE_CONTROLLER,
52         TRANSCODER,
53         SCREEN_CAPTURE_MONITOR
54     };
55     sptr<IRemoteObject> CreateStubObject(StubType type);
56     void DestroyStubObject(StubType type, sptr<IRemoteObject> object);
57     void DestroyStubObjectForPid(pid_t pid);
58     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args);
59     void DestroyDumper(StubType type, sptr<IRemoteObject> object);
60     void DestroyDumperForPid(pid_t pid);
61     void NotifyMemMgrLoaded();
62 
63 private:
64     MediaServerManager();
65 #ifdef SUPPORT_PLAYER
66     sptr<IRemoteObject> CreatePlayerStubObject();
67 #endif
68 #ifdef SUPPORT_RECORDER
69     sptr<IRemoteObject> CreateRecorderStubObject();
70     sptr<IRemoteObject> CreateRecorderProfilesStubObject();
71 #endif
72 #ifdef SUPPORT_TRANSCODER
73     sptr<IRemoteObject> CreateTransCoderStubObject();
74 #endif
75 #ifdef SUPPORT_METADATA
76     sptr<IRemoteObject> CreateAVMetadataHelperStubObject();
77 #endif
78 #ifdef SUPPORT_SCREEN_CAPTURE
79     sptr<IRemoteObject> CreateScreenCaptureStubObject();
80     sptr<IRemoteObject> CreateScreenCaptureMonitorStubObject();
81     sptr<IRemoteObject> CreateScreenCaptureControllerStubObject();
82 #endif
83     sptr<IRemoteObject> GetMonitorStubObject();
84 
85     void DestroyAVCodecStub(StubType type, sptr<IRemoteObject> object, pid_t pid);
86     void DestroyAVPlayerStub(StubType type, sptr<IRemoteObject> object, pid_t pid);
87     void DestroyAVRecorderStub(StubType type, sptr<IRemoteObject> object, pid_t pid);
88     void DestroyAVTransCoderStub(StubType type, sptr<IRemoteObject> object, pid_t pid);
89     void DestroyAVScreenCaptureStub(StubType type, sptr<IRemoteObject> object, pid_t pid);
90     void DestroyAVCodecStubForPid(pid_t pid);
91     void DestroyAVPlayerStubForPid(pid_t pid);
92     void DestroyAVRecorderStubForPid(pid_t pid);
93     void DestroyAVTranscoderStubForPid(pid_t pid);
94     void DestroyAVScreenCaptureStubForPid(pid_t pid);
95 
96     std::atomic<bool> isMemMgrLoaded_ {false};
97 
98     class AsyncExecutor {
99     public:
100         AsyncExecutor() = default;
101         virtual ~AsyncExecutor() = default;
102         void Commit(sptr<IRemoteObject> obj);
103         void Clear();
104     private:
105         void HandleAsyncExecution();
106         std::list<sptr<IRemoteObject>> freeList_;
107         std::mutex listMutex_;
108     };
109     std::map<sptr<IRemoteObject>, pid_t> recorderStubMap_;
110     std::map<sptr<IRemoteObject>, pid_t> transCoderStubMap_;
111     std::map<sptr<IRemoteObject>, pid_t> playerStubMap_;
112     std::map<sptr<IRemoteObject>, pid_t> avMetadataHelperStubMap_;
113     std::map<sptr<IRemoteObject>, pid_t> avCodecListStubMap_;
114     std::map<sptr<IRemoteObject>, pid_t> avCodecStubMap_;
115     std::map<sptr<IRemoteObject>, pid_t> recorderProfilesStubMap_;
116     std::map<sptr<IRemoteObject>, pid_t> screenCaptureStubMap_;
117     std::map<sptr<IRemoteObject>, pid_t> screenCaptureMonitorStubMap_;
118     std::map<sptr<IRemoteObject>, pid_t> screenCaptureControllerStubMap_;
119     std::map<StubType, std::vector<Dumper>> dumperTbl_;
120     AsyncExecutor executor_;
121 
122     std::mutex mutex_;
123 };
124 } // namespace Media
125 } // namespace OHOS
126 #endif // MEDIA_SERVER_MANAGER_H