blob: 6bd54ba6611eafe1febac664de62f54a021b3635 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
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),
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080051 ROUTE_BLUETOOTH_SCO = (1 << 2),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070052 ROUTE_HEADSET = (1 << 3),
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080053 ROUTE_BLUETOOTH_A2DP = (1 << 4),
54 ROUTE_ALL = 0xFFFFFFFF
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070055 };
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
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080099 static status_t getOutputSamplingRate(int* samplingRate);
100 static status_t getOutputFrameCount(int* frameCount);
101 static status_t getOutputLatency(uint32_t* latency);
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800102
103 static status_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
104 size_t* buffSize);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800105
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700106 // ----------------------------------------------------------------------------
107
108private:
109
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800110 class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700111 {
112 public:
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800113 AudioFlingerClient() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700114 }
115
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800116 // DeathRecipient
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700117 virtual void binderDied(const wp<IBinder>& who);
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800118
119 // IAudioFlingerClient
120 virtual void audioOutputChanged(uint32_t frameCount, uint32_t samplingRate, uint32_t latency);
121
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700122 };
123
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800124 static sp<AudioFlingerClient> gAudioFlingerClient;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700125
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800126 friend class AudioFlingerClient;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700127
128 static Mutex gLock;
129 static sp<IAudioFlinger> gAudioFlinger;
130 static audio_error_callback gAudioErrorCallback;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800131 static int gOutSamplingRate;
132 static int gOutFrameCount;
133 static uint32_t gOutLatency;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800134
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
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700141};
142
143}; // namespace android
144
145#endif /*ANDROID_AUDIOSYSTEM_H_*/