flash-media-source.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @file flash-media-source.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 _globalDocument = require('global/document');
  14. var _globalDocument2 = _interopRequireDefault(_globalDocument);
  15. var _videoJs = require('video.js');
  16. var _videoJs2 = _interopRequireDefault(_videoJs);
  17. var _flashSourceBuffer = require('./flash-source-buffer');
  18. var _flashSourceBuffer2 = _interopRequireDefault(_flashSourceBuffer);
  19. var _flashConstants = require('./flash-constants');
  20. var _flashConstants2 = _interopRequireDefault(_flashConstants);
  21. var _codecUtils = require('./codec-utils');
  22. /**
  23. * A flash implmentation of HTML MediaSources and a polyfill
  24. * for browsers that don't support native or HTML MediaSources..
  25. *
  26. * @link https://developer.mozilla.org/en-US/docs/Web/API/MediaSource
  27. * @class FlashMediaSource
  28. * @extends videojs.EventTarget
  29. */
  30. var FlashMediaSource = (function (_videojs$EventTarget) {
  31. _inherits(FlashMediaSource, _videojs$EventTarget);
  32. function FlashMediaSource() {
  33. var _this = this;
  34. _classCallCheck(this, FlashMediaSource);
  35. _get(Object.getPrototypeOf(FlashMediaSource.prototype), 'constructor', this).call(this);
  36. this.sourceBuffers = [];
  37. this.readyState = 'closed';
  38. this.on(['sourceopen', 'webkitsourceopen'], function (event) {
  39. // find the swf where we will push media data
  40. _this.swfObj = _globalDocument2['default'].getElementById(event.swfId);
  41. _this.player_ = (0, _videoJs2['default'])(_this.swfObj.parentNode);
  42. _this.tech_ = _this.swfObj.tech;
  43. _this.readyState = 'open';
  44. _this.tech_.on('seeking', function () {
  45. var i = _this.sourceBuffers.length;
  46. while (i--) {
  47. _this.sourceBuffers[i].abort();
  48. }
  49. });
  50. // trigger load events
  51. if (_this.swfObj) {
  52. _this.swfObj.vjs_load();
  53. }
  54. });
  55. }
  56. /**
  57. * Set or return the presentation duration.
  58. *
  59. * @param {Double} value the duration of the media in seconds
  60. * @param {Double} the current presentation duration
  61. * @link http://www.w3.org/TR/media-source/#widl-MediaSource-duration
  62. */
  63. /**
  64. * We have this function so that the html and flash interfaces
  65. * are the same.
  66. *
  67. * @private
  68. */
  69. _createClass(FlashMediaSource, [{
  70. key: 'addSeekableRange_',
  71. value: function addSeekableRange_() {}
  72. // intentional no-op
  73. /**
  74. * Create a new flash source buffer and add it to our flash media source.
  75. *
  76. * @link https://developer.mozilla.org/en-US/docs/Web/API/MediaSource/addSourceBuffer
  77. * @param {String} type the content-type of the source
  78. * @return {Object} the flash source buffer
  79. */
  80. }, {
  81. key: 'addSourceBuffer',
  82. value: function addSourceBuffer(type) {
  83. var parsedType = (0, _codecUtils.parseContentType)(type);
  84. var sourceBuffer = undefined;
  85. // if this is an FLV type, we'll push data to flash
  86. if (parsedType.type === 'video/mp2t' || parsedType.type === 'audio/mp2t') {
  87. // Flash source buffers
  88. sourceBuffer = new _flashSourceBuffer2['default'](this);
  89. } else {
  90. throw new Error('NotSupportedError (Video.js)');
  91. }
  92. this.sourceBuffers.push(sourceBuffer);
  93. return sourceBuffer;
  94. }
  95. /**
  96. * Signals the end of the stream.
  97. *
  98. * @link https://w3c.github.io/media-source/#widl-MediaSource-endOfStream-void-EndOfStreamError-error
  99. * @param {String=} error Signals that a playback error
  100. * has occurred. If specified, it must be either "network" or
  101. * "decode".
  102. */
  103. }, {
  104. key: 'endOfStream',
  105. value: function endOfStream(error) {
  106. if (error === 'network') {
  107. // MEDIA_ERR_NETWORK
  108. this.tech_.error(2);
  109. } else if (error === 'decode') {
  110. // MEDIA_ERR_DECODE
  111. this.tech_.error(3);
  112. }
  113. if (this.readyState !== 'ended') {
  114. this.readyState = 'ended';
  115. this.swfObj.vjs_endOfStream();
  116. }
  117. }
  118. }]);
  119. return FlashMediaSource;
  120. })(_videoJs2['default'].EventTarget);
  121. exports['default'] = FlashMediaSource;
  122. try {
  123. Object.defineProperty(FlashMediaSource.prototype, 'duration', {
  124. /**
  125. * Return the presentation duration.
  126. *
  127. * @return {Double} the duration of the media in seconds
  128. * @link http://www.w3.org/TR/media-source/#widl-MediaSource-duration
  129. */
  130. get: function get() {
  131. if (!this.swfObj) {
  132. return NaN;
  133. }
  134. // get the current duration from the SWF
  135. return this.swfObj.vjs_getProperty('duration');
  136. },
  137. /**
  138. * Set the presentation duration.
  139. *
  140. * @param {Double} value the duration of the media in seconds
  141. * @return {Double} the duration of the media in seconds
  142. * @link http://www.w3.org/TR/media-source/#widl-MediaSource-duration
  143. */
  144. set: function set(value) {
  145. var i = undefined;
  146. var oldDuration = this.swfObj.vjs_getProperty('duration');
  147. this.swfObj.vjs_setProperty('duration', value);
  148. if (value < oldDuration) {
  149. // In MSE, this triggers the range removal algorithm which causes
  150. // an update to occur
  151. for (i = 0; i < this.sourceBuffers.length; i++) {
  152. this.sourceBuffers[i].remove(value, oldDuration);
  153. }
  154. }
  155. return value;
  156. }
  157. });
  158. } catch (e) {
  159. // IE8 throws if defineProperty is called on a non-DOM node. We
  160. // don't support IE8 but we shouldn't throw an error if loaded
  161. // there.
  162. FlashMediaSource.prototype.duration = NaN;
  163. }
  164. for (var property in _flashConstants2['default']) {
  165. FlashMediaSource[property] = _flashConstants2['default'][property];
  166. }
  167. module.exports = exports['default'];