WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: ffmpeg rtmp stream of alsa / application sound  (Read 4432 times)

Offline onelife

  • Full Member
  • ***
  • Posts: 141
ffmpeg rtmp stream of alsa / application sound
« on: February 12, 2018, 12:40:21 AM »
Hi there Forum,

I'm hoping someone might have had some experience with this but maybe not posted here in the forum as I can't find much.

In short, I was trying to follow a setup guide that would create a loopback device of alsa similar to this :

Load the snd_aloop module:

Code: [Select]
modprobe snd-aloop pcm_substreams=1
Set the default ALSA audio output to one substream of the Loopback device in your .asoundrc (or /etc/asound.conf)

Code: [Select]
# .asoundrc
pcm.!default { type plug slave.pcm "hw:Loopback,0,0" }

You can now record audio from a running application using:

Code: [Select]
ffmpeg -f alsa -ac 2 -ar 44100 -i hw:Loopback,1,0 out.wav
But in trying to add the /etc/asound.conf file it causes alsa to stop working all together.

I'm trying to run a command in the end that would take the already playing sound from alsa (an application) and use ffmpeg to then send that stream to a RTMP server that would offer the feed as a streaming audio service.

Any ideas would be super welcome :)

Thank you!


Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14516
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #1 on: February 12, 2018, 02:31:34 AM »
Would it be easier to do it with pulseaudio?

Offline onelife

  • Full Member
  • ***
  • Posts: 141
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #2 on: February 13, 2018, 12:55:32 AM »
Hummm, I'm not sure about Pulse - I've never used Pulse and our application works well with Alsa at the moment as it also uses an audio daemon similar to mpd. So could be difficult to move over to pulse  :(

Anyone got any ideas ?

Offline onelife

  • Full Member
  • ***
  • Posts: 141
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #3 on: February 14, 2018, 03:16:41 AM »
Anyone have any ideas why this :

https://trac.ffmpeg.org/wiki/Capture/ALSA

The part about capturing from an application doesn't work on Pi Core? :

Record audio from an application
Load the snd_aloop module:

modprobe snd-aloop pcm_substreams=1
Set the default ALSA audio output to one substream of the Loopback device in your .asoundrc (or /etc/asound.conf)

# .asoundrc
pcm.!default { type plug slave.pcm "hw:Loopback,0,0" }
You can now record audio from a running application using:

ffmpeg -f alsa -ac 2 -ar 44100 -i hw:Loopback,1,0 out.wav

Really need to create a basic /etc/asound.conf file that will allow me to use ffmpeg.

Hope someone has any idea :)

Thanks

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14516
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #4 on: February 14, 2018, 03:53:47 AM »
tinycore extensions should be configured to use /usr/local/etc and not /etc - did you try ~/.asoundrc?

Offline onelife

  • Full Member
  • ***
  • Posts: 141
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #5 on: February 14, 2018, 04:24:52 AM »
Sadly no luck, even with the .asoundrc in the folder /home/snb/ it doesn't seem to work.

Strangely in the file :

/usr/local/share/alsa/alsa.conf

It points to use /etc ?

Code: [Select]
snb@snb:~$ cat /usr/local/share/alsa/alsa.conf
#
#  ALSA library configuration file
#

# pre-load the configuration files

@hooks [
{
func load
files [
{
@func concat
strings [
{ @func datadir }
"/alsa.conf.d/"
]
}
"/etc/asound.conf"
"~/.asoundrc"
]
errors false
}
]

# load card-specific configuration files (on request)

cards.@hooks [

Really seems weird and in theory should be a simple setup :( - I've also tried the route of sending our application audio to /dev/stdout and that I can do, but then ffmpeg can't pipe it in.

ANY help from anyone please :)

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14516
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #6 on: February 14, 2018, 05:32:18 AM »
alsa was definately compiled to use /usr/local/etc/alsa in x86/x86_64 and alsa.conf still points to /etc/asound.conf

It doesn't help your case, but I'll change it in x86_64 and test what happens.

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14516
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #7 on: February 15, 2018, 12:57:07 AM »
I gave this a go in corepure64 - I can record from my laptop built-in microphone (after first un-muting it) like this:
Code: [Select]
ffmpeg -f alsa -i hw:1 -t 30 out.wav
..but if i modify .asoundrc and load the loopback module as you mention above, I do not get any errors, but nothing is recorded.

You could ask for help on the alsa mailing list(s)?
« Last Edit: February 15, 2018, 03:06:54 AM by Juanito »

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14516
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #8 on: February 15, 2018, 03:01:49 AM »
Got it  :)

