@@ -1816,6 +1816,18 @@ audio_stream_t* AudioFlinger::PlaybackThread::stream()
18161816 return &mOutput ->stream ->common ;
18171817}
18181818
1819+ uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs ()
1820+ {
1821+ // A2DP output latency is not due only to buffering capacity. It also reflects encoding,
1822+ // decoding and transfer time. So sleeping for half of the latency would likely cause
1823+ // underruns
1824+ if (audio_is_a2dp_device ((audio_devices_t )mDevice )) {
1825+ return (uint32_t )((uint32_t )((mFrameCount * 1000 ) / mSampleRate ) * 1000 );
1826+ } else {
1827+ return (uint32_t )(mOutput ->stream ->get_latency (mOutput ->stream ) * 1000 ) / 2 ;
1828+ }
1829+ }
1830+
18191831// ----------------------------------------------------------------------------
18201832
18211833AudioFlinger::MixerThread::MixerThread (const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output, int id, uint32_t device)
@@ -2422,11 +2434,6 @@ status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>
24222434 return NO_ERROR;
24232435}
24242436
2425- uint32_t AudioFlinger::MixerThread::activeSleepTimeUs ()
2426- {
2427- return (uint32_t )(mOutput ->stream ->get_latency (mOutput ->stream ) * 1000 ) / 2 ;
2428- }
2429-
24302437uint32_t AudioFlinger::MixerThread::idleSleepTimeUs ()
24312438{
24322439 return (uint32_t )(((mFrameCount * 1000 ) / mSampleRate ) * 1000 ) / 2 ;
@@ -2893,7 +2900,7 @@ uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs()
28932900{
28942901 uint32_t time;
28952902 if (audio_is_linear_pcm (mFormat )) {
2896- time = ( uint32_t )( mOutput -> stream -> get_latency ( mOutput -> stream ) * 1000 ) / 2 ;
2903+ time = PlaybackThread::activeSleepTimeUs () ;
28972904 } else {
28982905 time = 10000 ;
28992906 }
0 commit comments