Move frame size out of the control block

This is part of a series to clean up the control block.

Change-Id: Ifab1c42ac0f8be704e571b292713cd2250d12a3f
diff --git a/include/media/AudioRecord.h b/include/media/AudioRecord.h
index bfb5d3a..2672db1 100644
--- a/include/media/AudioRecord.h
+++ b/include/media/AudioRecord.h
@@ -185,7 +185,7 @@
             audio_format_t format() const;
             int         channelCount() const;
             uint32_t    frameCount() const;
-            size_t      frameSize() const;
+            size_t      frameSize() const { return mFrameSize; }
             audio_source_t inputSource() const;
 
 
@@ -378,6 +378,7 @@
     uint32_t                mFrameCount;
     audio_format_t          mFormat;
     uint8_t                 mChannelCount;
+    size_t                  mFrameSize;         // app-level frame size == AudioFlinger frame size
     audio_source_t          mInputSource;
     status_t                mStatus;
     uint32_t                mLatency;
diff --git a/include/media/AudioTrack.h b/include/media/AudioTrack.h
index 639b567..3504f1f 100644
--- a/include/media/AudioTrack.h
+++ b/include/media/AudioTrack.h
@@ -229,7 +229,7 @@
     /* Return channelCount * (bit depth per channel / 8).
      * channelCount is determined from channelMask, and bit depth comes from format.
      */
-            size_t      frameSize() const;
+            size_t      frameSize() const { return mFrameSize; }
 
             sp<IMemory>& sharedBuffer();
 
@@ -517,6 +517,13 @@
     uint8_t                 mMuted;
     uint8_t                 mReserved;
     audio_channel_mask_t    mChannelMask;
+
+                // mFrameSize is equal to mFrameSizeAF for non-PCM or 16-bit PCM data.
+                // For 8-bit PCM data, mFrameSizeAF is
+                // twice as large because data is expanded to 16-bit before being stored in buffer.
+    size_t                  mFrameSize;             // app-level frame size
+    size_t                  mFrameSizeAF;           // AudioFlinger frame size
+
     status_t                mStatus;
     uint32_t                mLatency;
 
diff --git a/include/private/media/AudioTrackShared.h b/include/private/media/AudioTrackShared.h
index 8ef90c7..ac5372f 100644
--- a/include/private/media/AudioTrackShared.h
+++ b/include/private/media/AudioTrackShared.h
@@ -77,12 +77,9 @@
 
                 uint32_t    sampleRate;
 
-                // NOTE: audio_track_cblk_t::frameSize is not equal to AudioTrack::frameSize() for
-                // 8 bit PCM data: in this case,  mCblk->frameSize is based on a sample size of
-                // 16 bit because data is converted to 16 bit before being stored in buffer
+                uint8_t     mPad2;           // unused
 
                 // read-only for client, server writes once at initialization and is then read-only
-                uint8_t     frameSize;       // would normally be size_t, but 8 bits is plenty
                 uint8_t     mName;           // normal tracks: track name, fast tracks: track index
 
                 // used by client only
@@ -111,7 +108,7 @@
 
                 // if there is a shared buffer, "buffers" is the value of pointer() for the shared
                 // buffer, otherwise "buffers" points immediately after the control block
-                void*       buffer(void *buffers, uint32_t offset) const;
+                void*       buffer(void *buffers, uint32_t frameSize, uint32_t offset) const;
 
                 uint32_t    framesAvailableIn() { return framesAvailable(false); }
                 uint32_t    framesAvailableOut() { return framesAvailable(true); }
diff --git a/media/libmedia/AudioRecord.cpp b/media/libmedia/AudioRecord.cpp
index 263a7c7..b40aaf5 100644
--- a/media/libmedia/AudioRecord.cpp
+++ b/media/libmedia/AudioRecord.cpp
@@ -213,6 +213,13 @@
     mFrameCount = mCblk->frameCount;
     mChannelCount = (uint8_t)channelCount;
     mChannelMask = channelMask;
+
+    if (audio_is_linear_pcm(mFormat)) {
+        mFrameSize = channelCount * audio_bytes_per_sample(format);
+    } else {
+        mFrameSize = sizeof(uint8_t);
+    }
+
     mActive = false;
     mCbf = cbf;
     mNotificationFrames = notificationFrames;
@@ -258,15 +265,6 @@
     return mFrameCount;
 }
 
-size_t AudioRecord::frameSize() const
-{
-    if (audio_is_linear_pcm(mFormat)) {
-        return channelCount()*audio_bytes_per_sample(mFormat);
-    } else {
-        return sizeof(uint8_t);
-    }
-}
-
 audio_source_t AudioRecord::inputSource() const
 {
     return mInputSource;
@@ -560,8 +558,8 @@
     }
 
     audioBuffer->frameCount  = framesReq;
