Broken pipe mystery
I am using ALSA 0.9.0rc7 and Red Hat Linux 7.2. My sound card is a CS461x. My computer is a reasonably powerful beast with 512M of RAM and 1Gig clock speed.
I have written a simple capture program which reads interleaved samples from the sound card and goes around in a continuous while(1) loop. My program works perfectly - until it crashes after several hours with the message:
"read from audio interface failed: broken pipe".
I have narrowed it down to the following piece of code. The size of buf is 256 (shorts). Why would the snd_pcm_readi suddenly (randomly?) fail with a broken pipe??? Any ideas or suggestions would be greatly appreciated as I'm struggling with this annoying (and difficult to debug) problem.
Regards,
Pete
while (1)
{
if ((err = snd_pcm_readi(capture_handle,buf,128)) != 128
{
// this is where it fails after several hours of working
successfully
cout << "read from audio interface failed: " << snd_strerror(err) <<
endl;
exit(1);
}
}Quoted 3 lines · Pete Barnard
I have written a simple capture program which reads interleaved samples from the sound card and goes around in a continuous while(1) loop. My program works perfectly - until it crashes after several hours with the message:
Does it always crash after the same time?
What sampling frequency do you use (kHz) ?
Best regards,
--
Tomek
-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
Quoted 2 lines
Does it always crash after the same time?
This is something I was actually thinking about. I'm not sure at this stage but it is certainly a something worth exploring. I have never been there when the program crashes because everytime I run the program all night it has always crashed when I get back in the morning - and I'd always forgotten to put a system("date") line in the code before I went home. I won't be able to try it it until Monday but I'll definitely put a system("date") command in the code and get back to you when I can answer your question.
Quoted 1 line
What sampling frequency do you use (kHz) ?
16kHz is the sampling frequency.
Thank you replying.
Regards,
Pete
Quoted 9 lines
-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
Alsa-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel
-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
Quoted 17 lines · Pete Barnard
I am using ALSA 0.9.0rc7 and Red Hat Linux 7.2. My sound card is a CS461x. My computer is a reasonably powerful beast with 512M of RAM and 1Gig clock speed.
I have written a simple capture program which reads interleaved samples from the sound card and goes around in a continuous while(1) loop. My program works perfectly - until it crashes after several hours with the message:"read from audio interface failed: broken pipe".
I have narrowed it down to the following piece of code. The size of buf is 256 (shorts). Why would the
snd_pcm_readisuddenly (randomly?) fail with a broken pipe??? Any ideas or suggestions would be greatly appreciated as I'm struggling with this annoying (and difficult to debug) problem.Regards,
Pete
while (1)
Quoted 11 lines
{
if ((err = snd_pcm_readi(capture_handle,buf,128)) != 128
{
// this is where it fails after several hours of working
successfully
cout << "read from audio interface failed: " << snd_strerror(err) <<
endl;
exit(1);
}
}If I remember correctly the EPIPE error code is returned when an xrun occurs. In the case of a read it would be a buffer overrun. Your code should check for this return value and I believe you have to start the stream again with "snd_pcm_prepare". Check out the ALSA site for HOWTOs and the like. Cheers.
Josh Green
-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
Quoted 3 lines
Quoted 1 line
What sampling frequency do you use (kHz) ?
16kHz is the sampling frequency.
Please note what period and boundary size are you using. 16000*60*60*24 = 1382400000, this should be still OK, unless you are counting bytes, not frames. I suspect some problems when 32 bit counter wraps around (changes from positive to negative), but this should probably take longer in your case.
Anyway if it always takes the same time you should be able to calculate what your magic number is. If not, then something else.
Best regards,
--
Tomek
-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
:
Quoted 2 lines
Does it always crash after the same time?
No. It didin't crash after the same time. The problem is fixed now thanks
to Josh Green's tip. (I wrote a function that caught the EPIPE error and
then did a snd_pcm_prepare). I left it for 2 nights with a system("date") in the catch function and got an EPIPE once on one night and not at all on the second night - so even though it is fixed I'm still slightly curious as to why I got this problem/feature at all.
Regards,
Pete
Quoted 7 lines
What sampling frequency do you use (kHz) ?
Best regards,
--
Tomek
-------------------------------------------------------
This SF.NET email is sponsored by: FREE SSL Guide from Thawte are you planning your Web Server Security? Click here to get a FREE Thawte SSL guide and find the answers to all your SSL security issues.
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en
Quoted 11 lines · Pete Barnard
:
Quoted 2 lines
Does it always crash after the same time?
No. It didin't crash after the same time. The problem is fixed now thanks
to Josh Green's tip. (I wrote a function that caught the EPIPE error and
then did asnd_pcm_prepare). I left it for 2 nights with asystem("date")in the catch function and got an EPIPE once on one night and not at all on the second night - so even though it is fixed I'm still slightly curious as to why I got this problem/feature at all.
Quoted 4 lines
Regards,
Pete
Its due to your program not being able to meet the deadline for writing an audio buffer for playback. This is often Linux kernel related and there is lots of information about tuning your kernel (do a search for "linux kernel low latency"). It can be somewhat of a black art, because different hardware and Linux drivers can have adverse effects on latency. For instance if your hard disk is IDE and isn't tuned right (DMA not enabled for instance) you can get real long latency spikes when reading/writing the hard disk. You'll also want to look into the lowlat patch and perhaps the preempt patch as well. Kernels that come with Linux distributions have a tendency to not be very low latency friendly, so if you wan't low latency, build your own kernel with one of those patches.
On your program side:
You can increase the audio buffer size and/or count to minimize underruns (at the cost of higher latency audio playback).
If you require low latency, run your program with SCHED_FIFO scheduling (see man sched_setscheduler, requires root privileges). Note that your program then has the ability to lock up the machine, should it decide to consume all the CPU time.
I think this is one of the main things that still needs to be solved with Linux and audio. Low latency audio shouldn't entail buggy processes being able to bring the system to its knees. Cheers.
Josh Green
-------------------------------------------------------
This SF.NET email is sponsored by: FREE SSL Guide from Thawte are you planning your Web Server Security? Click here to get a FREE Thawte SSL guide and find the answers to all your SSL security issues.
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en