-
-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature #300: Reduce quality when window loses focus (from pull request #2162) #2212
Conversation
should probably just be else here?
assumes storage.player_quality is always enabled. ImprovedTube.playerQuality actually tests this, but its in the middle of function and easy to miss :(
this.focus === false case should save current playback quality, and try to restore saved value hmm, something like this? :
BUT! no idea if player.getPlaybackQuality() is guaranteed to always return something valid and sane. There is also danger of playerQualityWithoutFocus being called very early when player was just created and YT is slowly ramping up video download while setting initial video quality to very low, then we set qualityWithoutFocus, then user switches to the tab and we force set this saved (qualityWithoutFocus) very low quality :o Its not as easy peasy as I first thought :( for the future: Ideally ImprovedTube should have a facility to monitor for user manually changing quality thru player interface during playback. So the above, but with
should be fine? ps: Seeing as both playerQuality and playerQualityWithoutFocus have to perform this stupid 'find closest quality available' dance and thus share almost half the functions code, maybe it would be prudent to combining both at some point later in time when its debugged/ironed out and battle tested. |
started combining here #2162 (comment) |
merged before it was fixed
|
for the credits |
/*------------------------------------------------------------------------------
QUALITY
------------------------------------------------------------------------------*/
ImprovedTube.playerQuality = function (quality) {
if (!quality) quality = this.storage.player_quality;
if (quality && quality !== 'auto') {
var player = this.elements.player;
if (player && player.getAvailableQualityLevels && !player.dataset.defaultQuality) {
var available_quality_levels = player.getAvailableQualityLevels();
function closest(num, arr) {
let curr = arr[0];
let diff = Math.abs(num - curr);
for (let val = 0; val < arr.length; val++) {
let newdiff = Math.abs(num - arr[val]);
if (newdiff < diff) {
diff = newdiff;
curr = arr[val];
}
}
return curr;
}
if (!available_quality_levels.includes(quality)) {
let label = ['tiny', 'small', 'medium', 'large', 'hd720', 'hd1080', 'hd1440', 'hd2160', 'hd2880', 'highres'];
let resolution = ['144', '240', '360', '480', '720', '1080', '1440', '2160', '2880', '4320'];
let availableresolutions = available_quality_levels.map(q => resolution[label.indexOf(q)]);
quality = closest(resolution[label.indexOf(quality)], availableresolutions);
quality = label[resolution.indexOf(quality)];
}
player.setPlaybackQualityRange(quality);
player.setPlaybackQuality(quality);
player.dataset.defaultQuality = quality;
}
}
}; /*------------------------------------------------------------------------------
QUALITY WITHOUT FOCUS
------------------------------------------------------------------------------*/
ImprovedTube.playerQualityWithoutFocus = function () {
var qualityWithoutFocus = this.storage.player_quality_without_focus;
if (qualityWithoutFocus && qualityWithoutFocus !== 'disabled') {
if (this.focus === true) {
if (ImprovedTube.qualityBeforeBlur) {
ImprovedTube.playerQuality(ImprovedTube.qualityBeforeBlur);
} else {
ImprovedTube.playerQuality();
}
} else if (this.focus === false) {
ImprovedTube.qualityBeforeBlur = this.elements.player.getPlaybackQuality();
if (ImprovedTube.played_time > 0 && ImprovedTube.formatSecond(player.getDuration() * 0.997 - player.getCurrentTime()) > 11) {
setTimeout(function () {
if (this.focus === false) {
ImprovedTube.playerQuality(qualityWithoutFocus);
}
}, Math.max(321, 3210 - ImprovedTube.played_time));
}
}
}
}; |
crashes (player.getDuration)
are you using Cunningham's Law on me ? :] |
also would be nice to throw checking iv video is paused in there |
This commit is adding a feature described in #300. Users can choose the quality to which the player will lower when the window is unfocused. When the window is focused again, the quality is coming back to the desired one.
By mistake I closed previous pull request