videojs-contrib-media-sources.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * @file videojs-contrib-media-sources.js
  3. */
  4. 'use strict';
  5. Object.defineProperty(exports, '__esModule', {
  6. value: true
  7. });
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  9. var _globalWindow = require('global/window');
  10. var _globalWindow2 = _interopRequireDefault(_globalWindow);
  11. var _flashMediaSource = require('./flash-media-source');
  12. var _flashMediaSource2 = _interopRequireDefault(_flashMediaSource);
  13. var _htmlMediaSource = require('./html-media-source');
  14. var _htmlMediaSource2 = _interopRequireDefault(_htmlMediaSource);
  15. var _videoJs = require('video.js');
  16. var _videoJs2 = _interopRequireDefault(_videoJs);
  17. var urlCount = 0;
  18. // ------------
  19. // Media Source
  20. // ------------
  21. var defaults = {
  22. // how to determine the MediaSource implementation to use. There
  23. // are three available modes:
  24. // - auto: use native MediaSources where available and Flash
  25. // everywhere else
  26. // - html5: always use native MediaSources
  27. // - flash: always use the Flash MediaSource polyfill
  28. mode: 'auto'
  29. };
  30. // store references to the media sources so they can be connected
  31. // to a video element (a swf object)
  32. // TODO: can we store this somewhere local to this module?
  33. _videoJs2['default'].mediaSources = {};
  34. /**
  35. * Provide a method for a swf object to notify JS that a
  36. * media source is now open.
  37. *
  38. * @param {String} msObjectURL string referencing the MSE Object URL
  39. * @param {String} swfId the swf id
  40. */
  41. var open = function open(msObjectURL, swfId) {
  42. var mediaSource = _videoJs2['default'].mediaSources[msObjectURL];
  43. if (mediaSource) {
  44. mediaSource.trigger({ type: 'sourceopen', swfId: swfId });
  45. } else {
  46. throw new Error('Media Source not found (Video.js)');
  47. }
  48. };
  49. /**
  50. * Check to see if the native MediaSource object exists and supports
  51. * an MP4 container with both H.264 video and AAC-LC audio.
  52. *
  53. * @return {Boolean} if native media sources are supported
  54. */
  55. var supportsNativeMediaSources = function supportsNativeMediaSources() {
  56. return !!_globalWindow2['default'].MediaSource && !!_globalWindow2['default'].MediaSource.isTypeSupported && _globalWindow2['default'].MediaSource.isTypeSupported('video/mp4;codecs="avc1.4d400d,mp4a.40.2"');
  57. };
  58. /**
  59. * An emulation of the MediaSource API so that we can support
  60. * native and non-native functionality such as flash and
  61. * video/mp2t videos. returns an instance of HtmlMediaSource or
  62. * FlashMediaSource depending on what is supported and what options
  63. * are passed in.
  64. *
  65. * @link https://developer.mozilla.org/en-US/docs/Web/API/MediaSource/MediaSource
  66. * @param {Object} options options to use during setup.
  67. */
  68. var MediaSource = function MediaSource(options) {
  69. var settings = _videoJs2['default'].mergeOptions(defaults, options);
  70. this.MediaSource = {
  71. open: open,
  72. supportsNativeMediaSources: supportsNativeMediaSources
  73. };
  74. // determine whether HTML MediaSources should be used
  75. if (settings.mode === 'html5' || settings.mode === 'auto' && supportsNativeMediaSources()) {
  76. return new _htmlMediaSource2['default']();
  77. } else if (_videoJs2['default'].getTech('Flash')) {
  78. return new _flashMediaSource2['default']();
  79. }
  80. throw new Error('Cannot use Flash or Html5 to create a MediaSource for this video');
  81. };
  82. exports.MediaSource = MediaSource;
  83. MediaSource.open = open;
  84. MediaSource.supportsNativeMediaSources = supportsNativeMediaSources;
  85. /**
  86. * A wrapper around the native URL for our MSE object
  87. * implementation, this object is exposed under videojs.URL
  88. *
  89. * @link https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
  90. */
  91. var URL = {
  92. /**
  93. * A wrapper around the native createObjectURL for our objects.
  94. * This function maps a native or emulated mediaSource to a blob
  95. * url so that it can be loaded into video.js
  96. *
  97. * @link https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
  98. * @param {MediaSource} object the object to create a blob url to
  99. */
  100. createObjectURL: function createObjectURL(object) {
  101. var objectUrlPrefix = 'blob:vjs-media-source/';
  102. var url = undefined;
  103. // use the native MediaSource to generate an object URL
  104. if (object instanceof _htmlMediaSource2['default']) {
  105. url = _globalWindow2['default'].URL.createObjectURL(object.nativeMediaSource_);
  106. object.url_ = url;
  107. return url;
  108. }
  109. // if the object isn't an emulated MediaSource, delegate to the
  110. // native implementation
  111. if (!(object instanceof _flashMediaSource2['default'])) {
  112. url = _globalWindow2['default'].URL.createObjectURL(object);
  113. object.url_ = url;
  114. return url;
  115. }
  116. // build a URL that can be used to map back to the emulated
  117. // MediaSource
  118. url = objectUrlPrefix + urlCount;
  119. urlCount++;
  120. // setup the mapping back to object
  121. _videoJs2['default'].mediaSources[url] = object;
  122. return url;
  123. }
  124. };
  125. exports.URL = URL;
  126. _videoJs2['default'].MediaSource = MediaSource;
  127. _videoJs2['default'].URL = URL;