2022-05-19 07:29:25 -06:00
|
|
|
import AVFoundation
|
|
|
|
import MediaAccessibility
|
|
|
|
|
|
|
|
let RCTVideoUnset = -1
|
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
// MARK: - RCTPlayerOperations
|
|
|
|
|
2022-05-19 07:29:25 -06:00
|
|
|
/*!
|
|
|
|
* Collection of mutating functions
|
|
|
|
*/
|
|
|
|
enum RCTPlayerOperations {
|
2024-04-04 05:23:44 -06:00
|
|
|
static func setSideloadedText(player: AVPlayer?, textTracks: [TextTrack], criteria: SelectedTrackCriteria?) {
|
|
|
|
let type = criteria?.type
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
let trackCount: Int! = player?.currentItem?.tracks.count ?? 0
|
2023-08-12 04:18:47 -06:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
// The first few tracks will be audio & video track
|
|
|
|
var firstTextIndex = 0
|
|
|
|
for i in 0 ..< trackCount where player?.currentItem?.tracks[i].assetTrack?.hasMediaCharacteristic(.legible) ?? false {
|
|
|
|
firstTextIndex = i
|
|
|
|
break
|
|
|
|
}
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
var selectedTrackIndex: Int = RCTVideoUnset
|
|
|
|
|
|
|
|
if type == "disabled" {
|
|
|
|
// Select the last text index which is the disabled text track
|
|
|
|
selectedTrackIndex = trackCount - firstTextIndex
|
|
|
|
} else if type == "language" {
|
|
|
|
let selectedValue = criteria?.value as? String
|
|
|
|
for i in 0 ..< textTracks.count {
|
|
|
|
let currentTextTrack = textTracks[i]
|
|
|
|
if selectedValue == currentTextTrack.language {
|
|
|
|
selectedTrackIndex = i
|
|
|
|
break
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
}
|
|
|
|
} else if type == "title" {
|
|
|
|
let selectedValue = criteria?.value as? String
|
|
|
|
for i in 0 ..< textTracks.count {
|
|
|
|
let currentTextTrack = textTracks[i]
|
|
|
|
if selectedValue == currentTextTrack.title {
|
|
|
|
selectedTrackIndex = i
|
|
|
|
break
|
2024-01-06 12:06:53 -07:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
}
|
|
|
|
} else if type == "index" {
|
|
|
|
if let value = criteria?.value, let index = value as? Int {
|
|
|
|
if textTracks.count > index {
|
|
|
|
selectedTrackIndex = index
|
2024-01-06 12:06:53 -07:00
|
|
|
}
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
}
|
2023-08-12 04:18:47 -06:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
// in the situation that a selected text track is not available (eg. specifies a textTrack not available)
|
|
|
|
if (type != "disabled") && selectedTrackIndex == RCTVideoUnset {
|
|
|
|
let captioningMediaCharacteristics = MACaptionAppearanceCopyPreferredCaptioningMediaCharacteristics(.user)
|
|
|
|
let captionSettings = captioningMediaCharacteristics as? [AnyHashable]
|
|
|
|
if (captionSettings?.contains(AVMediaCharacteristic.transcribesSpokenDialogForAccessibility)) != nil {
|
|
|
|
selectedTrackIndex = 0 // If we can't find a match, use the first available track
|
|
|
|
let systemLanguage = NSLocale.preferredLanguages.first
|
|
|
|
for i in 0 ..< textTracks.count {
|
|
|
|
let currentTextTrack = textTracks[i]
|
|
|
|
if systemLanguage == currentTextTrack.language {
|
|
|
|
selectedTrackIndex = i
|
|
|
|
break
|
2024-01-06 12:06:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
}
|
2024-01-06 12:06:53 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
for i in firstTextIndex ..< trackCount {
|
|
|
|
var isEnabled = false
|
|
|
|
if selectedTrackIndex != RCTVideoUnset {
|
|
|
|
isEnabled = i == selectedTrackIndex + firstTextIndex
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
player?.currentItem?.tracks[i].isEnabled = isEnabled
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
}
|
2023-08-12 04:18:47 -06:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func setMediaSelectionTrackForCharacteristic(player: AVPlayer?, characteristic: AVMediaCharacteristic, criteria: SelectedTrackCriteria?) async {
|
2022-05-19 07:29:25 -06:00
|
|
|
let type = criteria?.type
|
2023-12-07 00:47:40 -07:00
|
|
|
var mediaOption: AVMediaSelectionOption!
|
2022-11-28 14:15:50 -07:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
guard let group = await RCTVideoAssetsUtils.getMediaSelectionGroup(asset: player?.currentItem?.asset, for: characteristic) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if type == "disabled" {
|
|
|
|
// Do nothing. We want to ensure option is nil
|
|
|
|
} else if (type == "language") || (type == "title") {
|
|
|
|
let value = criteria?.value as? String
|
|
|
|
for i in 0 ..< group.options.count {
|
|
|
|
let currentOption: AVMediaSelectionOption! = group.options[i]
|
|
|
|
var optionValue: String!
|
|
|
|
if type == "language" {
|
|
|
|
optionValue = currentOption.extendedLanguageTag
|
|
|
|
} else {
|
|
|
|
optionValue = currentOption.commonMetadata.map(\.value)[0] as? String
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
if value == optionValue {
|
|
|
|
mediaOption = currentOption
|
|
|
|
break
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
// } else if ([type isEqualToString:@"default"]) {
|
|
|
|
// option = group.defaultOption; */
|
|
|
|
} else if type == "index" {
|
|
|
|
if let value = criteria?.value, let index = value as? Int {
|
|
|
|
if group.options.count > index {
|
|
|
|
mediaOption = group.options[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // default. invalid type or "system"
|
|
|
|
await player?.currentItem?.selectMediaOptionAutomatically(in: group)
|
|
|
|
return
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
|
|
|
|
// If a match isn't found, option will be nil and text tracks will be disabled
|
|
|
|
await player?.currentItem?.select(mediaOption, in: group)
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
static func seek(player: AVPlayer, playerItem: AVPlayerItem, paused: Bool, seekTime: Float, seekTolerance: Float, completion: @escaping (Bool) -> Void) {
|
2023-12-07 00:47:40 -07:00
|
|
|
let timeScale = 1000
|
|
|
|
let cmSeekTime: CMTime = CMTimeMakeWithSeconds(Float64(seekTime), preferredTimescale: Int32(timeScale))
|
|
|
|
let current: CMTime = playerItem.currentTime()
|
|
|
|
let tolerance: CMTime = CMTimeMake(value: Int64(seekTolerance), timescale: Int32(timeScale))
|
2023-08-12 04:18:47 -06:00
|
|
|
|
2024-04-04 05:23:44 -06:00
|
|
|
guard CMTimeCompare(current, cmSeekTime) != 0 else {
|
|
|
|
// skip if there is no diff in current time and seek time
|
|
|
|
return
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-04-04 05:23:44 -06:00
|
|
|
|
|
|
|
if !paused { player.pause() }
|
|
|
|
|
|
|
|
player.seek(to: cmSeekTime, toleranceBefore: tolerance, toleranceAfter: tolerance, completionHandler: { (finished: Bool) in
|
|
|
|
completion(finished)
|
|
|
|
})
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2023-08-12 04:18:47 -06:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
static func configureAudio(ignoreSilentSwitch: String, mixWithOthers: String, audioOutput: String) {
|
|
|
|
let audioSession: AVAudioSession! = AVAudioSession.sharedInstance()
|
|
|
|
var category: AVAudioSession.Category?
|
|
|
|
var options: AVAudioSession.CategoryOptions?
|
2023-03-03 07:47:05 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
if ignoreSilentSwitch == "ignore" {
|
2023-10-09 14:27:29 -06:00
|
|
|
category = audioOutput == "earpiece" ? AVAudioSession.Category.playAndRecord : AVAudioSession.Category.playback
|
2023-12-07 00:47:40 -07:00
|
|
|
} else if ignoreSilentSwitch == "obey" {
|
2022-05-19 07:29:25 -06:00
|
|
|
category = AVAudioSession.Category.ambient
|
|
|
|
}
|
2023-03-03 07:47:05 -07:00
|
|
|
|
2023-12-07 00:47:40 -07:00
|
|
|
if mixWithOthers == "mix" {
|
2022-05-19 07:29:25 -06:00
|
|
|
options = .mixWithOthers
|
2023-12-07 00:47:40 -07:00
|
|
|
} else if mixWithOthers == "duck" {
|
2022-05-19 07:29:25 -06:00
|
|
|
options = .duckOthers
|
|
|
|
}
|
2023-03-03 07:47:05 -07:00
|
|
|
|
2024-01-04 12:16:23 -07:00
|
|
|
if let category, let options {
|
2022-05-19 07:29:25 -06:00
|
|
|
do {
|
2023-03-03 07:47:05 -07:00
|
|
|
try audioSession.setCategory(category, options: options)
|
2022-05-19 07:29:25 -06:00
|
|
|
} catch {
|
2023-03-03 07:47:05 -07:00
|
|
|
debugPrint("[RCTPlayerOperations] Problem setting up AVAudioSession category and options. Error: \(error).")
|
2023-08-12 04:18:47 -06:00
|
|
|
#if !os(tvOS)
|
2023-12-07 00:47:40 -07:00
|
|
|
// Handle specific set category and option combination error
|
|
|
|
// setCategory:AVAudioSessionCategoryPlayback withOptions:mixWithOthers || duckOthers
|
|
|
|
// Failed to set category, error: 'what' Error Domain=NSOSStatusErrorDomain
|
|
|
|
// https://developer.apple.com/forums/thread/714598
|
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
do {
|
|
|
|
debugPrint("[RCTPlayerOperations] Reseting AVAudioSession category to playAndRecord with defaultToSpeaker options.")
|
|
|
|
try audioSession.setCategory(
|
|
|
|
audioOutput == "earpiece" ? AVAudioSession.Category.playAndRecord : AVAudioSession.Category.playback,
|
|
|
|
options: AVAudioSession.CategoryOptions.defaultToSpeaker
|
|
|
|
)
|
|
|
|
} catch {
|
|
|
|
debugPrint("[RCTPlayerOperations] Reseting AVAudioSession category and options problem. Error: \(error).")
|
|
|
|
}
|
2023-03-03 07:47:05 -07:00
|
|
|
}
|
2023-08-12 04:18:47 -06:00
|
|
|
#endif
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-01-04 12:16:23 -07:00
|
|
|
} else if let category, options == nil {
|
2022-05-19 07:29:25 -06:00
|
|
|
do {
|
2023-03-03 07:47:05 -07:00
|
|
|
try audioSession.setCategory(category)
|
2022-05-19 07:29:25 -06:00
|
|
|
} catch {
|
2023-03-03 07:47:05 -07:00
|
|
|
debugPrint("[RCTPlayerOperations] Problem setting up AVAudioSession category. Error: \(error).")
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
2024-01-04 12:16:23 -07:00
|
|
|
} else if category == nil, let options {
|
2022-05-19 07:29:25 -06:00
|
|
|
do {
|
2023-03-03 07:47:05 -07:00
|
|
|
try audioSession.setCategory(audioSession.category, options: options)
|
2022-05-19 07:29:25 -06:00
|
|
|
} catch {
|
2023-03-03 07:47:05 -07:00
|
|
|
debugPrint("[RCTPlayerOperations] Problem setting up AVAudioSession options. Error: \(error).")
|
2022-05-19 07:29:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|