Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…native-video into fix/makeVideoventEmitterAgnosticOnPlayer

# Conflicts:
#	android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java
  • Loading branch information
olivier committed Oct 12, 2023
2 parents 2adf5b8 + a4073d3 commit b89a1f4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.brentvatne.exoplayer;

import android.annotation.TargetApi;
import android.content.Context;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.brentvatne.exoplayer;

import java.io.IOException;
import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy;
import com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException;
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy.LoadErrorInfo;
import com.google.android.exoplayer2.C;

public final class ReactExoplayerLoadErrorHandlingPolicy extends DefaultLoadErrorHandlingPolicy {
private int minLoadRetryCount = Integer.MAX_VALUE;
private final int minLoadRetryCount;

public ReactExoplayerLoadErrorHandlingPolicy(int minLoadRetryCount) {
super(minLoadRetryCount);
Expand All @@ -16,9 +14,11 @@ public ReactExoplayerLoadErrorHandlingPolicy(int minLoadRetryCount) {

@Override
public long getRetryDelayMsFor(LoadErrorInfo loadErrorInfo) {
String errorMessage = loadErrorInfo.exception.getMessage();

if (
loadErrorInfo.exception instanceof HttpDataSourceException &&
(loadErrorInfo.exception.getMessage() == "Unable to connect" || loadErrorInfo.exception.getMessage() == "Software caused connection abort")
errorMessage != null && (errorMessage.equals("Unable to connect") || errorMessage.equals("Software caused connection abort"))
) {
// Capture the error we get when there is no network connectivity and keep retrying it
return 1000; // Retry every second
Expand All @@ -33,4 +33,4 @@ public long getRetryDelayMsFor(LoadErrorInfo loadErrorInfo) {
public int getMinimumLoadableRetryCount(int dataType) {
return Integer.MAX_VALUE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.widget.FrameLayout;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import androidx.activity.OnBackPressedCallback;

Expand Down Expand Up @@ -491,7 +492,7 @@ private void addPlayerControl() {
* Update the layout
* @param view view needs to update layout
*
* This is a workaround for the open bug in react-native: https://github.com/facebook/react-native/issues/17968
* This is a workaround for the open bug in react-native: <a href="https://github.com/facebook/react-native/issues/17968">...</a>
*/
private void reLayout(View view) {
if (view == null) return;
Expand All @@ -501,8 +502,8 @@ private void reLayout(View view) {
}

private class RNVLoadControl extends DefaultLoadControl {
private int availableHeapInBytes = 0;
private Runtime runtime;
private final int availableHeapInBytes;
private final Runtime runtime;
public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBufferMs, int bufferForPlaybackMs, int bufferForPlaybackAfterRebufferMs, int targetBufferBytes, boolean prioritizeTimeOverSizeThresholds, int backBufferDurationMs, boolean retainBackBufferFromKeyframe) {
super(allocator,
minBufferMs,
Expand All @@ -514,7 +515,7 @@ public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBuffer
backBufferDurationMs,
retainBackBufferFromKeyframe);
runtime = Runtime.getRuntime();
ActivityManager activityManager = (ActivityManager) themedReactContext.getSystemService(themedReactContext.ACTIVITY_SERVICE);
ActivityManager activityManager = (ActivityManager) themedReactContext.getSystemService(ThemedReactContext.ACTIVITY_SERVICE);
availableHeapInBytes = (int) Math.floor(activityManager.getMemoryClass() * maxHeapAllocationPercent * 1024 * 1024);
}

Expand Down Expand Up @@ -545,13 +546,6 @@ public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurat
}
}

private void startBufferCheckTimer() {
Player player = this.player;
VideoEventEmitter eventEmitter = this.eventEmitter;
Handler mainHandler = this.mainHandler;

}

private void initializePlayer() {
ReactExoplayerView self = this;
Activity activity = themedReactContext.getCurrentActivity();
Expand Down Expand Up @@ -740,7 +734,6 @@ private void finishPlayerInitialization() {
initializePlayerControl();
setControls(controls);
applyModifiers();
startBufferCheckTimer();
}

private DrmSessionManager buildDrmSessionManager(UUID uuid, String licenseUrl, String[] keyRequestPropertiesArray) throws UnsupportedDrmException {
Expand Down Expand Up @@ -796,8 +789,8 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension, DrmSessi
}

MediaItem mediaItem = mediaItemBuilder.build();
MediaSource mediaSource = null;
DrmSessionManagerProvider drmProvider = null;
MediaSource mediaSource;
DrmSessionManagerProvider drmProvider;
if (drmSessionManager != null) {
drmProvider = new DrmSessionManagerProvider() {
@Override
Expand Down Expand Up @@ -874,9 +867,7 @@ private ArrayList<MediaSource> buildTextSources() {
Uri uri = Uri.parse(textTrack.getString("uri"));
MediaSource textSource = buildTextSource(title, uri, textTrack.getString("type"),
language);
if (textSource != null) {
textSources.add(textSource);
}
textSources.add(textSource);
}
return textSources;
}
Expand Down Expand Up @@ -1080,7 +1071,7 @@ public void onIsLoadingChanged(boolean isLoading) {
}

@Override
public void onEvents(Player player, Player.Events events) {
public void onEvents(@NonNull Player player, Player.Events events) {
if (events.contains(Player.EVENT_PLAYBACK_STATE_CHANGED) || events.contains(Player.EVENT_PLAY_WHEN_READY_CHANGED)) {
int playbackState = player.getPlaybackState();
boolean playWhenReady = player.getPlayWhenReady();
Expand Down Expand Up @@ -1128,6 +1119,7 @@ public void onEvents(Player player, Player.Events events) {
text += "unknown";
break;
}
DebugLog.d(TAG, text);
}
}

Expand Down Expand Up @@ -1273,12 +1265,12 @@ private ArrayList<VideoTrack> getVideoTrackInfoFromManifest(int retryCount) {
final Uri sourceUri = this.srcUri;
final long startTime = this.contentStartTime * 1000 - 100; // s -> ms with 100ms offset

Future<ArrayList<VideoTrack>> result = es.submit(new Callable<ArrayList<VideoTrack>>() {
DataSource ds = dataSource;
Uri uri = sourceUri;
long startTimeUs = startTime * 1000; // ms -> us
Future<ArrayList<VideoTrack>> result = es.submit(new Callable<>() {
final DataSource ds = dataSource;
final Uri uri = sourceUri;
final long startTimeUs = startTime * 1000; // ms -> us

public ArrayList<VideoTrack> call() throws Exception {
public ArrayList<VideoTrack> call() {
ArrayList<VideoTrack> videoTracks = new ArrayList<>();
try {
DashManifest manifest = DashUtil.loadManifest(this.ds, this.uri);
Expand Down Expand Up @@ -1313,7 +1305,9 @@ public ArrayList<VideoTrack> call() throws Exception {
}
}
}
} catch (Exception e) {}
} catch (Exception e) {
DebugLog.w(TAG, "error in getVideoTrackInfoFromManifest:" + e.getMessage());
}
return null;
}
});
Expand All @@ -1325,7 +1319,9 @@ public ArrayList<VideoTrack> call() throws Exception {
}
es.shutdown();
return results;
} catch (Exception e) {}
} catch (Exception e) {
DebugLog.w(TAG, "error in getVideoTrackInfoFromManifest handling request:" + e.getMessage());
}

return null;
}
Expand Down Expand Up @@ -1365,15 +1361,11 @@ private void onBuffering(boolean buffering) {
}

isBuffering = buffering;
if (buffering) {
eventEmitter.buffering(true);
} else {
eventEmitter.buffering(false);
}
eventEmitter.buffering(buffering);
}

@Override
public void onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, int reason) {
public void onPositionDiscontinuity(@NonNull Player.PositionInfo oldPosition, @NonNull Player.PositionInfo newPosition, int reason) {
if (playerNeedsSource) {
// This will only occur if the user has performed a seek whilst in the error state. Update the
// resume position so that if the user then retries, playback will resume from the position to
Expand All @@ -1395,7 +1387,7 @@ public void onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.Posi
}

@Override
public void onTimelineChanged(Timeline timeline, int reason) {
public void onTimelineChanged(@NonNull Timeline timeline, int reason) {
// Do nothing.
}

Expand All @@ -1422,7 +1414,7 @@ public void onRepeatModeChanged(int repeatMode) {
}

@Override
public void onTracksChanged(Tracks tracks) {
public void onTracksChanged(@NonNull Tracks tracks) {
eventEmitter.textTracks(getTextTrackInfo());
eventEmitter.audioTracks(getAudioTrackInfo());
eventEmitter.videoTracks(getVideoTrackInfo());
Expand All @@ -1439,13 +1431,9 @@ public void onIsPlayingChanged(boolean isPlaying) {
}

@Override
public void onPlayerError(PlaybackException e) {
if (e == null) {
return;
}
public void onPlayerError(@NonNull PlaybackException e) {
String errorString = "ExoPlaybackException: " + PlaybackException.getErrorCodeName(e.errorCode);
String errorCode = "2" + String.valueOf(e.errorCode);
boolean needsReInitialization = false;
switch(e.errorCode) {
case PlaybackException.ERROR_CODE_DRM_DEVICE_REVOKED:
case PlaybackException.ERROR_CODE_DRM_LICENSE_ACQUISITION_FAILED:
Expand All @@ -1472,9 +1460,6 @@ public void onPlayerError(PlaybackException e) {
initializePlayer();
} else {
updateResumePosition();
if (needsReInitialization) {
initializePlayer();
}
}
}

Expand All @@ -1495,7 +1480,7 @@ public int getTrackRendererIndex(int trackType) {
}

@Override
public void onMetadata(Metadata metadata) {
public void onMetadata(@NonNull Metadata metadata) {
ArrayList<TimedMetadata> metadataArray = new ArrayList<>();
for (int i = 0; i < metadata.length(); i++) {
Metadata.Entry entry = metadata.get(i);
Expand Down Expand Up @@ -1739,16 +1724,16 @@ public void setSelectedTrack(int trackType, String type, Dynamic value) {
if (groupIndex == C.INDEX_UNSET && trackType == C.TRACK_TYPE_VIDEO && groups.length != 0) { // Video auto
// Add all tracks as valid options for ABR to choose from
TrackGroup group = groups.get(0);
tracks = new ArrayList<Integer>(group.length);
ArrayList<Integer> allTracks = new ArrayList<Integer>(group.length);
tracks = new ArrayList<>(group.length);
ArrayList<Integer> allTracks = new ArrayList<>(group.length);
groupIndex = 0;
for (int j = 0; j < group.length; j++) {
allTracks.add(j);
}

// Valiate list of all tracks and add only supported formats
int supportedFormatLength = 0;
ArrayList<Integer> supportedTrackList = new ArrayList<Integer>();
ArrayList<Integer> supportedTrackList = new ArrayList<>();
for (int g = 0; g < allTracks.size(); g++) {
Format format = group.getFormat(g);
if (isFormatSupported(format)) {
Expand Down Expand Up @@ -1794,7 +1779,7 @@ private boolean isFormatSupported(Format format) {
if (mimeType == null) {
return true;
}
boolean isSupported = false;
boolean isSupported;
try {
MediaCodecInfo codecInfo = MediaCodecUtil.getDecoderInfo(mimeType, false, false);
isSupported = codecInfo.isVideoSizeAndRateSupportedV21(width, height, frameRate);
Expand Down Expand Up @@ -1950,7 +1935,7 @@ public void setBackBufferDurationMs(int backBufferDurationMs) {
}

public void setContentStartTime(int contentStartTime) {
this.contentStartTime = (long)contentStartTime;
this.contentStartTime = contentStartTime;
}

public void setDisableBuffering(boolean disableBuffering) {
Expand Down
19 changes: 10 additions & 9 deletions examples/basic/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
*/
const path = require('path');
const blacklist = require('metro-config/src/defaults/exclusionList');
const {getDefaultConfig, mergeConfig} = require('@react-native/metro-config');

module.exports = {
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
resolver: {
blacklistRE: blacklist([
// This stops "react-native run-windows" from causing the metro server to crash if its already running
Expand All @@ -19,12 +26,6 @@ module.exports = {
/(.*\/react-native-video\/node_modules\/.*)$/,
]),
},
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

0 comments on commit b89a1f4

Please sign in to comment.