Code cleanup

Code cleanup
This commit is contained in:
sridhar 2018-11-01 21:41:57 +05:30
parent 9dead2fefc
commit 5dce3e2161
4 changed files with 22 additions and 56 deletions

View File

@ -246,7 +246,7 @@ export default class Video extends Component {
onVideoSeek: this._onSeek, onVideoSeek: this._onSeek,
onVideoEnd: this._onEnd, onVideoEnd: this._onEnd,
onVideoBuffer: this._onBuffer, onVideoBuffer: this._onBuffer,
onBandwidthUpdate: this._onBandwidthUpdate, onVideoBandwidthUpdate: this._onBandwidthUpdate,
onTimedMetadata: this._onTimedMetadata, onTimedMetadata: this._onTimedMetadata,
onVideoAudioBecomingNoisy: this._onAudioBecomingNoisy, onVideoAudioBecomingNoisy: this._onAudioBecomingNoisy,
onVideoExternalPlaybackChange: this._onExternalPlaybackChange, onVideoExternalPlaybackChange: this._onExternalPlaybackChange,
@ -294,7 +294,7 @@ Video.propTypes = {
onVideoBuffer: PropTypes.func, onVideoBuffer: PropTypes.func,
onVideoError: PropTypes.func, onVideoError: PropTypes.func,
onVideoProgress: PropTypes.func, onVideoProgress: PropTypes.func,
onBandwidthUpdate: PropTypes.func, onVideoBandwidthUpdate: PropTypes.func,
onVideoSeek: PropTypes.func, onVideoSeek: PropTypes.func,
onVideoEnd: PropTypes.func, onVideoEnd: PropTypes.func,
onTimedMetadata: PropTypes.func, onTimedMetadata: PropTypes.func,
@ -366,7 +366,6 @@ Video.propTypes = {
playWhenInactive: PropTypes.bool, playWhenInactive: PropTypes.bool,
ignoreSilentSwitch: PropTypes.oneOf(['ignore', 'obey']), ignoreSilentSwitch: PropTypes.oneOf(['ignore', 'obey']),
reportBandwidth: PropTypes.bool, reportBandwidth: PropTypes.bool,
bandwidthUpdateInterval: PropTypes.number,
disableFocus: PropTypes.bool, disableFocus: PropTypes.bool,
controls: PropTypes.bool, controls: PropTypes.bool,
audioOnly: PropTypes.bool, audioOnly: PropTypes.bool,
@ -379,6 +378,7 @@ Video.propTypes = {
onBuffer: PropTypes.func, onBuffer: PropTypes.func,
onError: PropTypes.func, onError: PropTypes.func,
onProgress: PropTypes.func, onProgress: PropTypes.func,
onBandwidthUpdate: PropTypes.func,
onSeek: PropTypes.func, onSeek: PropTypes.func,
onEnd: PropTypes.func, onEnd: PropTypes.func,
onFullscreenPlayerWillPresent: PropTypes.func, onFullscreenPlayerWillPresent: PropTypes.func,

View File

@ -78,13 +78,14 @@ import java.util.Locale;
class ReactExoplayerView extends FrameLayout implements class ReactExoplayerView extends FrameLayout implements
LifecycleEventListener, LifecycleEventListener,
ExoPlayer.EventListener, ExoPlayer.EventListener,
BandwidthMeter.EventListener,
BecomingNoisyListener, BecomingNoisyListener,
AudioManager.OnAudioFocusChangeListener, AudioManager.OnAudioFocusChangeListener,
MetadataRenderer.Output { MetadataRenderer.Output {
private static final String TAG = "ReactExoplayerView"; private static final String TAG = "ReactExoplayerView";
private static DefaultBandwidthMeter BANDWIDTH_METER; private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
private static final CookieManager DEFAULT_COOKIE_MANAGER; private static final CookieManager DEFAULT_COOKIE_MANAGER;
private static final int SHOW_PROGRESS = 1; private static final int SHOW_PROGRESS = 1;
private static final int REPORT_BANDWIDTH = 1; private static final int REPORT_BANDWIDTH = 1;
@ -95,7 +96,7 @@ class ReactExoplayerView extends FrameLayout implements
} }
private final VideoEventEmitter eventEmitter; private final VideoEventEmitter eventEmitter;
private Handler mainHandler; private Handler mainHandler;
private ExoPlayerView exoPlayerView; private ExoPlayerView exoPlayerView;
@ -135,7 +136,6 @@ class ReactExoplayerView extends FrameLayout implements
private boolean playInBackground = false; private boolean playInBackground = false;
private boolean useTextureView = false; private boolean useTextureView = false;
private Map<String, String> requestHeaders; private Map<String, String> requestHeaders;
private float mBandwidthUpdateInterval = 250.0f;
private boolean mReportBandwidth = false; private boolean mReportBandwidth = false;
// \ End props // \ End props
@ -164,31 +164,14 @@ 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) { public ReactExoplayerView(ThemedReactContext context) {
super(context); super(context);
this.themedReactContext = context; this.themedReactContext = context;
buildBandwidthMeter(); this.eventEmitter = new VideoEventEmitter(context);
createViews(); createViews();
this.eventEmitter = new VideoEventEmitter(context);
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
themedReactContext.addLifecycleEventListener(this); themedReactContext.addLifecycleEventListener(this);
audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext); audioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(themedReactContext);
@ -263,17 +246,15 @@ class ReactExoplayerView extends FrameLayout implements
stopPlayback(); stopPlayback();
} }
//BandwidthMeter.EventListener implementation
// Internal methods @Override
private void buildBandwidthMeter() { public void onBandwidthSample(int elapsedMs, long bytes, long bitrate) {
BANDWIDTH_METER = new DefaultBandwidthMeter(new Handler(), new BandwidthMeter.EventListener() { if (mReportBandwidth == true) {
@Override eventEmitter.bandwidthReport(bitrate);
public void onBandwidthSample(int elapsedMs, long bytes, long bitrate) { }
System.out.println("Debug::::In function onBandwidthSample, elapsedMs = " + elapsedMs + " bytes = " + bytes + " bitrate = " + bitrate);
}
});
} }
// Internal methods
private void initializePlayer() { private void initializePlayer() {
if (player == null) { if (player == null) {
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
@ -285,6 +266,7 @@ class ReactExoplayerView extends FrameLayout implements
player.setMetadataOutput(this); player.setMetadataOutput(this);
exoPlayerView.setPlayer(player); exoPlayerView.setPlayer(player);
audioBecomingNoisyReceiver.setListener(this); audioBecomingNoisyReceiver.setListener(this);
BANDWIDTH_METER.addEventListener(new Handler(), this);
setPlayWhenReady(!isPaused); setPlayWhenReady(!isPaused);
playerNeedsSource = true; playerNeedsSource = true;
@ -372,10 +354,10 @@ class ReactExoplayerView extends FrameLayout implements
player = null; player = null;
trackSelector = null; trackSelector = null;
} }
bandwidthReporter.removeMessages(REPORT_BANDWIDTH);
progressHandler.removeMessages(SHOW_PROGRESS); progressHandler.removeMessages(SHOW_PROGRESS);
themedReactContext.removeLifecycleEventListener(this); themedReactContext.removeLifecycleEventListener(this);
audioBecomingNoisyReceiver.removeListener(); audioBecomingNoisyReceiver.removeListener();
BANDWIDTH_METER.removeEventListener(this);
} }
private boolean requestAudioFocus() { private boolean requestAudioFocus() {
@ -792,19 +774,9 @@ class ReactExoplayerView extends FrameLayout implements
mProgressUpdateInterval = progressUpdateInterval; mProgressUpdateInterval = progressUpdateInterval;
} }
public void setBandwidthUpdateInterval(final float bandwidthUpdateInterval) { public void setReportBandwidth(boolean reportBandwidth) {
mBandwidthUpdateInterval = bandwidthUpdateInterval;
}
public void setReportBandwidthModifier(boolean reportBandwidth) {
mReportBandwidth = 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) { public void setRawSrc(final Uri uri, final String extension) {
if (uri != null) { if (uri != null) {

View File

@ -45,7 +45,6 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_MS = "bufferForPlaybackMs"; private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_MS = "bufferForPlaybackMs";
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS = "bufferForPlaybackAfterRebufferMs"; private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS = "bufferForPlaybackAfterRebufferMs";
private static final String PROP_PROGRESS_UPDATE_INTERVAL = "progressUpdateInterval"; private static final String PROP_PROGRESS_UPDATE_INTERVAL = "progressUpdateInterval";
private static final String PROP_BANDWIDTH_UPDATE_INTERVAL = "bandwidthUpdateInterval";
private static final String PROP_REPORT_BANDWIDTH = "reportBandwidth"; private static final String PROP_REPORT_BANDWIDTH = "reportBandwidth";
private static final String PROP_SEEK = "seek"; private static final String PROP_SEEK = "seek";
private static final String PROP_RATE = "rate"; private static final String PROP_RATE = "rate";
@ -209,14 +208,9 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
videoView.setProgressUpdateInterval(progressUpdateInterval); videoView.setProgressUpdateInterval(progressUpdateInterval);
} }
@ReactProp(name = PROP_BANDWIDTH_UPDATE_INTERVAL, defaultFloat = 250.0f)
public void setBandwidthUpdateInterval(final ReactExoplayerView videoView, final float bandwidthUpdateInterval) {
videoView.setBandwidthUpdateInterval(bandwidthUpdateInterval);
}
@ReactProp(name = PROP_REPORT_BANDWIDTH, defaultBoolean = false) @ReactProp(name = PROP_REPORT_BANDWIDTH, defaultBoolean = false)
public void setReportBandwidth(final ReactExoplayerView videoView, final boolean reportBandwidth) { public void setReportBandwidth(final ReactExoplayerView videoView, final boolean reportBandwidth) {
videoView.setReportBandwidthModifier(reportBandwidth); videoView.setReportBandwidth(reportBandwidth);
} }
@ReactProp(name = PROP_SEEK) @ReactProp(name = PROP_SEEK)

View File

@ -29,7 +29,7 @@ class VideoEventEmitter {
private static final String EVENT_LOAD = "onVideoLoad"; private static final String EVENT_LOAD = "onVideoLoad";
private static final String EVENT_ERROR = "onVideoError"; private static final String EVENT_ERROR = "onVideoError";
private static final String EVENT_PROGRESS = "onVideoProgress"; private static final String EVENT_PROGRESS = "onVideoProgress";
private static final String EVENT_BANDWIDTH = "onBandwidthUpdate"; private static final String EVENT_BANDWIDTH = "onVideoBandwidthUpdate";
private static final String EVENT_SEEK = "onVideoSeek"; private static final String EVENT_SEEK = "onVideoSeek";
private static final String EVENT_END = "onVideoEnd"; private static final String EVENT_END = "onVideoEnd";
private static final String EVENT_FULLSCREEN_WILL_PRESENT = "onVideoFullscreenPlayerWillPresent"; private static final String EVENT_FULLSCREEN_WILL_PRESENT = "onVideoFullscreenPlayerWillPresent";
@ -178,7 +178,7 @@ class VideoEventEmitter {
void bandwidthReport(double bitRateEstimate) { void bandwidthReport(double bitRateEstimate) {
WritableMap event = Arguments.createMap(); WritableMap event = Arguments.createMap();
event.putDouble(EVENT_PROP_BITRATE_ESTIMATE, bitRateEstimate / 1000D); event.putDouble(EVENT_PROP_BITRATE_ESTIMATE, bitRateEstimate / 1024D);
receiveEvent(EVENT_BANDWIDTH, event); receiveEvent(EVENT_BANDWIDTH, event);
} }