From efb338ee2a87c11f8a0e83ec72c613e87b3e43cb Mon Sep 17 00:00:00 2001 From: Olivier Bouillet <62574056+freeboub@users.noreply.github.com> Date: Sat, 11 May 2024 18:57:59 +0200 Subject: [PATCH] chore(android): refactor side loaded text tracks (#3754) * perf: ensure we do not provide callback to native if no callback provided from app * chore: rework bufferConfig to make it more generic and reduce ReactExoplayerView code size * chore(android): refactor external text tracks management. Split parsing and tracks handling * chore: fix linter --- .../common/api/SideLoadedTextTrack.kt | 34 +++++++++++++++++++ .../common/api/SideLoadedTextTrackList.kt | 27 +++++++++++++++ .../exoplayer/ReactExoplayerView.java | 25 ++++++-------- .../exoplayer/ReactExoplayerViewManager.java | 4 ++- .../exoplayer/VideoPlaybackService.kt | 4 +-- 5 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 android/src/main/java/com/brentvatne/common/api/SideLoadedTextTrack.kt create mode 100644 android/src/main/java/com/brentvatne/common/api/SideLoadedTextTrackList.kt diff --git a/android/src/main/java/com/brentvatne/common/api/SideLoadedTextTrack.kt b/android/src/main/java/com/brentvatne/common/api/SideLoadedTextTrack.kt new file mode 100644 index 00000000..59408c78 --- /dev/null +++ b/android/src/main/java/com/brentvatne/common/api/SideLoadedTextTrack.kt @@ -0,0 +1,34 @@ +package com.brentvatne.common.api + +import android.net.Uri +import com.brentvatne.common.toolbox.ReactBridgeUtils +import com.facebook.react.bridge.ReadableMap + +/** + * Class representing a sideLoaded text track from application + * Do you use player import in this class + */ +class SideLoadedTextTrack { + var language: String? = null + var title: String? = null + var uri: Uri = Uri.EMPTY + var type: String? = null + companion object { + val SIDELOAD_TEXT_TRACK_LANGUAGE = "language" + val SIDELOAD_TEXT_TRACK_TITLE = "title" + val SIDELOAD_TEXT_TRACK_URI = "uri" + val SIDELOAD_TEXT_TRACK_TYPE = "type" + + fun parse(src: ReadableMap?): SideLoadedTextTrack { + val sideLoadedTextTrack = SideLoadedTextTrack() + if (src == null) { + return sideLoadedTextTrack + } + sideLoadedTextTrack.language = ReactBridgeUtils.safeGetString(src, SIDELOAD_TEXT_TRACK_LANGUAGE) + sideLoadedTextTrack.title = ReactBridgeUtils.safeGetString(src, SIDELOAD_TEXT_TRACK_TITLE, "") + sideLoadedTextTrack.uri = Uri.parse(ReactBridgeUtils.safeGetString(src, SIDELOAD_TEXT_TRACK_URI, "")) + sideLoadedTextTrack.type = ReactBridgeUtils.safeGetString(src, SIDELOAD_TEXT_TRACK_TYPE, "") + return sideLoadedTextTrack + } + } +} diff --git a/android/src/main/java/com/brentvatne/common/api/SideLoadedTextTrackList.kt b/android/src/main/java/com/brentvatne/common/api/SideLoadedTextTrackList.kt new file mode 100644 index 00000000..e3d519c4 --- /dev/null +++ b/android/src/main/java/com/brentvatne/common/api/SideLoadedTextTrackList.kt @@ -0,0 +1,27 @@ +package com.brentvatne.common.api + +import com.facebook.react.bridge.ReadableArray +import com.facebook.react.bridge.ReadableMap + +/** + * Class representing a list of sideLoaded text track from application + * Do you use player import in this class + */ + +class SideLoadedTextTrackList { + var tracks = ArrayList() + + companion object { + fun parse(src: ReadableArray?): SideLoadedTextTrackList? { + if (src == null) { + return null + } + var sideLoadedTextTrackList = SideLoadedTextTrackList() + for (i in 0 until src.size()) { + val textTrack: ReadableMap = src.getMap(i) + sideLoadedTextTrackList.tracks.add(SideLoadedTextTrack.parse(textTrack)) + } + return sideLoadedTextTrackList + } + } +} diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index 5ddc2bd8..39108e96 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -106,6 +106,8 @@ import androidx.media3.ui.LegacyPlayerControlView; import com.brentvatne.common.api.BufferConfig; import com.brentvatne.common.api.ResizeMode; +import com.brentvatne.common.api.SideLoadedTextTrack; +import com.brentvatne.common.api.SideLoadedTextTrackList; import com.brentvatne.common.api.SubtitleStyle; import com.brentvatne.common.api.TimedMetadata; import com.brentvatne.common.api.Track; @@ -117,8 +119,6 @@ import com.brentvatne.react.R; import com.brentvatne.receiver.AudioBecomingNoisyReceiver; import com.brentvatne.receiver.BecomingNoisyListener; import com.facebook.react.bridge.LifecycleEventListener; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.uimanager.ThemedReactContext; import com.google.ads.interactivemedia.v3.api.AdError; @@ -220,7 +220,7 @@ public class ReactExoplayerView extends FrameLayout implements private String videoTrackValue; private String textTrackType; private String textTrackValue; - private ReadableArray textTracks; + private SideLoadedTextTrackList textTracks; private boolean disableFocus; private boolean focusable = true; private boolean disableBuffering; @@ -995,17 +995,12 @@ public class ReactExoplayerView extends FrameLayout implements return textSources; } - for (int i = 0; i < textTracks.size(); ++i) { - ReadableMap textTrack = textTracks.getMap(i); - String language = textTrack.getString("language"); - String title = textTrack.hasKey("title") - ? textTrack.getString("title") : language + " " + i; - Uri uri = Uri.parse(textTrack.getString("uri")); - MediaSource textSource = buildTextSource(title, uri, textTrack.getString("type"), - language); - if (textSource != null) { - textSources.add(textSource); - } + for (SideLoadedTextTrack track : textTracks.getTracks()) { + MediaSource textSource = buildTextSource(track.getTitle(), + track.getUri(), + track.getType(), + track.getLanguage()); + textSources.add(textSource); } return textSources; } @@ -1718,7 +1713,7 @@ public class ReactExoplayerView extends FrameLayout implements } } - public void setTextTracks(ReadableArray textTracks) { + public void setTextTracks(SideLoadedTextTrackList textTracks) { this.textTracks = textTracks; reloadSource(); } diff --git a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java index 6b5b0d30..c39b3797 100644 --- a/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java +++ b/android/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java @@ -14,6 +14,7 @@ import androidx.media3.exoplayer.DefaultLoadControl; import com.brentvatne.common.api.BufferConfig; import com.brentvatne.common.api.ResizeMode; +import com.brentvatne.common.api.SideLoadedTextTrackList; import com.brentvatne.common.api.SubtitleStyle; import com.brentvatne.common.react.VideoEventEmitter; import com.brentvatne.common.toolbox.DebugLog; @@ -309,7 +310,8 @@ public class ReactExoplayerViewManager extends ViewGroupManager