If I set xmms to alsa playback via hw:2,0 - I can then record what xmms is playing with:
Code: [Select]
$ ffmpeg -f alsa -ac 2 -ar 44100 -i hw:2,1 out.wav
Where:
Code: [Select]
$ aplay -l | grep Loopback
card 2: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
card 2: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]

This is with a laptop running corepure64 - note also that, for some reason, hdmi sound always comes up as the alsa default and sound will not work unless I first load graphics-KERNEL.

I haven't played with sound much on the RPi, but as it also has hdmi there might be a similar issue?

Edit: This works on an RPi3 with picore 9.0.3:

Make some noise with:
Code: [Select]
$ speaker-test -Ddefault:1 -c2 -t wav -l1
Record the noise with:
Code: [Select]
$ ffmpeg -f alsa -ac 2 -ar 44100 -i hw:1,1 out.wav
Where:
Code: [Select]
$ aplay -l | grep Loopback
card 1: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
card 1: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]

Note that no ~/.asoundrc or graphics-KERNEL was required.
« Last Edit: February 15, 2018, 03:58:23 AM by Juanito »

Offline onelife

  • Full Member
  • ***
  • Posts: 141
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #9 on: February 15, 2018, 08:21:02 AM »
AWESOME to hear you made some progress :) Thank you!

However, sorry - I'm not a guru at all. When you say you set xmms to use hw:2,0 what would this be?

xmms is you application I assume and the hw:2,0? How would I tell my application to use that hw or do I set that in Alsa config?

Sorry, maybe I missed a step, but something not right this side.

Thank you again!

Offline Juanito

  • Administrator
  • Hero Member
  • *****
  • Posts: 14516
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #10 on: February 15, 2018, 09:56:24 PM »
I was using xmms to test on corepure64 and to check the file out.wav produced on an RPi3 using picore.

Did you try the commands listed after "Edit:" in the post above (these worked on the RPi3)?

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #11 on: August 02, 2022, 11:39:35 AM »
I hope it's okay to resurrect this old thread. I'm having the exact same issue as OP: I'm trying to use  ffmpeg  to record my screen and system sound (not microphone), but the resulting recordings have no sound. I'd prefer to do it using  alsa  but am open to a  pulseaudio
 solution. 

I'm on TCL13 x86_64. Alsa is present and audio works well (all applications produce sound). This machine is my daily driver.

Here's what I've tried:

Code: [Select]
$ tce-load -wi libsamplerate
$ sudo modprobe snd-aloop
$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC269VC Analog [ALC269VC Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 1: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
$ ffmpeg -f alsa -ac 2 -sample_rate 44100 -i hw:1,0 -f x11grab -r 29.97 -s 1366x768 -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -threads 0 result-1-0.mkv
$ ffmpeg -f alsa -ac 2 -sample_rate 44100 -i hw:1,1 -f x11grab -r 29.97 -s 1366x768 -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -threads 0 result-1-1.mkv

I reboot before playing back the two mkv files. Neither one has sound. Any ideas how to make it work?

« Last Edit: August 02, 2022, 11:52:31 AM by GNUser »

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #12 on: August 02, 2022, 11:56:55 AM »
By the way, this solution works in my Devuan partition but not in TCL:
https://www.maketecheasier.com/screencast-from-command-line-linux

The reason seems to be that in TCL, pulseaudio does not create a "pulse" device in alsa when it starts, so the italicized part of the ffmpeg command causes it to fail:
Code: [Select]
ffmpeg -f alsa -ac 2 -i [i]pulse[/i] -f x11grab -r 30 -s 1366x768 -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -threads 0 output.mkv
I can get the job done in Devuan but I prefer to do everything in my favorite distro, which is why I am interested in solving this ;)
« Last Edit: August 02, 2022, 12:18:42 PM by GNUser »

Offline GNUser

  • Hero Member
  • *****
  • Posts: 1343
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #13 on: August 02, 2022, 01:03:35 PM »
I tried recording audio from pulse instead of alsa, like this:

Code: [Select]
$ sudo /usr/local/etc/init.d/dbus start
starting dbus daemon...
$ start-pulseaudio-x11
$ pactl list sources
Source #0
State: SUSPENDED
Name: alsa_output.pci-0000_00_1b.0.analog-stereo.monitor
Description: Monitor of Built-in Audio Analog Stereo
Driver: module-alsa-card.c
Sample Specification: s16le 2ch 44100Hz
Channel Map: front-left,front-right
Owner Module: 6
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.pci-0000_00_1b.0.analog-stereo
Latency: 0 usec, configured 0 usec
Flags: DECIBEL_VOLUME LATENCY
Properties:
device.description = "Monitor of Built-in Audio Analog Stereo"
device.class = "monitor"
alsa.card = "0"
alsa.card_name = "HDA Intel PCH"
alsa.long_card_name = "HDA Intel PCH at 0x83838000 irq 33"
alsa.driver_name = "snd_hda_intel"
device.bus_path = "pci-0000:00:1b.0"
sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card0"
device.bus = "pci"
device.vendor.id = "8086"
device.form_factor = "internal"
device.string = "0"
module-udev-detect.discovered = "1"
device.icon_name = "audio-card-pci"
Formats:
pcm

