fix(ios): add text tracks only if we successfully insertTimeRage (#3557)

insertTimeRage can fail & if we add failed textTrack to our validTextTracks array, video can crash later on selectTextTrack
we also add en empty textTrack only we we have validTextTrack

related to https://github.com/react-native-video/react-native-video/issues/3480
This commit is contained in:
Gaëtan Kueny 2024-03-04 10:43:33 +01:00 committed by GitHub
parent c0aa3d6453
commit b73baad2c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -299,24 +299,33 @@ enum RCTVideoUtils {
if let textTracks { if let textTracks {
for i in 0 ..< tracks.count { for i in 0 ..< tracks.count {
guard let track = tracks[i]?.first else { continue } // fix when there's no textTrackAsset guard let track = tracks[i]?.first else { continue } // fix when there's no textTrackAsset
validTextTracks.append(textTracks[i])
let textCompTrack: AVMutableCompositionTrack! = mixComposition.addMutableTrack(withMediaType: AVMediaType.text, let textCompTrack: AVMutableCompositionTrack! = mixComposition.addMutableTrack(withMediaType: AVMediaType.text,
preferredTrackID: kCMPersistentTrackID_Invalid) preferredTrackID: kCMPersistentTrackID_Invalid)
try? textCompTrack.insertTimeRange(
do {
try textCompTrack.insertTimeRange(
CMTimeRangeMake(start: .zero, duration: videoAsset.timeRange.duration), CMTimeRangeMake(start: .zero, duration: videoAsset.timeRange.duration),
of: track, of: track,
at: .zero at: .zero
) )
validTextTracks.append(textTracks[i])
} catch {
// TODO: upgrade error by call some props callback to better inform user
print("Error occurred on textTrack insert attempt: \(error.localizedDescription)")
continue
}
} }
} }
return return
}.then { }.then {
if !validTextTracks.isEmpty {
let emptyVttFile: TextTrack? = self.createEmptyVttFile() let emptyVttFile: TextTrack? = self.createEmptyVttFile()
if emptyVttFile != nil { if emptyVttFile != nil {
validTextTracks.append(emptyVttFile!) validTextTracks.append(emptyVttFile!)
} }
}
fulfill(validTextTracks) fulfill(validTextTracks)
} }