Modernize Oboe driver

This commit is contained in:
derselbst 2021-01-13 19:02:13 +01:00
parent ca6bcda7d9
commit 2cfd56bb10
2 changed files with 39 additions and 49 deletions

View file

@ -592,7 +592,7 @@ else(NOT enable-pkgconfig)
if ( enable-oboe )
pkg_check_modules ( OBOE oboe-1.0 )
if ( OBOE_FOUND )
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set ( OBOE_SUPPORT 1 )
set ( OBOE_LIBS ${OBOE_LIBRARIES} )

View file

@ -22,6 +22,7 @@
*
* Audio driver for Android Oboe.
*
* This file may make use of C++14, because it's required by oboe anyway.
*/
extern "C" {
@ -37,7 +38,7 @@ extern "C" {
using namespace oboe;
static const int NUM_CHANNELS = 2;
constexpr int NUM_CHANNELS = 2;
class OboeAudioStreamCallback;
@ -49,10 +50,10 @@ class OboeAudioStreamCallback;
typedef struct
{
fluid_audio_driver_t driver;
fluid_synth_t *synth;
int cont;
OboeAudioStreamCallback *oboe_callback;
AudioStream *stream;
fluid_synth_t *synth = nullptr;
bool cont = false;
std::unique_ptr<OboeAudioStreamCallback> oboe_callback;
std::shared_ptr<AudioStream> stream;
} fluid_oboe_audio_driver_t;
@ -111,39 +112,25 @@ void fluid_oboe_audio_driver_settings(fluid_settings_t *settings)
fluid_audio_driver_t *
new_fluid_oboe_audio_driver(fluid_settings_t *settings, fluid_synth_t *synth)
{
Result result;
fluid_oboe_audio_driver_t *dev;
AudioStreamBuilder builder_obj;
AudioStreamBuilder *builder = &builder_obj;
AudioStream *stream;
int period_frames;
double sample_rate;
int is_sample_format_float;
int device_id;
int sharing_mode; // 0: Shared, 1: Exclusive
int performance_mode; // 0: None, 1: PowerSaving, 2: LowLatency
fluid_oboe_audio_driver_t *dev = nullptr;
try
{
dev = FLUID_NEW(fluid_oboe_audio_driver_t);
Result result;
AudioStreamBuilder builder_obj;
AudioStreamBuilder *builder = &builder_obj;
if(dev == NULL)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
return NULL;
}
int period_frames;
double sample_rate;
int is_sample_format_float;
int device_id;
int sharing_mode; // 0: Shared, 1: Exclusive
int performance_mode; // 0: None, 1: PowerSaving, 2: LowLatency
FLUID_MEMSET(dev, 0, sizeof(fluid_oboe_audio_driver_t));
dev = new fluid_oboe_audio_driver_t();
dev->synth = synth;
dev->oboe_callback = new(std::nothrow) OboeAudioStreamCallback(dev);
if(!dev->oboe_callback)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
goto error_recovery;
}
dev->oboe_callback = std::make_unique<OboeAudioStreamCallback>(dev);
fluid_settings_getint(settings, "audio.period-size", &period_frames);
fluid_settings_getnum(settings, "synth.sample-rate", &sample_rate);
@ -167,28 +154,33 @@ new_fluid_oboe_audio_driver(fluid_settings_t *settings, fluid_synth_t *synth)
performance_mode == 2 ? PerformanceMode::LowLatency : PerformanceMode::None)
->setUsage(Usage::Media)
->setContentType(ContentType::Music)
->setCallback(dev->oboe_callback);
->setCallback(dev->oboe_callback.get());
result = builder->openStream(dev->stream);
result = builder->openStream(&stream);
if(result != Result::OK)
{
FLUID_LOG(FLUID_ERR, "Unable to open Oboe audio stream");
goto error_recovery;
}
dev->stream = stream;
dev->cont = 1;
dev->cont = true;
FLUID_LOG(FLUID_INFO, "Using Oboe driver");
result = stream->start();
result = dev->stream->start();
if(result != Result::OK)
{
FLUID_LOG(FLUID_ERR, "Unable to start Oboe audio stream");
goto error_recovery;
}
return reinterpret_cast<fluid_audio_driver_t *>(dev);
return &dev->driver;
}
catch(const std::bad_alloc &)
{
FLUID_LOG(FLUID_ERR, "Out of memory");
}
catch(...)
{
@ -197,33 +189,31 @@ new_fluid_oboe_audio_driver(fluid_settings_t *settings, fluid_synth_t *synth)
error_recovery:
delete_fluid_oboe_audio_driver(reinterpret_cast<fluid_audio_driver_t *>(dev));
return NULL;
return nullptr;
}
void delete_fluid_oboe_audio_driver(fluid_audio_driver_t *p)
{
fluid_oboe_audio_driver_t *dev = reinterpret_cast<fluid_oboe_audio_driver_t *>(p);
fluid_return_if_fail(dev != NULL);
fluid_return_if_fail(dev != nullptr);
try
{
dev->cont = 0;
dev->cont = false;
if(dev->stream != NULL)
if(dev->stream != nullptr)
{
dev->stream->stop();
dev->stream->close();
}
}
catch(...) {}
catch(...)
{
FLUID_LOG(FLUID_ERR, "Exception caught while stopping and closing Oboe stream.");
}
// the audio stream is silently allocated with new, but neither the API docs nor code examples mention that it should be deleted
delete dev->stream;
delete dev->oboe_callback;
FLUID_FREE(dev);
delete dev;
}
#endif // OBOE_SUPPORT