Index: /pjproject/trunk/pjmedia/src/pjmedia/dsound.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia/dsound.c	(revision 468)
+++ /pjproject/trunk/pjmedia/src/pjmedia/dsound.c	(revision 469)
@@ -463,6 +463,8 @@
 
 
-    /* Raise self priority */
-    //SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
+    /* Raise self priority. We don't want the audio to be distorted by
+     * system activity.
+     */
+    SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
 
     /* Calculate bytes per frame */
Index: /pjproject/trunk/pjsip-apps/src/samples/sndtest.c
===================================================================
--- /pjproject/trunk/pjsip-apps/src/samples/sndtest.c	(revision 468)
+++ /pjproject/trunk/pjsip-apps/src/samples/sndtest.c	(revision 469)
@@ -37,5 +37,8 @@
 #define DURATION	    10000
 
-/* Max frames per sec. */
+/* Skip the first msec from the calculation */
+#define SKIP_DURATION	    1000
+
+/* Max frames per sec (to calculate number of delays to keep). */
 #define MAX_FRAMES_PER_SEC  100
 
@@ -134,4 +137,5 @@
     struct stream_data *strm_data = &test_data->playback_data;
 
+    /* Skip frames when test is not started or test has finished */
     if (!test_data->running) {
 	pj_memset(output, 0, size);
@@ -139,8 +143,10 @@
     }
 
+    /* Save last timestamp seen (to calculate drift) */
     strm_data->last_timestamp = timestamp;
 
     if (strm_data->last_called.u64 == 0) {
 	pj_get_timestamp(&strm_data->last_called);
+	/* Init min_delay to one frame */
 	strm_data->min_delay = test_data->samples_per_frame * 1000000 /
 			       test_data->clock_rate;
@@ -153,4 +159,5 @@
 	pj_get_timestamp(&now);
 	
+	/* Calculate frame interval */
 	delay = pj_elapsed_usec(&strm_data->last_called, &now);
 	if (delay < strm_data->min_delay)
@@ -161,6 +168,13 @@
 	strm_data->last_called = now;
 
+	/* Save the frame interval for later calculation */
 	strm_data->delay[strm_data->counter] = delay;
 	++strm_data->counter;
+
+    } else {
+
+	/* No space, can't take anymore frames */
+	test_data->running = 0;
+
     }
 
@@ -179,12 +193,15 @@
     PJ_UNUSED_ARG(size);
 
+    /* Skip frames when test is not started or test has finished */
     if (!test_data->running) {
 	return PJ_SUCCESS;
     }
 
+    /* Save last timestamp seen (to calculate drift) */
     strm_data->last_timestamp = timestamp;
 
     if (strm_data->last_called.u64 == 0) {
 	pj_get_timestamp(&strm_data->last_called);
+	/* Init min_delay to one frame */
 	strm_data->min_delay = test_data->samples_per_frame * 1000000 /
 			       test_data->clock_rate;
@@ -196,5 +213,6 @@
 
 	pj_get_timestamp(&now);
-	
+
+	/* Calculate frame interval */
 	delay = pj_elapsed_usec(&strm_data->last_called, &now);
 	if (delay < strm_data->min_delay)
@@ -205,6 +223,13 @@
 	strm_data->last_called = now;
 
+	/* Save the frame interval for later calculation */
 	strm_data->delay[strm_data->counter] = delay;
 	++strm_data->counter;
+
+    } else {
+
+	/* No space, can't take anymore frames */
+	test_data->running = 0;
+
     }
 
@@ -228,5 +253,5 @@
 {
     unsigned i, dur;
-    unsigned min_jitter, max_jitter, avg_jitter=0;
+    unsigned min_jitter, max_jitter, sum_jitter, avg_jitter=0;
 
     PJ_LOG(3,(THIS_FILE, "  %s stream report:", title));
@@ -276,4 +301,5 @@
     min_jitter = 0xFFFFF;
     max_jitter = 0;
+    sum_jitter = 0;
 
     for (i=1; i<strm_data->counter; ++i) {
@@ -285,6 +311,9 @@
 	if (jitter > (int)max_jitter) max_jitter = jitter;
 
-	avg_jitter = ((i-1) * avg_jitter + jitter) / i;
-    }
+	sum_jitter += jitter;
+    }
+
+    avg_jitter = (sum_jitter) / (strm_data->counter - 1);
+
     if (max_jitter < WARN_JITTER_USEC) {
 	PJ_LOG(3,(THIS_FILE, 
@@ -347,5 +376,5 @@
 
     /* Sleep for a while to let sound device "settles" */
-    pj_thread_sleep(100);
+    pj_thread_sleep(200);
 
 
@@ -359,8 +388,12 @@
     }
 
-    /* Let the stream runs for 1 second to get stable result.
+    PJ_LOG(3,(THIS_FILE,
+	      " Please wait while test is in progress (~%d secs)..",
+	      (DURATION+SKIP_DURATION)/1000));
+
+    /* Let the stream runs for few msec/sec to get stable result.
      * (capture normally begins with frames available simultaneously).
      */
-    pj_thread_sleep(1000);
+    pj_thread_sleep(SKIP_DURATION);
 
 
@@ -371,7 +404,4 @@
      * Let the test runs for a while.
      */
-    PJ_LOG(3,(THIS_FILE,
-	      " Please wait while test is in progress (~%d secs)..",
-	      (DURATION/1000)));
     pj_thread_sleep(DURATION);
 
@@ -427,5 +457,5 @@
 	    PJ_LOG(2,(THIS_FILE, 
 		      "   Sound capture is %d samples %s than playback "
-		      "at the end of the test (at the rate about %d samples"
+		      "at the end of the test (average is %d samples"
 		      " per second)",
 		      drift, which, 
