Ejemplo n.º 1
0
/*****************************************************************
 * Stream initialization
 * \return STREAM_OK if success, STREAM_ERROR otherwise
 */
static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
    radio_priv_t* priv;
    float frequency=0;
    int i;

    if (strncmp("radio://",stream->url,8) != 0)
        return STREAM_UNSUPPORTED;

    if(mode != STREAM_READ)
        return STREAM_UNSUPPORTED;

    priv=calloc(1,sizeof(radio_priv_t));

    if (!priv)
        return STREAM_ERROR;


    priv->radio_param=opts;

#ifdef CONFIG_RADIO_CAPTURE
    if (priv->radio_param->capture && strncmp("capture",priv->radio_param->capture,7)==0)
        priv->do_capture=1;
    else
        priv->do_capture=0;
#endif



    if (strncmp(priv->radio_param->driver,"default",7)==0)
        priv->driver=radio_drivers[0];
    else
        priv->driver=NULL;

    mp_tmsg(MSGT_RADIO,MSGL_V,"[radio] Available drivers: ");
    for(i=0; radio_drivers[i]; i++) {
        mp_msg(MSGT_RADIO,MSGL_V,"%s, ",radio_drivers[i]->name);
        if(strcmp(priv->radio_param->driver,radio_drivers[i]->name)==0)
            priv->driver=radio_drivers[i];
    }
    mp_msg(MSGT_RADIO,MSGL_V,"\n");

    if(priv->driver)
        mp_msg(MSGT_RADIO, MSGL_INFO, priv->driver->info);
    else {
        mp_tmsg(MSGT_RADIO, MSGL_INFO, "[radio] Unknown driver name: %s\n",priv->radio_param->driver);
        close_s(stream);
        return STREAM_ERROR;
    }

    stream->type = STREAMTYPE_RADIO;
    /* using rawaudio demuxer */
    *file_format =  DEMUXER_TYPE_RAWAUDIO;
    stream->flags = STREAM_READ;

    priv->radio_fd=-1;

    stream->start_pos=0;
    stream->end_pos=0;
    stream->priv=priv;
    stream->close=close_s;
    stream->fill_buffer=fill_buffer_s;

    priv->radio_fd = open(priv->radio_param->device, O_RDONLY);
    if (priv->radio_fd < 0) {
        mp_tmsg(MSGT_RADIO, MSGL_ERR, "[radio] Unable to open '%s': %s\n",
                priv->radio_param->device, strerror(errno));
        close_s(stream);
        return STREAM_ERROR;
    }
    mp_tmsg(MSGT_RADIO, MSGL_V, "[radio] Radio fd: %d, %s\n", priv->radio_fd,priv->radio_param->device);
    fcntl(priv->radio_fd, F_SETFD, FD_CLOEXEC);

    get_volume(priv, &priv->old_snd_volume);
    set_volume(priv,0);

    if (init_frac(priv)!=STREAM_OK) {
        close_s(stream);
        return STREAM_ERROR;
    };

    if (parse_channels(priv,priv->radio_param->freq_channel,&frequency)!=STREAM_OK) {
        close_s(stream);
        return STREAM_ERROR;
    }

    if ((frequency<priv->rangelow)||(frequency>priv->rangehigh)) {
        mp_tmsg(MSGT_RADIO, MSGL_ERR, "[radio] Wrong frequency: %.2f\n",frequency);
        close_s(stream);
        return STREAM_ERROR;
    } else
        mp_tmsg(MSGT_RADIO, MSGL_INFO, "[radio] Using frequency: %.2f.\n",frequency);

    if(set_frequency(priv,frequency)!=STREAM_OK) {
        close_s(stream);
        return STREAM_ERROR;
    }


    if (init_audio(priv)!=STREAM_OK) {
        close_s(stream);
        return STREAM_ERROR;
    }

#if defined(CONFIG_RADIO_CAPTURE) && defined(CONFIG_STREAM_CACHE)
    if(priv->do_capture) {
        //5 second cache
        if(!stream_enable_cache(stream,5*priv->audio_in.samplerate*priv->audio_in.channels*
                                priv->audio_in.bytes_per_sample,2*priv->audio_in.samplerate*priv->audio_in.channels*
                                priv->audio_in.bytes_per_sample,priv->audio_in.blocksize)) {
            mp_tmsg(MSGT_RADIO, MSGL_ERR, "[radio] Call to stream_enable_cache failed: %s\n",strerror(errno));
            close_s(stream);
            return STREAM_ERROR;
        }
    }
