I'm working on a native shared library that will be built inside aosp source tree, in this path "sdk-version/external"
in this shared library i'm trying to use audio flinger to use some of its function so i can capture audio buffer or track that will played in android system on device.
how can i use the audioflinger inside my shared library
i tried doing this
#include <system/audio.h>
#include <media/AudioSystem.h>
#include <media/AudioFlinger.h>
but i got this error
i also tried using the audio flinger with binder since audio flinger is part of ipc mechanism like this
// Get a reference to the AudioFlinger service using the IServiceManager
android::sp<android::IBinder> binder =
android::defaultServiceManager()->getService(android::String16("media.audio_flinger"));
if (binder != nullptr) {
gAudioFlinger = android::interface_cast<android::IAudioFlinger>(binder);
}
i got this error
Keep in mind that Android native dev is analogous to Linux native dev if you new to it, though be aware that Google likes to break things.
If you are going to use Android Binder to do IPC on Android, you most likely want to use AIDL/HIDL instead of doing everything manually. AIDL/HIDL are just source code generation mechanisms, and the generated code can be found in out/soong/.intermediates.
In your case, you want to call AudioFlinger's Android Binder interface, in other words, you want to use the interface defined in IAudioFlignerService.aidl, so your code should look like this if you are using the cpp backend.
Okay, where does the compiler find android/media/IAudioPolicyService.h and since
interface_cast
is a template function that just callsasInterface
of the type argument type, where can it find the definition and declaration of IAudioFlingerService?Well the source code for the definition and declaration for
IAudioFlingerService
is in out/soong/.intermediates/frameworks/av/media/libaudioclient/audioflinger-aidl-${cpp_or_ndk}-source, and in the AOSP source tree the generated AIDL/HIDL code are generally compiled into (shared) libraries and linked to form an ELF, so the compiler needs to know the location of the libraries too. They are in out/soong/.intermediates/frameworks/av/media/libaudioclient/audioflinger-aidl-${cpp_or_ndk}.In your Android.bp or Android.mk file, just include audioflinger-aidl-${cpp_or_ndk} as a shared library dependency and things should be fine at build time. At runtime, you may run into /dev/binder issues and you WILL runinto selinux issues. Once those are fixed, you still have to face this on Android 13: