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 Request] Allow diffrent preset for video quality based on power source #103 #2292

Merged
merged 9 commits into from
May 21, 2024

Conversation

Nirbhay97
Copy link
Contributor

issue -> quality and bandwidth #2240

ImprovedTube added a commit that referenced this pull request May 21, 2024
@ImprovedTube
Copy link
Member

Thank you! @Nirbhay97

  • Added "battery" to the extension permissions in the manifest.json file 195cd99

Typically our users might want 1080p usually and 720p or 480p on battery or to pause when the charger is unplugged
i also appreciate the gradually switching down like a gradual reminder.

    "qualityWhenRunningOnBattery":{   "message": "Quality, when running on battery"  },
    "pauseWhileIUnplugTheCharger":{    "message": "Pause when I unplug the charger!" },
    "whenBatteryIslowDecreaseQuality":{  "message": "Low battery: Gradually decrease the quality!"  },

The battery listener/s can depend on battery feature/s being enabled.

if (improvedTube.storage.qualityWhenRunningOnBattery || improvedTube.storage.pauseWhileIUnplugTheCharger || improvedTube.storage.whenBatteryIslowDecreaseQuality) {
    async function battery() {
        try {
            const battery = await navigator.getBattery(); 
            if (battery === null) { 
                console.log('Desktop!(?) "Battery === null"'); 
            } else if (battery.level === null) { 
                console.log("No battery present"); 
            } else if (improvedTube.storage.qualityWhenRunningOnBattery || improvedTube.storage.whenBatteryIslowDecreaseQuality) {
                battery.addEventListener("levelchange", () => {
                    if (!battery.charging) {
                        if (improvedTube.storage.qualityWhenRunningOnBattery) {
                            playerQuality(improvedTube.storage.qualityWhenRunningOnBattery);
                        }
                        if (improvedTube.storage.whenBatteryIslowDecreaseQuality && (battery.level < 0.15 || battery.dischargingTime < 1200)) {
                            if (battery.level > 0.11 || battery.dischargingTime > 900) {
                                playerQuality("large");
                            } else if (battery.level > 0.08 || battery.dischargingTime > 600) {
                                playerQuality("medium");
                            } else if (battery.level > 0.04 && battery.dischargingTime > 360) {
                                playerQuality("small");
                            } else {
                                playerQuality("tiny");
                            }
                        }
                    }
                });
            } 
            if (improvedTube.storage.pauseWhileIUnplugTheCharger) {
                battery.addEventListener("chargingchange", () => {
                    if (!battery.charging) { 
                        ImprovedTube.elements.player.pauseVideo(); 
                        ImprovedTube.paused = true; 
                    } else if (ImprovedTube.paused === true) { 
                        ImprovedTube.elements.player.playVideo(); 
                        delete ImprovedTube.paused; 
                    }
                });
            }
        } catch (error) { 
            console.error("Error accessing battery information:", error.message); 
        }
    } 
    battery();  
}

(untested)

  • The position in the menu can be next to our Quality & Quality without Focus (Codec, FPS, HDR settings also are related)
    • Many people use our extension on their desktop PC. So in our menu we can make battery features invisible on Desktops.

(BTW, in our menu UI, we only used the on click functions when a toggle should cause another one to flip, so thats not required here yet)

@Nirbhay97
Copy link
Contributor Author

Nirbhay97 commented May 21, 2024

That's a really well written and improved code considering different battery cases, a whole lot of learning for me! Thanks @ImprovedTube .

Since the PR is still open, what is to be done from my side now? Or what things shall I focus on now for this feat.

@ImprovedTube
Copy link
Member

Hi! @Nirbhay97
was yours tested already?

if you like you can.. : ..test/finish this with my code:

	ImprovedTube.playerQuality();
	ImprovedTube.batteryFeatures();

(we dont need to call it on focus, like QualityWithoutFocus)


..and/or check how to do this too:

So in our menu we can make battery features invisible on Desktops.

(which wont matter if it will be the same PR or another)

@Nirbhay97
Copy link
Contributor Author

Yes, I tested my code and it worked well,

  1. It will automatically resume previous set quality when on charging
  2. If battery goes below certain level, it will automatically start decreasing player quality

Yes, it makes more sense to hide this feat when using on desktop, which is currently not handled.

@ImprovedTube ImprovedTube merged commit 386e4b0 into code-charity:master May 21, 2024
ImprovedTube added a commit that referenced this pull request May 21, 2024
ImprovedTube added a commit that referenced this pull request May 21, 2024
@ImprovedTube
Copy link
Member

hi! @Nirbhay97
I commented out the toggles for now. As my continuation of your code might not be finished.
Looking forward!


It will automatically resume previous set quality when on charging

if it was set with our quality feature,

  • but not if it was set with youtube's player.
    compare:
    if (this.focus) {
    if (ImprovedTube.qualityBeforeBlur) {
    ImprovedTube.playerQuality(ImprovedTube.qualityBeforeBlur);
    }
    } else {
    if (!ImprovedTube.elements.video.paused) {
    ImprovedTube.qualityBeforeBlur = player.getPlaybackQuality();
    ImprovedTube.playerQuality(qualityWithoutFocus);

@raszpl
Copy link
Contributor

raszpl commented Jun 12, 2024

qualityWhenRunningOnBattery: {
whenBatteryIslowDecreaseQuality: {
pauseWhileIUnplugTheCharger

inconsistent Naming convention, lets stick to snake case since every other option is already that please :)

someone already planned that, because:

document.getElementById('quality_when_low_battery').dispatchEvent(new CustomEvent('render'));

but didnt finish, didnt add id and forgot to comment it :)
no quality_when_low_battery ID = Codec h254 modal doesnt close on OK :

player.js:920 Uncaught 
TypeError: Cannot read properties of null (reading 'dispatchEvent')
    at refresh (player.js:920:58)
    at Object.ok (player.js:934:9)
    at HTMLButtonElement.click (satus.js:1234:36)

#2377

@ImprovedTube
Copy link
Member

Sorry my additions & tuned numbers delayed the release of the feature yet.

Greetings @Nirbhay97

@ImprovedTube ImprovedTube added Open! Not to forget. Unfinished (or waiting for a requirement) (unless combined with label: riddle) and removed Open! Not to forget. Unfinished (or waiting for a requirement) (unless combined with label: riddle) labels Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Open! Not to forget. Unfinished (or waiting for a requirement) (unless combined with label: riddle)
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

None yet

3 participants