Audio Policy Refactor: reorganise headers inclusion

This patch reoganises the headers inclusion in order to prepare
the split of managerdefault into a manager and a separated lib
of pillar policy elements.

It also moves back the isStrategyActive to the manager to avoid
any dependancies from this pillars to the manager.

Change-Id: I1a35c45d86db7a3878a40dc7d6858dfea37c5ac9
Signed-off-by: François Gaffie <francois.gaffie@intel.com>
diff --git a/services/audiopolicy/managerdefault/ApmImplDefinitions.h b/services/audiopolicy/managerdefault/ApmImplDefinitions.h
index 620979b..62927da 100644
--- a/services/audiopolicy/managerdefault/ApmImplDefinitions.h
+++ b/services/audiopolicy/managerdefault/ApmImplDefinitions.h
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#pragma once
+
 namespace android {
 
 enum routing_strategy {
diff --git a/services/audiopolicy/managerdefault/AudioInputDescriptor.cpp b/services/audiopolicy/managerdefault/AudioInputDescriptor.cpp
index f4054c8..ce6b1e7 100644
--- a/services/audiopolicy/managerdefault/AudioInputDescriptor.cpp
+++ b/services/audiopolicy/managerdefault/AudioInputDescriptor.cpp
@@ -17,7 +17,11 @@
 #define LOG_TAG "APM::AudioInputDescriptor"
 //#define LOG_NDEBUG 0
 
-#include "AudioPolicyManager.h"
+#include "AudioInputDescriptor.h"
+#include "IOProfile.h"
+#include "Gains.h"
+#include "HwModule.h"
+#include <media/AudioPolicy.h>
 
 namespace android {
 
diff --git a/services/audiopolicy/managerdefault/AudioInputDescriptor.h b/services/audiopolicy/managerdefault/AudioInputDescriptor.h
index 02579e6..ce96228 100644
--- a/services/audiopolicy/managerdefault/AudioInputDescriptor.h
+++ b/services/audiopolicy/managerdefault/AudioInputDescriptor.h
@@ -14,8 +14,18 @@
  * limitations under the License.
  */
 
+#pragma once
+
+#include "Ports.h"
+#include <utils/Errors.h>
+#include <system/audio.h>
+#include <utils/SortedVector.h>
+
 namespace android {
 
+class IOProfile;
+class AudioMix;
+
 // descriptor for audio inputs. Used to maintain current configuration of each opened audio input
 // and keep track of the usage of this input.
 class AudioInputDescriptor: public AudioPortConfig
diff --git a/services/audiopolicy/managerdefault/AudioOutputDescriptor.cpp b/services/audiopolicy/managerdefault/AudioOutputDescriptor.cpp
index 4b85972..4dd9316 100644
--- a/services/audiopolicy/managerdefault/AudioOutputDescriptor.cpp
+++ b/services/audiopolicy/managerdefault/AudioOutputDescriptor.cpp
@@ -17,7 +17,11 @@
 #define LOG_TAG "APM::AudioOutputDescriptor"
 //#define LOG_NDEBUG 0
 
-#include "AudioPolicyManager.h"
+#include "AudioOutputDescriptor.h"
+#include "IOProfile.h"
+#include "Gains.h"
+#include "HwModule.h"
+#include <media/AudioPolicy.h>
 
 namespace android {
 
@@ -108,23 +112,15 @@
 
 bool AudioOutputDescriptor::isActive(uint32_t inPastMs) const
 {
-    return isStrategyActive(NUM_STRATEGIES, inPastMs);
-}
-
-bool AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
-                                                                       uint32_t inPastMs,
-                                                                       nsecs_t sysTime) const
-{
-    if ((sysTime == 0) && (inPastMs != 0)) {
+    nsecs_t sysTime = 0;
+    if (inPastMs != 0) {
         sysTime = systemTime();
     }
     for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
         if (i == AUDIO_STREAM_PATCH) {
             continue;
         }
-        if (((AudioPolicyManager::getStrategy((audio_stream_type_t)i) == strategy) ||
-                (NUM_STRATEGIES == strategy)) &&
-                isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
+        if (isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
             return true;
         }
     }
diff --git a/services/audiopolicy/managerdefault/AudioOutputDescriptor.h b/services/audiopolicy/managerdefault/AudioOutputDescriptor.h
index 32f46e4..f7a06ee 100644
--- a/services/audiopolicy/managerdefault/AudioOutputDescriptor.h
+++ b/services/audiopolicy/managerdefault/AudioOutputDescriptor.h
@@ -14,10 +14,19 @@
  * limitations under the License.
  */
 
+#pragma once
+
+#include "Ports.h"
 #include "ApmImplDefinitions.h"
+#include <utils/Errors.h>
+#include <utils/Timers.h>
+#include <system/audio.h>
 
 namespace android {
 
+class IOProfile;
+class AudioMix;
+
 // descriptor for audio outputs. Used to maintain current configuration of each opened audio output
 // and keep track of the usage of this output by each audio stream type.
 class AudioOutputDescriptor: public AudioPortConfig
@@ -38,9 +47,6 @@
     bool isStreamActive(audio_stream_type_t stream,
                         uint32_t inPastMs = 0,
                         nsecs_t sysTime = 0) const;
-    bool isStrategyActive(routing_strategy strategy,
-                     uint32_t inPastMs = 0,
-                     nsecs_t sysTime = 0) const;
 
     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
                            const struct audio_port_config *srcConfig = NULL) const;
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index 53ec0f6..730d32f 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -518,12 +518,12 @@
             // mute media and sonification strategies and delay device switch by the largest
             // latency of any output where either strategy is active.
             // This avoid sending the ring tone or music tail into the earpiece or headset.
-            if ((desc->isStrategyActive(STRATEGY_MEDIA,
-                                     SONIFICATION_HEADSET_MUSIC_DELAY,
-                                     sysTime) ||
-                    desc->isStrategyActive(STRATEGY_SONIFICATION,
-                                         SONIFICATION_HEADSET_MUSIC_DELAY,
-                                         sysTime)) &&
+            if ((isStrategyActive(desc, STRATEGY_MEDIA,
+                                  SONIFICATION_HEADSET_MUSIC_DELAY,
+                                  sysTime) ||
+                 isStrategyActive(desc, STRATEGY_SONIFICATION,
+                                  SONIFICATION_HEADSET_MUSIC_DELAY,
+                                  sysTime)) &&
                     (delayMs < (int)desc->mLatency*2)) {
                 delayMs = desc->mLatency*2;
             }
@@ -3971,7 +3971,7 @@
         // mute strategy while moving tracks from one output to another
         for (size_t i = 0; i < srcOutputs.size(); i++) {
             sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]);
-            if (desc->isStrategyActive(strategy)) {
+            if (isStrategyActive(desc, strategy)) {
                 setStrategyMute(strategy, true, srcOutputs[i]);
                 setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
             }
@@ -4117,27 +4117,27 @@
     //      use device for strategy DTMF
     // 9: the strategy for beacon, a.k.a. "transmitted through speaker" is active on the output:
     //      use device for strategy t-t-s
-    if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE) &&
+    if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) &&
         mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
         device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
     } else if (isInCall() ||
-                    outputDesc->isStrategyActive(STRATEGY_PHONE)) {
+                    isStrategyActive(outputDesc, STRATEGY_PHONE)) {
         device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
-    } else if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
+    } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) {
         device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
-    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
+    } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) {
         device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
-    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
+    } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) {
         device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
-    } else if (outputDesc->isStrategyActive(STRATEGY_ACCESSIBILITY)) {
+    } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) {
         device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache);
