aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Audio/AudioOutManager/IAudioOut.cs
blob: 5b6983d651e99f28192cc704d4e7342db2313f2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using ARMeilleure.Memory;
using Ryujinx.Audio;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.Kernel.Common;
using Ryujinx.HLE.HOS.Kernel.Threading;
using System;

namespace Ryujinx.HLE.HOS.Services.Audio.AudioOutManager
{
    class IAudioOut : IpcService, IDisposable
    {
        private IAalOutput _audioOut;
        private KEvent     _releaseEvent;
        private int        _track;

        public IAudioOut(IAalOutput audioOut, KEvent releaseEvent, int track)
        {
            _audioOut     = audioOut;
            _releaseEvent = releaseEvent;
            _track        = track;
        }

        [Command(0)]
        // GetAudioOutState() -> u32 state
        public ResultCode GetAudioOutState(ServiceCtx context)
        {
            context.ResponseData.Write((int)_audioOut.GetState(_track));

            return ResultCode.Success;
        }

        [Command(1)]
        // StartAudioOut()
        public ResultCode StartAudioOut(ServiceCtx context)
        {
            _audioOut.Start(_track);

            return ResultCode.Success;
        }

        [Command(2)]
        // StopAudioOut()
        public ResultCode StopAudioOut(ServiceCtx context)
        {
            _audioOut.Stop(_track);

            return ResultCode.Success;
        }

        [Command(3)]
        // AppendAudioOutBuffer(u64 tag, buffer<nn::audio::AudioOutBuffer, 5>)
        public ResultCode AppendAudioOutBuffer(ServiceCtx context)
        {
            return AppendAudioOutBufferImpl(context, context.Request.SendBuff[0].Position);
        }

        [Command(4)]
        // RegisterBufferEvent() -> handle<copy>
        public ResultCode RegisterBufferEvent(ServiceCtx context)
        {
            if (context.Process.HandleTable.GenerateHandle(_releaseEvent.ReadableEvent, out int handle) != KernelResult.Success)
            {
                throw new InvalidOperationException("Out of handles!");
            }

            context.Response.HandleDesc = IpcHandleDesc.MakeCopy(handle);

            return ResultCode.Success;
        }

        [Command(5)]
        // GetReleasedAudioOutBuffer() -> (u32 count, buffer<nn::audio::AudioOutBuffer, 6>)
        public ResultCode GetReleasedAudioOutBuffer(ServiceCtx context)
        {
            long position = context.Request.ReceiveBuff[0].Position;
            long size     = context.Request.ReceiveBuff[0].Size;

            return GetReleasedAudioOutBufferImpl(context, position, size);
        }

        [Command(6)]
        // ContainsAudioOutBuffer(u64 tag) -> b8
        public ResultCode ContainsAudioOutBuffer(ServiceCtx context)
        {
            long tag = context.RequestData.ReadInt64();

            context.ResponseData.Write(_audioOut.ContainsBuffer(_track, tag) ? 1 : 0);

            return 0;
        }

        [Command(7)] // 3.0.0+
        // AppendAudioOutBufferAuto(u64 tag, buffer<nn::audio::AudioOutBuffer, 0x21>)
        public ResultCode AppendAudioOutBufferAuto(ServiceCtx context)
        {
            (long position, long size) = context.Request.GetBufferType0x21();

            return AppendAudioOutBufferImpl(context, position);
        }

        public ResultCode AppendAudioOutBufferImpl(ServiceCtx context, long position)
        {
            long tag = context.RequestData.ReadInt64();

            AudioOutData data = MemoryHelper.Read<AudioOutData>(
                context.Memory,
                position);

            byte[] buffer = context.Memory.ReadBytes(
                data.SampleBufferPtr,
                data.SampleBufferSize);

            _audioOut.AppendBuffer(_track, tag, buffer);

            return ResultCode.Success;
        }

        [Command(8)] // 3.0.0+
        // GetReleasedAudioOutBufferAuto() -> (u32 count, buffer<nn::audio::AudioOutBuffer, 0x22>)
        public ResultCode GetReleasedAudioOutBufferAuto(ServiceCtx context)
        {
            (long position, long size) = context.Request.GetBufferType0x22();

            return GetReleasedAudioOutBufferImpl(context, position, size);
        }

        public ResultCode GetReleasedAudioOutBufferImpl(ServiceCtx context, long position, long size)
        {
            uint count = (uint)((ulong)size >> 3);

            long[] releasedBuffers = _audioOut.GetReleasedBuffers(_track, (int)count);

            for (uint index = 0; index < count; index++)
            {
                long tag = 0;

                if (index < releasedBuffers.Length)
                {
                    tag = releasedBuffers[index];
                }

                context.Memory.WriteInt64(position + index * 8, tag);
            }

            context.ResponseData.Write(releasedBuffers.Length);

            return ResultCode.Success;
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _audioOut.CloseTrack(_track);
            }
        }
    }
}