mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-21 12:32:40 +02:00
396 lines
11 KiB
C
396 lines
11 KiB
C
#define NX_SERVICE_ASSUME_NON_DOMAIN
|
|
#include "service_guard.h"
|
|
#include "kernel/event.h"
|
|
#include "services/audout.h"
|
|
#include "services/applet.h"
|
|
#include "runtime/hosversion.h"
|
|
|
|
#define DEVICE_NAME_LENGTH 0x100
|
|
#define DEFAULT_SAMPLE_RATE 0xBB80
|
|
#define DEFAULT_CHANNEL_COUNT 0x00020000
|
|
|
|
static Service g_audoutSrv;
|
|
static Service g_audoutIAudioOut;
|
|
static Service g_audoutaSrv;
|
|
static Service g_audoutdSrv;
|
|
|
|
static Event g_audoutBufferEvent;
|
|
|
|
static u32 g_sampleRate = 0;
|
|
static u32 g_channelCount = 0;
|
|
static PcmFormat g_pcmFormat = PcmFormat_Invalid;
|
|
static AudioOutState g_deviceState = AudioOutState_Stopped;
|
|
|
|
static Result _audoutRegisterBufferEvent(Event *BufferEvent);
|
|
|
|
NX_GENERATE_SERVICE_GUARD(audout);
|
|
NX_GENERATE_SERVICE_GUARD(audouta);
|
|
NX_GENERATE_SERVICE_GUARD(audoutd);
|
|
|
|
Result _audoutInitialize(void) {
|
|
Result rc = 0;
|
|
rc = smGetService(&g_audoutSrv, "audout:u");
|
|
|
|
// Setup the default device
|
|
if (R_SUCCEEDED(rc)) {
|
|
// Passing an empty device name will open the default "DeviceOut"
|
|
char DeviceNameIn[DEVICE_NAME_LENGTH] = {0};
|
|
char DeviceNameOut[DEVICE_NAME_LENGTH] = {0};
|
|
|
|
// Open audio output device
|
|
rc = audoutOpenAudioOut(DeviceNameIn, DeviceNameOut, DEFAULT_SAMPLE_RATE, DEFAULT_CHANNEL_COUNT, &g_sampleRate, &g_channelCount, &g_pcmFormat, &g_deviceState);
|
|
}
|
|
|
|
// Register global handle for buffer events
|
|
if (R_SUCCEEDED(rc))
|
|
rc = _audoutRegisterBufferEvent(&g_audoutBufferEvent);
|
|
|
|
return rc;
|
|
}
|
|
|
|
void _audoutCleanup(void) {
|
|
eventClose(&g_audoutBufferEvent);
|
|
|
|
g_sampleRate = 0;
|
|
g_channelCount = 0;
|
|
g_pcmFormat = PcmFormat_Invalid;
|
|
g_deviceState = AudioOutState_Stopped;
|
|
|
|
serviceClose(&g_audoutIAudioOut);
|
|
serviceClose(&g_audoutSrv);
|
|
}
|
|
|
|
Result _audoutaInitialize(void) {
|
|
if (hosversionAtLeast(11,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
return smGetService(&g_audoutaSrv, "audout:a");
|
|
}
|
|
|
|
void _audoutaCleanup(void) {
|
|
serviceClose(&g_audoutaSrv);
|
|
}
|
|
|
|
Result _audoutdInitialize(void) {
|
|
if (hosversionAtLeast(11,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
return smGetService(&g_audoutdSrv, "audout:d");
|
|
}
|
|
|
|
void _audoutdCleanup(void) {
|
|
serviceClose(&g_audoutdSrv);
|
|
}
|
|
|
|
Service* audoutGetServiceSession(void) {
|
|
return &g_audoutSrv;
|
|
}
|
|
|
|
Service* audoutGetServiceSession_AudioOut(void) {
|
|
return &g_audoutIAudioOut;
|
|
}
|
|
|
|
Service* audoutaGetServiceSession(void) {
|
|
return &g_audoutaSrv;
|
|
}
|
|
|
|
Service* audoutdGetServiceSession(void) {
|
|
return &g_audoutdSrv;
|
|
}
|
|
|
|
u32 audoutGetSampleRate(void) {
|
|
return g_sampleRate;
|
|
}
|
|
|
|
u32 audoutGetChannelCount(void) {
|
|
return g_channelCount;
|
|
}
|
|
|
|
PcmFormat audoutGetPcmFormat(void) {
|
|
return g_pcmFormat;
|
|
}
|
|
|
|
AudioOutState audoutGetDeviceState(void) {
|
|
return g_deviceState;
|
|
}
|
|
|
|
static Result _audoutCmdGetHandle(Service* srv, Handle* handle_out, u32 cmd_id) {
|
|
return serviceDispatch(srv, cmd_id,
|
|
.out_handle_attrs = { SfOutHandleAttr_HipcCopy },
|
|
.out_handles = handle_out,
|
|
);
|
|
}
|
|
|
|
static Result _audoutCmdGetEvent(Service* srv, Event* out_event, bool autoclear, u32 cmd_id) {
|
|
Handle tmp_handle = INVALID_HANDLE;
|
|
Result rc = 0;
|
|
|
|
rc = _audoutCmdGetHandle(srv, &tmp_handle, cmd_id);
|
|
if (R_SUCCEEDED(rc)) eventLoadRemote(out_event, tmp_handle, autoclear);
|
|
return rc;
|
|
}
|
|
|
|
static Result _audoutCmdNoIO(Service* srv, u32 cmd_id) {
|
|
return serviceDispatch(srv, cmd_id);
|
|
}
|
|
|
|
static Result _audoutCmdNoInOutU32(Service* srv, u32 *out, u32 cmd_id) {
|
|
return serviceDispatchOut(srv, cmd_id, *out);
|
|
}
|
|
|
|
Result audoutWaitPlayFinish(AudioOutBuffer **released, u32* released_count, u64 timeout) {
|
|
// Wait on the buffer event handle
|
|
Result rc = eventWait(&g_audoutBufferEvent, timeout);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
// Grab the released buffer
|
|
rc = audoutGetReleasedAudioOutBuffer(released, released_count);
|
|
}
|
|
|
|
return rc;
|
|
}
|
|
|
|
Result audoutPlayBuffer(AudioOutBuffer *source, AudioOutBuffer **released) {
|
|
Result rc = 0;
|
|
u32 released_count = 0;
|
|
|
|
// Try to push the supplied buffer to the audio output device
|
|
rc = audoutAppendAudioOutBuffer(source);
|
|
|
|
if (R_SUCCEEDED(rc))
|
|
rc = audoutWaitPlayFinish(released, &released_count, UINT64_MAX);
|
|
|
|
return rc;
|
|
}
|
|
|
|
Result audoutListAudioOuts(char *DeviceNames, s32 count, u32 *DeviceNamesCount) {
|
|
bool new_cmd = hosversionAtLeast(3,0,0);
|
|
return serviceDispatchOut(&g_audoutSrv, new_cmd==0 ? 0 : 2, *DeviceNamesCount,
|
|
.buffer_attrs = { (new_cmd==0 ? SfBufferAttr_HipcMapAlias : SfBufferAttr_HipcAutoSelect) | SfBufferAttr_Out },
|
|
.buffers = { { DeviceNames, count*DEVICE_NAME_LENGTH } },
|
|
);
|
|
}
|
|
|
|
Result audoutOpenAudioOut(const char *DeviceNameIn, char *DeviceNameOut, u32 SampleRateIn, u32 ChannelCountIn, u32 *SampleRateOut, u32 *ChannelCountOut, PcmFormat *Format, AudioOutState *State) {
|
|
bool new_cmd = hosversionAtLeast(3,0,0);
|
|
|
|
const struct {
|
|
u32 sample_rate;
|
|
u32 channel_count;
|
|
u64 AppletResourceUserId;
|
|
} in = { SampleRateIn, ChannelCountIn, appletGetAppletResourceUserId() };
|
|
|
|
struct {
|
|
u32 sample_rate;
|
|
u32 channel_count;
|
|
u32 pcm_format;
|
|
u32 state;
|
|
} out;
|
|
|
|
u32 tmpattr = new_cmd==0 ? SfBufferAttr_HipcMapAlias : SfBufferAttr_HipcAutoSelect;
|
|
Result rc = serviceDispatchInOut(&g_audoutSrv, new_cmd==0 ? 1 : 3, in, out,
|
|
.buffer_attrs = {
|
|
tmpattr | SfBufferAttr_In,
|
|
tmpattr | SfBufferAttr_Out,
|
|
},
|
|
.buffers = {
|
|
{ DeviceNameIn, DEVICE_NAME_LENGTH },
|
|
{ DeviceNameOut, DEVICE_NAME_LENGTH },
|
|
},
|
|
.in_send_pid = true,
|
|
.in_num_handles = 1,
|
|
.in_handles = { CUR_PROCESS_HANDLE },
|
|
.out_num_objects = 1,
|
|
.out_objects = &g_audoutIAudioOut,
|
|
);
|
|
if (R_SUCCEEDED(rc)) {
|
|
if (SampleRateOut) *SampleRateOut = out.sample_rate;
|
|
if (ChannelCountOut) *ChannelCountOut = out.channel_count;
|
|
if (Format) *Format = out.pcm_format;
|
|
if (State) *State = out.state;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
Result audoutGetAudioOutState(AudioOutState *State) {
|
|
u32 tmp=0;
|
|
Result rc = _audoutCmdNoInOutU32(&g_audoutIAudioOut, &tmp, 0);
|
|
if (R_SUCCEEDED(rc) && State) *State = tmp;
|
|
return rc;
|
|
}
|
|
|
|
Result audoutStartAudioOut(void) {
|
|
return _audoutCmdNoIO(&g_audoutIAudioOut, 1);
|
|
}
|
|
|
|
Result audoutStopAudioOut(void) {
|
|
return _audoutCmdNoIO(&g_audoutIAudioOut, 2);
|
|
}
|
|
|
|
Result audoutAppendAudioOutBuffer(AudioOutBuffer *Buffer) {
|
|
bool new_cmd = hosversionAtLeast(3,0,0);
|
|
u64 tmp = (u64)Buffer;
|
|
return serviceDispatchIn(&g_audoutIAudioOut, new_cmd==0 ? 3 : 7, tmp,
|
|
.buffer_attrs = { (new_cmd==0 ? SfBufferAttr_HipcMapAlias : SfBufferAttr_HipcAutoSelect) | SfBufferAttr_In },
|
|
.buffers = { { Buffer, sizeof(*Buffer) } },
|
|
);
|
|
}
|
|
|
|
static Result _audoutRegisterBufferEvent(Event *BufferEvent) {
|
|
return _audoutCmdGetEvent(&g_audoutIAudioOut, BufferEvent, true, 4);
|
|
}
|
|
|
|
Result audoutGetReleasedAudioOutBuffer(AudioOutBuffer **Buffer, u32 *ReleasedBuffersCount) {
|
|
bool new_cmd = hosversionAtLeast(3,0,0);
|
|
return serviceDispatchOut(&g_audoutIAudioOut, new_cmd==0 ? 5 : 8, *ReleasedBuffersCount,
|
|
.buffer_attrs = { (new_cmd==0 ? SfBufferAttr_HipcMapAlias : SfBufferAttr_HipcAutoSelect) | SfBufferAttr_Out },
|
|
.buffers = { { Buffer, sizeof(*Buffer) } },
|
|
);
|
|
}
|
|
|
|
Result audoutContainsAudioOutBuffer(AudioOutBuffer *Buffer, bool *ContainsBuffer) {
|
|
u64 tmp = (u64)Buffer;
|
|
u8 out=0;
|
|
Result rc = serviceDispatchInOut(&g_audoutIAudioOut, 6, tmp, out);
|
|
if (R_SUCCEEDED(rc) && ContainsBuffer) *ContainsBuffer = out & 1;
|
|
return rc;
|
|
}
|
|
|
|
Result audoutGetAudioOutBufferCount(u32 *count) {
|
|
if (hosversionBefore(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
return serviceDispatchOut(&g_audoutIAudioOut, 9, *count);
|
|
}
|
|
|
|
Result audoutGetAudioOutPlayedSampleCount(u64 *count) {
|
|
if (hosversionBefore(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
return serviceDispatchOut(&g_audoutIAudioOut, 10, *count);
|
|
}
|
|
|
|
Result audoutFlushAudioOutBuffers(bool *flushed) {
|
|
if (hosversionBefore(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
u8 out=0;
|
|
Result rc = serviceDispatchOut(&g_audoutIAudioOut, 11, out);
|
|
if (R_SUCCEEDED(rc) && flushed) *flushed = out & 1;
|
|
return rc;
|
|
}
|
|
|
|
Result audoutSetAudioOutVolume(float volume) {
|
|
if (hosversionBefore(6,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
return serviceDispatchIn(&g_audoutIAudioOut, 12, volume);
|
|
}
|
|
|
|
Result audoutGetAudioOutVolume(float *volume) {
|
|
if (hosversionBefore(6,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
return serviceDispatchOut(&g_audoutIAudioOut, 13, *volume);
|
|
}
|
|
|
|
Result audoutaRequestSuspendOld(u64 pid, u64 delay, Handle* handle_out) {
|
|
if (hosversionAtLeast(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
const struct {
|
|
u64 pid;
|
|
u64 delay;
|
|
} in = { pid, delay };
|
|
|
|
return serviceDispatchInOut(&g_audoutaSrv, 0, in, *handle_out);
|
|
}
|
|
|
|
Result audoutaRequestResumeOld(u64 pid, u64 delay, Handle* handle_out) {
|
|
if (hosversionAtLeast(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
const struct {
|
|
u64 pid;
|
|
u64 delay;
|
|
} in = { pid, delay };
|
|
|
|
return serviceDispatchInOut(&g_audoutaSrv, 1, in, *handle_out);
|
|
}
|
|
|
|
Result audoutaRequestSuspend(u64 pid, u64 delay) {
|
|
if (hosversionBefore(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
const struct {
|
|
u64 pid;
|
|
u64 delay;
|
|
} in = { pid, delay };
|
|
|
|
return serviceDispatchIn(&g_audoutaSrv, 0, in);
|
|
}
|
|
|
|
Result audoutaRequestResume(u64 pid, u64 delay) {
|
|
if (hosversionBefore(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
const struct {
|
|
u64 pid;
|
|
u64 delay;
|
|
} in = { pid, delay };
|
|
|
|
return serviceDispatchIn(&g_audoutaSrv, 1, in);
|
|
}
|
|
|
|
Result audoutaGetProcessMasterVolume(u64 pid, float* volume_out) {
|
|
return serviceDispatchInOut(&g_audoutaSrv, 2, pid, *volume_out);
|
|
}
|
|
|
|
Result audoutaSetProcessMasterVolume(u64 pid, u64 delay, float volume) {
|
|
const struct {
|
|
float volume;
|
|
u64 pid;
|
|
u64 delay;
|
|
} in = { volume, pid, delay };
|
|
|
|
return serviceDispatchIn(&g_audoutaSrv, 3, in);
|
|
}
|
|
|
|
Result audoutaGetProcessRecordVolume(u64 pid, float* volume_out) {
|
|
if (hosversionBefore(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
return serviceDispatchInOut(&g_audoutaSrv, 4, pid, *volume_out);
|
|
}
|
|
|
|
Result audoutaSetProcessRecordVolume(u64 pid, u64 delay, float volume) {
|
|
if (hosversionBefore(4,0,0))
|
|
return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer);
|
|
|
|
const struct {
|
|
float volume;
|
|
u64 pid;
|
|
u64 delay;
|
|
} in = { volume, pid, delay };
|
|
|
|
return serviceDispatchIn(&g_audoutaSrv, 5, in);
|
|
}
|
|
|
|
Result audoutdRequestSuspendForDebug(u64 pid, u64 delay) {
|
|
const struct {
|
|
u64 pid;
|
|
u64 delay;
|
|
} in = { pid, delay };
|
|
|
|
return serviceDispatchIn(&g_audoutdSrv, 0, in);
|
|
}
|
|
|
|
Result audoutdRequestResumeForDebug(u64 pid, u64 delay) {
|
|
const struct {
|
|
u64 pid;
|
|
u64 delay;
|
|
} in = { pid, delay };
|
|
|
|
return serviceDispatchIn(&g_audoutdSrv, 1, in);
|
|
}
|