1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-10-01 12:30:00 +02:00

Ensure that buffers are available after a signal when GetReleasedBuffers is called (#369)

This commit is contained in:
gdkchan 2018-09-18 00:12:47 -03:00 committed by GitHub
parent c7387be0d2
commit bec95cacc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,16 +28,12 @@ namespace Ryujinx.Audio.OpenAL
public PlaybackState State { get; set; }
private bool ShouldCallReleaseCallback;
private ConcurrentDictionary<long, int> Buffers;
private Queue<long> QueuedTagsQueue;
private Queue<long> ReleasedTagsQueue;
private int LastReleasedCount;
private bool Disposed;
public Track(int SampleRate, ALFormat Format, ReleaseCallback Callback)
@ -59,8 +55,6 @@ namespace Ryujinx.Audio.OpenAL
public bool ContainsBuffer(long Tag)
{
SyncQueuedTags();
foreach (long QueuedTag in QueuedTagsQueue)
{
if (QueuedTag == Tag)
@ -72,20 +66,29 @@ namespace Ryujinx.Audio.OpenAL
return false;
}
public long[] GetReleasedBuffers(int MaxCount)
public long[] GetReleasedBuffers(int Count)
{
ClearReleased();
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
ReleasedCount += ReleasedTagsQueue.Count;
if (Count > ReleasedCount)
{
Count = ReleasedCount;
}
List<long> Tags = new List<long>();
HashSet<long> Unique = new HashSet<long>();
while (MaxCount-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
{
if (Unique.Add(Tag))
while (Count-- > 0 && ReleasedTagsQueue.TryDequeue(out long Tag))
{
Tags.Add(Tag);
}
while (Count-- > 0 && QueuedTagsQueue.TryDequeue(out long Tag))
{
AL.SourceUnqueueBuffers(SourceId, 1);
Tags.Add(Tag);
}
return Tags.ToArray();
@ -112,67 +115,27 @@ namespace Ryujinx.Audio.OpenAL
return Id;
}
public void ClearReleased()
public void CallReleaseCallbackIfNeeded()
{
SyncQueuedTags();
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
CheckReleaseChanges(ReleasedCount);
if (ReleasedCount > 0)
{
AL.SourceUnqueueBuffers(SourceId, ReleasedCount);
}
}
public void CallReleaseCallbackIfNeeded()
//If we signal, then we also need to have released buffers available
//to return when GetReleasedBuffers is called.
//If playback needs to be re-started due to all buffers being processed,
//then OpenAL zeros the counts (ReleasedCount), so we keep it on the queue.
while (ReleasedCount-- > 0 && QueuedTagsQueue.TryDequeue(out long Tag))
{
CheckReleaseChanges();
AL.SourceUnqueueBuffers(SourceId, 1);
if (ShouldCallReleaseCallback)
{
ShouldCallReleaseCallback = false;
ReleasedTagsQueue.Enqueue(Tag);
}
Callback();
}
}
private void CheckReleaseChanges()
{
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
CheckReleaseChanges(ReleasedCount);
}
private void CheckReleaseChanges(int NewReleasedCount)
{
if (LastReleasedCount != NewReleasedCount)
{
LastReleasedCount = NewReleasedCount;
ShouldCallReleaseCallback = true;
}
}
private void SyncQueuedTags()
{
AL.GetSource(SourceId, ALGetSourcei.BuffersQueued, out int QueuedCount);
AL.GetSource(SourceId, ALGetSourcei.BuffersProcessed, out int ReleasedCount);
QueuedCount -= ReleasedCount;
while (QueuedTagsQueue.Count > QueuedCount)
{
ReleasedTagsQueue.Enqueue(QueuedTagsQueue.Dequeue());
}
while (ReleasedTagsQueue.Count > MaxReleased)
{
ReleasedTagsQueue.Dequeue();
}
}
public void Dispose()
{
Dispose(true);
@ -265,17 +228,23 @@ namespace Ryujinx.Audio.OpenAL
public void CloseTrack(int Track)
{
if (Tracks.TryRemove(Track, out Track Td))
{
lock (Td)
{
Td.Dispose();
}
}
}
public bool ContainsBuffer(int Track, long Tag)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
return Td.ContainsBuffer(Tag);
}
}
return false;
}
@ -283,9 +252,12 @@ namespace Ryujinx.Audio.OpenAL
public long[] GetReleasedBuffers(int Track, int MaxCount)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
return Td.GetReleasedBuffers(MaxCount);
}
}
return null;
}
@ -293,6 +265,8 @@ namespace Ryujinx.Audio.OpenAL
public void AppendBuffer<T>(int Track, long Tag, T[] Buffer) where T : struct
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
int BufferId = Td.AppendBuffer(Tag);
@ -305,16 +279,20 @@ namespace Ryujinx.Audio.OpenAL
StartPlaybackIfNeeded(Td);
}
}
}
public void Start(int Track)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
Td.State = PlaybackState.Playing;
StartPlaybackIfNeeded(Td);
}
}
}
private void StartPlaybackIfNeeded(Track Td)
{
@ -324,8 +302,6 @@ namespace Ryujinx.Audio.OpenAL
if (State != ALSourceState.Playing && Td.State == PlaybackState.Playing)
{
Td.ClearReleased();
AL.SourcePlay(Td.SourceId);
}
}
@ -333,12 +309,15 @@ namespace Ryujinx.Audio.OpenAL
public void Stop(int Track)
{
if (Tracks.TryGetValue(Track, out Track Td))
{
lock (Td)
{
Td.State = PlaybackState.Stopped;
AL.SourceStop(Td.SourceId);
}
}
}
public PlaybackState GetState(int Track)
{