1 /*
2  * Copyright (C) 2012 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 package android.media;
18 
19 import android.annotation.NonNull;
20 import android.media.MediaCryptoException;
21 import java.util.UUID;
22 
23 /**
24  * MediaCrypto class can be used in conjunction with {@link android.media.MediaCodec}
25  * to decode encrypted media data.
26  *
27  * Crypto schemes are assigned 16 byte UUIDs,
28  * the method {@link #isCryptoSchemeSupported} can be used to query if a given
29  * scheme is supported on the device.
30  *
31  */
32 public final class MediaCrypto {
33     /**
34      * Query if the given scheme identified by its UUID is supported on
35      * this device.
36      * @param uuid The UUID of the crypto scheme.
37      */
isCryptoSchemeSupported(@onNull UUID uuid)38     public static final boolean isCryptoSchemeSupported(@NonNull UUID uuid) {
39         return isCryptoSchemeSupportedNative(getByteArrayFromUUID(uuid));
40     }
41 
42     @NonNull
getByteArrayFromUUID(@onNull UUID uuid)43     private static final byte[] getByteArrayFromUUID(@NonNull UUID uuid) {
44         long msb = uuid.getMostSignificantBits();
45         long lsb = uuid.getLeastSignificantBits();
46 
47         byte[] uuidBytes = new byte[16];
48         for (int i = 0; i < 8; ++i) {
49             uuidBytes[i] = (byte)(msb >>> (8 * (7 - i)));
50             uuidBytes[8 + i] = (byte)(lsb >>> (8 * (7 - i)));
51         }
52 
53         return uuidBytes;
54     }
55 
isCryptoSchemeSupportedNative(@onNull byte[] uuid)56     private static final native boolean isCryptoSchemeSupportedNative(@NonNull byte[] uuid);
57 
58     /**
59      * Instantiate a MediaCrypto object using opaque, crypto scheme specific
60      * data.
61      * @param uuid The UUID of the crypto scheme.
62      * @param initData Opaque initialization data specific to the crypto scheme.
63      */
MediaCrypto(@onNull UUID uuid, @NonNull byte[] initData)64     public MediaCrypto(@NonNull UUID uuid, @NonNull byte[] initData) throws MediaCryptoException {
65         native_setup(getByteArrayFromUUID(uuid), initData);
66     }
67 
68     /**
69      * Query if the crypto scheme requires the use of a secure decoder
70      * to decode data of the given mime type.
71      * @param mime The mime type of the media data
72      */
requiresSecureDecoderComponent(@onNull String mime)73     public final native boolean requiresSecureDecoderComponent(@NonNull String mime);
74 
75     /**
76      * Associate a MediaDrm session with this MediaCrypto instance.  The
77      * MediaDrm session is used to securely load decryption keys for a
78      * crypto scheme.  The crypto keys loaded through the MediaDrm session
79      * may be selected for use during the decryption operation performed
80      * by {@link android.media.MediaCodec#queueSecureInputBuffer} by specifying
81      * their key ids in the {@link android.media.MediaCodec.CryptoInfo#key} field.
82      * @param sessionId the MediaDrm sessionId to associate with this
83      * MediaCrypto instance
84      * @throws MediaCryptoException on failure to set the sessionId
85      */
setMediaDrmSession(@onNull byte[] sessionId)86     public final native void setMediaDrmSession(@NonNull byte[] sessionId)
87         throws MediaCryptoException;
88 
89     @Override
finalize()90     protected void finalize() {
91         native_finalize();
92     }
93 
release()94     public native final void release();
native_init()95     private static native final void native_init();
96 
native_setup(@onNull byte[] uuid, @NonNull byte[] initData)97     private native final void native_setup(@NonNull byte[] uuid, @NonNull byte[] initData)
98         throws MediaCryptoException;
99 
native_finalize()100     private native final void native_finalize();
101 
102     static {
103         System.loadLibrary("media_jni");
native_init()104         native_init();
105     }
106 
107     private long mNativeContext;
108 }
109