-    } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
+    } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) {
         device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
-    } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
+    } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) {
         device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
-    } else if (outputDesc->isStrategyActive(STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
+    } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) {
         device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache);
-    } else if (outputDesc->isStrategyActive(STRATEGY_REROUTING)) {
+    } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) {
         device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache);
     }
 
@@ -4182,7 +4182,7 @@
     SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs);
     for (size_t i = 0; i < outputs.size(); i++) {
         sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]);
-        if (outputDesc->isStrategyActive(strategy)) {
+        if (isStrategyActive(outputDesc, strategy)) {
             devices = outputDesc->device();
             break;
         }
@@ -4698,7 +4698,7 @@
                 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
                       mute ? "muting" : "unmuting", i, curDevice, curOutput);
                 setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
-                if (desc->isStrategyActive((routing_strategy)i)) {
+                if (isStrategyActive(desc, (routing_strategy)i)) {
                     if (mute) {
                         // FIXME: should not need to double latency if volume could be applied
                         // immediately by the audioflinger mixer. We must account for the delay
@@ -4721,7 +4721,7 @@
             muteWaitMs = outputDesc->latency() * 2;
         }
         for (size_t i = 0; i < NUM_STRATEGIES; i++) {
-            if (outputDesc->isStrategyActive((routing_strategy)i)) {
+            if (isStrategyActive(outputDesc, (routing_strategy)i)) {
                 setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle);
                 // do tempMute unmute after twice the mute wait time
                 setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle,
@@ -5763,4 +5763,25 @@
     return true;
 }
 
+
+bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc,
+                                          routing_strategy strategy, uint32_t inPastMs,
+                                          nsecs_t sysTime) const
+{
+    if ((sysTime == 0) && (inPastMs != 0)) {
+        sysTime = systemTime();
+    }
+    for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) {
+        if (i == AUDIO_STREAM_PATCH) {
+            continue;
+        }
+        if (((getStrategy((audio_stream_type_t)i) == strategy) ||
+                (NUM_STRATEGIES == strategy)) &&
+                outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) {
+            return true;
+        }
+    }
+    return false;
+}
+
 }; // namespace android
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.h b/services/audiopolicy/managerdefault/AudioPolicyManager.h
index 61ea6f2..8308d54 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.h
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.h
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#pragma once
 
 #include <stdint.h>
 #include <sys/types.h>
