shaka-player 4.2.1 → 4.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/.github/workflows/build-and-test.yaml +31 -16
  2. package/.github/workflows/custom-actions/set-commit-status/action.yaml +1 -5
  3. package/.github/workflows/deflake.yaml +28 -0
  4. package/.github/workflows/selenium-lab-tests.yaml +13 -4
  5. package/AUTHORS +3 -0
  6. package/CHANGELOG.md +19 -0
  7. package/CONTRIBUTORS +3 -0
  8. package/demo/common/message_ids.js +1 -0
  9. package/demo/config.js +3 -1
  10. package/demo/locales/en.json +1 -0
  11. package/demo/locales/source.json +4 -0
  12. package/dist/demo.compiled.debug.js +7 -6
  13. package/dist/demo.compiled.debug.map +3 -3
  14. package/dist/demo.compiled.js +7 -6
  15. package/dist/demo.compiled.map +3 -3
  16. package/dist/deps.js +2 -2
  17. package/dist/locales.js +32 -32
  18. package/dist/receiver.compiled.debug.js +4 -3
  19. package/dist/receiver.compiled.debug.map +3 -3
  20. package/dist/receiver.compiled.js +4 -3
  21. package/dist/receiver.compiled.map +3 -3
  22. package/dist/shaka-player.compiled.d.ts +2 -2
  23. package/dist/shaka-player.compiled.debug.d.ts +2 -2
  24. package/dist/shaka-player.compiled.debug.externs.js +24 -24
  25. package/dist/shaka-player.compiled.debug.js +237 -229
  26. package/dist/shaka-player.compiled.debug.map +4 -4
  27. package/dist/shaka-player.compiled.externs.js +24 -24
  28. package/dist/shaka-player.compiled.js +744 -741
  29. package/dist/shaka-player.compiled.map +4 -4
  30. package/dist/shaka-player.ui.d.ts +2 -2
  31. package/dist/shaka-player.ui.debug.d.ts +2 -2
  32. package/dist/shaka-player.ui.debug.externs.js +24 -24
  33. package/dist/shaka-player.ui.debug.js +212 -203
  34. package/dist/shaka-player.ui.debug.map +4 -4
  35. package/dist/shaka-player.ui.externs.js +24 -24
  36. package/dist/shaka-player.ui.js +905 -901
  37. package/dist/shaka-player.ui.map +4 -4
  38. package/docs/jsdoc-template/publish.js +33 -0
  39. package/docs/jsdoc-template/tmpl/container.tmpl +4 -1
  40. package/docs/jsdoc.conf.json +1 -0
  41. package/externs/shaka/manifest_parser.js +4 -1
  42. package/externs/shaka/player.js +7 -1
  43. package/lib/ads/client_side_ad_manager.js +7 -0
  44. package/lib/cast/cast_sender.js +39 -14
  45. package/lib/cea/cea608_memory.js +4 -0
  46. package/lib/hls/hls_parser.js +30 -8
  47. package/lib/media/drm_engine.js +52 -1
  48. package/lib/media/media_source_engine.js +43 -15
  49. package/lib/media/segment_index.js +4 -1
  50. package/lib/media/streaming_engine.js +79 -14
  51. package/lib/offline/storage.js +1 -0
  52. package/lib/player.js +17 -5
  53. package/lib/text/ttml_text_parser.js +6 -0
  54. package/lib/util/platform.js +24 -0
  55. package/lib/util/player_configuration.js +4 -0
  56. package/lib/util/pssh.js +2 -1
  57. package/lib/util/stream_utils.js +2 -1
  58. package/package.json +1 -1
  59. package/test/cea/cea608_memory_unit.js +88 -0
  60. package/test/dash/dash_parser_content_protection_unit.js +1 -0
  61. package/test/dash/dash_parser_live_unit.js +1 -0
  62. package/test/dash/dash_parser_manifest_unit.js +1 -0
  63. package/test/dash/dash_parser_segment_base_unit.js +1 -0
  64. package/test/dash/dash_parser_segment_list_unit.js +1 -0
  65. package/test/dash/dash_parser_segment_template_unit.js +1 -0
  66. package/test/hls/hls_live_unit.js +169 -247
  67. package/test/hls/hls_parser_unit.js +60 -2
  68. package/test/media/drm_engine_unit.js +61 -0
  69. package/test/media/media_source_engine_integration.js +36 -0
  70. package/test/media/media_source_engine_unit.js +118 -1
  71. package/test/media/segment_index_unit.js +28 -2
  72. package/test/media/streaming_engine_integration.js +9 -7
  73. package/test/media/streaming_engine_unit.js +29 -0
  74. package/test/offline/offline_integration.js +3 -3
  75. package/test/player_integration.js +2 -2
  76. package/test/test/util/dash_parser_util.js +2 -0
  77. package/test/test/util/ttml_utils.js +0 -2
  78. package/test/text/ttml_text_parser_unit.js +46 -0
  79. package/test/ui/text_displayer_layout_unit.js +17 -2
  80. package/ui/controls.js +0 -7
