DummyAudioOut.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections.Concurrent;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Audio
  4. {
  5. /// <summary>
  6. /// A Dummy audio renderer that does not output any audio
  7. /// </summary>
  8. public class DummyAudioOut : IAalOutput
  9. {
  10. private int lastTrackId = 1;
  11. private ConcurrentQueue<int> m_TrackIds;
  12. private ConcurrentQueue<long> m_Buffers;
  13. private ConcurrentDictionary<int, ReleaseCallback> m_ReleaseCallbacks;
  14. public DummyAudioOut()
  15. {
  16. m_Buffers = new ConcurrentQueue<long>();
  17. m_TrackIds = new ConcurrentQueue<int>();
  18. m_ReleaseCallbacks = new ConcurrentDictionary<int, ReleaseCallback>();
  19. }
  20. /// <summary>
  21. /// Dummy audio output is always available, Baka!
  22. /// </summary>
  23. public static bool IsSupported => true;
  24. public PlaybackState GetState(int trackId) => PlaybackState.Stopped;
  25. public int OpenTrack(int sampleRate, int channels, ReleaseCallback callback)
  26. {
  27. int trackId;
  28. if(!m_TrackIds.TryDequeue(out trackId))
  29. {
  30. trackId = ++lastTrackId;
  31. }
  32. m_ReleaseCallbacks[trackId] = callback;
  33. return trackId;
  34. }
  35. public void CloseTrack(int trackId)
  36. {
  37. m_TrackIds.Enqueue(trackId);
  38. m_ReleaseCallbacks.Remove(trackId, out _);
  39. }
  40. public void Start(int trackId) { }
  41. public void Stop(int trackId) { }
  42. public void AppendBuffer<T>(int trackID, long bufferTag, T[] buffer)
  43. where T : struct
  44. {
  45. m_Buffers.Enqueue(bufferTag);
  46. if(m_ReleaseCallbacks.TryGetValue(trackID, out var callback))
  47. {
  48. callback?.Invoke();
  49. }
  50. }
  51. public long[] GetReleasedBuffers(int trackId, int maxCount)
  52. {
  53. List<long> bufferTags = new List<long>();
  54. for (int i = 0; i < maxCount; i++)
  55. {
  56. if (!m_Buffers.TryDequeue(out long tag))
  57. {
  58. break;
  59. }
  60. bufferTags.Add(tag);
  61. }
  62. return bufferTags.ToArray();
  63. }
  64. public bool ContainsBuffer(int trackID, long bufferTag) => false;
  65. public void Dispose()
  66. {
  67. m_Buffers.Clear();
  68. }
  69. }
  70. }