audiohal: Make sure audio data transfer related commands go via FMQ

When outputting audio, the framework issues several HAL calls
from the same thread that writes into data FMQ. These calls
also need to be served on the same thread that writes audio data
to HAL. The same thing happens when audio input is commenced.

Add a command FMQ for passing different commands to the HAL thread.
This way, depending on the calling thread, the same call may go
either via hwbinder or via the command queue.

This dramatically reduces jitter in RTT measurements (although
doesn't improve the latency).

Bug: 30222631
Test: scripted RTT app
Change-Id: I04c826e2479d8210fd9c99756241156cda3143b6
diff --git a/audio/2.0/IStreamOut.hal b/audio/2.0/IStreamOut.hal
index 336684f..2ec080d 100644
--- a/audio/2.0/IStreamOut.hal
+++ b/audio/2.0/IStreamOut.hal
@@ -44,44 +44,57 @@
     setVolume(float left, float right) generates (Result retval);
 
     /*
+     * Commands that can be executed on the driver writer thread.
+     */
+    enum WriteCommand : int32_t {
+        WRITE,
+        GET_PRESENTATION_POSITION,
+        GET_LATENCY
+    };
+
+    /*
      * Data structure passed back to the client via status message queue
      * of 'write' operation.
      *
-     * Possible values of 'writeRetval' field:
+     * Possible values of 'retval' field:
      *  - OK, write operation was successful;
      *  - INVALID_ARGUMENTS, stream was not configured properly;
-     *  - INVALID_STATE, stream is in a state that doesn't allow writes.
-     *
-     * Possible values of 'presentationPositionRetval' field (must only
-     * be considered if 'writeRetval' field is set to 'OK'):
-     *  - OK, presentation position retrieved successfully;
-     *  - INVALID_ARGUMENTS, indicates that the position can't be retrieved;
-     *  - INVALID_OPERATION, retrieving presentation position isn't supported;
+     *  - INVALID_STATE, stream is in a state that doesn't allow writes;
+     *  - INVALID_OPERATION, retrieving presentation position isn't supported.
      */
     struct WriteStatus {
-        Result writeRetval;
-        uint64_t written;
-        Result presentationPositionRetval;
-        uint64_t frames;    // presentation position
-        TimeSpec timeStamp; // presentation position
+        Result retval;
+        WriteCommand replyTo;  // discriminator
+        union Reply {
+            uint64_t written;  // WRITE command, amount of bytes written, >= 0.
+            struct PresentationPosition {  // same as generated by
+                uint64_t frames;           // getPresentationPosition.
+                TimeSpec timeStamp;
+            } presentationPosition;
+            uint32_t latencyMs; // Same as generated by getLatency.
+        } reply;
     };
 
     /*
      * Set up required transports for passing audio buffers to the driver.
      *
-     * The transport consists of two message queues: one is used for passing
-     * audio data from the client to the driver, another is used for reporting
-     * write operation status (amount of bytes actually written or error code),
-     * and the presentation position immediately after the write, see
-     * WriteStatus structure definition.
+     * The transport consists of three message queues:
+     *  -- command queue is used to instruct the writer thread what operation
+     *     to perform;
+     *  -- data queue is used for passing audio data from the client
+     *     to the driver;
+     *  -- status queue is used for reporting operation status
+     *     (e.g. amount of bytes actually written or error code).
+     * The driver operates on a dedicated thread.
      *
      * @param frameSize the size of a single frame, in bytes.
      * @param framesCount the number of frames in a buffer.
-     * @param threadPriority priority of the thread that performs writes.
+     * @param threadPriority priority of the driver thread.
      * @return retval OK if both message queues were created successfully.
      *                INVALID_STATE if the method was already called.
      *                INVALID_ARGUMENTS if there was a problem setting up
      *                                  the queues.
+     * @return commandMQ a message queue used for passing commands.
      * @return dataMQ a message queue used for passing audio data in the format
      *                specified at the stream opening.
      * @return statusMQ a message queue used for passing status from the driver
@@ -92,7 +105,9 @@
             ThreadPriority threadPriority)
     generates (
             Result retval,
-            fmq_sync<uint8_t> dataMQ, fmq_sync<WriteStatus> statusMQ);
+            fmq_sync<WriteCommand> commandMQ,
+            fmq_sync<uint8_t> dataMQ,
+            fmq_sync<WriteStatus> statusMQ);
 
     /*
      * Return the number of audio frames written by the audio DSP to DAC since