1/*
2 * Copyright (C) 2017 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 NUPLAYER_DRM_H_
18#define NUPLAYER_DRM_H_
19
20#include <binder/Parcel.h>
21#include <media/ICrypto.h>
22#include <media/IDrm.h>
23#include <media/stagefright/MetaData.h> // for CryptInfo
24
25
26namespace android {
27
28    struct DrmUUID {
29        static const int UUID_SIZE = 16;
30
31        DrmUUID() {
32            memset(this->uuid, 0, sizeof(uuid));
33        }
34
35        // to allow defining Vector/KeyedVector of UUID type
36        DrmUUID(const DrmUUID &a) {
37            memcpy(this->uuid, a.uuid, sizeof(uuid));
38        }
39
40        // to allow defining Vector/KeyedVector of UUID type
41        DrmUUID(const uint8_t uuid_in[UUID_SIZE]) {
42            memcpy(this->uuid, uuid_in, sizeof(uuid));
43        }
44
45        const uint8_t *ptr() const {
46            return uuid;
47        }
48
49        String8 toHexString() const {
50            return arrayToHex(uuid, UUID_SIZE);
51        }
52
53        static String8 toHexString(const uint8_t uuid_in[UUID_SIZE]) {
54            return arrayToHex(uuid_in, UUID_SIZE);
55        }
56
57        static String8 arrayToHex(const uint8_t *array, int bytes) {
58            String8 result;
59            for (int i = 0; i < bytes; i++) {
60                result.appendFormat("%02x", array[i]);
61            }
62
63            return result;
64        }
65
66    protected:
67        uint8_t uuid[UUID_SIZE];
68    };
69
70
71    struct NuPlayerDrm {
72
73        // static helpers - internal
74
75    protected:
76        static sp<IDrm> CreateDrm(status_t *pstatus);
77        static sp<ICrypto> createCrypto(status_t *pstatus);
78        static Vector<DrmUUID> parsePSSH(const void *pssh, size_t psshsize);
79        static Vector<DrmUUID> getSupportedDrmSchemes(const void *pssh, size_t psshsize);
80
81        // static helpers - public
82
83    public:
84        static sp<ICrypto> createCryptoAndPlugin(const uint8_t uuid[16],
85                const Vector<uint8_t> &drmSessionId, status_t &status);
86        // Parcel has only private copy constructor so passing it in rather than returning
87        static void retrieveDrmInfo(const void *pssh, size_t psshsize, Parcel *parcel);
88
89        ////////////////////////////////////////////////////////////////////////////////////////////
90        /// Helpers for NuPlayerDecoder
91        ////////////////////////////////////////////////////////////////////////////////////////////
92
93        static const uint8_t kBlockSize = 16; // AES_BLOCK_SIZE
94
95        struct CryptoInfo {
96            int numSubSamples;
97            uint8_t key[kBlockSize];
98            uint8_t iv[kBlockSize];
99            CryptoPlugin::Mode mode;
100            CryptoPlugin::Pattern pattern;
101            CryptoPlugin::SubSample *subSamples;
102        };
103
104        static CryptoInfo *makeCryptoInfo(
105                int numSubSamples,
106                uint8_t key[kBlockSize],
107                uint8_t iv[kBlockSize],
108                CryptoPlugin::Mode mode,
109                size_t *clearbytes,
110                size_t *encryptedbytes);
111
112        static CryptoInfo *getSampleCryptoInfo(sp<MetaData> meta);
113
114    };  // NuPlayerDrm
115
116}   // android
117
118#endif     //NUPLAYER_DRM_H_
119