From 6480a6462b6c870ddbc03334db04fed25db96458 Mon Sep 17 00:00:00 2001 From: sridhar Date: Sat, 25 Aug 2018 21:53:11 +0530 Subject: [PATCH] Reporting estimatedBandwidth Reporting estimatedBandwidth --- Video.js | 10 +++++ .../exoplayer/ReactExoplayerView.java | 39 +++++++++++++++++-- .../exoplayer/ReactExoplayerViewManager.java | 12 ++++++ .../exoplayer/VideoEventEmitter.java | 15 +++++-- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/Video.js b/Video.js index a10c7dd4..a0b7f35c 100644 --- a/Video.js +++ b/Video.js @@ -99,6 +99,12 @@ export default class Video extends Component { } }; + _onBandwidthUpdate = (event) => { + if (this.props.onBandwidthUpdate) { + this.props.onBandwidthUpdate(event.nativeEvent); + } + }; + _onSeek = (event) => { if (this.state.showPoster && !this.props.audioOnly) { this.setState({showPoster: false}); @@ -234,6 +240,7 @@ export default class Video extends Component { onVideoSeek: this._onSeek, onVideoEnd: this._onEnd, onVideoBuffer: this._onBuffer, + onBandwidthUpdate: this._onBandwidthUpdate, onTimedMetadata: this._onTimedMetadata, onVideoAudioBecomingNoisy: this._onAudioBecomingNoisy, onVideoFullscreenPlayerWillPresent: this._onFullscreenPlayerWillPresent, @@ -294,6 +301,7 @@ Video.propTypes = { onVideoBuffer: PropTypes.func, onVideoError: PropTypes.func, onVideoProgress: PropTypes.func, + onBandwidthUpdate: PropTypes.func, onVideoSeek: PropTypes.func, onVideoEnd: PropTypes.func, onTimedMetadata: PropTypes.func, @@ -363,6 +371,8 @@ Video.propTypes = { playInBackground: PropTypes.bool, playWhenInactive: PropTypes.bool, ignoreSilentSwitch: PropTypes.oneOf(['ignore', 'obey']), + reportBandwidth: PropTypes.bool, + bandwidthUpdateInterval: PropTypes.number, disableFocus: PropTypes.bool, controls: PropTypes.bool, audioOnly: PropTypes.bool, diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index eb303e14..308fc075 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -86,6 +86,7 @@ class ReactExoplayerView extends FrameLayout implements private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter(); private static final CookieManager DEFAULT_COOKIE_MANAGER; private static final int SHOW_PROGRESS = 1; + private static final int REPORT_BANDWIDTH = 1; static { DEFAULT_COOKIE_MANAGER = new CookieManager(); @@ -133,6 +134,8 @@ class ReactExoplayerView extends FrameLayout implements private boolean playInBackground = false; private boolean useTextureView = false; private Map requestHeaders; + private float mBandwidthUpdateInterval = 250.0f; + private boolean mReportBandwidth = false; // \ End props // React @@ -149,10 +152,9 @@ class ReactExoplayerView extends FrameLayout implements && player.getPlaybackState() == ExoPlayer.STATE_READY && player.getPlayWhenReady() ) { - long bitRateEstimate = BANDWIDTH_METER.getBitrateEstimate(); long pos = player.getCurrentPosition(); long bufferedDuration = player.getBufferedPercentage() * player.getDuration() / 100; - eventEmitter.progressChanged(pos, bufferedDuration, player.getDuration(), bitRateEstimate); + eventEmitter.progressChanged(pos, bufferedDuration, player.getDuration()); msg = obtainMessage(SHOW_PROGRESS); sendMessageDelayed(msg, Math.round(mProgressUpdateInterval)); } @@ -161,6 +163,23 @@ class ReactExoplayerView extends FrameLayout implements } }; + private final Handler bandwidthReporter = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case REPORT_BANDWIDTH: + if (player != null) { + long bitRateEstimate = BANDWIDTH_METER.getBitrateEstimate(); + + eventEmitter.bandwidthReport(bitRateEstimate); + msg = obtainMessage(REPORT_BANDWIDTH); + sendMessageDelayed(msg, Math.round(mBandwidthUpdateInterval)); + } + break; + } + } + }; + public ReactExoplayerView(ThemedReactContext context) { super(context); this.themedReactContext = context; @@ -338,6 +357,7 @@ class ReactExoplayerView extends FrameLayout implements player = null; trackSelector = null; } + bandwidthReporter.removeMessages(REPORT_BANDWIDTH); progressHandler.removeMessages(SHOW_PROGRESS); themedReactContext.removeLifecycleEventListener(this); audioBecomingNoisyReceiver.removeListener(); @@ -579,7 +599,6 @@ class ReactExoplayerView extends FrameLayout implements videoTracks.pushMap(videoTrack); } - } return videoTracks; } @@ -758,6 +777,20 @@ class ReactExoplayerView extends FrameLayout implements mProgressUpdateInterval = progressUpdateInterval; } + public void setBandwidthUpdateInterval(final float bandwidthUpdateInterval) { + mBandwidthUpdateInterval = bandwidthUpdateInterval; + } + + public void setReportBandwidthModifier(boolean reportBandwidth) { + mReportBandwidth = reportBandwidth; + if (mReportBandwidth) { + bandwidthReporter.removeMessages(REPORT_BANDWIDTH); + bandwidthReporter.sendEmptyMessage(REPORT_BANDWIDTH); + } else { + bandwidthReporter.removeMessages(REPORT_BANDWIDTH); + } + } + public void setRawSrc(final Uri uri, final String extension) { if (uri != null) { boolean isOriginalSourceNull = srcUri == null; diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java index f7510335..7817520f 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java @@ -45,6 +45,8 @@ public class ReactExoplayerViewManager extends ViewGroupManager