Add lock to prevent possible rare race condition in Win coreAudio capture implementation.

BUG = 
TEST = voe_auto_test

Review URL: https://webrtc-codereview.appspot.com/1320011

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3868 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/audio_device/win/audio_device_core_win.cc b/webrtc/modules/audio_device/win/audio_device_core_win.cc
index 0d4c60d..d517478 100644
--- a/webrtc/modules/audio_device/win/audio_device_core_win.cc
+++ b/webrtc/modules/audio_device/win/audio_device_core_win.cc
@@ -3677,8 +3677,8 @@
 
     if (FAILED(hr))
     {
-        _UnLock();
         _ptrClientOut->Stop();
+        _UnLock();
         _TraceCOMError(hr);
     }
 
@@ -3690,19 +3690,23 @@
         }
     }
 
+    _Lock();
+
     if (keepPlaying)
     {
-        hr = _ptrClientOut->Stop();
-        if (FAILED(hr))
+        if (_ptrClientOut != NULL)
         {
-            _TraceCOMError(hr);
+            hr = _ptrClientOut->Stop();
+            if (FAILED(hr))
+            {
+                _TraceCOMError(hr);
+            }
+            hr = _ptrClientOut->Reset();
+            if (FAILED(hr))
+            {
+                _TraceCOMError(hr);
+            }
         }
-        hr = _ptrClientOut->Reset();
-        if (FAILED(hr))
-        {
-            _TraceCOMError(hr);
-        }
-
         // Trigger callback from module process thread
         _playError = 1;
         WEBRTC_TRACE(kTraceError, kTraceUtility, _id, "kPlayoutError message posted: rendering thread has ended pre-maturely");
@@ -3712,6 +3716,8 @@
         WEBRTC_TRACE(kTraceInfo, kTraceAudioDevice, _id, "_Rendering thread is now terminated properly");
     }
 
+    _UnLock();
+
     return (DWORD)hr;
 }
 
@@ -4177,13 +4183,15 @@
 Exit:
     if (FAILED(hr))
     {
-        _UnLock();
         _ptrClientIn->Stop();
+        _UnLock();
         _TraceCOMError(hr);
     }
 
     RevertCaptureThreadPriority();
 
+    _Lock();
+
     if (keepRecording)
     {
         if (_ptrClientIn != NULL)
@@ -4212,6 +4220,8 @@
     SAFE_RELEASE(_ptrClientIn);
     SAFE_RELEASE(_ptrCaptureClient);
 
+    _UnLock();
+
     if (syncBuffer)
     {
         delete [] syncBuffer;