AudioPolicyManager: AudioInputDescriptor open ref count cleanup

Always initialize the mOpenRefCount field.
Add functions for management for open ref count.

Change-Id: I0bbd021283047abfebbc108ced68c79e29297f25
diff --git a/services/audiopolicy/common/managerdefinitions/include/AudioInputDescriptor.h b/services/audiopolicy/common/managerdefinitions/include/AudioInputDescriptor.h
index 48d09ed..6d04811 100644
--- a/services/audiopolicy/common/managerdefinitions/include/AudioInputDescriptor.h
+++ b/services/audiopolicy/common/managerdefinitions/include/AudioInputDescriptor.h
@@ -36,6 +36,8 @@
     void setIoHandle(audio_io_handle_t ioHandle);
     audio_port_handle_t getId() const;
     audio_module_handle_t getModuleHandle() const;
+    void changeOpenRefCount(int delta);
+    uint32_t getOpenRefCount() const;
 
     status_t    dump(int fd);
 
@@ -43,9 +45,8 @@
     audio_devices_t               mDevice;         // current device this input is routed to
     AudioMix                      *mPolicyMix;     // non NULL when used by a dynamic policy
     audio_patch_handle_t          mPatchHandle;
-    uint32_t                      mRefCount;       // number of AudioRecord clients using
-    // this input
-    uint32_t                      mOpenRefCount;
+    uint32_t                      mRefCount;       // number of AudioRecord clients active on
+                                                   // this input
     audio_source_t                mInputSource;    // input source selected by application
     //(mediarecorder.h)
     const sp<IOProfile>           mProfile;        // I/O profile this output derives from
@@ -63,6 +64,7 @@
 
 private:
     audio_port_handle_t           mId;
+    uint32_t                      mOpenRefCount;
     // Because a preemtible capture session can preempt another one, we end up in an endless loop
     // situation were each session is allowed to restart after being preempted,
     // thus preempting the other one which restarts and so on.
diff --git a/services/audiopolicy/common/managerdefinitions/src/AudioInputDescriptor.cpp b/services/audiopolicy/common/managerdefinitions/src/AudioInputDescriptor.cpp
index 362c645..e828cc0 100644
--- a/services/audiopolicy/common/managerdefinitions/src/AudioInputDescriptor.cpp
+++ b/services/audiopolicy/common/managerdefinitions/src/AudioInputDescriptor.cpp
@@ -29,7 +29,8 @@
 AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile)
     : mIoHandle(0),
       mDevice(AUDIO_DEVICE_NONE), mPolicyMix(NULL), mPatchHandle(0), mRefCount(0),
-      mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile), mIsSoundTrigger(false), mId(0)
+      mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile), mIsSoundTrigger(false), mId(0),
+      mOpenRefCount(0)
 {
     if (profile != NULL) {
         mSamplingRate = profile->pickSamplingRate();
@@ -55,6 +56,22 @@
     return mProfile->getModuleHandle();
 }
 
+void AudioInputDescriptor::changeOpenRefCount(int delta)
+{
+    if ((delta + (int)mOpenRefCount) < 0) {
+        ALOGW("changeOpenRefCount() invalid delta %d, refCount %d",  delta, mOpenRefCount);
+        mOpenRefCount = 0;
+        return;
+    }
+    mOpenRefCount += delta;
+    ALOGV("changeOpenRefCount() count %d", mOpenRefCount);
+}
+
+uint32_t AudioInputDescriptor::getOpenRefCount() const
+{
+    return mOpenRefCount;
+}
+
 audio_port_handle_t AudioInputDescriptor::getId() const
 {
     return mId;
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index 4d90546..6ae2aff 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -1456,7 +1456,6 @@
     sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile);
     inputDesc->mInputSource = inputSource;
     inputDesc->mRefCount = 0;
-    inputDesc->mOpenRefCount = 1;
     inputDesc->mSamplingRate = profileSamplingRate;
     inputDesc->mFormat = profileFormat;
     inputDesc->mChannelMask = profileChannelMask;
@@ -1464,6 +1463,7 @@
     inputDesc->mSessions.add(session);
     inputDesc->mIsSoundTrigger = isSoundTrigger;
     inputDesc->mPolicyMix = policyMix;
+    inputDesc->changeOpenRefCount(1);
 
     ALOGV("getInputForAttr() returns input type = %d", *inputType);
 
@@ -1648,12 +1648,12 @@
         return;
     }
     inputDesc->mSessions.remove(session);
-    if (inputDesc->mOpenRefCount == 0) {
-        ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount);
+    if (inputDesc->getOpenRefCount() == 0) {
+        ALOGW("releaseInput() invalid open ref count %d", inputDesc->getOpenRefCount());
         return;
     }
-    inputDesc->mOpenRefCount--;
-    if (inputDesc->mOpenRefCount > 0) {
+    inputDesc->changeOpenRefCount(-1);
+    if (inputDesc->getOpenRefCount() > 0) {
         ALOGV("releaseInput() exit > 0");
         return;
     }