Index: pjproject/branches/projects/uwp/pjmedia/src/pjmedia-audiodev/wasapi_dev.cpp
===================================================================
--- pjproject/branches/projects/uwp/pjmedia/src/pjmedia-audiodev/wasapi_dev.cpp	(revision 5210)
+++ pjproject/branches/projects/uwp/pjmedia/src/pjmedia-audiodev/wasapi_dev.cpp	(revision 5254)
@@ -27,4 +27,5 @@
 #include <windows.h>
 #include <audioclient.h>
+#include <Processthreadsapi.h>
 
 #if defined(PJ_WIN32_UWP) && PJ_WIN32_UWP
@@ -38,5 +39,5 @@
     using namespace Windows::Media::Devices;  
     using namespace Microsoft::WRL;
-    using namespace concurrency;
+    using namespace concurrency;   
 #else
     #include <phoneaudioclient.h>
@@ -107,6 +108,5 @@
     unsigned		    bytes_per_sample;
 
-    /* Playback */
-    LPCWSTR		    pb_id;		/* playback Id		    */
+    /* Playback */    
     pjmedia_aud_play_cb	    pb_cb;		/* Playback callback	    */
     IAudioClient2	   *default_pb_dev;     /* Default playback dev	    */
@@ -118,8 +118,10 @@
 #if defined(USE_ASYNC_ACTIVATE)
     ComPtr<AudioActivator>  pb_aud_act;
-#endif
-
-    /* Capture */
-    LPCWSTR		    cap_id;		/* Capture Id		    */	
+    Platform::String^	    pb_id;
+#else
+    LPCWSTR		    pb_id;		
+#endif
+
+    /* Capture */    
     pjmedia_aud_rec_cb	    cap_cb;		/* Capture callback	    */
     IAudioClient2	   *default_cap_dev;	/* Default capture dev	    */    
@@ -133,4 +135,7 @@
 #if defined(USE_ASYNC_ACTIVATE)
     ComPtr<AudioActivator>  cap_aud_act;
+    Platform::String^	    cap_id;
+#else
+    LPCWSTR		    cap_id;		
 #endif
 };
@@ -251,23 +256,4 @@
 	events[eventCount++] = strm->cap_event;
 
