1/*
2 * Copyright (C) 2015 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 DRM_SESSION_MANAGER_H_
18
19#define DRM_SESSION_MANAGER_H_
20
21#include <media/stagefright/foundation/ABase.h>
22#include <utils/RefBase.h>
23#include <utils/KeyedVector.h>
24#include <utils/threads.h>
25#include <utils/Vector.h>
26
27namespace android {
28
29class DrmSessionManagerTest;
30struct DrmSessionClientInterface;
31struct ProcessInfoInterface;
32
33bool isEqualSessionId(const Vector<uint8_t> &sessionId1, const Vector<uint8_t> &sessionId2);
34
35struct SessionInfo {
36    sp<DrmSessionClientInterface> drm;
37    Vector<uint8_t> sessionId;
38    int64_t timeStamp;
39};
40
41typedef Vector<SessionInfo > SessionInfos;
42typedef KeyedVector<int, SessionInfos > PidSessionInfosMap;
43
44struct DrmSessionManager : public RefBase {
45    static sp<DrmSessionManager> Instance();
46
47    DrmSessionManager();
48    explicit DrmSessionManager(sp<ProcessInfoInterface> processInfo);
49
50    void addSession(int pid, const sp<DrmSessionClientInterface>& drm, const Vector<uint8_t>& sessionId);
51    void useSession(const Vector<uint8_t>& sessionId);
52    void removeSession(const Vector<uint8_t>& sessionId);
53    void removeDrm(const sp<DrmSessionClientInterface>& drm);
54    bool reclaimSession(int callingPid);
55
56protected:
57    virtual ~DrmSessionManager();
58
59private:
60    friend class DrmSessionManagerTest;
61
62    int64_t getTime_l();
63    bool getLowestPriority_l(int* lowestPriorityPid, int* lowestPriority);
64    bool getLeastUsedSession_l(
65            int pid, sp<DrmSessionClientInterface>* drm, Vector<uint8_t>* sessionId);
66
67    sp<ProcessInfoInterface> mProcessInfo;
68    mutable Mutex mLock;
69    PidSessionInfosMap mSessionMap;
70    int64_t mTime;
71
72    DISALLOW_EVIL_CONSTRUCTORS(DrmSessionManager);
73};
74
75}  // namespace android
76
77#endif  // DRM_SESSION_MANAGER_H_
78