Skip to content
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 #2162

Closed
wants to merge 0 commits into from

Conversation

Alpaczyk
Copy link
Contributor

@Alpaczyk Alpaczyk commented Apr 3, 2024

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.

@ImprovedTube
Copy link
Member

Awesome! @Alpaczyk

  • you added a .vs/slnx.sqlite
  • nice, you even translated the key to many language files!!
    • sorry, github shows conflicts (your fork wasnt updated since march 11)

      crowdin

      • and Github can't understand these changes (line breaks and tabs only) (besides that orders changed too)
        • which is why i claim white-space aren't required in the world and could be added as a flavor/theme locally, for beautification, like https://github.com/google/blockly (even thats utopia for today) (👋😅 @raszpl )
          • Except for python. - Or all code languages could be like python, including JSON and then we wouldn't use curly brackets { }. ( And then curly brackets could be the extra added visually only for those used to all other languages besides Python. )

Github also doesn't let me accept the .js changes alone already, before thinking more about JSON.

@raszpl
Copy link
Contributor

raszpl commented Apr 4, 2024

This is a fun feature.

if(qualityWithoutFocus != 'disabled' && player){

will trigger for everyone :-) tons of problems with this all around, its the bogus way satus menu system stores user settings. I think this should be fine:
if(qualityWithoutFocus && qualityWithoutFocus != 'disabled' && player){

refresh = function () {
document.getElementById('player_quality').dispatchEvent(new CustomEvent('render'));
document.getElementById('player_codecs').dispatchEvent(new CustomEvent('render'));
document.getElementById('optimize_codec_for_hardware_acceleration').dispatchEvent(new CustomEvent('render'));

needs
document.getElementById('player_quality_without_focus').dispatchEvent(new CustomEvent('render'));
to update displayed resolution in case someone disables high res codecs

player.setPlaybackQuality(qualityWithoutFocus);
player.setPlaybackQualityRange(qualityWithoutFocus);
}
if (this.focus === true){
player.setPlaybackQuality(qualityWithFocus);
player.setPlaybackQualityRange(qualityWithFocus);

There was this #1666 I cant find a video lacking 480p right now to test if its still an issue, if it is then this stupid amount of code in playerQuality is required:

ImprovedTube.playerQuality = function () {
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;
};
var player = this.elements.player,
quality = this.storage.player_quality;
if (player && player.getAvailableQualityLevels && !player.dataset.defaultQuality) {
var available_quality_levels = player.getAvailableQualityLevels();
if (quality && quality !== 'auto') {
if (available_quality_levels.includes(quality) === false) {
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.reduce(function (array, elem) {
array.push(resolution[label.indexOf(elem)]); return array;
}, []);
quality = closest (resolution[label.indexOf(quality)], availableresolutions);
quality = label[resolution.indexOf(quality)];
}

@ImprovedTube
Copy link
Member

if(qualityWithoutFocus && qualityWithoutFocus != 'disabled' && player){

yes @raszpl we need if(qualityWithoutFocus while dropdown's defaults are not stored by default (#1685). And we won't need to check != 'disabled' anymore, if we stop to ever store that value (after checking/remembering if that has a purpose in any case or two)


the function can begin like this:

ImprovedTube.playerQualityWithoutFocus = function () { 
    qualityWithoutFocus = this.storage.player_quality_without_focus;
   	if(qualityWithoutFocus && qualityWithoutFocus != 'disabled'){`
 		var player = this.elements.player,
    	if (player) { 
			qualityWithFocus = this.storage.player_quality;
			.............

( same for playerQuality():

ImprovedTube.playerQuality = function ()
	quality = this.storage.player_quality;	
	if (quality && quality !== 'auto') {
		if (quality && quality !== 'auto') {
		var player = this.elements.player,
		if (player && player.getAvailableQualityLevels && !player.dataset.defaultQuality) {		if (player && player.getAvailableQualityLevels && !player.dataset.defaultQuality) {
		var available_quality_levels = player.getAvailableQualityLevels();		var available_quality_levels = player.getAvailableQualityLevels();	
			
			function closest (num, arr) {
                let curr = arr[0];
                let diff = Math.abs (num - curr);
				.......

)

And shorter, combined:

ImprovedTube.playerQualityWithoutFocus = function () {  qualityWithoutFocus = this.storage.player_quality_without_focus;
   if(qualityWithoutFocus && qualityWithoutFocus != 'disabled'){
   	if(this.focus === false) { ImprovedTube.playerQuality(qualityWithoutFocus);}
		else if (this.focus === true) { ImprovedTube.playerQuality(); }
	}
}	

adding qualityWithoutFocus ? quality = qualityWithoutFocus : to the start of playerQuality()...

(

ImprovedTube.playerQuality = function (qualityWithoutFocus) {
	qualityWithoutFocus ? quality = qualityWithoutFocus : 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) === false) {
				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.reduce(function (array, elem) {
					array.push(resolution[label.indexOf(elem)]); return array;
					}, []);

				quality = closest (resolution[label.indexOf(quality)], availableresolutions);
				quality = label[resolution.indexOf(quality)];
			}

			player.setPlaybackQualityRange(quality);
			player.setPlaybackQuality(quality);
			player.dataset.defaultQuality = quality;
		}
	}
};         

)

@ImprovedTube ImprovedTube marked this pull request as draft April 4, 2024 13:46
@ImprovedTube ImprovedTube added 🧩Plan ready Solution or some specification noted; To-Do; steps for implementation (+raw brainstorming too maybe) Knowledge Base / Dokumenation for developers We should repurpose this for future reference / Wiki / Education / Introduction 🥳🤩Yay!👏 legendary labels Apr 4, 2024
@ImprovedTube
Copy link
Member

... if(this.focus === false) { if (remainingTime < (17s + 0.2% videoLength)) { break; }

(Pseudo code)

@Alpaczyk
Copy link
Contributor Author

#2212 new pull request with changes regarding the comments

ImprovedTube added a commit that referenced this pull request Apr 27, 2024
Feature #300: Reduce quality when window loses focus (from pull request #2162)
ImprovedTube added a commit that referenced this pull request Apr 27, 2024
ImprovedTube added a commit that referenced this pull request Apr 27, 2024
ImprovedTube added a commit that referenced this pull request Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Knowledge Base / Dokumenation for developers We should repurpose this for future reference / Wiki / Education / Introduction 🧩Plan ready Solution or some specification noted; To-Do; steps for implementation (+raw brainstorming too maybe) 🥳🤩Yay!👏 legendary
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants