Ejemplo n.º 1
0
/*****************************************************************
 * \brief init audio device
 * \return STREAM_OK if success, STREAM_ERROR otherwise
 */
static int init_audio(radio_priv_t *priv)
{
    int is_oss=1;
    int seconds=2;
    char* tmp;
    if (priv->audio_initialized) return 1;

    /* do_capture==0 mplayer was not started with capture keyword, so disabling capture*/
    if(!priv->do_capture)
        return STREAM_OK;

    if (!priv->radio_param->adevice) {
        priv->do_capture=0;
        return STREAM_OK;
    }

    priv->do_capture=1;
    mp_tmsg(MSGT_RADIO,MSGL_V,"[radio] Starting capture stuff.\n");
#ifdef CONFIG_ALSA
    while ((tmp = strrchr(priv->radio_param->adevice, '='))) {
        tmp[0] = ':';
        //adevice option looks like ALSA device name. Switching to ALSA
        is_oss=0;
    }
    while ((tmp = strrchr(priv->radio_param->adevice, '.')))
        tmp[0] = ',';
#endif

    if(audio_in_init(&priv->audio_in, is_oss?AUDIO_IN_OSS:AUDIO_IN_ALSA)<0) {
        mp_tmsg(MSGT_RADIO, MSGL_ERR, "[radio] audio_in_init failed.\n");
    }

    audio_in_set_device(&priv->audio_in, priv->radio_param->adevice);
    audio_in_set_channels(&priv->audio_in, priv->radio_param->achannels);
    audio_in_set_samplerate(&priv->audio_in, priv->radio_param->arate);

    if (audio_in_setup(&priv->audio_in) < 0) {
        mp_tmsg(MSGT_RADIO, MSGL_ERR, "[radio] audio_in_setup call failed: %s\n", strerror(errno));
        return STREAM_ERROR;
    }
#ifdef CONFIG_OSS_AUDIO
    if(is_oss)
        ioctl(priv->audio_in.oss.audio_fd, SNDCTL_DSP_NONBLOCK, 0);
#endif
#ifdef CONFIG_ALSA
    if(!is_oss)
        snd_pcm_nonblock(priv->audio_in.alsa.handle,1);
#endif

    priv->audio_buffer_size = seconds*priv->audio_in.samplerate*priv->audio_in.channels*
                              priv->audio_in.bytes_per_sample+priv->audio_in.blocksize;
    if (priv->audio_buffer_size < 256*priv->audio_in.blocksize)
        priv->audio_buffer_size = 256*priv->audio_in.blocksize;
    mp_tmsg(MSGT_RADIO, MSGL_V, "[radio] Audio capture - buffer=%d bytes (block=%d bytes).\n",
            priv->audio_buffer_size,priv->audio_in.blocksize);
    /* start capture */
    priv->audio_ringbuffer = calloc(1, priv->audio_buffer_size);
    if (!priv->audio_ringbuffer) {
        mp_tmsg(MSGT_RADIO, MSGL_ERR, "[radio] cannot allocate audio buffer (block=%d,buf=%d): %s\n",priv->audio_in.blocksize, priv->audio_buffer_size, strerror(errno));
        return STREAM_ERROR;
    }
    priv->audio_head = 0;
    priv->audio_tail = 0;
    priv->audio_cnt = 0;
    priv->audio_drop = 0;

    audio_in_start_capture(&priv->audio_in);

    priv->audio_initialized = 1;

    return STREAM_OK;
}
Ejemplo n.º 2
0
/*****************************************************************
 * \brief init audio device
 * \return STREAM_OK if success, STREAM_ERROR otherwise
 */
static int init_audio(radio_priv_t *priv)
{
    int is_oss=1;
    int seconds=2;
    char* tmp;
    if (priv->audio_inited) return 1;

    /* do_capture==0 mplayer was not started with capture keyword, so disabling capture*/
    if(!priv->do_capture)
        return STREAM_OK;

    if (!priv->radio_param->adevice){
        priv->do_capture=0;
        return STREAM_OK;
    }

    priv->do_capture=1;
    mp_msg(MSGT_RADIO,MSGL_V,MSGTR_RADIO_CaptureStarting);
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
    while ((tmp = strrchr(priv->radio_param->adevice, '='))){
        tmp[0] = ':';
        //adevice option looks like ALSA device name. Switching to ALSA
        is_oss=0;
    }
    while ((tmp = strrchr(priv->radio_param->adevice, '.')))
        tmp[0] = ',';
#endif

    if(audio_in_init(&priv->audio_in, is_oss?AUDIO_IN_OSS:AUDIO_IN_ALSA)<0){
        mp_msg(MSGT_RADIO, MSGL_ERR, MSGTR_RADIO_AudioInInitFailed);
    }

    audio_in_set_device(&priv->audio_in, priv->radio_param->adevice);
    audio_in_set_channels(&priv->audio_in, priv->radio_param->achannels);
    audio_in_set_samplerate(&priv->audio_in, priv->radio_param->arate);

    if (audio_in_setup(&priv->audio_in) < 0) {
        mp_msg(MSGT_RADIO, MSGL_ERR, MSGTR_RADIO_AudioInSetupFailed, strerror(errno));
        return STREAM_ERROR;
    }
#ifdef USE_OSS_AUDIO
    if(is_oss)
        ioctl(priv->audio_in.oss.audio_fd, SNDCTL_DSP_NONBLOCK, 0);
#endif
#if defined(HAVE_ALSA9) || defined(HAVE_ALSA1X)
    if(!is_oss)
        snd_pcm_nonblock(priv->audio_in.alsa.handle,1);
#endif

    priv->audio_buffer_size = seconds*priv->audio_in.samplerate*priv->audio_in.channels*
            priv->audio_in.bytes_per_sample+priv->audio_in.blocksize;
    if (priv->audio_buffer_size < 256*priv->audio_in.blocksize)
        priv->audio_buffer_size = 256*priv->audio_in.blocksize;
    mp_msg(MSGT_RADIO, MSGL_V, MSGTR_RADIO_AudioBuffer,
        priv->audio_buffer_size,priv->audio_in.blocksize);
    /* start capture */
    priv->audio_ringbuffer = calloc(1, priv->audio_buffer_size);
    if (!priv->audio_ringbuffer) {
        mp_msg(MSGT_RADIO, MSGL_ERR, MSGTR_RADIO_AllocateBufferFailed,priv->audio_in.blocksize, priv->audio_buffer_size, strerror(errno));
        return STREAM_ERROR;
    }
    priv->audio_head = 0;
    priv->audio_tail = 0;
    priv->audio_cnt = 0;
    priv->audio_drop = 0;


    priv->audio_inited = 1;

    return STREAM_OK;
}