OSDN Git Service

aaudio: update state for legacy mode
authorPhil Burk <philburk@google.com>
Fri, 28 Jul 2017 22:12:13 +0000 (15:12 -0700)
committerPhil Burk <philburk@google.com>
Thu, 3 Aug 2017 00:44:16 +0000 (00:44 +0000)
Add logging to the loopback test.
Update state in the callback.
Implement AAudioStreamRecord::getFramesWritten().

Bug: 62781173
Bug: 64214193
Test: aaudio_loopback.cpp, look for STARTED state while running
Change-Id: Iba867d048dcfcafe6db1d095abe2766f1aabe608

media/libaaudio/examples/loopback/src/loopback.cpp
media/libaaudio/src/client/AudioStreamInternal.cpp
media/libaaudio/src/client/AudioStreamInternal.h
media/libaaudio/src/core/AudioStream.cpp
media/libaaudio/src/core/AudioStream.h
media/libaaudio/src/legacy/AudioStreamLegacy.cpp
media/libaaudio/src/legacy/AudioStreamRecord.cpp
media/libaaudio/src/legacy/AudioStreamRecord.h
media/libaaudio/src/legacy/AudioStreamTrack.cpp
media/libaaudio/src/legacy/AudioStreamTrack.h

index 144c941..df0df04 100644 (file)
@@ -417,11 +417,18 @@ int main(int argc, const char **argv)
             sleep(1);
             printf("%4d: ", i);
             loopbackData.loopbackProcessor->printStatus();
-            int64_t framesWritten = AAudioStream_getFramesWritten(loopbackData.inputStream);
-            int64_t framesRead = AAudioStream_getFramesRead(loopbackData.inputStream);
-            printf(" input written = %lld, read %lld, xruns = %d\n",
-                   (long long) framesWritten,
-                   (long long) framesRead,
+
+            int64_t inputFramesWritten = AAudioStream_getFramesWritten(loopbackData.inputStream);
+            int64_t inputFramesRead = AAudioStream_getFramesRead(loopbackData.inputStream);
+            int64_t outputFramesWritten = AAudioStream_getFramesWritten(outputStream);
+            int64_t outputFramesRead = AAudioStream_getFramesRead(outputStream);
+            printf(" INPUT: wr %lld rd %lld state %s, OUTPUT: wr %lld rd %lld state %s, xruns %d\n",
+                   (long long) inputFramesWritten,
+                   (long long) inputFramesRead,
+                   AAudio_convertStreamStateToText(AAudioStream_getState(loopbackData.inputStream)),
+                   (long long) outputFramesWritten,
+                   (long long) outputFramesRead,
+                   AAudio_convertStreamStateToText(AAudioStream_getState(outputStream)),
                    AAudioStream_getXRunCount(outputStream)
             );
         }
index 259c9b9..8c3045a 100644 (file)
@@ -363,7 +363,7 @@ aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId,
     }
 }
 
