DrmInitData.java 11.8 KB
Newer Older
1
/*
O
olly 已提交
2
 * Copyright (C) 2016 The Android Open Source Project
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
O
olly 已提交
16
package com.google.android.exoplayer2.drm;
17

A
andrewlewis 已提交
18 19
import android.os.Parcel;
import android.os.Parcelable;
20
import android.support.annotation.Nullable;
O
olly 已提交
21 22 23 24
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.drm.DrmInitData.SchemeData;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
25
import java.util.ArrayList;
26
import java.util.Arrays;
O
olly 已提交
27
import java.util.Comparator;
28
import java.util.List;
29 30 31
import java.util.UUID;

/**
O
olly 已提交
32
 * Initialization data for one or more DRM schemes.
33
 */
O
olly 已提交
34
public final class DrmInitData implements Comparator<SchemeData>, Parcelable {
35

36 37 38 39 40 41
  /**
   * Merges {@link DrmInitData} obtained from a media manifest and a media stream.
   *
   * <p>The result is generated as follows.
   *
   * <ol>
O
Oliver Woodman 已提交
42
   *   <li>
43 44
   *     Include all {@link SchemeData}s from {@code manifestData} where {@link
   *     SchemeData#hasData()} is true.
O
Oliver Woodman 已提交
45 46
   *   </li>
   *   <li>
47 48
   *     Include all {@link SchemeData}s in {@code mediaData} where {@link SchemeData#hasData()} is
   *     true and for which we did not include an entry from the manifest targeting the same UUID.
O
Oliver Woodman 已提交
49 50
   *   </li>
   *   <li>
51 52
   *     If available, the scheme type from the manifest is used. If not, the scheme type from the
   *     media is used.
O
Oliver Woodman 已提交
53
   *   </li>
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
   * </ol>
   *
   * @param manifestData DRM session acquisition data obtained from the manifest.
   * @param mediaData DRM session acquisition data obtained from the media.
   * @return A {@link DrmInitData} obtained from merging a media manifest and a media stream.
   */
  public static @Nullable DrmInitData createSessionCreationData(
      @Nullable DrmInitData manifestData, @Nullable DrmInitData mediaData) {
    ArrayList<SchemeData> result = new ArrayList<>();
    String schemeType = null;
    if (manifestData != null) {
      schemeType = manifestData.schemeType;
      for (SchemeData data : manifestData.schemeDatas) {
        if (data.hasData()) {
          result.add(data);
        }
      }
    }

    if (mediaData != null) {
      if (schemeType == null) {
        schemeType = mediaData.schemeType;
      }
      int manifestDatasCount = result.size();
      for (SchemeData data : mediaData.schemeDatas) {
        if (data.hasData() && !containsSchemeDataWithUuid(result, manifestDatasCount, data.uuid)) {
          result.add(data);
        }
      }
    }

    return result.isEmpty() ? null : new DrmInitData(schemeType, result);
  }

O
olly 已提交
88
  private final SchemeData[] schemeDatas;
89

O
olly 已提交
90 91
  // Lazily initialized hashcode.
  private int hashCode;
92

93 94 95 96 97
  /**
   * The protection scheme type, or null if not applicable or unknown.
   */
  @Nullable public final String schemeType;

98 99 100 101 102
  /**
   * Number of {@link SchemeData}s.
   */
  public final int schemeDataCount;

103 104 105
  /**
   * @param schemeDatas Scheme initialization data for possibly multiple DRM schemes.
   */
O
olly 已提交
106
  public DrmInitData(List<SchemeData> schemeDatas) {
107
    this(null, false, schemeDatas.toArray(new SchemeData[schemeDatas.size()]));
O
olly 已提交
108
  }
109

110 111 112 113 114 115 116 117
  /**
   * @param schemeType See {@link #schemeType}.
   * @param schemeDatas Scheme initialization data for possibly multiple DRM schemes.
   */
  public DrmInitData(String schemeType, List<SchemeData> schemeDatas) {
    this(schemeType, false, schemeDatas.toArray(new SchemeData[schemeDatas.size()]));
  }

118 119 120
  /**
   * @param schemeDatas Scheme initialization data for possibly multiple DRM schemes.
   */
O
olly 已提交
121
  public DrmInitData(SchemeData... schemeDatas) {
122 123 124 125
    this(null, schemeDatas);
  }

