Skip to content

Commit

Permalink
fix videos sync
Browse files Browse the repository at this point in the history
  • Loading branch information
plozinski committed Jul 23, 2024
1 parent bd157fc commit f847e73
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React

class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverHandler {
struct VideoState: OptionSet {
let rawValue: UInt
var rawValue: UInt

static let unknown = VideoState(rawValue: 0)
static let loaded = VideoState(rawValue: 1 << 0)
Expand Down Expand Up @@ -792,31 +792,33 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
return
}
let item: AVPlayerItem? = _player?.currentItem

_pendingSeek = true

guard item != nil, let player = _player, let item, item.status == AVPlayerItem.Status.readyToPlay else {
_pendingSeekTime = time.floatValue
return
}

RCTPlayerOperations.seek(
player: player,
playerItem: item,
paused: _paused,
seekTime: time.floatValue,
seekTolerance: tolerance.floatValue
) { [weak self] (_: Bool) in
guard let self else { return }

self._playerObserver.addTimeObserverIfNotSet()
self.setPaused(self._paused)
self.onVideoSeek?(["currentTime": NSNumber(value: Float(CMTimeGetSeconds(item.currentTime()))),
"seekTime": time,
"target": self.reactTag])
if (item != nil) && item?.status == .readyToPlay {

Check failure on line 796 in ios/Video/RCTVideo.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)

Check failure on line 796 in ios/Video/RCTVideo.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Vertical Whitespace after Opening Braces Violation: Don't include vertical whitespace (empty line) after opening braces (vertical_whitespace_opening_braces)
let cmSeekTime: CMTime = CMTimeMakeWithSeconds(Float64(time), preferredTimescale: 1000)
let current: CMTime = item?.currentTime() ?? .zero

Check failure on line 799 in ios/Video/RCTVideo.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
let tolerance: CMTime = CMTimeMake(value: Int64(tolerance), timescale: 1000)
let wasPaused = _paused

Check failure on line 802 in ios/Video/RCTVideo.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
if CMTimeCompare(current, cmSeekTime) != 0 {
if !wasPaused { _player?.pause() }
_player?.seek(to: cmSeekTime, toleranceBefore: tolerance, toleranceAfter: tolerance) { finished in

Check failure on line 805 in ios/Video/RCTVideo.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Unused Closure Parameter Violation: Unused parameter in a closure should be replaced with _ (unused_closure_parameter)
self._playerObserver.addTimeObserverIfNotSet()
if !wasPaused {
self.setPaused(false)
}
if (self.onVideoSeek != nil) {

Check failure on line 810 in ios/Video/RCTVideo.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Control Statement Violation: `if`, `for`, `guard`, `switch`, `while`, and `catch` statements shouldn't unnecessarily wrap their conditionals or arguments in parentheses (control_statement)
self.onVideoSeek?(["currentTime": NSNumber(value: Float(CMTimeGetSeconds(item?.currentTime() ?? .zero))),
"seekTime": time,
"target": self.reactTag])
}
}
_pendingSeek = false
}
} else {
_pendingSeek = true
_pendingSeekTime = Float(time)
}

_pendingSeek = false
}

@objc
Expand Down Expand Up @@ -1760,7 +1762,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
_player?.rate = 0.0
peripheralPlayer?.rate = 0.0
} else {
if !isVideoReady() || peripheral.isVideoReady() {
if !isVideoReady() || !peripheral.isVideoReady() {
_principalPendingPlayRequest = true
return
}
Expand Down Expand Up @@ -1818,6 +1820,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
peripheralPlayer?.pause()

let seekGroup: DispatchGroup = .init()

Check failure on line 1823 in ios/Video/RCTVideo.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
seekGroup.enter()
_player?.seek(to: cmSeekTime, toleranceBefore: tolerance, toleranceAfter: tolerance, completionHandler: { _ in
seekGroup.leave()
Expand Down Expand Up @@ -1860,7 +1863,8 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
if !self.isManaged() {
return
}
_videoState != state
_videoState = .init(rawValue: .init(_videoState.rawValue | state.rawValue))

Check failure on line 1867 in ios/Video/RCTVideo.swift

View workflow job for this annotation

GitHub Actions / Swift-Lint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
if self.isPeripheral() {
let principal = self.principal()
principal?.onPeripheralVideoStatusChange()
Expand All @@ -1869,7 +1873,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH

func onPeripheralVideoStatusChange() {
let peripheral = self.peripheral()
if peripheral != nil {
if peripheral == nil {
return
}
if peripheral?.isVideoReady() ?? false && _principalPendingPlayRequest {
Expand Down

0 comments on commit f847e73

Please sign in to comment.