audio: fix audio glitch when closing pcm stream

Holding the audio device mutex while calling pcm_close() is not necessary
and will cause writes on other output streams to be blocked until close
completes which can take several hundred milliseconds.

Not holding the audio device mutex during the whole standby sequence
forces to change the lock order between audio device and output stream mutex.

The result is that we do not acquire the audio device mutex systematically before
the stream mutex in out_write(). This is not a problem with this audio HAL
as set_mode() does not acquire the stream mutex and out_set_parameters() is always
called in the same thread (same priority) as out_write().

Same change done for input threads.

Bug 8267567.

Change-Id: I17bb187c0564200f6362586885e61500d52d5bc2
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 67f0b3a..0781688 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -138,7 +138,7 @@
 
 struct stream_out {
     struct audio_stream_out stream;
-    pthread_mutex_t lock;
+    pthread_mutex_t lock; /* see note below on mutex acquisition order */
     struct pcm_config config;
     struct pcm *pcm;
     int standby;
@@ -155,7 +155,7 @@
 
 struct stream_in {
     struct audio_stream_in stream;
-    pthread_mutex_t lock;
+    pthread_mutex_t lock; /* see note below on mutex acquisition order */
     struct pcm_config config;
     struct pcm *pcm;
     int standby;
@@ -198,7 +198,7 @@
 
 struct audio_device {
     struct audio_hw_device device;
-    pthread_mutex_t lock;
+    pthread_mutex_t lock; /* see note below on mutex acquisition order */
     struct mixer *mixer;
     audio_mode_t mode;
     audio_devices_t out_device;
@@ -243,6 +243,11 @@
     csd_stop_voice_t csd_stop_voice;
 };
 
+/*
+ * NOTE: when multiple mutexes have to be acquired, always take the
+ * stream_in or stream_out mutex first, followed by the audio_device mutex.
+ */
+
 struct pcm_config pcm_config_deep_buffer = {
     .channels = 2,
     .rate = DEFAULT_OUTPUT_SAMPLING_RATE,