Robert Shih | 28c2ed3 | 2019-10-27 22:55:12 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 32 | namespace android { |
| 33 | namespace DrmUtils { |
| 34 | |
| 35 | namespace { |
| 36 | template<typename Iface> |
| 37 | sp<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 | |
| 62 | template<typename Iface, typename Hal> |
| 63 | sp<Iface> MakeObject(status_t *pstatus) { |
| 64 | if (UseDrmService()) { |
| 65 | return MakeObjectWithService<Iface>(pstatus); |
| 66 | } else { |
| 67 | return new Hal(); |
| 68 | } |
| 69 | } |
| 70 | } // namespace |
| 71 | |
| 72 | bool UseDrmService() { |
| 73 | return property_get_bool("persist.device_config.media_native.mediadrmserver", true); |
| 74 | } |
| 75 | |
| 76 | sp<IDrm> MakeDrm(status_t *pstatus) { |
| 77 | return MakeObject<IDrm, DrmHal>(pstatus); |
| 78 | } |
| 79 | |
| 80 | sp<ICrypto> MakeCrypto(status_t *pstatus) { |
| 81 | return MakeObject<ICrypto, CryptoHal>(pstatus); |
| 82 | } |
| 83 | |
| 84 | } // namespace DrmUtils |
| 85 | } // namespace android |