Changeset 4480


Ignore:
Timestamp:
Apr 19, 2013 8:57:44 AM (11 years ago)
Author:
bennylp
Message:

Re #1656: BB10 sound device fixes:

  • fixed hardcoded clock rate etc.
  • handle audio routing setting when opening via param
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia-audiodev/bb10_dev.c

    r4475 r4480  
    5353#define THIS_FILE                       "bb10_dev.c" 
    5454#define BB10_DEVICE_NAME                "plughw:%d,%d" 
    55 /* Double these for 16khz sampling */ 
    56 #define PREFERRED_FRAME_SIZE 320 
    57 #define VOIP_SAMPLE_RATE 8000 
     55 
    5856 
    5957/* Set to 1 to enable tracing */ 
     
    434432{ 
    435433    struct bb10_stream* stream = (struct bb10_stream *) arg; 
    436     /* Handle from bb10_open_playback */ 
    437     /* Will be 640 */ 
    438434    int size                    = stream->pb_buf_size; 
    439     /* 160 frames for 20ms */ 
    440435    unsigned long nframes       = stream->pb_frames; 
    441436    void *user_data             = stream->user_data; 
     
    683678} 
    684679 
     680static int32_t get_alsa_pcm_fmt(const pjmedia_aud_param *param) 
     681{ 
     682    switch (param->bits_per_sample) { 
     683    case 8: 
     684        return SND_PCM_SFMT_S8; 
     685    case 16: 
     686        return SND_PCM_SFMT_S16_LE; 
     687    case 24: 
     688        return SND_PCM_SFMT_S24_LE; 
     689    case 32: 
     690        return SND_PCM_SFMT_S32_LE; 
     691    default: 
     692        PJ_ASSERT_RETURN(!"Unsupported bits_per_frame", SND_PCM_SFMT_S16_LE); 
     693    } 
     694} 
     695 
    685696static pj_status_t bb10_open_playback (struct bb10_stream *stream, 
    686697                                       const pjmedia_aud_param *param) 
     
    697708        return PJMEDIA_EAUD_INVDEV; 
    698709    } 
     710 
     711    PJ_ASSERT_RETURN(param->bits_per_sample == 16, PJMEDIA_EAUD_SAMPFORMAT); 
    699712 
    700713    /* Use the bb10 audio manager API to open as opposed to QNX core audio 
     
    727740    } 
    728741 
    729     /* TODO PJ_ZERO */ 
    730742    memset (&pi, 0, sizeof (pi)); 
    731743    pi.channel = SND_PCM_CHANNEL_PLAYBACK; 
     
    742754    pp.start_mode = SND_PCM_START_DATA; 
    743755    pp.stop_mode = SND_PCM_STOP_ROLLOVER; 
    744     /* HARD CODE for the time being PJMEDIA expects 640 for 16khz */ 
    745     pp.buf.block.frag_size = PREFERRED_FRAME_SIZE*2; 
     756    pp.buf.block.frag_size = param->samples_per_frame * param->bits_per_sample / 8; 
    746757    /* RIM recommends maximum of 3 */ 
    747758    pp.buf.block.frags_max = 3; 
    748759    pp.buf.block.frags_min = 1; 
    749760    pp.format.interleave = 1; 
    750     /* HARD CODE for the time being PJMEDIA expects 16khz */ 
    751     PJ_TODO(REMOVE_SAMPLE_RATE_HARD_CODE); 
    752     pj_assert(param->clock_rate == VOIP_SAMPLE_RATE * 2); 
    753     pp.format.rate = VOIP_SAMPLE_RATE*2; 
    754     pp.format.voices = 1; 
    755     pp.format.format = SND_PCM_SFMT_S16_LE; 
     761    pp.format.rate = param->clock_rate; 
     762    pp.format.voices = param->channel_count; 
     763    pp.format.format = get_alsa_pcm_fmt(param); 
    756764 
    757765    /* Make the calls as per the wave sample */ 
     
    778786    /* Set the sound device buffer size and latency */ 
    779787    if (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_LATENCY) { 
    780         tmp_buf_size = (rate / 1000) * param->output_latency_ms; 
     788        tmp_buf_size = rate * param->output_latency_ms / 1000; 
    781789    } else { 
    782         tmp_buf_size = (rate / 1000) * PJMEDIA_SND_DEFAULT_PLAY_LATENCY; 
     790        tmp_buf_size = rate * PJMEDIA_SND_DEFAULT_PLAY_LATENCY / 1000; 
    783791    } 
    784792    /* Set period size to samples_per_frame frames. */ 
    785     stream->pb_frames = param->samples_per_frame; 
    786     stream->param.output_latency_ms = tmp_buf_size / (rate / 1000); 
     793    stream->pb_frames = param->samples_per_frame / param->channel_count; 
     794    stream->param.output_latency_ms = tmp_buf_size * 1000 / rate; 
    787795 
    788796    /* Set our buffer */ 
    789797    stream->pb_buf_size = stream->pb_frames * param->channel_count * 
    790                           (param->bits_per_sample/8); 
     798                          param->bits_per_sample / 8; 
    791799    stream->pb_buf = (char *) pj_pool_alloc(stream->pool, stream->pb_buf_size); 
    792800 
     
    811819    if (param->rec_id < 0 || param->rec_id >= stream->af->dev_cnt) 
    812820        return PJMEDIA_EAUD_INVDEV; 
     821 
     822    PJ_ASSERT_RETURN(param->bits_per_sample == 16, PJMEDIA_EAUD_SAMPFORMAT); 
    813823 
    814824    if ((ret=audio_manager_snd_pcm_open_name(AUDIO_TYPE_VIDEO_CHAT, 
     
    854864    /* Auto-recover from errors */ 
    855865    pp.stop_mode = SND_PCM_STOP_ROLLOVER; 
    856     /* HARD CODE for the time being PJMEDIA expects 640 for 16khz */ 
    857     pp.buf.block.frag_size = PREFERRED_FRAME_SIZE*2; 
     866    pp.buf.block.frag_size = param->samples_per_frame * param->bits_per_sample / 8; 
    858867    /* From January 2013 gold OS release. RIM recommend these for capture */ 
    859868    pp.buf.block.frags_max = 1; 
    860869    pp.buf.block.frags_min = 1; 
    861870    pp.format.interleave = 1; 
    862     /* HARD CODE for the time being PJMEDIA expects 16khz */ 
    863     PJ_TODO(REMOVE_SAMPLE_RATE_HARD_CODE); 
    864     pj_assert(param->clock_rate == VOIP_SAMPLE_RATE * 2); 
    865     pp.format.rate = VOIP_SAMPLE_RATE*2; 
    866     pp.format.voices = 1; 
    867     pp.format.format = SND_PCM_SFMT_S16_LE; 
     871    pp.format.rate = param->clock_rate; 
     872    pp.format.voices = param->channel_count; 
     873    pp.format.format = get_alsa_pcm_fmt(param); 
    868874 
    869875    /* make the request */ 
     
    898904    /* Set the sound device buffer size and latency */ 
    899905    if (param->flags & PJMEDIA_AUD_DEV_CAP_INPUT_LATENCY) { 
    900         tmp_buf_size = (rate / 1000) * param->input_latency_ms; 
     906        tmp_buf_size = rate * param->input_latency_ms / 1000; 
    901907    } else { 
    902         tmp_buf_size = (rate / 1000) * PJMEDIA_SND_DEFAULT_REC_LATENCY; 
    903     } 
    904  
    905     stream->param.input_latency_ms = tmp_buf_size / (rate / 1000); 
     908        tmp_buf_size = rate * PJMEDIA_SND_DEFAULT_REC_LATENCY / 1000; 
     909    } 
     910 
     911    stream->param.input_latency_ms = tmp_buf_size * 1000 / rate; 
    906912 
    907913    /* Set our buffer */ 
    908914    stream->ca_buf_size = stream->ca_frames * param->channel_count * 
    909                           (param->bits_per_sample/8); 
     915                          param->bits_per_sample / 8; 
    910916    stream->ca_buf = (char *)pj_pool_alloc (stream->pool, stream->ca_buf_size); 
    911917 
     
    966972    } 
    967973 
    968     /* Part of the play functionality but the RIM/Truphone loopback sample 
    969      * initialializes after the play and capture 
    970      * "false" is default/earpiece for output 
    971      */ 
    972     status = bb10_initialize_playback_ctrl(stream,false); 
    973     if (status != PJ_SUCCESS) { 
    974         return PJMEDIA_EAUD_SYSERR; 
     974    /* Set the audio routing ONLY if app explicitly asks one */ 
     975    if ((param->dir & PJMEDIA_DIR_PLAYBACK) && 
     976        (param->flags & PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE)) 
     977    { 
     978        status = bb10_stream_set_cap(&stream->base, 
     979                                     PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE, 
     980                                     &param->output_route); 
     981        if (status != PJ_SUCCESS) { 
     982            TRACE_((THIS_FILE, "Error setting output route")); 
     983            bb10_stream_destroy(&stream->base); 
     984            return status; 
     985        } 
     986    } else { 
     987        /* Legacy behavior: if none specified, set to speaker */ 
     988        status = bb10_initialize_playback_ctrl(stream, false); 
    975989    } 
    976990 
Note: See TracChangeset for help on using the changeset viewer.