  /**
126
   * @param schemeType See {@link #schemeType}.
127 128 129 130
   * @param schemeDatas Scheme initialization data for possibly multiple DRM schemes.
   */
  public DrmInitData(@Nullable String schemeType, SchemeData... schemeDatas) {
    this(schemeType, true, schemeDatas);
O
olly 已提交
131
  }
132

133 134 135
  private DrmInitData(@Nullable String schemeType, boolean cloneSchemeDatas,
      SchemeData... schemeDatas) {
    this.schemeType = schemeType;
O
olly 已提交
136 137 138
    if (cloneSchemeDatas) {
      schemeDatas = schemeDatas.clone();
    }
139
    // Sorting ensures that universal scheme data (i.e. data that applies to all schemes) is matched
O
olly 已提交
140 141 142
    // last. It's also required by the equals and hashcode implementations.
    Arrays.sort(schemeDatas, this);
    this.schemeDatas = schemeDatas;
143
    schemeDataCount = schemeDatas.length;
144 145
  }

O
olly 已提交
146
  /* package */ DrmInitData(Parcel in) {
147
    schemeType = in.readString();
O
olly 已提交
148
    schemeDatas = in.createTypedArray(SchemeData.CREATOR);
149
    schemeDataCount = schemeDatas.length;
O
olly 已提交
150 151
  }

152
  /**
O
olly 已提交
153 154
   * Retrieves data for a given DRM scheme, specified by its UUID.
   *
155
   * @deprecated Use {@link #get(int)} and {@link SchemeData#matches(UUID)} instead.
O
olly 已提交
156 157
   * @param uuid The DRM scheme's UUID.
   * @return The initialization data for the scheme, or null if the scheme is not supported.
158
   */
159
  @Deprecated
O
olly 已提交
160 161 162 163 164
  public SchemeData get(UUID uuid) {
    for (SchemeData schemeData : schemeDatas) {
      if (schemeData.matches(uuid)) {
        return schemeData;
      }
165
    }
O
olly 已提交
166 167
    return null;
  }
168

169 170 171
  /**
   * Retrieves the {@link SchemeData} at a given index.
   *
172 173
   * @param index The index of the scheme to return. Must not exceed {@link #schemeDataCount}.
   * @return The {@link SchemeData} at the specified index.
174 175 176 177 178
   */
  public SchemeData get(int index) {
    return schemeDatas[index];
  }

179
  /**
180
   * Returns a copy with the specified protection scheme type.
181 182
   *
   * @param schemeType A protection scheme type. May be null.
183
   * @return A copy with the specified protection scheme type.
184 185
   */
  public DrmInitData copyWithSchemeType(@Nullable String schemeType) {
186
    if (Util.areEqual(this.schemeType, schemeType)) {
187 188
      return this;
    }
189
    return new DrmInitData(schemeType, false, schemeDatas);
190 191
  }

O
olly 已提交
192 193 194
  @Override
  public int hashCode() {
    if (hashCode == 0) {
195 196 197
      int result = (schemeType == null ? 0 : schemeType.hashCode());
      result = 31 * result + Arrays.hashCode(schemeDatas);
      hashCode = result;
198
    }
O
olly 已提交
199 200
    return hashCode;
  }
201

O
olly 已提交
202 203 204 205
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
206
    }
O
olly 已提交
207 208
    if (obj == null || getClass() != obj.getClass()) {
      return false;
209
    }
210 211 212
    DrmInitData other = (DrmInitData) obj;
    return Util.areEqual(schemeType, other.schemeType)
        && Arrays.equals(schemeDatas, other.schemeDatas);
O
olly 已提交
213
  }
214

O
olly 已提交
215 216 217 218
  @Override
  public int compare(SchemeData first, SchemeData second) {
    return C.UUID_NIL.equals(first.uuid) ? (C.UUID_NIL.equals(second.uuid) ? 0 : 1)
        : first.uuid.compareTo(second.uuid);
219 220
  }

O
olly 已提交
221 222 223 224 225 226 227 228 229
  // Parcelable implementation.

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
230
    dest.writeString(schemeType);
O
olly 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    dest.writeTypedArray(schemeDatas, 0);
  }

  public static final Parcelable.Creator<DrmInitData> CREATOR =
      new Parcelable.Creator<DrmInitData>() {

    @Override
    public DrmInitData createFromParcel(Parcel in) {
      return new DrmInitData(in);
    }

    @Override
    public DrmInitData[] newArray(int size) {
      return new DrmInitData[size];
    }

  };

249 250 251 252 253 254 255 256 257 258 259 260
  // Internal methods.

  private static boolean containsSchemeDataWithUuid(
      ArrayList<SchemeData> datas, int limit, UUID uuid) {
    for (int i = 0; i < limit; i++) {
      if (datas.get(i).uuid.equals(uuid)) {
        return true;
      }
    }
    return false;
  }

261 262 263
  /**
   * Scheme initialization data.
   */
