-
Notifications
You must be signed in to change notification settings - Fork 22
/
enableAirPlay.js
103 lines (91 loc) · 3.11 KB
/
enableAirPlay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
/**
* @module enableAirPlay
*/
var hasAirPlayAPISupport = require('./lib/hasAirPlayAPISupport'),
_ = require('underscore');
/**
* @private
* @param {object} the Video.js Player instance
* @returns {AirPlayButton} or `undefined` if it does not exist
*/
function getExistingAirPlayButton(player) {
return player.controlBar.getChild('airPlayButton');
}
/**
* Adds the AirPlayButton Component to the player's ControlBar component, if the
* AirPlayButton does not already exist in the ControlBar.
* @private
* @param player {object} the Video.js Player instance
* @param options {object}
*/
function ensureAirPlayButtonExists(player, options) {
var existingAirPlayButton = getExistingAirPlayButton(player);
if (options.addButtonToControlBar && !existingAirPlayButton) {
player.controlBar.addChild('airPlayButton', options);
}
}
/**
* Handles requests for AirPlay triggered by the AirPlayButton Component.
*
* @private
* @param player {object} the Video.js Player instance
*/
function onAirPlayRequested(player) {
var mediaEl = player.el().querySelector('video, audio');
if (mediaEl && mediaEl.webkitShowPlaybackTargetPicker) {
mediaEl.webkitShowPlaybackTargetPicker();
}
}
/**
* Adds an event listener for the `airPlayRequested` event triggered by the AirPlayButton
* Component.
*
* @private
* @param player {object} the Video.js Player instance
*/
function listenForAirPlayEvents(player) {
// Respond to requests for AirPlay. The AirPlayButton component triggers this event
// when the user clicks the AirPlay button.
player.on('airPlayRequested', onAirPlayRequested.bind(null, player));
}
/**
* Sets up the AirPlay plugin.
*
* @private
* @param player {object} the Video.js player
* @param options {object} the plugin options
*/
function enableAirPlay(player, options) {
if (!player.controlBar) {
return;
}
if (hasAirPlayAPISupport()) {
listenForAirPlayEvents(player);
ensureAirPlayButtonExists(player, options);
}
}
/**
* Registers the AirPlay plugin with Video.js. Calls
* {@link http://docs.videojs.com/module-videojs.html#~registerPlugin|videojs#registerPlugin},
* which will add a plugin function called `airPlay` to any instance of a Video.js player
* that is created after calling this function. Call `player.airPlay(options)`, passing in
* configuration options, to enable the AirPlay plugin on your Player instance.
*
* Currently, the only configuration option is:
*
* * **buttonText** - the text to display inside of the button component. By default,
* this text is hidden and is used for accessibility purposes.
*
* @param {object} videojs
* @see http://docs.videojs.com/module-videojs.html#~registerPlugin
*/
module.exports = function(videojs) {
videojs.registerPlugin('airPlay', function(options) {
var pluginOptions = _.extend({ addButtonToControlBar: true }, options || {});
// `this` is an instance of a Video.js Player.
// Wait until the player is "ready" so that the player's control bar component has
// been created.
this.ready(enableAirPlay.bind(this, this, pluginOptions));
});
};