bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch, int32_t *old_br, int32_t *old_sr, int32_t *old_ch) { /* Values are checked in toxav.c */ if (*old_sr != new_sr || *old_ch != new_ch) { OpusEncoder *new_encoder = create_audio_encoder(log, new_br, new_sr, new_ch); if (new_encoder == NULL) { return false; } opus_encoder_destroy(*e); *e = new_encoder; } else if (*old_br == new_br) { return true; /* Nothing changed */ } int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br)); if (status != OPUS_OK) { LOGGER_ERROR(log, "Error while setting encoder ctl: %s", opus_strerror(status)); return false; } *old_br = new_br; *old_sr = new_sr; *old_ch = new_ch; LOGGER_DEBUG(log, "Reconfigured audio encoder br: %d sr: %d cc:%d", new_br, new_sr, new_ch); return true; }
JNIEXPORT jlong JNICALL createAudioEncoder(JNIEnv* env, jclass clasz, jobject typeObj, jint sample_rate, jint channel, jint sample_szie, jint bitrate) { jclass clazz = NULL; clazz = (env)->GetObjectClass(typeObj); if (clazz == NULL) { LOGD("GetObjectClass error"); return JNI_FALSE; } jmethodID getType = (env)->GetMethodID(clazz, "type", "()I"); jint type = env->CallIntMethod(typeObj, getType); audio_codec_type_t nativeType = getAudioCodecType(type); return create_audio_encoder(nativeType, sample_rate, channel, sample_szie, bitrate); }
ACSession *ac_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data) { ACSession *ac = (ACSession *)calloc(sizeof(ACSession), 1); if (!ac) { LOGGER_WARNING(log, "Allocation failed! Application might misbehave!"); return NULL; } if (create_recursive_mutex(ac->queue_mutex) != 0) { LOGGER_WARNING(log, "Failed to create recursive mutex!"); free(ac); return NULL; } int status; ac->decoder = opus_decoder_create(48000, 2, &status); if (status != OPUS_OK) { LOGGER_ERROR(log, "Error while starting audio decoder: %s", opus_strerror(status)); goto BASE_CLEANUP; } if (!(ac->j_buf = jbuf_new(3))) { LOGGER_WARNING(log, "Jitter buffer creaton failed!"); opus_decoder_destroy(ac->decoder); goto BASE_CLEANUP; } ac->log = log; /* Initialize encoders with default values */ ac->encoder = create_audio_encoder(log, 48000, 48000, 2); if (ac->encoder == NULL) { goto DECODER_CLEANUP; } ac->le_bit_rate = 48000; ac->le_sample_rate = 48000; ac->le_channel_count = 2; ac->ld_channel_count = 2; ac->ld_sample_rate = 48000; ac->ldrts = 0; /* Make it possible to reconfigure straight away */ /* These need to be set in order to properly * do error correction with opus */ ac->lp_frame_duration = 120; ac->lp_sampling_rate = 48000; ac->lp_channel_count = 1; ac->av = av; ac->friend_number = friend_number; ac->acb.first = cb; ac->acb.second = cb_data; return ac; DECODER_CLEANUP: opus_decoder_destroy(ac->decoder); jbuf_free((struct JitterBuffer *)ac->j_buf); BASE_CLEANUP: pthread_mutex_destroy(ac->queue_mutex); free(ac); return NULL; }