-
Notifications
You must be signed in to change notification settings - Fork 1
/
videojs.viblast-menu.js
150 lines (122 loc) · 4.39 KB
/
videojs.viblast-menu.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(function() {
var videojs = window.videojs;
(function(window, videojs) {
var MenuItem = videojs.getComponent('MenuItem');
var ViblastQualityItem = videojs.extend(MenuItem, {
constructor : function(player, options) {
var createQualityLabel = options.customLabel && (options.customLabel[options["viblastQualityStreamtype"]+"Stream"] || options.customLabel['default']);
if (typeof createQualityLabel != 'function') {
createQualityLabel = this.createLabel;
}
options.label = createQualityLabel(options.quality, options.index);
options.selectable = true;
MenuItem.call(this, player, options);
var this_ = this;
player.on("quality-changed", function(event, data) {
if (data.data === this_.options_.quality) {
this_.selected(true);
} else {
this_.selected(false);
}
});
}
});
ViblastQualityItem.prototype.createLabel = function (quality, index) {
//custom generated, not a real quality
if (quality === 'auto') {
return quality;
}
if (!isNaN(quality.bandwidth)) {
return quality.bandwidth/1000+'kbps';
} else if (!isNaN(quality.width)){
return quality.width+"x"+quality.height;
} else {
return index;
}
}
ViblastQualityItem.prototype.handleClick = function(event) {
if (this.options_.label === 'auto') {
this.player_.viblast.abr = true;
} else {
this.player_.viblast['video'].quality = this.options_.quality;
}
this.player_.trigger("quality-changed", {
data : this.options_.quality
});
};
ViblastQualityItem.prototype.buildCSSClass = function() {
return MenuItem.prototype.buildCSSClass.call(this) + ` viblast-quality-item`;
}
MenuItem.registerComponent('ViblastQualityItem', ViblastQualityItem);
var MenuButton = videojs.getComponent('MenuButton');
var ViblastQualityMenuButton = videojs.extend(MenuButton, {
constructor : function(player, options) {
MenuButton.call(this, player, options);
}
});
ViblastQualityMenuButton.prototype.createItems = function() {
var menuItems = [];
for (var index in this.options_.qualities) {
menuItems.push(new ViblastQualityItem(
this.player_,
{
customLabel: this.options_.customLabel,
quality : this.options_.qualities[index],
viblastQualityStreamtype:this.options_.viblastQualityStreamtype,
index:(index+1),
})
);
}
menuItems.push(new ViblastQualityItem(
this.player_,
{
customLabel: this.options_.customLabel,
quality: "auto",
viblastQualityStreamtype:this.options_.viblastQualityStreamtype,
index:0,
selected : true,
})
);
return menuItems;
};
ViblastQualityMenuButton.prototype.buildCSSClass = function() {
return MenuButton.prototype.buildCSSClass.call(this) + `viblast-quality-menu vjs-icon-cog`;
}
ViblastQualityMenuButton.prototype.controlText_ = 'Qualities';
videojs.registerComponent('ViblastQualityMenuButton', ViblastQualityMenuButton);
var ViblastQualityMenu = function(options) {
var processQualities = function(player) {
if (player.viblast['video'].qualities.length>1 && player.viblast['audio'].qualities.length<=1) {
options.qualities = player.viblast['video'].qualities;
options.viblastQualityStreamtype="video";
} else if (player.viblast['video'].qualities.length<=1 && player.viblast['audio'].qualities.length>1) {
options.qualities = player.viblast['audio'].qualities;
options.viblastQualityStreamtype="audio";
} else if (player.viblast['video'].qualities.length>1 && player.viblast['audio'].qualities.length>1) {
// build more complex control;
return;
} else {
return;
}
var menuButton = new ViblastQualityMenuButton(player, options);
player.controlBar.el()
.insertBefore(menuButton.el(), player.controlBar.fullscreenToggle.el());
}
this.on("ready", function() {
if (this.viblast) {
var player = this;
//if Viblast already has the variant list fill the component
if ((player.viblast.video && player.viblast.video.qualities) ||
(player.viblast.audio && player.viblast.audio.qualities)) {
processQualities(player);
} else { //else wait until it is ready
player.viblast.addEventListener('updatedmetadata', function(event) {
processQualities(player);
});
}
}
});
};
videojs.plugin('ViblastQualityMenu', ViblastQualityMenu);
})(window, videojs);
})();