2018-03-16 01:06:24 +01:00
|
|
|
using Ryujinx.Audio;
|
2018-10-17 19:15:50 +02:00
|
|
|
using Ryujinx.Common.Logging;
|
2020-05-04 00:54:50 +02:00
|
|
|
using Ryujinx.Cpu;
|
2018-12-18 06:33:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2019-09-19 02:45:11 +02:00
|
|
|
using Ryujinx.HLE.HOS.Services.Audio.AudioOutManager;
|
2018-02-25 05:34:16 +01:00
|
|
|
using System.Text;
|
|
|
|
|
2019-09-19 02:45:11 +02:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Audio
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-07-10 17:59:54 +02:00
|
|
|
[Service("audout:u")]
|
2018-03-22 00:30:10 +01:00
|
|
|
class IAudioOutManager : IpcService
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2019-07-12 03:13:43 +02:00
|
|
|
private const string DefaultAudioOutput = "DeviceOut";
|
|
|
|
private const int DefaultSampleRate = 48000;
|
|
|
|
private const int DefaultChannelsCount = 2;
|
2018-04-24 22:14:26 +02:00
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
public IAudioOutManager(ServiceCtx context) { }
|
2018-02-25 05:34:16 +01:00
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
[Command(0)]
|
|
|
|
// ListAudioOuts() -> (u32 count, buffer<bytes, 6>)
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode ListAudioOuts(ServiceCtx context)
|
2018-07-08 17:42:10 +02:00
|
|
|
{
|
|
|
|
return ListAudioOutsImpl(
|
2018-12-06 12:16:24 +01:00
|
|
|
context,
|
|
|
|
context.Request.ReceiveBuff[0].Position,
|
|
|
|
context.Request.ReceiveBuff[0].Size);
|
2018-02-25 05:34:16 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
[Command(1)]
|
|
|
|
// OpenAudioOut(u32 sample_rate, u16 unused, u16 channel_count, nn::applet::AppletResourceUserId, pid, handle<copy, process>, buffer<bytes, 5> name_in)
|
|
|
|
// -> (u32 sample_rate, u32 channel_count, u32 pcm_format, u32, object<nn::audio::detail::IAudioOut>, buffer<bytes, 6> name_out)
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode OpenAudioOut(ServiceCtx context)
|
2018-06-15 17:41:07 +02:00
|
|
|
{
|
2018-07-08 17:42:10 +02:00
|
|
|
return OpenAudioOutImpl(
|
2018-12-06 12:16:24 +01:00
|
|
|
context,
|
|
|
|
context.Request.SendBuff[0].Position,
|
|
|
|
context.Request.SendBuff[0].Size,
|
|
|
|
context.Request.ReceiveBuff[0].Position,
|
|
|
|
context.Request.ReceiveBuff[0].Size);
|
2018-06-15 17:41:07 +02:00
|
|
|
}
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
[Command(2)] // 3.0.0+
|
|
|
|
// ListAudioOutsAuto() -> (u32 count, buffer<bytes, 0x22>)
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode ListAudioOutsAuto(ServiceCtx context)
|
2018-07-08 17:42:10 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
(long recvPosition, long recvSize) = context.Request.GetBufferType0x22();
|
2018-06-15 17:41:07 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return ListAudioOutsImpl(context, recvPosition, recvSize);
|
2018-06-15 17:41:07 +02:00
|
|
|
}
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2019-07-12 03:13:43 +02:00
|
|
|
[Command(3)] // 3.0.0+
|
|
|
|
// OpenAudioOutAuto(u32 sample_rate, u16 unused, u16 channel_count, nn::applet::AppletResourceUserId, pid, handle<copy, process>, buffer<bytes, 0x21>)
|
|
|
|
// -> (u32 sample_rate, u32 channel_count, u32 pcm_format, u32, object<nn::audio::detail::IAudioOut>, buffer<bytes, 0x22> name_out)
|
2019-07-14 21:04:38 +02:00
|
|
|
public ResultCode OpenAudioOutAuto(ServiceCtx context)
|
2018-06-15 17:41:07 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
|
|
|
|
(long recvPosition, long recvSize) = context.Request.GetBufferType0x22();
|
2018-06-15 17:41:07 +02:00
|
|
|
|
2018-07-08 17:42:10 +02:00
|
|
|
return OpenAudioOutImpl(
|
2018-12-06 12:16:24 +01:00
|
|
|
context,
|
|
|
|
sendPosition,
|
|
|
|
sendSize,
|
|
|
|
recvPosition,
|
|
|
|
recvSize);
|
2018-06-15 17:41:07 +02:00
|
|
|
}
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
private ResultCode ListAudioOutsImpl(ServiceCtx context, long position, long size)
|
2018-06-15 17:41:07 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
int nameCount = 0;
|
2018-06-15 17:41:07 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(DefaultAudioOutput + "\0");
|
2018-06-15 17:41:07 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((ulong)deviceNameBuffer.Length <= (ulong)size)
|
2018-06-15 17:41:07 +02:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
context.Memory.Write((ulong)position, deviceNameBuffer);
|
2018-06-15 17:41:07 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
nameCount++;
|
2018-06-15 17:41:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
|
2018-06-15 17:41:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
context.ResponseData.Write(nameCount);
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-06-15 17:41:07 +02:00
|
|
|
}
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
private ResultCode OpenAudioOutImpl(ServiceCtx context, long sendPosition, long sendSize, long receivePosition, long receiveSize)
|
2018-02-25 05:34:16 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
string deviceName = MemoryHelper.ReadAsciiString(
|
|
|
|
context.Memory,
|
|
|
|
sendPosition,
|
|
|
|
sendSize);
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (deviceName == string.Empty)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
deviceName = DefaultAudioOutput;
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (deviceName != DefaultAudioOutput)
|
2018-07-08 17:42:10 +02:00
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Warning?.Print(LogClass.Audio, "Invalid device name!");
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.DeviceNotFound;
|
2018-07-08 17:42:10 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(deviceName + "\0");
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((ulong)deviceNameBuffer.Length <= (ulong)receiveSize)
|
2018-04-24 22:14:26 +02:00
|
|
|
{
|
2020-05-04 00:54:50 +02:00
|
|
|
context.Memory.Write((ulong)receivePosition, deviceNameBuffer);
|
2018-04-24 22:14:26 +02:00
|
|
|
}
|
|
|
|
else
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Error?.Print(LogClass.ServiceAudio, $"Output buffer size {receiveSize} too small!");
|
2018-07-08 17:42:10 +02:00
|
|
|
}
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int sampleRate = context.RequestData.ReadInt32();
|
|
|
|
int channels = context.RequestData.ReadInt32();
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (sampleRate == 0)
|
2018-07-10 03:49:07 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
sampleRate = DefaultSampleRate;
|
2018-07-10 03:49:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (sampleRate != DefaultSampleRate)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2020-08-04 01:32:53 +02:00
|
|
|
Logger.Warning?.Print(LogClass.Audio, "Invalid sample rate!");
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.UnsupportedSampleRate;
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
channels = (ushort)channels;
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (channels == 0)
|
2018-03-16 01:06:24 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
channels = DefaultChannelsCount;
|
2018-03-16 01:06:24 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
KEvent releaseEvent = new KEvent(context.Device.System.KernelContext);
|
2018-03-19 19:58:46 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
ReleaseCallback callback = () =>
|
2018-03-19 19:58:46 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
releaseEvent.ReadableEvent.Signal();
|
2018-03-19 19:58:46 +01:00
|
|
|
};
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
IAalOutput audioOut = context.Device.AudioOut;
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int track = audioOut.OpenTrack(sampleRate, channels, callback);
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
MakeObject(context, new IAudioOut(audioOut, releaseEvent, track));
|
2018-03-16 01:06:24 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
context.ResponseData.Write(sampleRate);
|
|
|
|
context.ResponseData.Write(channels);
|
|
|
|
context.ResponseData.Write((int)SampleFormat.PcmInt16);
|
|
|
|
context.ResponseData.Write((int)PlaybackState.Stopped);
|
2018-07-08 17:42:10 +02:00
|
|
|
|
2019-07-14 21:04:38 +02:00
|
|
|
return ResultCode.Success;
|
2018-02-25 05:34:16 +01:00
|
|
|
}
|
|
|
|
}
|
2019-07-12 03:13:43 +02:00
|
|
|
}
|