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