-    /* Raise self priority. We don't want the audio to be distorted by
-     * system activity.
-     */
-#if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE != 0
-    if (strm->param.dir & PJMEDIA_DIR_PLAYBACK)
-	CeSetThreadPriority(GetCurrentThread(), 153);
-    else
-	CeSetThreadPriority(GetCurrentThread(), 247);
-#else
-    //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
-#endif
-
-    /* Raise thread priority */
-    //   mmcs_handle = AvSetMmThreadCharacteristicsW(L"Audio", 
-    //						     &mmcss_task_index);
-    //   if (!mmcs_handle) {
-    //PJ_LOG(4,(THIS_FILE, "Unable to enable MMCS on wasapi stream thread"));
-    //   }
-
     /*
      * Loop while not signalled to quit, wait for event objects to be 
@@ -336,4 +322,5 @@
 	    hr = strm->pb_client->GetBuffer(frame_to_render, &cur_pb_buf);
 	    if (FAILED(hr)) {
+		PJ_LOG(4, (THIS_FILE, "Error getting wasapi buffer"));
 		continue;
 	    }
@@ -353,4 +340,5 @@
 	    hr = strm->pb_client->ReleaseBuffer(frame_to_render, 0);
 	    if (FAILED(hr)) {
+		PJ_LOG(4, (THIS_FILE, "Error releasing wasapi buffer"));
 		continue;
 	    }
@@ -363,6 +351,8 @@
 
 	    hr = strm->cap_client->GetNextPacketSize(&packet_size);
-	    if (FAILED(hr))
+	    if (FAILED(hr)) {
+		PJ_LOG(4, (THIS_FILE, "Error getting next packet size"));
 		continue;
+	    }
 	    
 	    while (packet_size) {
@@ -380,4 +370,6 @@
 
 		if (FAILED(hr) || (next_frame_size == 0)) {
+		    PJ_LOG(4, (THIS_FILE, "Error getting next buffer, \
+			       next frame size : %d", next_frame_size));
 		    packet_size = 0;
 		    continue;
@@ -480,11 +472,11 @@
 
 		hr = strm->cap_client->GetNextPacketSize(&packet_size);
-		if (FAILED(hr))
+		if (FAILED(hr)) {
+		    PJ_LOG(4, (THIS_FILE, "Error getting next packet size"));
 		    packet_size = 0;
+		}
 	    }
 	}
     }
-
-    //AvRevertMmThreadCharacteristics(mmcs_handle);
 
     PJ_LOG(5,(THIS_FILE, "WASAPI: thread stopping.."));
@@ -499,5 +491,5 @@
     ComPtr<IActivateAudioInterfaceAsyncOperation> async_op;
     ws->cap_id = MediaDevice::GetDefaultAudioCaptureId(
-				      AudioDeviceRole::Communications)->Data();
+					      AudioDeviceRole::Communications);
 #else
     ws->cap_id = GetDefaultAudioCaptureId(AudioDeviceRole::Communications);
@@ -510,4 +502,5 @@
 
 #if defined(USE_ASYNC_ACTIVATE)
+
     ws->cap_aud_act = Make<AudioActivator>();
     if (ws->cap_aud_act == NULL) {
@@ -515,10 +508,12 @@
 	return PJMEDIA_EAUD_SYSERR;
     }
-    hr = ActivateAudioInterfaceAsync(ws->cap_id, __uuidof(IAudioClient2), 
+    hr = ActivateAudioInterfaceAsync(ws->cap_id->Data(), 
+				     __uuidof(IAudioClient2), 
 				     NULL, ws->cap_aud_act.Get(), 
 				     &async_op);
 
+    //pj_thread_sleep(100);
     auto task_completed = create_task(ws->cap_aud_act->task_completed);
-    task_completed.wait();
+    task_completed.wait();    
     ws->default_cap_dev = task_completed.get().Get();
 #else
@@ -526,4 +521,12 @@
 				(void**)&ws->default_cap_dev);
 #endif
+    AudioClientProperties properties = {};
+    if (SUCCEEDED(hr))
+    {
+	properties.cbSize = sizeof AudioClientProperties;
+	properties.eCategory = AudioCategory_Communications;
+	hr = ws->default_cap_dev->SetClientProperties(&properties);
+    }
+
     return FAILED(hr) ? PJMEDIA_EAUD_SYSERR : PJ_SUCCESS;
 }
@@ -634,5 +637,5 @@
 
     ws->pb_id = MediaDevice::GetDefaultAudioRenderId(
-				      AudioDeviceRole::Communications)->Data();
+					      AudioDeviceRole::Communications);
 #else
     ws->pb_id = GetDefaultAudioRenderId(AudioDeviceRole::Communications);
@@ -650,8 +653,10 @@
 	return PJMEDIA_EAUD_SYSERR;
     }
-    hr = ActivateAudioInterfaceAsync(ws->pb_id, __uuidof(IAudioClient2),
+    hr = ActivateAudioInterfaceAsync(ws->pb_id->Data(), 
+				     __uuidof(IAudioClient2),
 				     NULL, ws->pb_aud_act.Get(),
 				     &async_op);
 
+    //pj_thread_sleep(100);
     auto task_completed = create_task(ws->pb_aud_act->task_completed);
     task_completed.wait();
@@ -661,4 +666,12 @@
 				(void**)&ws->default_pb_dev);
 #endif
+
+    AudioClientProperties properties = {};
+    if (SUCCEEDED(hr))
+    {
+	properties.cbSize = sizeof AudioClientProperties;
+	properties.eCategory = AudioCategory_Communications;
+	hr = ws->default_pb_dev->SetClientProperties(&properties);
+    }
     
     return FAILED(hr) ? PJMEDIA_EAUD_SYSERR : PJ_SUCCESS;
@@ -1025,29 +1038,4 @@
     }
 
-    /* Apply the remaining settings */
-    /* Set the output volume */
- //   if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {
- //       status = wasapi_stream_set_cap(&strm->base,
- //                             PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,
- //                             &param->output_vol);
-	//if (status != PJ_SUCCESS) {
-	//    PJ_LOG(4, (THIS_FILE, "Error setting output volume:%d", status));
-	//}
- //   }
-
- //   /* Set the audio routing ONLY if app explicitly asks one */
- //   if ((param->dir & PJMEDIA_DIR_PLAYBACK) &&
-	//(param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE))
- //   {
-	//PJ_TODO(CREATE_STREAM_WITH_AUDIO_ROUTE);
-	////status = wasapi_stream_set_cap(&strm->base,
-	////			       PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE,
-	////			       &param->output_route);
-	////if (status != PJ_SUCCESS) {
-	////    PJ_LOG(4, (THIS_FILE, "Error setting output route,status: %d",
-	////	       status));
-	////}
- //   } 
-
     strm->quit_event = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, 
 				     EVENT_ALL_ACCESS);
