videojs-contrib-quality-levels.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*! @name videojs-contrib-quality-levels @version 3.0.0 @license Apache-2.0 */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('video.js')) :
  4. typeof define === 'function' && define.amd ? define(['video.js'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.videojsContribQualityLevels = factory(global.videojs));
  6. }(this, (function (videojs) { 'use strict';
  7. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  8. var videojs__default = /*#__PURE__*/_interopDefaultLegacy(videojs);
  9. /**
  10. * A single QualityLevel.
  11. *
  12. * interface QualityLevel {
  13. * readonly attribute DOMString id;
  14. * attribute DOMString label;
  15. * readonly attribute long width;
  16. * readonly attribute long height;
  17. * readonly attribute long bitrate;
  18. * attribute boolean enabled;
  19. * };
  20. *
  21. * @class QualityLevel
  22. */
  23. class QualityLevel {
  24. /**
  25. * Creates a QualityLevel
  26. *
  27. * @param {Representation|Object} representation The representation of the quality level
  28. * @param {string} representation.id Unique id of the QualityLevel
  29. * @param {number=} representation.width Resolution width of the QualityLevel
  30. * @param {number=} representation.height Resolution height of the QualityLevel
  31. * @param {number} representation.bandwidth Bitrate of the QualityLevel
  32. * @param {number=} representation.frameRate Frame-rate of the QualityLevel
  33. * @param {Function} representation.enabled Callback to enable/disable QualityLevel
  34. */
  35. constructor(representation) {
  36. let level = this; // eslint-disable-line
  37. level.id = representation.id;
  38. level.label = level.id;
  39. level.width = representation.width;
  40. level.height = representation.height;
  41. level.bitrate = representation.bandwidth;
  42. level.frameRate = representation.frameRate;
  43. level.enabled_ = representation.enabled;
  44. Object.defineProperty(level, 'enabled', {
  45. /**
  46. * Get whether the QualityLevel is enabled.
  47. *
  48. * @return {boolean} True if the QualityLevel is enabled.
  49. */
  50. get() {
  51. return level.enabled_();
  52. },
  53. /**
  54. * Enable or disable the QualityLevel.
  55. *
  56. * @param {boolean} enable true to enable QualityLevel, false to disable.
  57. */
  58. set(enable) {
  59. level.enabled_(enable);
  60. }
  61. });
  62. return level;
  63. }
  64. }
  65. /**
  66. * A list of QualityLevels.
  67. *
  68. * interface QualityLevelList : EventTarget {
  69. * getter QualityLevel (unsigned long index);
  70. * readonly attribute unsigned long length;
  71. * readonly attribute long selectedIndex;
  72. *
  73. * void addQualityLevel(QualityLevel qualityLevel)
  74. * void removeQualityLevel(QualityLevel remove)
  75. * QualityLevel? getQualityLevelById(DOMString id);
  76. *
  77. * attribute EventHandler onchange;
  78. * attribute EventHandler onaddqualitylevel;
  79. * attribute EventHandler onremovequalitylevel;
  80. * };
  81. *
  82. * @extends videojs.EventTarget
  83. * @class QualityLevelList
  84. */
  85. class QualityLevelList extends videojs__default['default'].EventTarget {
  86. constructor() {
  87. super();
  88. let list = this; // eslint-disable-line
  89. list.levels_ = [];
  90. list.selectedIndex_ = -1;
  91. /**
  92. * Get the index of the currently selected QualityLevel.
  93. *
  94. * @returns {number} The index of the selected QualityLevel. -1 if none selected.
  95. * @readonly
  96. */
  97. Object.defineProperty(list, 'selectedIndex', {
  98. get() {
  99. return list.selectedIndex_;
  100. }
  101. });
  102. /**
  103. * Get the length of the list of QualityLevels.
  104. *
  105. * @returns {number} The length of the list.
  106. * @readonly
  107. */
  108. Object.defineProperty(list, 'length', {
  109. get() {
  110. return list.levels_.length;
  111. }
  112. });
  113. return list;
  114. }
  115. /**
  116. * Adds a quality level to the list.
  117. *
  118. * @param {Representation|Object} representation The representation of the quality level
  119. * @param {string} representation.id Unique id of the QualityLevel
  120. * @param {number=} representation.width Resolution width of the QualityLevel
  121. * @param {number=} representation.height Resolution height of the QualityLevel
  122. * @param {number} representation.bandwidth Bitrate of the QualityLevel
  123. * @param {number=} representation.frameRate Frame-rate of the QualityLevel
  124. * @param {Function} representation.enabled Callback to enable/disable QualityLevel
  125. * @return {QualityLevel} the QualityLevel added to the list
  126. * @method addQualityLevel
  127. */
  128. addQualityLevel(representation) {
  129. let qualityLevel = this.getQualityLevelById(representation.id); // Do not add duplicate quality levels
  130. if (qualityLevel) {
  131. return qualityLevel;
  132. }
  133. const index = this.levels_.length;
  134. qualityLevel = new QualityLevel(representation);
  135. if (!('' + index in this)) {
  136. Object.defineProperty(this, index, {
  137. get() {
  138. return this.levels_[index];
  139. }
  140. });
  141. }
  142. this.levels_.push(qualityLevel);
  143. this.trigger({
  144. qualityLevel,
  145. type: 'addqualitylevel'
  146. });
  147. return qualityLevel;
  148. }
  149. /**
  150. * Removes a quality level from the list.
  151. *
  152. * @param {QualityLevel} remove QualityLevel to remove to the list.
  153. * @return {QualityLevel|null} the QualityLevel removed or null if nothing removed
  154. * @method removeQualityLevel
  155. */
  156. removeQualityLevel(qualityLevel) {
  157. let removed = null;
  158. for (let i = 0, l = this.length; i < l; i++) {
  159. if (this[i] === qualityLevel) {
  160. removed = this.levels_.splice(i, 1)[0];
  161. if (this.selectedIndex_ === i) {
  162. this.selectedIndex_ = -1;
  163. } else if (this.selectedIndex_ > i) {
  164. this.selectedIndex_--;
  165. }
  166. break;
  167. }
  168. }
  169. if (removed) {
  170. this.trigger({
  171. qualityLevel,
  172. type: 'removequalitylevel'
  173. });
  174. }
  175. return removed;
  176. }
  177. /**
  178. * Searches for a QualityLevel with the given id.
  179. *
  180. * @param {string} id The id of the QualityLevel to find.
  181. * @return {QualityLevel|null} The QualityLevel with id, or null if not found.
  182. * @method getQualityLevelById
  183. */
  184. getQualityLevelById(id) {
  185. for (let i = 0, l = this.length; i < l; i++) {
  186. const level = this[i];
  187. if (level.id === id) {
  188. return level;
  189. }
  190. }
  191. return null;
  192. }
  193. /**
  194. * Resets the list of QualityLevels to empty
  195. *
  196. * @method dispose
  197. */
  198. dispose() {
  199. this.selectedIndex_ = -1;
  200. this.levels_.length = 0;
  201. }
  202. }
  203. /**
  204. * change - The selected QualityLevel has changed.
  205. * addqualitylevel - A QualityLevel has been added to the QualityLevelList.
  206. * removequalitylevel - A QualityLevel has been removed from the QualityLevelList.
  207. */
  208. QualityLevelList.prototype.allowedEvents_ = {
  209. change: 'change',
  210. addqualitylevel: 'addqualitylevel',
  211. removequalitylevel: 'removequalitylevel'
  212. }; // emulate attribute EventHandler support to allow for feature detection
  213. for (const event in QualityLevelList.prototype.allowedEvents_) {
  214. QualityLevelList.prototype['on' + event] = null;
  215. }
  216. var version = "3.0.0";
  217. const registerPlugin = videojs__default['default'].registerPlugin || videojs__default['default'].plugin;
  218. /**
  219. * Initialization function for the qualityLevels plugin. Sets up the QualityLevelList and
  220. * event handlers.
  221. *
  222. * @param {Player} player Player object.
  223. * @param {Object} options Plugin options object.
  224. * @function initPlugin
  225. */
  226. const initPlugin = function (player, options) {
  227. const originalPluginFn = player.qualityLevels;
  228. const qualityLevelList = new QualityLevelList();
  229. const disposeHandler = function () {
  230. qualityLevelList.dispose();
  231. player.qualityLevels = originalPluginFn;
  232. player.off('dispose', disposeHandler);
  233. };
  234. player.on('dispose', disposeHandler);
  235. player.qualityLevels = () => qualityLevelList;
  236. player.qualityLevels.VERSION = version;
  237. return qualityLevelList;
  238. };
  239. /**
  240. * A video.js plugin.
  241. *
  242. * In the plugin function, the value of `this` is a video.js `Player`
  243. * instance. You cannot rely on the player being in a "ready" state here,
  244. * depending on how the plugin is invoked. This may or may not be important
  245. * to you; if not, remove the wait for "ready"!
  246. *
  247. * @param {Object} options Plugin options object
  248. * @function qualityLevels
  249. */
  250. const qualityLevels = function (options) {
  251. return initPlugin(this, videojs__default['default'].mergeOptions({}, options));
  252. }; // Register the plugin with video.js.
  253. registerPlugin('qualityLevels', qualityLevels); // Include the version number.
  254. qualityLevels.VERSION = version;
  255. return qualityLevels;
  256. })));