Merge pull request #1655 from 24i/feature/handle-props-racing-conditions-on-expplayer

handle racing conditions when props are setted on exoplayer
This commit is contained in:
Ash Mishra 2019-07-11 09:30:35 -07:00 committed by GitHub
commit 03114bc780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 47 deletions

View File

@ -1,5 +1,8 @@
## Changelog ## Changelog
### next
* Handle racing conditions when props are setted on exoplayer
### Version 4.4.3 ### Version 4.4.3
* Fix mute/unmute when controls are present (iOS) [#1654](https://github.com/react-native-community/react-native-video/pull/1654) * Fix mute/unmute when controls are present (iOS) [#1654](https://github.com/react-native-community/react-native-video/pull/1654)
* Fix Android videos being able to play with background music/audio from other apps. * Fix Android videos being able to play with background music/audio from other apps.

View File

@ -144,6 +144,7 @@ class ReactExoplayerView extends FrameLayout implements
private boolean playInBackground = false; private boolean playInBackground = false;
private Map<String, String> requestHeaders; private Map<String, String> requestHeaders;
private boolean mReportBandwidth = false; private boolean mReportBandwidth = false;
private boolean controls;
// \ End props // \ End props
// React // React
@ -267,6 +268,7 @@ class ReactExoplayerView extends FrameLayout implements
* Toggling the visibility of the player control view * Toggling the visibility of the player control view
*/ */
private void togglePlayerControlVisibility() { private void togglePlayerControlVisibility() {
if(player == null) return;
reLayout(playerControlView); reLayout(playerControlView);
if (playerControlView.isVisible()) { if (playerControlView.isVisible()) {
playerControlView.hide(); playerControlView.hide();
@ -312,10 +314,15 @@ class ReactExoplayerView extends FrameLayout implements
* Adding Player control to the frame layout * Adding Player control to the frame layout
*/ */
private void addPlayerControl() { private void addPlayerControl() {
if(player == null) return;
LayoutParams layoutParams = new LayoutParams( LayoutParams layoutParams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT); LayoutParams.MATCH_PARENT);
playerControlView.setLayoutParams(layoutParams); playerControlView.setLayoutParams(layoutParams);
int indexOfPC = indexOfChild(playerControlView);
if (indexOfPC != -1) {
removeViewAt(indexOfPC);
}
addView(playerControlView, 1, layoutParams); addView(playerControlView, 1, layoutParams);
} }
@ -333,53 +340,62 @@ class ReactExoplayerView extends FrameLayout implements
} }
private void initializePlayer() { private void initializePlayer() {
if (player == null) { ReactExoplayerView self = this;
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER); // This ensures all props have been setted, to avoid async racing conditions.
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); new Handler().postDelayed(new Runnable() {
trackSelector.setParameters(trackSelector.buildUponParameters() @Override
public void run() {
if (player == null) {
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(BANDWIDTH_METER);
trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
trackSelector.setParameters(trackSelector.buildUponParameters()
.setMaxVideoBitrate(maxBitRate == 0 ? Integer.MAX_VALUE : maxBitRate)); .setMaxVideoBitrate(maxBitRate == 0 ? Integer.MAX_VALUE : maxBitRate));
DefaultAllocator allocator = new DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE); DefaultAllocator allocator = new DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE);
DefaultLoadControl defaultLoadControl = new DefaultLoadControl(allocator, minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs, -1, true); DefaultLoadControl defaultLoadControl = new DefaultLoadControl(allocator, minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs, -1, true);
player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, defaultLoadControl); player = ExoPlayerFactory.newSimpleInstance(getContext(), trackSelector, defaultLoadControl);
player.addListener(this); player.addListener(self);
player.setMetadataOutput(this); player.setMetadataOutput(self);
exoPlayerView.setPlayer(player); exoPlayerView.setPlayer(player);
audioBecomingNoisyReceiver.setListener(this); audioBecomingNoisyReceiver.setListener(self);
BANDWIDTH_METER.addEventListener(new Handler(), this); BANDWIDTH_METER.addEventListener(new Handler(), self);
setPlayWhenReady(!isPaused); setPlayWhenReady(!isPaused);
playerNeedsSource = true; playerNeedsSource = true;
PlaybackParameters params = new PlaybackParameters(rate, 1f); PlaybackParameters params = new PlaybackParameters(rate, 1f);
player.setPlaybackParameters(params); player.setPlaybackParameters(params);
} }
if (playerNeedsSource && srcUri != null) { if (playerNeedsSource && srcUri != null) {
ArrayList<MediaSource> mediaSourceList = buildTextSources(); ArrayList<MediaSource> mediaSourceList = buildTextSources();
MediaSource videoSource = buildMediaSource(srcUri, extension); MediaSource videoSource = buildMediaSource(srcUri, extension);
MediaSource mediaSource; MediaSource mediaSource;
if (mediaSourceList.size() == 0) { if (mediaSourceList.size() == 0) {
mediaSource = videoSource; mediaSource = videoSource;
} else { } else {
mediaSourceList.add(0, videoSource); mediaSourceList.add(0, videoSource);
MediaSource[] textSourceArray = mediaSourceList.toArray( MediaSource[] textSourceArray = mediaSourceList.toArray(
new MediaSource[mediaSourceList.size()] new MediaSource[mediaSourceList.size()]
); );
mediaSource = new MergingMediaSource(textSourceArray); mediaSource = new MergingMediaSource(textSourceArray);
}
boolean haveResumePosition = resumeWindow != C.INDEX_UNSET;
if (haveResumePosition) {
player.seekTo(resumeWindow, resumePosition);
}
player.prepare(mediaSource, !haveResumePosition, false);
playerNeedsSource = false;
eventEmitter.loadStart();
loadVideoStarted = true;
}
// Initializing the playerControlView
initializePlayerControl();
setControls(controls);
applyModifiers();
} }
}, 1);
boolean haveResumePosition = resumeWindow != C.INDEX_UNSET;
if (haveResumePosition) {
player.seekTo(resumeWindow, resumePosition);
}
player.prepare(mediaSource, !haveResumePosition, false);
playerNeedsSource = false;
eventEmitter.loadStart();
loadVideoStarted = true;
}
// Initializing the playerControlView
initializePlayerControl();
} }
private MediaSource buildMediaSource(Uri uri, String overrideExtension) { private MediaSource buildMediaSource(Uri uri, String overrideExtension) {
@ -439,8 +455,8 @@ class ReactExoplayerView extends FrameLayout implements
updateResumePosition(); updateResumePosition();
player.release(); player.release();
player.setMetadataOutput(null); player.setMetadataOutput(null);
player = null;
trackSelector = null; trackSelector = null;
player = null;
} }
progressHandler.removeMessages(SHOW_PROGRESS); progressHandler.removeMessages(SHOW_PROGRESS);
themedReactContext.removeLifecycleEventListener(this); themedReactContext.removeLifecycleEventListener(this);
@ -894,6 +910,10 @@ class ReactExoplayerView extends FrameLayout implements
exoPlayerView.setResizeMode(resizeMode); exoPlayerView.setResizeMode(resizeMode);
} }
private void applyModifiers() {
setRepeatModifier(repeat);
}
public void setRepeatModifier(boolean repeat) { public void setRepeatModifier(boolean repeat) {
if (player != null) { if (player != null) {
if (repeat) { if (repeat) {
@ -906,6 +926,7 @@ class ReactExoplayerView extends FrameLayout implements
} }
public void setSelectedTrack(int trackType, String type, Dynamic value) { public void setSelectedTrack(int trackType, String type, Dynamic value) {
if (player == null) return;
int rendererIndex = getTrackRendererIndex(trackType); int rendererIndex = getTrackRendererIndex(trackType);
if (rendererIndex == C.INDEX_UNSET) { if (rendererIndex == C.INDEX_UNSET) {
return; return;
@ -1156,10 +1177,15 @@ class ReactExoplayerView extends FrameLayout implements
* @param controls Controls prop, if true enable controls, if false disable them * @param controls Controls prop, if true enable controls, if false disable them
*/ */
public void setControls(boolean controls) { public void setControls(boolean controls) {
if (controls && exoPlayerView != null) { this.controls = controls;
if (player == null || exoPlayerView == null) return;
if (controls) {
addPlayerControl(); addPlayerControl();
} else if (getChildAt(1) instanceof PlayerControlView && exoPlayerView != null) { } else {
removeViewAt(1); int indexOfPC = indexOfChild(playerControlView);
if (indexOfPC != -1) {
removeViewAt(indexOfPC);
}
} }
} }
} }