Exemple #1
0
CodecState *codec_init_session ( uint32_t audio_bitrate,
                                 uint16_t audio_frame_duration,
                                 uint32_t audio_sample_rate,
                                 uint32_t audio_channels,
                                 uint16_t video_width,
                                 uint16_t video_height,
                                 uint32_t video_bitrate )
{
    CodecState *retu = calloc(sizeof(CodecState), 1);
    assert(retu);

    retu->audio_bitrate = audio_bitrate;
    retu->audio_sample_rate = audio_sample_rate;

    /* Encoders */
    if (!video_width || !video_height) { /* Disable video */
        /*video_width = 320;
        video_height = 240; */
    }
    else {
        retu->supported_actions |= ( 0 == init_video_encoder(retu, video_width, video_height, video_bitrate) ) ? v_encoding : 0;
        retu->supported_actions |= ( 0 == init_video_decoder(retu) ) ? v_decoding : 0;
    }

    retu->supported_actions |= ( 0 == init_audio_encoder(retu, audio_channels) ) ? a_encoding : 0;
    retu->supported_actions |= ( 0 == init_audio_decoder(retu, audio_channels) ) ? a_decoding : 0;

    return retu;
}
Exemple #2
0
CodecState *codec_init_session ( uint32_t audio_bitrate,
                                 uint16_t audio_frame_duration,
                                 uint32_t audio_sample_rate,
                                 uint32_t audio_channels,
                                 uint16_t video_width,
                                 uint16_t video_height,
                                 uint32_t video_bitrate )
{
    CodecState *retu = calloc(sizeof(CodecState), 1);

    if (!retu) return NULL;

    retu->audio_bitrate = audio_bitrate;
    retu->audio_sample_rate = audio_sample_rate;

    /* Encoders */
    if (!video_width || !video_height) { /* Disable video */
        /*video_width = 320;
        video_height = 240; */
    } else {
        retu->capabilities |= ( 0 == init_video_encoder(retu, video_width, video_height, video_bitrate) ) ? v_encoding : 0;
        retu->capabilities |= ( 0 == init_video_decoder(retu) ) ? v_decoding : 0;
    }

    retu->capabilities |= ( 0 == init_audio_encoder(retu, audio_channels) ) ? a_encoding : 0;
    retu->capabilities |= ( 0 == init_audio_decoder(retu, audio_channels) ) ? a_decoding : 0;

    if ( retu->capabilities == 0  ) { /* everything failed */
        free (retu);
        return NULL;
    }

    return retu;
}
Exemple #3
0
CSSession *cs_new(const ToxAvCSettings *cs_self, const ToxAvCSettings *cs_peer, uint32_t jbuf_size, int has_video)
{
    CSSession *cs = calloc(sizeof(CSSession), 1);

    if (!cs) {
        LOGGER_WARNING("Allocation failed! Application might misbehave!");
        return NULL;
    }

    if (create_recursive_mutex(cs->queue_mutex) != 0) {
        LOGGER_WARNING("Failed to create recursive mutex!");
        free(cs);
        return NULL;
    }

    if ( !(cs->j_buf = jbuf_new(jbuf_size)) ) {
        LOGGER_WARNING("Jitter buffer creaton failed!");
        goto error;
    }

    cs->audio_encoder_bitrate        = cs_self->audio_bitrate;
    cs->audio_encoder_sample_rate    = cs_self->audio_sample_rate;
    cs->audio_encoder_channels       = cs_self->audio_channels;
    cs->audio_encoder_frame_duration = cs_self->audio_frame_duration;

    cs->audio_decoder_bitrate        = cs_peer->audio_bitrate;
    cs->audio_decoder_sample_rate    = cs_peer->audio_sample_rate;
    cs->audio_decoder_channels       = cs_peer->audio_channels;
    cs->audio_decoder_frame_duration = cs_peer->audio_frame_duration;


    cs->capabilities |= ( 0 == init_audio_encoder(cs) ) ? cs_AudioEncoding : 0;
    cs->capabilities |= ( 0 == init_audio_decoder(cs) ) ? cs_AudioDecoding : 0;

    if ( !(cs->capabilities & cs_AudioEncoding) || !(cs->capabilities & cs_AudioDecoding) ) goto error;

    if ((cs->support_video = has_video)) {
        cs->max_video_frame_size = MAX_VIDEOFRAME_SIZE;
        cs->video_frame_piece_size = VIDEOFRAME_PIECE_SIZE;

        cs->capabilities |= ( 0 == init_video_encoder(cs, cs_self->max_video_width,
                              cs_self->max_video_height, cs_self->video_bitrate) ) ? cs_VideoEncoding : 0;
        cs->capabilities |= ( 0 == init_video_decoder(cs) ) ? cs_VideoDecoding : 0;

        if ( !(cs->capabilities & cs_VideoEncoding) || !(cs->capabilities & cs_VideoDecoding) ) goto error;

        if ( !(cs->frame_buf = calloc(cs->max_video_frame_size, 1)) ) goto error;

        if ( !(cs->split_video_frame = calloc(cs->video_frame_piece_size + VIDEOFRAME_HEADER_SIZE, 1)) )
            goto error;

        if ( !(cs->vbuf_raw = buffer_new(VIDEO_DECODE_BUFFER_SIZE)) ) goto error;
    }

    return cs;

error:
    LOGGER_WARNING("Error initializing codec session! Application might misbehave!");

    pthread_mutex_destroy(cs->queue_mutex);

    if ( cs->audio_encoder ) opus_encoder_destroy(cs->audio_encoder);

    if ( cs->audio_decoder ) opus_decoder_destroy(cs->audio_decoder);


    if (has_video) {
        if ( cs->capabilities & cs_VideoDecoding ) vpx_codec_destroy(&cs->v_decoder);

        if ( cs->capabilities & cs_VideoEncoding ) vpx_codec_destroy(&cs->v_encoder);

        buffer_free(cs->vbuf_raw);

        free(cs->frame_buf);
        free(cs->split_video_frame);
    }

    jbuf_free(cs->j_buf);
    free(cs);

    return NULL;
}