VirtualDeviceSessionRegistry.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System.Collections.Generic;
  18. namespace Ryujinx.Audio.Renderer.Device
  19. {
  20. /// <summary>
  21. /// Represent an instance containing a registry of <see cref="VirtualDeviceSession"/>.
  22. /// </summary>
  23. public class VirtualDeviceSessionRegistry
  24. {
  25. /// <summary>
  26. /// The session registry, used to store the sessions of a given AppletResourceId.
  27. /// </summary>
  28. private Dictionary<ulong, VirtualDeviceSession[]> _sessionsRegistry = new Dictionary<ulong, VirtualDeviceSession[]>();
  29. /// <summary>
  30. /// The default <see cref="VirtualDevice"/>.
  31. /// </summary>
  32. /// <remarks>This is used when the USB device is the default one on older revision.</remarks>
  33. public VirtualDevice DefaultDevice => VirtualDevice.Devices[0];
  34. /// <summary>
  35. /// The current active <see cref="VirtualDevice"/>.
  36. /// </summary>
  37. // TODO: make this configurable
  38. public VirtualDevice ActiveDevice = VirtualDevice.Devices[2];
  39. /// <summary>
  40. /// Get the associated <see cref="T:VirtualDeviceSession[]"/> from an AppletResourceId.
  41. /// </summary>
  42. /// <param name="resourceAppletId">The AppletResourceId used.</param>
  43. /// <returns>The associated <see cref="T:VirtualDeviceSession[]"/> from an AppletResourceId.</returns>
  44. public VirtualDeviceSession[] GetSessionByAppletResourceId(ulong resourceAppletId)
  45. {
  46. if (_sessionsRegistry.TryGetValue(resourceAppletId, out VirtualDeviceSession[] result))
  47. {
  48. return result;
  49. }
  50. result = CreateSessionsFromBehaviourContext();
  51. _sessionsRegistry.Add(resourceAppletId, result);
  52. return result;
  53. }
  54. /// <summary>
  55. /// Create a new array of sessions for each <see cref="VirtualDevice"/>.
  56. /// </summary>
  57. /// <returns>A new array of sessions for each <see cref="VirtualDevice"/>.</returns>
  58. private static VirtualDeviceSession[] CreateSessionsFromBehaviourContext()
  59. {
  60. VirtualDeviceSession[] virtualDeviceSession = new VirtualDeviceSession[VirtualDevice.Devices.Length];
  61. for (int i = 0; i < virtualDeviceSession.Length; i++)
  62. {
  63. virtualDeviceSession[i] = new VirtualDeviceSession(VirtualDevice.Devices[i]);
  64. }
  65. return virtualDeviceSession;
  66. }
  67. }
  68. }