All of lore.kernel.org
 help / color / mirror / Atom feed
* Handle large period size end-of-stream situation
@ 2013-04-03 15:07 Patrick Lai
  2013-04-03 16:49 ` Clemens Ladisch
  2013-04-04  5:35 ` Gabriel M. Beddingfield
  0 siblings, 2 replies; 7+ messages in thread
From: Patrick Lai @ 2013-04-03 15:07 UTC (permalink / raw)
  To: alsa-devel

Hi,

I am dealing with a use case requiring passing large buffer down to the
DSP. Application in the user-space passes PCM through mmap interface.
Per my understanding, ALSA is designed to work with DMA. Data is
transferred in chunk equal to period size. At the end of playback,
there may not be enough audio data left in the music stream to fill
entire period. If remaining audio data only takes up very small chunk
of period, playback takes longer to stop. Given that period size I have
to deal with is quite large, this problem is observed easily. If my
understanding is correct, what is the standard/recommended way of
handling end of stream case?

Thanks
Patrick

-- 
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Handle large period size end-of-stream situation
  2013-04-03 15:07 Handle large period size end-of-stream situation Patrick Lai
@ 2013-04-03 16:49 ` Clemens Ladisch
  2013-04-03 22:22   ` Patrick Lai
  2013-04-04  5:35 ` Gabriel M. Beddingfield
  1 sibling, 1 reply; 7+ messages in thread
From: Clemens Ladisch @ 2013-04-03 16:49 UTC (permalink / raw)
  To: Patrick Lai; +Cc: alsa-devel

Patrick Lai wrote:
> Data is transferred in chunk equal to period size.

Data is transferred in whatever chunk size the hardware uses.

The period size specifies when the hardware raises an interrupt.
If some application is blocking, this interrupt is the only mechanism
by which userspace is woken up.

> At the end of playback, there may not be enough audio data left in the
> music stream to fill entire period. If remaining audio data only takes
> up very small chunk of period, playback takes longer to stop. Given
> that period size I have to deal with is quite large, this problem is
> observed easily. If my understanding is correct, what is the standard/
> recommended way of handling end of stream case?

I guess you wouldn't ask if your hardware supported smaller periods? ;-)

Use non-blocking writes, and use some other timer to wait for the actual
data to be played.  (But if the hardware cannot report the current
playback position accurately, ALSA won't notice that the playback has
reached the end.)


Regards,
Clemens

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Handle large period size end-of-stream situation
  2013-04-03 16:49 ` Clemens Ladisch
@ 2013-04-03 22:22   ` Patrick Lai
  0 siblings, 0 replies; 7+ messages in thread
From: Patrick Lai @ 2013-04-03 22:22 UTC (permalink / raw)
  To: Clemens Ladisch; +Cc: alsa-devel

On 4/3/2013 9:49 AM, Clemens Ladisch wrote:
> Patrick Lai wrote:
>> Data is transferred in chunk equal to period size.
>
> Data is transferred in whatever chunk size the hardware uses.
>
> The period size specifies when the hardware raises an interrupt.
> If some application is blocking, this interrupt is the only mechanism
> by which userspace is woken up.
>
>> At the end of playback, there may not be enough audio data left in the
>> music stream to fill entire period. If remaining audio data only takes
>> up very small chunk of period, playback takes longer to stop. Given
>> that period size I have to deal with is quite large, this problem is
>> observed easily. If my understanding is correct, what is the standard/
>> recommended way of handling end of stream case?
>
> I guess you wouldn't ask if your hardware supported smaller periods? ;-)

The hardware can accept smaller period but smaller period would result
in more frequent interrupt to application processor. This leads to
higher power consumption.

>
> Use non-blocking writes, and use some other timer to wait for the actual
> data to be played.  (But if the hardware cannot report the current
> playback position accurately, ALSA won't notice that the playback has
> reached the end.)

My concern is not about blocking too long but audio playback ending 
longer than necessary. If I understand correctly, you are suggesting 
that application, once done transferring residual data, set up timer 
according to expected duration of residual data. Upon waking up from 
timer, call ALSA library API to get the current hardware pointer.
Is my understanding correct?

>
>
> Regards,
> Clemens
>


-- 
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Handle large period size end-of-stream situation
  2013-04-03 15:07 Handle large period size end-of-stream situation Patrick Lai
  2013-04-03 16:49 ` Clemens Ladisch
@ 2013-04-04  5:35 ` Gabriel M. Beddingfield
  2013-04-04  9:27   ` Clemens Ladisch
  1 sibling, 1 reply; 7+ messages in thread
From: Gabriel M. Beddingfield @ 2013-04-04  5:35 UTC (permalink / raw)
  To: Patrick Lai; +Cc: alsa-devel

