diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2017-06-21 07:10:39 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2017-06-21 07:10:39 +0000 |
commit | 2a014e1720d234272cf6ec7c847b98b0b34e8db6 (patch) | |
tree | ba303b0eb1822117cd29ead9a4d842399e7814e6 | |
parent | f86c8cf4b5f109c78fcbea910c94779d9acb60c8 (diff) | |
parent | e9c0e6bdeed48d7bf3a04fce0ec42927b71aca57 (diff) | |
download | fugu-2a014e1720d234272cf6ec7c847b98b0b34e8db6.tar.gz |
release-request-5bf29450-4eb0-4d71-a15a-d8858bf184d3-for-git_oc-release-4120128 snap-temp-L37600000076154351android-cts-8.0_r1android-8.0.0_r9android-8.0.0_r30android-8.0.0_r15oreo-r3-release
Change-Id: I32ea0ec7677a1b31660ffa9c2dac95e248b9148e
-rw-r--r-- | libaudio/AudioHardwareInput.cpp | 2 | ||||
-rw-r--r-- | libaudio/AudioStreamIn.cpp | 33 | ||||
-rw-r--r-- | libaudio/AudioStreamIn.h | 3 |
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 |