summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2017-06-21 00:58:53 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2017-06-21 00:58:54 +0000
commite9c0e6bdeed48d7bf3a04fce0ec42927b71aca57 (patch)
treeba303b0eb1822117cd29ead9a4d842399e7814e6
parent869491ef0035cfea9e5642c3a6baf82dfb12c8cc (diff)
parentd1f86b16005f798c0584b57d35f621b5fc851c29 (diff)
downloadfugu-e9c0e6bdeed48d7bf3a04fce0ec42927b71aca57.tar.gz
Merge "audio HAL: remote mic capture improvements" into oc-devoreo-dev
-rw-r--r--libaudio/AudioHardwareInput.cpp2
-rw-r--r--libaudio/AudioStreamIn.cpp33
-rw-r--r--libaudio/AudioStreamIn.h3
3 files changed, 36 insertions, 2 deletions
diff --git a/libaudio/AudioHardwareInput.cpp b/libaudio/AudioHardwareInput.cpp
index 6a7a9f4..eef0e89 100644
--- a/libaudio/AudioHardwareInput.cpp
+++ b/libaudio/AudioHardwareInput.cpp
@@ -76,7 +76,7 @@ status_t AudioHardwareInput::getMicMute(bool* mute)
}
// milliseconds per ALSA period
-const uint32_t AudioHardwareInput::kPeriodMsec = 10;
+const uint32_t AudioHardwareInput::kPeriodMsec = 20;
size_t AudioHardwareInput::calculateInputBufferSize(uint32_t outputSampleRate,
audio_format_t format,
diff --git a/libaudio/AudioStreamIn.cpp b/libaudio/AudioStreamIn.cpp
index 89df69a..36156aa 100644
--- a/libaudio/AudioStreamIn.cpp
+++ b/libaudio/AudioStreamIn.cpp
@@ -43,7 +43,7 @@ const audio_format_t AudioStreamIn::kAudioFormat = AUDIO_FORMAT_PCM_16_BIT;
const uint32_t AudioStreamIn::kChannelMask = AUDIO_CHANNEL_IN_MONO;
// number of periods in the ALSA buffer
-const int AudioStreamIn::kPeriodCount = 4;
+const int AudioStreamIn::kPeriodCount = 32;
AudioStreamIn::AudioStreamIn(AudioHardwareInput& owner)
: mOwnerHAL(owner)
@@ -58,6 +58,9 @@ AudioStreamIn::AudioStreamIn(AudioHardwareInput& owner)
, mInputSource(AUDIO_SOURCE_DEFAULT)
, mReadStatus(0)
, mFramesIn(0)
+ , mLastReadFinishedNs(-1)
+ , mLastBytesRead(0)
+ , mMinAllowedReadTimeNs(0)
{
struct resampler_buffer_provider& provider =
mResamplerProviderWrapper.provider;
@@ -282,12 +285,37 @@ ssize_t AudioStreamIn::read(void* buffer, size_t bytes)
// if we have never returned any data from an actual device and need
// to synth on the first call to read)
usleep(bytes * 1000000 / getFrameSize() / mRequestedSampleRate);
+ mLastReadFinishedNs = -1;
} else {
bool mute;
mOwnerHAL.getMicMute(&mute);
if (mute) {
memset(buffer, 0, bytes);
}
+
+ nsecs_t now = systemTime();
+
+ if (mLastReadFinishedNs != -1) {
+ const nsecs_t kMinsleeptimeNs = 1000000; // don't sleep less than 1ms
+ const nsecs_t deltaNs = now - mLastReadFinishedNs;
+
+ if (bytes != mLastBytesRead) {
+ mMinAllowedReadTimeNs =
+ (((nsecs_t)bytes * 1000000000) / getFrameSize()) / mRequestedSampleRate / 2;
+ mLastBytesRead = bytes;
+ }
+
+ // Make sure total read time is at least the duration corresponding to half the amount
+ // of data requested.
+ // Note: deltaNs is always > 0 here
+ if (mMinAllowedReadTimeNs > deltaNs + kMinsleeptimeNs) {
+ usleep((mMinAllowedReadTimeNs - deltaNs) / 1000);
+ // Throttle must be attributed to the previous read time to allow
+ // back-to-back throttling.
+ now = systemTime();
+ }
+ }
+ mLastReadFinishedNs = now;
}
return bytes;
@@ -375,6 +403,9 @@ status_t AudioStreamIn::startInputStream_l()
}
mBuffer = new int16_t[mBufferSize / sizeof(uint16_t)];
+ mLastReadFinishedNs = -1;
+ mLastBytesRead = 0;
+
if (mResampler) {
release_resampler(mResampler);
mResampler = NULL;
diff --git a/libaudio/AudioStreamIn.h b/libaudio/AudioStreamIn.h
index 8c2d444..1fe4410 100644
--- a/libaudio/AudioStreamIn.h
+++ b/libaudio/AudioStreamIn.h
@@ -115,6 +115,9 @@ class AudioStreamIn {
int mInputSource;
int mReadStatus;
unsigned int mFramesIn;
+ nsecs_t mLastReadFinishedNs;
+ size_t mLastBytesRead;
+ nsecs_t mMinAllowedReadTimeNs;
};
}; // namespace android