I see high CPU usage when mjpg-streamer is running

You may observe a high CPU usage on your OctoPrint host when mjpg-streamer is running. The diagnostic and the solution depend on the host type and on the webcam type.

The first step to solve this is to find out whether your webcam can directly output a compressed MJPEG stream, or if it can only output an uncompressed YUYV stream, by running the v4l2-ctl --list-formats command.

Here is an example of a Logitech webcam that can output a compressed MJPEG stream:

$ v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
	Index       : 0
	Type        : Video Capture
	Pixel Format: 'YUYV'
	Name        : YUV 4:2:2 (YUYV)

	Index       : 1
	Type        : Video Capture
	Pixel Format: 'MJPG' (compressed)
	Name        : MJPEG

If your camera can output a compressed MJPEG stream then you should not have any problems with high CPU usage when running mjpg-streamer, independently of the resolution chosen for the video stream.

However, most cheap USB webcams can only output an uncompressed YUYV stream, requiring mjpg-streamer to compress the video stream in software. On a Raspberry Pi, at a resolution of 640x480 this will result in a CPU usage of 60~80%. The solution is to step down the resolution to 320x240, resulting in a more reasonable CPU usage of 15~20%.

Halving the resolution results in a 4x reduction in CPU usage!

There is another caveat to using a Raspberry Pi as an OctoPrint host with mjpg-streamer, because it shares its USB bandwidth between all the USB peripherals and the Ethernet interface. When using mjpg-streamer at high resolutions, it is possible that the USB bandwidth available for the 3D printer serial interface is not sufficient for some prints. Again, the solution is to step down the video resolution.

And finally one last note about the Raspicam: it is capable of high resolution video streaming and the video compression is handled in principle by the VideoMax core in the Broadcom SOC (in other words, low CPU usage independently of the chosen resolution). But the caveat noted above still applies, with the same solution.

1 Like