- Timestamp:
- Mar 7, 2016 11:15:34 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/branches/projects/uwp/pjmedia/src/pjmedia-audiodev/wasapi_dev.cpp
r5210 r5254 27 27 #include <windows.h> 28 28 #include <audioclient.h> 29 #include <Processthreadsapi.h> 29 30 30 31 #if defined(PJ_WIN32_UWP) && PJ_WIN32_UWP … … 38 39 using namespace Windows::Media::Devices; 39 40 using namespace Microsoft::WRL; 40 using namespace concurrency; 41 using namespace concurrency; 41 42 #else 42 43 #include <phoneaudioclient.h> … … 107 108 unsigned bytes_per_sample; 108 109 109 /* Playback */ 110 LPCWSTR pb_id; /* playback Id */ 110 /* Playback */ 111 111 pjmedia_aud_play_cb pb_cb; /* Playback callback */ 112 112 IAudioClient2 *default_pb_dev; /* Default playback dev */ … … 118 118 #if defined(USE_ASYNC_ACTIVATE) 119 119 ComPtr<AudioActivator> pb_aud_act; 120 #endif 121 122 /* Capture */ 123 LPCWSTR cap_id; /* Capture Id */ 120 Platform::String^ pb_id; 121 #else 122 LPCWSTR pb_id; 123 #endif 124 125 /* Capture */ 124 126 pjmedia_aud_rec_cb cap_cb; /* Capture callback */ 125 127 IAudioClient2 *default_cap_dev; /* Default capture dev */ … … 133 135 #if defined(USE_ASYNC_ACTIVATE) 134 136 ComPtr<AudioActivator> cap_aud_act; 137 Platform::String^ cap_id; 138 #else 139 LPCWSTR cap_id; 135 140 #endif 136 141 }; … … 251 256 events[eventCount++] = strm->cap_event; 252 257 253 /* Raise self priority. We don't want the audio to be distorted by254 * system activity.255 */256 #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE != 0257 if (strm->param.dir & PJMEDIA_DIR_PLAYBACK)258 CeSetThreadPriority(GetCurrentThread(), 153);259 else260 CeSetThreadPriority(GetCurrentThread(), 247);261 #else262 //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);263 #endif264 265 /* Raise thread priority */266 // mmcs_handle = AvSetMmThreadCharacteristicsW(L"Audio",267 // &mmcss_task_index);268 // if (!mmcs_handle) {269 //PJ_LOG(4,(THIS_FILE, "Unable to enable MMCS on wasapi stream thread"));270 // }271 272 258 /* 273 259 * Loop while not signalled to quit, wait for event objects to be … … 336 322 hr = strm->pb_client->GetBuffer(frame_to_render, &cur_pb_buf); 337 323 if (FAILED(hr)) { 324 PJ_LOG(4, (THIS_FILE, "Error getting wasapi buffer")); 338 325 continue; 339 326 } … … 353 340 hr = strm->pb_client->ReleaseBuffer(frame_to_render, 0); 354 341 if (FAILED(hr)) { 342 PJ_LOG(4, (THIS_FILE, "Error releasing wasapi buffer")); 355 343 continue; 356 344 } … … 363 351 364 352 hr = strm->cap_client->GetNextPacketSize(&packet_size); 365 if (FAILED(hr)) 353 if (FAILED(hr)) { 354 PJ_LOG(4, (THIS_FILE, "Error getting next packet size")); 366 355 continue; 356 } 367 357 368 358 while (packet_size) { … … 380 370 381 371 if (FAILED(hr) || (next_frame_size == 0)) { 372 PJ_LOG(4, (THIS_FILE, "Error getting next buffer, \ 373 next frame size : %d", next_frame_size)); 382 374 packet_size = 0; 383 375 continue; … … 480 472 481 473 hr = strm->cap_client->GetNextPacketSize(&packet_size); 482 if (FAILED(hr)) 474 if (FAILED(hr)) { 475 PJ_LOG(4, (THIS_FILE, "Error getting next packet size")); 483 476 packet_size = 0; 477 } 484 478 } 485 479 } 486 480 } 487 488 //AvRevertMmThreadCharacteristics(mmcs_handle);489 481 490 482 PJ_LOG(5,(THIS_FILE, "WASAPI: thread stopping..")); … … 499 491 ComPtr<IActivateAudioInterfaceAsyncOperation> async_op; 500 492 ws->cap_id = MediaDevice::GetDefaultAudioCaptureId( 501 AudioDeviceRole::Communications)->Data();493 AudioDeviceRole::Communications); 502 494 #else 503 495 ws->cap_id = GetDefaultAudioCaptureId(AudioDeviceRole::Communications); … … 510 502 511 503 #if defined(USE_ASYNC_ACTIVATE) 504 512 505 ws->cap_aud_act = Make<AudioActivator>(); 513 506 if (ws->cap_aud_act == NULL) { … … 515 508 return PJMEDIA_EAUD_SYSERR; 516 509 } 517 hr = ActivateAudioInterfaceAsync(ws->cap_id, __uuidof(IAudioClient2), 510 hr = ActivateAudioInterfaceAsync(ws->cap_id->Data(), 511 __uuidof(IAudioClient2), 518 512 NULL, ws->cap_aud_act.Get(), 519 513 &async_op); 520 514 515 //pj_thread_sleep(100); 521 516 auto task_completed = create_task(ws->cap_aud_act->task_completed); 522 task_completed.wait(); 517 task_completed.wait(); 523 518 ws->default_cap_dev = task_completed.get().Get(); 524 519 #else … … 526 521 (void**)&ws->default_cap_dev); 527 522 #endif 523 AudioClientProperties properties = {}; 524 if (SUCCEEDED(hr)) 525 { 526 properties.cbSize = sizeof AudioClientProperties; 527 properties.eCategory = AudioCategory_Communications; 528 hr = ws->default_cap_dev->SetClientProperties(&properties); 529 } 530 528 531 return FAILED(hr) ? PJMEDIA_EAUD_SYSERR : PJ_SUCCESS; 529 532 } … … 634 637 635 638 ws->pb_id = MediaDevice::GetDefaultAudioRenderId( 636 AudioDeviceRole::Communications)->Data();639 AudioDeviceRole::Communications); 637 640 #else 638 641 ws->pb_id = GetDefaultAudioRenderId(AudioDeviceRole::Communications); … … 650 653 return PJMEDIA_EAUD_SYSERR; 651 654 } 652 hr = ActivateAudioInterfaceAsync(ws->pb_id, __uuidof(IAudioClient2), 655 hr = ActivateAudioInterfaceAsync(ws->pb_id->Data(), 656 __uuidof(IAudioClient2), 653 657 NULL, ws->pb_aud_act.Get(), 654 658 &async_op); 655 659 660 //pj_thread_sleep(100); 656 661 auto task_completed = create_task(ws->pb_aud_act->task_completed); 657 662 task_completed.wait(); … … 661 666 (void**)&ws->default_pb_dev); 662 667 #endif 668 669 AudioClientProperties properties = {}; 670 if (SUCCEEDED(hr)) 671 { 672 properties.cbSize = sizeof AudioClientProperties; 673 properties.eCategory = AudioCategory_Communications; 674 hr = ws->default_pb_dev->SetClientProperties(&properties); 675 } 663 676 664 677 return FAILED(hr) ? PJMEDIA_EAUD_SYSERR : PJ_SUCCESS; … … 1025 1038 } 1026 1039 1027 /* Apply the remaining settings */1028 /* Set the output volume */1029 // if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING) {1030 // status = wasapi_stream_set_cap(&strm->base,1031 // PJMEDIA_AUD_DEV_CAP_OUTPUT_VOLUME_SETTING,1032 // ¶m->output_vol);1033 //if (status != PJ_SUCCESS) {1034 // PJ_LOG(4, (THIS_FILE, "Error setting output volume:%d", status));1035 //}1036 // }1037 1038 // /* Set the audio routing ONLY if app explicitly asks one */1039 // if ((param->dir & PJMEDIA_DIR_PLAYBACK) &&1040 //(param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE))1041 // {1042 //PJ_TODO(CREATE_STREAM_WITH_AUDIO_ROUTE);1043 ////status = wasapi_stream_set_cap(&strm->base,1044 //// PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE,1045 //// ¶m->output_route);1046 ////if (status != PJ_SUCCESS) {1047 //// PJ_LOG(4, (THIS_FILE, "Error setting output route,status: %d",1048 //// status));1049 ////}1050 // }1051 1052 1040 strm->quit_event = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, 1053 1041 EVENT_ALL_ACCESS); … … 1127 1115 1128 1116 PJ_ASSERT_RETURN(s && pval, PJ_EINVAL); 1129 1130 1131 // if (cap==PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE &&1132 // (strm->param.dir & PJMEDIA_DIR_PLAYBACK))1133 // {1134 //pjmedia_aud_dev_route route;1135 //AudioRoutingEndpoint endpoint;1136 //AudioRoutingManager ^routing_mgr = AudioRoutingManager::GetDefault();1137 1138 //PJ_ASSERT_RETURN(pval, PJ_EINVAL);1139 1140 // route = *((pjmedia_aud_dev_route*)pval);1141 // /* Use the initialization function which lazy-inits the1142 // * handle for routing1143 // */1144 //switch (route) {1145 // case PJMEDIA_AUD_DEV_ROUTE_DEFAULT :1146 // endpoint = AudioRoutingEndpoint::Default;1147 // break;1148 // case PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER :1149 // endpoint = AudioRoutingEndpoint::Speakerphone;1150 // break;1151 // case PJMEDIA_AUD_DEV_ROUTE_EARPIECE :1152 // endpoint = AudioRoutingEndpoint::Earpiece;1153 // break;1154 // case PJMEDIA_AUD_DEV_ROUTE_BLUETOOTH :1155 // endpoint = AudioRoutingEndpoint::Bluetooth;1156 // break;1157 // default:1158 // endpoint = AudioRoutingEndpoint::Default;1159 //}1160 //routing_mgr->SetAudioEndpoint(endpoint);1161 //1162 //return PJ_SUCCESS;1163 // }1164 1117 1165 1118 return PJMEDIA_EAUD_INVCAP; … … 1296 1249 } 1297 1250 1298 if (ws->cap_id) {1299 CoTaskMemFree((LPVOID)ws->cap_id);1300 }1301 1302 if (ws->pb_id) {1303 CoTaskMemFree((LPVOID)ws->pb_id);1304 }1305 1251 1306 1252 #if defined(USE_ASYNC_ACTIVATE) … … 1312 1258 if (ws->pb_aud_act) { 1313 1259 ws->pb_aud_act = nullptr; 1260 } 1261 1262 if (ws->cap_id) { 1263 ws->cap_id = nullptr; 1264 } 1265 1266 if (ws->pb_id) { 1267 ws->pb_id = nullptr; 1268 } 1269 #else 1270 if (ws->cap_id) { 1271 CoTaskMemFree((LPVOID)ws->cap_id); 1272 } 1273 1274 if (ws->pb_id) { 1275 CoTaskMemFree((LPVOID)ws->pb_id); 1314 1276 } 1315 1277 #endif
Note: See TracChangeset
for help on using the changeset viewer.