videojs-flash.es.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. import videojs from 'video.js';
  2. import { version } from 'videojs-swf/package.json';
  3. import window from 'global/window';
  4. var version$1 = "2.2.1";
  5. /**
  6. * @file flash-rtmp.js
  7. * @module flash-rtmp
  8. */
  9. /**
  10. * Add RTMP properties to the {@link Flash} Tech.
  11. *
  12. * @param {Flash} Flash
  13. * The flash tech class.
  14. *
  15. * @mixin FlashRtmpDecorator
  16. *
  17. * @return {Flash}
  18. * The flash tech with RTMP properties added.
  19. */
  20. function FlashRtmpDecorator(Flash) {
  21. Flash.streamingFormats = {
  22. 'rtmp/mp4': 'MP4',
  23. 'rtmp/flv': 'FLV'
  24. };
  25. /**
  26. * Join connection and stream with an ampersand.
  27. *
  28. * @param {string} connection
  29. * The connection string.
  30. *
  31. * @param {string} stream
  32. * The stream string.
  33. *
  34. * @return {string}
  35. * The connection and stream joined with an `&` character
  36. */
  37. Flash.streamFromParts = function (connection, stream) {
  38. return connection + '&' + stream;
  39. };
  40. /**
  41. * The flash parts object that contains connection and stream info.
  42. *
  43. * @typedef {Object} Flash~PartsObject
  44. *
  45. * @property {string} connection
  46. * The connection string of a source, defaults to an empty string.
  47. *
  48. * @property {string} stream
  49. * The stream string of the source, defaults to an empty string.
  50. */
  51. /**
  52. * Convert a source url into a stream and connection parts.
  53. *
  54. * @param {string} src
  55. * the source url
  56. *
  57. * @return {Flash~PartsObject}
  58. * The parts object that contains a connection and a stream
  59. */
  60. Flash.streamToParts = function (src) {
  61. var parts = {
  62. connection: '',
  63. stream: ''
  64. };
  65. if (!src) {
  66. return parts;
  67. }
  68. // Look for the normal URL separator we expect, '&'.
  69. // If found, we split the URL into two pieces around the
  70. // first '&'.
  71. var connEnd = src.search(/&(?![\w-]+=)/);
  72. var streamBegin = void 0;
  73. if (connEnd !== -1) {
  74. streamBegin = connEnd + 1;
  75. } else {
  76. // If there's not a '&', we use the last '/' as the delimiter.
  77. connEnd = streamBegin = src.lastIndexOf('/') + 1;
  78. if (connEnd === 0) {
  79. // really, there's not a '/'?
  80. connEnd = streamBegin = src.length;
  81. }
  82. }
  83. parts.connection = src.substring(0, connEnd);
  84. parts.stream = src.substring(streamBegin, src.length);
  85. return parts;
  86. };
  87. /**
  88. * Check if the source type is a streaming type.
  89. *
  90. * @param {string} srcType
  91. * The mime type to check.
  92. *
  93. * @return {boolean}
  94. * - True if the source type is a streaming type.
  95. * - False if the source type is not a streaming type.
  96. */
  97. Flash.isStreamingType = function (srcType) {
  98. return srcType in Flash.streamingFormats;
  99. };
  100. // RTMP has four variations, any string starting
  101. // with one of these protocols should be valid
  102. /**
  103. * Regular expression used to check if the source is an rtmp source.
  104. *
  105. * @property {RegExp} Flash.RTMP_RE
  106. */
  107. Flash.RTMP_RE = /^rtmp[set]?:\/\//i;
  108. /**
  109. * Check if the source itself is a streaming type.
  110. *
  111. * @param {string} src
  112. * The url to the source.
  113. *
  114. * @return {boolean}
  115. * - True if the source url indicates that the source is streaming.
  116. * - False if the shource url indicates that the source url is not streaming.
  117. */
  118. Flash.isStreamingSrc = function (src) {
  119. return Flash.RTMP_RE.test(src);
  120. };
  121. /**
  122. * A source handler for RTMP urls
  123. * @type {Object}
  124. */
  125. Flash.rtmpSourceHandler = {};
  126. /**
  127. * Check if Flash can play the given mime type.
  128. *
  129. * @param {string} type
  130. * The mime type to check
  131. *
  132. * @return {string}
  133. * 'maybe', or '' (empty string)
  134. */
  135. Flash.rtmpSourceHandler.canPlayType = function (type) {
  136. if (Flash.isStreamingType(type)) {
  137. return 'maybe';
  138. }
  139. return '';
  140. };
  141. /**
  142. * Check if Flash can handle the source natively
  143. *
  144. * @param {Object} source
  145. * The source object
  146. *
  147. * @param {Object} [options]
  148. * The options passed to the tech
  149. *
  150. * @return {string}
  151. * 'maybe', or '' (empty string)
  152. */
  153. Flash.rtmpSourceHandler.canHandleSource = function (source, options) {
  154. var can = Flash.rtmpSourceHandler.canPlayType(source.type);
  155. if (can) {
  156. return can;
  157. }
  158. if (Flash.isStreamingSrc(source.src)) {
  159. return 'maybe';
  160. }
  161. return '';
  162. };
  163. /**
  164. * Pass the source to the flash object.
  165. *
  166. * @param {Object} source
  167. * The source object
  168. *
  169. * @param {Flash} tech
  170. * The instance of the Flash tech
  171. *
  172. * @param {Object} [options]
  173. * The options to pass to the source
  174. */
  175. Flash.rtmpSourceHandler.handleSource = function (source, tech, options) {
  176. var srcParts = Flash.streamToParts(source.src);
  177. tech.setRtmpConnection(srcParts.connection);
  178. tech.setRtmpStream(srcParts.stream);
  179. };
  180. // Register the native source handler
  181. Flash.registerSourceHandler(Flash.rtmpSourceHandler);
  182. return Flash;
  183. }
  184. var classCallCheck = function (instance, Constructor) {
  185. if (!(instance instanceof Constructor)) {
  186. throw new TypeError("Cannot call a class as a function");
  187. }
  188. };
  189. var inherits = function (subClass, superClass) {
  190. if (typeof superClass !== "function" && superClass !== null) {
  191. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  192. }
  193. subClass.prototype = Object.create(superClass && superClass.prototype, {
  194. constructor: {
  195. value: subClass,
  196. enumerable: false,
  197. writable: true,
  198. configurable: true
  199. }
  200. });
  201. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  202. };
  203. var possibleConstructorReturn = function (self, call) {
  204. if (!self) {
  205. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  206. }
  207. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  208. };
  209. /**
  210. * @file flash.js
  211. * VideoJS-SWF - Custom Flash Player with HTML5-ish API
  212. * https://github.com/zencoder/video-js-swf
  213. * Not using setupTriggers. Using global onEvent func to distribute events
  214. */
  215. var Tech = videojs.getComponent('Tech');
  216. var Dom = videojs.dom;
  217. var Url = videojs.url;
  218. var createTimeRange = videojs.createTimeRange;
  219. var mergeOptions = videojs.mergeOptions;
  220. var navigator = window && window.navigator || {};
  221. /**
  222. * Flash Media Controller - Wrapper for Flash Media API
  223. *
  224. * @mixes FlashRtmpDecorator
  225. * @mixes Tech~SouceHandlerAdditions
  226. * @extends Tech
  227. */
  228. var Flash = function (_Tech) {
  229. inherits(Flash, _Tech);
  230. /**
  231. * Create an instance of this Tech.
  232. *
  233. * @param {Object} [options]
  234. * The key/value store of player options.
  235. *
  236. * @param {Component~ReadyCallback} ready
  237. * Callback function to call when the `Flash` Tech is ready.
  238. */
  239. function Flash(options, ready) {
  240. classCallCheck(this, Flash);
  241. // Set the source when ready
  242. var _this = possibleConstructorReturn(this, _Tech.call(this, options, ready));
  243. if (options.source) {
  244. _this.ready(function () {
  245. this.setSource(options.source);
  246. }, true);
  247. }
  248. // Having issues with Flash reloading on certain page actions
  249. // (hide/resize/fullscreen) in certain browsers
  250. // This allows resetting the playhead when we catch the reload
  251. if (options.startTime) {
  252. _this.ready(function () {
  253. this.load();
  254. this.play();
  255. this.currentTime(options.startTime);
  256. }, true);
  257. }
  258. // Add global window functions that the swf expects
  259. // A 4.x workflow we weren't able to solve for in 5.0
  260. // because of the need to hard code these functions
  261. // into the swf for security reasons
  262. window.videojs = window.videojs || {};
  263. window.videojs.Flash = window.videojs.Flash || {};
  264. window.videojs.Flash.onReady = Flash.onReady;
  265. window.videojs.Flash.onEvent = Flash.onEvent;
  266. window.videojs.Flash.onError = Flash.onError;
  267. _this.on('seeked', function () {
  268. this.lastSeekTarget_ = undefined;
  269. });
  270. return _this;
  271. }
  272. /**
  273. * Create the `Flash` Tech's DOM element.
  274. *
  275. * @return {Element}
  276. * The element that gets created.
  277. */
  278. Flash.prototype.createEl = function createEl() {
  279. var options = this.options_;
  280. // If video.js is hosted locally you should also set the location
  281. // for the hosted swf, which should be relative to the page (not video.js)
  282. // Otherwise this adds a CDN url.
  283. // The CDN also auto-adds a swf URL for that specific version.
  284. if (!options.swf) {
  285. options.swf = 'https://vjs.zencdn.net/swf/' + version + '/video-js.swf';
  286. }
  287. // Generate ID for swf object
  288. var objId = options.techId;
  289. // Merge default flashvars with ones passed in to init
  290. var flashVars = mergeOptions({
  291. // SWF Callback Functions
  292. readyFunction: 'videojs.Flash.onReady',
  293. eventProxyFunction: 'videojs.Flash.onEvent',
  294. errorEventProxyFunction: 'videojs.Flash.onError',
  295. // Player Settings
  296. autoplay: options.autoplay,
  297. preload: options.preload,
  298. loop: options.loop,
  299. muted: options.muted
  300. }, options.flashVars);
  301. // Merge default parames with ones passed in
  302. var params = mergeOptions({
  303. // Opaque is needed to overlay controls, but can affect playback performance
  304. wmode: 'opaque',
  305. // Using bgcolor prevents a white flash when the object is loading
  306. bgcolor: '#000000'
  307. }, options.params);
  308. // Merge default attributes with ones passed in
  309. var attributes = mergeOptions({
  310. // Both ID and Name needed or swf to identify itself
  311. id: objId,
  312. name: objId,
  313. 'class': 'vjs-tech'
  314. }, options.attributes);
  315. this.el_ = Flash.embed(options.swf, flashVars, params, attributes);
  316. this.el_.tech = this;
  317. return this.el_;
  318. };
  319. /**
  320. * Called by {@link Player#play} to play using the `Flash` `Tech`.
  321. */
  322. Flash.prototype.play = function play() {
  323. if (this.ended()) {
  324. this.setCurrentTime(0);
  325. }
  326. this.el_.vjs_play();
  327. };
  328. /**
  329. * Called by {@link Player#pause} to pause using the `Flash` `Tech`.
  330. */
  331. Flash.prototype.pause = function pause() {
  332. this.el_.vjs_pause();
  333. };
  334. /**
  335. * A getter/setter for the `Flash` Tech's source object.
  336. * > Note: Please use {@link Flash#setSource}
  337. *
  338. * @param {Tech~SourceObject} [src]
  339. * The source object you want to set on the `Flash` techs.
  340. *
  341. * @return {Tech~SourceObject|undefined}
  342. * - The current source object when a source is not passed in.
  343. * - undefined when setting
  344. *
  345. * @deprecated Since version 5.
  346. */
  347. Flash.prototype.src = function src(_src) {
  348. if (_src === undefined) {
  349. return this.currentSrc();
  350. }
  351. // Setting src through `src` not `setSrc` will be deprecated
  352. return this.setSrc(_src);
  353. };
  354. /**
  355. * A getter/setter for the `Flash` Tech's source object.
  356. *
  357. * @param {Tech~SourceObject} [src]
  358. * The source object you want to set on the `Flash` techs.
  359. */
  360. Flash.prototype.setSrc = function setSrc(src) {
  361. var _this2 = this;
  362. // Make sure source URL is absolute.
  363. src = Url.getAbsoluteURL(src);
  364. this.el_.vjs_src(src);
  365. // Currently the SWF doesn't autoplay if you load a source later.
  366. // e.g. Load player w/ no source, wait 2s, set src.
  367. if (this.autoplay()) {
  368. this.setTimeout(function () {
  369. return _this2.play();
  370. }, 0);
  371. }
  372. };
  373. /**
  374. * Indicates whether the media is currently seeking to a new position or not.
  375. *
  376. * @return {boolean}
  377. * - True if seeking to a new position
  378. * - False otherwise
  379. */
  380. Flash.prototype.seeking = function seeking() {
  381. return this.lastSeekTarget_ !== undefined;
  382. };
  383. /**
  384. * Returns the current time in seconds that the media is at in playback.
  385. *
  386. * @param {number} time
  387. * Current playtime of the media in seconds.
  388. */
  389. Flash.prototype.setCurrentTime = function setCurrentTime(time) {
  390. var seekable = this.seekable();
  391. if (seekable.length) {
  392. // clamp to the current seekable range
  393. time = time > seekable.start(0) ? time : seekable.start(0);
  394. time = time < seekable.end(seekable.length - 1) ? time : seekable.end(seekable.length - 1);
  395. this.lastSeekTarget_ = time;
  396. this.trigger('seeking');
  397. this.el_.vjs_setProperty('currentTime', time);
  398. _Tech.prototype.setCurrentTime.call(this);
  399. }
  400. };
  401. /**
  402. * Get the current playback time in seconds
  403. *
  404. * @return {number}
  405. * The current time of playback in seconds.
  406. */
  407. Flash.prototype.currentTime = function currentTime() {
  408. // when seeking make the reported time keep up with the requested time
  409. // by reading the time we're seeking to
  410. if (this.seeking()) {
  411. return this.lastSeekTarget_ || 0;
  412. }
  413. return this.el_.vjs_getProperty('currentTime');
  414. };
  415. /**
  416. * Get the current source
  417. *
  418. * @method currentSrc
  419. * @return {Tech~SourceObject}
  420. * The current source
  421. */
  422. Flash.prototype.currentSrc = function currentSrc() {
  423. if (this.currentSource_) {
  424. return this.currentSource_.src;
  425. }
  426. return this.el_.vjs_getProperty('currentSrc');
  427. };
  428. /**
  429. * Get the total duration of the current media.
  430. *
  431. * @return {number}
  432. 8 The total duration of the current media.
  433. */
  434. Flash.prototype.duration = function duration() {
  435. if (this.readyState() === 0) {
  436. return NaN;
  437. }
  438. var duration = this.el_.vjs_getProperty('duration');
  439. return duration >= 0 ? duration : Infinity;
  440. };
  441. /**
  442. * Load media into Tech.
  443. */
  444. Flash.prototype.load = function load() {
  445. this.el_.vjs_load();
  446. };
  447. /**
  448. * Get the poster image that was set on the tech.
  449. */
  450. Flash.prototype.poster = function poster() {
  451. this.el_.vjs_getProperty('poster');
  452. };
  453. /**
  454. * Poster images are not handled by the Flash tech so make this is a no-op.
  455. */
  456. Flash.prototype.setPoster = function setPoster() {};
  457. /**
  458. * Determine the time ranges that can be seeked to in the media.
  459. *
  460. * @return {TimeRange}
  461. * Returns the time ranges that can be seeked to.
  462. */
  463. Flash.prototype.seekable = function seekable() {
  464. var duration = this.duration();
  465. if (duration === 0) {
  466. return createTimeRange();
  467. }
  468. return createTimeRange(0, duration);
  469. };
  470. /**
  471. * Get and create a `TimeRange` object for buffering.
  472. *
  473. * @return {TimeRange}
  474. * The time range object that was created.
  475. */
  476. Flash.prototype.buffered = function buffered() {
  477. var ranges = this.el_.vjs_getProperty('buffered');
  478. if (ranges.length === 0) {
  479. return createTimeRange();
  480. }
  481. return createTimeRange(ranges[0][0], ranges[0][1]);
  482. };
  483. /**
  484. * Get fullscreen support -
  485. *
  486. * Flash does not allow fullscreen through javascript
  487. * so this always returns false.
  488. *
  489. * @return {boolean}
  490. * The Flash tech does not support fullscreen, so it will always return false.
  491. */
  492. Flash.prototype.supportsFullScreen = function supportsFullScreen() {
  493. // Flash does not allow fullscreen through javascript
  494. return false;
  495. };
  496. /**
  497. * Flash does not allow fullscreen through javascript
  498. * so this always returns false.
  499. *
  500. * @return {boolean}
  501. * The Flash tech does not support fullscreen, so it will always return false.
  502. */
  503. Flash.prototype.enterFullScreen = function enterFullScreen() {
  504. return false;
  505. };
  506. /**
  507. * Gets available media playback quality metrics as specified by the W3C's Media
  508. * Playback Quality API.
  509. *
  510. * @see [Spec]{@link https://wicg.github.io/media-playback-quality}
  511. *
  512. * @return {Object}
  513. * An object with supported media playback quality metrics
  514. */
  515. Flash.prototype.getVideoPlaybackQuality = function getVideoPlaybackQuality() {
  516. var videoPlaybackQuality = this.el_.vjs_getProperty('getVideoPlaybackQuality');
  517. if (window.performance && typeof window.performance.now === 'function') {
  518. videoPlaybackQuality.creationTime = window.performance.now();
  519. } else if (window.performance && window.performance.timing && typeof window.performance.timing.navigationStart === 'number') {
  520. videoPlaybackQuality.creationTime = window.Date.now() - window.performance.timing.navigationStart;
  521. }
  522. return videoPlaybackQuality;
  523. };
  524. return Flash;
  525. }(Tech);
  526. // Create setters and getters for attributes
  527. var _readWrite = ['rtmpConnection', 'rtmpStream', 'preload', 'defaultPlaybackRate', 'playbackRate', 'autoplay', 'loop', 'controls', 'volume', 'muted', 'defaultMuted'];
  528. var _readOnly = ['networkState', 'readyState', 'initialTime', 'startOffsetTime', 'paused', 'ended', 'videoWidth', 'videoHeight'];
  529. var _api = Flash.prototype;
  530. /**
  531. * Create setters for the swf on the element
  532. *
  533. * @param {string} attr
  534. * The name of the parameter
  535. *
  536. * @private
  537. */
  538. function _createSetter(attr) {
  539. var attrUpper = attr.charAt(0).toUpperCase() + attr.slice(1);
  540. _api['set' + attrUpper] = function (val) {
  541. return this.el_.vjs_setProperty(attr, val);
  542. };
  543. }
  544. /**
  545. * Create getters for the swf on the element
  546. *
  547. * @param {string} attr
  548. * The name of the parameter
  549. *
  550. * @private
  551. */
  552. function _createGetter(attr) {
  553. _api[attr] = function () {
  554. return this.el_.vjs_getProperty(attr);
  555. };
  556. }
  557. // Create getter and setters for all read/write attributes
  558. for (var i = 0; i < _readWrite.length; i++) {
  559. _createGetter(_readWrite[i]);
  560. _createSetter(_readWrite[i]);
  561. }
  562. // Create getters for read-only attributes
  563. for (var _i = 0; _i < _readOnly.length; _i++) {
  564. _createGetter(_readOnly[_i]);
  565. }
  566. /** ------------------------------ Getters ------------------------------ **/
  567. /**
  568. * Get the value of `rtmpConnection` from the swf.
  569. *
  570. * @method Flash#rtmpConnection
  571. * @return {string}
  572. * The current value of `rtmpConnection` on the swf.
  573. */
  574. /**
  575. * Get the value of `rtmpStream` from the swf.
  576. *
  577. * @method Flash#rtmpStream
  578. * @return {string}
  579. * The current value of `rtmpStream` on the swf.
  580. */
  581. /**
  582. * Get the value of `preload` from the swf. `preload` indicates
  583. * what should download before the media is interacted with. It can have the following
  584. * values:
  585. * - none: nothing should be downloaded
  586. * - metadata: poster and the first few frames of the media may be downloaded to get
  587. * media dimensions and other metadata
  588. * - auto: allow the media and metadata for the media to be downloaded before
  589. * interaction
  590. *
  591. * @method Flash#preload
  592. * @return {string}
  593. * The value of `preload` from the swf. Will be 'none', 'metadata',
  594. * or 'auto'.
  595. */
  596. /**
  597. * Get the value of `defaultPlaybackRate` from the swf.
  598. *
  599. * @method Flash#defaultPlaybackRate
  600. * @return {number}
  601. * The current value of `defaultPlaybackRate` on the swf.
  602. */
  603. /**
  604. * Get the value of `playbackRate` from the swf. `playbackRate` indicates
  605. * the rate at which the media is currently playing back. Examples:
  606. * - if playbackRate is set to 2, media will play twice as fast.
  607. * - if playbackRate is set to 0.5, media will play half as fast.
  608. *
  609. * @method Flash#playbackRate
  610. * @return {number}
  611. * The value of `playbackRate` from the swf. A number indicating
  612. * the current playback speed of the media, where 1 is normal speed.
  613. */
  614. /**
  615. * Get the value of `autoplay` from the swf. `autoplay` indicates
  616. * that the media should start to play as soon as the page is ready.
  617. *
  618. * @method Flash#autoplay
  619. * @return {boolean}
  620. * - The value of `autoplay` from the swf.
  621. * - True indicates that the media ashould start as soon as the page loads.
  622. * - False indicates that the media should not start as soon as the page loads.
  623. */
  624. /**
  625. * Get the value of `loop` from the swf. `loop` indicates
  626. * that the media should return to the start of the media and continue playing once
  627. * it reaches the end.
  628. *
  629. * @method Flash#loop
  630. * @return {boolean}
  631. * - The value of `loop` from the swf.
  632. * - True indicates that playback should seek back to start once
  633. * the end of a media is reached.
  634. * - False indicates that playback should not loop back to the start when the
  635. * end of the media is reached.
  636. */
  637. /**
  638. * Get the value of `mediaGroup` from the swf.
  639. *
  640. * @method Flash#mediaGroup
  641. * @return {string}
  642. * The current value of `mediaGroup` on the swf.
  643. */
  644. /**
  645. * Get the value of `controller` from the swf.
  646. *
  647. * @method Flash#controller
  648. * @return {string}
  649. * The current value of `controller` on the swf.
  650. */
  651. /**
  652. * Get the value of `controls` from the swf. `controls` indicates
  653. * whether the native flash controls should be shown or hidden.
  654. *
  655. * @method Flash#controls
  656. * @return {boolean}
  657. * - The value of `controls` from the swf.
  658. * - True indicates that native controls should be showing.
  659. * - False indicates that native controls should be hidden.
  660. */
  661. /**
  662. * Get the value of the `volume` from the swf. `volume` indicates the current
  663. * audio level as a percentage in decimal form. This means that 1 is 100%, 0.5 is 50%, and
  664. * so on.
  665. *
  666. * @method Flash#volume
  667. * @return {number}
  668. * The volume percent as a decimal. Value will be between 0-1.
  669. */
  670. /**
  671. * Get the value of the `muted` from the swf. `muted` indicates the current
  672. * audio level should be silent.
  673. *
  674. * @method Flash#muted
  675. * @return {boolean}
  676. * - True if the audio should be set to silent
  677. * - False otherwise
  678. */
  679. /**
  680. * Get the value of `defaultMuted` from the swf. `defaultMuted` indicates
  681. * whether the media should start muted or not. Only changes the default state of the
  682. * media. `muted` and `defaultMuted` can have different values. `muted` indicates the
  683. * current state.
  684. *
  685. * @method Flash#defaultMuted
  686. * @return {boolean}
  687. * - The value of `defaultMuted` from the swf.
  688. * - True indicates that the media should start muted.
  689. * - False indicates that the media should not start muted.
  690. */
  691. /**
  692. * Get the value of `networkState` from the swf. `networkState` indicates
  693. * the current network state. It returns an enumeration from the following list:
  694. * - 0: NETWORK_EMPTY
  695. * - 1: NEWORK_IDLE
  696. * - 2: NETWORK_LOADING
  697. * - 3: NETWORK_NO_SOURCE
  698. *
  699. * @method Flash#networkState
  700. * @return {number}
  701. * The value of `networkState` from the swf. This will be a number
  702. * from the list in the description.
  703. */
  704. /**
  705. * Get the value of `readyState` from the swf. `readyState` indicates
  706. * the current state of the media element. It returns an enumeration from the
  707. * following list:
  708. * - 0: HAVE_NOTHING
  709. * - 1: HAVE_METADATA
  710. * - 2: HAVE_CURRENT_DATA
  711. * - 3: HAVE_FUTURE_DATA
  712. * - 4: HAVE_ENOUGH_DATA
  713. *
  714. * @method Flash#readyState
  715. * @return {number}
  716. * The value of `readyState` from the swf. This will be a number
  717. * from the list in the description.
  718. */
  719. /**
  720. * Get the value of `readyState` from the swf. `readyState` indicates
  721. * the current state of the media element. It returns an enumeration from the
  722. * following list:
  723. * - 0: HAVE_NOTHING
  724. * - 1: HAVE_METADATA
  725. * - 2: HAVE_CURRENT_DATA
  726. * - 3: HAVE_FUTURE_DATA
  727. * - 4: HAVE_ENOUGH_DATA
  728. *
  729. * @method Flash#readyState
  730. * @return {number}
  731. * The value of `readyState` from the swf. This will be a number
  732. * from the list in the description.
  733. */
  734. /**
  735. * Get the value of `initialTime` from the swf.
  736. *
  737. * @method Flash#initialTime
  738. * @return {number}
  739. * The `initialTime` proprety on the swf.
  740. */
  741. /**
  742. * Get the value of `startOffsetTime` from the swf.
  743. *
  744. * @method Flash#startOffsetTime
  745. * @return {number}
  746. * The `startOffsetTime` proprety on the swf.
  747. */
  748. /**
  749. * Get the value of `paused` from the swf. `paused` indicates whether the swf
  750. * is current paused or not.
  751. *
  752. * @method Flash#paused
  753. * @return {boolean}
  754. * The value of `paused` from the swf.
  755. */
  756. /**
  757. * Get the value of `ended` from the swf. `ended` indicates whether
  758. * the media has reached the end or not.
  759. *
  760. * @method Flash#ended
  761. * @return {boolean}
  762. * - True indicates that the media has ended.
  763. * - False indicates that the media has not ended.
  764. *
  765. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-ended}
  766. */
  767. /**
  768. * Get the value of `videoWidth` from the swf. `videoWidth` indicates
  769. * the current width of the media in css pixels.
  770. *
  771. * @method Flash#videoWidth
  772. * @return {number}
  773. * The value of `videoWidth` from the swf. This will be a number
  774. * in css pixels.
  775. */
  776. /**
  777. * Get the value of `videoHeight` from the swf. `videoHeigth` indicates
  778. * the current height of the media in css pixels.
  779. *
  780. * @method Flassh.prototype.videoHeight
  781. * @return {number}
  782. * The value of `videoHeight` from the swf. This will be a number
  783. * in css pixels.
  784. */
  785. /** ------------------------------ Setters ------------------------------ **/
  786. /**
  787. * Set the value of `rtmpConnection` on the swf.
  788. *
  789. * @method Flash#setRtmpConnection
  790. * @param {string} rtmpConnection
  791. * New value to set the `rtmpConnection` property to.
  792. */
  793. /**
  794. * Set the value of `rtmpStream` on the swf.
  795. *
  796. * @method Flash#setRtmpStream
  797. * @param {string} rtmpStream
  798. * New value to set the `rtmpStream` property to.
  799. */
  800. /**
  801. * Set the value of `preload` on the swf. `preload` indicates
  802. * what should download before the media is interacted with. It can have the following
  803. * values:
  804. * - none: nothing should be downloaded
  805. * - metadata: poster and the first few frames of the media may be downloaded to get
  806. * media dimensions and other metadata
  807. * - auto: allow the media and metadata for the media to be downloaded before
  808. * interaction
  809. *
  810. * @method Flash#setPreload
  811. * @param {string} preload
  812. * The value of `preload` to set on the swf. Should be 'none', 'metadata',
  813. * or 'auto'.
  814. */
  815. /**
  816. * Set the value of `defaultPlaybackRate` on the swf.
  817. *
  818. * @method Flash#setDefaultPlaybackRate
  819. * @param {number} defaultPlaybackRate
  820. * New value to set the `defaultPlaybackRate` property to.
  821. */
  822. /**
  823. * Set the value of `playbackRate` on the swf. `playbackRate` indicates
  824. * the rate at which the media is currently playing back. Examples:
  825. * - if playbackRate is set to 2, media will play twice as fast.
  826. * - if playbackRate is set to 0.5, media will play half as fast.
  827. *
  828. * @method Flash#setPlaybackRate
  829. * @param {number} playbackRate
  830. * New value of `playbackRate` on the swf. A number indicating
  831. * the current playback speed of the media, where 1 is normal speed.
  832. */
  833. /**
  834. * Set the value of `autoplay` on the swf. `autoplay` indicates
  835. * that the media should start to play as soon as the page is ready.
  836. *
  837. * @method Flash#setAutoplay
  838. * @param {boolean} autoplay
  839. * - The value of `autoplay` from the swf.
  840. * - True indicates that the media ashould start as soon as the page loads.
  841. * - False indicates that the media should not start as soon as the page loads.
  842. */
  843. /**
  844. * Set the value of `loop` on the swf. `loop` indicates
  845. * that the media should return to the start of the media and continue playing once
  846. * it reaches the end.
  847. *
  848. * @method Flash#setLoop
  849. * @param {boolean} loop
  850. * - True indicates that playback should seek back to start once
  851. * the end of a media is reached.
  852. * - False indicates that playback should not loop back to the start when the
  853. * end of the media is reached.
  854. */
  855. /**
  856. * Set the value of `mediaGroup` on the swf.
  857. *
  858. * @method Flash#setMediaGroup
  859. * @param {string} mediaGroup
  860. * New value of `mediaGroup` to set on the swf.
  861. */
  862. /**
  863. * Set the value of `controller` on the swf.
  864. *
  865. * @method Flash#setController
  866. * @param {string} controller
  867. * New value the current value of `controller` on the swf.
  868. */
  869. /**
  870. * Get the value of `controls` from the swf. `controls` indicates
  871. * whether the native flash controls should be shown or hidden.
  872. *
  873. * @method Flash#controls
  874. * @return {boolean}
  875. * - The value of `controls` from the swf.
  876. * - True indicates that native controls should be showing.
  877. * - False indicates that native controls should be hidden.
  878. */
  879. /**
  880. * Set the value of the `volume` on the swf. `volume` indicates the current
  881. * audio level as a percentage in decimal form. This means that 1 is 100%, 0.5 is 50%, and
  882. * so on.
  883. *
  884. * @method Flash#setVolume
  885. * @param {number} percentAsDecimal
  886. * The volume percent as a decimal. Value will be between 0-1.
  887. */
  888. /**
  889. * Set the value of the `muted` on the swf. `muted` indicates that the current
  890. * audio level should be silent.
  891. *
  892. * @method Flash#setMuted
  893. * @param {boolean} muted
  894. * - True if the audio should be set to silent
  895. * - False otherwise
  896. */
  897. /**
  898. * Set the value of `defaultMuted` on the swf. `defaultMuted` indicates
  899. * whether the media should start muted or not. Only changes the default state of the
  900. * media. `muted` and `defaultMuted` can have different values. `muted` indicates the
  901. * current state.
  902. *
  903. * @method Flash#setDefaultMuted
  904. * @param {boolean} defaultMuted
  905. * - True indicates that the media should start muted.
  906. * - False indicates that the media should not start muted.
  907. */
  908. /* Flash Support Testing -------------------------------------------------------- */
  909. /**
  910. * Check if the Flash tech is currently supported.
  911. *
  912. * @return {boolean}
  913. * - True for Chrome and Safari Desktop and Microsoft Edge and if flash tech is supported
  914. * - False otherwise
  915. */
  916. Flash.isSupported = function () {
  917. // for Chrome Desktop and Safari Desktop
  918. if (videojs.browser.IS_CHROME && (!videojs.browser.IS_ANDROID || !videojs.browser.IS_IOS) || videojs.browser.IS_SAFARI && !videojs.browser.IS_IOS || videojs.browser.IS_EDGE) {
  919. return true;
  920. }
  921. // for other browsers
  922. return Flash.version()[0] >= 10;
  923. };
  924. // Add Source Handler pattern functions to this tech
  925. Tech.withSourceHandlers(Flash);
  926. /*
  927. * Native source handler for flash, simply passes the source to the swf element.
  928. *
  929. * @property {Tech~SourceObject} source
  930. * The source object
  931. *
  932. * @property {Flash} tech
  933. * The instance of the Flash tech
  934. */
  935. Flash.nativeSourceHandler = {};
  936. /**
  937. * Check if the Flash can play the given mime type.
  938. *
  939. * @param {string} type
  940. * The mimetype to check
  941. *
  942. * @return {string}
  943. * 'maybe', or '' (empty string)
  944. */
  945. Flash.nativeSourceHandler.canPlayType = function (type) {
  946. if (type in Flash.formats) {
  947. return 'maybe';
  948. }
  949. return '';
  950. };
  951. /**
  952. * Check if the media element can handle a source natively.
  953. *
  954. * @param {Tech~SourceObject} source
  955. * The source object
  956. *
  957. * @param {Object} [options]
  958. * Options to be passed to the tech.
  959. *
  960. * @return {string}
  961. * 'maybe', or '' (empty string).
  962. */
  963. Flash.nativeSourceHandler.canHandleSource = function (source, options) {
  964. var type = void 0;
  965. /**
  966. * Guess the mime type of a file if it does not have one
  967. *
  968. * @param {Tech~SourceObject} src
  969. * The source object to guess the mime type for
  970. *
  971. * @return {string}
  972. * The mime type that was guessed
  973. */
  974. function guessMimeType(src) {
  975. var ext = Url.getFileExtension(src);
  976. if (ext) {
  977. return 'video/' + ext;
  978. }
  979. return '';
  980. }
  981. if (!source.type) {
  982. type = guessMimeType(source.src);
  983. } else {
  984. // Strip code information from the type because we don't get that specific
  985. type = source.type.replace(/;.*/, '').toLowerCase();
  986. }
  987. return Flash.nativeSourceHandler.canPlayType(type);
  988. };
  989. /**
  990. * Pass the source to the swf.
  991. *
  992. * @param {Tech~SourceObject} source
  993. * The source object
  994. *
  995. * @param {Flash} tech
  996. * The instance of the Flash tech
  997. *
  998. * @param {Object} [options]
  999. * The options to pass to the source
  1000. */
  1001. Flash.nativeSourceHandler.handleSource = function (source, tech, options) {
  1002. tech.setSrc(source.src);
  1003. };
  1004. /**
  1005. * noop for native source handler dispose, as cleanup will happen automatically.
  1006. */
  1007. Flash.nativeSourceHandler.dispose = function () {};
  1008. // Register the native source handler
  1009. Flash.registerSourceHandler(Flash.nativeSourceHandler);
  1010. /**
  1011. * Flash supported mime types.
  1012. *
  1013. * @constant {Object}
  1014. */
  1015. Flash.formats = {
  1016. 'video/flv': 'FLV',
  1017. 'video/x-flv': 'FLV',
  1018. 'video/mp4': 'MP4',
  1019. 'video/m4v': 'MP4'
  1020. };
  1021. /**
  1022. * Called when the the swf is "ready", and makes sure that the swf is really
  1023. * ready using {@link Flash#checkReady}
  1024. *
  1025. * @param {Object} currSwf
  1026. * The current swf object
  1027. */
  1028. Flash.onReady = function (currSwf) {
  1029. var el = Dom.$('#' + currSwf);
  1030. var tech = el && el.tech;
  1031. // if there is no el then the tech has been disposed
  1032. // and the tech element was removed from the player div
  1033. if (tech && tech.el()) {
  1034. // check that the flash object is really ready
  1035. Flash.checkReady(tech);
  1036. }
  1037. };
  1038. /**
  1039. * The SWF isn't always ready when it says it is. Sometimes the API functions still
  1040. * need to be added to the object. If it's not ready, we set a timeout to check again
  1041. * shortly.
  1042. *
  1043. * @param {Flash} tech
  1044. * The instance of the flash tech to check.
  1045. */
  1046. Flash.checkReady = function (tech) {
  1047. // stop worrying if the tech has been disposed
  1048. if (!tech.el()) {
  1049. return;
  1050. }
  1051. // check if API property exists
  1052. if (tech.el().vjs_getProperty) {
  1053. // tell tech it's ready
  1054. tech.triggerReady();
  1055. } else {
  1056. // wait longer
  1057. this.setTimeout(function () {
  1058. Flash.checkReady(tech);
  1059. }, 50);
  1060. }
  1061. };
  1062. /**
  1063. * Trigger events from the swf on the Flash Tech.
  1064. *
  1065. * @param {number} swfID
  1066. * The id of the swf that had the event
  1067. *
  1068. * @param {string} eventName
  1069. * The name of the event to trigger
  1070. */
  1071. Flash.onEvent = function (swfID, eventName) {
  1072. var tech = Dom.$('#' + swfID).tech;
  1073. var args = Array.prototype.slice.call(arguments, 2);
  1074. // dispatch Flash events asynchronously for two reasons:
  1075. // - Flash swallows any exceptions generated by javascript it
  1076. // invokes
  1077. // - Flash is suspended until the javascript returns which may cause
  1078. // playback performance issues
  1079. tech.setTimeout(function () {
  1080. tech.trigger(eventName, args);
  1081. }, 1);
  1082. };
  1083. /**
  1084. * Log errors from the swf on the Flash tech.
  1085. *
  1086. * @param {number} swfID
  1087. * The id of the swf that had an error.
  1088. *
  1089. * @param {string} err
  1090. * The error to set on the Flash Tech.
  1091. *
  1092. * @return {MediaError|undefined}
  1093. * - Returns a MediaError when err is 'srcnotfound'
  1094. * - Returns undefined otherwise.
  1095. */
  1096. Flash.onError = function (swfID, err) {
  1097. var tech = Dom.$('#' + swfID).tech;
  1098. // trigger MEDIA_ERR_SRC_NOT_SUPPORTED
  1099. if (err === 'srcnotfound') {
  1100. return tech.error(4);
  1101. }
  1102. // trigger a custom error
  1103. if (typeof err === 'string') {
  1104. tech.error('FLASH: ' + err);
  1105. } else {
  1106. err.origin = 'flash';
  1107. tech.error(err);
  1108. }
  1109. };
  1110. /**
  1111. * Get the current version of Flash that is in use on the page.
  1112. *
  1113. * @return {Array}
  1114. * an array of versions that are available.
  1115. */
  1116. Flash.version = function () {
  1117. var version$$1 = '0,0,0';
  1118. // IE
  1119. try {
  1120. version$$1 = new window.ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  1121. // other browsers
  1122. } catch (e) {
  1123. try {
  1124. if (navigator.mimeTypes['application/x-shockwave-flash'].enabledPlugin) {
  1125. version$$1 = (navigator.plugins['Shockwave Flash 2.0'] || navigator.plugins['Shockwave Flash']).description.replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  1126. }
  1127. } catch (err) {
  1128. // satisfy linter
  1129. }
  1130. }
  1131. return version$$1.split(',');
  1132. };
  1133. /**
  1134. * Only use for non-iframe embeds.
  1135. *
  1136. * @param {Object} swf
  1137. * The videojs-swf object.
  1138. *
  1139. * @param {Object} flashVars
  1140. * Names and values to use as flash option variables.
  1141. *
  1142. * @param {Object} params
  1143. * Style parameters to set on the object.
  1144. *
  1145. * @param {Object} attributes
  1146. * Attributes to set on the element.
  1147. *
  1148. * @return {Element}
  1149. * The embeded Flash DOM element.
  1150. */
  1151. Flash.embed = function (swf, flashVars, params, attributes) {
  1152. var code = Flash.getEmbedCode(swf, flashVars, params, attributes);
  1153. // Get element by embedding code and retrieving created element
  1154. var obj = Dom.createEl('div', { innerHTML: code }).childNodes[0];
  1155. return obj;
  1156. };
  1157. /**
  1158. * Only use for non-iframe embeds.
  1159. *
  1160. * @param {Object} swf
  1161. * The videojs-swf object.
  1162. *
  1163. * @param {Object} flashVars
  1164. * Names and values to use as flash option variables.
  1165. *
  1166. * @param {Object} params
  1167. * Style parameters to set on the object.
  1168. *
  1169. * @param {Object} attributes
  1170. * Attributes to set on the element.
  1171. *
  1172. * @return {Element}
  1173. * The embeded Flash DOM element.
  1174. */
  1175. Flash.getEmbedCode = function (swf, flashVars, params, attributes) {
  1176. var objTag = '<object type="application/x-shockwave-flash" ';
  1177. var flashVarsString = '';
  1178. var paramsString = '';
  1179. var attrsString = '';
  1180. // Convert flash vars to string
  1181. if (flashVars) {
  1182. Object.getOwnPropertyNames(flashVars).forEach(function (key) {
  1183. flashVarsString += key + '=' + flashVars[key] + '&amp;';
  1184. });
  1185. }
  1186. // Add swf, flashVars, and other default params
  1187. params = mergeOptions({
  1188. movie: swf,
  1189. flashvars: flashVarsString,
  1190. // Required to talk to swf
  1191. allowScriptAccess: 'always',
  1192. // All should be default, but having security issues.
  1193. allowNetworking: 'all'
  1194. }, params);
  1195. // Create param tags string
  1196. Object.getOwnPropertyNames(params).forEach(function (key) {
  1197. paramsString += '<param name="' + key + '" value="' + params[key] + '" />';
  1198. });
  1199. attributes = mergeOptions({
  1200. // Add swf to attributes (need both for IE and Others to work)
  1201. data: swf,
  1202. // Default to 100% width/height
  1203. width: '100%',
  1204. height: '100%'
  1205. }, attributes);
  1206. // Create Attributes string
  1207. Object.getOwnPropertyNames(attributes).forEach(function (key) {
  1208. attrsString += key + '="' + attributes[key] + '" ';
  1209. });
  1210. return '' + objTag + attrsString + '>' + paramsString + '</object>';
  1211. };
  1212. // Run Flash through the RTMP decorator
  1213. FlashRtmpDecorator(Flash);
  1214. if (Tech.getTech('Flash')) {
  1215. videojs.log.warn('Not using videojs-flash as it appears to already be registered');
  1216. videojs.log.warn('videojs-flash should only be used with video.js@6 and above');
  1217. } else {
  1218. videojs.registerTech('Flash', Flash);
  1219. }
  1220. Flash.VERSION = version$1;
  1221. export default Flash;