blob: 7437f652666015ebf233d80ac0840b13dd05066b [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
The Android Open Source Projectda996f32009-02-13 12:57:50 -080032 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
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070051 enum audio_format {
The Android Open Source Projectda996f32009-02-13 12:57:50 -080052 FORMAT_DEFAULT = 0,
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070053 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),
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080070 ROUTE_BLUETOOTH_SCO = (1 << 2),
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070071 ROUTE_HEADSET = (1 << 3),
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080072 ROUTE_BLUETOOTH_A2DP = (1 << 4),
73 ROUTE_ALL = 0xFFFFFFFF
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070074 };
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
The Android Open Source Projectda996f32009-02-13 12:57:50 -0800118 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);
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800123
124 static status_t getInputBufferSize(uint32_t sampleRate, int format, int channelCount,
125 size_t* buffSize);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800126
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700127 // ----------------------------------------------------------------------------
128
129private:
130
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800131 class AudioFlingerClient: public IBinder::DeathRecipient, public BnAudioFlingerClient
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700132 {
133 public:
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800134 AudioFlingerClient() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700135 }
136
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800137 // DeathRecipient
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700138 virtual void binderDied(const wp<IBinder>& who);
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800139
140 // IAudioFlingerClient
The Android Open Source Projectda996f32009-02-13 12:57:50 -0800141 virtual void a2dpEnabledChanged(bool enabled);
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800142
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700143 };
The Android Open Source Projectda996f32009-02-13 12:57:50 -0800144 static int getOutput(int streamType);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700145
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800146 static sp<AudioFlingerClient> gAudioFlingerClient;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700147
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800148 friend class AudioFlingerClient;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700149
150 static Mutex gLock;
151 static sp<IAudioFlinger> gAudioFlinger;
152 static audio_error_callback gAudioErrorCallback;
The Android Open Source Projectda996f32009-02-13 12:57:50 -0800153 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;
The Android Open Source Projectd24b8182009-02-10 15:44:00 -0800157
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
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700164};
165
166}; // namespace android
167
168#endif /*ANDROID_AUDIOSYSTEM_H_*/