3 ** Copyright 2007, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
18 #include <cutils/properties.h>
22 #define LOG_TAG "AudioHardwareInterface"
23 #include <utils/Log.h>
24 #include <utils/String8.h>
26 #include "AudioHardwareStub.h"
27 #include "AudioHardwareGeneric.h"
29 // #define DUMP_FLINGER_OUT // if defined allows recording samples in a file
30 #ifdef DUMP_FLINGER_OUT
31 #include "AudioDumpInterface.h"
35 // change to 1 to log routing calls
36 #define LOG_ROUTING_CALLS 0
41 static const char* routingModeStrings[] =
51 static const char* routeStrings[] =
58 static const char* routeNone = "NONE";
60 static const char* displayMode(int mode)
62 if ((mode < -2) || (mode > 2))
63 return routingModeStrings[0];
64 return routingModeStrings[mode+3];
67 static const char* displayRoutes(uint32_t routes)
69 static char routeStr[80];
74 for (int i = 0; i < 4; ++i, bitMask <<= 1) {
75 if (routes & bitMask) {
76 strcat(routeStr, routeStrings[i]);
79 routeStr[strlen(routeStr)-1] = 0;
84 // ----------------------------------------------------------------------------
86 AudioHardwareInterface* AudioHardwareInterface::create()
89 * FIXME: This code needs to instantiate the correct audio device
90 * interface. For now - we use compile-time switches.
92 AudioHardwareInterface* hw = 0;
93 char value[PROPERTY_VALUE_MAX];
96 hw = new AudioHardwareGeneric();
98 // if running in emulation - use the emulator driver
99 if (property_get("ro.kernel.qemu", value, 0)) {
100 LOGD("Running in emulation - using generic audio driver");
101 hw = new AudioHardwareGeneric();
104 LOGV("Creating Vendor Specific AudioHardware");
105 hw = createAudioHardware();
108 if (hw->initCheck() != NO_ERROR) {
109 LOGW("Using stubbed audio hardware. No sound will be produced.");
111 hw = new AudioHardwareStub();
114 #ifdef DUMP_FLINGER_OUT
115 // This code adds a record of buffers in a file to write calls made by AudioFlinger.
116 // It replaces the current AudioHardwareInterface object by an intermediate one which
117 // will record buffers in a file (after sending them to hardware) for testing purpose.
118 // This feature is enabled by defining symbol DUMP_FLINGER_OUT and setting environement
119 // "audioflinger.dump = 1". The output file is "tmp/FlingerOut.pcm". Pause are not recorded
123 property_get("audioflinger.dump", value, "0");
127 hw = new AudioDumpInterface(hw); // replace interface
132 LOGV("No Dump mode");
140 AudioStreamOut::~AudioStreamOut()
144 AudioStreamIn::~AudioStreamIn() {}
146 AudioHardwareInterface::AudioHardwareInterface()
148 // force a routing update on initialization
149 memset(&mRoutes, 0, sizeof(mRoutes));
153 // generics for audio routing - the real work is done in doRouting
154 status_t AudioHardwareInterface::setRouting(int mode, uint32_t routes)
156 #if LOG_ROUTING_CALLS
157 LOGD("setRouting: mode=%s, routes=[%s]", displayMode(mode), displayRoutes(routes));
159 if (mode == AudioSystem::MODE_CURRENT)
161 if ((mode < 0) || (mode >= AudioSystem::NUM_MODES))
163 uint32_t old = mRoutes[mode];
164 mRoutes[mode] = routes;
165 if ((mode != mMode) || (old == routes))
167 #if LOG_ROUTING_CALLS
168 const char* oldRouteStr = strdup(displayRoutes(old));
169 LOGD("doRouting: mode=%s, old route=[%s], new route=[%s]",
170 displayMode(mode), oldRouteStr, displayRoutes(routes));
176 status_t AudioHardwareInterface::getRouting(int mode, uint32_t* routes)
178 if (mode == AudioSystem::MODE_CURRENT)
180 if ((mode < 0) || (mode >= AudioSystem::NUM_MODES))
182 *routes = mRoutes[mode];
183 #if LOG_ROUTING_CALLS
184 LOGD("getRouting: mode=%s, routes=[%s]",
185 displayMode(mode), displayRoutes(*routes));
190 status_t AudioHardwareInterface::setMode(int mode)
192 #if LOG_ROUTING_CALLS
193 LOGD("setMode(%s)", displayMode(mode));
195 if ((mode < 0) || (mode >= AudioSystem::NUM_MODES))
199 #if LOG_ROUTING_CALLS
200 LOGD("doRouting: old mode=%s, new mode=%s route=[%s]",
201 displayMode(mMode), displayMode(mode), displayRoutes(mRoutes[mode]));
207 status_t AudioHardwareInterface::getMode(int* mode)
209 // Implement: set audio routing
214 status_t AudioHardwareInterface::setParameter(const char* key, const char* value)
216 // default implementation is to ignore
220 status_t AudioHardwareInterface::dumpState(int fd, const Vector<String16>& args)
222 const size_t SIZE = 256;
225 snprintf(buffer, SIZE, "AudioHardwareInterface::dumpState\n");
226 result.append(buffer);
227 snprintf(buffer, SIZE, "\tmMode: %d\n", mMode);
228 result.append(buffer);
229 for (int i = 0, n = AudioSystem::NUM_MODES; i < n; ++i) {
230 snprintf(buffer, SIZE, "\tmRoutes[%d]: %d\n", i, mRoutes[i]);
231 result.append(buffer);
233 ::write(fd, result.string(), result.size());
234 dump(fd, args); // Dump the state of the concrete child.
238 // ----------------------------------------------------------------------------
240 }; // namespace android