Re: Race Condition With Irregular Streaming

Chris Robinson <[email protected]>
Newsgroups gmane.comp.lib.openal
Message-ID <[email protected]>
On Thursday, August 05, 2010 9:36:22 am Ben Supnik wrote:
> Here's the problem: since my messages are generated semi-randomly by the
> game, I might go to enqueue the next message just before or just after
> the source under-runs and stops itself.
> 
> My concern is that I might check the source status (to see if it's
> playing) and then have it stop just _after_ the check.
> 
> At this point, the source needs a "play" call, but I don't know that,
> and my newly queued sound isn't actually running.
> 
> If I simply play the source all of the time and there is still pending
> work to be done, I will restart the old messages.
> 
> Is there a race-condition-free way to write this logic?  Or do I need to
> poll the channel for under-run periodically to catch any "full stops"
> that I missed?

Something like that might work:

ALint processed, state;
alSourceQueueBuffers(src, 1, &newbuf);
alGetSourcei(src, AL_SOURCE_STATE, &state);

alGetSourcei(src, AL_BUFFERS_PROCESSED, &processed);
while(processed > 0) {
    ALint bid;
    alSourceUnqueueBuffers(src, 1, &bid);
    processed--;
}

if(state != AL_PLAYING) {
    ALint queued;
    alGetSourcei(src, AL_BUFFERS_QUEUED, &queued);
    if(queued)  alSourcePlay(src);
}

Once the buffer is queued, you can get the source state, then the processed 
buffer count. After clearing out the processed buffers, you check if the 
source was marked as playing.

If the source had an under-run before the latest buffered was queued, it will 
not be marked as processed so it will remain queued and be played when the old 
buffers are cleared and the state check is made. In this case, the new buffer 
will play on its own.

If the source somehow had an under-run after the new buffer was queued, before 
getting the source state, then the buffers will all be unqueued and 
alSourcePlay will not be called since there's 0 buffers queued. In this case, 
the new buffer will have played.

If the source had an under-run after getting the source state and before the 
processed count was queried, then all buffers will be unqueued and the source 
won't be called to play again. In this case, the new buffer will have played.


There might be a simpler way to do it, but that should work.
_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.