diff --git a/CMakeLists.txt b/CMakeLists.txt index a39df1e..a459e06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ pkg_check_modules(AVUTIL libavutil) pkg_check_modules(SWSCALE libswscale) pkg_check_modules(XLIB x11-xcb) pkg_check_modules(LIBVA vdpau) +pkg_check_modules(PULSE libpulse-simple) if(AVCODEC_FOUND AND AVUTIL_FOUND AND SWSCALE_FOUND AND SDL_FOUND) set(SOFTWARE_FOUND TRUE) @@ -67,6 +68,12 @@ if (BROADCOM_FOUND OR FREESCALE_FOUND OR CMAKE_BUILD_TYPE MATCHES Debug) list(APPEND MOONLIGHT_OPTIONS EMBEDDED) endif() +if (PULSE_FOUND) + list(APPEND SRC_LIST ./src/audio/pulse.c) + list(APPEND MOONLIGHT_DEFINITIONS HAVE_PULSE) + list(APPEND MOONLIGHT_OPTIONS PULSE) +endif() + configure_file("./src/configuration.h.in" "${PROJECT_BINARY_DIR}/configuration.h") include_directories("${PROJECT_BINARY_DIR}") @@ -115,6 +122,11 @@ if (SOFTWARE_FOUND) endif() endif() +if (PULSE_FOUND) + target_include_directories(moonlight PRIVATE ${PULSE_INCLUDE_DIRS}) + target_link_libraries(moonlight ${PULSE_LIBRARIES}) +endif() + set_property(TARGET moonlight PROPERTY COMPILE_DEFINITIONS ${MOONLIGHT_DEFINITIONS}) target_include_directories(moonlight PRIVATE ${GAMESTREAM_INCLUDE_DIR} ${MOONLIGHT_COMMON_INCLUDE_DIR} ${OPUS_INCLUDE_DIRS} ${EVDEV_INCLUDE_DIRS} ${UDEV_INCLUDE_DIRS}) target_link_libraries(moonlight ${EVDEV_LIBRARIES} ${ALSA_LIBRARY} ${OPUS_LIBRARY} ${UDEV_LIBRARIES} ${CMAKE_DL_LIBS}) diff --git a/src/audio.h b/src/audio.h index e41a738..229d7bd 100644 --- a/src/audio.h +++ b/src/audio.h @@ -24,4 +24,7 @@ extern const char* audio_device; extern AUDIO_RENDERER_CALLBACKS audio_callbacks_alsa; #ifdef HAVE_SDL extern AUDIO_RENDERER_CALLBACKS audio_callbacks_sdl; -#endif \ No newline at end of file +#endif +#ifdef HAVE_PULSE +extern AUDIO_RENDERER_CALLBACKS audio_callbacks_pulse; +#endif diff --git a/src/audio/pulse.c b/src/audio/pulse.c new file mode 100644 index 0000000..af9a1e9 --- /dev/null +++ b/src/audio/pulse.c @@ -0,0 +1,99 @@ +/* + * This file is part of Moonlight Embedded. + * + * Copyright (C) 2015 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 . + */ + +#include "../audio.h" + +#include +#include + +#include +#include +#include + +#define MAX_CHANNEL_COUNT 6 +#define FRAME_SIZE 240 + +static OpusMSDecoder* decoder; +static pa_simple *dev = NULL; +static short pcmBuffer[FRAME_SIZE * MAX_CHANNEL_COUNT]; +static int channelCount; + +static void pulse_renderer_init(int audioConfiguration, POPUS_MULTISTREAM_CONFIGURATION opusConfig) { + int rc, error; + unsigned char alsaMapping[6]; + + channelCount = opusConfig->channelCount; + + /* The supplied mapping array has order: FL-FR-C-LFE-RL-RR + * ALSA expects the order: FL-FR-RL-RR-C-LFE + * We need copy the mapping locally and swap the channels around. + */ + alsaMapping[0] = opusConfig->mapping[0]; + alsaMapping[1] = opusConfig->mapping[1]; + if (opusConfig->channelCount == 6) { + alsaMapping[2] = opusConfig->mapping[4]; + alsaMapping[3] = opusConfig->mapping[5]; + alsaMapping[4] = opusConfig->mapping[2]; + alsaMapping[5] = opusConfig->mapping[3]; + } + + decoder = opus_multistream_decoder_create(opusConfig->sampleRate, + opusConfig->channelCount, + opusConfig->streams, + opusConfig->coupledStreams, + alsaMapping, + &rc); + + pa_sample_spec spec = { + .format = PA_SAMPLE_S16LE, + .rate = opusConfig->sampleRate, + .channels = opusConfig->channelCount + }; + + dev = pa_simple_new(NULL, "Moonlight Embedded", PA_STREAM_PLAYBACK, NULL, "Streaming", &spec, NULL, NULL, &error); + + if (!dev) { + printf("Pulseaudio error: %s\n", pa_strerror(error)); + exit(-1); + } +} + +static void pulse_renderer_decode_and_play_sample(char* data, int length) { + int decodeLen = opus_multistream_decode(decoder, data, length, pcmBuffer, FRAME_SIZE, 0); + if (decodeLen > 0) { + int error; + int rc = pa_simple_write(dev, pcmBuffer, decodeLen * sizeof(short) * channelCount, &error); + + if (rc<0) + printf("Pulseaudio error: %s\n", pa_strerror(error)); + } else { + printf("Opus error from decode: %d\n", decodeLen); + } +} + +static void pulse_renderer_cleanup() { + pa_simple_free(dev); +} + +AUDIO_RENDERER_CALLBACKS audio_callbacks_pulse = { + .init = pulse_renderer_init, + .cleanup = pulse_renderer_cleanup, + .decodeAndPlaySample = pulse_renderer_decode_and_play_sample, + .capabilities = CAPABILITY_DIRECT_SUBMIT, +}; diff --git a/src/platform.c b/src/platform.c index 50b91b2..2278042 100644 --- a/src/platform.c +++ b/src/platform.c @@ -88,6 +88,9 @@ AUDIO_RENDERER_CALLBACKS* platform_get_audio(enum platform system) { return &audio_callbacks_sdl; #endif default: + #ifdef HAVE_PULSE + return &audio_callbacks_pulse; + #endif return &audio_callbacks_alsa; } return NULL;