Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3522c808 authored by Eric Laurent's avatar Eric Laurent
Browse files

Fix issue 1992233: DTMF tones on Sholes is really long.

Add a parameter to ToneGenerator.startTone() allowing the caller to specify the tone duration. This is used by the phone application to have a precise control on the DTMF tone duration which was not possible with the use of delayed messaged.
Also modified AudioFlinger output threads so that 0s are written to the audio output stream when no more tracks are ready to mix instead of just sleeping. This avoids an issue where the end of a previous DTMF tone could stay in audio hardware buffers and be played just before the beginning of the next DTMF tone.
parent e40e5507
Loading
Loading
Loading
Loading
+63 −55
Original line number Diff line number Diff line
@@ -1160,7 +1160,7 @@ AudioFlinger::MixerThread::~MixerThread()

bool AudioFlinger::MixerThread::threadLoop()
{
    unsigned long sleepTime = kBufferRecoveryInUsecs;
    unsigned long sleepTime = 0;
    int16_t* curBuf = mMixBuffer;
    Vector< sp<Track> > tracksToRemove;
    size_t enabledTracks = 0;
@@ -1215,6 +1215,7 @@ bool AudioFlinger::MixerThread::threadLoop()
                    }

                    standbyTime = systemTime() + kStandbyTimeInNsecs;
                    sleepTime = 0;
                    continue;
                }
            }
@@ -1222,14 +1223,31 @@ bool AudioFlinger::MixerThread::threadLoop()
            enabledTracks = prepareTracks_l(activeTracks, &tracksToRemove);
       }

        if (LIKELY(enabledTracks)) {
            // mix buffers...
            mAudioMixer->process(curBuf);

        // output audio to hardware
        if (mSuspended) {
            usleep(kMaxBufferRecoveryInUsecs);
        } else {
            if (LIKELY(enabledTracks)) {
                // mix buffers...
                mAudioMixer->process(curBuf);
                sleepTime = 0;
                standbyTime = systemTime() + kStandbyTimeInNsecs;
            } else {
                sleepTime += kBufferRecoveryInUsecs;
                // There was nothing to mix this round, which means all
                // active tracks were late. Sleep a little bit to give
                // them another chance. If we're too late, write 0s to audio
                // hardware to avoid underrun.
                if (sleepTime < kMaxBufferRecoveryInUsecs) {
                    usleep(kBufferRecoveryInUsecs);
                } else {
                    memset (curBuf, 0, mixBufferSize);
                    sleepTime = 0;
                }
            }
            // sleepTime == 0 means PCM data were written to mMixBuffer[]
            if (sleepTime == 0) {
                mLastWriteTime = systemTime();
                mInWrite = true;
                int bytesWritten = (int)mOutput->write(curBuf, mixBufferSize);
@@ -1237,24 +1255,11 @@ bool AudioFlinger::MixerThread::threadLoop()
                mNumWrites++;
                mInWrite = false;
                mStandby = false;
                nsecs_t temp = systemTime();
                standbyTime = temp + kStandbyTimeInNsecs;
                nsecs_t delta = temp - mLastWriteTime;
                nsecs_t delta = systemTime() - mLastWriteTime;
                if (delta > maxPeriod) {
                    LOGW("write blocked for %llu msecs", ns2ms(delta));
                    mNumDelayedWrites++;
                }
                sleepTime = kBufferRecoveryInUsecs;
            }
        } else {
            // There was nothing to mix this round, which means all
            // active tracks were late. Sleep a little bit to give
            // them another chance. If we're too late, the audio
            // hardware will zero-fill for us.
            // LOGV("thread %p no buffers - usleep(%lu)", this, sleepTime);
            usleep(sleepTime);
            if (sleepTime < kMaxBufferRecoveryInUsecs) {
                sleepTime += kBufferRecoveryInUsecs;
            }
        }

@@ -1568,7 +1573,7 @@ AudioFlinger::DirectOutputThread::~DirectOutputThread()

bool AudioFlinger::DirectOutputThread::threadLoop()
{
    unsigned long sleepTime = kBufferRecoveryInUsecs;
    unsigned long sleepTime = 0;
    sp<Track> trackToRemove;
    sp<Track> activeTrack;
    nsecs_t standbyTime = systemTime();
@@ -1618,6 +1623,7 @@ bool AudioFlinger::DirectOutputThread::threadLoop()
                    }

                    standbyTime = systemTime() + kStandbyTimeInNsecs;
                    sleepTime = 0;
                    continue;
                }
            }
@@ -1710,13 +1716,15 @@ bool AudioFlinger::DirectOutputThread::threadLoop()
            }
       }

        // output audio to hardware
        if (mSuspended) {
            usleep(kMaxBufferRecoveryInUsecs);
        } else {
            if (activeTrack != 0) {
                AudioBufferProvider::Buffer buffer;
                size_t frameCount = mFrameCount;
                curBuf = (int8_t *)mMixBuffer;
                // output audio to hardware
            mLastWriteTime = systemTime();
            mInWrite = true;
                while(frameCount) {
                    buffer.frameCount = frameCount;
                    activeTrack->getNextBuffer(&buffer);
@@ -1729,27 +1737,27 @@ bool AudioFlinger::DirectOutputThread::threadLoop()
                    curBuf += buffer.frameCount * mFrameSize;
                    activeTrack->releaseBuffer(&buffer);
                }
            if (mSuspended) {
                usleep(kMaxBufferRecoveryInUsecs);
                sleepTime = 0;
                standbyTime = systemTime() + kStandbyTimeInNsecs;
            } else {
                sleepTime += kBufferRecoveryInUsecs;
                if (sleepTime < kMaxBufferRecoveryInUsecs) {
                    usleep(kBufferRecoveryInUsecs);
                } else {
                    memset (mMixBuffer, 0, mFrameCount * mFrameSize);
                    sleepTime = 0;
                }
            }

            // sleepTime == 0 means PCM data were written to mMixBuffer[]
            if (sleepTime == 0) {
                mLastWriteTime = systemTime();
                mInWrite = true;
                int bytesWritten = (int)mOutput->write(mMixBuffer, mixBufferSize);
                if (bytesWritten) mBytesWritten += bytesWritten;
                mNumWrites++;
                mInWrite = false;
                mStandby = false;
                nsecs_t temp = systemTime();
                standbyTime = temp + kStandbyTimeInNsecs;
                sleepTime = kBufferRecoveryInUsecs;
            }
        } else {
            // There was nothing to mix this round, which means all
            // active tracks were late. Sleep a little bit to give
            // them another chance. If we're too late, the audio
            // hardware will zero-fill for us.
            //LOGV("no buffers - usleep(%lu)", sleepTime);
            usleep(sleepTime);
            if (sleepTime < kMaxBufferRecoveryInUsecs) {
                sleepTime += kBufferRecoveryInUsecs;
            }
        }