Ignore:
Timestamp:
Mar 7, 2016 11:15:34 PM (8 years ago)
Author:
riza
Message:

Re: #1900:

  • Add new property file (pjproject-vs14-api-def.props) to define the API used
  • Add ioqueue specific to uwp using winRT networking API
  • Add uwp GUI sample APP using Voip architecture
  • Add async activation for wasapi dev
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/projects/uwp/pjmedia/src/pjmedia-audiodev/wasapi_dev.cpp

    r5210 r5254  
    2727#include <windows.h> 
    2828#include <audioclient.h> 
     29#include <Processthreadsapi.h> 
    2930 
    3031#if defined(PJ_WIN32_UWP) && PJ_WIN32_UWP 
     
    3839    using namespace Windows::Media::Devices;   
    3940    using namespace Microsoft::WRL; 
    40     using namespace concurrency; 
     41    using namespace concurrency;    
    4142#else 
    4243    #include <phoneaudioclient.h> 
     
    107108    unsigned                bytes_per_sample; 
    108109 
    109     /* Playback */ 
    110     LPCWSTR                 pb_id;              /* playback Id              */ 
     110    /* Playback */     
    111111    pjmedia_aud_play_cb     pb_cb;              /* Playback callback        */ 
    112112    IAudioClient2          *default_pb_dev;     /* Default playback dev     */ 
     
    118118#if defined(USE_ASYNC_ACTIVATE) 
    119119    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 */     
    124126    pjmedia_aud_rec_cb      cap_cb;             /* Capture callback         */ 
    125127    IAudioClient2          *default_cap_dev;    /* Default capture dev      */     
     
    133135#if defined(USE_ASYNC_ACTIVATE) 
    134136    ComPtr<AudioActivator>  cap_aud_act; 
     137    Platform::String^       cap_id; 
     138#else 
     139    LPCWSTR                 cap_id;              
    135140#endif 
    136141}; 
     
    251256        events[eventCount++] = strm->cap_event; 
    252257 
    253     /* Raise self priority. We don't want the audio to be distorted by 
    254      * system activity. 
    255      */ 
    256 #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE != 0 
    257     if (strm->param.dir & PJMEDIA_DIR_PLAYBACK) 
    258         CeSetThreadPriority(GetCurrentThread(), 153); 
    259     else 
    260         CeSetThreadPriority(GetCurrentThread(), 247); 
    261 #else 
    262     //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); 
    263 #endif 
    264  
    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  
    272258    /* 
    273259     * Loop while not signalled to quit, wait for event objects to be  
     
    336322            hr = strm->pb_client->GetBuffer(frame_to_render, &cur_pb_buf); 
    337323            if (FAILED(hr)) { 
     324                PJ_LOG(4, (THIS_FILE, "Error getting wasapi buffer")); 
    338325                continue; 
    339326            } 
     
    353340            hr = strm->pb_client->ReleaseBuffer(frame_to_render, 0); 
    354341            if (FAILED(hr)) { 
     342                PJ_LOG(4, (THIS_FILE, "Error releasing wasapi buffer")); 
    355343                continue; 
    356344            } 
     
    363351 
    364352            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")); 
    366355                continue; 
     356            } 
    367357             
    368358            while (packet_size) { 
     
    380370 
    381371                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)); 
    382374                    packet_size = 0; 
    383375                    continue; 
     
    480472 
    481473                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")); 
    483476                    packet_size = 0; 
     477                } 
    484478            } 
    485479        } 
    486480    } 
    487  
    488     //AvRevertMmThreadCharacteristics(mmcs_handle); 
    489481 
    490482    PJ_LOG(5,(THIS_FILE, "WASAPI: thread stopping..")); 
     
    499491    ComPtr<IActivateAudioInterfaceAsyncOperation> async_op; 
    500492    ws->cap_id = MediaDevice::GetDefaultAudioCaptureId( 
    501                                       AudioDeviceRole::Communications)->Data(); 
     493                                              AudioDeviceRole::Communications); 
    502494#else 
    503495    ws->cap_id = GetDefaultAudioCaptureId(AudioDeviceRole::Communications); 
     
    510502 
    511503#if defined(USE_ASYNC_ACTIVATE) 
     504 
    512505    ws->cap_aud_act = Make<AudioActivator>(); 
    513506    if (ws->cap_aud_act == NULL) { 
     
    515508        return PJMEDIA_EAUD_SYSERR; 
    516509    } 
    517     hr = ActivateAudioInterfaceAsync(ws->cap_id, __uuidof(IAudioClient2),  
     510    hr = ActivateAudioInterfaceAsync(ws->cap_id->Data(),  
     511                                     __uuidof(IAudioClient2),  
    518512                                     NULL, ws->cap_aud_act.Get(),  
    519513                                     &async_op); 
    520514 
     515    //pj_thread_sleep(100); 
    521516    auto task_completed = create_task(ws->cap_aud_act->task_completed); 
    522     task_completed.wait(); 
     517    task_completed.wait();     
    523518    ws->default_cap_dev = task_completed.get().Get(); 
    524519#else 
     
    526521                                (void**)&ws->default_cap_dev); 
    527522#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 
    528531    return FAILED(hr) ? PJMEDIA_EAUD_SYSERR : PJ_SUCCESS; 
    529532} 
     
    634637 
    635638    ws->pb_id = MediaDevice::GetDefaultAudioRenderId( 
    636                                       AudioDeviceRole::Communications)->Data(); 
     639                                              AudioDeviceRole::Communications); 
    637640#else 
    638641    ws->pb_id = GetDefaultAudioRenderId(AudioDeviceRole::Communications); 
     
    650653        return PJMEDIA_EAUD_SYSERR; 
    651654    } 
    652     hr = ActivateAudioInterfaceAsync(ws->pb_id, __uuidof(IAudioClient2), 
     655    hr = ActivateAudioInterfaceAsync(ws->pb_id->Data(),  
     656                                     __uuidof(IAudioClient2), 
    653657                                     NULL, ws->pb_aud_act.Get(), 
    654658                                     &async_op); 
    655659 
     660    //pj_thread_sleep(100); 
    656661    auto task_completed = create_task(ws->pb_aud_act->task_completed); 
    657662    task_completed.wait(); 
     
    661666                                (void**)&ws->default_pb_dev); 
    662667#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    } 
    663676     
    664677    return FAILED(hr) ? PJMEDIA_EAUD_SYSERR : PJ_SUCCESS; 
     
    10251038    } 
    10261039 
    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  //                             &param->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         ////                           &param->output_route); 
    1046         ////if (status != PJ_SUCCESS) { 
    1047         ////    PJ_LOG(4, (THIS_FILE, "Error setting output route,status: %d", 
    1048         ////           status)); 
    1049         ////} 
    1050  //   }  
    1051  
    10521040    strm->quit_event = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET,  
    10531041                                     EVENT_ALL_ACCESS); 
     
    11271115 
    11281116    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 the 
    1142  //        * handle for routing 
    1143  //        */ 
    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  //   }  
    11641117 
    11651118    return PJMEDIA_EAUD_INVCAP; 
     
    12961249    } 
    12971250 
    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     } 
    13051251 
    13061252#if defined(USE_ASYNC_ACTIVATE) 
     
    13121258    if (ws->pb_aud_act) { 
    13131259        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); 
    13141276    } 
    13151277#endif 
Note: See TracChangeset for help on using the changeset viewer.