matanui159 / ReplaySorcery

An open-source, instant-replay solution for Linux
GNU General Public License v3.0
854 stars 27 forks source link

High pulseaudio CPU usage #94

Closed melvyn2 closed 3 years ago

melvyn2 commented 3 years ago

Describe the bug Whenever ReplaySorcery is set to use pulseadio input, pulseaudio uses around 110% CPU constantly. I thought that it might be resampling, but pulseaudio is set to 48kHz, and changing ReplaySorcery's sample rate to 48k did not fix it.

Desktop:

Configuration: replay-sorcery.conf.txt

Logs: rs.log

matanui159 commented 3 years ago

This has been reported in the past. Looking around the internet there's quite alot of reports floating about saying PulseAudio has a high CPU usage. Apparently it can be fixed with configuration but I'm not much of a PulseAudio expert to know how to do that.

parkerlreed commented 3 years ago

Seeing this here as well. Thing is it's not pulseaudio in general. 100% CPU usage is not normal under any other circumsatnces.

Something with replay-sourcery is making it spike and stay at 100%

image

If I record the same monitor with parec

$ parec -d alsa_output.pci-0000_00_1f.3.analog-stereo.monitor | twolame -r - out.mp3

It barely even registers at 0.7%

image

matanui159 commented 3 years ago

what does pactl list sources show?

parkerlreed commented 3 years ago

Different setup with same issue

Connecting to Pulse Audio device: alsa_output.usb-Creative_Technology_Ltd._Sound_Blaster_Extigy-00.analog-stereo.monitor...
Source #0
        State: SUSPENDED
        Name: alsa_output.usb-Creative_Technology_Ltd._Sound_Blaster_Extigy-00.analog-stereo.monitor
        Description: Monitor of SoundBlaster Extigy Analog Stereo
        Driver: module-alsa-card.c
        Sample Specification: s16le 2ch 48000Hz
        Channel Map: front-left,front-right
        Owner Module: 7
        Mute: no
        Volume: front-left: 65536 / 100% / 0.00 dB,   front-right: 65536 / 100% / 0.00 dB
                balance 0.00
        Base Volume: 65536 / 100% / 0.00 dB
        Monitor of Sink: alsa_output.usb-Creative_Technology_Ltd._Sound_Blaster_Extigy-00.analog-stereo
        Latency: 0 usec, configured 0 usec
        Flags: DECIBEL_VOLUME LATENCY 
        Properties:
                device.description = "Monitor of SoundBlaster Extigy Analog Stereo"
                device.class = "monitor"
                alsa.card = "3"
                alsa.card_name = "Sound Blaster Extigy"
                alsa.long_card_name = "Creative Technology Ltd. Sound Blaster Extigy at usb-0000:00:14.0-8, full speed"
                alsa.driver_name = "snd_usb_audio"
                device.bus_path = "pci-0000:00:14.0-usb-0:8:1.0"
                sysfs.path = "/devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8:1.0/sound/card3"
                udev.id = "usb-Creative_Technology_Ltd._Sound_Blaster_Extigy-00"
                device.bus = "usb"
                device.vendor.id = "041e"
                device.vendor.name = "Creative Technology, Ltd"
                device.product.id = "3000"
                device.product.name = "SoundBlaster Extigy"
                device.serial = "Creative_Technology_Ltd._Sound_Blaster_Extigy"
                device.string = "3"
                module-udev-detect.discovered = "1"
                device.icon_name = "audio-card-usb"
        Formats:
                pcm

Source #1
        State: SUSPENDED
        Name: alsa_input.usb-Creative_Technology_Ltd._Sound_Blaster_Extigy-00.analog-stereo
        Description: SoundBlaster Extigy Analog Stereo
        Driver: module-alsa-card.c
        Sample Specification: s16le 2ch 48000Hz
        Channel Map: front-left,front-right
        Owner Module: 7
        Mute: no
        Volume: front-left: 24099 /  37% / -26.07 dB,   front-right: 24099 /  37% / -26.07 dB
                balance 0.00
        Base Volume: 65536 / 100% / 0.00 dB
        Monitor of Sink: n/a
        Latency: 0 usec, configured 0 usec
        Flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY 
        Properties:
                alsa.resolution_bits = "16"
                device.api = "alsa"
                device.class = "sound"
                alsa.class = "generic"
                alsa.subclass = "generic-mix"
                alsa.name = "USB Audio"
                alsa.id = "USB Audio"
                alsa.subdevice = "0"
                alsa.subdevice_name = "subdevice #0"
                alsa.device = "0"
                alsa.card = "3"
                alsa.card_name = "Sound Blaster Extigy"
                alsa.long_card_name = "Creative Technology Ltd. Sound Blaster Extigy at usb-0000:00:14.0-8, full speed"
                alsa.driver_name = "snd_usb_audio"
                device.bus_path = "pci-0000:00:14.0-usb-0:8:1.0"
                sysfs.path = "/devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8:1.0/sound/card3"
                udev.id = "usb-Creative_Technology_Ltd._Sound_Blaster_Extigy-00"
                device.bus = "usb"
                device.vendor.id = "041e"
                device.vendor.name = "Creative Technology, Ltd"
                device.product.id = "3000"
                device.product.name = "SoundBlaster Extigy"
                device.serial = "Creative_Technology_Ltd._Sound_Blaster_Extigy"
                device.string = "front:3"
                device.buffering.buffer_size = "384000"
                device.buffering.fragment_size = "192000"
                device.access_mode = "mmap+timer"
                device.profile.name = "analog-stereo"
                device.profile.description = "Analog Stereo"
                device.description = "SoundBlaster Extigy Analog Stereo"
                module-udev-detect.discovered = "1"
                device.icon_name = "audio-card-usb"
        Ports:
                analog-input-mic: Microphone (type: Mic, priority: 8700, availability unknown)
                analog-input-linein: Line In (type: Line, priority: 8100, availability unknown)
        Active Port: analog-input-mic
        Formats:
                pcm
matanui159 commented 3 years ago

Maybe the issue could be related to the differences between PulseAudio (2ch s16le) and my recorder (1ch float). Maybe if I move that conversion out of PulseAudio and into FFmpeg it might help

parkerlreed commented 3 years ago

I can confirm with testing ffmpeg with pulse, that the CPU usage is much more in line (pulse doesn't even become a concern)

-f pulse -i <name.montior>

parkerlreed commented 3 years ago

For reference my full current command line for continuous recording

ffmpeg -y -f pulse -i default  -device /dev/dri/card0 -framerate 60 -f kmsgrab -i - -vaapi_device /dev/dri/renderD128 -vf 'hwmap=derive_device=vaapi,crop=1920:1080:0:0,scale_vaapi=1920:1080:nv12' -c:v h264_vaapi -c:a aac output.mkv
matanui159 commented 3 years ago

I have another theory, can you check if you still have high CPU usage if you set audioDevice = system or explicitly set it to a specific device name?

melvyn2 commented 3 years ago

I have another theory, can you check if you still have high CPU usage if you set audioDevice = system or explicitly set it to a specific device name?

Setting the device to system fixes the issue, and now pulseaudio is back to normal and replaysorcery only uses ~50% cpu (same as no audio).

matanui159 commented 3 years ago

I've pushed up a potential fix to master, can you test if it works (when using audioDevice = auto)?

kira-bruneau commented 3 years ago

If it helps, I've verified that https://github.com/matanui159/ReplaySorcery/commit/541bccc9487c0677091029f4e9c66ef921a428b3 fixes the issue for me :slightly_smiling_face:. See https://github.com/NixOS/nixpkgs/pull/123045#issuecomment-841658990.