-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDemoPlayer.java
509 lines (457 loc) · 19.4 KB
/
DemoPlayer.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
package com.axinom.drm.sample.player;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import com.axinom.drm.sample.R;
import com.axinom.drm.sample.license.LicenseManagerErrorCode;
import com.axinom.drm.sample.license.OfflineLicenseManager;
import com.axinom.drm.sample.license.interfaces.IOfflineLicenseManagerListener;
import com.axinom.drm.sample.license.internal.model.DrmMessage;
import com.axinom.drm.sample.license.internal.utils.DrmUtils;
import com.axinom.drm.sample.offline.AxDownloadService;
import com.axinom.drm.sample.offline.AxDownloadTracker;
import com.axinom.drm.sample.offline.AxOfflineManager;
import com.axinom.drm.sample.util.Utility;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.drm.DefaultDrmSessionManager;
import com.google.android.exoplayer2.drm.FrameworkMediaDrm;
import com.google.android.exoplayer2.drm.HttpMediaDrmCallback;
import com.google.android.exoplayer2.offline.DownloadHelper;
import com.google.android.exoplayer2.offline.DownloadRequest;
import com.google.android.exoplayer2.offline.DownloadService;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource;
import com.google.android.exoplayer2.source.hls.DefaultHlsDataSourceFactory;
import com.google.android.exoplayer2.source.hls.HlsMediaSource;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.CueGroup;
import com.google.android.exoplayer2.text.TextOutput;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.ui.StyledPlayerView;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.util.Util;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* A wrapper around {@link ExoPlayer} that provides a higher level interface.
*/
class DemoPlayer implements TextOutput, IOfflineLicenseManagerListener, Player.Listener {
// Parameters used to initialize the player
public static class Params {
MediaItem mediaItem;
long startPosition = 0;
boolean startOnPrepared = true;
boolean shouldPlayOffline = false;
}
public static class UnsupportedFormatException extends Exception { }
/**
* Listener interface for player events.
*/
public interface Listener {
void onPlayerLog(String message, String tag);
void onPlayerError(Exception e);
}
private static final String TAG = DemoPlayer.class.getSimpleName();
private ExoPlayer mPlayer;
private boolean mPlayerIsCreated = false;
private Context mContext;
// Manager class for offline licenses
private OfflineLicenseManager mOfflineLicenseManager;
// A factory for DataSource instances
private DataSource.Factory mMediaDataSourceFactory;
// Defines downloaded content
private DownloadRequest mDownloadRequest;
private Params mParams;
// A DrmSessionManager that supports playbacks using ExoMediaDrm
private DefaultDrmSessionManager mDrmSessionManager;
// Media format
private int mFormat;
private final CopyOnWriteArrayList<Listener> mListeners;
public DemoPlayer(Context context) {
mContext = context;
mListeners = new CopyOnWriteArrayList<>();
}
private boolean isCreated(){
return mPlayerIsCreated;
}
// A method for creating player using predefined parameters
private void playerCreate(Params params){
dispatchPlayerLog("Creating the player");
mMediaDataSourceFactory = buildDataSourceFactory();
MediaItem.DrmConfiguration drmConfiguration = Utility.getDrmConfiguration(params.mediaItem);
try {
mPlayerIsCreated = false;
// Only DASH and HLS formats are allowed for playback in this application
mFormat = Util.inferContentType(Utility.getPlaybackProperties(params.mediaItem).uri);
if (mFormat != C.CONTENT_TYPE_DASH && mFormat != C.CONTENT_TYPE_HLS) {
throw new UnsupportedFormatException();
}
if (drmConfiguration != null) {
mDrmSessionManager = buildDrmSessionManager(String.valueOf(drmConfiguration.licenseUri),
drmConfiguration.licenseRequestHeaders.get("X-AxDRM-Message"));
// OfflineLicenseManager should be initialized and license keys received only if
// offline playback is required
if (mParams.shouldPlayOffline) {
mOfflineLicenseManager = new OfflineLicenseManager(mContext);
mOfflineLicenseManager.setEventListener(this);
mOfflineLicenseManager.getLicenseKeys(
String.valueOf(Utility.getPlaybackProperties(params.mediaItem).uri));
}
}
} catch (UnsupportedFormatException | NullPointerException e){
dispatchPlayerError(e);
return;
}
// Defining DefaultTrackSelector for the player
DefaultTrackSelector trackSelector = new DefaultTrackSelector(mContext);
trackSelector.setParameters(
new DefaultTrackSelector.Parameters.Builder(mContext).setPreferredTextLanguage("eng"));
// Defining DefaultRenderersFactory for the player
DefaultRenderersFactory renderersFactory = new DefaultRenderersFactory(mContext);
try {
mPlayer = new ExoPlayer.Builder(mContext, renderersFactory)
.setTrackSelector(trackSelector)
.build();
mPlayer.addListener(this);
mPlayerIsCreated = true;
// Preparing the player can start here in case content should be played online (offline license
// has to be acquired first before starting the preparation process) or if the content is not
// protected.
if (!mParams.shouldPlayOffline || drmConfiguration == null) {
startPlayerPrepare();
}
} catch (NullPointerException e){
dispatchPlayerError(e);
}
}
// Returns a new DataSource factory
private DataSource.Factory buildDataSourceFactory() {
dispatchPlayerLog("Building DataSourceFactory");
// If offline playback is allowed and media has already been downloaded,
// use CacheDataSource from AxOfflineManager
if (mParams.shouldPlayOffline) {
AxDownloadTracker axDownloadTracker = AxOfflineManager.getInstance().getDownloadTracker();
if (axDownloadTracker != null) {
mDownloadRequest = axDownloadTracker.getDownloadRequest(
Utility.getPlaybackProperties(mParams.mediaItem).uri);
if (mDownloadRequest != null) {
return AxOfflineManager.getInstance().buildDataSourceFactory(mContext);
}
}
dispatchPlayerErrorMessage(mContext.getString(R.string.error_offline_playback));
return null;
} else {
return new DefaultDataSource.Factory(mContext, buildHttpDataSourceFactory());
}
}
private void dispatchPlayerLog(String message){
for (Listener listener : mListeners) {
listener.onPlayerLog(message, TAG);
}
}
private void dispatchPlayerError(Exception e){
for (Listener listener : mListeners) {
listener.onPlayerError(e);
}
}
private void dispatchPlayerErrorMessage(String message){
dispatchPlayerError(new Exception(message));
}
public void addListener(Listener listener) {
mListeners.add(listener);
}
// A method for building MediaSource for playback
private MediaSource buildMediaSource(Context context, Uri videoUri){
dispatchPlayerLog("Building MediaSource with videoUri = [" + videoUri + "]");
// If offline playback is allowed and media has already been downloaded,
// use the existing download request to create the media source
if (mParams.shouldPlayOffline) {
if (mDownloadRequest != null) {
dispatchPlayerLog(mContext.getString(R.string.player_offline_playback));
return DownloadHelper.createMediaSource(mDownloadRequest, mMediaDataSourceFactory, mDrmSessionManager);
}
dispatchPlayerErrorMessage(mContext.getString(R.string.error_offline_playback));
return null;
} else {
dispatchPlayerLog(mContext.getString(R.string.player_online_playback));
DataSource.Factory dataSourceFactory = new DefaultDataSource.Factory(context);
if (mFormat == C.CONTENT_TYPE_DASH) {
if (mDrmSessionManager != null) {
return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(dataSourceFactory), dataSourceFactory)
.setDrmSessionManagerProvider(unusedMediaItem -> mDrmSessionManager)
.createMediaSource(mParams.mediaItem);
} else {
return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(dataSourceFactory), dataSourceFactory)
.createMediaSource(mParams.mediaItem);
}
} else {
if (mDrmSessionManager != null) {
return new HlsMediaSource.Factory(
new DefaultHlsDataSourceFactory(dataSourceFactory))
.setDrmSessionManagerProvider(unusedMediaItem -> mDrmSessionManager)
.createMediaSource(mParams.mediaItem);
} else {
return new HlsMediaSource.Factory(
new DefaultHlsDataSourceFactory(dataSourceFactory))
.createMediaSource(mParams.mediaItem);
}
}
}
}
// A method for building HttpDataSource.Factory
private HttpDataSource.Factory buildHttpDataSourceFactory() {
return new DefaultHttpDataSource.Factory();
}
// A method for building DrmSessionManager
private DefaultDrmSessionManager buildDrmSessionManager(String licenseUrl, String drmToken) {
dispatchPlayerLog("Building DrmSessionManager with licenseUrl = [" +
licenseUrl + "] and drmToken = [" + drmToken + "]");
HttpMediaDrmCallback drmCallback = new HttpMediaDrmCallback(licenseUrl,
buildHttpDataSourceFactory());
// Here the license token is attached to license request
if (drmToken != null) {
drmCallback.setKeyRequestProperty("X-AxDRM-Message", drmToken);
}
return new DefaultDrmSessionManager.Builder()
.setUuidAndExoMediaDrmProvider(C.WIDEVINE_UUID, FrameworkMediaDrm.DEFAULT_PROVIDER)
.setMultiSession(true)
.build(drmCallback);
}
// A method for preparing the player for creation
public void prepare(Params params, StyledPlayerView playerView) {
dispatchPlayerLog("Preparing the player for creation");
mParams = params;
playerRelease();
playerCreate(params);
if (isCreated()) {
mPlayer.setPlayWhenReady(params.startOnPrepared);
playerView.setPlayer(mPlayer);
}
}
// A method for preparing the player
private void startPlayerPrepare() {
dispatchPlayerLog("Starting the preparation of the player for video playback");
MediaSource mediaSource = buildMediaSource(
mContext, Utility.getPlaybackProperties(mParams.mediaItem).uri);
if (mediaSource != null) {
mPlayer.setMediaSource(mediaSource);
mPlayer.prepare();
if (mParams.startPosition != 0){
mPlayer.seekTo(mParams.startPosition);
}
}
}
// General method for releasing the player
private void playerRelease(){
dispatchPlayerLog("Releasing the player");
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
// More specific method for releasing the player that also clears the context.
public void release() {
playerRelease();
mContext = null;
}
public long getCurrentPosition() {
if (mPlayerIsCreated) return mPlayer.getCurrentPosition();
return 0;
}
private void dumpSelectedTracks() {
TrackSelectionArray trackSelectionArray = mPlayer.getCurrentTrackSelections();
for (int i = 0; i < trackSelectionArray.length; i++){
TrackSelection trackSelection = trackSelectionArray.get(i);
if (trackSelection != null) {
Log.d(TAG, "trackSelection, index = " + i);
}
}
}
private void dumpAvailableTracks(){
TrackGroupArray trackGroups = mPlayer.getCurrentTrackGroups();
for (int i = 0; i < trackGroups.length; i++){
TrackGroup trackGroup = trackGroups.get(i);
for (int trackIndex = 0; trackIndex < trackGroup.length; trackIndex++){
Format format = trackGroup.getFormat(trackIndex);
Log.d(TAG, "group = " + i + ", track = " + trackIndex + ", format = " + format);
}
}
}
@Override
public void onPlaybackStateChanged(int state) {
String stateString;
switch (state){
case Player.STATE_IDLE:
stateString = "idle";
break;
case Player.STATE_BUFFERING:
stateString = "buffering";
break;
case Player.STATE_READY:
stateString = "ready";
dumpAvailableTracks();
dumpSelectedTracks();
break;
case Player.STATE_ENDED:
stateString = "ended";
break;
default:
stateString = "unknown state";
}
dispatchPlayerLog("Player state changed to = [" + state + " (" + stateString + ")]");
}
@Override
public void onPlayerError(@NonNull PlaybackException exception) {
dispatchPlayerError(exception);
}
// TextOutput deprecated implementation
@Override
public void onCues(@NonNull List<Cue> cues) {
dispatchPlayerLog("deprecated onCues() called with: cues = [" + cues + "]");
}
// TextOutput implementation
@Override
public void onCues(CueGroup cueGroup) {
dispatchPlayerLog("onCues() called with: cues = [" + cueGroup.cues + "]");
}
@Override
public void onLicenseDownloaded(String manifestUrl) {
// Not used player implementation
}
@Override
public void onLicenseDownloadedWithResult(String manifestUrl, byte[] keyIds) {
onOfflineLicenseAcquired(manifestUrl, keyIds);
}
@Override
public void onLicenseDownloadFailed(int code, String description, String manifestUrl) {
dispatchPlayerLog("onLicenseDownloadFailed for manifest: " + manifestUrl + " code: " + code);
}
@Override
public void onLicenseCheck(boolean isValid, String manifestUrl) {
// Not used in player implementation
}
@Override
public void onLicenseCheckFailed(int code, String description, String manifestUrl) {
// Not used in player implementation
}
@Override
public void onLicenseReleased(String manifestUrl) {
// Not used in player implementation
}
@Override
public void onLicenseReleaseFailed(int code, String description, String manifestUrl) {
// Not used in player implementation
}
@Override
public void onLicenseKeysRestored(String manifestUrl, byte[] keyIds) {
dispatchPlayerLog("onLicenseKeysRestored() called with: manifestUrl = [" + manifestUrl + "]");
onOfflineLicenseAcquired(manifestUrl, keyIds);
}
@Override
public void onLicenseRestoreFailed(int code, String description, String manifestUrl) {
dispatchPlayerLog("License restore failed for manifest: " + manifestUrl + " code: " + code);
onNoOfflineLicenseFound(manifestUrl, code);
}
@Override
public void onAllLicensesReleased() {
// Not used in player implementation
}
@Override
public void onAllLicensesReleaseFailed(int i, String s) {
// Not used in player implementation
}
// Called when offline license is acquired. Player can now be properly prepared
@SuppressWarnings("unused")
public void onOfflineLicenseAcquired(String manifestUrl, byte[] keyIds) {
dispatchPlayerLog("Offline license acquired.");
// Offline license acquired. Starting in MODE_QUERY
mDrmSessionManager.setMode(DefaultDrmSessionManager.MODE_QUERY, keyIds);
startPlayerPrepare();
}
// Called when no offline license is found.
public void onNoOfflineLicenseFound(String manifestUrl, int licenseErrorCode) {
dispatchPlayerLog("No offline license found.");
DrmMessage drmMessage = null;
MediaItem.DrmConfiguration drmConfiguration = Utility.getDrmConfiguration(mParams.mediaItem);
if (drmConfiguration != null && !TextUtils.isEmpty(
drmConfiguration.licenseRequestHeaders.get("X-AxDRM-Message"))) {
drmMessage = DrmUtils.parseDrmString(
drmConfiguration.licenseRequestHeaders.get("X-AxDRM-Message"));
if (drmMessage == null) {
dispatchPlayerErrorMessage(mContext.getString(R.string.error_drm_token_parse));
return;
}
}
// Check that if license persistent flag is true, then we can try to download license
if (drmMessage != null && drmMessage.persistent) {
dispatchPlayerLog("Drm message has persistent flag");
if (!hasConnection()) {
if (licenseErrorCode == LicenseManagerErrorCode.ERROR_308.getCode()) {
dispatchPlayerErrorMessage(mContext.getString(R.string.error_drm_keys_expired));
} else {
dispatchPlayerErrorMessage(mContext.getString(R.string.error_no_connection_for_license_download));
}
return;
}
if (TextUtils.isEmpty((CharSequence) drmConfiguration.licenseUri)) {
dispatchPlayerErrorMessage(mContext.getString(R.string.error_license_server));
return;
}
dispatchPlayerLog("Trying to download and save license.");
mOfflineLicenseManager.downloadLicenseWithResult(String.valueOf(drmConfiguration.licenseUri),
manifestUrl, drmConfiguration.licenseRequestHeaders.get("X-AxDRM-Message"), true);
} else {
dispatchPlayerErrorMessage(mContext.getString(R.string.error_drm_message_not_persistent));
}
}
/**
* Returns true if there is InternetConnection
*
* @return boolean
*/
private boolean hasConnection() {
final ConnectivityManager cm = (ConnectivityManager) mContext.getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean hasConnection = (cm != null && cm.getActiveNetworkInfo() != null);
dispatchPlayerLog("Device has network connection: " + hasConnection);
return hasConnection;
}
// Method for deleting currently playing video.
public void onDeletePressed() {
dispatchPlayerLog("Deleting the currently playing content.");
AxDownloadTracker axDownloadTracker = AxOfflineManager.getInstance().getDownloadTracker();
if (axDownloadTracker != null && axDownloadTracker.isDownloaded(
String.valueOf(Utility.getPlaybackProperties(mParams.mediaItem).uri))) {
// License is removed for the selected video
if (mOfflineLicenseManager != null) {
mOfflineLicenseManager = new OfflineLicenseManager(mContext);
mOfflineLicenseManager.setEventListener(this);
mOfflineLicenseManager.releaseLicense(
String.valueOf(Utility.getPlaybackProperties(mParams.mediaItem).uri));
}
// Removes a download
DownloadService.sendRemoveDownload(mContext, AxDownloadService.class,
axDownloadTracker.getDownloadRequest(
Utility.getPlaybackProperties(mParams.mediaItem).uri).id, false);
}
}
}