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    @Override
74    protected void finalize() {
75        native_finalize();
76    }
77
78    public native final void release();
79    private static native final void native_init();
80
81    private native final void native_setup(byte[] uuid, byte[] initData)
82        throws MediaCryptoException;
83
84    private native final void native_finalize();
85
86    static {
87        System.loadLibrary("media_jni");
88        native_init();
89    }
90
91    private long mNativeContext;
92}
93