shaka-player 4.1.3 → 4.1.5

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 (85) 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 +168 -54
  5. package/AUTHORS +2 -0
  6. package/CHANGELOG.md +29 -0
  7. package/CONTRIBUTORS +2 -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/controls.css +1 -1
  13. package/dist/controls.css.map +1 -1
  14. package/dist/demo.compiled.debug.js +6 -6
  15. package/dist/demo.compiled.debug.map +2 -2
  16. package/dist/demo.compiled.js +6 -6
  17. package/dist/demo.compiled.map +2 -2
  18. package/dist/deps.js +1 -1
  19. package/dist/locales.js +31 -31
  20. package/dist/receiver.compiled.debug.js +3 -3
  21. package/dist/receiver.compiled.debug.map +2 -2
  22. package/dist/receiver.compiled.js +3 -3
  23. package/dist/receiver.compiled.map +2 -2
  24. package/dist/shaka-player.compiled.d.ts +2 -2
  25. package/dist/shaka-player.compiled.debug.d.ts +2 -2
  26. package/dist/shaka-player.compiled.debug.externs.js +24 -24
  27. package/dist/shaka-player.compiled.debug.js +209 -200
  28. package/dist/shaka-player.compiled.debug.map +4 -4
  29. package/dist/shaka-player.compiled.externs.js +24 -24
  30. package/dist/shaka-player.compiled.js +736 -733
  31. package/dist/shaka-player.compiled.map +4 -4
  32. package/dist/shaka-player.ui.d.ts +2 -2
  33. package/dist/shaka-player.ui.debug.d.ts +2 -2
  34. package/dist/shaka-player.ui.debug.externs.js +24 -24
  35. package/dist/shaka-player.ui.debug.js +263 -254
  36. package/dist/shaka-player.ui.debug.map +4 -4
  37. package/dist/shaka-player.ui.externs.js +24 -24
  38. package/dist/shaka-player.ui.js +900 -896
  39. package/dist/shaka-player.ui.map +4 -4
  40. package/docs/jsdoc-template/publish.js +33 -0
  41. package/docs/jsdoc-template/tmpl/container.tmpl +4 -1
  42. package/docs/jsdoc.conf.json +1 -0
  43. package/externs/shaka/manifest_parser.js +4 -1
  44. package/externs/shaka/player.js +7 -1
  45. package/lib/ads/client_side_ad_manager.js +7 -0
  46. package/lib/cast/cast_sender.js +39 -14
  47. package/lib/cea/cea608_memory.js +4 -0
  48. package/lib/hls/hls_parser.js +30 -8
  49. package/lib/media/drm_engine.js +52 -1
  50. package/lib/media/media_source_engine.js +43 -15
  51. package/lib/media/streaming_engine.js +79 -14
  52. package/lib/offline/storage.js +1 -0
  53. package/lib/player.js +29 -5
  54. package/lib/text/ttml_text_parser.js +6 -0
  55. package/lib/text/ui_text_displayer.js +8 -0
  56. package/lib/util/platform.js +24 -0
  57. package/lib/util/player_configuration.js +5 -0
  58. package/lib/util/pssh.js +2 -1
  59. package/lib/util/stream_utils.js +2 -1
  60. package/package.json +1 -1
  61. package/test/cea/cea608_memory_unit.js +88 -0
  62. package/test/dash/dash_parser_content_protection_unit.js +1 -0
  63. package/test/dash/dash_parser_live_unit.js +1 -0
  64. package/test/dash/dash_parser_manifest_unit.js +1 -0
  65. package/test/dash/dash_parser_segment_base_unit.js +1 -0
  66. package/test/dash/dash_parser_segment_list_unit.js +1 -0
  67. package/test/dash/dash_parser_segment_template_unit.js +1 -0
  68. package/test/hls/hls_live_unit.js +169 -247
  69. package/test/hls/hls_parser_unit.js +60 -2
  70. package/test/media/drm_engine_unit.js +90 -0
  71. package/test/media/media_source_engine_integration.js +36 -0
  72. package/test/media/media_source_engine_unit.js +118 -1
  73. package/test/media/streaming_engine_integration.js +9 -7
  74. package/test/media/streaming_engine_unit.js +29 -0
  75. package/test/offline/offline_integration.js +3 -3
  76. package/test/player_integration.js +2 -2
  77. package/test/player_src_equals_integration.js +2 -0
  78. package/test/player_unit.js +39 -1
  79. package/test/test/assets/small.mp4 +0 -0
  80. package/test/test/util/dash_parser_util.js +2 -0
  81. package/test/test/util/ttml_utils.js +0 -2
  82. package/test/text/ttml_text_parser_unit.js +46 -0
  83. package/test/ui/text_displayer_layout_unit.js +17 -2
  84. package/ui/controls.js +0 -7
  85. package/ui/less/ad_controls.less +4 -0
@@ -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);
@@ -287,6 +287,21 @@ shaka.hls.HlsParser = class {
287
287
  }
288
288
 
289
289
  await Promise.all(updates);
290
+
291
+ this.notifySegments_();
292
+
293
+ // If any hasEndList is false, the stream is still live.
294
+ const stillLive = streamInfos.some((s) => s.hasEndList == false);
295
+ if (!stillLive) {
296
+ // Convert the presentation to VOD and set the duration.
297
+ const PresentationType = shaka.hls.HlsParser.PresentationType_;
298
+ this.setPresentationType_(PresentationType.VOD);
299
+
300
+ const maxTimestamps = streamInfos.map((s) => s.maxTimestamp);
301
+ // The duration is the minimum of the end times of all streams.
302
+ this.presentationTimeline_.setDuration(Math.min(...maxTimestamps));
303
+ this.playerInterface_.updateDuration();
304
+ }
290
305
  }
