feat: add oss audio callback for FreeBSD

OSS is the native sound system of the FreeBSD.It will be used on the x11_vaapi platform.It was added to reduce moonlight's dependency on FreeBSD.
This commit is contained in:
armin-25689 2023-12-31 15:14:22 +08:00 committed by Cameron Gutman
parent 3b72f5195b
commit 3840b1409e
4 changed files with 118 additions and 0 deletions

View File

@ -87,6 +87,13 @@ add_executable(moonlight ${SRC_LIST})
target_link_libraries(moonlight m)
target_link_libraries(moonlight gamestream)
if (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
set(ALSA_FOUND FALSE)
set(PULSE_FOUND FALSE)
set(CEC_FOUND FALSE)
target_sources(moonlight PRIVATE ./src/audio/oss.c)
endif()
if (CEC_FOUND)
list(APPEND MOONLIGHT_DEFINITIONS HAVE_LIBCEC)
list(APPEND MOONLIGHT_OPTIONS CEC)

View File

@ -31,3 +31,6 @@ extern AUDIO_RENDERER_CALLBACKS audio_callbacks_sdl;
extern AUDIO_RENDERER_CALLBACKS audio_callbacks_pulse;
bool audio_pulse_init(char* audio_device);
#endif
#ifdef __FreeBSD__
extern AUDIO_RENDERER_CALLBACKS audio_callbacks_oss;
#endif

105
src/audio/oss.c Normal file
View File

@ -0,0 +1,105 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015-2017 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef __FreeBSD__
#include <sys/soundcard.h>
#include "audio.h"
#include <opus_multistream.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
static OpusMSDecoder* decoder;
static short* pcmBuffer;
static int samplesPerFrame;
static int channelCount;
static int fd;
static int oss_renderer_init(int audioConfiguration, POPUS_MULTISTREAM_CONFIGURATION opusConfig, void* context, int arFlags) {
int rc;
decoder = opus_multistream_decoder_create(opusConfig->sampleRate, opusConfig->channelCount, opusConfig->streams, opusConfig->coupledStreams, opusConfig->mapping, &rc);
channelCount = opusConfig->channelCount;
samplesPerFrame = opusConfig->samplesPerFrame;
pcmBuffer = malloc(sizeof(short) * channelCount * samplesPerFrame);
if (pcmBuffer == NULL)
return -1;
const char* oss_name = "/dev/dsp";
fd = open(oss_name, O_WRONLY);
// buffer size for fragment ,selector 12 is 4096;11 is 2048;10 is 1024; 13is 8192
if (fd == -1) {
close(fd);
printf("Open audio device /dev/dsp faild!!!");
return -1;
}
int frag = 12;
if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag) == -1)
printf("Set framgment for /dev/dsp faild.");
int format = AFMT_S16_LE;
int channels = opusConfig->channelCount;
int rate = opusConfig->sampleRate;
if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) == -1)
printf("Set framgment for /dev/dsp faild.");
if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) == -1)
printf("Set channels for /dev/dsp faild.");
if (ioctl(fd, SNDCTL_DSP_SPEED, &rate) == -1)
printf("Set sameple rate for /dev/dsp faild.");
return 0;
}
static void oss_renderer_cleanup() {
if (decoder != NULL) {
opus_multistream_decoder_destroy(decoder);
decoder = NULL;
}
if (pcmBuffer != NULL) {
free(pcmBuffer);
pcmBuffer = NULL;
}
if (fd != 0) {
close(fd);
fd = 0;
}
}
static void oss_renderer_decode_and_play_sample(char* data, int length) {
int decodeLen = opus_multistream_decode(decoder, data, length, pcmBuffer, samplesPerFrame, 0);
if (decodeLen > 0) {
write(fd, pcmBuffer, decodeLen * channelCount * sizeof(short));
} else {
printf("Opus error from decode: %d\n", decodeLen);
}
}
AUDIO_RENDERER_CALLBACKS audio_callbacks_oss = {
.init = oss_renderer_init,
.cleanup = oss_renderer_cleanup,
.decodeAndPlaySample = oss_renderer_decode_and_play_sample,
.capabilities = CAPABILITY_DIRECT_SUBMIT | CAPABILITY_SUPPORTS_ARBITRARY_AUDIO_DURATION,
};
#endif

View File

@ -202,6 +202,9 @@ AUDIO_RENDERER_CALLBACKS* platform_get_audio(enum platform system, char* audio_d
#ifdef HAVE_ALSA
return &audio_callbacks_alsa;
#endif
#ifdef __FreeBSD__
return &audio_callbacks_oss;
#endif
}
return NULL;
}