audioflinger: only hold wakeLock when started

The AAudio MMAP streams used to hold a wakeLock from
the time they were opened until closed.
Now it is acquired when the first stream is started
and released when the first stream is stopped.

This was tested by creating shared streams using
OboeTester and write_sine_callback and input_monitor_callback and
then adding lots of logs to see when the
wakeLock was acquired and released.

Note that ~ThreadBase() calls releaseWakeLock_l() so ~MmapThread()
does not need to.

Bug: 167345722
Test: Launch OboeTester
Test: Tap TEST OUTPUT
Test: Tap OPEN
Test: Enter: adb shell dumpsys power
Test: See no MmapPlayback wakelock
Test: Tap START
Test: Enter: adb shell dumpsys power
Test: See "ACQ MmapPlayback"
Test: Tap STOP
Test: Enter: adb shell dumpsys power
Test: See "REL MmapPlayback"

Test: atest AAudioTests
Change-Id: Iad87f5ebd22f20c89699e8b1a75d8df482046360
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index 662a0ca..1e0bcac 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -8781,7 +8781,6 @@
 
 AudioFlinger::MmapThread::~MmapThread()
 {
-    releaseWakeLock_l();
 }
 
 void AudioFlinger::MmapThread::onFirstRef()
@@ -8831,7 +8830,6 @@
         return NO_INIT;
     }
     mStandby = true;
-    acquireWakeLock();
     return mHalStream->createMmapBuffer(minSizeFrames, info);
 }
 
@@ -8870,8 +8868,12 @@
     status_t ret;
 
     if (*handle == mPortId) {
-        // for the first track, reuse portId and session allocated when the stream was opened
-        return exitStandby();
+        // For the first track, reuse portId and session allocated when the stream was opened.
+        ret = exitStandby();
+        if (ret == NO_ERROR) {
+            acquireWakeLock();
+        }
+        return ret;
     }
 
     audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE;
@@ -8992,6 +8994,7 @@
 
     if (handle == mPortId) {
         mHalStream->stop();
+        releaseWakeLock();
         return NO_ERROR;
     }