291
306
 
292
307
  /**
@@ -297,7 +312,6 @@ shaka.hls.HlsParser = class {
297
312
  * @private
298
313
  */
299
314
  async updateStream_(streamInfo) {
300
- const PresentationType = shaka.hls.HlsParser.PresentationType_;
301
315
  const manifestUri = streamInfo.absoluteMediaPlaylistUri;
302
316
  const uriObj = new goog.Uri(manifestUri);
303
317
  if (this.lowLatencyMode_ && streamInfo.canSkipSegments) {
@@ -330,6 +344,7 @@ shaka.hls.HlsParser = class {
330
344
  streamInfo.verbatimMediaPlaylistUri, playlist, stream.type,
331
345
  stream.mimeType, streamInfo.mediaSequenceToStartTime, mediaVariables,
332
346
  stream.codecs);
347
+ this.segmentsToNotifyByStream_.push(segments);
333
348
 
334
349
  stream.segmentIndex.mergeAndEvict(
335
350
  segments, this.presentationTimeline_.getSegmentAvailabilityStart());
@@ -350,10 +365,10 @@ shaka.hls.HlsParser = class {
350
365
  shaka.hls.Utils.getFirstTagWithName(playlist.tags, 'EXT-X-ENDLIST');
351
366
 
352
367
  if (endListTag) {
353
- // Convert the presentation to VOD and set the duration to the last
354
- // segment's end time.
355
- this.setPresentationType_(PresentationType.VOD);
356
- this.presentationTimeline_.setDuration(newestSegment.endTime);
368
+ // Flag this for later. We don't convert the whole presentation into VOD
369
+ // until we've seen the ENDLIST tag for all active playlists.
370
+ streamInfo.hasEndList = true;
371
+ streamInfo.maxTimestamp = newestSegment.endTime;
357
372
  }
358
373
  }
359
374
 
@@ -1761,6 +1776,7 @@ shaka.hls.HlsParser = class {
1761
1776
  maxTimestamp: lastEndTime,
1762
1777
  mediaSequenceToStartTime,
1763
1778
  canSkipSegments,
1779
+ hasEndList: false,
1764
1780
  };
1765
1781
  }
1766
1782
 
@@ -2523,8 +2539,11 @@ shaka.hls.HlsParser = class {
2523
2539
  try {
2524
2540
  await this.update();
2525
2541
 
2526
- const delay = this.updatePlaylistDelay_;
2527
- this.updatePlaylistTimer_.tickAfter(/* seconds= */ delay);
2542
+ // This may have converted to VOD, in which case we stop updating.
2543
+ if (this.isLive_()) {
2544
+ const delay = this.updatePlaylistDelay_;
2545
+ this.updatePlaylistTimer_.tickAfter(/* seconds= */ delay);
2546
+ }
2528
2547
  } catch (error) {
2529
2548
  // Detect a call to stop() during this.update()
2530
2549
  if (!this.playerInterface_) {
@@ -2707,7 +2726,8 @@ shaka.hls.HlsParser = class {
2707
2726
  * absoluteMediaPlaylistUri: string,
2708
2727
  * maxTimestamp: number,
2709
2728
  * mediaSequenceToStartTime: !Map.<number, number>,
2710
- * canSkipSegments: boolean
2729
+ * canSkipSegments: boolean,
2730
+ * hasEndList: boolean
2711
2731
  * }}
2712
2732
  *
2713
2733
  * @description
@@ -2730,6 +2750,8 @@ shaka.hls.HlsParser = class {
2730
2750
  * @property {boolean} canSkipSegments
2731
2751
  * True if the server supports delta playlist updates, and we can send a
2732
2752
  * request for a playlist that can skip older media segments.
2753
+ * @property {boolean} hasEndList
2754
+ * True if the stream has an EXT-X-ENDLIST tag.
2733
2755
  */
2734
2756
  shaka.hls.HlsParser.StreamInfo;
2735
2757
 
@@ -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
  }
@@ -2329,6 +2343,43 @@ shaka.media.DrmEngine = class {
2329
2343
  }
2330
2344
  }
2331
2345
  }
2346
+
2347
+ /**
2348
+ * Parse pssh from a media segment and announce new initData
2349
+ *
2350
+ * @param {shaka.util.ManifestParserUtils.ContentType} contentType
2351
+ * @param {!BufferSource} mediaSegment
2352
+ * @return {!Promise<void>}
2353
+ */
2354
+ parseInbandPssh(contentType, mediaSegment) {
2355
+ if (!this.config_.parseInbandPsshEnabled) {
2356
+ return Promise.resolve();
2357
+ }
2358
+
2359
+ const ContentType = shaka.util.ManifestParserUtils.ContentType;
2360
+ if (![ContentType.AUDIO, ContentType.VIDEO].includes(contentType)) {
2361
+ return Promise.resolve();
2362
+ }
2363
+
2364
+ const pssh = new shaka.util.Pssh(
2365
+ shaka.util.BufferUtils.toUint8(mediaSegment));
2366
+
2367
+ let totalLength = 0;
2368
+ for (const data of pssh.data) {
2369
+ totalLength += data.length;
2370
+ }
2371
+ if (totalLength == 0) {
2372
+ return Promise.resolve();
2373
+ }
2374
+ const combinedData = new Uint8Array(totalLength);
2375
+ let pos = 0;
2376
+ for (const data of pssh.data) {
2377
+ combinedData.set(data, pos);
2378
+ pos += data.length;
2379
+ }
2380
+ this.newInitData('cenc', combinedData);
2381
+ return this.allSessionsLoaded_;
2382
+ }
2332
2383
  };
2333
2384
 
2334
2385
 
@@ -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
  }