From 57d31914f74177773f3ea53d7eb79524ca11f26c Mon Sep 17 00:00:00 2001
From: aymeric <aymeric@3f6dc0c8-ddfe-455d-9043-3cd528dc4637>
Date: Mon, 9 Nov 2009 19:39:26 +0000
Subject: [PATCH] fix race condition

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@755 3f6dc0c8-ddfe-455d-9043-3cd528dc4637
---
 linphone/mediastreamer2/src/winsnd2.c    | 19 +++++++++++++++----
 linphone/mediastreamer2/src/winsndds.cpp |  9 ++++++++-
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/linphone/mediastreamer2/src/winsnd2.c b/linphone/mediastreamer2/src/winsnd2.c
index 8d5de36d40..f8b6f24813 100755
--- a/linphone/mediastreamer2/src/winsnd2.c
+++ b/linphone/mediastreamer2/src/winsnd2.c
@@ -901,7 +901,8 @@ static void winsndcard_unload(MSSndCardManager *m){
 
 static void winsndcard_detect(MSSndCardManager *m){
 	_winsndcard_detect(m);
-	ms_thread_create(&poller_thread,NULL,new_device_polling_thread,NULL);
+	if (poller_thread==NULL)
+		ms_thread_create(&poller_thread,NULL,new_device_polling_thread,NULL);
 }
 
 typedef struct WinSnd{
@@ -1105,6 +1106,12 @@ static void winsnd_read_preprocess(MSFilter *f){
 			return ;
 		}
 	}
+#ifndef _TRUE_TIME
+	ms_mutex_lock(&f->ticker->lock);
+	ms_ticker_set_time_func(f->ticker,winsnd_get_cur_time,d);
+	ms_mutex_unlock(&f->ticker->lock);
+#endif
+
 	bsize=WINSND_NSAMPLES*d->wfx.nAvgBytesPerSec/8000;
 	ms_debug("Using input buffers of %i bytes",bsize);
 	for(i=0;i<WINSND_NBUFS;++i){
@@ -1115,11 +1122,13 @@ static void winsnd_read_preprocess(MSFilter *f){
 	mr=waveInStart(d->indev);
 	if (mr != MMSYSERR_NOERROR){
 		ms_error("waveInStart() error");
-		return ;
-	}
 #ifndef _TRUE_TIME
-	ms_ticker_set_time_func(f->ticker,winsnd_get_cur_time,d);
+		ms_mutex_lock(&f->ticker->lock);
+		ms_ticker_set_time_func(f->ticker,NULL,NULL);
+		ms_mutex_unlock(&f->ticker->lock);
 #endif
+		return ;
+	}
 }
 
 static void winsnd_read_postprocess(MSFilter *f){
@@ -1127,7 +1136,9 @@ static void winsnd_read_postprocess(MSFilter *f){
 	MMRESULT mr;
 	int i;
 #ifndef _TRUE_TIME
+	ms_mutex_lock(&f->ticker->lock);
 	ms_ticker_set_time_func(f->ticker,NULL,NULL);
+	ms_mutex_unlock(&f->ticker->lock);
 #endif
 	d->running=FALSE;
 	mr=waveInStop(d->indev);
diff --git a/linphone/mediastreamer2/src/winsndds.cpp b/linphone/mediastreamer2/src/winsndds.cpp
index c0cb5b83aa..fd20609a3b 100644
--- a/linphone/mediastreamer2/src/winsndds.cpp
+++ b/linphone/mediastreamer2/src/winsndds.cpp
@@ -1147,7 +1147,8 @@ static void winsnddscard_unload(MSSndCardManager *m){
 
 static void winsnddscard_detect(MSSndCardManager *m){
 	_winsnddscard_detect(m);
-	ms_thread_create(&poller_thread,NULL,new_device_polling_thread,NULL);
+	if (poller_thread==NULL)
+		ms_thread_create(&poller_thread,NULL,new_device_polling_thread,NULL);
 }
 
 typedef struct WinSndDs{
@@ -1350,7 +1351,9 @@ static void winsndds_read_preprocess(MSFilter *f){
 
 	hr = IDirectSoundCaptureBuffer_Start( d->lpDirectSoundInputBuffer, DSCBSTART_LOOPING );
 
+	ms_mutex_lock(&f->ticker->lock);
 	ms_ticker_set_time_func(f->ticker,winsndds_get_cur_time,d);
+	ms_mutex_unlock(&f->ticker->lock);
 
 	d->thread_running=TRUE;
 	ms_thread_create(&d->thread,NULL,winsndds_read_thread,d);
@@ -1370,7 +1373,9 @@ static void winsndds_read_postprocess(MSFilter *f){
 	ms_mutex_unlock(&d->thread_lock);
 	ms_thread_join(d->thread,NULL);
 
+	ms_mutex_lock(&f->ticker->lock);
 	ms_ticker_set_time_func(f->ticker,NULL,NULL);
+	ms_mutex_unlock(&f->ticker->lock);
 
 	if( d->lpDirectSoundInputBuffer )
 	{
@@ -1519,7 +1524,9 @@ static void winsndds_write_preprocess(MSFilter *f){
 
 		hr = IDirectSoundCaptureBuffer_Start( d_capture_filter->lpDirectSoundInputBuffer, DSCBSTART_LOOPING );
 
+		ms_mutex_lock(&f->ticker->lock);
 		ms_ticker_set_time_func(f_capture_filter->ticker,winsndds_get_cur_time,d_capture_filter);
+		ms_mutex_unlock(&f->ticker->lock);
 
 		d_capture_filter->thread_running=TRUE;
 		ms_thread_create(&d_capture_filter->thread,NULL,winsndds_read_thread,d_capture_filter);
-- 
GitLab