/
MediaItem.java
2518 lines (2245 loc) · 89.7 KB
/
MediaItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 The Android Open Source Project
*
* 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.
*/
package androidx.media3.common;
import static androidx.media3.common.util.Assertions.checkArgument;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkState;
import static androidx.media3.common.util.Util.msToUs;
import static androidx.media3.common.util.Util.usToMs;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.BundleCollectionUtil;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.InlineMe;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/** Representation of a media item. */
public final class MediaItem implements Bundleable {
/**
* Creates a {@link MediaItem} for the given URI.
*
* @param uri The URI.
* @return An {@link MediaItem} for the given URI.
*/
public static MediaItem fromUri(String uri) {
return new MediaItem.Builder().setUri(uri).build();
}
/**
* Creates a {@link MediaItem} for the given {@link Uri URI}.
*
* @param uri The {@link Uri uri}.
* @return An {@link MediaItem} for the given URI.
*/
public static MediaItem fromUri(Uri uri) {
return new MediaItem.Builder().setUri(uri).build();
}
/** A builder for {@link MediaItem} instances. */
public static final class Builder {
@Nullable private String mediaId;
@Nullable private Uri uri;
@Nullable private String mimeType;
// TODO: Change this to ClippingProperties once all the deprecated individual setters are
// removed.
private ClippingConfiguration.Builder clippingConfiguration;
// TODO: Change this to @Nullable DrmConfiguration once all the deprecated individual setters
// are removed.
private DrmConfiguration.Builder drmConfiguration;
private List<StreamKey> streamKeys;
@Nullable private String customCacheKey;
private ImmutableList<SubtitleConfiguration> subtitleConfigurations;
@Nullable private AdsConfiguration adsConfiguration;
@Nullable private Object tag;
private long imageDurationMs;
@Nullable private MediaMetadata mediaMetadata;
// TODO: Change this to LiveConfiguration once all the deprecated individual setters
// are removed.
private LiveConfiguration.Builder liveConfiguration;
private RequestMetadata requestMetadata;
/** Creates a builder. */
@SuppressWarnings("deprecation") // Temporarily uses DrmConfiguration.Builder() constructor.
public Builder() {
clippingConfiguration = new ClippingConfiguration.Builder();
drmConfiguration = new DrmConfiguration.Builder();
streamKeys = Collections.emptyList();
subtitleConfigurations = ImmutableList.of();
liveConfiguration = new LiveConfiguration.Builder();
requestMetadata = RequestMetadata.EMPTY;
imageDurationMs = C.TIME_UNSET;
}
private Builder(MediaItem mediaItem) {
this();
clippingConfiguration = mediaItem.clippingConfiguration.buildUpon();
mediaId = mediaItem.mediaId;
mediaMetadata = mediaItem.mediaMetadata;
liveConfiguration = mediaItem.liveConfiguration.buildUpon();
requestMetadata = mediaItem.requestMetadata;
@Nullable LocalConfiguration localConfiguration = mediaItem.localConfiguration;
if (localConfiguration != null) {
customCacheKey = localConfiguration.customCacheKey;
mimeType = localConfiguration.mimeType;
uri = localConfiguration.uri;
streamKeys = localConfiguration.streamKeys;
subtitleConfigurations = localConfiguration.subtitleConfigurations;
tag = localConfiguration.tag;
drmConfiguration =
localConfiguration.drmConfiguration != null
? localConfiguration.drmConfiguration.buildUpon()
: new DrmConfiguration.Builder();
adsConfiguration = localConfiguration.adsConfiguration;
imageDurationMs = localConfiguration.imageDurationMs;
}
}
/**
* Sets the optional media ID which identifies the media item.
*
* <p>By default {@link #DEFAULT_MEDIA_ID} is used.
*/
@CanIgnoreReturnValue
public Builder setMediaId(String mediaId) {
this.mediaId = checkNotNull(mediaId);
return this;
}
/**
* Sets the optional URI.
*
* <p>If {@code uri} is null or unset then no {@link LocalConfiguration} object is created
* during {@link #build()} and no other {@code Builder} methods that would populate {@link
* MediaItem#localConfiguration} should be called.
*/
@CanIgnoreReturnValue
public Builder setUri(@Nullable String uri) {
return setUri(uri == null ? null : Uri.parse(uri));
}
/**
* Sets the optional URI.
*
* <p>If {@code uri} is null or unset then no {@link LocalConfiguration} object is created
* during {@link #build()} and no other {@code Builder} methods that would populate {@link
* MediaItem#localConfiguration} should be called.
*/
@CanIgnoreReturnValue
public Builder setUri(@Nullable Uri uri) {
this.uri = uri;
return this;
}
/**
* Sets the optional MIME type.
*
* <p>The MIME type may be used as a hint for inferring the type of the media item.
*
* <p>This method should only be called if {@link #setUri} is passed a non-null value.
*
* @param mimeType The MIME type.
*/
@CanIgnoreReturnValue
public Builder setMimeType(@Nullable String mimeType) {
this.mimeType = mimeType;
return this;
}
/** Sets the {@link ClippingConfiguration}, defaults to {@link ClippingConfiguration#UNSET}. */
@CanIgnoreReturnValue
public Builder setClippingConfiguration(ClippingConfiguration clippingConfiguration) {
this.clippingConfiguration = clippingConfiguration.buildUpon();
return this;
}
/**
* @deprecated Use {@link #setClippingConfiguration(ClippingConfiguration)} and {@link
* ClippingConfiguration.Builder#setStartPositionMs(long)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setClipStartPositionMs(@IntRange(from = 0) long startPositionMs) {
clippingConfiguration.setStartPositionMs(startPositionMs);
return this;
}
/**
* @deprecated Use {@link #setClippingConfiguration(ClippingConfiguration)} and {@link
* ClippingConfiguration.Builder#setEndPositionMs(long)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setClipEndPositionMs(long endPositionMs) {
clippingConfiguration.setEndPositionMs(endPositionMs);
return this;
}
/**
* @deprecated Use {@link #setClippingConfiguration(ClippingConfiguration)} and {@link
* ClippingConfiguration.Builder#setRelativeToLiveWindow(boolean)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setClipRelativeToLiveWindow(boolean relativeToLiveWindow) {
clippingConfiguration.setRelativeToLiveWindow(relativeToLiveWindow);
return this;
}
/**
* @deprecated Use {@link #setClippingConfiguration(ClippingConfiguration)} and {@link
* ClippingConfiguration.Builder#setRelativeToDefaultPosition(boolean)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setClipRelativeToDefaultPosition(boolean relativeToDefaultPosition) {
clippingConfiguration.setRelativeToDefaultPosition(relativeToDefaultPosition);
return this;
}
/**
* @deprecated Use {@link #setClippingConfiguration(ClippingConfiguration)} and {@link
* ClippingConfiguration.Builder#setStartsAtKeyFrame(boolean)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setClipStartsAtKeyFrame(boolean startsAtKeyFrame) {
clippingConfiguration.setStartsAtKeyFrame(startsAtKeyFrame);
return this;
}
/** Sets the optional DRM configuration. */
@CanIgnoreReturnValue
public Builder setDrmConfiguration(@Nullable DrmConfiguration drmConfiguration) {
this.drmConfiguration =
drmConfiguration != null ? drmConfiguration.buildUpon() : new DrmConfiguration.Builder();
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setLicenseUri(Uri)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmLicenseUri(@Nullable Uri licenseUri) {
drmConfiguration.setLicenseUri(licenseUri);
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setLicenseUri(String)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmLicenseUri(@Nullable String licenseUri) {
drmConfiguration.setLicenseUri(licenseUri);
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setLicenseRequestHeaders(Map)} instead. Note that {@link
* DrmConfiguration.Builder#setLicenseRequestHeaders(Map)} doesn't accept null, use an empty
* map to clear the headers.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmLicenseRequestHeaders(
@Nullable Map<String, String> licenseRequestHeaders) {
drmConfiguration.setLicenseRequestHeaders(
licenseRequestHeaders != null ? licenseRequestHeaders : ImmutableMap.of());
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and pass the {@code uuid} to
* {@link DrmConfiguration.Builder#Builder(UUID)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmUuid(@Nullable UUID uuid) {
drmConfiguration.setNullableScheme(uuid);
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setMultiSession(boolean)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmMultiSession(boolean multiSession) {
drmConfiguration.setMultiSession(multiSession);
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setForceDefaultLicenseUri(boolean)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmForceDefaultLicenseUri(boolean forceDefaultLicenseUri) {
drmConfiguration.setForceDefaultLicenseUri(forceDefaultLicenseUri);
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setPlayClearContentWithoutKey(boolean)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmPlayClearContentWithoutKey(boolean playClearContentWithoutKey) {
drmConfiguration.setPlayClearContentWithoutKey(playClearContentWithoutKey);
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setForceSessionsForAudioAndVideoTracks(boolean)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmSessionForClearPeriods(boolean sessionForClearPeriods) {
drmConfiguration.setForceSessionsForAudioAndVideoTracks(sessionForClearPeriods);
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setForcedSessionTrackTypes(List)} instead. Note that {@link
* DrmConfiguration.Builder#setForcedSessionTrackTypes(List)} doesn't accept null, use an
* empty list to clear the contents.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmSessionForClearTypes(
@Nullable List<@C.TrackType Integer> sessionForClearTypes) {
drmConfiguration.setForcedSessionTrackTypes(
sessionForClearTypes != null ? sessionForClearTypes : ImmutableList.of());
return this;
}
/**
* @deprecated Use {@link #setDrmConfiguration(DrmConfiguration)} and {@link
* DrmConfiguration.Builder#setKeySetId(byte[])} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setDrmKeySetId(@Nullable byte[] keySetId) {
drmConfiguration.setKeySetId(keySetId);
return this;
}
/**
* Sets the optional stream keys by which the manifest is filtered (only used for adaptive
* streams).
*
* <p>{@code null} or an empty {@link List} can be used for a reset.
*
* <p>If {@link #setUri} is passed a non-null {@code uri}, the stream keys are used to create a
* {@link LocalConfiguration} object. Otherwise they will be ignored.
*/
@CanIgnoreReturnValue
@UnstableApi
public Builder setStreamKeys(@Nullable List<StreamKey> streamKeys) {
this.streamKeys =
streamKeys != null && !streamKeys.isEmpty()
? Collections.unmodifiableList(new ArrayList<>(streamKeys))
: Collections.emptyList();
return this;
}
/**
* Sets the optional custom cache key (only used for progressive streams).
*
* <p>This method should only be called if {@link #setUri} is passed a non-null value.
*/
@CanIgnoreReturnValue
@UnstableApi
public Builder setCustomCacheKey(@Nullable String customCacheKey) {
this.customCacheKey = customCacheKey;
return this;
}
/**
* @deprecated Use {@link #setSubtitleConfigurations(List)} instead. Note that {@link
* #setSubtitleConfigurations(List)} doesn't accept null, use an empty list to clear the
* contents.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setSubtitles(@Nullable List<Subtitle> subtitles) {
this.subtitleConfigurations =
subtitles != null ? ImmutableList.copyOf(subtitles) : ImmutableList.of();
return this;
}
/**
* Sets the optional subtitles.
*
* <p>This method should only be called if {@link #setUri} is passed a non-null value.
*/
@CanIgnoreReturnValue
public Builder setSubtitleConfigurations(List<SubtitleConfiguration> subtitleConfigurations) {
this.subtitleConfigurations = ImmutableList.copyOf(subtitleConfigurations);
return this;
}
/**
* Sets the optional {@link AdsConfiguration}.
*
* <p>This method should only be called if {@link #setUri} is passed a non-null value.
*/
@CanIgnoreReturnValue
public Builder setAdsConfiguration(@Nullable AdsConfiguration adsConfiguration) {
this.adsConfiguration = adsConfiguration;
return this;
}
/**
* @deprecated Use {@link #setAdsConfiguration(AdsConfiguration)}, parse the {@code adTagUri}
* with {@link Uri#parse(String)} and pass the result to {@link
* AdsConfiguration.Builder#Builder(Uri)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setAdTagUri(@Nullable String adTagUri) {
return setAdTagUri(adTagUri != null ? Uri.parse(adTagUri) : null);
}
/**
* @deprecated Use {@link #setAdsConfiguration(AdsConfiguration)} and pass the {@code adTagUri}
* to {@link AdsConfiguration.Builder#Builder(Uri)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setAdTagUri(@Nullable Uri adTagUri) {
return setAdTagUri(adTagUri, /* adsId= */ null);
}
/**
* @deprecated Use {@link #setAdsConfiguration(AdsConfiguration)}, pass the {@code adTagUri} to
* {@link AdsConfiguration.Builder#Builder(Uri)} and the {@code adsId} to {@link
* AdsConfiguration.Builder#setAdsId(Object)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setAdTagUri(@Nullable Uri adTagUri, @Nullable Object adsId) {
this.adsConfiguration =
adTagUri != null ? new AdsConfiguration.Builder(adTagUri).setAdsId(adsId).build() : null;
return this;
}
/** Sets the {@link LiveConfiguration}. Defaults to {@link LiveConfiguration#UNSET}. */
@CanIgnoreReturnValue
public Builder setLiveConfiguration(LiveConfiguration liveConfiguration) {
this.liveConfiguration = liveConfiguration.buildUpon();
return this;
}
/**
* @deprecated Use {@link #setLiveConfiguration(LiveConfiguration)} and {@link
* LiveConfiguration.Builder#setTargetOffsetMs(long)}.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setLiveTargetOffsetMs(long liveTargetOffsetMs) {
liveConfiguration.setTargetOffsetMs(liveTargetOffsetMs);
return this;
}
/**
* @deprecated Use {@link #setLiveConfiguration(LiveConfiguration)} and {@link
* LiveConfiguration.Builder#setMinOffsetMs(long)}.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setLiveMinOffsetMs(long liveMinOffsetMs) {
liveConfiguration.setMinOffsetMs(liveMinOffsetMs);
return this;
}
/**
* @deprecated Use {@link #setLiveConfiguration(LiveConfiguration)} and {@link
* LiveConfiguration.Builder#setMaxOffsetMs(long)}.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setLiveMaxOffsetMs(long liveMaxOffsetMs) {
liveConfiguration.setMaxOffsetMs(liveMaxOffsetMs);
return this;
}
/**
* @deprecated Use {@link #setLiveConfiguration(LiveConfiguration)} and {@link
* LiveConfiguration.Builder#setMinPlaybackSpeed(float)}.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setLiveMinPlaybackSpeed(float minPlaybackSpeed) {
liveConfiguration.setMinPlaybackSpeed(minPlaybackSpeed);
return this;
}
/**
* @deprecated Use {@link #setLiveConfiguration(LiveConfiguration)} and {@link
* LiveConfiguration.Builder#setMaxPlaybackSpeed(float)}.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
public Builder setLiveMaxPlaybackSpeed(float maxPlaybackSpeed) {
liveConfiguration.setMaxPlaybackSpeed(maxPlaybackSpeed);
return this;
}
/**
* Sets the optional tag for custom attributes. The tag for the media source which will be
* published in the {@code androidx.media3.common.Timeline} of the source as {@code
* androidx.media3.common.Timeline.Window#tag}.
*
* <p>This method should only be called if {@link #setUri} is passed a non-null value.
*/
@CanIgnoreReturnValue
public Builder setTag(@Nullable Object tag) {
this.tag = tag;
return this;
}
/**
* Sets the image duration in video output, in milliseconds.
*
* <p>Must be set if {@linkplain #setUri the uri} is set and resolves to an image. Ignored
* otherwise.
*
* <p>Default value is {@link C#TIME_UNSET}.
*/
@CanIgnoreReturnValue
@UnstableApi
public Builder setImageDurationMs(long imageDurationMs) {
checkArgument(imageDurationMs > 0 || imageDurationMs == C.TIME_UNSET);
this.imageDurationMs = imageDurationMs;
return this;
}
/** Sets the media metadata. */
@CanIgnoreReturnValue
public Builder setMediaMetadata(MediaMetadata mediaMetadata) {
this.mediaMetadata = mediaMetadata;
return this;
}
/** Sets the request metadata. */
@CanIgnoreReturnValue
public Builder setRequestMetadata(RequestMetadata requestMetadata) {
this.requestMetadata = requestMetadata;
return this;
}
/** Returns a new {@link MediaItem} instance with the current builder values. */
public MediaItem build() {
// TODO: remove this check once all the deprecated individual DRM setters are removed.
checkState(drmConfiguration.licenseUri == null || drmConfiguration.scheme != null);
@Nullable LocalConfiguration localConfiguration = null;
@Nullable Uri uri = this.uri;
if (uri != null) {
localConfiguration =
new LocalConfiguration(
uri,
mimeType,
drmConfiguration.scheme != null ? drmConfiguration.build() : null,
adsConfiguration,
streamKeys,
customCacheKey,
subtitleConfigurations,
tag,
imageDurationMs);
}
return new MediaItem(
mediaId != null ? mediaId : DEFAULT_MEDIA_ID,
clippingConfiguration.buildClippingProperties(),
localConfiguration,
liveConfiguration.build(),
mediaMetadata != null ? mediaMetadata : MediaMetadata.EMPTY,
requestMetadata);
}
}
/** DRM configuration for a media item. */
public static final class DrmConfiguration implements Bundleable {
/** Builder for {@link DrmConfiguration}. */
public static final class Builder {
// TODO remove @Nullable annotation when the deprecated zero-arg constructor is removed.
@Nullable private UUID scheme;
@Nullable private Uri licenseUri;
private ImmutableMap<String, String> licenseRequestHeaders;
private boolean multiSession;
private boolean playClearContentWithoutKey;
private boolean forceDefaultLicenseUri;
private ImmutableList<@C.TrackType Integer> forcedSessionTrackTypes;
@Nullable private byte[] keySetId;
/**
* Constructs an instance.
*
* @param scheme The {@link UUID} of the protection scheme.
*/
public Builder(UUID scheme) {
this.scheme = scheme;
this.licenseRequestHeaders = ImmutableMap.of();
this.forcedSessionTrackTypes = ImmutableList.of();
}
/**
* @deprecated This only exists to support the deprecated setters for individual DRM
* properties on {@link MediaItem.Builder}.
*/
@Deprecated
private Builder() {
this.licenseRequestHeaders = ImmutableMap.of();
this.forcedSessionTrackTypes = ImmutableList.of();
}
private Builder(DrmConfiguration drmConfiguration) {
this.scheme = drmConfiguration.scheme;
this.licenseUri = drmConfiguration.licenseUri;
this.licenseRequestHeaders = drmConfiguration.licenseRequestHeaders;
this.multiSession = drmConfiguration.multiSession;
this.playClearContentWithoutKey = drmConfiguration.playClearContentWithoutKey;
this.forceDefaultLicenseUri = drmConfiguration.forceDefaultLicenseUri;
this.forcedSessionTrackTypes = drmConfiguration.forcedSessionTrackTypes;
this.keySetId = drmConfiguration.keySetId;
}
/** Sets the {@link UUID} of the protection scheme. */
@CanIgnoreReturnValue
public Builder setScheme(UUID scheme) {
this.scheme = scheme;
return this;
}
/**
* @deprecated This only exists to support the deprecated {@link
* MediaItem.Builder#setDrmUuid(UUID)}.
*/
@CanIgnoreReturnValue
@Deprecated
private Builder setNullableScheme(@Nullable UUID scheme) {
this.scheme = scheme;
return this;
}
/** Sets the optional default DRM license server URI. */
@CanIgnoreReturnValue
public Builder setLicenseUri(@Nullable Uri licenseUri) {
this.licenseUri = licenseUri;
return this;
}
/** Sets the optional default DRM license server URI. */
@CanIgnoreReturnValue
public Builder setLicenseUri(@Nullable String licenseUri) {
this.licenseUri = licenseUri == null ? null : Uri.parse(licenseUri);
return this;
}
/** Sets the optional request headers attached to DRM license requests. */
@CanIgnoreReturnValue
public Builder setLicenseRequestHeaders(Map<String, String> licenseRequestHeaders) {
this.licenseRequestHeaders = ImmutableMap.copyOf(licenseRequestHeaders);
return this;
}
/** Sets whether multi session is enabled. */
@CanIgnoreReturnValue
public Builder setMultiSession(boolean multiSession) {
this.multiSession = multiSession;
return this;
}
/**
* Sets whether to always use the default DRM license server URI even if the media specifies
* its own DRM license server URI.
*/
@CanIgnoreReturnValue
public Builder setForceDefaultLicenseUri(boolean forceDefaultLicenseUri) {
this.forceDefaultLicenseUri = forceDefaultLicenseUri;
return this;
}
/**
* Sets whether clear samples within protected content should be played when keys for the
* encrypted part of the content have yet to be loaded.
*/
@CanIgnoreReturnValue
public Builder setPlayClearContentWithoutKey(boolean playClearContentWithoutKey) {
this.playClearContentWithoutKey = playClearContentWithoutKey;
return this;
}
/**
* @deprecated Use {@link #setForceSessionsForAudioAndVideoTracks(boolean)} instead.
*/
@CanIgnoreReturnValue
@UnstableApi
@Deprecated
@InlineMe(
replacement =
"this.setForceSessionsForAudioAndVideoTracks(forceSessionsForAudioAndVideoTracks)")
public Builder forceSessionsForAudioAndVideoTracks(
boolean forceSessionsForAudioAndVideoTracks) {
return setForceSessionsForAudioAndVideoTracks(forceSessionsForAudioAndVideoTracks);
}
/**
* Sets whether a DRM session should be used for clear tracks of type {@link
* C#TRACK_TYPE_VIDEO} and {@link C#TRACK_TYPE_AUDIO}.
*
* <p>This method overrides what has been set by previously calling {@link
* #setForcedSessionTrackTypes(List)}.
*/
@CanIgnoreReturnValue
public Builder setForceSessionsForAudioAndVideoTracks(
boolean forceSessionsForAudioAndVideoTracks) {
this.setForcedSessionTrackTypes(
forceSessionsForAudioAndVideoTracks
? ImmutableList.of(C.TRACK_TYPE_VIDEO, C.TRACK_TYPE_AUDIO)
: ImmutableList.of());
return this;
}
/**
* Sets a list of {@link C.TrackType track type} constants for which to use a DRM session even
* when the tracks are in the clear.
*
* <p>For the common case of using a DRM session for {@link C#TRACK_TYPE_VIDEO} and {@link
* C#TRACK_TYPE_AUDIO}, {@link #setForceSessionsForAudioAndVideoTracks(boolean)} can be used.
*
* <p>This method overrides what has been set by previously calling {@link
* #setForceSessionsForAudioAndVideoTracks(boolean)}.
*/
@CanIgnoreReturnValue
public Builder setForcedSessionTrackTypes(
List<@C.TrackType Integer> forcedSessionTrackTypes) {
this.forcedSessionTrackTypes = ImmutableList.copyOf(forcedSessionTrackTypes);
return this;
}
/**
* Sets the key set ID of the offline license.
*
* <p>The key set ID identifies an offline license. The ID is required to query, renew or
* release an existing offline license (see {@code DefaultDrmSessionManager#setMode(int
* mode,byte[] offlineLicenseKeySetId)}).
*/
@CanIgnoreReturnValue
public Builder setKeySetId(@Nullable byte[] keySetId) {
this.keySetId = keySetId != null ? Arrays.copyOf(keySetId, keySetId.length) : null;
return this;
}
public DrmConfiguration build() {
return new DrmConfiguration(this);
}
}
/** The UUID of the protection scheme. */
public final UUID scheme;
/**
* @deprecated Use {@link #scheme} instead.
*/
@UnstableApi @Deprecated public final UUID uuid;
/**
* Optional default DRM license server {@link Uri}. If {@code null} then the DRM license server
* must be specified by the media.
*/
@Nullable public final Uri licenseUri;
/**
* @deprecated Use {@link #licenseRequestHeaders} instead.
*/
@UnstableApi @Deprecated public final ImmutableMap<String, String> requestHeaders;
/** The headers to attach to requests sent to the DRM license server. */
public final ImmutableMap<String, String> licenseRequestHeaders;
/** Whether the DRM configuration is multi session enabled. */
public final boolean multiSession;
/**
* Whether clear samples within protected content should be played when keys for the encrypted
* part of the content have yet to be loaded.
*/
public final boolean playClearContentWithoutKey;
/**
* Whether to force use of {@link #licenseUri} even if the media specifies its own DRM license
* server URI.
*/
public final boolean forceDefaultLicenseUri;
/**
* @deprecated Use {@link #forcedSessionTrackTypes}.
*/
@UnstableApi @Deprecated public final ImmutableList<@C.TrackType Integer> sessionForClearTypes;
/**
* The types of tracks for which to always use a DRM session even if the content is unencrypted.
*/
public final ImmutableList<@C.TrackType Integer> forcedSessionTrackTypes;
@Nullable private final byte[] keySetId;
@SuppressWarnings("deprecation") // Setting deprecated field
private DrmConfiguration(Builder builder) {
checkState(!(builder.forceDefaultLicenseUri && builder.licenseUri == null));
this.scheme = checkNotNull(builder.scheme);
this.uuid = scheme;
this.licenseUri = builder.licenseUri;
this.requestHeaders = builder.licenseRequestHeaders;
this.licenseRequestHeaders = builder.licenseRequestHeaders;
this.multiSession = builder.multiSession;
this.forceDefaultLicenseUri = builder.forceDefaultLicenseUri;
this.playClearContentWithoutKey = builder.playClearContentWithoutKey;
this.sessionForClearTypes = builder.forcedSessionTrackTypes;
this.forcedSessionTrackTypes = builder.forcedSessionTrackTypes;
this.keySetId =
builder.keySetId != null
? Arrays.copyOf(builder.keySetId, builder.keySetId.length)
: null;
}
/** Returns the key set ID of the offline license. */
@Nullable
public byte[] getKeySetId() {
return keySetId != null ? Arrays.copyOf(keySetId, keySetId.length) : null;
}
/** Returns a {@link Builder} initialized with the values of this instance. */
public Builder buildUpon() {
return new Builder(this);
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof DrmConfiguration)) {
return false;
}
DrmConfiguration other = (DrmConfiguration) obj;
return scheme.equals(other.scheme)
&& Util.areEqual(licenseUri, other.licenseUri)
&& Util.areEqual(licenseRequestHeaders, other.licenseRequestHeaders)
&& multiSession == other.multiSession
&& forceDefaultLicenseUri == other.forceDefaultLicenseUri
&& playClearContentWithoutKey == other.playClearContentWithoutKey
&& forcedSessionTrackTypes.equals(other.forcedSessionTrackTypes)
&& Arrays.equals(keySetId, other.keySetId);
}
@Override
public int hashCode() {
int result = scheme.hashCode();
result = 31 * result + (licenseUri != null ? licenseUri.hashCode() : 0);
result = 31 * result + licenseRequestHeaders.hashCode();
result = 31 * result + (multiSession ? 1 : 0);
result = 31 * result + (forceDefaultLicenseUri ? 1 : 0);
result = 31 * result + (playClearContentWithoutKey ? 1 : 0);
result = 31 * result + forcedSessionTrackTypes.hashCode();
result = 31 * result + Arrays.hashCode(keySetId);
return result;
}
// Bundleable implementation.
private static final String FIELD_SCHEME = Util.intToStringMaxRadix(0);
private static final String FIELD_LICENSE_URI = Util.intToStringMaxRadix(1);
private static final String FIELD_LICENSE_REQUEST_HEADERS = Util.intToStringMaxRadix(2);
private static final String FIELD_MULTI_SESSION = Util.intToStringMaxRadix(3);
private static final String FIELD_PLAY_CLEAR_CONTENT_WITHOUT_KEY = Util.intToStringMaxRadix(4);
private static final String FIELD_FORCE_DEFAULT_LICENSE_URI = Util.intToStringMaxRadix(5);
private static final String FIELD_FORCED_SESSION_TRACK_TYPES = Util.intToStringMaxRadix(6);
private static final String FIELD_KEY_SET_ID = Util.intToStringMaxRadix(7);
/**
* An object that can restore {@link DrmConfiguration} from a {@link Bundle}.
*
* @deprecated Use {@link #fromBundle} instead.
*/
@UnstableApi
@Deprecated
@SuppressWarnings("deprecation") // Deprecated instance of deprecated class
public static final Creator<DrmConfiguration> CREATOR = DrmConfiguration::fromBundle;
/** Restores a {@code DrmConfiguration} from a {@link Bundle}. */
@UnstableApi
public static DrmConfiguration fromBundle(Bundle bundle) {
UUID scheme = UUID.fromString(checkNotNull(bundle.getString(FIELD_SCHEME)));
@Nullable Uri licenseUri = bundle.getParcelable(FIELD_LICENSE_URI);
Bundle licenseMapAsBundle =
BundleCollectionUtil.getBundleWithDefault(
bundle, FIELD_LICENSE_REQUEST_HEADERS, Bundle.EMPTY);
ImmutableMap<String, String> licenseRequestHeaders =
BundleCollectionUtil.bundleToStringImmutableMap(licenseMapAsBundle);
boolean multiSession = bundle.getBoolean(FIELD_MULTI_SESSION, false);
boolean playClearContentWithoutKey =
bundle.getBoolean(FIELD_PLAY_CLEAR_CONTENT_WITHOUT_KEY, false);
boolean forceDefaultLicenseUri = bundle.getBoolean(FIELD_FORCE_DEFAULT_LICENSE_URI, false);
ArrayList<@C.TrackType Integer> forcedSessionTrackTypesArray =
BundleCollectionUtil.getIntegerArrayListWithDefault(
bundle, FIELD_FORCED_SESSION_TRACK_TYPES, new ArrayList<>());
ImmutableList<@C.TrackType Integer> forcedSessionTrackTypes =
ImmutableList.copyOf(forcedSessionTrackTypesArray);
@Nullable byte[] keySetId = bundle.getByteArray(FIELD_KEY_SET_ID);
Builder builder = new Builder(scheme);
return builder
.setLicenseUri(licenseUri)
.setLicenseRequestHeaders(licenseRequestHeaders)
.setMultiSession(multiSession)
.setForceDefaultLicenseUri(forceDefaultLicenseUri)
.setPlayClearContentWithoutKey(playClearContentWithoutKey)
.setForcedSessionTrackTypes(forcedSessionTrackTypes)
.setKeySetId(keySetId)
.build();
}
@UnstableApi
@Override
public Bundle toBundle() {
Bundle bundle = new Bundle();
bundle.putString(FIELD_SCHEME, scheme.toString());
if (licenseUri != null) {
bundle.putParcelable(FIELD_LICENSE_URI, licenseUri);
}
if (!licenseRequestHeaders.isEmpty()) {
bundle.putBundle(
FIELD_LICENSE_REQUEST_HEADERS,
BundleCollectionUtil.stringMapToBundle(licenseRequestHeaders));
}
if (multiSession) {
bundle.putBoolean(FIELD_MULTI_SESSION, multiSession);
}
if (playClearContentWithoutKey) {
bundle.putBoolean(FIELD_PLAY_CLEAR_CONTENT_WITHOUT_KEY, playClearContentWithoutKey);
}
if (forceDefaultLicenseUri) {
bundle.putBoolean(FIELD_FORCE_DEFAULT_LICENSE_URI, forceDefaultLicenseUri);
}
if (!forcedSessionTrackTypes.isEmpty()) {
bundle.putIntegerArrayList(
FIELD_FORCED_SESSION_TRACK_TYPES, new ArrayList<>(forcedSessionTrackTypes));
}
if (keySetId != null) {
bundle.putByteArray(FIELD_KEY_SET_ID, keySetId);
}
return bundle;
}
}