Skip to content

Adaptative video bitrate

Pedro Sánchez edited this page Jul 3, 2024 · 4 revisions

Adaptative video bitrate

This feature require API19+.

You can select manually your video bitrate on fly with this method:

rtmpCamera1.setVideoBitrateOnFly(bitrate);

Also you can set auto adaptive video bitrate.

Code example:

Version 1.6.4+

  //class needed
  private BitrateAdapter bitrateAdapter;

  @Override
  public void onConnectionSuccess() {
    bitrateAdapter = new BitrateAdapter(new BitrateAdapter.Listener() {

      @Override
      public void onBitrateAdapted(int bitrate) {
        rtmpCamera1.setVideoBitrateOnFly(bitrate);
      }
    });
    bitrateAdapter.setMaxBitrate(videoBitrate);
  }

  @Override
  public void onNewBitrate(long bitrate) {
    if (bitrateAdapter != null) bitrateAdapter.adaptBitrate(bitrate);
  }

Where videoBitrate is the value set to prepareVideo method.

Deprecated

//classes needed
  private ConnectionClassManager connectionClassManager;
  private UploadBandwidthSampler uploadBandwidthSampler;

//initialize classes
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    connectionClassManager = ConnectionClassManager.getInstance();
    uploadBandwidthSampler = UploadBandwidthSampler.getInstance();
  }

//register listener
@Override
  protected void onResume() {
    super.onResume();
    connectionClassManager.register(new ConnectionClassManager.ConnectionClassStateChangeListener() {
      @Override
      public void onBandwidthStateChange(double bandwidth) {
        AdapterBitrateParser.parseBitrate(videoBitrate, (int)bandwidth, new AdapterBitrateParser.Callback() {
          @Override
          public void onNewBitrate(int bitrate) {
            rtmpCamera1.setVideoBitrateOnFly(bitrate);
          }
        });
      }
    });
  }

//unregister listener
 @Override
  protected void onPause() {
    super.onPause();
    connectionClassManager.remove();
  }

//You need start adaptative bitrate before startStream
uploadBandwidthSampler.startSampling();
rtmpCamera1.startStream(etUrl.getText().toString());

//You should stop adaptive bitrate before stopStream
uploadBandwidthSampler.stopSampling();
rtmpCamera1.stopStream();

Where videoBitrate is the value set to prepareVideo method.

Optionally you can:

//Let library auto calculate your max video bitrate related by your resolution.
AdapterBitrateParser.calculateMaxVideoBitrate(rtmpCamera1.getResolutionValue()); 

//Select your own max video bitrate (kbps)
AdapterBitrateParser.maxVideoBitrate = 1200;

//Set delay to check video bitrate (ms)
AdapterBitrateParser.DELAY = 1000;

//Set video bitrate interval that library will use to increase or decrease it (kbps).
AdapterBitrateParser.DIFFERENCE = 500;.
Clone this wiki locally