CallbackProtector.h revision 83ac345e264c1e22b7a2f1a110b2fe92473394ec
1/*
2 * Copyright (C) 2011 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#include "utils/threads.h"
18
19//--------------------------------------------------------------------------------------------------
20namespace android {
21
22class AudioTrackProtector : public RefBase {
23
24public:
25    AudioTrackProtector();
26    virtual ~AudioTrackProtector();
27
28    /**
29     * Indicates whether it's safe to enter the AudioTrack callback. It would typically return
30     * false if the AudioTrack is about to be destroyed
31     */
32    bool enterCb();
33
34    /**
35     * This method must be paired to each call to enterCb(), only if enterCb() returned that it is
36     * safe enter the callback;
37     */
38    void exitCb();
39
40    /**
41     * Called to signal the track is about to be destroyed, so whenever an AudioTrack callback is
42     * entered (see enterCb) it will be notified it is pointless to process the callback. This will
43     * return immediately if there are no callbacks, and will block until current callbacks exit.
44     */
45    void requestCbExitAndWait();
46
47protected:
48    Mutex mLock;
49    Condition mCbExitedCondition;
50
51    bool mSafeToEnterCb;
52
53    /** Counts the number of AudioTrack callbacks actively locking the associated AudioPlayer */
54    unsigned int mCbCount;
55};
56
57} // namespace android
58