@@ -440,6 +440,7 @@ function buildNav(members) {
440
440
  [members.externals, 'Externals', seen, linktoExternal],
441
441
  [members.namespaces, 'Namespaces', seen, linkto],
442
442
  [members.classes, 'Classes', seen, linkto],
443
+ [members.enums, 'Enums', seen, linkto],
443
444
  [members.interfaces, 'Interfaces', seen, linkto],
444
445
  [members.events, 'Events', seen, linkto],
445
446
  [members.mixins, 'Mixins', seen, linkto],
@@ -484,6 +485,7 @@ function fixUpType(type) {
484
485
  exports.publish = (taffyData, opts, tutorials) => {
485
486
  let classes;
486
487
  let conf;
488
+ let enums;
487
489
  let externals;
488
490
  let files;
489
491
  let fromDir;
@@ -733,6 +735,31 @@ exports.publish = (taffyData, opts, tutorials) => {
733
735
  });
734
736
 
735
737
  members = helper.getMembers(data);
738
+ members.enums = [];
739
+
740
+ data().each(doclet => {
741
+ if (doclet.isEnum) {
742
+ const hasParent = !!docletMap[doclet.memberof];
743
+
744
+ // Enums without parents (not attached to a class, but directly to a
745
+ // namespace) aren't recognized by jsdoc by default and need to be
746
+ // massaged into the docs separately.
747
+ if (!hasParent) {
748
+ // Temporarily call this a "class", so that we can fix the URL.
749
+ doclet.kind = 'class';
750
+ // Regenerate the URL, so that this enum gets its own page
751
+ // instead of a URL that links to an ID within a non-existent
752
+ // parent page.
753
+ delete helper.longnameToUrl[doclet.longname];
754
+ helper.createLink(doclet);
755
+ // Mark this as an enum (custom type), so that it doesn't get
756
+ // slotted with the classes.
757
+ doclet.kind = 'enum';
758
+ members.enums.push(doclet);
759
+ }
760
+ }
761
+ });
762
+
736
763
  members.tutorials = tutorials.children;
737
764
  if (conf.default.sortTutorialsByConfigOrder) {
738
765
  members.tutorials.sort((a, b) => {
@@ -778,6 +805,7 @@ exports.publish = (taffyData, opts, tutorials) => {
778
805
 
779
806
  // set up the lists that we'll use to generate pages
780
807
  classes = taffy(members.classes);
808
+ enums = taffy(members.enums);
781
809
  modules = taffy(members.modules);
782
810
  namespaces = taffy(members.namespaces);
783
811
  mixins = taffy(members.mixins);
@@ -786,6 +814,7 @@ exports.publish = (taffyData, opts, tutorials) => {
786
814
 
787
815
  Object.keys(helper.longnameToUrl).forEach(longname => {
788
816
  const myClasses = helper.find(classes, {longname: longname});
817
+ const myEnums = helper.find(enums, {longname: longname});
789
818
  const myExternals = helper.find(externals, {longname: longname});
790
819
  const myInterfaces = helper.find(interfaces, {longname: longname});
791
820
  const myMixins = helper.find(mixins, {longname: longname});
@@ -800,6 +829,10 @@ exports.publish = (taffyData, opts, tutorials) => {
800
829
  generate(`Class: ${myClasses[0].longname}`, myClasses, helper.longnameToUrl[longname]);
801
830
  }
802
831
 
832
+ if (myEnums.length) {
833
+ generate(`Enum: ${myEnums[0].longname}`, myEnums, helper.longnameToUrl[longname]);
834
+ }
835
+
803
836
  if (myNamespaces.length) {
804
837
  generate(`Namespace: ${myNamespaces[0].longname}`, myNamespaces, helper.longnameToUrl[longname]);
805
838
  }
@@ -144,7 +144,10 @@
144
144
  return m.longname && m.longname.indexOf('module:') !== 0;
145
145
  });
146
146
  }
147
- if (members && members.length && members.forEach) {
147
+
148
+ // Skip enums, since they already have their values listed under
149
+ // "Properties".
150
+ if (members && members.length && members.forEach && doc.kind != 'enum') {
148
151
  ?>
149
152
  <h3 class="subsection-title">Members</h3>
150
153
 
@@ -16,6 +16,7 @@
16
16
  "navOrder": [
17
17
  "Tutorials",
18
18
  "Classes",
19
+ "Enums",
19
20
  "Interfaces",
20
21
  "Events"
21
22
  ]
@@ -111,7 +111,8 @@ shaka.extern.ManifestParser = class {
111
111
  * onError: function(!shaka.util.Error),
112
112
  * isLowLatencyMode: function():boolean,
113
113
  * isAutoLowLatencyMode: function():boolean,
114
- * enableLowLatencyMode: function()
114
+ * enableLowLatencyMode: function(),
115
+ * updateDuration: function()
115
116
  * }}
116
117
  *
117
118
  * @description
@@ -147,6 +148,8 @@ shaka.extern.ManifestParser = class {
147
148
  * Return true if auto low latency streaming mode is enabled.
148
149
  * @property {function()} enableLowLatencyMode
149
150
  * Enable low latency streaming mode.
151
+ * @property {function()} updateDuration
152
+ * Update the presentation duration based on PresentationTimeline.
150
153
  * @exportDoc
151
154
  */
152
155
  shaka.extern.ManifestParser.PlayerInterface;
@@ -632,7 +632,8 @@ shaka.extern.AdvancedDrmConfiguration;
632
632
  * logLicenseExchange: boolean,
633
633
  * updateExpirationTime: number,
634
634
  * preferredKeySystems: !Array.<string>,
635
- * keySystemsMapping: !Object.<string, string>
635
+ * keySystemsMapping: !Object.<string, string>,
636
+ * parseInbandPsshEnabled: boolean
636
637
  * }}
637
638
  *
638
639
  * @property {shaka.extern.RetryParameters} retryParameters
@@ -675,6 +676,11 @@ shaka.extern.AdvancedDrmConfiguration;
675
676
  * Specifies the priorties of available DRM key systems.
676
677
  * @property {Object.<string, string>} keySystemsMapping
677
678
  * A map of key system name to key system name.
679
+ * @property {boolean} parseInbandPsshEnabled
680
+ * <i>Defaults to true on Xbox One, and false for all other browsers.</i><br>
681
+ * When true parse DRM init data from pssh boxes in media and init segments
682
+ * and ignore 'encrypted' events.
683
+ * This is required when using in-band key rotation on Xbox One.
678
684
  *
679
685
  * @exportDoc
680
686
  */
@@ -205,6 +205,13 @@ shaka.ads.ClientSideAdManager = class {
205
205
  this.video_.offsetHeight, viewMode);
206
206
  });
207
207
  this.resizeObserver_.observe(this.video_);
208
+ } else {
209
+ this.eventManager_.listen(document, 'fullscreenchange', () => {
210
+ const viewMode = this.isFullScreenEnabled_() ?
211
+ google.ima.ViewMode.FULLSCREEN : google.ima.ViewMode.NORMAL;
212
+ this.imaAdsManager_.resize(this.video_.offsetWidth,
213
+ this.video_.offsetHeight, viewMode);
214
+ });
208
215
  }
209
216
 
210
217
  // Single video and overlay ads will start at this time
@@ -192,21 +192,14 @@ shaka.cast.CastSender = class {
192
192
  // If the API becomes available before this instance dies, init will be
193
193
  // called again.
194
194
 
195
- // A note about this value: In our testing environment, we load both
196
- // uncompiled and compiled code. This global callback in uncompiled mode
197
- // can be overwritten by the same in compiled mode. The two versions will
198
- // each have their own instances_ map. Therefore the callback must have a
199
- // name, as opposed to being anonymous. This way, the CastSender tests
200
- // can invoke the named static method instead of using a global that could
201
- // be overwritten.
202
- if (!window.__onGCastApiAvailable) {
203
- window.__onGCastApiAvailable = shaka.cast.CastSender.onSdkLoaded_;
204
- }
205
- if (window.__onGCastApiAvailable != shaka.cast.CastSender.onSdkLoaded_) {
206
- shaka.log.alwaysWarn('A global Cast SDK hook is already installed! ' +
207
- 'Shaka Player will be unable to receive a notification when the ' +
208
- 'Cast SDK is ready.');
195
+ // Check if our callback is already installed.
196
+ if (window.__onGCastApiAvailable !== CastSender.onGCastApiAvailable_) {
197
+ // Save pre-existing __onGCastApiAvailable in order to restore later.
198
+ CastSender.__onGCastApiAvailable_ =
199
+ window.__onGCastApiAvailable || null;
200
+ window.__onGCastApiAvailable = CastSender.onGCastApiAvailable_;
209
201
  }
202
+
210
203
  return;
211
204
  }
212
205
 
@@ -757,6 +750,9 @@ shaka.cast.CastSender.hasReceivers_ = false;
757
750
  /** @private {chrome.cast.Session} */
758
751
  shaka.cast.CastSender.session_ = null;
759
752
 
753
+ /** @private {?function(boolean)} */
754
+ shaka.cast.CastSender.__onGCastApiAvailable_ = null;
755
+
760
756
  /**
761
757
  * A set of all living CastSender instances. The constructor and destroy
762
758
  * methods will add and remove instances from this set.
@@ -784,3 +780,32 @@ shaka.cast.CastSender.onSdkLoaded_ = (loaded) => {
784
780
  }
785
781
  }
786
782
  };
783
+
784
+ /**
785
+ * @param {boolean} available
786
+ * @private
787
+ */
788
+ shaka.cast.CastSender.onGCastApiAvailable_ = (available) => {
789
+ // Restore callback from saved.
790
+ if (shaka.cast.CastSender.__onGCastApiAvailable_) {
791
+ window.__onGCastApiAvailable =
792
+ shaka.cast.CastSender.__onGCastApiAvailable_;
793
+ } else {
794
+ delete window.__onGCastApiAvailable;
795
+ }
796
+ shaka.cast.CastSender.__onGCastApiAvailable_ = null;
797
+
798
+ // A note about this value: In our testing environment, we load both
799
+ // uncompiled and compiled code. This global callback in uncompiled mode
800
+ // can be overwritten by the same in compiled mode. The two versions will
801
+ // each have their own instances_ map. Therefore the callback must have a
802
+ // name, as opposed to being anonymous. This way, the CastSender tests
803
+ // can invoke the named static method instead of using a global that could
804
+ // be overwritten.
805
+ shaka.cast.CastSender.onSdkLoaded_(available);
806
+
807
+ // call restored callback (if any)
808
+ if (typeof window.__onGCastApiAvailable === 'function') {
809
+ window.__onGCastApiAvailable(available);
810
+ }
811
+ };
@@ -184,6 +184,10 @@ shaka.cea.Cea608Memory = class {
184
184
  * @param {number} count Count of rows to move.
185
185
  */
186
186
  moveRows(dst, src, count) {
187
+ if (src < 0 || dst < 0) {
188
+ return;
189
+ }
190
+
187
191
  if (dst >= src) {
188
192
  for (let i = count-1; i >= 0; i--) {
189
193
  this.rows_[dst + i] = this.rows_[src + i].map((e) => e);
@@ -284,6 +284,21 @@ shaka.hls.HlsParser = class {
284
284
  }
285
285
 
286
286
  await Promise.all(updates);
287
+
288
+ this.notifySegments_();
289
+
290
+ // If any hasEndList is false, the stream is still live.
291
+ const stillLive = streamInfos.some((s) => s.hasEndList == false);
292
+ if (!stillLive) {
293
+ // Convert the presentation to VOD and set the duration.
294
+ const PresentationType = shaka.hls.HlsParser.PresentationType_;
295
+ this.setPresentationType_(PresentationType.VOD);
296
+
297
+ const maxTimestamps = streamInfos.map((s) => s.maxTimestamp);
298
+ // The duration is the minimum of the end times of all streams.
299
+ this.presentationTimeline_.setDuration(Math.min(...maxTimestamps));
300
+ this.playerInterface_.updateDuration();
301
+ }
287
302
  }
288
303
 
289
304
  /**
@@ -294,7 +309,6 @@ shaka.hls.HlsParser = class {
294
309
  * @private
295
310
  */
296
311
  async updateStream_(streamInfo) {
297
- const PresentationType = shaka.hls.HlsParser.PresentationType_;
298
312
  const manifestUri = streamInfo.absoluteMediaPlaylistUri;
299
313
  const uriObj = new goog.Uri(manifestUri);
300
314
  if (this.lowLatencyMode_ && streamInfo.canSkipSegments) {
@@ -327,6 +341,7 @@ shaka.hls.HlsParser = class {
327
341
  streamInfo.verbatimMediaPlaylistUri, playlist, stream.type,
328
342
  stream.mimeType, streamInfo.mediaSequenceToStartTime, mediaVariables,
329
343
  stream.codecs);
344
+ this.segmentsToNotifyByStream_.push(segments);
330
345
 
331
346
  stream.segmentIndex.mergeAndEvict(
332
347
  segments, this.presentationTimeline_.getSegmentAvailabilityStart());
@@ -347,10 +362,10 @@ shaka.hls.HlsParser = class {
347
362
  shaka.hls.Utils.getFirstTagWithName(playlist.tags, 'EXT-X-ENDLIST');
348
363
 
349
364
  if (endListTag) {
350
- // Convert the presentation to VOD and set the duration to the last
351
- // segment's end time.
352
- this.setPresentationType_(PresentationType.VOD);
353
- this.presentationTimeline_.setDuration(newestSegment.endTime);
365
+ // Flag this for later. We don't convert the whole presentation into VOD
366
+ // until we've seen the ENDLIST tag for all active playlists.
367
+ streamInfo.hasEndList = true;
368
+ streamInfo.maxTimestamp = newestSegment.endTime;
354
369
  }
355
370
  }
356
371
 
@@ -1707,6 +1722,7 @@ shaka.hls.HlsParser = class {
1707
1722
  maxTimestamp: lastEndTime,
1708
1723
  mediaSequenceToStartTime,
1709
1724
  canSkipSegments,
1725
+ hasEndList: false,
1710
1726
  };
1711
1727
  }
1712
1728
 
@@ -2557,8 +2573,11 @@ shaka.hls.HlsParser = class {
2557
2573
  try {
2558
2574
  await this.update();
2559
2575
 
2560
- const delay = this.updatePlaylistDelay_;
2561
- this.updatePlaylistTimer_.tickAfter(/* seconds= */ delay);
2576
+ // This may have converted to VOD, in which case we stop updating.
2577
+ if (this.isLive_()) {
2578
+ const delay = this.updatePlaylistDelay_;
2579
+ this.updatePlaylistTimer_.tickAfter(/* seconds= */ delay);
2580
+ }
2562
2581
  } catch (error) {
2563
2582
  // Detect a call to stop() during this.update()
2564
2583
  if (!this.playerInterface_) {
@@ -2749,7 +2768,8 @@ shaka.hls.HlsParser = class {
2749
2768
  * absoluteMediaPlaylistUri: string,
2750
2769
  * maxTimestamp: number,
2751
2770
  * mediaSequenceToStartTime: !Map.<number, number>,
2752
- * canSkipSegments: boolean
2771
+ * canSkipSegments: boolean,
2772
+ * hasEndList: boolean
2753
2773
  * }}
2754
2774
  *
2755
2775
  * @description
@@ -2772,6 +2792,8 @@ shaka.hls.HlsParser = class {
2772
2792
  * @property {boolean} canSkipSegments
2773
2793
  * True if the server supports delta playlist updates, and we can send a
2774
2794
  * request for a playlist that can skip older media segments.
2795
+ * @property {boolean} hasEndList
2796
+ * True if the stream has an EXT-X-ENDLIST tag.
2775
2797
  */
2776
2798
  shaka.hls.HlsParser.StreamInfo;
2777
2799
 
@@ -18,9 +18,11 @@ goog.require('shaka.util.FakeEvent');
18
18
  goog.require('shaka.util.IDestroyable');
19
19
  goog.require('shaka.util.Iterables');
20
20
  goog.require('shaka.util.Lazy');
21
+ goog.require('shaka.util.ManifestParserUtils');
21
22
  goog.require('shaka.util.MapUtils');
22
23
  goog.require('shaka.util.MimeUtils');
23
24
  goog.require('shaka.util.Platform');
25
+ goog.require('shaka.util.Pssh');
24
26
  goog.require('shaka.util.PublicPromise');
25
27
  goog.require('shaka.util.StreamUtils');
26
28
  goog.require('shaka.util.StringUtils');
@@ -497,7 +499,11 @@ shaka.media.DrmEngine = class {
497
499
 
498
500
  // Explicit init data for any one stream or an offline session is
499
501
  // sufficient to suppress 'encrypted' events for all streams.
500
- if (!manifestInitData && !this.offlineSessionIds_.length) {
502
+ // Also suppress 'encrypted' events when parsing in-band ppsh
503
+ // from media segments because that serves the same purpose as the
504
+ // 'encrypted' events.
505
+ if (!manifestInitData && !this.offlineSessionIds_.length &&
506
+ !this.config_.parseInbandPsshEnabled) {
501
507
  this.eventManager_.listen(
502
508
  this.video_, 'encrypted', (e) => this.onEncryptedEvent_(e));
503
509
  }
@@ -668,6 +674,14 @@ shaka.media.DrmEngine = class {
668
674
  }
669
675
  }
670
676
 
677
+ // If there are pre-existing sessions that have all been loaded
678
+ // then reset the allSessionsLoaded_ promise, which can now be
679
+ // used to wait for new sesssions to be loaded
680
+ if (this.activeSessions_.size > 0 && this.areAllSessionsLoaded_()) {
681
+ this.allSessionsLoaded_.resolve();
682
+ this.allSessionsLoaded_ = new shaka.util.PublicPromise();
683
+ this.allSessionsLoaded_.catch(() => {});
684
+ }
671
685
  this.createSession(initDataType, initData,
672
686
  this.currentDrmInfo_.sessionType);
673
687
  }
@@ -2327,6 +2341,43 @@ shaka.media.DrmEngine = class {
2327
2341
  }
2328
2342
  }
2329
2343
  }
2344
+
2345
+ /**
2346
+ * Parse pssh from a media segment and announce new initData
2347
+ *
2348
+ * @param {shaka.util.ManifestParserUtils.ContentType} contentType
2349
+ * @param {!BufferSource} mediaSegment
2350
+ * @return {!Promise<void>}
2351
+ */
2352
+ parseInbandPssh(contentType, mediaSegment) {
2353
+ if (!this.config_.parseInbandPsshEnabled) {
2354
+ return Promise.resolve();
2355
+ }
2356
+
2357
+ const ContentType = shaka.util.ManifestParserUtils.ContentType;
2358
+ if (![ContentType.AUDIO, ContentType.VIDEO].includes(contentType)) {
2359
+ return Promise.resolve();
2360
+ }
2361
+
2362
+ const pssh = new shaka.util.Pssh(
2363
+ shaka.util.BufferUtils.toUint8(mediaSegment));
2364
+
2365
+ let totalLength = 0;
2366
+ for (const data of pssh.data) {
2367
+ totalLength += data.length;
2368
+ }
2369
+ if (totalLength == 0) {
2370
+ return Promise.resolve();
2371
+ }
2372
+ const combinedData = new Uint8Array(totalLength);
2373
+ let pos = 0;
2374
+ for (const data of pssh.data) {
2375
+ combinedData.set(data, pos);
2376
+ pos += data.length;
2377
+ }
2378
+ this.newInitData('cenc', combinedData);
2379
+ return this.allSessionsLoaded_;
2380
+ }
2330
2381
  };
2331
2382
 
2332
2383
 
@@ -522,9 +522,13 @@ shaka.media.MediaSourceEngine = class {
522
522
  * @param {?boolean} hasClosedCaptions True if the buffer contains CEA closed
523
523
  * captions
524
524
  * @param {boolean=} seeked True if we just seeked
525
+ * @param {boolean=} adaptation True if we just automatically switched active
526
+ * variant(s).
525
527
  * @return {!Promise}
526
528
  */
527
- async appendBuffer(contentType, data, reference, hasClosedCaptions, seeked) {
529
+ async appendBuffer(
530
+ contentType, data, reference, hasClosedCaptions, seeked = false,
531
+ adaptation = false) {
528
532
  const ContentType = shaka.util.ManifestParserUtils.ContentType;
529
533
 
530
534
  if (contentType == ContentType.TEXT) {
@@ -625,6 +629,14 @@ shaka.media.MediaSourceEngine = class {
625
629
 
626
630
  await this.enqueueOperation_(
627
631
  contentType, () => this.append_(contentType, data));
632
+ // If the input buffer passed to SourceBuffer#appendBuffer() does not
633
+ // contain a complete media segment, the call will exit while the
634
+ // SourceBuffer's append state is
635
+ // still PARSING_MEDIA_SEGMENT. Reset the parser state by calling
636
+ // abort() to safely reset timestampOffset to 'originalOffset'.
637
+ // https://www.w3.org/TR/media-source-2/#sourcebuffer-segment-parser-loop
638
+ await this.enqueueOperation_(
639
+ contentType, () => this.abort_(contentType));
628
640
 
629
641
  // Reset the offset and append window.
630
642
  sourceBuffer.timestampOffset = originalOffset;
@@ -638,10 +650,17 @@ shaka.media.MediaSourceEngine = class {
638
650
  const textOffset = (reference.startTime || 0) - (mediaStartTime || 0);
639
651
  this.textSequenceModeOffset_.resolve(textOffset);
640
652
 
641
- // Finally, clear the buffer.
653
+ // Clear the buffer.
642
654
  await this.enqueueOperation_(
643
655
  contentType,
644
656
  () => this.remove_(contentType, 0, this.mediaSource_.duration));
657
+
658
+ // Finally, flush the buffer in case of choppy video start on HLS fMP4.
659
+ if (contentType == ContentType.VIDEO) {
660
+ await this.enqueueOperation_(
661
+ contentType,
662
+ () => this.flush_(contentType));
663
+ }
645
664
  }
646
665
 
647
666
  // Now switch to sequence mode and fall through to our normal operations.
@@ -650,10 +669,15 @@ shaka.media.MediaSourceEngine = class {
650
669
 
651
670
  if (reference && this.sequenceMode_ && contentType != ContentType.TEXT) {
652
671
  // In sequence mode, for non-text streams, if we just cleared the buffer
653
- // and are performing an unbuffered seek, we need to set a new
654
- // timestampOffset on the sourceBuffer.
655
- if (seeked) {
672
+ // and are either performing an unbuffered seek or handling an automatic
673
+ // adaptation, we need to set a new timestampOffset on the sourceBuffer.
674
+ if (seeked || adaptation) {
656
675
  const timestampOffset = reference.startTime;
676
+ // The logic to call abort() before setting the timestampOffset is
677
+ // extended during unbuffered seeks or automatic adaptations; it is
678
+ // possible for the append state to be PARSING_MEDIA_SEGMENT from the
679
+ // previous SourceBuffer#appendBuffer() call.
680
+ this.enqueueOperation_(contentType, () => this.abort_(contentType));
657
681
  this.enqueueOperation_(
658
682
  contentType,
659
683
  () => this.setTimestampOffset_(contentType, timestampOffset));
@@ -849,21 +873,25 @@ shaka.media.MediaSourceEngine = class {
849
873
  }
850
874
 
851
875
  /**
852
- * We only support increasing duration at this time. Decreasing duration
853
- * causes the MSE removal algorithm to run, which results in an 'updateend'
854
- * event. Supporting this scenario would be complicated, and is not currently
855
- * needed.
856
- *
857
876
  * @param {number} duration
858
877
  * @return {!Promise}
859
878
  */
860
879
  async setDuration(duration) {
861
- goog.asserts.assert(
862
- isNaN(this.mediaSource_.duration) ||
863
- this.mediaSource_.duration <= duration,
864
- 'duration cannot decrease: ' + this.mediaSource_.duration + ' -> ' +
865
- duration);
866
880
  await this.enqueueBlockingOperation_(() => {
881
+ // Reducing the duration causes the MSE removal algorithm to run, which
882
+ // triggers an 'updateend' event to fire. To handle this scenario, we
883
+ // have to insert a dummy operation into the beginning of each queue,
884
+ // which the 'updateend' handler will remove.
885
+ if (duration < this.mediaSource_.duration) {
886
+ for (const contentType in this.sourceBuffers_) {
887
+ const dummyOperation = {
888
+ start: () => {},
889
+ p: new shaka.util.PublicPromise(),
890
+ };
891
+ this.queues_[contentType].unshift(dummyOperation);
892
+ }
893
+ }
894
+
867
895
  this.mediaSource_.duration = duration;
868
896
  });
869
897
  }
@@ -364,7 +364,10 @@ shaka.media.SegmentIndex = class {
364
364
  lastReference.partialReferences,
365
365
  lastReference.tilesLayout,
366
366
  lastReference.tileDuration,
367
- lastReference.syncTime);
367
+ lastReference.syncTime,
368
+ lastReference.status,
369
+ lastReference.hlsAes128Key,
370
+ );
368
371
  }
369
372
 
370
373