0

I'm trying to get the bandwidth using getBitRateEstimate but it keeps returning -1 :

(I think the issue has something to do with not providing the meter to the DataSourceFactory, but I can't figure out how to do it)

Here is the code :

private void createView(Context context) {
    LayoutParams layoutParams = new LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);

    playerView = new PlayerView(getContext());
    bandwidthMeter = new DefaultBandwidthMeter();
    playerView.setLayoutParams(layoutParams);
    myContext = context;
    addView(playerView, 0, layoutParams);
}

public void initializeMediaPlayer() {
    /**
     * Create Simple Exoplayer Player
     */
    if(player == null) {
        //bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory = new 
     AdaptiveTrackSelection.Factory(bandwidthMeter);
        TrackSelector trackSelector = new 
     DefaultTrackSelector(videoTrackSelectionFactory);
        DefaultAllocator defaultAllocator = new 
     DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE);
        LoadControl loadControl = new 
     DefaultLoadControl(defaultAllocator, 1450, 3000, 1000, 1000, 
        C.LENGTH_UNSET, false);

        RenderersFactory renderersFactory = new DefaultRenderersFactory(myContext);
        player = ExoPlayerFactory.newSimpleInstance(renderersFactory, trackSelector, loadControl);
        playerView.setUseController(false);
        playerView.setPlayer(player);
    }
    /**
     * Create RTMP Data Source
     */

    rtmpDataSourceFactory = new RtmpDataSourceFactory();
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    MediaSource videoSource = new ExtractorMediaSource.Factory(rtmpDataSourceFactory).createMediaSource(Uri.parse(source));
    //MediaSource videoSource = new ExtractorMediaSource(Uri.parse("rtmp://35.231.80.114/live/myStream"),
    //      rtmpDataSourceFactory, extractorsFactory, null, null);
    player.addListener(new PlayerEventListener());
    player.setPlayWhenReady(true);
    player.seekToDefaultPosition();
    player.prepare(videoSource);

}
ArzhRo
  • 71
  • 2
  • 4

1 Answers1

0

The RtmpDataSourceFactory has a constructor which takes a TransferListener as a parameter. The DefaultBandwidthMeter implements the TransferListener which you hence can pass to the constructor:

DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
RtmpDataSourceFactory rtmpDataSourceFactory =
    new RtmpDataSourceFactory(defaultBandwidthMeter);

With this the bandwidth meter is notified about transferred data with which to calculate the bitrate estimate.

marcbaechinger
  • 2,759
  • 18
  • 21
  • Thanks, yeah it was my fault for using a bandWidthMeter and not a defaultBandWidthMeter (the former won't go in the factory). – ArzhRo Mar 29 '18 at 21:51