virtual-source-buffer.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /**
  2. * @file virtual-source-buffer.js
  3. */
  4. 'use strict';
  5. Object.defineProperty(exports, '__esModule', {
  6. value: true
  7. });
  8. var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
  9. var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  12. function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  13. var _videoJs = require('video.js');
  14. var _videoJs2 = _interopRequireDefault(_videoJs);
  15. var _createTextTracksIfNecessary = require('./create-text-tracks-if-necessary');
  16. var _createTextTracksIfNecessary2 = _interopRequireDefault(_createTextTracksIfNecessary);
  17. var _removeCuesFromTrack = require('./remove-cues-from-track');
  18. var _removeCuesFromTrack2 = _interopRequireDefault(_removeCuesFromTrack);
  19. var _addTextTrackData = require('./add-text-track-data');
  20. var _webwackify = require('webwackify');
  21. var _webwackify2 = _interopRequireDefault(_webwackify);
  22. var _transmuxerWorker = require('./transmuxer-worker');
  23. var _transmuxerWorker2 = _interopRequireDefault(_transmuxerWorker);
  24. var _codecUtils = require('./codec-utils');
  25. var resolveTransmuxWorker = function resolveTransmuxWorker() {
  26. var result = undefined;
  27. try {
  28. result = require.resolve('./transmuxer-worker');
  29. } catch (e) {
  30. // no result
  31. }
  32. return result;
  33. };
  34. // We create a wrapper around the SourceBuffer so that we can manage the
  35. // state of the `updating` property manually. We have to do this because
  36. // Firefox changes `updating` to false long before triggering `updateend`
  37. // events and that was causing strange problems in videojs-contrib-hls
  38. var makeWrappedSourceBuffer = function makeWrappedSourceBuffer(mediaSource, mimeType) {
  39. var sourceBuffer = mediaSource.addSourceBuffer(mimeType);
  40. var wrapper = Object.create(null);
  41. wrapper.updating = false;
  42. wrapper.realBuffer_ = sourceBuffer;
  43. var _loop = function (key) {
  44. if (typeof sourceBuffer[key] === 'function') {
  45. wrapper[key] = function () {
  46. return sourceBuffer[key].apply(sourceBuffer, arguments);
  47. };
  48. } else if (typeof wrapper[key] === 'undefined') {
  49. Object.defineProperty(wrapper, key, {
  50. get: function get() {
  51. return sourceBuffer[key];
  52. },
  53. set: function set(v) {
  54. return sourceBuffer[key] = v;
  55. }
  56. });
  57. }
  58. };
  59. for (var key in sourceBuffer) {
  60. _loop(key);
  61. }
  62. return wrapper;
  63. };
  64. /**
  65. * Returns a list of gops in the buffer that have a pts value of 3 seconds or more in
  66. * front of current time.
  67. *
  68. * @param {Array} buffer
  69. * The current buffer of gop information
  70. * @param {Player} player
  71. * The player instance
  72. * @param {Double} mapping
  73. * Offset to map display time to stream presentation time
  74. * @return {Array}
  75. * List of gops considered safe to append over
  76. */
  77. var gopsSafeToAlignWith = function gopsSafeToAlignWith(buffer, player, mapping) {
  78. if (!player || !buffer.length) {
  79. return [];
  80. }
  81. // pts value for current time + 3 seconds to give a bit more wiggle room
  82. var currentTimePts = Math.ceil((player.currentTime() - mapping + 3) * 90000);
  83. var i = undefined;
  84. for (i = 0; i < buffer.length; i++) {
  85. if (buffer[i].pts > currentTimePts) {
  86. break;
  87. }
  88. }
  89. return buffer.slice(i);
  90. };
  91. exports.gopsSafeToAlignWith = gopsSafeToAlignWith;
  92. /**
  93. * Appends gop information (timing and byteLength) received by the transmuxer for the
  94. * gops appended in the last call to appendBuffer
  95. *
  96. * @param {Array} buffer
  97. * The current buffer of gop information
  98. * @param {Array} gops
  99. * List of new gop information
  100. * @param {boolean} replace
  101. * If true, replace the buffer with the new gop information. If false, append the
  102. * new gop information to the buffer in the right location of time.
  103. * @return {Array}
  104. * Updated list of gop information
  105. */
  106. var updateGopBuffer = function updateGopBuffer(buffer, gops, replace) {
  107. if (!gops.length) {
  108. return buffer;
  109. }
  110. if (replace) {
  111. // If we are in safe append mode, then completely overwrite the gop buffer
  112. // with the most recent appeneded data. This will make sure that when appending
  113. // future segments, we only try to align with gops that are both ahead of current
  114. // time and in the last segment appended.
  115. return gops.slice();
  116. }
  117. var start = gops[0].pts;
  118. var i = 0;
  119. for (i; i < buffer.length; i++) {
  120. if (buffer[i].pts >= start) {
  121. break;
  122. }
  123. }
  124. return buffer.slice(0, i).concat(gops);
  125. };
  126. exports.updateGopBuffer = updateGopBuffer;
  127. /**
  128. * Removes gop information in buffer that overlaps with provided start and end
  129. *
  130. * @param {Array} buffer
  131. * The current buffer of gop information
  132. * @param {Double} start
  133. * position to start the remove at
  134. * @param {Double} end
  135. * position to end the remove at
  136. * @param {Double} mapping
  137. * Offset to map display time to stream presentation time
  138. */
  139. var removeGopBuffer = function removeGopBuffer(buffer, start, end, mapping) {
  140. var startPts = Math.ceil((start - mapping) * 90000);
  141. var endPts = Math.ceil((end - mapping) * 90000);
  142. var updatedBuffer = buffer.slice();
  143. var i = buffer.length;
  144. while (i--) {
  145. if (buffer[i].pts <= endPts) {
  146. break;
  147. }
  148. }
  149. if (i === -1) {
  150. // no removal because end of remove range is before start of buffer
  151. return updatedBuffer;
  152. }
  153. var j = i + 1;
  154. while (j--) {
  155. if (buffer[j].pts <= startPts) {
  156. break;
  157. }
  158. }
  159. // clamp remove range start to 0 index
  160. j = Math.max(j, 0);
  161. updatedBuffer.splice(j, i - j + 1);
  162. return updatedBuffer;
  163. };
  164. exports.removeGopBuffer = removeGopBuffer;
  165. /**
  166. * VirtualSourceBuffers exist so that we can transmux non native formats
  167. * into a native format, but keep the same api as a native source buffer.
  168. * It creates a transmuxer, that works in its own thread (a web worker) and
  169. * that transmuxer muxes the data into a native format. VirtualSourceBuffer will
  170. * then send all of that data to the naive sourcebuffer so that it is
  171. * indestinguishable from a natively supported format.
  172. *
  173. * @param {HtmlMediaSource} mediaSource the parent mediaSource
  174. * @param {Array} codecs array of codecs that we will be dealing with
  175. * @class VirtualSourceBuffer
  176. * @extends video.js.EventTarget
  177. */
  178. var VirtualSourceBuffer = (function (_videojs$EventTarget) {
  179. _inherits(VirtualSourceBuffer, _videojs$EventTarget);
  180. function VirtualSourceBuffer(mediaSource, codecs) {
  181. var _this = this;
  182. _classCallCheck(this, VirtualSourceBuffer);
  183. _get(Object.getPrototypeOf(VirtualSourceBuffer.prototype), 'constructor', this).call(this, _videoJs2['default'].EventTarget);
  184. this.timestampOffset_ = 0;
  185. this.pendingBuffers_ = [];
  186. this.bufferUpdating_ = false;
  187. this.mediaSource_ = mediaSource;
  188. this.codecs_ = codecs;
  189. this.audioCodec_ = null;
  190. this.videoCodec_ = null;
  191. this.audioDisabled_ = false;
  192. this.appendAudioInitSegment_ = true;
  193. this.gopBuffer_ = [];
  194. this.timeMapping_ = 0;
  195. this.safeAppend_ = _videoJs2['default'].browser.IE_VERSION >= 11;
  196. var options = {
  197. remux: false,
  198. alignGopsAtEnd: this.safeAppend_
  199. };
  200. this.codecs_.forEach(function (codec) {
  201. if ((0, _codecUtils.isAudioCodec)(codec)) {
  202. _this.audioCodec_ = codec;
  203. } else if ((0, _codecUtils.isVideoCodec)(codec)) {
  204. _this.videoCodec_ = codec;
  205. }
  206. });
  207. // append muxed segments to their respective native buffers as
  208. // soon as they are available
  209. this.transmuxer_ = (0, _webwackify2['default'])(_transmuxerWorker2['default'], resolveTransmuxWorker());
  210. this.transmuxer_.postMessage({ action: 'init', options: options });
  211. this.transmuxer_.onmessage = function (event) {
  212. if (event.data.action === 'data') {
  213. return _this.data_(event);
  214. }
  215. if (event.data.action === 'done') {
  216. return _this.done_(event);
  217. }
  218. if (event.data.action === 'gopInfo') {
  219. return _this.appendGopInfo_(event);
  220. }
  221. };
  222. // this timestampOffset is a property with the side-effect of resetting
  223. // baseMediaDecodeTime in the transmuxer on the setter
  224. Object.defineProperty(this, 'timestampOffset', {
  225. get: function get() {
  226. return this.timestampOffset_;
  227. },
  228. set: function set(val) {
  229. if (typeof val === 'number' && val >= 0) {
  230. this.timestampOffset_ = val;
  231. this.appendAudioInitSegment_ = true;
  232. // reset gop buffer on timestampoffset as this signals a change in timeline
  233. this.gopBuffer_.length = 0;
  234. this.timeMapping_ = 0;
  235. // We have to tell the transmuxer to set the baseMediaDecodeTime to
  236. // the desired timestampOffset for the next segment
  237. this.transmuxer_.postMessage({
  238. action: 'setTimestampOffset',
  239. timestampOffset: val
  240. });
  241. }
  242. }
  243. });
  244. // setting the append window affects both source buffers
  245. Object.defineProperty(this, 'appendWindowStart', {
  246. get: function get() {
  247. return (this.videoBuffer_ || this.audioBuffer_).appendWindowStart;
  248. },
  249. set: function set(start) {
  250. if (this.videoBuffer_) {
  251. this.videoBuffer_.appendWindowStart = start;
  252. }
  253. if (this.audioBuffer_) {
  254. this.audioBuffer_.appendWindowStart = start;
  255. }
  256. }
  257. });
  258. // this buffer is "updating" if either of its native buffers are
  259. Object.defineProperty(this, 'updating', {
  260. get: function get() {
  261. return !!(this.bufferUpdating_ || !this.audioDisabled_ && this.audioBuffer_ && this.audioBuffer_.updating || this.videoBuffer_ && this.videoBuffer_.updating);
  262. }
  263. });
  264. // the buffered property is the intersection of the buffered
  265. // ranges of the native source buffers
  266. Object.defineProperty(this, 'buffered', {
  267. get: function get() {
  268. var start = null;
  269. var end = null;
  270. var arity = 0;
  271. var extents = [];
  272. var ranges = [];
  273. // neither buffer has been created yet
  274. if (!this.videoBuffer_ && !this.audioBuffer_) {
  275. return _videoJs2['default'].createTimeRange();
  276. }
  277. // only one buffer is configured
  278. if (!this.videoBuffer_) {
  279. return this.audioBuffer_.buffered;
  280. }
  281. if (!this.audioBuffer_) {
  282. return this.videoBuffer_.buffered;
  283. }
  284. // both buffers are configured
  285. if (this.audioDisabled_) {
  286. return this.videoBuffer_.buffered;
  287. }
  288. // both buffers are empty
  289. if (this.videoBuffer_.buffered.length === 0 && this.audioBuffer_.buffered.length === 0) {
  290. return _videoJs2['default'].createTimeRange();
  291. }
  292. // Handle the case where we have both buffers and create an
  293. // intersection of the two
  294. var videoBuffered = this.videoBuffer_.buffered;
  295. var audioBuffered = this.audioBuffer_.buffered;
  296. var count = videoBuffered.length;
  297. // A) Gather up all start and end times
  298. while (count--) {
  299. extents.push({ time: videoBuffered.start(count), type: 'start' });
  300. extents.push({ time: videoBuffered.end(count), type: 'end' });
  301. }
  302. count = audioBuffered.length;
  303. while (count--) {
  304. extents.push({ time: audioBuffered.start(count), type: 'start' });
  305. extents.push({ time: audioBuffered.end(count), type: 'end' });
  306. }
  307. // B) Sort them by time
  308. extents.sort(function (a, b) {
  309. return a.time - b.time;
  310. });
  311. // C) Go along one by one incrementing arity for start and decrementing
  312. // arity for ends
  313. for (count = 0; count < extents.length; count++) {
  314. if (extents[count].type === 'start') {
  315. arity++;
  316. // D) If arity is ever incremented to 2 we are entering an
  317. // overlapping range
  318. if (arity === 2) {
  319. start = extents[count].time;
  320. }
  321. } else if (extents[count].type === 'end') {
  322. arity--;
  323. // E) If arity is ever decremented to 1 we leaving an
  324. // overlapping range
  325. if (arity === 1) {
  326. end = extents[count].time;
  327. }
  328. }
  329. // F) Record overlapping ranges
  330. if (start !== null && end !== null) {
  331. ranges.push([start, end]);
  332. start = null;
  333. end = null;
  334. }
  335. }
  336. return _videoJs2['default'].createTimeRanges(ranges);
  337. }
  338. });
  339. }
  340. /**
  341. * When we get a data event from the transmuxer
  342. * we call this function and handle the data that
  343. * was sent to us
  344. *
  345. * @private
  346. * @param {Event} event the data event from the transmuxer
  347. */
  348. _createClass(VirtualSourceBuffer, [{
  349. key: 'data_',
  350. value: function data_(event) {
  351. var segment = event.data.segment;
  352. // Cast ArrayBuffer to TypedArray
  353. segment.data = new Uint8Array(segment.data, event.data.byteOffset, event.data.byteLength);
  354. segment.initSegment = new Uint8Array(segment.initSegment.data, segment.initSegment.byteOffset, segment.initSegment.byteLength);
  355. (0, _createTextTracksIfNecessary2['default'])(this, this.mediaSource_, segment);
  356. // Add the segments to the pendingBuffers array
  357. this.pendingBuffers_.push(segment);
  358. return;
  359. }
  360. /**
  361. * When we get a done event from the transmuxer
  362. * we call this function and we process all
  363. * of the pending data that we have been saving in the
  364. * data_ function
  365. *
  366. * @private
  367. * @param {Event} event the done event from the transmuxer
  368. */
  369. }, {
  370. key: 'done_',
  371. value: function done_(event) {
  372. // Don't process and append data if the mediaSource is closed
  373. if (this.mediaSource_.readyState === 'closed') {
  374. this.pendingBuffers_.length = 0;
  375. return;
  376. }
  377. // All buffers should have been flushed from the muxer
  378. // start processing anything we have received
  379. this.processPendingSegments_();
  380. return;
  381. }
  382. /**
  383. * Create our internal native audio/video source buffers and add
  384. * event handlers to them with the following conditions:
  385. * 1. they do not already exist on the mediaSource
  386. * 2. this VSB has a codec for them
  387. *
  388. * @private
  389. */
  390. }, {
  391. key: 'createRealSourceBuffers_',
  392. value: function createRealSourceBuffers_() {
  393. var _this2 = this;
  394. var types = ['audio', 'video'];
  395. types.forEach(function (type) {
  396. // Don't create a SourceBuffer of this type if we don't have a
  397. // codec for it
  398. if (!_this2[type + 'Codec_']) {
  399. return;
  400. }
  401. // Do nothing if a SourceBuffer of this type already exists
  402. if (_this2[type + 'Buffer_']) {
  403. return;
  404. }
  405. var buffer = null;
  406. // If the mediasource already has a SourceBuffer for the codec
  407. // use that
  408. if (_this2.mediaSource_[type + 'Buffer_']) {
  409. buffer = _this2.mediaSource_[type + 'Buffer_'];
  410. // In multiple audio track cases, the audio source buffer is disabled
  411. // on the main VirtualSourceBuffer by the HTMLMediaSource much earlier
  412. // than createRealSourceBuffers_ is called to create the second
  413. // VirtualSourceBuffer because that happens as a side-effect of
  414. // videojs-contrib-hls starting the audioSegmentLoader. As a result,
  415. // the audioBuffer is essentially "ownerless" and no one will toggle
  416. // the `updating` state back to false once the `updateend` event is received
  417. //
  418. // Setting `updating` to false manually will work around this
  419. // situation and allow work to continue
  420. buffer.updating = false;
  421. } else {
  422. var codecProperty = type + 'Codec_';
  423. var mimeType = type + '/mp4;codecs="' + _this2[codecProperty] + '"';
  424. buffer = makeWrappedSourceBuffer(_this2.mediaSource_.nativeMediaSource_, mimeType);
  425. _this2.mediaSource_[type + 'Buffer_'] = buffer;
  426. }
  427. _this2[type + 'Buffer_'] = buffer;
  428. // Wire up the events to the SourceBuffer
  429. ['update', 'updatestart', 'updateend'].forEach(function (event) {
  430. buffer.addEventListener(event, function () {
  431. // if audio is disabled
  432. if (type === 'audio' && _this2.audioDisabled_) {
  433. return;
  434. }
  435. if (event === 'updateend') {
  436. _this2[type + 'Buffer_'].updating = false;
  437. }
  438. var shouldTrigger = types.every(function (t) {
  439. // skip checking audio's updating status if audio
  440. // is not enabled
  441. if (t === 'audio' && _this2.audioDisabled_) {
  442. return true;
  443. }
  444. // if the other type if updating we don't trigger
  445. if (type !== t && _this2[t + 'Buffer_'] && _this2[t + 'Buffer_'].updating) {
  446. return false;
  447. }
  448. return true;
  449. });
  450. if (shouldTrigger) {
  451. return _this2.trigger(event);
  452. }
  453. });
  454. });
  455. });
  456. }
  457. /**
  458. * Emulate the native mediasource function, but our function will
  459. * send all of the proposed segments to the transmuxer so that we
  460. * can transmux them before we append them to our internal
  461. * native source buffers in the correct format.
  462. *
  463. * @link https://developer.mozilla.org/en-US/docs/Web/API/SourceBuffer/appendBuffer
  464. * @param {Uint8Array} segment the segment to append to the buffer
  465. */
  466. }, {
  467. key: 'appendBuffer',
  468. value: function appendBuffer(segment) {
  469. // Start the internal "updating" state
  470. this.bufferUpdating_ = true;
  471. if (this.audioBuffer_ && this.audioBuffer_.buffered.length) {
  472. var audioBuffered = this.audioBuffer_.buffered;
  473. this.transmuxer_.postMessage({
  474. action: 'setAudioAppendStart',
  475. appendStart: audioBuffered.end(audioBuffered.length - 1)
  476. });
  477. }
  478. if (this.videoBuffer_) {
  479. this.transmuxer_.postMessage({
  480. action: 'alignGopsWith',
  481. gopsToAlignWith: gopsSafeToAlignWith(this.gopBuffer_, this.mediaSource_.player_, this.timeMapping_)
  482. });
  483. }
  484. this.transmuxer_.postMessage({
  485. action: 'push',
  486. // Send the typed-array of data as an ArrayBuffer so that
  487. // it can be sent as a "Transferable" and avoid the costly
  488. // memory copy
  489. data: segment.buffer,
  490. // To recreate the original typed-array, we need information
  491. // about what portion of the ArrayBuffer it was a view into
  492. byteOffset: segment.byteOffset,
  493. byteLength: segment.byteLength
  494. }, [segment.buffer]);
  495. this.transmuxer_.postMessage({ action: 'flush' });
  496. }
  497. /**
  498. * Appends gop information (timing and byteLength) received by the transmuxer for the
  499. * gops appended in the last call to appendBuffer
  500. *
  501. * @param {Event} event
  502. * The gopInfo event from the transmuxer
  503. * @param {Array} event.data.gopInfo
  504. * List of gop info to append
  505. */
  506. }, {
  507. key: 'appendGopInfo_',
  508. value: function appendGopInfo_(event) {
  509. this.gopBuffer_ = updateGopBuffer(this.gopBuffer_, event.data.gopInfo, this.safeAppend_);
  510. }
  511. /**
  512. * Emulate the native mediasource function and remove parts
  513. * of the buffer from any of our internal buffers that exist
  514. *
  515. * @link https://developer.mozilla.org/en-US/docs/Web/API/SourceBuffer/remove
  516. * @param {Double} start position to start the remove at
  517. * @param {Double} end position to end the remove at
  518. */
  519. }, {
  520. key: 'remove',
  521. value: function remove(start, end) {
  522. if (this.videoBuffer_) {
  523. this.videoBuffer_.updating = true;
  524. this.videoBuffer_.remove(start, end);
  525. this.gopBuffer_ = removeGopBuffer(this.gopBuffer_, start, end, this.timeMapping_);
  526. }
  527. if (!this.audioDisabled_ && this.audioBuffer_) {
  528. this.audioBuffer_.updating = true;
  529. this.audioBuffer_.remove(start, end);
  530. }
  531. // Remove Metadata Cues (id3)
  532. (0, _removeCuesFromTrack2['default'])(start, end, this.metadataTrack_);
  533. // Remove Any Captions
  534. if (this.inbandTextTracks_) {
  535. for (var track in this.inbandTextTracks_) {
  536. (0, _removeCuesFromTrack2['default'])(start, end, this.inbandTextTracks_[track]);
  537. }
  538. }
  539. }
  540. /**
  541. * Process any segments that the muxer has output
  542. * Concatenate segments together based on type and append them into
  543. * their respective sourceBuffers
  544. *
  545. * @private
  546. */
  547. }, {
  548. key: 'processPendingSegments_',
  549. value: function processPendingSegments_() {
  550. var sortedSegments = {
  551. video: {
  552. segments: [],
  553. bytes: 0
  554. },
  555. audio: {
  556. segments: [],
  557. bytes: 0
  558. },
  559. captions: [],
  560. metadata: []
  561. };
  562. // Sort segments into separate video/audio arrays and
  563. // keep track of their total byte lengths
  564. sortedSegments = this.pendingBuffers_.reduce(function (segmentObj, segment) {
  565. var type = segment.type;
  566. var data = segment.data;
  567. var initSegment = segment.initSegment;
  568. segmentObj[type].segments.push(data);
  569. segmentObj[type].bytes += data.byteLength;
  570. segmentObj[type].initSegment = initSegment;
  571. // Gather any captions into a single array
  572. if (segment.captions) {
  573. segmentObj.captions = segmentObj.captions.concat(segment.captions);
  574. }
  575. if (segment.info) {
  576. segmentObj[type].info = segment.info;
  577. }
  578. // Gather any metadata into a single array
  579. if (segment.metadata) {
  580. segmentObj.metadata = segmentObj.metadata.concat(segment.metadata);
  581. }
  582. return segmentObj;
  583. }, sortedSegments);
  584. // Create the real source buffers if they don't exist by now since we
  585. // finally are sure what tracks are contained in the source
  586. if (!this.videoBuffer_ && !this.audioBuffer_) {
  587. // Remove any codecs that may have been specified by default but
  588. // are no longer applicable now
  589. if (sortedSegments.video.bytes === 0) {
  590. this.videoCodec_ = null;
  591. }
  592. if (sortedSegments.audio.bytes === 0) {
  593. this.audioCodec_ = null;
  594. }
  595. this.createRealSourceBuffers_();
  596. }
  597. if (sortedSegments.audio.info) {
  598. this.mediaSource_.trigger({ type: 'audioinfo', info: sortedSegments.audio.info });
  599. }
  600. if (sortedSegments.video.info) {
  601. this.mediaSource_.trigger({ type: 'videoinfo', info: sortedSegments.video.info });
  602. }
  603. if (this.appendAudioInitSegment_) {
  604. if (!this.audioDisabled_ && this.audioBuffer_) {
  605. sortedSegments.audio.segments.unshift(sortedSegments.audio.initSegment);
  606. sortedSegments.audio.bytes += sortedSegments.audio.initSegment.byteLength;
  607. }
  608. this.appendAudioInitSegment_ = false;
  609. }
  610. var triggerUpdateend = false;
  611. // Merge multiple video and audio segments into one and append
  612. if (this.videoBuffer_ && sortedSegments.video.bytes) {
  613. sortedSegments.video.segments.unshift(sortedSegments.video.initSegment);
  614. sortedSegments.video.bytes += sortedSegments.video.initSegment.byteLength;
  615. this.concatAndAppendSegments_(sortedSegments.video, this.videoBuffer_);
  616. // TODO: are video tracks the only ones with text tracks?
  617. (0, _addTextTrackData.addTextTrackData)(this, sortedSegments.captions, sortedSegments.metadata);
  618. } else if (this.videoBuffer_ && (this.audioDisabled_ || !this.audioBuffer_)) {
  619. // The transmuxer did not return any bytes of video, meaning it was all trimmed
  620. // for gop alignment. Since we have a video buffer and audio is disabled, updateend
  621. // will never be triggered by this source buffer, which will cause contrib-hls
  622. // to be stuck forever waiting for updateend. If audio is not disabled, updateend
  623. // will be triggered by the audio buffer, which will be sent upwards since the video
  624. // buffer will not be in an updating state.
  625. triggerUpdateend = true;
  626. }
  627. if (!this.audioDisabled_ && this.audioBuffer_) {
  628. this.concatAndAppendSegments_(sortedSegments.audio, this.audioBuffer_);
  629. }
  630. this.pendingBuffers_.length = 0;
  631. if (triggerUpdateend) {
  632. this.trigger('updateend');
  633. }
  634. // We are no longer in the internal "updating" state
  635. this.bufferUpdating_ = false;
  636. }
  637. /**
  638. * Combine all segments into a single Uint8Array and then append them
  639. * to the destination buffer
  640. *
  641. * @param {Object} segmentObj
  642. * @param {SourceBuffer} destinationBuffer native source buffer to append data to
  643. * @private
  644. */
  645. }, {
  646. key: 'concatAndAppendSegments_',
  647. value: function concatAndAppendSegments_(segmentObj, destinationBuffer) {
  648. var offset = 0;
  649. var tempBuffer = undefined;
  650. if (segmentObj.bytes) {
  651. tempBuffer = new Uint8Array(segmentObj.bytes);
  652. // Combine the individual segments into one large typed-array
  653. segmentObj.segments.forEach(function (segment) {
  654. tempBuffer.set(segment, offset);
  655. offset += segment.byteLength;
  656. });
  657. try {
  658. destinationBuffer.updating = true;
  659. destinationBuffer.appendBuffer(tempBuffer);
  660. } catch (error) {
  661. if (this.mediaSource_.player_) {
  662. this.mediaSource_.player_.error({
  663. code: -3,
  664. type: 'APPEND_BUFFER_ERR',
  665. message: error.message,
  666. originalError: error
  667. });
  668. }
  669. }
  670. }
  671. }
  672. /**
  673. * Emulate the native mediasource function. abort any soureBuffer
  674. * actions and throw out any un-appended data.
  675. *
  676. * @link https://developer.mozilla.org/en-US/docs/Web/API/SourceBuffer/abort
  677. */
  678. }, {
  679. key: 'abort',
  680. value: function abort() {
  681. if (this.videoBuffer_) {
  682. this.videoBuffer_.abort();
  683. }
  684. if (!this.audioDisabled_ && this.audioBuffer_) {
  685. this.audioBuffer_.abort();
  686. }
  687. if (this.transmuxer_) {
  688. this.transmuxer_.postMessage({ action: 'reset' });
  689. }
  690. this.pendingBuffers_.length = 0;
  691. this.bufferUpdating_ = false;
  692. }
  693. }]);
  694. return VirtualSourceBuffer;
  695. })(_videoJs2['default'].EventTarget);
  696. exports['default'] = VirtualSourceBuffer;