static int audio_bitrate_driver_execute_action(MSBitrateDriver *objbase, const MSRateControlAction *action){ MSAudioBitrateDriver *obj=(MSAudioBitrateDriver*)objbase; ms_message("MSAudioBitrateDriver: executing action of type %s, value=%i",ms_rate_control_action_type_name(action->type),action->value); if (action->type==MSRateControlActionDecreaseBitrate){ /*reducing bitrate of the codec actually doesn't work very well (not enough). Increasing ptime is much more efficient*/ if (inc_ptime(obj)==-1){ if (obj->nom_bitrate>0){ int cur_br=0; int new_br; if (obj->nom_bitrate==0){ if (ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&obj->nom_bitrate)!=0){ ms_message("MSAudioBitrateDriver: Encoder has nominal bitrate %i",obj->nom_bitrate); } obj->cur_bitrate=obj->nom_bitrate; } /*if max ptime is reached, then try to reduce the codec bitrate if possible */ if (ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&cur_br)!=0){ ms_message("AudioBitrateController: GET_BITRATE failed"); return 0; } new_br=cur_br-((cur_br*action->value)/100); ms_message("MSAudioBitrateDriver: Attempting to reduce audio bitrate to %i",new_br); if (ms_filter_call_method(obj->encoder,MS_FILTER_SET_BITRATE,&new_br)!=0){ ms_message("MSAudioBitrateDriver: SET_BITRATE failed, incrementing ptime"); inc_ptime(obj); return 0; } new_br=0; ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&new_br); ms_message("MSAudioBitrateDriver: bitrate actually set to %i",new_br); obj->cur_bitrate=new_br; } } }else if (action->type==MSRateControlActionDecreasePacketRate){ inc_ptime(obj); }else if (action->type==MSRateControlActionIncreaseQuality){ if (obj->cur_bitrate<obj->nom_bitrate){ ms_message("MSAudioBitrateDriver: increasing bitrate of codec"); if (ms_filter_call_method(obj->encoder,MS_FILTER_SET_BITRATE,&obj->nom_bitrate)!=0){ ms_message("MSAudioBitrateDriver: could not restore nominal codec bitrate (%i)",obj->nom_bitrate); }else obj->cur_bitrate=obj->nom_bitrate; }else if (obj->cur_ptime>obj->min_ptime){ obj->cur_ptime-=obj->min_ptime; apply_ptime(obj); }else return -1; } return 0; }
static int audio_bitrate_driver_execute_action(MSBitrateDriver *objbase, const MSRateControlAction *action){ MSAudioBitrateDriver *obj=(MSAudioBitrateDriver*)objbase; ms_message("MSAudioBitrateDriver: executing action of type %s, value=%i",ms_rate_control_action_type_name(action->type),action->value); if (obj->nom_bitrate==0){ ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&obj->nom_bitrate); if (obj->nom_bitrate==0){ ms_warning("MSAudioBitrateDriver: Not doing bitrate control on audio encoder, it does not seem to support that. Controlling ptime only."); obj->nom_bitrate=-1; }else obj->cur_bitrate=obj->nom_bitrate; } if (obj->cur_ptime==0){ ms_filter_call_method(obj->encoder,MS_AUDIO_ENCODER_GET_PTIME,&obj->cur_ptime); if (obj->cur_ptime==0){ ms_warning("MSAudioBitrateDriver: encoder %s does not implement MS_AUDIO_ENCODER_GET_PTIME. Consider to implement this method for better accuracy of rate control.",obj->encoder->desc->name); obj->cur_ptime=obj->min_ptime; } } if (action->type==MSRateControlActionDecreaseBitrate){ /*reducing bitrate of the codec isn't sufficient. Increasing ptime is much more efficient*/ if (inc_ptime(obj)==-1){ if (obj->nom_bitrate>0){ int cur_br=0; int new_br; /*if max ptime is reached, then try to reduce the codec bitrate if possible */ if (ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&cur_br)!=0){ ms_message("MSAudioBitrateDriver: GET_BITRATE failed"); return 0; } obj->cur_bitrate=cur_br; new_br=cur_br-((cur_br*action->value)/100); ms_message("MSAudioBitrateDriver: Attempting to reduce audio bitrate to %i",new_br); if (ms_filter_call_method(obj->encoder,MS_FILTER_SET_BITRATE,&new_br)!=0){ ms_message("MSAudioBitrateDriver: SET_BITRATE failed, incrementing ptime"); inc_ptime(obj); return 0; } new_br=0; ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&new_br); ms_message("MSAudioBitrateDriver: bitrate actually set to %i",new_br); obj->cur_bitrate=new_br; } } }else if (action->type==MSRateControlActionDecreasePacketRate){ inc_ptime(obj); }else if (action->type==MSRateControlActionIncreaseQuality){ if (obj->nom_bitrate>0){ if (ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&obj->cur_bitrate)==0){ if (obj->cur_bitrate > 0 && obj->cur_bitrate<obj->nom_bitrate){ obj->cur_bitrate=(obj->cur_bitrate*140)/100; if (obj->cur_bitrate> obj->nom_bitrate) obj->cur_bitrate=obj->nom_bitrate; ms_message("MSAudioBitrateDriver: increasing bitrate of codec to %i",obj->cur_bitrate); if (ms_filter_call_method(obj->encoder,MS_FILTER_SET_BITRATE,&obj->cur_bitrate)!=0){ ms_message("MSAudioBitrateDriver: could not set codec bitrate to %i",obj->cur_bitrate); }else obj->cur_bitrate=obj->nom_bitrate; /* so that we do not attempt this anymore*/ return 0; } }else ms_warning("MSAudioBitrateDriver: MS_FILTER_GET_BITRATE failed."); } if (obj->cur_ptime>obj->min_ptime){ obj->cur_ptime-=obj->min_ptime; apply_ptime(obj); }else return -1; } return 0; }
static int audio_bitrate_driver_execute_action(MSBitrateDriver *objbase, const MSRateControlAction *action){ MSAudioBitrateDriver *obj=(MSAudioBitrateDriver*)objbase; ms_message("MSAudioBitrateDriver: executing action of type %s, value=%i",ms_rate_control_action_type_name(action->type),action->value); if (obj->nom_bitrate==0){ ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&obj->nom_bitrate); if (obj->nom_bitrate==0){ ms_warning("MSAudioBitrateDriver: Not doing bitrate control on audio encoder, it does not seem to support that. Controlling ptime only."); obj->nom_bitrate=-1; }else obj->cur_bitrate=obj->nom_bitrate; } if (obj->cur_ptime==0 || ms_filter_has_method(obj->encoder,MS_AUDIO_ENCODER_GET_PTIME)){ /*always sync current ptime if possible*/ ms_filter_call_method(obj->encoder,MS_AUDIO_ENCODER_GET_PTIME,&obj->cur_ptime); if (obj->cur_ptime==0){ ms_warning("MSAudioBitrateDriver: encoder %s does not implement MS_AUDIO_ENCODER_GET_PTIME. Consider to implement this method for better accuracy of rate control.",obj->encoder->desc->name); obj->cur_ptime=obj->min_ptime; } } if (action->type==MSRateControlActionDecreaseBitrate){ /*reducing bitrate of the codec isn't sufficient. Increasing ptime is much more efficient*/ if ((obj->encoder_caps & MS_AUDIO_ENCODER_CAP_AUTO_PTIME) || inc_ptime(obj)){ if (obj->nom_bitrate>0){ int cur_br=0; int new_br; /*if max ptime is reached, then try to reduce the codec bitrate if possible */ if (ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&cur_br)!=0){ ms_message("MSAudioBitrateDriver: GET_BITRATE failed"); return 0; } obj->cur_bitrate=cur_br; new_br=cur_br-((cur_br*action->value)/100); ms_message("MSAudioBitrateDriver: Attempting to reduce audio bitrate from %i to %i",cur_br,new_br); if (ms_filter_call_method(obj->encoder,MS_FILTER_SET_BITRATE,&new_br)!=0){ ms_message("MSAudioBitrateDriver: SET_BITRATE failed, incrementing ptime"); return inc_ptime(obj); } else { rtp_session_set_target_upload_bandwidth(obj->session, new_br); } new_br=0; ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&new_br); ms_message("MSAudioBitrateDriver: bitrate actually set to %i",new_br); obj->cur_bitrate=new_br; } } }else if (action->type==MSRateControlActionDecreasePacketRate){ return inc_ptime(obj); }else if (action->type==MSRateControlActionIncreaseQuality){ int ret=0; if (!(obj->encoder_caps & MS_AUDIO_ENCODER_CAP_AUTO_PTIME)){ if (obj->cur_ptime>obj->min_ptime){ ret=dec_ptime(obj); } } if (obj->nom_bitrate>0){ int cur_bitrate=0; if (ms_filter_call_method(obj->encoder,MS_FILTER_GET_BITRATE,&cur_bitrate)==0){ if (cur_bitrate > 0 && cur_bitrate<obj->nom_bitrate){ obj->cur_bitrate=(obj->cur_bitrate*140)/100; if (obj->cur_bitrate>= obj->nom_bitrate) { obj->cur_bitrate=obj->nom_bitrate; ret=-1;/*we reached the nominal value*/ } ms_message("MSAudioBitrateDriver: increasing bitrate of codec to %i",obj->cur_bitrate); if (ms_filter_call_method(obj->encoder,MS_FILTER_SET_BITRATE,&obj->cur_bitrate)!=0){ ms_message("MSAudioBitrateDriver: could not set codec bitrate to %i",obj->cur_bitrate); }else { rtp_session_set_target_upload_bandwidth(obj->session, obj->cur_bitrate); } } }else ms_warning("MSAudioBitrateDriver: MS_FILTER_GET_BITRATE failed."); } return ret; } return 0; }