2018-02-25 04:34:16 +00:00
|
|
|
using ChocolArm64.Memory;
|
2018-03-16 00:06:24 +00:00
|
|
|
using Ryujinx.Audio;
|
2018-10-17 18:15:50 +01:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-08-17 00:47:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
2018-12-18 05:33:36 +00:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Threading;
|
2018-08-17 00:47:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Services.Aud.AudioOut;
|
2018-02-25 04:34:16 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
|
2018-08-17 00:47:36 +01:00
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
2018-07-08 16:42:10 +01:00
|
|
|
|
2018-08-17 00:47:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Aud
|
2018-02-25 04:34:16 +00:00
|
|
|
{
|
2018-03-21 23:30:10 +00:00
|
|
|
class IAudioOutManager : IpcService
|
2018-02-25 04:34:16 +00:00
|
|
|
{
|
2018-04-24 21:14:26 +01:00
|
|
|
private const string DefaultAudioOutput = "DeviceOut";
|
|
|
|
|
2018-07-10 02:49:07 +01:00
|
|
|
private const int DefaultSampleRate = 48000;
|
|
|
|
|
|
|
|
private const int DefaultChannelsCount = 2;
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private Dictionary<int, ServiceProcessRequest> _commands;
|
2018-02-25 04:34:16 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
|
2018-02-25 04:34:16 +00:00
|
|
|
|
2018-03-21 23:30:10 +00:00
|
|
|
public IAudioOutManager()
|
2018-02-25 04:34:16 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
_commands = new Dictionary<int, ServiceProcessRequest>
|
2018-02-25 04:34:16 +00:00
|
|
|
{
|
2018-06-15 16:24:02 +01:00
|
|
|
{ 0, ListAudioOuts },
|
|
|
|
{ 1, OpenAudioOut },
|
|
|
|
{ 2, ListAudioOutsAuto },
|
2018-06-15 16:54:18 +01:00
|
|
|
{ 3, OpenAudioOutAuto }
|
2018-02-25 04:34:16 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public long ListAudioOuts(ServiceCtx context)
|
2018-07-08 16:42:10 +01:00
|
|
|
{
|
|
|
|
return ListAudioOutsImpl(
|
2018-12-06 11:16:24 +00:00
|
|
|
context,
|
|
|
|
context.Request.ReceiveBuff[0].Position,
|
|
|
|
context.Request.ReceiveBuff[0].Size);
|
2018-02-25 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public long OpenAudioOut(ServiceCtx context)
|
2018-06-15 16:41:07 +01:00
|
|
|
{
|
2018-07-08 16:42:10 +01:00
|
|
|
return OpenAudioOutImpl(
|
2018-12-06 11:16:24 +00: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 16:41:07 +01:00
|
|
|
}
|
2018-07-08 16:42:10 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public long ListAudioOutsAuto(ServiceCtx context)
|
2018-07-08 16:42:10 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
(long recvPosition, long recvSize) = context.Request.GetBufferType0x22();
|
2018-06-15 16:41:07 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
return ListAudioOutsImpl(context, recvPosition, recvSize);
|
2018-06-15 16:41:07 +01:00
|
|
|
}
|
2018-07-08 16:42:10 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
public long OpenAudioOutAuto(ServiceCtx context)
|
2018-06-15 16:41:07 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
(long sendPosition, long sendSize) = context.Request.GetBufferType0x21();
|
|
|
|
(long recvPosition, long recvSize) = context.Request.GetBufferType0x22();
|
2018-06-15 16:41:07 +01:00
|
|
|
|
2018-07-08 16:42:10 +01:00
|
|
|
return OpenAudioOutImpl(
|
2018-12-06 11:16:24 +00:00
|
|
|
context,
|
|
|
|
sendPosition,
|
|
|
|
sendSize,
|
|
|
|
recvPosition,
|
|
|
|
recvSize);
|
2018-06-15 16:41:07 +01:00
|
|
|
}
|
2018-07-08 16:42:10 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private long ListAudioOutsImpl(ServiceCtx context, long position, long size)
|
2018-06-15 16:41:07 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
int nameCount = 0;
|
2018-06-15 16:41:07 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(DefaultAudioOutput + "\0");
|
2018-06-15 16:41:07 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if ((ulong)deviceNameBuffer.Length <= (ulong)size)
|
2018-06-15 16:41:07 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Memory.WriteBytes(position, deviceNameBuffer);
|
2018-06-15 16:41:07 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
nameCount++;
|
2018-06-15 16:41:07 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {size} too small!");
|
2018-06-15 16:41:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write(nameCount);
|
2018-07-08 16:42:10 +01:00
|
|
|
|
|
|
|
return 0;
|
2018-06-15 16:41:07 +01:00
|
|
|
}
|
2018-07-08 16:42:10 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
private long OpenAudioOutImpl(ServiceCtx context, long sendPosition, long sendSize, long receivePosition, long receiveSize)
|
2018-02-25 04:34:16 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
string deviceName = MemoryHelper.ReadAsciiString(
|
|
|
|
context.Memory,
|
|
|
|
sendPosition,
|
|
|
|
sendSize);
|
2018-07-08 16:42:10 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (deviceName == string.Empty)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
deviceName = DefaultAudioOutput;
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (deviceName != DefaultAudioOutput)
|
2018-07-08 16:42:10 +01:00
|
|
|
{
|
2018-10-17 18:15:50 +01:00
|
|
|
Logger.PrintWarning(LogClass.Audio, "Invalid device name!");
|
2018-07-08 16:42:10 +01:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Audio, AudErr.DeviceNotFound);
|
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
byte[] deviceNameBuffer = Encoding.ASCII.GetBytes(deviceName + "\0");
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if ((ulong)deviceNameBuffer.Length <= (ulong)receiveSize)
|
2018-04-24 21:14:26 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
context.Memory.WriteBytes(receivePosition, deviceNameBuffer);
|
2018-04-24 21:14:26 +01:00
|
|
|
}
|
|
|
|
else
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
Logger.PrintError(LogClass.ServiceAudio, $"Output buffer size {receiveSize} too small!");
|
2018-07-08 16:42:10 +01:00
|
|
|
}
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int sampleRate = context.RequestData.ReadInt32();
|
|
|
|
int channels = context.RequestData.ReadInt32();
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (sampleRate == 0)
|
2018-07-10 02:49:07 +01:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
sampleRate = DefaultSampleRate;
|
2018-07-10 02:49:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (sampleRate != DefaultSampleRate)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-10-17 18:15:50 +01:00
|
|
|
Logger.PrintWarning(LogClass.Audio, "Invalid sample rate!");
|
2018-07-08 16:42:10 +01:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Audio, AudErr.UnsupportedSampleRate);
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
channels = (ushort)channels;
|
2018-07-08 16:42:10 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
if (channels == 0)
|
2018-03-16 00:06:24 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
channels = DefaultChannelsCount;
|
2018-03-16 00:06:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
KEvent releaseEvent = new KEvent(context.Device.System);
|
2018-03-19 18:58:46 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
ReleaseCallback callback = () =>
|
2018-03-19 18:58:46 +00:00
|
|
|
{
|
2018-12-06 11:16:24 +00:00
|
|
|
releaseEvent.ReadableEvent.Signal();
|
2018-03-19 18:58:46 +00:00
|
|
|
};
|
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
IAalOutput audioOut = context.Device.AudioOut;
|
2018-07-08 16:42:10 +01:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
int track = audioOut.OpenTrack(sampleRate, channels, callback);
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
MakeObject(context, new IAudioOut(audioOut, releaseEvent, track));
|
2018-03-16 00:06:24 +00:00
|
|
|
|
2018-12-06 11:16:24 +00:00
|
|
|
context.ResponseData.Write(sampleRate);
|
|
|
|
context.ResponseData.Write(channels);
|
|
|
|
context.ResponseData.Write((int)SampleFormat.PcmInt16);
|
|
|
|
context.ResponseData.Write((int)PlaybackState.Stopped);
|
2018-07-08 16:42:10 +01:00
|
|
|
|
|
|
|
return 0;
|
2018-02-25 04:34:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 16:24:02 +01:00
|
|
|
}
|