AudioSystem.h revision 5e07b5774c8b376776caa4f5b0a193767697e97e
1/*
2 * Copyright (C) 2008 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#ifndef ANDROID_AUDIOSYSTEM_H_
18#define ANDROID_AUDIOSYSTEM_H_
19
20#include <utils/RefBase.h>
21#include <utils/threads.h>
22#include <media/IAudioFlinger.h>
23
24namespace android {
25
26typedef void (*audio_error_callback)(status_t err);
27
28class AudioSystem
29{
30public:
31
32    enum audio_format {
33        DEFAULT = 0,
34        PCM_16_BIT,
35        PCM_8_BIT,
36        INVALID_FORMAT
37    };
38
39    enum audio_mode {
40        MODE_INVALID = -2,
41        MODE_CURRENT = -1,
42        MODE_NORMAL = 0,
43        MODE_RINGTONE,
44        MODE_IN_CALL,
45        NUM_MODES  // not a valid entry, denotes end-of-list
46    };
47
48    enum audio_routes {
49        ROUTE_EARPIECE       = (1 << 0),
50        ROUTE_SPEAKER        = (1 << 1),
51        ROUTE_BLUETOOTH_SCO  = (1 << 2),
52        ROUTE_HEADSET        = (1 << 3),
53        ROUTE_BLUETOOTH_A2DP = (1 << 4),
54        ROUTE_ALL       = 0xFFFFFFFF
55    };
56
57    /* These are static methods to control the system-wide AudioFlinger
58     * only privileged processes can have access to them
59     */
60
61    // routing helper functions
62    static status_t speakerphone(bool state);
63    static status_t isSpeakerphoneOn(bool* state);
64    static status_t bluetoothSco(bool state);
65    static status_t isBluetoothScoOn(bool* state);
66    static status_t muteMicrophone(bool state);
67    static status_t isMicrophoneMuted(bool *state);
68
69    static status_t setMasterVolume(float value);
70    static status_t setMasterMute(bool mute);
71    static status_t getMasterVolume(float* volume);
72    static status_t getMasterMute(bool* mute);
73
74    static status_t setStreamVolume(int stream, float value);
75    static status_t setStreamMute(int stream, bool mute);
76    static status_t getStreamVolume(int stream, float* volume);
77    static status_t getStreamMute(int stream, bool* mute);
78
79    static status_t setMode(int mode);
80    static status_t getMode(int* mode);
81
82    static status_t setRouting(int mode, uint32_t routes, uint32_t mask);
83    static status_t getRouting(int mode, uint32_t* routes);
84
85    static status_t isMusicActive(bool *state);
86
87    // Temporary interface, do not use
88    // TODO: Replace with a more generic key:value get/set mechanism
89    static status_t setParameter(const char* key, const char* value);
90
91    static void setErrorCallback(audio_error_callback cb);
92
93    // helper function to obtain AudioFlinger service handle
94    static const sp<IAudioFlinger>& get_audio_flinger();
95
96    static float linearToLog(int volume);
97    static int logToLinear(float volume);
98
99    static status_t getOutputSamplingRate(int* samplingRate);
100    static status_t getOutputFrameCount(int* frameCount);
101    static status_t getOutputLatency(uint32_t* latency);
102
103    static status_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
104        size_t* buffSize);
105
106    // ----------------------------------------------------------------------------
107
108private:
109
110    class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient
111    {
112    public:
113        AudioFlingerClient() {
114        }
115
116        // DeathRecipient
117        virtual void binderDied(const wp<IBinder>& who);
118
119        // IAudioFlingerClient
120        virtual void audioOutputChanged(uint32_t frameCount, uint32_t samplingRate, uint32_t latency);
121
122    };
123
124    static sp<AudioFlingerClient> gAudioFlingerClient;
125
126    friend class AudioFlingerClient;
127
128    static Mutex gLock;
129    static sp<IAudioFlinger> gAudioFlinger;
130    static audio_error_callback gAudioErrorCallback;
131    static int gOutSamplingRate;
132    static int gOutFrameCount;
133    static uint32_t gOutLatency;
134
135    static size_t gInBuffSize;
136    // previous parameters for recording buffer size queries
137    static uint32_t gPrevInSamplingRate;
138    static int gPrevInFormat;
139    static int gPrevInChannelCount;
140
141};
142
143};  // namespace android
144
145#endif  /*ANDROID_AUDIOSYSTEM_H_*/
146