blob: 5cfd7c0c69fe6d4580bc6ec81b21098df06554f4 [file] [log] [blame]
Robert Shih28c2ed32019-10-27 22:55:12 -07001/*
2 * Copyright (C) 2019 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//#define LOG_NDEBUG 0
18#define LOG_TAG "DrmUtils"
19
20#include <utils/String16.h>
21#include <binder/IInterface.h>
22#include <binder/IServiceManager.h>
23#include <cutils/properties.h>
24
25#include <mediadrm/CryptoHal.h>
26#include <mediadrm/DrmHal.h>
27#include <mediadrm/DrmUtils.h>
28#include <mediadrm/ICrypto.h>
29#include <mediadrm/IDrm.h>
30#include <mediadrm/IMediaDrmService.h>
31
32namespace android {
33namespace DrmUtils {
34
35namespace {
36template<typename Iface>
37sp<Iface> MakeObjectWithService(status_t *pstatus) {
38 status_t err = OK;
39 status_t &status = pstatus ? *pstatus : err;
40 sp<IServiceManager> sm = defaultServiceManager();
41 sp<IBinder> binder = sm->getService(String16("media.drm"));
42
43 sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder);
44 if (service == NULL) {
45 status = UNKNOWN_ERROR;
46 return NULL;
47 }
48
49 auto obj = service->makeObject<Iface>();
50 if (obj == NULL) {
51 status = UNKNOWN_ERROR;
52 return NULL;
53 }
54
55 status = obj->initCheck();
56 if (status != OK && status != NO_INIT) {
57 return NULL;
58 }
59 return obj;
60}
61
62template<typename Iface, typename Hal>
63sp<Iface> MakeObject(status_t *pstatus) {
64 if (UseDrmService()) {
65 return MakeObjectWithService<Iface>(pstatus);
66 } else {
67 return new Hal();
68 }
69}
70} // namespace
71
72bool UseDrmService() {
Robert Shih17c6d822019-11-07 11:31:43 -080073 return property_get_bool("mediadrm.use_mediadrmserver", true);
Robert Shih28c2ed32019-10-27 22:55:12 -070074}
75
76sp<IDrm> MakeDrm(status_t *pstatus) {
77 return MakeObject<IDrm, DrmHal>(pstatus);
78}
79
80sp<ICrypto> MakeCrypto(status_t *pstatus) {
81 return MakeObject<ICrypto, CryptoHal>(pstatus);
82}
83
84} // namespace DrmUtils
85} // namespace android