@@ -61,9 +62,6 @@
 // Can be overridden by the audio.offload.min.duration.secs property
 #define OFFLOAD_DEFAULT_MIN_DURATION_SECS 60
 
-#define MAX_MIXER_SAMPLING_RATE 48000
-#define MAX_MIXER_CHANNEL_COUNT 8
-
 // ----------------------------------------------------------------------------
 // AudioPolicyManager implements audio policy manager behavior common to all platforms.
 // ----------------------------------------------------------------------------
@@ -555,6 +553,9 @@
         sp<DeviceDescriptor>  getDeviceDescriptor(const audio_devices_t device,
                                                   const char *device_address,
                                                   const char *device_name);
+
+        bool isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, routing_strategy strategy,
+                              uint32_t inPastMs = 0, nsecs_t sysTime = 0) const;
 };
 
 };
diff --git a/services/audiopolicy/managerdefault/ConfigParsingUtils.cpp b/services/audiopolicy/managerdefault/ConfigParsingUtils.cpp
index 1afd487..300f35a 100644
--- a/services/audiopolicy/managerdefault/ConfigParsingUtils.cpp
+++ b/services/audiopolicy/managerdefault/ConfigParsingUtils.cpp
@@ -17,7 +17,8 @@
 #define LOG_TAG "APM::ConfigParsingUtils"
 //#define LOG_NDEBUG 0
 
