blob: a62ad8c8ed91381548b5697ad183983d54f10dd3 [file] [log] [blame]
hkuang26587cb2020-01-16 10:36:08 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_MEDIA_TRANSCODING_CLIENT_MANAGER_H
18#define ANDROID_MEDIA_TRANSCODING_CLIENT_MANAGER_H
19
Chong Zhang8e062632020-03-31 10:56:37 -070020#include <aidl/android/media/ITranscodingClient.h>
Chong Zhang6d58e4b2020-03-31 09:41:10 -070021#include <aidl/android/media/ITranscodingClientCallback.h>
hkuang26587cb2020-01-16 10:36:08 -080022#include <sys/types.h>
23#include <utils/Condition.h>
hkuang26587cb2020-01-16 10:36:08 -080024#include <utils/String8.h>
25#include <utils/Vector.h>
26
Chong Zhang3fa408f2020-04-30 11:04:28 -070027#include <map>
hkuang26587cb2020-01-16 10:36:08 -080028#include <mutex>
29#include <unordered_map>
Chong Zhang3fa408f2020-04-30 11:04:28 -070030#include <unordered_set>
hkuang26587cb2020-01-16 10:36:08 -080031
Chong Zhang6d58e4b2020-03-31 09:41:10 -070032#include "SchedulerClientInterface.h"
33
hkuang26587cb2020-01-16 10:36:08 -080034namespace android {
35
Chong Zhang8e062632020-03-31 10:56:37 -070036using ::aidl::android::media::ITranscodingClient;
Chong Zhang6d58e4b2020-03-31 09:41:10 -070037using ::aidl::android::media::ITranscodingClientCallback;
hkuang26587cb2020-01-16 10:36:08 -080038
39/*
40 * TranscodingClientManager manages all the transcoding clients across different processes.
41 *
Chong Zhang3fa408f2020-04-30 11:04:28 -070042 * TranscodingClientManager manages all the clients's registration/unregistration and clients'
43 * information. It also bookkeeps all the clients' information. It also monitors the death of the
hkuang26587cb2020-01-16 10:36:08 -080044 * clients. Upon client's death, it will remove the client from it.
45 *
46 * TODO(hkuang): Hook up with ResourceManager for resource management.
47 * TODO(hkuang): Hook up with MediaMetrics to log all the transactions.
48 */
Chong Zhang15c192a2020-05-05 16:24:00 -070049class TranscodingClientManager : public std::enable_shared_from_this<TranscodingClientManager> {
Chong Zhang8e062632020-03-31 10:56:37 -070050public:
Chong Zhang6d58e4b2020-03-31 09:41:10 -070051 virtual ~TranscodingClientManager();
hkuang26587cb2020-01-16 10:36:08 -080052
53 /**
54 * Adds a new client to the manager.
55 *
Chong Zhang6d58e4b2020-03-31 09:41:10 -070056 * The client must have valid callback, pid, uid, clientName and opPackageName.
57 * Otherwise, this will return a non-zero errorcode. If the client callback has
Chong Zhang8e062632020-03-31 10:56:37 -070058 * already been added, it will also return non-zero errorcode.
hkuang26587cb2020-01-16 10:36:08 -080059 *
Chong Zhang6d58e4b2020-03-31 09:41:10 -070060 * @param callback client callback for the service to call this client.
Chong Zhang8e062632020-03-31 10:56:37 -070061 * @param pid client's process id.
62 * @param uid client's user id.
63 * @param clientName client's name.
64 * @param opPackageName client's package name.
65 * @param client output holding the ITranscodingClient interface for the client
66 * to use for subsequent communications with the service.
hkuang26587cb2020-01-16 10:36:08 -080067 * @return 0 if client is added successfully, non-zero errorcode otherwise.
68 */
Chong Zhang6d58e4b2020-03-31 09:41:10 -070069 status_t addClient(const std::shared_ptr<ITranscodingClientCallback>& callback, pid_t pid,
70 uid_t uid, const std::string& clientName, const std::string& opPackageName,
71 std::shared_ptr<ITranscodingClient>* client);
Chong Zhang8e062632020-03-31 10:56:37 -070072
73 /**
74 * Gets the number of clients.
75 */
76 size_t getNumOfClients() const;
77
78 /**
79 * Dump all the client information to the fd.
80 */
81 void dumpAllClients(int fd, const Vector<String16>& args);
82
83private:
84 friend class MediaTranscodingService;
85 friend class TranscodingClientManagerTest;
Chong Zhang8e062632020-03-31 10:56:37 -070086 struct ClientImpl;
87
Chong Zhang6d58e4b2020-03-31 09:41:10 -070088 // Only allow MediaTranscodingService and unit tests to instantiate.
89 TranscodingClientManager(const std::shared_ptr<SchedulerClientInterface>& scheduler);
Chong Zhang8e062632020-03-31 10:56:37 -070090
91 /**
hkuang26587cb2020-01-16 10:36:08 -080092 * Removes an existing client from the manager.
93 *
94 * If the client does not exist, this will return non-zero errorcode.
95 *
96 * @param clientId id of the client to be removed..
97 * @return 0 if client is removed successfully, non-zero errorcode otherwise.
98 */
Chong Zhang8e062632020-03-31 10:56:37 -070099 status_t removeClient(ClientIdType clientId);
hkuang26587cb2020-01-16 10:36:08 -0800100
hkuang9c04b8d2020-01-22 10:03:21 -0800101 static void BinderDiedCallback(void* cookie);
102
hkuang26587cb2020-01-16 10:36:08 -0800103 mutable std::mutex mLock;
Chong Zhang8e062632020-03-31 10:56:37 -0700104 std::unordered_map<ClientIdType, std::shared_ptr<ClientImpl>> mClientIdToClientMap
hkuang26587cb2020-01-16 10:36:08 -0800105 GUARDED_BY(mLock);
Chong Zhang3fa408f2020-04-30 11:04:28 -0700106 std::unordered_set<uintptr_t> mRegisteredCallbacks GUARDED_BY(mLock);
hkuang26587cb2020-01-16 10:36:08 -0800107
hkuang26587cb2020-01-16 10:36:08 -0800108 ::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700109
110 std::shared_ptr<SchedulerClientInterface> mJobScheduler;
Chong Zhang3fa408f2020-04-30 11:04:28 -0700111
112 static std::atomic<ClientIdType> sCookieCounter;
113 static std::mutex sCookie2ClientLock;
114 static std::map<ClientIdType, std::shared_ptr<ClientImpl>> sCookie2Client
115 GUARDED_BY(sCookie2ClientLock);
hkuang26587cb2020-01-16 10:36:08 -0800116};
117
118} // namespace android
119#endif // ANDROID_MEDIA_TRANSCODING_SERVICE_H