Ignore:
Timestamp:
Mar 15, 2006 8:56:04 PM (18 years ago)
Author:
bennylp
Message:

Tidying up sound device, register PortAudio? error codes, and initial support for stereo sound device (untested)

File:
1 edited

Legend:

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

    r245 r319  
    1818 */ 
    1919#include <pjmedia/sound.h> 
     20#include <pjmedia/errno.h> 
     21#include <pj/log.h> 
     22#include <pj/os.h> 
    2023#include <pj/string.h> 
    21 #include <pj/os.h> 
    22 #include <pj/log.h> 
    2324#include <portaudio.h> 
    2425 
     
    3839    int              bytes_per_sample; 
    3940    pj_uint32_t      samples_per_sec; 
     41    int              channel_count; 
    4042    pj_uint32_t      timestamp; 
    4143    pj_uint32_t      underflow; 
     
    189191 * Open stream. 
    190192 */ 
    191 PJ_DEF(pj_snd_stream*) pj_snd_open_recorder( int index, 
    192                                              const pj_snd_stream_info *param, 
    193                                              pj_snd_rec_cb rec_cb, 
    194                                              void *user_data) 
     193PJ_DEF(pj_status_t) pj_snd_open_recorder( int index, 
     194                                          unsigned clock_rate, 
     195                                          unsigned channel_count, 
     196                                          unsigned samples_per_frame, 
     197                                          unsigned bits_per_sample, 
     198                                          pj_snd_rec_cb rec_cb, 
     199                                          void *user_data, 
     200                                          pj_snd_stream **p_snd_strm) 
    195201{ 
    196202    pj_pool_t *pool; 
     
    209215        } 
    210216        if (index == count) { 
    211             PJ_LOG(2,(THIS_FILE, "Error: unable to find recorder device")); 
    212             return NULL; 
     217            /* No such device. */ 
     218            return PJ_ENOTFOUND; 
    213219        } 
    214220    } else { 
    215221        paDevInfo = Pa_GetDeviceInfo(index); 
    216         if (!paDevInfo) 
    217             return NULL; 
    218     } 
    219  
    220     if (param->bits_per_sample == 8) 
     222        if (!paDevInfo) { 
     223            /* Assumed it is "No such device" error. */ 
     224            return PJ_ENOTFOUND; 
     225        } 
     226    } 
     227 
     228    if (bits_per_sample == 8) 
    221229        sampleFormat = paUInt8; 
    222     else if (param->bits_per_sample == 16) 
     230    else if (bits_per_sample == 16) 
    223231        sampleFormat = paInt16; 
    224     else if (param->bits_per_sample == 32) 
     232    else if (bits_per_sample == 32) 
    225233        sampleFormat = paInt32; 
    226234    else 
    227         return NULL; 
     235        return PJ_ENOTSUP; 
    228236     
    229237    pool = pj_pool_create( snd_mgr.factory, "sndstream", 1024, 1024, NULL); 
    230238    if (!pool) 
    231         return NULL; 
    232  
    233     stream = pj_pool_calloc(pool, 1, sizeof(*stream)); 
     239        return PJ_ENOMEM; 
     240 
     241    stream = pj_pool_zalloc(pool, sizeof(*stream)); 
    234242    stream->pool = pool; 
    235     stream->name = pj_str("recorder"); 
     243    stream->name = pj_str("snd-rec"); 
    236244    stream->user_data = user_data; 
    237245    stream->dev_index = index; 
    238     stream->samples_per_sec = param->samples_per_frame; 
    239     stream->bytes_per_sample = param->bits_per_sample / 8; 
     246    stream->samples_per_sec = samples_per_frame; 
     247    stream->bytes_per_sample = bits_per_sample / 8; 
     248    stream->channel_count = channel_count; 
    240249    stream->rec_cb = rec_cb; 
    241250 
    242251    pj_memset(&inputParam, 0, sizeof(inputParam)); 
    243252    inputParam.device = index; 
    244     inputParam.channelCount = 1; 
     253    inputParam.channelCount = channel_count; 
    245254    inputParam.hostApiSpecificStreamInfo = NULL; 
    246255    inputParam.sampleFormat = sampleFormat; 
     
    248257 
    249258    err = Pa_OpenStream( &stream->stream, &inputParam, NULL, 
    250                          param->samples_per_sec,  
    251                          param->samples_per_frame * param->frames_per_packet,  
    252                          0, 
    253                          &PaRecorderCallback, stream ); 
     259                         clock_rate, samples_per_frame,  
     260                         0, &PaRecorderCallback, stream ); 
    254261    if (err != paNoError) { 
    255262        pj_pool_release(pool); 
    256         PJ_LOG(2,(THIS_FILE, "Error opening player: %s", Pa_GetErrorText(err))); 
    257         return NULL; 
     263        return PJMEDIA_ERRNO_FROM_PORTAUDIO(err); 
    258264    } 
    259265 
    260266    PJ_LOG(5,(THIS_FILE, "%s opening device %s for recording, sample rate=%d, " 
     267                         "channel count=%d, " 
    261268                         "%d bits per sample, %d samples per buffer", 
    262269                         (err==0 ? "Success" : "Error"), 
    263                          paDevInfo->name, param->samples_per_sec,  
    264                          param->bits_per_sample, 
    265                          param->samples_per_frame * param->frames_per_packet)); 
    266  
    267     return stream; 
    268 } 
    269  
    270  
    271 PJ_DEF(pj_snd_stream*) pj_snd_open_player(int index, 
    272                                            const pj_snd_stream_info *param, 
    273                                            pj_snd_play_cb play_cb, 
    274                                            void *user_data) 
     270                         paDevInfo->name, clock_rate, channel_count, 
     271                         bits_per_sample, samples_per_frame)); 
     272 
     273    *p_snd_strm = stream; 
     274    return PJ_SUCCESS; 
     275} 
     276 
     277 
     278PJ_DEF(pj_status_t) pj_snd_open_player( int index, 
     279                                        unsigned clock_rate, 
     280                                        unsigned channel_count, 
     281                                        unsigned samples_per_frame, 
     282                                        unsigned bits_per_sample, 
     283                                        pj_snd_play_cb play_cb, 
     284                                        void *user_data, 
     285                                        pj_snd_stream **p_snd_strm) 
    275286{ 
    276287    pj_pool_t *pool; 
     
    289300        } 
    290301        if (index == count) { 
    291             PJ_LOG(2,(THIS_FILE, "Error: unable to find player device")); 
    292             return NULL; 
     302            /* No such device. */ 
     303            return PJ_ENOTFOUND; 
    293304        } 
    294305    } else { 
    295306        paDevInfo = Pa_GetDeviceInfo(index); 
    296         if (!paDevInfo) 
    297             return NULL; 
    298     } 
    299  
    300     if (param->bits_per_sample == 8) 
     307        if (!paDevInfo) { 
     308            /* Assumed it is "No such device" error. */ 
     309            return PJ_ENOTFOUND; 
     310        } 
     311    } 
     312 
     313    if (bits_per_sample == 8) 
    301314        sampleFormat = paUInt8; 
    302     else if (param->bits_per_sample == 16) 
     315    else if (bits_per_sample == 16) 
    303316        sampleFormat = paInt16; 
    304     else if (param->bits_per_sample == 32) 
     317    else if (bits_per_sample == 32) 
    305318        sampleFormat = paInt32; 
    306319    else 
    307         return NULL; 
     320        return PJ_ENOTSUP; 
    308321     
    309322    pool = pj_pool_create( snd_mgr.factory, "sndstream", 1024, 1024, NULL); 
    310323    if (!pool) 
    311         return NULL; 
     324        return PJ_ENOMEM; 
    312325 
    313326    stream = pj_pool_calloc(pool, 1, sizeof(*stream)); 
     
    315328    stream->name = pj_str("player"); 
    316329    stream->user_data = user_data; 
    317     stream->samples_per_sec = param->samples_per_frame; 
    318     stream->bytes_per_sample = param->bits_per_sample / 8; 
     330    stream->samples_per_sec = samples_per_frame; 
     331    stream->bytes_per_sample = bits_per_sample / 8; 
     332    stream->channel_count = channel_count; 
    319333    stream->dev_index = index; 
    320334    stream->play_cb = play_cb; 
     
    322336    pj_memset(&outputParam, 0, sizeof(outputParam)); 
    323337    outputParam.device = index; 
    324     outputParam.channelCount = 1; 
     338    outputParam.channelCount = channel_count; 
    325339    outputParam.hostApiSpecificStreamInfo = NULL; 
    326340    outputParam.sampleFormat = sampleFormat; 
     
    328342 
    329343    err = Pa_OpenStream( &stream->stream, NULL, &outputParam, 
    330                          param->samples_per_sec,  
    331                          param->samples_per_frame * param->frames_per_packet,  
    332                          0, 
    333                          &PaPlayerCallback, stream ); 
     344                         clock_rate,  samples_per_frame,  
     345                         0, &PaPlayerCallback, stream ); 
    334346    if (err != paNoError) { 
    335347        pj_pool_release(pool); 
    336         PJ_LOG(2,(THIS_FILE, "Error opening player: %s", Pa_GetErrorText(err))); 
    337         return NULL; 
     348        return PJMEDIA_ERRNO_FROM_PORTAUDIO(err); 
    338349    } 
    339350 
    340351    PJ_LOG(5,(THIS_FILE, "%s opening device %s for playing, sample rate=%d, " 
     352                         "channel count=%d, " 
    341353                         "%d bits per sample, %d samples per buffer", 
    342354                         (err==0 ? "Success" : "Error"), 
    343                          paDevInfo->name, param->samples_per_sec,  
    344                          param->bits_per_sample, 
    345                          param->samples_per_frame * param->frames_per_packet)); 
    346  
    347     return stream; 
     355                         paDevInfo->name, clock_rate, channel_count, 
     356                         bits_per_sample, samples_per_frame)); 
     357 
     358    *p_snd_strm = stream; 
     359 
     360    return PJ_SUCCESS; 
    348361} 
    349362 
Note: See TracChangeset for help on using the changeset viewer.