-aaudio_result_t AudioStreamInternal::updateStateWhileWaiting() {
+aaudio_result_t AudioStreamInternal::updateStateMachine() {
     if (isDataCallbackActive()) {
         return AAUDIO_OK; // state is getting updated by the callback thread read/write call
     }
index 607e734..13cf16c 100644 (file)
@@ -50,7 +50,7 @@ public:
                                        int64_t *framePosition,
                                        int64_t *timeNanoseconds) override;
 
-    virtual aaudio_result_t updateStateWhileWaiting() override;
+    virtual aaudio_result_t updateStateMachine() override;
 
     aaudio_result_t open(const AudioStreamBuilder &builder) override;
 
index 4859c69..6c4a193 100644 (file)
@@ -98,7 +98,7 @@ aaudio_result_t AudioStream::waitForStateChange(aaudio_stream_state_t currentSta
                                                 aaudio_stream_state_t *nextState,
                                                 int64_t timeoutNanoseconds)
 {
-    aaudio_result_t result = updateStateWhileWaiting();
+    aaudio_result_t result = updateStateMachine();
     if (result != AAUDIO_OK) {
         return result;
     }
@@ -112,7 +112,7 @@ aaudio_result_t AudioStream::waitForStateChange(aaudio_stream_state_t currentSta
         AudioClock::sleepForNanos(durationNanos);
         timeoutNanoseconds -= durationNanos;
 
-        aaudio_result_t result = updateStateWhileWaiting();
+        aaudio_result_t result = updateStateMachine();
         if (result != AAUDIO_OK) {
             return result;
         }
index e5fdcc6..ad18751 100644 (file)
@@ -68,10 +68,10 @@ public:
 
 
     /**
-     * Update state while in the middle of waitForStateChange()
+     * Update state machine.()
      * @return
      */
-    virtual aaudio_result_t updateStateWhileWaiting() = 0;
+    virtual aaudio_result_t updateStateMachine() = 0;
 
 
     // =========== End ABSTRACT methods ===========================
index dd5e3c0..2816bac 100644 (file)
@@ -94,12 +94,15 @@ void AudioStreamLegacy::processCallbackCommon(aaudio_callback_operation_t opcode
                 } else {
                     audioBuffer->size = 0;
                 }
-                break;
+
+                if (updateStateMachine() == AAUDIO_OK) {
+                    break; // don't fall through
+                }
             }
         }
         /// FALL THROUGH
 
-            // Stream got rerouted so we disconnect.
+        // Stream got rerouted so we disconnect.
         case AAUDIO_CALLBACK_OPERATION_DISCONNECTED: {
             setState(AAUDIO_STREAM_STATE_DISCONNECTED);
             ALOGD("processCallbackCommon() stream disconnected");
index 8e8070c..72ba751 100644 (file)
@@ -207,7 +207,7 @@ aaudio_result_t AudioStreamRecord::requestStart()
     if (mAudioRecord.get() == nullptr) {
         return AAUDIO_ERROR_INVALID_STATE;
     }
-    // Get current position so we can detect when the track is playing.
+    // Get current position so we can detect when the track is recording.
     status_t err = mAudioRecord->getPosition(&mPositionWhenStarting);
     if (err != OK) {
         return AAudioConvert_androidToAAudioResult(err);
@@ -235,7 +235,7 @@ aaudio_result_t AudioStreamRecord::requestStop() {
     return AAUDIO_OK;
 }
 
-aaudio_result_t AudioStreamRecord::updateStateWhileWaiting()
+aaudio_result_t AudioStreamRecord::updateStateMachine()
 {
     aaudio_result_t result = AAUDIO_OK;
     aaudio_wrapping_frames_t position;
@@ -292,6 +292,12 @@ aaudio_result_t AudioStreamRecord::read(void *buffer,
     }
     int32_t framesRead = (int32_t)(bytesRead / bytesPerFrame);
     incrementFramesRead(framesRead);
+
+    result = updateStateMachine();
+    if (result != AAUDIO_OK) {
+        return result;
+    }
+
     return (aaudio_result_t) framesRead;
 }
 
@@ -330,3 +336,21 @@ aaudio_result_t AudioStreamRecord::getTimestamp(clockid_t clockId,
     }
     return getBestTimestamp(clockId, framePosition, timeNanoseconds, &extendedTimestamp);
 }
+
+int64_t AudioStreamRecord::getFramesWritten() {
+    aaudio_wrapping_frames_t position;
+    status_t result;
+    switch (getState()) {
+        case AAUDIO_STREAM_STATE_STARTING:
+        case AAUDIO_STREAM_STATE_STARTED:
+        case AAUDIO_STREAM_STATE_STOPPING:
+            result = mAudioRecord->getPosition(&position);
+            if (result == OK) {
+                mFramesWritten.update32(position);
+            }
+            break;
+        default:
+            break;
+    }
+    return AudioStreamLegacy::getFramesWritten();
+}
index 2c6a7eb..c1723ba 100644 (file)
@@ -59,9 +59,11 @@ public:
 
     int32_t getXRunCount() const override;
 
+    int64_t getFramesWritten() override;
+
     int32_t getFramesPerBurst() const override;
 
-    aaudio_result_t updateStateWhileWaiting() override;
+    aaudio_result_t updateStateMachine() override;
 
     aaudio_direction_t getDirection() const override {
         return AAUDIO_DIRECTION_INPUT;
index 77f31e2..734ef8b 100644 (file)
@@ -296,7 +296,7 @@ aaudio_result_t AudioStreamTrack::requestStop() {
     return AAUDIO_OK;
 }
 
-aaudio_result_t AudioStreamTrack::updateStateWhileWaiting()
+aaudio_result_t AudioStreamTrack::updateStateMachine()
 {
     status_t err;
     aaudio_wrapping_frames_t position;
@@ -373,6 +373,12 @@ aaudio_result_t AudioStreamTrack::write(const void *buffer,
     }
     int32_t framesWritten = (int32_t)(bytesWritten / bytesPerFrame);
     incrementFramesWritten(framesWritten);
+
+    result = updateStateMachine();
+    if (result != AAUDIO_OK) {
+        return result;
+    }
+
     return framesWritten;
 }
 
index ff429ea..3230ac8 100644 (file)
@@ -67,7 +67,7 @@ public:
         return AAUDIO_DIRECTION_OUTPUT;
     }
 
-    aaudio_result_t updateStateWhileWaiting() override;
+    aaudio_result_t updateStateMachine() override;
 
     // This is public so it can be called from the C callback function.
     void processCallback(int event, void *info) override;
@@ -81,8 +81,7 @@ private:
     // adapts between variable sized blocks and fixed size blocks
     FixedBlockReader                 mFixedBlockReader;
 
-    // TODO add 64-bit position reporting to AudioRecord and use it.
-    aaudio_wrapping_frames_t         mPositionWhenStarting = 0;
+    // TODO add 64-bit position reporting to AudioTrack and use it.
     aaudio_wrapping_frames_t         mPositionWhenPausing = 0;
 };