Skip to content

Commit ad8d175

Browse files
committed
mAudioHwDevs and related cleanup
Inline AudioFlinger::initCheck and remove unnecessary lock. Remove redundant check of mAudioHwDevs.size(). No need to lock mHardwareLock for each device separately during initialization. Use size_t not int to loop through Vector, since size() returns size_t. Add missing hardware lock for get_mic_mute() and get_input_buffer_size(). Add comments. Change-Id: Iafae78ef78bbf65f703d99fcc27c2f4ff221aedc
1 parent da639f5 commit ad8d175

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

services/audioflinger/AudioFlinger.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,29 +188,32 @@ void AudioFlinger::onFirstRef()
188188
mod->name, mod->id);
189189
mAudioHwDevs.push(dev);
190190

191-
if (!mPrimaryHardwareDev) {
191+
if (mPrimaryHardwareDev == NULL) {
192192
mPrimaryHardwareDev = dev;
193193
ALOGI("Using '%s' (%s.%s) as the primary audio interface",
194194
mod->name, mod->id, audio_interfaces[i]);
195195
}
196196
}
197197

198-
mHardwareStatus = AUDIO_HW_INIT;
199-
200-
if (!mPrimaryHardwareDev || mAudioHwDevs.size() == 0) {
198+
if (mPrimaryHardwareDev == NULL) {
201199
ALOGE("Primary audio interface not found");
202-
return;
200+
// proceed, all later accesses to mPrimaryHardwareDev verify it's safe with initCheck()
203201
}
204202

203+
// Currently (mPrimaryHardwareDev == NULL) == (mAudioHwDevs.size() == 0), but the way the
204+
// primary HW dev is selected can change so these conditions might not always be equivalent.
205+
// When that happens, re-visit all the code that assumes this.
206+
207+
AutoMutex lock(mHardwareLock);
208+
205209
for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
206210
audio_hw_device_t *dev = mAudioHwDevs[i];
207211

208212
mHardwareStatus = AUDIO_HW_INIT;
209213
rc = dev->init_check(dev);
214+
mHardwareStatus = AUDIO_HW_IDLE;
210215
if (rc == 0) {
211-
AutoMutex lock(mHardwareLock);
212-
213-
mMode = AUDIO_MODE_NORMAL;
216+
mMode = AUDIO_MODE_NORMAL; // assigned multiple times with same value
214217
mHardwareStatus = AUDIO_HW_SET_MODE;
215218
dev->set_mode(dev, mMode);
216219
mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
@@ -220,17 +223,8 @@ void AudioFlinger::onFirstRef()
220223
}
221224
}
222225

223-
status_t AudioFlinger::initCheck() const
224-
{
225-
Mutex::Autolock _l(mLock);
226-
if (mPrimaryHardwareDev == NULL || mAudioHwDevs.size() == 0)
227-
return NO_INIT;
228-
return NO_ERROR;
229-
}
230-
231226
AudioFlinger::~AudioFlinger()
232227
{
233-
int num_devs = mAudioHwDevs.size();
234228

235229
while (!mRecordThreads.isEmpty()) {
236230
// closeInput() will remove first entry from mRecordThreads
@@ -241,9 +235,9 @@ AudioFlinger::~AudioFlinger()
241235
closeOutput(mPlaybackThreads.keyAt(0));
242236
}
243237

244-
for (int i = 0; i < num_devs; i++) {
245-
audio_hw_device_t *dev = mAudioHwDevs[i];
246-
audio_hw_device_close(dev);
238+
for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
239+
// no mHardwareLock needed, as there are no other references to this
240+
audio_hw_device_close(mAudioHwDevs[i]);
247241
}
248242
}
249243

@@ -625,6 +619,7 @@ bool AudioFlinger::getMicMute() const
625619
}
626620

627621
bool state = AUDIO_MODE_INVALID;
622+
AutoMutex lock(mHardwareLock);
628623
mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
629624
mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state);
630625
mHardwareStatus = AUDIO_HW_IDLE;
@@ -856,7 +851,11 @@ size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t form
856851
return 0;
857852
}
858853

859-
return mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount);
854+
AutoMutex lock(mHardwareLock);
855+
mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
856+
size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount);
857+
mHardwareStatus = AUDIO_HW_IDLE;
858+
return size;
860859
}
861860

862861
unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const

services/audioflinger/AudioFlinger.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ class AudioFlinger :
199199
AudioFlinger();
200200
virtual ~AudioFlinger();
201201

202-
status_t initCheck() const;
202+
// call in any IAudioFlinger method that accesses mPrimaryHardwareDev
203+
status_t initCheck() const { return mPrimaryHardwareDev == NULL ? NO_INIT : NO_ERROR; }
204+
203205
virtual void onFirstRef();
204206
audio_hw_device_t* findSuitableHwDev_l(uint32_t devices);
205207
void purgeStaleEffects_l();
@@ -1391,7 +1393,9 @@ mutable Mutex mLock; // mutex for process, commands and handl
13911393
DefaultKeyedVector< pid_t, wp<Client> > mClients; // see ~Client()
13921394

13931395
mutable Mutex mHardwareLock;
1394-
audio_hw_device_t* mPrimaryHardwareDev;
1396+
1397+
// These two fields are immutable after onFirstRef(), so no lock needed to access
1398+
audio_hw_device_t* mPrimaryHardwareDev; // mAudioHwDevs[0] or NULL
13951399
Vector<audio_hw_device_t*> mAudioHwDevs;
13961400

13971401
enum hardware_call_state {
@@ -1411,6 +1415,7 @@ mutable Mutex mLock; // mutex for process, commands and handl
14111415
AUDIO_HW_SET_MIC_MUTE,
14121416
AUDIO_SET_VOICE_VOLUME,
14131417
AUDIO_SET_PARAMETER,
1418+
AUDIO_HW_GET_INPUT_BUFFER_SIZE,
14141419
};
14151420

14161421
mutable hardware_call_state mHardwareStatus; // for dump only

0 commit comments

Comments
 (0)