blob: 4ca66014b6cffc9af706b7b6ccb3751669eef638 [file] [log] [blame]
Ytai Ben-Tsvi987d12c2020-03-24 17:35:44 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <mutex>
20#include <utils/StrongPointer.h>
21
22namespace android {
23namespace media {
24// Must be pre-declared, or else there isn't a good way to generate a header
25// library.
26class ICaptureStateListener;
27}
28
29// A utility for managing capture state change notifications.
30//
31// We are making some strong assumptions, for the sake of simplicity:
32// - There is no way to explicitly unregister listeners. The only way for a
33// listener to unregister is by dying.
34// - There's only at most one listener at a given time. Attempting to register
35// a second listener will cause a crash.
36// - This class isn't really meant to ever be destroyed. We expose a destructor
37// because it is convenient to use this class as a global instance or a member
38// of another class, but it will crash if destroyed while a listener is
39// registered.
40//
41// All of these assumptions can be lifted if there is ever a need.
42//
43// This class is thread-safe.
44class CaptureStateNotifier {
45public:
46 // Ctor.
47 // Accepts the initial active state.
48 explicit CaptureStateNotifier(bool initialActive);
49
50 // Register a listener to be notified of state changes.
51 // The current state is returned and from that point on any change will be
52 // notified of.
53 bool RegisterListener(const sp<media::ICaptureStateListener>& listener);
54
55 // Change the current capture state.
56 // Active means "actively capturing".
57 void setCaptureState(bool active);
58
59 // Dtor. Do not actually call at runtime. Will cause a crash if a listener
60 // is registered.
61 ~CaptureStateNotifier();
62
63private:
64 std::mutex mMutex;
65 sp<media::ICaptureStateListener> mListener;
66 bool mActive;
67
68 class DeathRecipient;
69
70 void binderDied();
71};
72
73} // namespace android