@@ -9,6 +9,7 @@ goog.provide('shaka.media.DrmEngine');
9
9
goog . require ( 'goog.asserts' ) ;
10
10
goog . require ( 'shaka.log' ) ;
11
11
goog . require ( 'shaka.net.NetworkingEngine' ) ;
12
+ goog . require ( 'shaka.util.ArrayUtils' ) ;
12
13
goog . require ( 'shaka.util.BufferUtils' ) ;
13
14
goog . require ( 'shaka.util.Destroyer' ) ;
14
15
goog . require ( 'shaka.util.DrmUtils' ) ;
@@ -67,12 +68,18 @@ shaka.media.DrmEngine = class {
67
68
*/
68
69
this . activeSessions_ = new Map ( ) ;
69
70
71
+ /** @private {!Array.<!shaka.net.NetworkingEngine.PendingRequest>} */
72
+ this . activeRequests_ = [ ] ;
73
+
70
74
/**
71
75
* @private {!Map<string,
72
76
* {initData: ?Uint8Array, initDataType: ?string}> }
73
77
*/
74
78
this . storedPersistentSessions_ = new Map ( ) ;
75
79
80
+ /** @private {boolean} */
81
+ this . hasInitData_ = false ;
82
+
76
83
/** @private {!shaka.util.PublicPromise} */
77
84
this . allSessionsLoaded_ = new shaka . util . PublicPromise ( ) ;
78
85
@@ -706,6 +713,7 @@ shaka.media.DrmEngine = class {
706
713
707
714
// Reset the promise for the next sessions to come if key needs aren't
708
715
// satisfied with persistent sessions
716
+ this . hasInitData_ = false ;
709
717
this . allSessionsLoaded_ = new shaka . util . PublicPromise ( ) ;
710
718
this . allSessionsLoaded_ . catch ( ( ) => { } ) ;
711
719
}
@@ -767,11 +775,16 @@ shaka.media.DrmEngine = class {
767
775
}
768
776
}
769
777
778
+ // Mark that there is init data, so that the preloader will know to wait
779
+ // for sessions to be loaded.
780
+ this . hasInitData_ = true ;
781
+
770
782
// If there are pre-existing sessions that have all been loaded
771
783
// then reset the allSessionsLoaded_ promise, which can now be
772
784
// used to wait for new sesssions to be loaded
773
785
if ( this . activeSessions_ . size > 0 && this . areAllSessionsLoaded_ ( ) ) {
774
786
this . allSessionsLoaded_ . resolve ( ) ;
787
+ this . hasInitData_ = false ;
775
788
this . allSessionsLoaded_ = new shaka . util . PublicPromise ( ) ;
776
789
this . allSessionsLoaded_ . catch ( ( ) => { } ) ;
777
790
}
@@ -1381,6 +1394,14 @@ shaka.media.DrmEngine = class {
1381
1394
this . video_ . paused && ! this . initialRequestsSent_ ) ;
1382
1395
}
1383
1396
1397
+ /** @return {!Promise } */
1398
+ async waitForActiveRequests ( ) {
1399
+ if ( this . hasInitData_ ) {
1400
+ await this . allSessionsLoaded_ ;
1401
+ await Promise . all ( this . activeRequests_ . map ( ( req ) => req . promise ) ) ;
1402
+ }
1403
+ }
1404
+
1384
1405
/**
1385
1406
* Sends a license request.
1386
1407
* @param {!MediaKeyMessageEvent } event
@@ -1440,7 +1461,9 @@ shaka.media.DrmEngine = class {
1440
1461
try {
1441
1462
const req = this . playerInterface_ . netEngine . request (
1442
1463
requestType , request , { isPreload : this . isPreload_ ( ) } ) ;
1464
+ this . activeRequests_ . push ( req ) ;
1443
1465
response = await req . promise ;
1466
+ shaka . util . ArrayUtils . remove ( this . activeRequests_ , req ) ;
1444
1467
} catch ( error ) {
1445
1468
if ( this . destroyer_ . destroyed ( ) ) {
1446
1469
return ;
0 commit comments