Fix #1195: assertion during thread registration in coreaudio and portaudio's input/output callbacks on Mac OS X and #1196: using system's default audio input/output device instead of first available device.



git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@3404 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c b/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c
index b235b7e..e8c77e6 100644
--- a/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c
+++ b/pjmedia/src/pjmedia-audiodev/coreaudio_dev.c
@@ -417,6 +417,49 @@
 	 */
 	return PJMEDIA_EAUD_INIT;
     }
+    
+    if (dev_size > 1) {
+	AudioDeviceID dev_id = kAudioObjectUnknown;
+	unsigned idx = 0;
+	
+	/* Find default audio input device */
+	addr.mSelector = kAudioHardwarePropertyDefaultInputDevice;
+	addr.mScope = kAudioObjectPropertyScopeGlobal;
+	addr.mElement = kAudioObjectPropertyElementMaster;
+	size = sizeof(dev_id);
+	
+	ostatus = AudioObjectGetPropertyData(kAudioObjectSystemObject,
+					     &addr, 0, NULL,
+					     &size, (void *)&dev_id);
+	if (ostatus != noErr && dev_id != dev_ids[idx]) {
+	    AudioDeviceID temp_id = dev_ids[idx];
+	    
+	    for (i = idx + 1; i < dev_size; i++) {
+		if (dev_ids[i] == dev_id) {
+		    dev_ids[idx++] = dev_id;
+		    dev_ids[i] = temp_id;
+		    break;
+		}
+	    }
+	}
+
+	/* Find default audio output device */
+	addr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;	
+	ostatus = AudioObjectGetPropertyData(kAudioObjectSystemObject,
+					     &addr, 0, NULL,
+					     &size, (void *)&dev_id);
+	if (ostatus != noErr && dev_id != dev_ids[idx]) {
+	    AudioDeviceID temp_id = dev_ids[idx];
+	    
+	    for (i = idx + 1; i < dev_size; i++) {
+		if (dev_ids[i] == dev_id) {
+		    dev_ids[idx] = dev_id;
+		    dev_ids[i] = temp_id;
+		    break;
+		}
+	    }
+	}
+    }
 
     /* Build the devices' info */
     cf->dev_info = (struct coreaudio_dev_info*)
@@ -649,6 +692,7 @@
      */
     if (strm->rec_thread_initialized == 0 || !pj_thread_is_registered())
     {
+	pj_bzero(strm->rec_thread_desc, sizeof(pj_thread_desc));
 	status = pj_thread_register("ca_rec", strm->rec_thread_desc,
 				    &strm->rec_thread);
 	strm->rec_thread_initialized = 1;
@@ -789,6 +833,7 @@
      */
     if (strm->rec_thread_initialized == 0 || !pj_thread_is_registered())
     {
+	pj_bzero(strm->rec_thread_desc, sizeof(pj_thread_desc));
 	status = pj_thread_register("ca_rec", strm->rec_thread_desc,
 				    &strm->rec_thread);
 	strm->rec_thread_initialized = 1;
@@ -904,6 +949,7 @@
      */
     if (stream->play_thread_initialized == 0 || !pj_thread_is_registered())
     {
+	pj_bzero(stream->play_thread_desc, sizeof(pj_thread_desc));
 	status = pj_thread_register("coreaudio", stream->play_thread_desc,
 				    &stream->play_thread);
 	stream->play_thread_initialized = 1;
diff --git a/pjmedia/src/pjmedia-audiodev/pa_dev.c b/pjmedia/src/pjmedia-audiodev/pa_dev.c
index 095f772..1188b8a 100644
--- a/pjmedia/src/pjmedia-audiodev/pa_dev.c
+++ b/pjmedia/src/pjmedia-audiodev/pa_dev.c
@@ -185,6 +185,7 @@
      */
     if (stream->rec_thread_initialized == 0 || !pj_thread_is_registered()) 
     {
+	pj_bzero(stream->rec_thread_desc, sizeof(pj_thread_desc));
 	status = pj_thread_register("pa_rec", stream->rec_thread_desc, 
 				    &stream->rec_thread);
 	stream->rec_thread_initialized = 1;
@@ -297,6 +298,7 @@
      */
     if (stream->play_thread_initialized == 0 || !pj_thread_is_registered()) 
     {
+	pj_bzero(stream->play_thread_desc, sizeof(pj_thread_desc));
 	status = pj_thread_register("portaudio", stream->play_thread_desc,
 				    &stream->play_thread);
 	stream->play_thread_initialized = 1;