#endif

    set_volume(priv,priv->radio_param->volume);

    return STREAM_OK;
}
Ejemplo n.º 2
0
struct demuxer *demux_open_withparams(struct MPOpts *opts, stream_t *vs,
                int file_format, int audio_id, int video_id, int dvdsub_id,
                char *filename, struct demuxer_params *params)
{
    stream_t *as = NULL, *ss = NULL;
    demuxer_t *vd, *ad = NULL, *sd = NULL;
    demuxer_t *res;
    int afmt = DEMUXER_TYPE_UNKNOWN, sfmt = DEMUXER_TYPE_UNKNOWN;
    int demuxer_type;
    int audio_demuxer_type = 0, sub_demuxer_type = 0;
    int demuxer_force = 0, audio_demuxer_force = 0, sub_demuxer_force = 0;

    if ((demuxer_type =
         get_demuxer_type_from_name(opts->demuxer_name, &demuxer_force)) < 0) {
        mp_msg(MSGT_DEMUXER, MSGL_ERR, "-demuxer %s does not exist.\n",
               opts->demuxer_name);
        return NULL;
    }
    if ((audio_demuxer_type =
         get_demuxer_type_from_name(opts->audio_demuxer_name,
                                    &audio_demuxer_force)) < 0) {
        mp_msg(MSGT_DEMUXER, MSGL_ERR, "-audio-demuxer %s does not exist.\n",
               opts->audio_demuxer_name);
        if (opts->audio_stream)
            return NULL;
    }
    if ((sub_demuxer_type =
         get_demuxer_type_from_name(opts->sub_demuxer_name,
                                    &sub_demuxer_force)) < 0) {
        mp_msg(MSGT_DEMUXER, MSGL_ERR, "-sub-demuxer %s does not exist.\n",
               opts->sub_demuxer_name);
        if (opts->sub_stream)
            return NULL;
    }

    if (opts->audio_stream) {
        as = open_stream(opts->audio_stream, 0, &afmt);
        if (!as) {
            mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open audio stream: %s\n",
                   opts->audio_stream);
            return NULL;
        }
        if (opts->audio_stream_cache) {
            if (!stream_enable_cache
                (as, opts->audio_stream_cache * 1024,
                 opts->audio_stream_cache * 1024 *
                            (opts->stream_cache_min_percent / 100.0),
                 opts->audio_stream_cache * 1024 *
                            (opts->stream_cache_seek_min_percent / 100.0))) {
                free_stream(as);
                mp_msg(MSGT_DEMUXER, MSGL_ERR,
                       "Can't enable audio stream cache\n");
                return NULL;
            }
        }
    }
    if (opts->sub_stream) {
        ss = open_stream(opts->sub_stream, 0, &sfmt);
        if (!ss) {
            mp_tmsg(MSGT_DEMUXER, MSGL_ERR, "Cannot open subtitle stream: %s\n",
                   opts->sub_stream);
            return NULL;
        }
    }

    vd = demux_open_stream(opts, vs, demuxer_type ? demuxer_type : file_format,
                           demuxer_force, opts->audio_stream ? -2 : audio_id,
                           video_id, opts->sub_stream ? -2 : dvdsub_id,
                           filename, params);
    if (!vd) {
        if (as)
            free_stream(as);
        if (ss)
            free_stream(ss);
        return NULL;
    }
    if (as) {
        ad = demux_open_stream(opts, as,
                               audio_demuxer_type ? audio_demuxer_type : afmt,
                               audio_demuxer_force, audio_id, -2, -2,
                               opts->audio_stream, params);
        if (!ad) {
            mp_tmsg(MSGT_DEMUXER, MSGL_WARN, "Failed to open audio demuxer: %s\n",
                   opts->audio_stream);
            free_stream(as);
        } else if (ad->audio->sh
                   && ((sh_audio_t *) ad->audio->sh)->format == 0x55) // MP3
            opts->hr_mp3_seek = 1;    // Enable high res seeking
    }
    if (ss) {
        sd = demux_open_stream(opts, ss,
                               sub_demuxer_type ? sub_demuxer_type : sfmt,
                               sub_demuxer_force, -2, -2, dvdsub_id,
                               opts->sub_stream, params);
        if (!sd) {
            mp_tmsg(MSGT_DEMUXER, MSGL_WARN,
                   "Failed to open subtitle demuxer: %s\n", opts->sub_stream);
            free_stream(ss);
        }
    }

    if (ad && sd)
        res = new_demuxers_demuxer(vd, ad, sd);
    else if (ad)
        res = new_demuxers_demuxer(vd, ad, vd);
    else if (sd)
        res = new_demuxers_demuxer(vd, vd, sd);
    else
        res = vd;

    opts->correct_pts = opts->user_correct_pts;
    if (opts->correct_pts < 0)
        opts->correct_pts =
            demux_control(vd ? vd : res, DEMUXER_CTRL_CORRECT_PTS,
                          NULL) == DEMUXER_CTRL_OK;
    return res;
}