Source #1
State: SUSPENDED
Name: alsa_input.pci-0000_00_1b.0.analog-stereo
Description: Built-in Audio Analog Stereo
Driver: module-alsa-card.c
Sample Specification: s16le 2ch 44100Hz
Channel Map: front-left,front-right
Owner Module: 6
Mute: no
Volume: front-left: 10387 /  16% / -48.00 dB,   front-right: 10387 /  16% / -48.00 dB
        balance 0.00
Base Volume: 6554 /  10% / -60.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 = "ALC269VC Analog"
alsa.id = "ALC269VC Analog"
alsa.subdevice = "0"
alsa.subdevice_name = "subdevice #0"
alsa.device = "0"
alsa.card = "0"
alsa.card_name = "HDA Intel PCH"
alsa.long_card_name = "HDA Intel PCH at 0x83838000 irq 33"
alsa.driver_name = "snd_hda_intel"
device.bus_path = "pci-0000:00:1b.0"
sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card0"
device.bus = "pci"
device.vendor.id = "8086"
device.form_factor = "internal"
device.string = "front:0"
device.buffering.buffer_size = "352800"
device.buffering.fragment_size = "176400"
device.access_mode = "mmap+timer"
device.profile.name = "analog-stereo"
device.profile.description = "Analog Stereo"
device.description = "Built-in Audio Analog Stereo"
alsa.mixer_name = "Realtek ALC269VC"
alsa.components = "HDA:10ec0269,17aa21fa,00100203 HDA:80862806,80860101,00100000"
module-udev-detect.discovered = "1"
device.icon_name = "audio-card-pci"
Ports:
analog-input-internal-mic: Internal Microphone (priority: 8900)
analog-input-dock-mic: Dock Microphone (priority: 7800, not available)
analog-input-mic: Microphone (priority: 8700, not available)
Active Port: analog-input-internal-mic
Formats:
pcm
$ ffmpeg -f pulse -ac 2 -sample_rate 44100 -i alsa_output.pci-0000_00_1b.0.analog-stereo.monitor -f x11grab -r 29.97 -s 1366x768 -i :
0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -threads 0 output-pulse.mkv
But the resulting recording (output-pulse.mkv) is unusable: Video is choppy, audio and video are very out of sync.

So far I cannot figure out how to record screen + system sound in TCL, neither with alsa (attempt results in recording with no sound) nor with pulseaudio (attempt results in recording with choppy video and lack of video/audio sync) :(

« Last Edit: August 02, 2022, 01:06:53 PM by GNUser »

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 662
Re: ffmpeg rtmp stream of alsa / application sound
« Reply #14 on: August 02, 2022, 10:09:42 PM »
My suggestion is to try first just to grab the screen, with correct codec that support your cpu/gpu for encoding.

When this is correct and working fine, you could start to do the exact the same with the sound, just grab the sound and encode that with correct codec.

Then you combine the video and the sound stream together. And see if that works well.
Don't start to solve to problems together. That could also add more problems to your first problem.
 
Now a-days i have seen that all stream services uses av1 to encode the video stream.
Like netflix, facebook, youtube, twitch...and so on...

I am getting older and no longer keep up with developments, that happens around me.
This blow my mind then i realize this, and in Sweden we have some IP TV broadcast companies that now change there IP TOP Set box strategy, to just quit with mpeg and just go over to av1, throw away the old boxes and invest in new ones that support av1.
Then you have a IP SET TOP BOX with Android with WIFI6 with lots of app support.
So you can install your self many apps in your own IP SET TOP BOX.
 
Changing from mpeg4 to av1, that because it's free (Not like mpeg2/mpeg4(H264)) and I have read about it that's faster and take smaller bandwidth. But what i have read about av1 is that ours cpu/gpu don't support that codec in hardware so the cpu have to process that like software.(I have not confirmed that yet). Or ours software like ffmpeg don't support encoding/decoding av1 in hardware.

So next gen, stream players like google stream cast uses av1 to decode the stream.

Here some links to read more about it:
https://en.wikipedia.org/wiki/AV1
https://netflixtechblog.com/bringing-av1-streaming-to-netflix-members-tvs-b7fc88e42320
https://www.tomshardware.com/news/intel-av1-encoder-for-cpus

Have a Best hacking with our community open source software and codecs.