blob: f58e4c08663d3d1eed2222d522e7d86fd7337f8a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2007, The Android Open Source Project
4**
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
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
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.
16*/
17
18#include <cutils/properties.h>
19#include <string.h>
20#include <unistd.h>
Eric Laurenta553c252009-07-17 12:17:14 -070021//#define LOG_NDEBUG 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
23#define LOG_TAG "AudioHardwareInterface"
24#include <utils/Log.h>
25#include <utils/String8.h>
26
27#include "AudioHardwareStub.h"
28#include "AudioHardwareGeneric.h"
Eric Laurenta553c252009-07-17 12:17:14 -070029#ifdef WITH_A2DP
30#include "A2dpAudioInterface.h"
31#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Eric Laurenta553c252009-07-17 12:17:14 -070033#ifdef ENABLE_AUDIO_DUMP
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034#include "AudioDumpInterface.h"
35#endif
36
37
38// change to 1 to log routing calls
Eric Laurenta553c252009-07-17 12:17:14 -070039#define LOG_ROUTING_CALLS 1
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41namespace android {
42
43#if LOG_ROUTING_CALLS
44static const char* routingModeStrings[] =
45{
46 "OUT OF RANGE",
47 "INVALID",
48 "CURRENT",
49 "NORMAL",
50 "RINGTONE",
Eric Laurent4f9e0f12010-12-10 11:46:12 -080051 "IN_CALL",
52 "IN_COMMUNICATION"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053};
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055static const char* routeNone = "NONE";
56
57static const char* displayMode(int mode)
58{
Eric Laurent4f9e0f12010-12-10 11:46:12 -080059 if ((mode < AudioSystem::MODE_INVALID) || (mode >= AudioSystem::NUM_MODES))
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 return routingModeStrings[0];
61 return routingModeStrings[mode+3];
62}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063#endif
64
65// ----------------------------------------------------------------------------
66
67AudioHardwareInterface* AudioHardwareInterface::create()
68{
69 /*
70 * FIXME: This code needs to instantiate the correct audio device
71 * interface. For now - we use compile-time switches.
72 */
73 AudioHardwareInterface* hw = 0;
74 char value[PROPERTY_VALUE_MAX];
75
76#ifdef GENERIC_AUDIO
77 hw = new AudioHardwareGeneric();
78#else
79 // if running in emulation - use the emulator driver
80 if (property_get("ro.kernel.qemu", value, 0)) {
81 LOGD("Running in emulation - using generic audio driver");
82 hw = new AudioHardwareGeneric();
83 }
84 else {
85 LOGV("Creating Vendor Specific AudioHardware");
86 hw = createAudioHardware();
87 }
88#endif
89 if (hw->initCheck() != NO_ERROR) {
90 LOGW("Using stubbed audio hardware. No sound will be produced.");
91 delete hw;
92 hw = new AudioHardwareStub();
93 }
94
Eric Laurenta553c252009-07-17 12:17:14 -070095#ifdef WITH_A2DP
96 hw = new A2dpAudioInterface(hw);
97#endif
98
99#ifdef ENABLE_AUDIO_DUMP
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 // This code adds a record of buffers in a file to write calls made by AudioFlinger.
101 // It replaces the current AudioHardwareInterface object by an intermediate one which
102 // will record buffers in a file (after sending them to hardware) for testing purpose.
Eric Laurenta553c252009-07-17 12:17:14 -0700103 // This feature is enabled by defining symbol ENABLE_AUDIO_DUMP.
104 // The output file is set with setParameters("test_cmd_file_name=<name>"). Pause are not recorded in the file.
105 LOGV("opening PCM dump interface");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 hw = new AudioDumpInterface(hw); // replace interface
107#endif
108 return hw;
109}
110
111AudioStreamOut::~AudioStreamOut()
112{
113}
114
115AudioStreamIn::~AudioStreamIn() {}
116
117AudioHardwareBase::AudioHardwareBase()
118{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 mMode = 0;
120}
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122status_t AudioHardwareBase::setMode(int mode)
123{
124#if LOG_ROUTING_CALLS
125 LOGD("setMode(%s)", displayMode(mode));
126#endif
127 if ((mode < 0) || (mode >= AudioSystem::NUM_MODES))
128 return BAD_VALUE;
129 if (mMode == mode)
Eric Laurenta553c252009-07-17 12:17:14 -0700130 return ALREADY_EXISTS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 mMode = mode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 return NO_ERROR;
133}
134
Eric Laurenta553c252009-07-17 12:17:14 -0700135// default implementation
136status_t AudioHardwareBase::setParameters(const String8& keyValuePairs)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 return NO_ERROR;
139}
140
Eric Laurenta553c252009-07-17 12:17:14 -0700141// default implementation
142String8 AudioHardwareBase::getParameters(const String8& keys)
143{
Eric Laurentab19b212009-08-04 06:17:43 -0700144 AudioParameter param = AudioParameter(keys);
145 return param.toString();
Eric Laurenta553c252009-07-17 12:17:14 -0700146}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148// default implementation
149size_t AudioHardwareBase::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
150{
151 if (sampleRate != 8000) {
152 LOGW("getInputBufferSize bad sampling rate: %d", sampleRate);
153 return 0;
154 }
155 if (format != AudioSystem::PCM_16_BIT) {
156 LOGW("getInputBufferSize bad format: %d", format);
157 return 0;
158 }
159 if (channelCount != 1) {
160 LOGW("getInputBufferSize bad channel count: %d", channelCount);
161 return 0;
162 }
163
164 return 320;
165}
166
167status_t AudioHardwareBase::dumpState(int fd, const Vector<String16>& args)
168{
169 const size_t SIZE = 256;
170 char buffer[SIZE];
171 String8 result;
172 snprintf(buffer, SIZE, "AudioHardwareBase::dumpState\n");
173 result.append(buffer);
174 snprintf(buffer, SIZE, "\tmMode: %d\n", mMode);
175 result.append(buffer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 ::write(fd, result.string(), result.size());
177 dump(fd, args); // Dump the state of the concrete child.
178 return NO_ERROR;
179}
180
181// ----------------------------------------------------------------------------
182
183}; // namespace android