-    audioBuffer->size        = framesReq*cblk->frameSize;
-    audioBuffer->raw         = cblk->buffer(mBuffers, u);
+    audioBuffer->size        = framesReq * mFrameSize;
+    audioBuffer->raw         = cblk->buffer(mBuffers, mFrameSize, u);
     active = mActive;
     return active ? status_t(NO_ERROR) : status_t(STOPPED);
 }
diff --git a/media/libmedia/AudioTrack.cpp b/media/libmedia/AudioTrack.cpp
index 26cf877..4a4759e 100644
--- a/media/libmedia/AudioTrack.cpp
+++ b/media/libmedia/AudioTrack.cpp
@@ -286,6 +286,15 @@
     mFormat = format;
     mChannelMask = channelMask;
     mChannelCount = channelCount;
+
+    if (audio_is_linear_pcm(format)) {
+        mFrameSize = channelCount * audio_bytes_per_sample(format);
+        mFrameSizeAF = channelCount * sizeof(int16_t);
+    } else {
+        mFrameSize = sizeof(uint8_t);
+        mFrameSizeAF = sizeof(uint8_t);
+    }
+
     mSharedBuffer = sharedBuffer;
     mMuted = false;
     mActive = false;
@@ -332,15 +341,6 @@
     return mCblk->frameCount;
 }
 
-size_t AudioTrack::frameSize() const
-{
-    if (audio_is_linear_pcm(mFormat)) {
-        return channelCount()*audio_bytes_per_sample(mFormat);
-    } else {
-        return sizeof(uint8_t);
-    }
-}
-
 sp<IMemory>& AudioTrack::sharedBuffer()
 {
     return mSharedBuffer;
@@ -1026,8 +1026,8 @@
     }
 
     audioBuffer->frameCount = framesReq;
-    audioBuffer->size = framesReq * cblk->frameSize;
-    audioBuffer->raw = cblk->buffer(mBuffers, u);
+    audioBuffer->size = framesReq * mFrameSizeAF;
+    audioBuffer->raw = cblk->buffer(mBuffers, mFrameSizeAF, u);
     active = mActive;
     return active ? status_t(NO_ERROR) : status_t(STOPPED);
 }
@@ -1302,7 +1302,7 @@
         // NOTE: cblk->frameSize is not equal to AudioTrack::frameSize() for
         // 8 bit PCM data: in this case,  cblk->frameSize is based on a sample size of
         // 16 bit.
-        audioBuffer.frameCount = writtenSize/cblk->frameSize;
+        audioBuffer.frameCount = writtenSize / mFrameSizeAF;
 
         frames -= audioBuffer.frameCount;
 
@@ -1373,7 +1373,7 @@
                 if (user > server) {
                     frames = ((user - server) > newCblk->frameCount) ?
                             newCblk->frameCount : (user - server);
-                    memset(mBuffers, 0, frames * newCblk->frameSize);
+                    memset(mBuffers, 0, frames * mFrameSizeAF);
                 }
                 // restart playback even if buffer is not completely filled.
                 android_atomic_or(CBLK_FORCEREADY, &newCblk->flags);
@@ -1588,7 +1588,7 @@
     return true;
 }
 
-void* audio_track_cblk_t::buffer(void *buffers, uint32_t offset) const
+void* audio_track_cblk_t::buffer(void *buffers, size_t frameSize, uint32_t offset) const
 {
     return (int8_t *)buffers + (offset - userBase) * frameSize;
 }
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index c0f5c7b..eb20019 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -4190,6 +4190,7 @@
         mState(IDLE),
         mSampleRate(sampleRate),
         mFormat(format),
+        mFrameSize(0),  // will be set to correct value in constructor
         mStepServerFailed(false),
         mSessionId(sessionId)
         // mChannelCount
