AudioSystem.h revision 7a2146d5807030b2629f347736be5301b61e8811
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 stream_type {
33        DEFAULT         =-1,
34        VOICE_CALL      = 0,
35        SYSTEM          = 1,
36        RING            = 2,
37        MUSIC           = 3,
38        ALARM           = 4,
39        NOTIFICATION    = 5,
40        BLUETOOTH_SCO   = 6,
41        NUM_STREAM_TYPES
42    };
43
44    enum audio_output_type {
45        AUDIO_OUTPUT_DEFAULT      =-1,
46        AUDIO_OUTPUT_HARDWARE     = 0,
47        AUDIO_OUTPUT_A2DP         = 1,
48        NUM_AUDIO_OUTPUT_TYPES
49    };
50
51    enum audio_format {
52        FORMAT_DEFAULT = 0,
53        PCM_16_BIT,
54        PCM_8_BIT,
55        INVALID_FORMAT
56    };
57
58    enum audio_mode {
59        MODE_INVALID = -2,
60        MODE_CURRENT = -1,
61        MODE_NORMAL = 0,
62        MODE_RINGTONE,
63        MODE_IN_CALL,
64        NUM_MODES  // not a valid entry, denotes end-of-list
65    };
66
67    enum audio_routes {
68        ROUTE_EARPIECE       = (1 << 0),
69        ROUTE_SPEAKER        = (1 << 1),
70        ROUTE_BLUETOOTH_SCO  = (1 << 2),
71        ROUTE_HEADSET        = (1 << 3),
72        ROUTE_BLUETOOTH_A2DP = (1 << 4),
73        ROUTE_ALL       = 0xFFFFFFFF
74    };
75
76    /* These are static methods to control the system-wide AudioFlinger
77     * only privileged processes can have access to them
78     */
79
80    // routing helper functions
81    static status_t speakerphone(bool state);
82    static status_t isSpeakerphoneOn(bool* state);
83    static status_t bluetoothSco(bool state);
84    static status_t isBluetoothScoOn(bool* state);
85    static status_t muteMicrophone(bool state);
86    static status_t isMicrophoneMuted(bool *state);
87
88    static status_t setMasterVolume(float value);
89    static status_t setMasterMute(bool mute);
90    static status_t getMasterVolume(float* volume);
91    static status_t getMasterMute(bool* mute);
92
93    static status_t setStreamVolume(int stream, float value);
94    static status_t setStreamMute(int stream, bool mute);
95    static status_t getStreamVolume(int stream, float* volume);
96    static status_t getStreamMute(int stream, bool* mute);
97
98    static status_t setMode(int mode);
99    static status_t getMode(int* mode);
100
101    static status_t setRouting(int mode, uint32_t routes, uint32_t mask);
102    static status_t getRouting(int mode, uint32_t* routes);
103
104    static status_t isMusicActive(bool *state);
105
106    // Temporary interface, do not use
107    // TODO: Replace with a more generic key:value get/set mechanism
108    static status_t setParameter(const char* key, const char* value);
109
110    static void setErrorCallback(audio_error_callback cb);
111
112    // helper function to obtain AudioFlinger service handle
113    static const sp<IAudioFlinger>& get_audio_flinger();
114
115    static float linearToLog(int volume);
116    static int logToLinear(float volume);
117
118    static status_t getOutputSamplingRate(int* samplingRate, int stream = DEFAULT);
119    static status_t getOutputFrameCount(int* frameCount, int stream = DEFAULT);
120    static status_t getOutputLatency(uint32_t* latency, int stream = DEFAULT);
121
122    static bool routedToA2dpOutput(int streamType);
123
124    static status_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
125        size_t* buffSize);
126
127    // ----------------------------------------------------------------------------
128
129private:
130
131    class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient
132    {
133    public:
134        AudioFlingerClient() {
135        }
136
137        // DeathRecipient
138        virtual void binderDied(const wp<IBinder>& who);
139
140        // IAudioFlingerClient
141        virtual void a2dpEnabledChanged(bool enabled);
142
143    };
144    static int getOutput(int streamType);
145
146    static sp<AudioFlingerClient> gAudioFlingerClient;
147
148    friend class AudioFlingerClient;
149
150    static Mutex gLock;
151    static sp<IAudioFlinger> gAudioFlinger;
152    static audio_error_callback gAudioErrorCallback;
153    static int gOutSamplingRate[NUM_AUDIO_OUTPUT_TYPES];
154    static int gOutFrameCount[NUM_AUDIO_OUTPUT_TYPES];
155    static uint32_t gOutLatency[NUM_AUDIO_OUTPUT_TYPES];
156    static bool gA2dpEnabled;
157
158    static size_t gInBuffSize;
159    // previous parameters for recording buffer size queries
160    static uint32_t gPrevInSamplingRate;
161    static int gPrevInFormat;
162    static int gPrevInChannelCount;
163
164};
165
166};  // namespace android
167
168#endif  /*ANDROID_AUDIOSYSTEM_H_*/
169