| 1 |
#include <cassert> |
| 2 |
#include <cstdint> |
| 3 |
#include <dlfcn.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#ifndef _CUBEB_MEDIA_LIBRARY_H_ |
| 6 |
#define _CUBEB_MEDIA_LIBRARY_H_ |
| 7 |
|
| 8 |
typedef int32_t (*get_output_latency_ptr)(uint32_t * latency, int stream_type); |
| 9 |
|
| 10 |
struct media_lib { |
| 11 |
void * libmedia; |
| 12 |
get_output_latency_ptr get_output_latency; |
| 13 |
}; |
| 14 |
|
| 15 |
typedef struct media_lib media_lib; |
| 16 |
|
| 17 |
media_lib * |
| 18 |
cubeb_load_media_library() |
| 19 |
{ |
| 20 |
media_lib ml = {}; |
| 21 |
ml.libmedia = dlopen("libmedia.so", RTLD_LAZY); |
| 22 |
if (!ml.libmedia) { |
| 23 |
return nullptr; |
| 24 |
} |
| 25 |
|
| 26 |
// Get the latency, in ms, from AudioFlinger. First, try the most recent |
| 27 |
// signature. status_t AudioSystem::getOutputLatency(uint32_t* latency, |
| 28 |
// audio_stream_type_t streamType) |
| 29 |
ml.get_output_latency = (get_output_latency_ptr)dlsym( |
| 30 |
ml.libmedia, |
| 31 |
"_ZN7android11AudioSystem16getOutputLatencyEPj19audio_stream_type_t"); |
| 32 |
if (!ml.get_output_latency) { |
| 33 |
// In case of failure, try the signature from legacy version. |
| 34 |
// status_t AudioSystem::getOutputLatency(uint32_t* latency, int streamType) |
| 35 |
ml.get_output_latency = (get_output_latency_ptr)dlsym( |
| 36 |
ml.libmedia, "_ZN7android11AudioSystem16getOutputLatencyEPji"); |
| 37 |
if (!ml.get_output_latency) { |
| 38 |
return nullptr; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
media_lib * rv = nullptr; |
| 43 |
rv = (media_lib *)calloc(1, sizeof(media_lib)); |
| 44 |
assert(rv); |
| 45 |
*rv = ml; |
| 46 |
return rv; |
| 47 |
} |
| 48 |
|
| 49 |
void |
| 50 |
cubeb_close_media_library(media_lib * ml) |
| 51 |
{ |
| 52 |
dlclose(ml->libmedia); |
| 53 |
ml->libmedia = NULL; |
| 54 |
ml->get_output_latency = NULL; |
| 55 |
free(ml); |
| 56 |
} |
| 57 |
|
| 58 |
uint32_t |
| 59 |
cubeb_get_output_latency_from_media_library(media_lib * ml) |
| 60 |
{ |
| 61 |
uint32_t latency = 0; |
| 62 |
const int audio_stream_type_music = 3; |
| 63 |
int32_t r = ml->get_output_latency(&latency, audio_stream_type_music); |
| 64 |
if (r) { |
| 65 |
return 0; |
| 66 |
} |
| 67 |
return latency; |
| 68 |
} |
| 69 |
|
| 70 |
#endif // _CUBEB_MEDIA_LIBRARY_H_ |