On 04/03/2013 08:07 AM, Patrick Lai wrote:
> I am dealing with a use case requiring passing large buffer down to the
> DSP. Application in the user-space passes PCM through mmap interface.
> Per my understanding, ALSA is designed to work with DMA. Data is
> transferred in chunk equal to period size. At the end of playback,
> there may not be enough audio data left in the music stream to fill
> entire period. If remaining audio data only takes up very small chunk
> of period, playback takes longer to stop. Given that period size I have
> to deal with is quite large, this problem is observed easily. If my
> understanding is correct, what is the standard/recommended way of
> handling end of stream case?

In the typical end-of-stream case, you simply write the data that you 
have.  When the hw pointer catches up to the application pointer, you 
get an XRUN and the stream stops immediately.  There's no requirement 
that streams have to stop on period boundaries.

-gabriel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Handle large period size end-of-stream situation
  2013-04-04  5:35 ` Gabriel M. Beddingfield
@ 2013-04-04  9:27   ` Clemens Ladisch
  2013-04-04 15:09     ` Gabriel M. Beddingfield
  0 siblings, 1 reply; 7+ messages in thread
From: Clemens Ladisch @ 2013-04-04  9:27 UTC (permalink / raw)
  To: Gabriel M. Beddingfield; +Cc: Patrick Lai, alsa-devel

Gabriel M. Beddingfield wrote:
> In the typical end-of-stream case, you simply write the data that you
> have.  When the hw pointer catches up to the application pointer, you
> get an XRUN and the stream stops immediately.

But that happens only when ALSA actually reads the hardware pointer.
The application could ask for the pointer position for some reason, but
when it is blocked, only the end-of-period interrupt will cause that.


Regards,
Clemens

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Handle large period size end-of-stream situation
  2013-04-04  9:27   ` Clemens Ladisch
@ 2013-04-04 15:09     ` Gabriel M. Beddingfield
  2013-04-04 17:08       ` Clemens Ladisch
  0 siblings, 1 reply; 7+ messages in thread
From: Gabriel M. Beddingfield @ 2013-04-04 15:09 UTC (permalink / raw)
  To: Clemens Ladisch; +Cc: Patrick Lai, alsa-devel

On 04/04/2013 02:27 AM, Clemens Ladisch wrote:
> Gabriel M. Beddingfield wrote:
>> In the typical end-of-stream case, you simply write the data that you
>> have.  When the hw pointer catches up to the application pointer, you
>> get an XRUN and the stream stops immediately.
>
> But that happens only when ALSA actually reads the hardware pointer.
> The application could ask for the pointer position for some reason, but
> when it is blocked, only the end-of-period interrupt will cause that.

I stand corrected.  (Thanks also to Peter Ujfalusi for setting me straight.)

So, if the hardware can detect it then the hardware needs to signal an 
interrupt for the XRUN.

Another option... from userspace you can call SYNC_PTR (e.g. 
pcm_get_htimestamp() in tinyalsa) to update the hw and application 
pointers, then inspect them to detect when all the data is transferred. 
  When you see all is played, immediately stop the stream.  Again, this 
presumes that the "hw" side is able to query the state of the pointer.

-gabriel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Handle large period size end-of-stream situation
  2013-04-04 15:09     ` Gabriel M. Beddingfield
@ 2013-04-04 17:08       ` Clemens Ladisch
  0 siblings, 0 replies; 7+ messages in thread
From: Clemens Ladisch @ 2013-04-04 17:08 UTC (permalink / raw)
  To: Gabriel M. Beddingfield; +Cc: Patrick Lai, alsa-devel

Gabriel M. Beddingfield wrote:
> So, if the hardware can detect it then the hardware needs to signal an interrupt for the XRUN.

The hardware typically doesn't know the application pointer.

> Another option... from userspace you can call SYNC_PTR (e.g.
> pcm_get_htimestamp() in tinyalsa) to update the hw and application
> pointers, then inspect them to detect when all the data is transferred.
> When you see all is played, immediately stop the stream.

ALSA checks for xruns before returning the pointer.  It's enough to just
ask for the pointer (or the state).


Regards,
Clemens

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-04-04 17:08 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-03 15:07 Handle large period size end-of-stream situation Patrick Lai
2013-04-03 16:49 ` Clemens Ladisch
2013-04-03 22:22   ` Patrick Lai
2013-04-04  5:35 ` Gabriel M. Beddingfield
2013-04-04  9:27   ` Clemens Ladisch
2013-04-04 15:09     ` Gabriel M. Beddingfield
2013-04-04 17:08       ` Clemens Ladisch

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.