1 /*
2  * Copyright (C) 2016 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 package android.media;
17 
18 import android.annotation.NonNull;
19 import android.media.MediaDrm;
20 
21 import java.util.Arrays;
22 import java.util.UUID;
23 
24 /**
25  * Encapsulates initialization data required by a {@link MediaDrm} instance.
26  */
27 public abstract class DrmInitData {
28 
29     /**
30      * Prevent public constuctor access
31      */
DrmInitData()32     /* package private */ DrmInitData() {
33     }
34 
35     /**
36      * Retrieves initialization data for a given DRM scheme, specified by its UUID.
37      *
38      * @param schemeUuid The DRM scheme's UUID.
39      * @return The initialization data for the scheme, or null if the scheme is not supported.
40      * @deprecated Use {@link #getSchemeInitDataCount} and {@link #getSchemeInitDataAt} instead.
41      */
42     @Deprecated
get(UUID schemeUuid)43     public abstract SchemeInitData get(UUID schemeUuid);
44 
45     /**
46      * Returns the number of {@link SchemeInitData} elements available through {@link
47      * #getSchemeInitDataAt}.
48      */
getSchemeInitDataCount()49     public int getSchemeInitDataCount() {
50         return 0;
51     }
52 
53     /**
54      * Returns the {@link SchemeInitData} with the given {@code index}.
55      *
56      * @param index The index of the {@link SchemeInitData} to return.
57      * @return The {@link SchemeInitData} associated with the given {@code index}.
58      * @throws IndexOutOfBoundsException If the given {@code index} is negative or greater than
59      *         {@link #getSchemeInitDataCount}{@code - 1}.
60      */
getSchemeInitDataAt(int index)61     @NonNull public SchemeInitData getSchemeInitDataAt(int index) {
62         throw new IndexOutOfBoundsException();
63     }
64 
65     /**
66      * Scheme initialization data.
67      */
68     public static final class SchemeInitData {
69 
70         /**
71          * The Nil UUID, as defined in RFC 4122, section 4.1.7.
72          */
73         @NonNull public static final UUID UUID_NIL = new UUID(0, 0);
74 
75         /**
76          * The UUID associated with this scheme initialization data. May be {@link #UUID_NIL} if
77          * unknown or not applicable.
78          */
79         @NonNull public final UUID uuid;
80         /**
81          * The mimeType of {@link #data}.
82          */
83         public final String mimeType;
84         /**
85          * The initialization data.
86          */
87         public final byte[] data;
88 
89         /**
90          * @param uuid The UUID associated with this scheme initialization data.
91          * @param mimeType The mimeType of the initialization data.
92          * @param data The initialization data.
93          *
94          * @hide
95          */
SchemeInitData(UUID uuid, String mimeType, byte[] data)96         public SchemeInitData(UUID uuid, String mimeType, byte[] data) {
97             this.uuid = uuid;
98             this.mimeType = mimeType;
99             this.data = data;
100         }
101 
102         @Override
equals(Object obj)103         public boolean equals(Object obj) {
104             if (!(obj instanceof SchemeInitData)) {
105                 return false;
106             }
107             if (obj == this) {
108                 return true;
109             }
110 
111             SchemeInitData other = (SchemeInitData) obj;
112             return uuid.equals(other.uuid)
113                     && mimeType.equals(other.mimeType)
114                     && Arrays.equals(data, other.data);
115         }
116 
117         @Override
hashCode()118         public int hashCode() {
119             return uuid.hashCode() + 31 * (mimeType.hashCode() + 31 * Arrays.hashCode(data));
120         }
121 
122     }
123 
124 }
125