[alsa-devel] non-blocking mode example
Hi,
could anyone please link me to or show me a small demo example, like "pcm_min.c" with working snd_pcm_open in SND_PCM_NONBLOCK - mode? Standard I/O transfers or Event waiting routines. I get both working - but only in blocking mode.
I want to stop/pause with commands inside a tcp-server.
Regards Thomas
Thanks Clemens,
it is really not a small example and an uneven path to go through the whole files... But may be you or someone else could answer my short questions? I have read a file to buffer with "allframes".
1.) Do I use snd_pcm_writei(...) with a size of "allframes" or loop it with smaller packages (periodsize)
2.) What are the actual functions to get the interrupt and send the frames needed into the ringbuffer (standard I/O or select)?
If you start with ALSA you will find enough pieces of code. But it is not very clear, because many pieces of cake are done with much elder APIs from ALSA...
Best regards Thomas
Am 12.08.2017 um 18:30 schrieb Clemens Ladisch:
ThomasJF wrote:
could anyone please link me to or show me a small demo example, like "pcm_min.c" with working snd_pcm_open in SND_PCM_NONBLOCK - mode?
alsa-utils/alsaloop, but it's not quite small.
Regards, Clemens _______________________________________________ Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
ThomasJF wrote:
I have read a file to buffer with "allframes".
1.) Do I use snd_pcm_writei(...) with a size of "allframes" or loop it with smaller packages (periodsize)
In blocking mode, the call would be able to write all bytes, eventually. But in non-blocking mode, it is never possible to write more than the buffer size immediately. So if you try with more frames, the function will write as much as it can, and you have to retry with the remaining frames later anyway.
2.) What are the actual functions to get the interrupt and send the frames needed into the ringbuffer (standard I/O or select)?
The snd_pcm_writei() call sends the frames to the buffer. The notification that the device is ready (i.e., that there is enough free space in the buffer to write more samples) comes through any of the file-handle-waiting functions (select, poll, epoll).
Regards, Clemens
Thanks again Clemens,
I did it with success (moving the pointer with arithmetic forward after each periodsize in the big buffer). Now I have a do-while loop with select. Now a (I hope ...) last question:
To get the tcp-commands for stop/pause:
1.) Do I have to adapt the tcp-commands inside this loop or...
2.) Do I have to work with any poll descriptors (which?)
Best regards Thomas
Am 13.08.2017 um 19:46 schrieb Clemens Ladisch:
ThomasJF wrote:
I have read a file to buffer with "allframes".
1.) Do I use snd_pcm_writei(...) with a size of "allframes" or loop it with smaller packages (periodsize)
In blocking mode, the call would be able to write all bytes, eventually. But in non-blocking mode, it is never possible to write more than the buffer size immediately. So if you try with more frames, the function will write as much as it can, and you have to retry with the remaining frames later anyway.
2.) What are the actual functions to get the interrupt and send the frames needed into the ringbuffer (standard I/O or select)?
The snd_pcm_writei() call sends the frames to the buffer. The notification that the device is ready (i.e., that there is enough free space in the buffer to write more samples) comes through any of the file-handle-waiting functions (select, poll, epoll).
Regards, Clemens _______________________________________________ Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
ThomasJF wrote:
Now I have a do-while loop with select.
Better use poll(); select() is cumbersome with the ALSA's API.
To get the tcp-commands for stop/pause:
1.) Do I have to adapt the tcp-commands inside this loop or...
2.) Do I have to work with any poll descriptors (which?)
These function allow to wait for multiple file descriptors to become ready. So wait for all the fds from the ALSA device _and_ the fd of the socket.
Regards, Clemens
Thank you again Clemens,
now it is working with poll and the two fds. Also pause, play and stop.
If it stops, the snd_pcm_wait in my loop returns a negative error code and the state changes to SND_PCM_STATE_SETUP, then it finishes and everything seems to be ok.
Anything to do here?
Best regards Thomas
Am 15.08.2017 um 14:49 schrieb Clemens Ladisch:
ThomasJF wrote:
Now I have a do-while loop with select.
Better use poll(); select() is cumbersome with the ALSA's API.
To get the tcp-commands for stop/pause:
1.) Do I have to adapt the tcp-commands inside this loop or...
2.) Do I have to work with any poll descriptors (which?)
These function allow to wait for multiple file descriptors to become ready. So wait for all the fds from the ALSA device _and_ the fd of the socket.
Regards, Clemens _______________________________________________ Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Without snd_pcm_wait() I get an open error (Device or resource busy).
In short I have inside the loop:
poll(ufds, 2, 0); snd_pcm_wait() snd_pcm_writei(...)
Regards Thomas
Am 17.08.2017 um 16:48 schrieb Clemens Ladisch:
ThomasJF wrote:
now it is working with poll and the two fds. Also pause, play and stop.
If it stops, the snd_pcm_wait in my loop returns a negative error code
Why are you calling snd_pcm_wait() when you already have poll()?
Regards, Clemens _______________________________________________ Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
If I use a higher timeout than 0 with poll, the stream starts without snd_pcm_wait().
Now I get these snd_pcm_writei errors:
stop: File descriptor in bad state pause: ok play (after pause): Resource temporarily unavailable
Regards Thomas
Am 17.08.2017 um 18:03 schrieb ThomasJF:
Without snd_pcm_wait() I get an open error (Device or resource busy).
In short I have inside the loop:
poll(ufds, 2, 0); snd_pcm_wait() snd_pcm_writei(...)
Regards Thomas
Am 17.08.2017 um 16:48 schrieb Clemens Ladisch:
ThomasJF wrote:
now it is working with poll and the two fds. Also pause, play and stop.
If it stops, the snd_pcm_wait in my loop returns a negative error code
Why are you calling snd_pcm_wait() when you already have poll()?
Regards, Clemens _______________________________________________ Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
ThomasJF wrote:
If I use a higher timeout than 0 with poll, the stream starts without snd_pcm_wait().
Now I get these snd_pcm_writei errors:
stop: File descriptor in bad state pause: ok play (after pause): Resource temporarily unavailable
I don't see the actual code. But EAGAIN is not an error.
Regards, Clemens
I could change the code (attached a short part), so I have only one problem. After stop, the stream stops and everything looks good(?), but with this message from snd_pcm_writei: -77 (-EBADFD)
Regards Thomas
Am 17.08.2017 um 19:18 schrieb Clemens Ladisch:
ThomasJF wrote:
If I use a higher timeout than 0 with poll, the stream starts without snd_pcm_wait().
Now I get these snd_pcm_writei errors:
stop: File descriptor in bad state pause: ok play (after pause): Resource temporarily unavailable
I don't see the actual code. But EAGAIN is not an error.
Regards, Clemens _______________________________________________ Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
ThomasJF wrote:
I could change the code (attached a short part)
This mailing list does not have attachments.
After stop, the stream stops and everything looks good(?), but with this message from snd_pcm_writei: -77 (-EBADFD)
You cannot write to a device in the stopped state. This is to be expected.
And you must actually use the return value of snd_pcm_poll_descriptors_count() (there are device types with more than one fd), and you must not look at the returned event flags except with snd_pcm_poll_descriptors_revents().
Regards, Clemens
Thank you Clemens,
but while the stream is running the snd_pcm_poll_descriptors_count couldn't change - or...? Then it should be inside the loop.
Regards Thomas
Am 18.08.2017 um 12:37 schrieb Clemens Ladisch:
ThomasJF wrote:
I could change the code (attached a short part)
This mailing list does not have attachments.
After stop, the stream stops and everything looks good(?), but with this message from snd_pcm_writei: -77 (-EBADFD)
You cannot write to a device in the stopped state. This is to be expected
And you must actually use the return value of snd_pcm_poll_descriptors_count() (there are device types with more than one fd), and you must not look at the returned event flags except with snd_pcm_poll_descriptors_revents().
Regards, Clemens _______________________________________________ Alsa-devel mailing list Alsa-devel@alsa-project.org http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
participants (2)
-
Clemens Ladisch
-
ThomasJF