chore(android): add null check for id of videoFormat (#4174)

* chore(android): add null check for id of videoFormat

* chore: null check videoFormat.id on onBandwidthSample

* fix: PR feedback

* fix: linter error

* chore: update trackId fallback value
This commit is contained in:
Seyed Mostafa Hasani 2024-09-17 15:41:02 +03:30 committed by GitHub
parent 0538b3b468
commit 1ef2b3a977
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 22 deletions

View File

@ -64,11 +64,11 @@ class VideoEventEmitter {
audioTracks: ArrayList<Track>, audioTracks: ArrayList<Track>,
textTracks: ArrayList<Track>, textTracks: ArrayList<Track>,
videoTracks: ArrayList<VideoTrack>, videoTracks: ArrayList<VideoTrack>,
trackId: String trackId: String?
) -> Unit ) -> Unit
lateinit var onVideoError: (errorString: String, exception: Exception, errorCode: String) -> Unit lateinit var onVideoError: (errorString: String, exception: Exception, errorCode: String) -> Unit
lateinit var onVideoProgress: (currentPosition: Long, bufferedDuration: Long, seekableDuration: Long, currentPlaybackTime: Double) -> Unit lateinit var onVideoProgress: (currentPosition: Long, bufferedDuration: Long, seekableDuration: Long, currentPlaybackTime: Double) -> Unit
lateinit var onVideoBandwidthUpdate: (bitRateEstimate: Long, height: Int, width: Int, trackId: String) -> Unit lateinit var onVideoBandwidthUpdate: (bitRateEstimate: Long, height: Int, width: Int, trackId: String?) -> Unit
lateinit var onVideoPlaybackStateChanged: (isPlaying: Boolean, isSeeking: Boolean) -> Unit lateinit var onVideoPlaybackStateChanged: (isPlaying: Boolean, isSeeking: Boolean) -> Unit
lateinit var onVideoSeek: (currentPosition: Long, seekTime: Long) -> Unit lateinit var onVideoSeek: (currentPosition: Long, seekTime: Long) -> Unit
lateinit var onVideoEnd: () -> Unit lateinit var onVideoEnd: () -> Unit
@ -108,7 +108,7 @@ class VideoEventEmitter {
val naturalSize: WritableMap = aspectRatioToNaturalSize(videoWidth, videoHeight) val naturalSize: WritableMap = aspectRatioToNaturalSize(videoWidth, videoHeight)
putMap("naturalSize", naturalSize) putMap("naturalSize", naturalSize)
putString("trackId", trackId) trackId?.let { putString("trackId", it) }
putArray("videoTracks", videoTracksToArray(videoTracks)) putArray("videoTracks", videoTracksToArray(videoTracks))
putArray("audioTracks", audioTracksToArray(audioTracks)) putArray("audioTracks", audioTracksToArray(audioTracks))
putArray("textTracks", textTracksToArray(textTracks)) putArray("textTracks", textTracksToArray(textTracks))
@ -153,9 +153,13 @@ class VideoEventEmitter {
onVideoBandwidthUpdate = { bitRateEstimate, height, width, trackId -> onVideoBandwidthUpdate = { bitRateEstimate, height, width, trackId ->
event.dispatch(EventTypes.EVENT_BANDWIDTH) { event.dispatch(EventTypes.EVENT_BANDWIDTH) {
putDouble("bitrate", bitRateEstimate.toDouble()) putDouble("bitrate", bitRateEstimate.toDouble())
if (width > 0) {
putInt("width", width) putInt("width", width)
}
if (height > 0) {
putInt("height", height) putInt("height", height)
putString("trackId", trackId) }
trackId?.let { putString("trackId", it) }
} }
} }
onVideoPlaybackStateChanged = { isPlaying, isSeeking -> onVideoPlaybackStateChanged = { isPlaying, isSeeking ->
@ -336,15 +340,19 @@ class VideoEventEmitter {
private fun aspectRatioToNaturalSize(videoWidth: Int, videoHeight: Int): WritableMap = private fun aspectRatioToNaturalSize(videoWidth: Int, videoHeight: Int): WritableMap =
Arguments.createMap().apply { Arguments.createMap().apply {
if (videoWidth > 0) {
putInt("width", videoWidth) putInt("width", videoWidth)
putInt("height", videoHeight)
val orientation = if (videoWidth > videoHeight) {
"landscape"
} else if (videoWidth < videoHeight) {
"portrait"
} else {
"square"
} }
if (videoHeight > 0) {
putInt("height", videoHeight)
}
val orientation = when {
videoWidth > videoHeight -> "landscape"
videoWidth < videoHeight -> "portrait"
else -> "square"
}
putString("orientation", orientation) putString("orientation", orientation)
} }
} }

View File

@ -139,7 +139,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -385,12 +384,13 @@ public class ReactExoplayerView extends FrameLayout implements
public void onBandwidthSample(int elapsedMs, long bytes, long bitrate) { public void onBandwidthSample(int elapsedMs, long bytes, long bitrate) {
if (mReportBandwidth) { if (mReportBandwidth) {
if (player == null) { if (player == null) {
eventEmitter.onVideoBandwidthUpdate.invoke(bitrate, 0, 0, "-1"); eventEmitter.onVideoBandwidthUpdate.invoke(bitrate, 0, 0, null);
} else { } else {
Format videoFormat = player.getVideoFormat(); Format videoFormat = player.getVideoFormat();
int width = videoFormat != null ? videoFormat.width : 0; boolean isRotatedContent = videoFormat != null && (videoFormat.rotationDegrees == 90 || videoFormat.rotationDegrees == 270);
int height = videoFormat != null ? videoFormat.height : 0; int width = videoFormat != null ? (isRotatedContent ? videoFormat.height : videoFormat.width) : 0;
String trackId = videoFormat != null ? videoFormat.id : "-1"; int height = videoFormat != null ? (isRotatedContent ? videoFormat.width : videoFormat.height) : 0;
String trackId = videoFormat != null ? videoFormat.id : null;
eventEmitter.onVideoBandwidthUpdate.invoke(bitrate, height, width, trackId); eventEmitter.onVideoBandwidthUpdate.invoke(bitrate, height, width, trackId);
} }
} }
@ -1427,7 +1427,7 @@ public class ReactExoplayerView extends FrameLayout implements
boolean isRotatedContent = videoFormat != null && (videoFormat.rotationDegrees == 90 || videoFormat.rotationDegrees == 270); boolean isRotatedContent = videoFormat != null && (videoFormat.rotationDegrees == 90 || videoFormat.rotationDegrees == 270);
int width = videoFormat != null ? (isRotatedContent ? videoFormat.height : videoFormat.width) : 0; int width = videoFormat != null ? (isRotatedContent ? videoFormat.height : videoFormat.width) : 0;
int height = videoFormat != null ? (isRotatedContent ? videoFormat.width : videoFormat.height) : 0; int height = videoFormat != null ? (isRotatedContent ? videoFormat.width : videoFormat.height) : 0;
String trackId = videoFormat != null ? videoFormat.id : "-1"; String trackId = videoFormat != null ? videoFormat.id : null;
// Properties that must be accessed on the main thread // Properties that must be accessed on the main thread
long duration = player.getDuration(); long duration = player.getDuration();

View File

@ -222,13 +222,14 @@ Callback function that is called when the media is loaded and ready to play.
Payload: Payload:
| Property | Type | Description | | Property | Type | Description |
| ----------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | |-------------|--------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| currentTime | number | Time in seconds where the media will start | | currentTime | number | Time in seconds where the media will start |
| duration | number | Length of the media in seconds | | duration | number | Length of the media in seconds |
| naturalSize | object | Properties:<br/> _ width - Width in pixels that the video was encoded at<br/> _ height - Height in pixels that the video was encoded at<br/> \* orientation - "portrait", "landscape" or "square" | | naturalSize | object | Properties:<br/> _ width - Width in pixels that the video was encoded at<br/> _ height - Height in pixels that the video was encoded at<br/> \* orientation - "portrait", "landscape" or "square" |
| audioTracks | array | An array of audio track info objects with the following properties:<br/> _ index - Index number<br/> _ title - Description of the track<br/> _ language - 2 letter [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) or 3 letter [ISO639-2](https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes) language code<br/> _ type - Mime type of track | | audioTracks | array | An array of audio track info objects with the following properties:<br/> _ index - Index number<br/> _ title - Description of the track<br/> _ language - 2 letter [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) or 3 letter [ISO639-2](https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes) language code<br/> _ type - Mime type of track |
| textTracks | array | An array of text track info objects with the following properties:<br/> _ index - Index number<br/> _ title - Description of the track<br/> _ language - 2 letter [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) or 3 letter [ISO 639-2](https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes) language code<br/> _ type - Mime type of track | | textTracks | array | An array of text track info objects with the following properties:<br/> _ index - Index number<br/> _ title - Description of the track<br/> _ language - 2 letter [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) or 3 letter [ISO 639-2](https://en.wikipedia.org/wiki/List_of_ISO_639-2_codes) language code<br/> _ type - Mime type of track |
| videoTracks | array | An array of video track info objects with the following properties:<br/> _ trackId - ID for the track<br/> _ bitrate - Bit rate in bits per second<br/> _ codecs - Comma separated list of codecs<br/> _ height - Height of the video<br/> \* width - Width of the video | | videoTracks | array | An array of video track info objects with the following properties:<br/> _ trackId - ID for the track<br/> _ bitrate - Bit rate in bits per second<br/> _ codecs - Comma separated list of codecs<br/> _ height - Height of the video<br/> \* width - Width of the video |
| trackId | string | Provide key information about the video track, typically including: `Resolution`, `Bitrate`. |
Example: Example:
@ -260,7 +261,8 @@ Example:
{ index: 0, bitrate: 3987904, codecs: "avc1.640028", height: 720, trackId: "f1-v1-x3", width: 1280 }, { index: 0, bitrate: 3987904, codecs: "avc1.640028", height: 720, trackId: "f1-v1-x3", width: 1280 },
{ index: 1, bitrate: 7981888, codecs: "avc1.640028", height: 1080, trackId: "f2-v1-x3", width: 1920 }, { index: 1, bitrate: 7981888, codecs: "avc1.640028", height: 1080, trackId: "f2-v1-x3", width: 1920 },
{ index: 2, bitrate: 1994979, codecs: "avc1.4d401f", height: 480, trackId: "f3-v1-x3", width: 848 } { index: 2, bitrate: 1994979, codecs: "avc1.4d401f", height: 480, trackId: "f3-v1-x3", width: 848 }
] ],
trackId: "720p 2400kbps"
} }
``` ```