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

Very high CPU usage on desktop Chrome and Brave browsers #66

Closed
pmiklos opened this issue Feb 7, 2019 · 23 comments
Closed

Very high CPU usage on desktop Chrome and Brave browsers #66

pmiklos opened this issue Feb 7, 2019 · 23 comments

Comments

@pmiklos
Copy link

pmiklos commented Feb 7, 2019

  • Device Name: Ubuntu Linux (Dell XPS 13, Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz)
  • Device Version: Ubuntu 16.04.5 LTS ( 4.15.0-42-generic mute the video #45~16.04.1-Ubuntu SMP Mon Nov 19 13:02:27 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux)
  • Browser Name: Chrome / Brave
  • Browser Version: Chrome 72.0.3626.81 (Official Build) (64-bit) / Brave 0.58.21 Chromium: 71.0.3578.98 (Official Build)
  • NoSleep.js Version: 0.9.0

Expected Behavior

Do not use excessive CPU

Actual Behavior

The CPU usage goes up to 170-180% (inspecting in Chrome task manager) as soon as the NoSleep enable() function is called and my laptop starts overheating.

I doesn't seem to happen with Firefox and I also tested on Android 9 (Pixel 3) which didn't seem to overheat or use much cpu.

I understand that the primary target of this library is mobile devices, but it still shouldn't behave as described above on desktop.

Code

I used the live example page

How Do We Reproduce?

Go to the live demo page and enable wake lock, press Shift+Esc and see the CPU usage shooting up.

@pmiklos
Copy link
Author

pmiklos commented Feb 7, 2019

It has something to do with the webm file. If I disable that from the video sources, mp4 starts playing and that one uses significantly less cpu (jumping around 10%-20%) .
I wonder why the mp4 is implemented in way to constantly update the video position rather than looping. Looping seems to use less resources.

@pmiklos
Copy link
Author

pmiklos commented Feb 7, 2019

generated a new webm video as below that consumes only 1-3% CPU. It's slightly bigger in size though:

 ffmpeg -r 20 -t 1 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
 ffmpeg -i noop.mpeg -c:v libvpx -an noop.webm

@pmiklos
Copy link
Author

pmiklos commented Feb 8, 2019

found a better way, generate a 5sec 1fps webm which doesn't even spike the CPU:

ffmpeg -r 20 -t 4 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
ffmpeg -i noop.mpeg -c:v libvpx -an -r 1 noop.webm

The only thing is that the code has to be changed to check the currently playing source when testing for the media to loop

if (this.noSleepVideo.currentSrc.includes('video/webm')) {
     this.noSleepVideo.setAttribute('loop', '')
}

@LeandroJF
Copy link

Hey, can you post here the new generated video files? You can use this tool to convert them to data URI:
https://dopiaza.org/tools/datauri/index.php

Tks in advance

@pmiklos
Copy link
Author

pmiklos commented Feb 11, 2019

You can find it here. It required another change in index.js to detect the video (since it's not 1sec anymore). Also I only tested it with Brave and Chrome browsers on desktop and with Android Brave on Pixel 3.

pmiklos@963a9e8

@LeandroJF
Copy link

Ok, tks

@virpo
Copy link

virpo commented Feb 12, 2019

I created a pull request that will hopefully fix these CPU issues: #65

@viktorvi
Copy link

I have same problem with Chrome.

@pedromarquesm
Copy link

just found out this is the problem for my computer to always having it's fans at max.
CPU usage from Chrome task manager pointed out 170
After disabling NoSleep, way below 20...
any fix by now?

@hugovk
Copy link

hugovk commented Apr 18, 2019

@pedromarquesm Use NoSleep.js from #65.

@pedromarquesm
Copy link

last commit on 5 december of 2018. is this repo already dead?
There are improvements not being accepted...

@garygrubb
Copy link

found a better way, generate a 5sec 1fps webm which doesn't even spike the CPU:

ffmpeg -r 20 -t 4 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
ffmpeg -i noop.mpeg -c:v libvpx -an -r 1 noop.webm

The only thing is that the code has to be changed to check the currently playing source when testing for the media to loop

if (this.noSleepVideo.currentSrc.includes('video/webm')) {
     this.noSleepVideo.setAttribute('loop', '')
}

Thank you. This really makes a difference. It reduced by CPU utilisation from 50% to almost idle.

@a22aaass
Copy link

found a better way, generate a 5sec 1fps webm which doesn't even spike the CPU:

ffmpeg -r 20 -t 4 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
ffmpeg -i noop.mpeg -c:v libvpx -an -r 1 noop.webm

The only thing is that the code has to be changed to check the currently playing source when testing for the media to loop

if (this.noSleepVideo.currentSrc.includes('video/webm')) {
     this.noSleepVideo.setAttribute('loop', '')
}

Thank you. This really makes a difference. It reduced by CPU utilisation from 50% to almost idle.

how can i use it?
"ffmpeg -r 20 -t 4 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
ffmpeg -i noop.mpeg -c:v libvpx -an -r 1 noop.webm"

@garygrubb
Copy link

garygrubb commented Apr 24, 2019

found a better way, generate a 5sec 1fps webm which doesn't even spike the CPU:

ffmpeg -r 20 -t 4 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
ffmpeg -i noop.mpeg -c:v libvpx -an -r 1 noop.webm

The only thing is that the code has to be changed to check the currently playing source when testing for the media to loop

if (this.noSleepVideo.currentSrc.includes('video/webm')) {
     this.noSleepVideo.setAttribute('loop', '')
}

Thank you. This really makes a difference. It reduced by CPU utilisation from 50% to almost idle.

how can i use it?
"ffmpeg -r 20 -t 4 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
ffmpeg -i noop.mpeg -c:v libvpx -an -r 1 noop.webm"

You need to run the ffmpeg commands on a Linux machine where the tool is available to generate the template video files. Use thee videos in NoSleep.js instead of importing the strings which are present by default.

This is my code in a react project.

// const webm= 'data:video/webm;base64,G...3AA/vz0AAA=';
// const mp4= 'data:video/mp4;base64,AAAAIG...DA=';

import webm from './webm.webm'
import mp4 from './mp4.mpeg'

Also don't forget to update the if statement as described in previous post.

@a22aaass
Copy link

found a better way, generate a 5sec 1fps webm which doesn't even spike the CPU:

ffmpeg -r 20 -t 4 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
ffmpeg -i noop.mpeg -c:v libvpx -an -r 1 noop.webm

The only thing is that the code has to be changed to check the currently playing source when testing for the media to loop

if (this.noSleepVideo.currentSrc.includes('video/webm')) {
     this.noSleepVideo.setAttribute('loop', '')
}

Thank you. This really makes a difference. It reduced by CPU utilisation from 50% to almost idle.

how can i use it?
"ffmpeg -r 20 -t 4 -s 1x1 -f rawvideo -pix_fmt rgb24 -i /dev/zero noop.mpeg
ffmpeg -i noop.mpeg -c:v libvpx -an -r 1 noop.webm"

You need to run the ffmpeg commands on a Linux machine where the tool is available to generate the template video files. Use thee videos in NoSleep.js instead of importing the strings which are present by default.

This is my code in a react project.

// const webm= 'data:video/webm;base64,G...3AA/vz0AAA=';
// const mp4= 'data:video/mp4;base64,AAAAIG...DA=';

import webm from './webm.webm'
import mp4 from './mp4.mpeg'

Also don't forget to update the if statement as described in previous post.

yep,i get it
tks!!!! I hope we shall be good friends

@jmcbee
Copy link

jmcbee commented Sep 12, 2019

Man, I thought I was going crazy. I've been trying to profile my reactjs app trying to find the problem. I didn't think it would be caused by this lib.

@pedrommarquesm
Copy link

pedrommarquesm commented Sep 12, 2019

Man, I thought I was going crazy. I've been trying to profile my reactjs app trying to find the problem. I didn't think it would be caused by this lib.

well, don't even try to profile it on development mode :) I was getting almost 1gb of memory use (well because of development mode)
of course a normal memory use, in production mode is about 20-50mb.

I also had a similar problem with a particular version of typescript. the filewatcher was very CPU intensive. eventually they solved it

@timtribers
Copy link

Can't wait to use the latest version of this lib (thx for all the hard work recently @richtr), but unfortunately I'm stuck on v0.8 - if I move to any later release I get this problem of maxed out CPU in Chrome (on Windows). I take it that the video file changed after that release. Does it need something like PR#65 to fix this? (Will post this there too)

@mhmo91
Copy link

mhmo91 commented Jul 10, 2020

Man, I thought I was going crazy. I've been trying to profile my reactjs app trying to find the problem. I didn't think it would be caused by this lib.

Same here!
there should be a Warning somewhere in the library readMe. i wasted sooooo many hours because of this!!!

@richtr
Copy link
Owner

richtr commented Dec 16, 2020

The video files suggested in #65 have now been merged to master (via 2ebb901).

This should reduce CPU usage based on the discussions from #65.

I will release a new version of this library shortly with these fixes included.

@richtr richtr closed this as completed Dec 16, 2020
@MasterInQuestion
Copy link

MasterInQuestion commented Apr 16, 2023

    [ Quote pmiklos @ CE 2019-02-07 05:55:53 UTC:
https://github.com/richtr/NoSleep.js/issues/66#issuecomment-461297387
    I wonder why the mp4 is implemented in way to constantly update the video position rather than looping. Looping seems to use less resources. ]
<^>    Related commits:
    https://github.com/richtr/NoSleep.js/pull/32/files#diff-89779e3bb9293db4f0888f94691e5e313edfe27a2a234cf037e866f9f197abc8R27-R31
    https://github.com/richtr/NoSleep.js/commit/016580ddd200ff6e7a12f0904c314e3ee2c9bae1#diff-bfe9874d239014961b1ae4e89875a6155667db834a410aaaa2ebe3cf89820556R25

    Also note: theoretically updating the playback position (when done properly) should cause less load than looping the whole video.
    NoSleep.js failed to do so by listening on the wrong event ("timeupdate") and doing pointless operations (`if ( currentTime > 0.5 )` and `Math.random()`).

@a22aaass
Copy link

a22aaass commented Apr 16, 2023 via email

@MasterInQuestion
Copy link

MasterInQuestion commented Apr 18, 2023

@a22aaass,
    Initially I thought your reply was an AI generated test [1], my apologies.
    After more careful checking I noted your reply might be of actual meaning.
    .
    So I would like to inform you that I'm recently analyzing the optimal way to implement the WakeLock.
    For more details, check the linked posts. (or my recent activities)

[1] Due to the extremely close timestamp to my post, and some low-effort background research that happened to have failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests