Loading media/libaudioclient/Android.bp +12 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ cc_library_shared { "AudioVolumeGroup.cpp", ], shared_libs: [ "capture_state_listener-aidl-cpp", "libaudiofoundation", "libaudioutils", "libbinder", Loading @@ -34,6 +35,9 @@ cc_library_shared { ], include_dirs: ["system/media/audio_utils/include"], export_include_dirs: ["include"], export_shared_lib_headers: [ "capture_state_listener-aidl-cpp", ], } cc_library_shared { Loading Loading @@ -148,3 +152,11 @@ filegroup { ], path: "aidl", } aidl_interface { name: "capture_state_listener-aidl", local_include_dir: "aidl", srcs: [ "aidl/android/media/ICaptureStateListener.aidl", ], } media/libaudioclient/aidl/android/media/ICaptureStateListener.aidl 0 → 100644 +21 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.media; interface ICaptureStateListener { void setCaptureState(boolean active); } services/audiopolicy/service/Android.mk +2 −1 Original line number Diff line number Diff line Loading @@ -6,7 +6,8 @@ LOCAL_SRC_FILES:= \ AudioPolicyService.cpp \ AudioPolicyEffects.cpp \ AudioPolicyInterfaceImpl.cpp \ AudioPolicyClientImpl.cpp AudioPolicyClientImpl.cpp \ CaptureStateNotifier.cpp LOCAL_C_INCLUDES := \ frameworks/av/services/audioflinger \ Loading services/audiopolicy/service/CaptureStateNotifier.cpp 0 → 100644 +58 −0 Original line number Diff line number Diff line #include "CaptureStateNotifier.h" #define LOG_TAG "CaptureStateNotifier" #include <android/media/ICaptureStateListener.h> #include <binder/IBinder.h> #include <utils/Log.h> namespace android { using media::ICaptureStateListener; class CaptureStateNotifier::DeathRecipient : public IBinder::DeathRecipient { public: DeathRecipient(CaptureStateNotifier* notifier) : mNotifier(notifier) {} void binderDied(const wp<IBinder>&) override { mNotifier->binderDied(); } private: CaptureStateNotifier* const mNotifier; }; CaptureStateNotifier::CaptureStateNotifier(bool initialActive) { mActive = initialActive; } CaptureStateNotifier::~CaptureStateNotifier() { LOG_ALWAYS_FATAL_IF(mListener != nullptr); } bool CaptureStateNotifier::RegisterListener(const sp<ICaptureStateListener>& listener) { std::lock_guard<std::mutex> _l(mMutex); LOG_ALWAYS_FATAL_IF(mListener != nullptr); ALOGI("Registering a listener"); mListener = listener; sp<IBinder> binder = IInterface::asBinder(mListener); binder->linkToDeath(new DeathRecipient(this)); return mActive; } void CaptureStateNotifier::setCaptureState(bool active) { std::lock_guard<std::mutex> _l(mMutex); mActive = active; if (mListener) { mListener->setCaptureState(active); } } void CaptureStateNotifier::binderDied() { std::lock_guard<std::mutex> _l(mMutex); mListener.clear(); ALOGI("Listener binder died"); } } // namespace android No newline at end of file services/audiopolicy/service/CaptureStateNotifier.h 0 → 100644 +73 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <mutex> #include <utils/StrongPointer.h> namespace android { namespace media { // Must be pre-declared, or else there isn't a good way to generate a header // library. class ICaptureStateListener; } // A utility for managing capture state change notifications. // // We are making some strong assumptions, for the sake of simplicity: // - There is no way to explicitly unregister listeners. The only way for a // listener to unregister is by dying. // - There's only at most one listener at a given time. Attempting to register // a second listener will cause a crash. // - This class isn't really meant to ever be destroyed. We expose a destructor // because it is convenient to use this class as a global instance or a member // of another class, but it will crash if destroyed while a listener is // registered. // // All of these assumptions can be lifted if there is ever a need. // // This class is thread-safe. class CaptureStateNotifier { public: // Ctor. // Accepts the initial active state. explicit CaptureStateNotifier(bool initialActive); // Register a listener to be notified of state changes. // The current state is returned and from that point on any change will be // notified of. bool RegisterListener(const sp<media::ICaptureStateListener>& listener); // Change the current capture state. // Active means "actively capturing". void setCaptureState(bool active); // Dtor. Do not actually call at runtime. Will cause a crash if a listener // is registered. ~CaptureStateNotifier(); private: std::mutex mMutex; sp<media::ICaptureStateListener> mListener; bool mActive; class DeathRecipient; void binderDied(); }; } // namespace android Loading
media/libaudioclient/Android.bp +12 −0 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ cc_library_shared { "AudioVolumeGroup.cpp", ], shared_libs: [ "capture_state_listener-aidl-cpp", "libaudiofoundation", "libaudioutils", "libbinder", Loading @@ -34,6 +35,9 @@ cc_library_shared { ], include_dirs: ["system/media/audio_utils/include"], export_include_dirs: ["include"], export_shared_lib_headers: [ "capture_state_listener-aidl-cpp", ], } cc_library_shared { Loading Loading @@ -148,3 +152,11 @@ filegroup { ], path: "aidl", } aidl_interface { name: "capture_state_listener-aidl", local_include_dir: "aidl", srcs: [ "aidl/android/media/ICaptureStateListener.aidl", ], }
media/libaudioclient/aidl/android/media/ICaptureStateListener.aidl 0 → 100644 +21 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.media; interface ICaptureStateListener { void setCaptureState(boolean active); }
services/audiopolicy/service/Android.mk +2 −1 Original line number Diff line number Diff line Loading @@ -6,7 +6,8 @@ LOCAL_SRC_FILES:= \ AudioPolicyService.cpp \ AudioPolicyEffects.cpp \ AudioPolicyInterfaceImpl.cpp \ AudioPolicyClientImpl.cpp AudioPolicyClientImpl.cpp \ CaptureStateNotifier.cpp LOCAL_C_INCLUDES := \ frameworks/av/services/audioflinger \ Loading
services/audiopolicy/service/CaptureStateNotifier.cpp 0 → 100644 +58 −0 Original line number Diff line number Diff line #include "CaptureStateNotifier.h" #define LOG_TAG "CaptureStateNotifier" #include <android/media/ICaptureStateListener.h> #include <binder/IBinder.h> #include <utils/Log.h> namespace android { using media::ICaptureStateListener; class CaptureStateNotifier::DeathRecipient : public IBinder::DeathRecipient { public: DeathRecipient(CaptureStateNotifier* notifier) : mNotifier(notifier) {} void binderDied(const wp<IBinder>&) override { mNotifier->binderDied(); } private: CaptureStateNotifier* const mNotifier; }; CaptureStateNotifier::CaptureStateNotifier(bool initialActive) { mActive = initialActive; } CaptureStateNotifier::~CaptureStateNotifier() { LOG_ALWAYS_FATAL_IF(mListener != nullptr); } bool CaptureStateNotifier::RegisterListener(const sp<ICaptureStateListener>& listener) { std::lock_guard<std::mutex> _l(mMutex); LOG_ALWAYS_FATAL_IF(mListener != nullptr); ALOGI("Registering a listener"); mListener = listener; sp<IBinder> binder = IInterface::asBinder(mListener); binder->linkToDeath(new DeathRecipient(this)); return mActive; } void CaptureStateNotifier::setCaptureState(bool active) { std::lock_guard<std::mutex> _l(mMutex); mActive = active; if (mListener) { mListener->setCaptureState(active); } } void CaptureStateNotifier::binderDied() { std::lock_guard<std::mutex> _l(mMutex); mListener.clear(); ALOGI("Listener binder died"); } } // namespace android No newline at end of file
services/audiopolicy/service/CaptureStateNotifier.h 0 → 100644 +73 −0 Original line number Diff line number Diff line /* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <mutex> #include <utils/StrongPointer.h> namespace android { namespace media { // Must be pre-declared, or else there isn't a good way to generate a header // library. class ICaptureStateListener; } // A utility for managing capture state change notifications. // // We are making some strong assumptions, for the sake of simplicity: // - There is no way to explicitly unregister listeners. The only way for a // listener to unregister is by dying. // - There's only at most one listener at a given time. Attempting to register // a second listener will cause a crash. // - This class isn't really meant to ever be destroyed. We expose a destructor // because it is convenient to use this class as a global instance or a member // of another class, but it will crash if destroyed while a listener is // registered. // // All of these assumptions can be lifted if there is ever a need. // // This class is thread-safe. class CaptureStateNotifier { public: // Ctor. // Accepts the initial active state. explicit CaptureStateNotifier(bool initialActive); // Register a listener to be notified of state changes. // The current state is returned and from that point on any change will be // notified of. bool RegisterListener(const sp<media::ICaptureStateListener>& listener); // Change the current capture state. // Active means "actively capturing". void setCaptureState(bool active); // Dtor. Do not actually call at runtime. Will cause a crash if a listener // is registered. ~CaptureStateNotifier(); private: std::mutex mMutex; sp<media::ICaptureStateListener> mListener; bool mActive; class DeathRecipient; void binderDied(); }; } // namespace android