@@ -1127,39 +1115,4 @@
 
     PJ_ASSERT_RETURN(s && pval, PJ_EINVAL);
-
-
- //   if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE &&
- //       (strm->param.dir & PJMEDIA_DIR_PLAYBACK))
- //   {
-	//pjmedia_aud_dev_route route;
-	//AudioRoutingEndpoint endpoint;
-	//AudioRoutingManager ^routing_mgr = AudioRoutingManager::GetDefault();
-
-	//PJ_ASSERT_RETURN(pval, PJ_EINVAL);
-
- //   	route = *((pjmedia_aud_dev_route*)pval);
- //       /* Use the initialization function which lazy-inits the
- //        * handle for routing
- //        */
-	//switch (route) {
-	//    case PJMEDIA_AUD_DEV_ROUTE_DEFAULT :
-	//	endpoint = AudioRoutingEndpoint::Default;
-	//	break;
-	//    case PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER :
-	//	endpoint = AudioRoutingEndpoint::Speakerphone;
-	//	break;
-	//    case PJMEDIA_AUD_DEV_ROUTE_EARPIECE :
-	//	endpoint = AudioRoutingEndpoint::Earpiece;
-	//	break;    
-	//    case PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH :
-	//	endpoint = AudioRoutingEndpoint::Bluetooth;
-	//	break;
-	//    default:
-	//	endpoint = AudioRoutingEndpoint::Default;		
-	//}
-	//routing_mgr->SetAudioEndpoint(endpoint);
-	//
-	//return PJ_SUCCESS;
- //   } 
 
     return PJMEDIA_EAUD_INVCAP;
@@ -1296,11 +1249,4 @@
     }
 
-    if (ws->cap_id) {
-	CoTaskMemFree((LPVOID)ws->cap_id);
-    }
-
-    if (ws->pb_id) {
-	CoTaskMemFree((LPVOID)ws->pb_id);
-    }
 
 #if defined(USE_ASYNC_ACTIVATE)
@@ -1312,4 +1258,20 @@
     if (ws->pb_aud_act) {
 	ws->pb_aud_act = nullptr;
+    }
+
+    if (ws->cap_id) {
+	ws->cap_id = nullptr;
+    }
+
+    if (ws->pb_id) {
+	ws->pb_id = nullptr;
+    }
+#else
+    if (ws->cap_id) {
+	CoTaskMemFree((LPVOID)ws->cap_id);
+    }
+
+    if (ws->pb_id) {
+	CoTaskMemFree((LPVOID)ws->pb_id);
     }
 #endif
