| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- // Copyright (c) 2019-2021 Ryujinx
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Lesser General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- //
- using Ryujinx.Audio.Integration;
- using Ryujinx.Common;
- using System;
- using System.Diagnostics;
- namespace Ryujinx.Audio.Common
- {
- /// <summary>
- /// An audio device session.
- /// </summary>
- class AudioDeviceSession : IDisposable
- {
- /// <summary>
- /// The volume of the <see cref="AudioDeviceSession"/>.
- /// </summary>
- private float _volume;
- /// <summary>
- /// The state of the <see cref="AudioDeviceSession"/>.
- /// </summary>
- private AudioDeviceState _state;
- /// <summary>
- /// Array of all buffers currently used or released.
- /// </summary>
- private AudioBuffer[] _buffers;
- /// <summary>
- /// The server index inside <see cref="_buffers"/> (appended but not queued to device driver).
- /// </summary>
- private uint _serverBufferIndex;
- /// <summary>
- /// The hardware index inside <see cref="_buffers"/> (queued to device driver).
- /// </summary>
- private uint _hardwareBufferIndex;
- /// <summary>
- /// The released index inside <see cref="_buffers"/> (released by the device driver).
- /// </summary>
- private uint _releasedBufferIndex;
- /// <summary>
- /// The count of buffer appended (server side).
- /// </summary>
- private uint _bufferAppendedCount;
- /// <summary>
- /// The count of buffer registered (driver side).
- /// </summary>
- private uint _bufferRegisteredCount;
- /// <summary>
- /// The count of buffer released (released by the driver side).
- /// </summary>
- private uint _bufferReleasedCount;
- /// <summary>
- /// The released buffer event.
- /// </summary>
- private IWritableEvent _bufferEvent;
- /// <summary>
- /// The session on the device driver.
- /// </summary>
- private IHardwareDeviceSession _hardwareDeviceSession;
- /// <summary>
- /// Max number of buffers that can be registered to the device driver at a time.
- /// </summary>
- private uint _bufferRegisteredLimit;
- /// <summary>
- /// Create a new <see cref="AudioDeviceSession"/>.
- /// </summary>
- /// <param name="deviceSession">The device driver session associated</param>
- /// <param name="bufferEvent">The release buffer event</param>
- /// <param name="bufferRegisteredLimit">The max number of buffers that can be registered to the device driver at a time</param>
- public AudioDeviceSession(IHardwareDeviceSession deviceSession, IWritableEvent bufferEvent, uint bufferRegisteredLimit = 4)
- {
- _bufferEvent = bufferEvent;
- _hardwareDeviceSession = deviceSession;
- _bufferRegisteredLimit = bufferRegisteredLimit;
- _buffers = new AudioBuffer[Constants.AudioDeviceBufferCountMax];
- _serverBufferIndex = 0;
- _hardwareBufferIndex = 0;
- _releasedBufferIndex = 0;
- _bufferAppendedCount = 0;
- _bufferRegisteredCount = 0;
- _bufferReleasedCount = 0;
- _volume = deviceSession.GetVolume();
- _state = AudioDeviceState.Stopped;
- }
- /// <summary>
- /// Get the released buffer event.
- /// </summary>
- /// <returns>The released buffer event</returns>
- public IWritableEvent GetBufferEvent()
- {
- return _bufferEvent;
- }
- /// <summary>
- /// Get the state of the session.
- /// </summary>
- /// <returns>The state of the session</returns>
- public AudioDeviceState GetState()
- {
- Debug.Assert(_state == AudioDeviceState.Started || _state == AudioDeviceState.Stopped);
- return _state;
- }
- /// <summary>
- /// Get the total buffer count (server + driver + released).
- /// </summary>
- /// <returns>Return the total buffer count</returns>
- private uint GetTotalBufferCount()
- {
- uint bufferCount = _bufferAppendedCount + _bufferRegisteredCount + _bufferReleasedCount;
- Debug.Assert(bufferCount <= Constants.AudioDeviceBufferCountMax);
- return bufferCount;
- }
- /// <summary>
- /// Register a new <see cref="AudioBuffer"/> on the server side.
- /// </summary>
- /// <param name="buffer">The <see cref="AudioBuffer"/> to register</param>
- /// <returns>True if the operation succeeded</returns>
- private bool RegisterBuffer(AudioBuffer buffer)
- {
- if (GetTotalBufferCount() == Constants.AudioDeviceBufferCountMax)
- {
- return false;
- }
- _buffers[_serverBufferIndex] = buffer;
- _serverBufferIndex = (_serverBufferIndex + 1) % Constants.AudioDeviceBufferCountMax;
- _bufferAppendedCount++;
- return true;
- }
- /// <summary>
- /// Flush server buffers to hardware.
- /// </summary>
- private void FlushToHardware()
- {
- uint bufferToFlushCount = Math.Min(Math.Min(_bufferAppendedCount, 4), _bufferRegisteredLimit - _bufferRegisteredCount);
- AudioBuffer[] buffersToFlush = new AudioBuffer[bufferToFlushCount];
- uint hardwareBufferIndex = _hardwareBufferIndex;
- for (int i = 0; i < buffersToFlush.Length; i++)
- {
- buffersToFlush[i] = _buffers[hardwareBufferIndex];
- _bufferAppendedCount--;
- _bufferRegisteredCount++;
- hardwareBufferIndex = (hardwareBufferIndex + 1) % Constants.AudioDeviceBufferCountMax;
- }
- _hardwareBufferIndex = hardwareBufferIndex;
- for (int i = 0; i < buffersToFlush.Length; i++)
- {
- _hardwareDeviceSession.QueueBuffer(buffersToFlush[i]);
- }
- }
- /// <summary>
- /// Get the current index of the <see cref="AudioBuffer"/> playing on the driver side.
- /// </summary>
- /// <param name="playingIndex">The output index of the <see cref="AudioBuffer"/> playing on the driver side</param>
- /// <returns>True if any buffer is playing</returns>
- private bool TryGetPlayingBufferIndex(out uint playingIndex)
- {
- if (_bufferRegisteredCount > 0)
- {
- playingIndex = (_hardwareBufferIndex - _bufferRegisteredCount) % Constants.AudioDeviceBufferCountMax;
- return true;
- }
- playingIndex = 0;
- return false;
- }
- /// <summary>
- /// Try to pop the <see cref="AudioBuffer"/> playing on the driver side.
- /// </summary>
- /// <param name="buffer">The output <see cref="AudioBuffer"/> playing on the driver side</param>
- /// <returns>True if any buffer is playing</returns>
- private bool TryPopPlayingBuffer(out AudioBuffer buffer)
- {
- if (_bufferRegisteredCount > 0)
- {
- uint bufferIndex = (_hardwareBufferIndex - _bufferRegisteredCount) % Constants.AudioDeviceBufferCountMax;
- buffer = _buffers[bufferIndex];
- _buffers[bufferIndex] = null;
- _bufferRegisteredCount--;
- return true;
- }
- buffer = null;
- return false;
- }
- /// <summary>
- /// Try to pop a <see cref="AudioBuffer"/> released by the driver side.
- /// </summary>
- /// <param name="buffer">The output <see cref="AudioBuffer"/> released by the driver side</param>
- /// <returns>True if any buffer has been released</returns>
- public bool TryPopReleasedBuffer(out AudioBuffer buffer)
- {
- if (_bufferReleasedCount > 0)
- {
- uint bufferIndex = (_releasedBufferIndex - _bufferReleasedCount) % Constants.AudioDeviceBufferCountMax;
- buffer = _buffers[bufferIndex];
- _buffers[bufferIndex] = null;
- _bufferReleasedCount--;
- return true;
- }
- buffer = null;
- return false;
- }
- /// <summary>
- /// Release a <see cref="AudioBuffer"/>.
- /// </summary>
- /// <param name="buffer">The <see cref="AudioBuffer"/> to release</param>
- private void ReleaseBuffer(AudioBuffer buffer)
- {
- buffer.PlayedTimestamp = (ulong)PerformanceCounter.ElapsedNanoseconds;
- _bufferRegisteredCount--;
- _bufferReleasedCount++;
- _releasedBufferIndex = (_releasedBufferIndex + 1) % Constants.AudioDeviceBufferCountMax;
- }
- /// <summary>
- /// Update the released buffers.
- /// </summary>
- /// <param name="updateForStop">True if the session is currently stopping</param>
- private void UpdateReleaseBuffers(bool updateForStop = false)
- {
- bool wasAnyBuffersReleased = false;
- while (TryGetPlayingBufferIndex(out uint playingIndex))
- {
- if (!updateForStop && !_hardwareDeviceSession.WasBufferFullyConsumed(_buffers[playingIndex]))
- {
- break;
- }
- if (updateForStop)
- {
- _hardwareDeviceSession.UnregisterBuffer(_buffers[playingIndex]);
- }
- ReleaseBuffer(_buffers[playingIndex]);
- wasAnyBuffersReleased = true;
- }
- if (wasAnyBuffersReleased)
- {
- _bufferEvent.Signal();
- }
- }
- /// <summary>
- /// Append a new <see cref="AudioBuffer"/>.
- /// </summary>
- /// <param name="buffer">The <see cref="AudioBuffer"/> to append</param>
- /// <returns>True if the buffer was appended</returns>
- public bool AppendBuffer(AudioBuffer buffer)
- {
- if (_hardwareDeviceSession.RegisterBuffer(buffer))
- {
- if (RegisterBuffer(buffer))
- {
- FlushToHardware();
- return true;
- }
- _hardwareDeviceSession.UnregisterBuffer(buffer);
- }
- return false;
- }
- public bool AppendUacBuffer(AudioBuffer buffer, uint handle)
- {
- // NOTE: On hardware, there is another RegisterBuffer method taking an handle.
- // This variant of the call always return false (stubbed?) as a result this logic will never succeed.
- return false;
- }
- /// <summary>
- /// Start the audio session.
- /// </summary>
- /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
- public ResultCode Start()
- {
- if (_state == AudioDeviceState.Started)
- {
- return ResultCode.OperationFailed;
- }
- _hardwareDeviceSession.Start();
- _state = AudioDeviceState.Started;
- FlushToHardware();
- _hardwareDeviceSession.SetVolume(_volume);
- return ResultCode.Success;
- }
- /// <summary>
- /// Stop the audio session.
- /// </summary>
- /// <returns>A <see cref="ResultCode"/> reporting an error or a success</returns>
- public ResultCode Stop()
- {
- if (_state == AudioDeviceState.Started)
- {
- _hardwareDeviceSession.Stop();
- UpdateReleaseBuffers(true);
- _state = AudioDeviceState.Stopped;
- }
- return ResultCode.Success;
- }
- /// <summary>
- /// Get the volume of the session.
- /// </summary>
- /// <returns>The volume of the session</returns>
- public float GetVolume()
- {
- return _hardwareDeviceSession.GetVolume();
- }
- /// <summary>
- /// Set the volume of the session.
- /// </summary>
- /// <param name="volume">The new volume to set</param>
- public void SetVolume(float volume)
- {
- _volume = volume;
- if (_state == AudioDeviceState.Started)
- {
- _hardwareDeviceSession.SetVolume(volume);
- }
- }
- /// <summary>
- /// Get the count of buffer currently in use (server + driver side).
- /// </summary>
- /// <returns>The count of buffer currently in use</returns>
- public uint GetBufferCount()
- {
- return _bufferAppendedCount + _bufferRegisteredCount;
- }
- /// <summary>
- /// Check if a buffer is present.
- /// </summary>
- /// <param name="bufferTag">The unique tag of the buffer</param>
- /// <returns>Return true if a buffer is present</returns>
- public bool ContainsBuffer(ulong bufferTag)
- {
- uint bufferIndex = (_releasedBufferIndex - _bufferReleasedCount) % Constants.AudioDeviceBufferCountMax;
- for (int i = 0; i < GetTotalBufferCount(); i++)
- {
- if (_buffers[bufferIndex].BufferTag == bufferTag)
- {
- return true;
- }
- bufferIndex = (bufferIndex + 1) % Constants.AudioDeviceBufferCountMax;
- }
- return false;
- }
- /// <summary>
- /// Get the count of sample played in this session.
- /// </summary>
- /// <returns>The count of sample played in this session</returns>
- public ulong GetPlayedSampleCount()
- {
- if (_state == AudioDeviceState.Stopped)
- {
- return 0;
- }
- else
- {
- return _hardwareDeviceSession.GetPlayedSampleCount();
- }
- }
- /// <summary>
- /// Flush all buffers to the initial state.
- /// </summary>
- /// <returns>True if any buffer was flushed</returns>
- public bool FlushBuffers()
- {
- if (_state == AudioDeviceState.Stopped)
- {
- return false;
- }
- uint bufferCount = GetBufferCount();
- while (TryPopReleasedBuffer(out AudioBuffer buffer))
- {
- _hardwareDeviceSession.UnregisterBuffer(buffer);
- }
- while (TryPopPlayingBuffer(out AudioBuffer buffer))
- {
- _hardwareDeviceSession.UnregisterBuffer(buffer);
- }
- if (_bufferRegisteredCount == 0 || (_bufferReleasedCount + _bufferAppendedCount) > Constants.AudioDeviceBufferCountMax)
- {
- return false;
- }
- _bufferReleasedCount += _bufferAppendedCount;
- _releasedBufferIndex = (_releasedBufferIndex + _bufferAppendedCount) % Constants.AudioDeviceBufferCountMax;
- _bufferAppendedCount = 0;
- _hardwareBufferIndex = _serverBufferIndex;
- if (bufferCount > 0)
- {
- _bufferEvent.Signal();
- }
- return true;
- }
- /// <summary>
- /// Update the session.
- /// </summary>
- public void Update()
- {
- if (_state == AudioDeviceState.Started)
- {
- UpdateReleaseBuffers();
- FlushToHardware();
- }
- }
- public void Dispose()
- {
- Dispose(true);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- // Tell the hardware session that we are ending.
- _hardwareDeviceSession.PrepareToClose();
- // Unregister all buffers
- while (TryPopReleasedBuffer(out AudioBuffer buffer))
- {
- _hardwareDeviceSession.UnregisterBuffer(buffer);
- }
- while (TryPopPlayingBuffer(out AudioBuffer buffer))
- {
- _hardwareDeviceSession.UnregisterBuffer(buffer);
- }
- // Finally dispose hardware session.
- _hardwareDeviceSession.Dispose();
- _bufferEvent.Signal();
- }
- }
- }
- }
|