Ticket #384: Prefer to use Direct Sound as the sound device backend on Windows

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@1456 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjmedia/include/pjmedia/config.h b/pjmedia/include/pjmedia/config.h
index 0b7f542..a0c0ee5 100644
--- a/pjmedia/include/pjmedia/config.h
+++ b/pjmedia/include/pjmedia/config.h
@@ -73,6 +73,16 @@
 
 
 /**
+ * Specify whether we prefer to use DirectSound on Windows.
+ *
+ * Say yes!
+ */
+#ifndef PJMEDIA_PREFER_DIRECT_SOUND
+#   define PJMEDIA_PREFER_DIRECT_SOUND	    1
+#endif
+
+
+/**
  * Specify number of sound buffers. Larger number is better for sound
  * stability and to accomodate sound devices that are unable to send frames
  * in timely manner, however it would probably cause more audio delay (and 
diff --git a/pjmedia/src/pjmedia/pasound.c b/pjmedia/src/pjmedia/pasound.c
index a2ec627..1a5e819 100644
--- a/pjmedia/src/pjmedia/pasound.c
+++ b/pjmedia/src/pjmedia/pasound.c
@@ -265,6 +265,23 @@
 {
     int i, count;
 
+    /* Special for Windows - try to use the DirectSound implementation
+     * first since it provides better latency.
+     */
+#if PJMEDIA_PREFER_DIRECT_SOUND
+    if (Pa_HostApiTypeIdToHostApiIndex(paDirectSound) >= 0) {
+	const PaHostApiInfo *pHI;
+	int index = Pa_HostApiTypeIdToHostApiIndex(paDirectSound);
+	pHI = Pa_GetHostApiInfo(index);
+	if (pHI) {
+	    const PaDeviceInfo *paDevInfo = NULL;
+	    paDevInfo = Pa_GetDeviceInfo(pHI->defaultInputDevice);
+	    if (paDevInfo && paDevInfo->maxInputChannels >= channel_count)
+		return pHI->defaultInputDevice;
+	}
+    }
+#endif
+
     /* Enumerate the host api's for the default devices, and return
      * the device with suitable channels.
      */
@@ -304,6 +321,23 @@
 {
     int i, count;
 
+    /* Special for Windows - try to use the DirectSound implementation
+     * first since it provides better latency.
+     */
+#if PJMEDIA_PREFER_DIRECT_SOUND
+    if (Pa_HostApiTypeIdToHostApiIndex(paDirectSound) >= 0) {
+	const PaHostApiInfo *pHI;
+	int index = Pa_HostApiTypeIdToHostApiIndex(paDirectSound);
+	pHI = Pa_GetHostApiInfo(index);
+	if (pHI) {
+	    const PaDeviceInfo *paDevInfo = NULL;
+	    paDevInfo = Pa_GetDeviceInfo(pHI->defaultOutputDevice);
+	    if (paDevInfo && paDevInfo->maxOutputChannels >= channel_count)
+		return pHI->defaultOutputDevice;
+	}
+    }
+#endif
+
     /* Enumerate the host api's for the default devices, and return
      * the device with suitable channels.
      */
@@ -334,7 +368,7 @@
 	if (paDevInfo->maxOutputChannels >= channel_count)
 	    return i;
     }
-    
+
     return -1;
 }