Changeset 462


Ignore:
Timestamp:
May 20, 2006 3:44:55 PM (18 years ago)
Author:
bennylp
Message:

Added device enumeration capability in DirectSound?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia/dsound.c

    r451 r462  
    4747#define DEFAULT_BUFFER_COUNT        8 
    4848 
     49#define MAX_HARDWARE                16 
     50 
     51struct dsound_dev_info 
     52{ 
     53    pjmedia_snd_dev_info    info; 
     54    LPGUID                  lpGuid; 
     55}; 
     56 
     57static unsigned dev_count; 
     58static struct dsound_dev_info dev_info[MAX_HARDWARE]; 
     59 
    4960 
    5061 
     
    117128 */ 
    118129static pj_status_t init_player_stream( struct dsound_stream *ds_strm, 
     130                                       int dev_id, 
    119131                                       unsigned clock_rate, 
    120132                                       unsigned channel_count, 
     
    133145    PJ_ASSERT_RETURN(buffer_count <= MAX_PACKET_BUFFER_COUNT, PJ_EINVAL); 
    134146 
     147    /* Check device ID */ 
     148    if (dev_id == -1) 
     149        dev_id = 0; 
     150 
     151    PJ_ASSERT_RETURN(dev_id>=0 && dev_id < (int)dev_count, PJ_EINVAL); 
     152 
    135153    /* 
    136154     * Create DirectSound device. 
    137155     */ 
    138     hr = DirectSoundCreate(NULL, &ds_strm->ds.play.lpDs, NULL); 
     156    hr = DirectSoundCreate(dev_info[dev_id].lpGuid, &ds_strm->ds.play.lpDs,  
     157                           NULL); 
    139158    if (FAILED(hr)) 
    140159        return PJ_RETURN_OS_ERROR(hr); 
     
    226245 */ 
    227246static pj_status_t init_capture_stream( struct dsound_stream *ds_strm, 
     247                                        int dev_id, 
    228248                                        unsigned clock_rate, 
    229249                                        unsigned channel_count, 
     
    242262 
    243263 
     264    /* Check device id */ 
     265    if (dev_id == -1) 
     266        dev_id = 0; 
     267 
     268    PJ_ASSERT_RETURN(dev_id>=0 && dev_id < (int)dev_count, PJ_EINVAL); 
     269 
    244270    /* 
    245271     * Creating recorder device. 
    246272     */ 
    247     hr = DirectSoundCaptureCreate(NULL, &ds_strm->ds.capture.lpDs, NULL); 
     273    hr = DirectSoundCaptureCreate(dev_info[dev_id].lpGuid,  
     274                                  &ds_strm->ds.capture.lpDs, NULL); 
    248275    if (FAILED(hr)) 
    249276        return PJ_RETURN_OS_ERROR(hr); 
     
    502529 
    503530 
     531/* DirectSound enum device callback */ 
     532static BOOL CALLBACK DSEnumCallback( LPGUID lpGuid, LPCSTR lpcstrDescription,   
     533                                     LPCSTR lpcstrModule, LPVOID lpContext) 
     534{ 
     535    unsigned index, max = sizeof(dev_info[index].info.name); 
     536    pj_bool_t is_capture_device = (lpContext != NULL); 
     537 
     538 
     539    PJ_UNUSED_ARG(lpcstrModule); 
     540 
     541 
     542    /* Put the capture and playback of the same devices to the same  
     543     * dev_info item, by looking at the GUID. 
     544     */ 
     545    for (index=0; index<dev_count; ++index) { 
     546        if (dev_info[index].lpGuid == lpGuid) 
     547            break; 
     548    } 
     549 
     550    if (index == dev_count) 
     551        ++dev_count; 
     552    else if (dev_count >= MAX_HARDWARE) { 
     553        pj_assert(!"Too many DirectSound hardware found"); 
     554        PJ_LOG(4,(THIS_FILE, "Too many hardware found, some devices will " 
     555                             "not be listed")); 
     556        return FALSE; 
     557    } 
     558 
     559    strncpy(dev_info[index].info.name, lpcstrDescription, max); 
     560    dev_info[index].info.name[max-1] = '\0'; 
     561    dev_info[index].lpGuid = lpGuid; 
     562    dev_info[index].info.default_samples_per_sec = 44100; 
     563     
     564    /* Just assumed that device supports stereo capture/playback */ 
     565    if (is_capture_device) 
     566        dev_info[index].info.input_count+=2; 
     567    else 
     568        dev_info[index].info.output_count+=2; 
     569 
     570    return TRUE; 
     571} 
     572 
    504573 
    505574/* 
     
    508577PJ_DEF(pj_status_t) pjmedia_snd_init(pj_pool_factory *factory) 
    509578{ 
     579    HRESULT hr; 
     580    unsigned i; 
     581 
    510582    pool_factory = factory; 
     583 
     584    /* Enumerate sound playback devices */ 
     585    hr = DirectSoundEnumerate(&DSEnumCallback, NULL); 
     586    if (FAILED(hr)) 
     587        return PJ_RETURN_OS_ERROR(hr); 
     588 
     589    /* Enumerate sound capture devices */ 
     590    hr = DirectSoundEnumerate(&DSEnumCallback, (void*)1); 
     591    if (FAILED(hr)) 
     592        return PJ_RETURN_OS_ERROR(hr); 
     593 
     594    PJ_LOG(4,(THIS_FILE, "DirectSound initialized, found %d devices:", 
     595              dev_count)); 
     596    for (i=0; i<dev_count; ++i) { 
     597        PJ_LOG(4,(THIS_FILE, " dev_id %d: %s  (in=%d, out=%d)",  
     598                  i, dev_info[i].info.name,  
     599                  dev_info[i].info.input_count,  
     600                  dev_info[i].info.output_count)); 
     601    } 
     602 
    511603    return PJ_SUCCESS; 
    512604} 
     
    525617PJ_DEF(int) pjmedia_snd_get_dev_count(void) 
    526618{ 
    527     return 1; 
     619    return dev_count; 
    528620} 
    529621 
     
    533625PJ_DEF(const pjmedia_snd_dev_info*) pjmedia_snd_get_dev_info(unsigned index) 
    534626{ 
    535     static pjmedia_snd_dev_info info; 
    536  
    537     PJ_UNUSED_ARG(index); 
    538  
    539     pj_memset(&info, 0, sizeof(info)); 
    540  
    541     info.default_samples_per_sec = 44100; 
    542     info.input_count = 1; 
    543     info.output_count = 1; 
    544     pj_ansi_strcpy(info.name, "Default DirectSound Device"); 
    545  
    546     return &info; 
     627    PJ_ASSERT_RETURN(index < dev_count, NULL); 
     628 
     629    return &dev_info[index].info; 
    547630} 
    548631 
     
    577660    PJ_ASSERT_RETURN(bits_per_sample == BITS_PER_SAMPLE, PJ_EINVAL); 
    578661 
    579     /* Don't support device at present */ 
    580     PJ_UNUSED_ARG(rec_id); 
    581     PJ_UNUSED_ARG(play_id); 
    582      
    583  
    584662    /* Create and Initialize stream descriptor */ 
    585663    pool = pj_pool_create(pool_factory, "dsound-dev", 1000, 1000, NULL); 
     
    602680    /* Create player stream */ 
    603681    if (dir & PJMEDIA_DIR_PLAYBACK) { 
    604         status = init_player_stream( &strm->play_strm, clock_rate, 
     682        status = init_player_stream( &strm->play_strm, play_id, clock_rate, 
    605683                                     channel_count, samples_per_frame, 
    606684                                     DEFAULT_BUFFER_COUNT ); 
     
    613691    /* Create capture stream */ 
    614692    if (dir & PJMEDIA_DIR_CAPTURE) { 
    615         status = init_capture_stream( &strm->rec_strm, clock_rate, 
     693        status = init_capture_stream( &strm->rec_strm, rec_id, clock_rate, 
    616694                                      channel_count, samples_per_frame, 
    617695                                      DEFAULT_BUFFER_COUNT); 
     
    709787        if (FAILED(hr)) 
    710788            return PJ_RETURN_OS_ERROR(hr); 
     789        PJ_LOG(5,(THIS_FILE, "DirectSound playback stream started")); 
    711790    } 
    712791     
     
    716795        if (FAILED(hr)) 
    717796            return PJ_RETURN_OS_ERROR(hr); 
     797        PJ_LOG(5,(THIS_FILE, "DirectSound capture stream started")); 
    718798    } 
    719799 
Note: See TracChangeset for help on using the changeset viewer.