2018-02-24 20:34:16 -08:00
|
|
|
using ChocolArm64.Memory;
|
2018-03-15 17:06:24 -07:00
|
|
|
using Ryujinx.Audio;
|
2018-08-16 16:47:36 -07:00
|
|
|
using Ryujinx.HLE.HOS.Ipc;
|
|
|
|
using Ryujinx.HLE.HOS.Kernel;
|
|
|
|
using Ryujinx.HLE.HOS.Services.Aud.AudioOut;
|
2018-06-10 17:46:42 -07:00
|
|
|
using Ryujinx.HLE.Logging;
|
2018-02-24 20:34:16 -08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
|
2018-08-16 16:47:36 -07:00
|
|
|
using static Ryujinx.HLE.HOS.ErrorCode;
|
2018-07-08 08:42:10 -07:00
|
|
|
|
2018-08-16 16:47:36 -07:00
|
|
|
namespace Ryujinx.HLE.HOS.Services.Aud
|
2018-02-24 20:34:16 -08:00
|
|
|
{
|
2018-03-21 16:30:10 -07:00
|
|
|
class IAudioOutManager : IpcService
|
2018-02-24 20:34:16 -08:00
|
|
|
{
|
2018-04-24 13:14:26 -07:00
|
|
|
private const string DefaultAudioOutput = "DeviceOut";
|
|
|
|
|
2018-07-09 18:49:07 -07:00
|
|
|
private const int DefaultSampleRate = 48000;
|
|
|
|
|
|
|
|
private const int DefaultChannelsCount = 2;
|
|
|
|
|
2018-02-24 20:34:16 -08:00
|
|
|
private Dictionary<int, ServiceProcessRequest> m_Commands;
|
|
|
|
|
2018-03-19 11:58:46 -07:00
|
|
|
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
|
2018-02-24 20:34:16 -08:00
|
|
|
|
2018-03-21 16:30:10 -07:00
|
|
|
public IAudioOutManager()
|
2018-02-24 20:34:16 -08:00
|
|
|
{
|
|
|
|
m_Commands = new Dictionary<int, ServiceProcessRequest>()
|
|
|
|
{
|
2018-06-15 08:24:02 -07:00
|
|
|
{ 0, ListAudioOuts },
|
|
|
|
{ 1, OpenAudioOut },
|
|
|
|
{ 2, ListAudioOutsAuto },
|
2018-06-15 08:54:18 -07:00
|
|
|
{ 3, OpenAudioOutAuto }
|
2018-02-24 20:34:16 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public long ListAudioOuts(ServiceCtx Context)
|
2018-07-08 08:42:10 -07:00
|
|
|
{
|
|
|
|
return ListAudioOutsImpl(
|
|
|
|
Context,
|
|
|
|
Context.Request.ReceiveBuff[0].Position,
|
|
|
|
Context.Request.ReceiveBuff[0].Size);
|
2018-02-24 20:34:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public long OpenAudioOut(ServiceCtx Context)
|
2018-06-15 08:41:07 -07:00
|
|
|
{
|
2018-07-08 08:42:10 -07:00
|
|
|
return OpenAudioOutImpl(
|
|
|
|
Context,
|
|
|
|
Context.Request.SendBuff[0].Position,
|
|
|
|
Context.Request.SendBuff[0].Size,
|
|
|
|
Context.Request.ReceiveBuff[0].Position,
|
|
|
|
Context.Request.ReceiveBuff[0].Size);
|
2018-06-15 08:41:07 -07:00
|
|
|
}
|
2018-07-08 08:42:10 -07:00
|
|
|
|
2018-06-15 08:41:07 -07:00
|
|
|
public long ListAudioOutsAuto(ServiceCtx Context)
|
2018-07-08 08:42:10 -07:00
|
|
|
{
|
|
|
|
(long RecvPosition, long RecvSize) = Context.Request.GetBufferType0x22();
|
2018-06-15 08:41:07 -07:00
|
|
|
|
2018-07-08 08:42:10 -07:00
|
|
|
return ListAudioOutsImpl(Context, RecvPosition, RecvSize);
|
2018-06-15 08:41:07 -07:00
|
|
|
}
|
2018-07-08 08:42:10 -07:00
|
|
|
|
2018-06-15 08:41:07 -07:00
|
|
|
public long OpenAudioOutAuto(ServiceCtx Context)
|
|
|
|
{
|
2018-07-08 08:42:10 -07:00
|
|
|
(long SendPosition, long SendSize) = Context.Request.GetBufferType0x21();
|
|
|
|
(long RecvPosition, long RecvSize) = Context.Request.GetBufferType0x22();
|
2018-06-15 08:41:07 -07:00
|
|
|
|
2018-07-08 08:42:10 -07:00
|
|
|
return OpenAudioOutImpl(
|
|
|
|
Context,
|
|
|
|
SendPosition,
|
|
|
|
SendSize,
|
|
|
|
RecvPosition,
|
|
|
|
RecvSize);
|
2018-06-15 08:41:07 -07:00
|
|
|
}
|
2018-07-08 08:42:10 -07:00
|
|
|
|
|
|
|
private long ListAudioOutsImpl(ServiceCtx Context, long Position, long Size)
|
2018-06-15 08:41:07 -07:00
|
|
|
{
|
|
|
|
int NameCount = 0;
|
|
|
|
|
|
|
|
byte[] DeviceNameBuffer = Encoding.ASCII.GetBytes(DefaultAudioOutput + "\0");
|
|
|
|
|
|
|
|
if ((ulong)DeviceNameBuffer.Length <= (ulong)Size)
|
|
|
|
{
|
|
|
|
Context.Memory.WriteBytes(Position, DeviceNameBuffer);
|
|
|
|
|
|
|
|
NameCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-16 16:47:36 -07:00
|
|
|
Context.Device.Log.PrintError(LogClass.ServiceAudio, $"Output buffer size {Size} too small!");
|
2018-06-15 08:41:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Context.ResponseData.Write(NameCount);
|
2018-07-08 08:42:10 -07:00
|
|
|
|
|
|
|
return 0;
|
2018-06-15 08:41:07 -07:00
|
|
|
}
|
2018-07-08 08:42:10 -07:00
|
|
|
|
|
|
|
private long OpenAudioOutImpl(ServiceCtx Context, long SendPosition, long SendSize, long ReceivePosition, long ReceiveSize)
|
2018-02-24 20:34:16 -08:00
|
|
|
{
|
2018-03-15 17:06:24 -07:00
|
|
|
string DeviceName = AMemoryHelper.ReadAsciiString(
|
|
|
|
Context.Memory,
|
2018-06-15 08:41:07 -07:00
|
|
|
SendPosition,
|
2018-07-08 08:42:10 -07:00
|
|
|
SendSize);
|
|
|
|
|
2018-03-15 17:06:24 -07:00
|
|
|
if (DeviceName == string.Empty)
|
|
|
|
{
|
2018-04-24 13:14:26 -07:00
|
|
|
DeviceName = DefaultAudioOutput;
|
2018-03-15 17:06:24 -07:00
|
|
|
}
|
|
|
|
|
2018-07-08 08:42:10 -07:00
|
|
|
if (DeviceName != DefaultAudioOutput)
|
|
|
|
{
|
2018-08-16 16:47:36 -07:00
|
|
|
Context.Device.Log.PrintWarning(LogClass.Audio, "Invalid device name!");
|
2018-07-08 08:42:10 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Audio, AudErr.DeviceNotFound);
|
|
|
|
}
|
|
|
|
|
2018-04-26 07:34:40 -07:00
|
|
|
byte[] DeviceNameBuffer = Encoding.ASCII.GetBytes(DeviceName + "\0");
|
2018-03-15 17:06:24 -07:00
|
|
|
|
2018-06-15 08:41:07 -07:00
|
|
|
if ((ulong)DeviceNameBuffer.Length <= (ulong)ReceiveSize)
|
2018-04-24 13:14:26 -07:00
|
|
|
{
|
2018-06-15 08:41:07 -07:00
|
|
|
Context.Memory.WriteBytes(ReceivePosition, DeviceNameBuffer);
|
2018-04-24 13:14:26 -07:00
|
|
|
}
|
|
|
|
else
|
2018-03-15 17:06:24 -07:00
|
|
|
{
|
2018-08-16 16:47:36 -07:00
|
|
|
Context.Device.Log.PrintError(LogClass.ServiceAudio, $"Output buffer size {ReceiveSize} too small!");
|
2018-07-08 08:42:10 -07:00
|
|
|
}
|
2018-03-15 17:06:24 -07:00
|
|
|
|
|
|
|
int SampleRate = Context.RequestData.ReadInt32();
|
|
|
|
int Channels = Context.RequestData.ReadInt32();
|
|
|
|
|
2018-07-09 18:49:07 -07:00
|
|
|
if (SampleRate == 0)
|
|
|
|
{
|
|
|
|
SampleRate = DefaultSampleRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SampleRate != DefaultSampleRate)
|
2018-03-15 17:06:24 -07:00
|
|
|
{
|
2018-08-16 16:47:36 -07:00
|
|
|
Context.Device.Log.PrintWarning(LogClass.Audio, "Invalid sample rate!");
|
2018-07-08 08:42:10 -07:00
|
|
|
|
|
|
|
return MakeError(ErrorModule.Audio, AudErr.UnsupportedSampleRate);
|
2018-03-15 17:06:24 -07:00
|
|
|
}
|
|
|
|
|
2018-07-08 08:42:10 -07:00
|
|
|
Channels = (ushort)Channels;
|
|
|
|
|
|
|
|
if (Channels == 0)
|
2018-03-15 17:06:24 -07:00
|
|
|
{
|
2018-07-09 18:49:07 -07:00
|
|
|
Channels = DefaultChannelsCount;
|
2018-03-15 17:06:24 -07:00
|
|
|
}
|
|
|
|
|
2018-09-18 16:36:43 -07:00
|
|
|
KEvent ReleaseEvent = new KEvent(Context.Device.System);
|
2018-03-19 11:58:46 -07:00
|
|
|
|
|
|
|
ReleaseCallback Callback = () =>
|
|
|
|
{
|
2018-09-23 11:11:46 -07:00
|
|
|
ReleaseEvent.ReadableEvent.Signal();
|
2018-03-19 11:58:46 -07:00
|
|
|
};
|
|
|
|
|
2018-08-16 16:47:36 -07:00
|
|
|
IAalOutput AudioOut = Context.Device.AudioOut;
|
2018-07-08 08:42:10 -07:00
|
|
|
|
2018-07-14 19:57:41 -07:00
|
|
|
int Track = AudioOut.OpenTrack(SampleRate, Channels, Callback);
|
2018-03-15 17:06:24 -07:00
|
|
|
|
2018-03-19 11:58:46 -07:00
|
|
|
MakeObject(Context, new IAudioOut(AudioOut, ReleaseEvent, Track));
|
2018-03-15 17:06:24 -07:00
|
|
|
|
|
|
|
Context.ResponseData.Write(SampleRate);
|
|
|
|
Context.ResponseData.Write(Channels);
|
2018-07-14 19:57:41 -07:00
|
|
|
Context.ResponseData.Write((int)SampleFormat.PcmInt16);
|
2018-03-15 17:06:24 -07:00
|
|
|
Context.ResponseData.Write((int)PlaybackState.Stopped);
|
2018-07-08 08:42:10 -07:00
|
|
|
|
|
|
|
return 0;
|
2018-02-24 20:34:16 -08:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 08:24:02 -07:00
|
|
|
}
|