-#include "AudioPolicyManager.h"
+#include "ConfigParsingUtils.h"
+#include <utils/Log.h>
 
 namespace android {
 
diff --git a/services/audiopolicy/managerdefault/ConfigParsingUtils.h b/services/audiopolicy/managerdefault/ConfigParsingUtils.h
index b2d9763..45e96d9 100644
--- a/services/audiopolicy/managerdefault/ConfigParsingUtils.h
+++ b/services/audiopolicy/managerdefault/ConfigParsingUtils.h
@@ -14,6 +14,11 @@
  * limitations under the License.
  */
 
+#pragma once
+
+#include <system/audio.h>
+#include <sys/types.h>
+
 namespace android {
 
 // ----------------------------------------------------------------------------
diff --git a/services/audiopolicy/managerdefault/Devices.cpp b/services/audiopolicy/managerdefault/Devices.cpp
index 5b1401e..574cff5 100644
--- a/services/audiopolicy/managerdefault/Devices.cpp
+++ b/services/audiopolicy/managerdefault/Devices.cpp
@@ -17,7 +17,10 @@
 #define LOG_TAG "APM::Devices"
 //#define LOG_NDEBUG 0
 
-#include "AudioPolicyManager.h"
+#include "Devices.h"
+#include "Gains.h"
+#include "HwModule.h"
+#include "ConfigParsingUtils.h"
 
 namespace android {
 
diff --git a/services/audiopolicy/managerdefault/Devices.h b/services/audiopolicy/managerdefault/Devices.h
index af2fbda..7ccf559 100644
--- a/services/audiopolicy/managerdefault/Devices.h
+++ b/services/audiopolicy/managerdefault/Devices.h
@@ -14,10 +14,16 @@
  * limitations under the License.
  */
 
-namespace android {
+#pragma once
 
-class AudioPort;
-class AudioPortConfig;
+#include "Ports.h"
+#include <utils/Errors.h>
+#include <utils/String8.h>
+#include <utils/SortedVector.h>
+#include <cutils/config_utils.h>
+#include <system/audio.h>
+
+namespace android {
 
 class DeviceDescriptor: public AudioPort, public AudioPortConfig
 {
diff --git a/services/audiopolicy/managerdefault/Gains.cpp b/services/audiopolicy/managerdefault/Gains.cpp
index 4aca26d..98a8d1c 100644
--- a/services/audiopolicy/managerdefault/Gains.cpp
+++ b/services/audiopolicy/managerdefault/Gains.cpp
@@ -24,7 +24,9 @@
 #define ALOGVV(a...) do { } while(0)
 #endif
 
-#include "AudioPolicyManager.h"
+#include "Gains.h"
+#include <utils/Log.h>
+#include <utils/String8.h>
 
 #include <math.h>
 
diff --git a/services/audiopolicy/managerdefault/Gains.h b/services/audiopolicy/managerdefault/Gains.h
index b4ab129..f638c8e 100644
--- a/services/audiopolicy/managerdefault/Gains.h
+++ b/services/audiopolicy/managerdefault/Gains.h
@@ -14,6 +14,13 @@
  * limitations under the License.
  */
 
+#pragma once
+
+#include <utils/Errors.h>
+#include <utils/RefBase.h>
+#include <system/audio.h>
+#include <utils/KeyedVector.h>
+
 namespace android {
 
 class VolumeCurvePoint
diff --git a/services/audiopolicy/managerdefault/HwModule.cpp b/services/audiopolicy/managerdefault/HwModule.cpp
index a04bdc8..39dc889 100644
--- a/services/audiopolicy/managerdefault/HwModule.cpp
+++ b/services/audiopolicy/managerdefault/HwModule.cpp
@@ -17,7 +17,10 @@
 #define LOG_TAG "APM::HwModule"
 //#define LOG_NDEBUG 0
 
-#include "AudioPolicyManager.h"
+#include "HwModule.h"
+#include "IOProfile.h"
+#include "Gains.h"
+#include "ConfigParsingUtils.h"
 #include "audio_policy_conf.h"
 #include <hardware/audio.h>
 
diff --git a/services/audiopolicy/managerdefault/HwModule.h b/services/audiopolicy/managerdefault/HwModule.h
index f814dd9..d9e6cdf 100644
--- a/services/audiopolicy/managerdefault/HwModule.h
+++ b/services/audiopolicy/managerdefault/HwModule.h
@@ -14,8 +14,20 @@
  * limitations under the License.
  */
 
+#pragma once
+
+#include "Devices.h"
+#include <utils/RefBase.h>
+#include <utils/String8.h>
+#include <utils/Errors.h>
+#include <utils/Vector.h>
+#include <system/audio.h>
+#include <cutils/config_utils.h>
+
 namespace android {
 
+class IOProfile;
+
 class HwModule : public RefBase
 {
 public:
diff --git a/services/audiopolicy/managerdefault/IOProfile.cpp b/services/audiopolicy/managerdefault/IOProfile.cpp
index 8000914..f82ff92 100644
--- a/services/audiopolicy/managerdefault/IOProfile.cpp
+++ b/services/audiopolicy/managerdefault/IOProfile.cpp
@@ -17,7 +17,9 @@
 #define LOG_TAG "APM::IOProfile"
 //#define LOG_NDEBUG 0
 
-#include "AudioPolicyManager.h"
+#include "IOProfile.h"
+#include "HwModule.h"
+#include "Gains.h"
 
 namespace android {
 
diff --git a/services/audiopolicy/managerdefault/IOProfile.h b/services/audiopolicy/managerdefault/IOProfile.h
index 3317969..a9d93d1 100644
--- a/services/audiopolicy/managerdefault/IOProfile.h
+++ b/services/audiopolicy/managerdefault/IOProfile.h
@@ -14,6 +14,13 @@
  * limitations under the License.
  */
 
+#pragma once
+
+#include "Ports.h"
+#include "Devices.h"
+#include <utils/String8.h>
+#include <system/audio.h>
+
 namespace android {
 
 class HwModule;
diff --git a/services/audiopolicy/managerdefault/Ports.cpp b/services/audiopolicy/managerdefault/Ports.cpp
index 3e55cee..326f9c5 100644
--- a/services/audiopolicy/managerdefault/Ports.cpp
+++ b/services/audiopolicy/managerdefault/Ports.cpp
@@ -17,12 +17,16 @@
 #define LOG_TAG "APM::Ports"
 //#define LOG_NDEBUG 0
 
-#include "AudioPolicyManager.h"
-
+#include "Ports.h"
+#include "HwModule.h"
+#include "Gains.h"
+#include "ConfigParsingUtils.h"
 #include "audio_policy_conf.h"
 
 namespace android {
 
+int32_t volatile AudioPort::mNextUniqueId = 1;
+
 // --- AudioPort class implementation
 
 AudioPort::AudioPort(const String8& name, audio_port_type_t type,
@@ -34,7 +38,7 @@
 }
 
 void AudioPort::attach(const sp<HwModule>& module) {
-    mId = AudioPolicyManager::nextUniqueId();
+    mId = android_atomic_inc(&mNextUniqueId);
     mModule = module;
 }
 
diff --git a/services/audiopolicy/managerdefault/Ports.h b/services/audiopolicy/managerdefault/Ports.h
index f6e0e93..6e0e2fe 100644
--- a/services/audiopolicy/managerdefault/Ports.h
+++ b/services/audiopolicy/managerdefault/Ports.h
@@ -14,9 +14,22 @@
  * limitations under the License.
  */
 
+#pragma once
+
+#include <utils/String8.h>
+#include <utils/Vector.h>
+#include <utils/RefBase.h>
+#include <utils/Errors.h>
+#include <system/audio.h>
+#include <cutils/config_utils.h>
+
+#define MAX_MIXER_SAMPLING_RATE 48000
+#define MAX_MIXER_CHANNEL_COUNT 8
+
 namespace android {
 
 class HwModule;
+class AudioGain;
 
 class AudioPort: public virtual RefBase
 {
@@ -86,6 +99,9 @@
     // and a unique ID for identifying a port to the (upcoming) selection API,
     // and its relationship to the mId in AudioOutputDescriptor and AudioInputDescriptor.
     audio_port_handle_t mId;
+
+private:
+    static volatile int32_t mNextUniqueId;
 };
 
 class AudioPortConfig: public virtual RefBase
diff --git a/services/audiopolicy/managerdefault/audio_policy_conf.h b/services/audiopolicy/managerdefault/audio_policy_conf.h
index 2535a67..441bf7b 100644
--- a/services/audiopolicy/managerdefault/audio_policy_conf.h
+++ b/services/audiopolicy/managerdefault/audio_policy_conf.h
@@ -14,10 +14,7 @@
  * limitations under the License.
  */
 
-
-#ifndef ANDROID_AUDIO_POLICY_CONF_H
-#define ANDROID_AUDIO_POLICY_CONF_H
-
+#pragma once
 
 /////////////////////////////////////////////////
 //      Definitions for audio policy configuration file (audio_policy.conf)
@@ -71,7 +68,3 @@
 #define GAIN_STEP_VALUE "step_value_mB"
 #define GAIN_MIN_RAMP_MS "min_ramp_ms"
 #define GAIN_MAX_RAMP_MS "max_ramp_ms"
-
-
-
-#endif  // ANDROID_AUDIO_POLICY_CONF_H