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 and associate it with a MediaDrm session 60 * 61 * @param uuid The UUID of the crypto scheme. 62 * @param sessionId The MediaDrm sessionId to associate with this 63 * MediaCrypto session. The sessionId may be changed after the MediaCrypto 64 * is created using {@link #setMediaDrmSession} 65 */ MediaCrypto(@onNull UUID uuid, @NonNull byte[] sessionId)66 public MediaCrypto(@NonNull UUID uuid, @NonNull byte[] sessionId) throws MediaCryptoException { 67 native_setup(getByteArrayFromUUID(uuid), sessionId); 68 } 69 70 /** 71 * Query if the crypto scheme requires the use of a secure decoder 72 * to decode data of the given mime type. 73 * @param mime The mime type of the media data 74 */ requiresSecureDecoderComponent(@onNull String mime)75 public final native boolean requiresSecureDecoderComponent(@NonNull String mime); 76 77 /** 78 * Associate a new MediaDrm session with this MediaCrypto instance. 79 * 80 * <p>The MediaDrm session is used to securely load decryption keys for a 81 * crypto scheme. The crypto keys loaded through the MediaDrm session 82 * may be selected for use during the decryption operation performed 83 * by {@link android.media.MediaCodec#queueSecureInputBuffer} by specifying 84 * their key IDs in the {@link android.media.MediaCodec.CryptoInfo#key} field. 85 * 86 * @param sessionId The MediaDrm sessionId to associate with this MediaCrypto 87 * instance. The session's scheme must match the scheme UUID used when 88 * constructing this MediaCrypto instance. 89 * @throws MediaCryptoException on failure to set the sessionId 90 */ setMediaDrmSession(@onNull byte[] sessionId)91 public final native void setMediaDrmSession(@NonNull byte[] sessionId) 92 throws MediaCryptoException; 93 94 @Override finalize()95 protected void finalize() { 96 native_finalize(); 97 } 98 release()99 public native final void release(); native_init()100 private static native final void native_init(); 101 native_setup(@onNull byte[] uuid, @NonNull byte[] initData)102 private native final void native_setup(@NonNull byte[] uuid, @NonNull byte[] initData) 103 throws MediaCryptoException; 104 native_finalize()105 private native final void native_finalize(); 106 107 static { 108 System.loadLibrary("media_jni"); native_init()109 native_init(); 110 } 111 112 private long mNativeContext; 113 } 114