diff options
author | Paul Adenot <[email protected]> | 2023-08-17 17:09:14 +0200 |
---|---|---|
committer | Paul Adenot <[email protected]> | 2023-08-25 20:09:58 +0200 |
commit | 33ade008b5befa3a56871e61078d5a5d923bcbf8 (patch) | |
tree | 8915e355e16088197747e9659234f42218299913 | |
parent | ac8474a5929e9de3bce84f16f8c589240eb9f7c4 (diff) | |
download | cubeb-33ade008b5befa3a56871e61078d5a5d923bcbf8.tar.gz cubeb-33ade008b5befa3a56871e61078d5a5d923bcbf8.zip |
OpenSLES: compile as C++
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/android/cubeb-output-latency.h | 6 | ||||
-rw-r--r-- | src/android/cubeb_media_library.h | 22 | ||||
-rw-r--r-- | src/cubeb-jni.h | 8 | ||||
-rw-r--r-- | src/cubeb_opensl.cpp (renamed from src/cubeb_opensl.c) | 31 |
5 files changed, 45 insertions, 24 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index dcbc196..e10a99f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -277,7 +277,7 @@ endif() check_include_files(SLES/OpenSLES.h USE_OPENSL) if(USE_OPENSL) target_sources(cubeb PRIVATE - src/cubeb_opensl.c + src/cubeb_opensl.cpp src/cubeb-jni.cpp) target_compile_definitions(cubeb PRIVATE USE_OPENSL) target_link_libraries(cubeb PRIVATE OpenSLES) diff --git a/src/android/cubeb-output-latency.h b/src/android/cubeb-output-latency.h index 870a884..f8ee0e2 100644 --- a/src/android/cubeb-output-latency.h +++ b/src/android/cubeb-output-latency.h @@ -19,7 +19,7 @@ output_latency_function * cubeb_output_latency_load_method(int version) { output_latency_function * ol = NULL; - ol = calloc(1, sizeof(output_latency_function)); + ol = (output_latency_function*) calloc(1, sizeof(output_latency_function)); ol->version = version; @@ -61,6 +61,8 @@ cubeb_output_latency_unload_method(output_latency_function * ol) free(ol); } +extern "C" { + uint32_t cubeb_get_output_latency(output_latency_function * ol) { @@ -73,4 +75,6 @@ cubeb_get_output_latency(output_latency_function * ol) return cubeb_get_output_latency_from_media_library(ol->from_lib); } +} + #endif // _CUBEB_OUTPUT_LATENCY_H_ diff --git a/src/android/cubeb_media_library.h b/src/android/cubeb_media_library.h index 27fbc86..c7aecc5 100644 --- a/src/android/cubeb_media_library.h +++ b/src/android/cubeb_media_library.h @@ -1,9 +1,15 @@ +#include <dlfcn.h> +#include <stdlib.h> +#include <cassert> +#include <cstdint> #ifndef _CUBEB_MEDIA_LIBRARY_H_ #define _CUBEB_MEDIA_LIBRARY_H_ +typedef int32_t (*get_output_latency_ptr)(uint32_t * latency, int stream_type); + struct media_lib { void * libmedia; - int32_t (*get_output_latency)(uint32_t * latency, int stream_type); + get_output_latency_ptr get_output_latency; }; typedef struct media_lib media_lib; @@ -11,30 +17,30 @@ typedef struct media_lib media_lib; media_lib * cubeb_load_media_library() { - media_lib ml = {0}; + media_lib ml = {}; ml.libmedia = dlopen("libmedia.so", RTLD_LAZY); if (!ml.libmedia) { - return NULL; + return nullptr; } // Get the latency, in ms, from AudioFlinger. First, try the most recent // signature. status_t AudioSystem::getOutputLatency(uint32_t* latency, // audio_stream_type_t streamType) - ml.get_output_latency = dlsym( + ml.get_output_latency = (get_output_latency_ptr)dlsym( ml.libmedia, "_ZN7android11AudioSystem16getOutputLatencyEPj19audio_stream_type_t"); if (!ml.get_output_latency) { // In case of failure, try the signature from legacy version. // status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType) ml.get_output_latency = - dlsym(ml.libmedia, "_ZN7android11AudioSystem16getOutputLatencyEPji"); + (get_output_latency_ptr)dlsym(ml.libmedia, "_ZN7android11AudioSystem16getOutputLatencyEPji"); if (!ml.get_output_latency) { - return NULL; + return nullptr; } } - media_lib * rv = NULL; - rv = calloc(1, sizeof(media_lib)); + media_lib * rv = nullptr; + rv = (media_lib*) calloc(1, sizeof(media_lib)); assert(rv); *rv = ml; return rv; diff --git a/src/cubeb-jni.h b/src/cubeb-jni.h index c4a712a..d63629f 100644 --- a/src/cubeb-jni.h +++ b/src/cubeb-jni.h @@ -3,6 +3,10 @@ typedef struct cubeb_jni cubeb_jni; +#ifdef __cplusplus +extern "C" { +#endif + cubeb_jni * cubeb_jni_init(); int @@ -10,4 +14,8 @@ cubeb_get_output_latency_from_jni(cubeb_jni * cubeb_jni_ptr); void cubeb_jni_destroy(cubeb_jni * cubeb_jni_ptr); +#ifdef __cplusplus +}; +#endif + #endif // _CUBEB_JNI_H_ diff --git a/src/cubeb_opensl.c b/src/cubeb_opensl.cpp index 7165446..d44f86c 100644 --- a/src/cubeb_opensl.c +++ b/src/cubeb_opensl.cpp @@ -74,7 +74,7 @@ #define DEFAULT_SAMPLE_RATE 48000 #define DEFAULT_NUM_OF_FRAMES 480 -static struct cubeb_ops const opensl_ops; +extern cubeb_ops const opensl_ops; struct cubeb { struct cubeb_ops const * ops; @@ -255,7 +255,7 @@ opensl_set_shutdown(cubeb_stream * stm, uint32_t value) static void play_callback(SLPlayItf caller, void * user_ptr, SLuint32 event) { - cubeb_stream * stm = user_ptr; + cubeb_stream * stm = static_cast<cubeb_stream*>(user_ptr); assert(stm); switch (event) { case SL_PLAYEVENT_HEADATMARKER: @@ -269,7 +269,7 @@ play_callback(SLPlayItf caller, void * user_ptr, SLuint32 event) static void recorder_marker_callback(SLRecordItf caller, void * pContext, SLuint32 event) { - cubeb_stream * stm = pContext; + cubeb_stream * stm = static_cast<cubeb_stream*>(pContext); assert(stm); if (event == SL_RECORDEVENT_HEADATMARKER) { @@ -295,7 +295,7 @@ recorder_marker_callback(SLRecordItf caller, void * pContext, SLuint32 event) static void bufferqueue_callback(SLBufferQueueItf caller, void * user_ptr) { - cubeb_stream * stm = user_ptr; + cubeb_stream * stm = static_cast<cubeb_stream*>(user_ptr); assert(stm); SLBufferQueueState state; SLresult res; @@ -308,7 +308,7 @@ bufferqueue_callback(SLBufferQueueItf caller, void * user_ptr) return; } - uint8_t * buf = stm->queuebuf[stm->queuebuf_idx]; + uint8_t * buf = reinterpret_cast<uint8_t*>(stm->queuebuf[stm->queuebuf_idx]); written = 0; int r = pthread_mutex_lock(&stm->mutex); assert(r == 0); @@ -319,6 +319,7 @@ bufferqueue_callback(SLBufferQueueItf caller, void * user_ptr) if (!draining && !shutdown) { written = cubeb_resampler_fill(stm->resampler, NULL, NULL, buf, stm->queuebuf_len / stm->framesize); + LOG("bufferqueue_callback: resampler fill returned %ld frames", written); if (written < 0 || written * stm->framesize > stm->queuebuf_len) { r = pthread_mutex_lock(&stm->mutex); @@ -411,7 +412,7 @@ void recorder_callback(SLAndroidSimpleBufferQueueItf bq, void * context) { assert(context); - cubeb_stream * stm = context; + cubeb_stream * stm = static_cast<cubeb_stream*>(context); assert(stm->recorderBufferQueueItf); int r = pthread_mutex_lock(&stm->mutex); @@ -473,7 +474,7 @@ void recorder_fullduplex_callback(SLAndroidSimpleBufferQueueItf bq, void * context) { assert(context); - cubeb_stream * stm = context; + cubeb_stream * stm = static_cast<cubeb_stream*>(context); assert(stm->recorderBufferQueueItf); int r = pthread_mutex_lock(&stm->mutex); @@ -516,7 +517,7 @@ static void player_fullduplex_callback(SLBufferQueueItf caller, void * user_ptr) { TIMESTAMP("ENTER"); - cubeb_stream * stm = user_ptr; + cubeb_stream * stm = static_cast<cubeb_stream*>(user_ptr); assert(stm); SLresult res; @@ -665,7 +666,8 @@ get_android_version(void) } #endif -/*static*/ int +extern "C" { +int opensl_init(cubeb ** context, char const * context_name) { cubeb * ctx; @@ -681,7 +683,7 @@ opensl_init(cubeb ** context, char const * context_name) *context = NULL; - ctx = calloc(1, sizeof(*ctx)); + ctx = static_cast<cubeb*>(calloc(1, sizeof(*ctx))); assert(ctx); ctx->ops = &opensl_ops; @@ -773,6 +775,7 @@ opensl_init(cubeb ** context, char const * context_name) LOG("Cubeb init (%p) success", ctx); return CUBEB_OK; } +} static char const * opensl_get_backend_id(cubeb * ctx) @@ -1332,7 +1335,7 @@ opensl_configure_playback(cubeb_stream * stm, cubeb_stream_params * params) // will be consumed and kick off the buffer queue callback. // Note the duration of a single frame is less than 1ms. We don't bother // adjusting the playback position. - uint8_t * buf = stm->queuebuf[stm->queuebuf_idx++]; + uint8_t * buf = reinterpret_cast<uint8_t*>(stm->queuebuf[stm->queuebuf_idx++]); memset(buf, 0, stm->framesize); res = (*stm->bufq)->Enqueue(stm->bufq, buf, stm->framesize); assert(res == SL_RESULT_SUCCESS); @@ -1374,7 +1377,7 @@ opensl_stream_init(cubeb * ctx, cubeb_stream ** stream, cubeb_data_callback data_callback, cubeb_state_callback state_callback, void * user_ptr) { - cubeb_stream * stm; + cubeb_stream * stm = nullptr; assert(ctx); if (input_device || output_device) { @@ -1395,7 +1398,7 @@ opensl_stream_init(cubeb * ctx, cubeb_stream ** stream, return r; } - stm = calloc(1, sizeof(*stm)); + stm = reinterpret_cast<cubeb_stream*>(calloc(1, sizeof(*stm))); assert(stm); stm->context = ctx; @@ -1778,7 +1781,7 @@ opensl_stream_set_volume(cubeb_stream * stm, float volume) return CUBEB_OK; } -static struct cubeb_ops const opensl_ops = { +struct cubeb_ops const opensl_ops = { .init = opensl_init, .get_backend_id = opensl_get_backend_id, .get_max_channel_count = opensl_get_max_channel_count, |