@@ -4311,7 +4312,7 @@
 
 void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const {
     audio_track_cblk_t* cblk = this->cblk();
-    size_t frameSize = cblk->frameSize;
+    size_t frameSize = mFrameSize;
     int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize;
     int8_t *bufferEnd = bufferStart + frames * frameSize;
 
@@ -4363,11 +4364,11 @@
     mUnderrunCount(0),
     mCachedVolume(1.0)
 {
+    // NOTE: frame size for 8 bit PCM data is based on a sample size of
+    // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
+    mFrameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) :
+            sizeof(uint8_t);
     if (mCblk != NULL) {
-        // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
-        // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
-        mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) :
-                sizeof(uint8_t);
         // to avoid leaking a track name, do not allocate one unless there is an mCblk
         mName = thread->getTrackName_l(channelMask, sessionId);
         mCblk->mName = mName;
@@ -5014,7 +5015,7 @@
             // this frame in media time units and adding it to the PTS of the
             // buffer.
             int64_t frameCount = mTimedBufferQueue[trimEnd].buffer()->size()
-                               / mCblk->frameSize;
+                               / mFrameSize;
 
             if (!mMediaTimeToSampleTransform.doReverseTransform(frameCount,
                                                                 &bufEnd)) {
@@ -5074,7 +5075,7 @@
                 " bytes.  (update reason: \"%s\")",
                 bufBytes, consumedAlready, logTag);
 
-    uint32_t bufFrames = (bufBytes - consumedAlready) / mCblk->frameSize;
+    uint32_t bufFrames = (bufBytes - consumedAlready) / mFrameSize;
     ALOG_ASSERT(mFramesPendingInQueue >= bufFrames,
                 "Bad bookkeeping while updating frames pending.  Should have at"
                 " least %u queued frames, but we think we have only %u.  (update"
@@ -5095,7 +5096,7 @@
 
     Mutex::Autolock _l(mTimedBufferQueueLock);
 
-    uint32_t bufFrames = buffer->size() / mCblk->frameSize;
+    uint32_t bufFrames = buffer->size() / mFrameSize;
     mFramesPendingInQueue += bufFrames;
     mTimedBufferQueue.add(TimedBuffer(buffer, pts));
 
@@ -5192,7 +5193,7 @@
         // adjust the head buffer's PTS to reflect the portion of the head buffer
         // that has already been consumed
         int64_t effectivePTS = headLocalPTS +
-                ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate());
+                ((head.position() / mFrameSize) * mLocalTimeFreq / sampleRate());
 
         // Calculate the delta in samples between the head of the input buffer
         // queue and the start of the next output buffer that will be written.
@@ -5257,7 +5258,7 @@
             // the next input sample is late
             uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32));
             size_t onTimeSamplePosition =
-                    head.position() + lateFrames * mCblk->frameSize;
+                    head.position() + lateFrames * mFrameSize;
 
             if (onTimeSamplePosition > head.buffer()->size()) {
                 // all the remaining samples in the head are too late, so
@@ -5292,7 +5293,7 @@
                    head.position());
 
     uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) /
-                                 mCblk->frameSize);
+                                 mFrameSize);
     size_t framesRequested = buffer->frameCount;
     buffer->frameCount = min(framesLeftInHead, framesRequested);
 
@@ -5307,9 +5308,9 @@
     uint32_t numFrames, AudioBufferProvider::Buffer* buffer) {
 
     // lazily allocate a buffer filled with silence
-    if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) {
+    if (mTimedSilenceBufferSize < numFrames * mFrameSize) {
         delete [] mTimedSilenceBuffer;
-        mTimedSilenceBufferSize = numFrames * mCblk->frameSize;
+        mTimedSilenceBufferSize = numFrames * mFrameSize;
         mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize];
         memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize);
     }
@@ -5357,7 +5358,7 @@
                     start, end, buffer->raw);
 
         head.setPosition(head.position() +
-                (buffer->frameCount * mCblk->frameSize));
+                (buffer->frameCount * mFrameSize));
         mQueueHeadInFlight = false;
 
         ALOG_ASSERT(mFramesPendingInQueue >= buffer->frameCount,
@@ -5409,15 +5410,13 @@
                   channelMask, frameCount, 0 /*sharedBuffer*/, sessionId),
         mOverflow(false)
 {
-    if (mCblk != NULL) {
-        ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
-        if (format == AUDIO_FORMAT_PCM_16_BIT) {
-            mCblk->frameSize = mChannelCount * sizeof(int16_t);
-        } else if (format == AUDIO_FORMAT_PCM_8_BIT) {
-            mCblk->frameSize = mChannelCount * sizeof(int8_t);
-        } else {
-            mCblk->frameSize = sizeof(int8_t);
-        }
+    ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
+    if (format == AUDIO_FORMAT_PCM_16_BIT) {
+        mFrameSize = mChannelCount * sizeof(int16_t);
+    } else if (format == AUDIO_FORMAT_PCM_8_BIT) {
+        mFrameSize = mChannelCount * sizeof(int8_t);
+    } else {
+        mFrameSize = sizeof(int8_t);
     }
 }
 
@@ -5749,7 +5748,7 @@
     }
 
     buffer->frameCount  = framesReq;
-    buffer->raw         = cblk->buffer(mBuffers, u);
+    buffer->raw         = cblk->buffer(mBuffers, mFrameSize, u);
     return NO_ERROR;
 }
 
@@ -6157,7 +6156,7 @@
                         if (framesIn) {
                             int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize;
                             int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) *
-                                    mActiveTrack->mCblk->frameSize;
+                                    mActiveTrack->mFrameSize;
                             if (framesIn > framesOut)
                                 framesIn = framesOut;
                             mRsmpInIndex += framesIn;
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index fc24bed..38744d0 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -472,6 +472,10 @@
             const uint32_t      mSampleRate;    // initial sample rate only; for tracks which
                                 // support dynamic rates, the current value is in control block
             const audio_format_t mFormat;
+            size_t              mFrameSize; // AudioFlinger's view of frame size in shared memory,
+                                            // where for AudioTrack (but not AudioRecord),
+                                            // 8-bit PCM samples are stored as 16-bit
+                                            // FIXME should be const
             bool                mStepServerFailed;
             const int           mSessionId;
             uint8_t             mChannelCount;