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 MediaDrm session with this MediaCrypto instance. The 79 * MediaDrm session is used to securely load decryption keys for a 80 * crypto scheme. The crypto keys loaded through the MediaDrm session 81 * may be selected for use during the decryption operation performed 82 * by {@link android.media.MediaCodec#queueSecureInputBuffer} by specifying 83 * their key ids in the {@link android.media.MediaCodec.CryptoInfo#key} field. 84 * @param sessionId the MediaDrm sessionId to associate with this 85 * MediaCrypto instance 86 * @throws MediaCryptoException on failure to set the sessionId 87 */ setMediaDrmSession(@onNull byte[] sessionId)88 public final native void setMediaDrmSession(@NonNull byte[] sessionId) 89 throws MediaCryptoException; 90 91 @Override finalize()92 protected void finalize() { 93 native_finalize(); 94 } 95 release()96 public native final void release(); native_init()97 private static native final void native_init(); 98 native_setup(@onNull byte[] uuid, @NonNull byte[] initData)99 private native final void native_setup(@NonNull byte[] uuid, @NonNull byte[] initData) 100 throws MediaCryptoException; 101 native_finalize()102 private native final void native_finalize(); 103 104 static { 105 System.loadLibrary("media_jni"); native_init()106 native_init(); 107 } 108 109 private long mNativeContext; 110 } 111