O
olly 已提交
264
  public static final class SchemeData implements Parcelable {
265

266 267 268
    // Lazily initialized hashcode.
    private int hashCode;

O
olly 已提交
269 270 271 272 273
    /**
     * The {@link UUID} of the DRM scheme, or {@link C#UUID_NIL} if the data is universal (i.e.
     * applies to all schemes).
     */
    private final UUID uuid;
274 275 276 277 278
    /**
     * The mimeType of {@link #data}.
     */
    public final String mimeType;
    /**
279
     * The initialization data. May be null for scheme support checks only.
280 281
     */
    public final byte[] data;
282 283 284 285
    /**
     * Whether secure decryption is required.
     */
    public final boolean requiresSecureDecryption;
286 287

    /**
O
olly 已提交
288 289
     * @param uuid The {@link UUID} of the DRM scheme, or {@link C#UUID_NIL} if the data is
     *     universal (i.e. applies to all schemes).
290 291
     * @param mimeType See {@link #mimeType}.
     * @param data See {@link #data}.
292
     */
293 294
    public SchemeData(UUID uuid, String mimeType, byte[] data) {
      this(uuid, mimeType, data, false);
295 296 297 298 299
    }

    /**
     * @param uuid The {@link UUID} of the DRM scheme, or {@link C#UUID_NIL} if the data is
     *     universal (i.e. applies to all schemes).
300 301 302
     * @param mimeType See {@link #mimeType}.
     * @param data See {@link #data}.
     * @param requiresSecureDecryption See {@link #requiresSecureDecryption}.
303
     */
304
    public SchemeData(UUID uuid, String mimeType, byte[] data, boolean requiresSecureDecryption) {
O
olly 已提交
305
      this.uuid = Assertions.checkNotNull(uuid);
306
      this.mimeType = Assertions.checkNotNull(mimeType);
307
      this.data = data;
308
      this.requiresSecureDecryption = requiresSecureDecryption;
309 310
    }

O
olly 已提交
311 312 313 314
    /* package */ SchemeData(Parcel in) {
      uuid = new UUID(in.readLong(), in.readLong());
      mimeType = in.readString();
      data = in.createByteArray();
315
      requiresSecureDecryption = in.readByte() != 0;
O
olly 已提交
316 317
    }

O
olly 已提交
318 319 320 321
    /**
     * Returns whether this initialization data applies to the specified scheme.
     *
     * @param schemeUuid The scheme {@link UUID}.
O
olly 已提交
322
     * @return Whether this initialization data applies to the specified scheme.
O
olly 已提交
323 324 325 326 327
     */
    public boolean matches(UUID schemeUuid) {
      return C.UUID_NIL.equals(uuid) || schemeUuid.equals(uuid);
    }

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    /**
     * Returns whether this {@link SchemeData} can be used to replace {@code other}.
     *
     * @param other A {@link SchemeData}.
     * @return Whether this {@link SchemeData} can be used to replace {@code other}.
     */
    public boolean canReplace(SchemeData other) {
      return hasData() && !other.hasData() && matches(other.uuid);
    }

    /**
     * Returns whether {@link #data} is non-null.
     */
    public boolean hasData() {
      return data != null;
    }

345 346
    @Override
    public boolean equals(Object obj) {
O
olly 已提交
347
      if (!(obj instanceof SchemeData)) {
348 349 350 351 352
        return false;
      }
      if (obj == this) {
        return true;
      }
O
olly 已提交
353 354
      SchemeData other = (SchemeData) obj;
      return mimeType.equals(other.mimeType) && Util.areEqual(uuid, other.uuid)
355
          && Arrays.equals(data, other.data);
356 357 358 359
    }

    @Override
    public int hashCode() {
360
      if (hashCode == 0) {
O
olly 已提交
361
        int result = uuid.hashCode();
O
olly 已提交
362 363 364
        result = 31 * result + mimeType.hashCode();
        result = 31 * result + Arrays.hashCode(data);
        hashCode = result;
365 366 367 368
      }
      return hashCode;
    }

O
olly 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381
    // Parcelable implementation.

    @Override
    public int describeContents() {
      return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
      dest.writeLong(uuid.getMostSignificantBits());
      dest.writeLong(uuid.getLeastSignificantBits());
      dest.writeString(mimeType);
      dest.writeByteArray(data);
382
      dest.writeByte((byte) (requiresSecureDecryption ? 1 : 0));
O
olly 已提交
383 384
    }

O
olly 已提交
385
    @SuppressWarnings("hiding")
O
olly 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
    public static final Parcelable.Creator<SchemeData> CREATOR =
        new Parcelable.Creator<SchemeData>() {

      @Override
      public SchemeData createFromParcel(Parcel in) {
        return new SchemeData(in);
      }

      @Override
      public SchemeData[] newArray(int size) {
        return new SchemeData[size];
      }

    };

401 402
  }

403
}