summaryrefslogtreecommitdiff
path: root/media/jni/soundpool/SoundPool.h
blob: d5b16ef629cdab58a557765904c5229f7d7a5643 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (C) 2007 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 "SoundManager.h"
#include "StreamManager.h"

namespace android {

/**
 * Native class for Java SoundPool, manages a pool of sounds.
 *
 * See the Android SoundPool Java documentation for description of valid values.
 * https://developer.android.com/reference/android/media/SoundPool
 */
class SoundPool {
public:
    SoundPool(int32_t maxStreams, const audio_attributes_t* attributes);
    ~SoundPool();

    // SoundPool Java API support
    int32_t load(int fd, int64_t offset, int64_t length, int32_t priority);
    bool unload(int32_t soundID);
    int32_t play(int32_t soundID, float leftVolume, float rightVolume, int32_t priority,
            int32_t loop, float rate);
    void pause(int32_t streamID);
    void autoPause();
    void resume(int32_t streamID);
    void autoResume();
    void stop(int32_t streamID);
    void setVolume(int32_t streamID, float leftVolume, float rightVolume);
    void setPriority(int32_t streamID, int32_t priority);
    void setLoop(int32_t streamID, int32_t loop);
    void setRate(int32_t streamID, float rate);
    void setCallback(SoundPoolCallback* callback, void* user);
    void* getUserData() const;

    // not exposed in the public Java API, used for internal playerSetVolume() muting.
    void mute(bool muting);

private:

    // Constructor initialized variables
    // Can access without lock as they are internally locked,
    // though care needs to be taken that the final result composed of
    // individually consistent actions are consistent.
    soundpool::SoundManager  mSoundManager;
    soundpool::StreamManager mStreamManager;

    // mApiLock serializes SoundPool application calls (configurable by kUseApiLock).
    // It only locks at the SoundPool layer and not below.  At this level,
    // mApiLock is only required for autoPause() and autoResume() to prevent zippering
    // of the individual pauses and resumes, and mute() for self-interaction with itself.
    // It is optional for all other apis.
    mutable std::mutex        mApiLock;
};

} // end namespace android