Skip to content

Commit

Permalink
[av] Fix precision of the player position (expo#26018)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz authored Jan 11, 2024
1 parent 9e2d8f7 commit 6fc7e2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/expo-av/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 🐛 Bug fixes

- [iOS] Improve precision for syncing two videos and updating new video position when user sets tolerances to 0 ([#26018](https://github.com/expo/expo/pull/26018) by [@jpudysz](https://github.com/jpudysz))

### 💡 Others

## 13.10.2 - 2024-01-10
Expand Down
41 changes: 31 additions & 10 deletions packages/expo-av/ios/EXAV/EXAVPlayerData.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ - (void)_finishLoadingNewPlayer
self.currentPosition = self.player.currentTime;

self.player.currentItem.audioTimePitchAlgorithm = self.pitchCorrectionQuality;
self.player.volume = self.volume.floatValue;
self.player.volume = self.volume.doubleValue;
self.player.muted = self.isMuted;
[self _updateLooping:self.isLooping];

Expand All @@ -205,13 +205,34 @@ - (BOOL)_shouldPlayerPlay
return _shouldPlay && ![_rate isEqualToNumber:@(0)];
}

- (BOOL)_hasZeroTolerance:(NSDictionary *)parameters
{
if ([parameters objectForKey:EXAVPlayerDataStatusSeekMillisToleranceBeforeKeyPath] == nil) {
return NO;
}

NSNumber *seekMillisToleranceBefore = parameters[EXAVPlayerDataStatusSeekMillisToleranceBeforeKeyPath];

if (CMTimeCompare(CMTimeMakeWithSeconds(seekMillisToleranceBefore.doubleValue / 1000, NSEC_PER_SEC), kCMTimeZero) != 0) {
return NO;
}

if ([parameters objectForKey:EXAVPlayerDataStatusSeekMillisToleranceAfterKeyPath] == nil) {
return NO;
}

NSNumber *seekMillisToleranceAfter = parameters[EXAVPlayerDataStatusSeekMillisToleranceAfterKeyPath];

return CMTimeCompare(CMTimeMakeWithSeconds(seekMillisToleranceAfter.doubleValue / 1000, NSEC_PER_SEC), kCMTimeZero) == 0;
}

- (NSError *)_tryPlayPlayerWithRateAndMuteIfNecessary
{
if (_player && [self _shouldPlayerPlay]) {
NSError *error = [_exAV promoteAudioSessionIfNecessary];
if (!error) {
_player.muted = _isMuted;
_player.rate = [_rate floatValue];
_player.rate = [_rate doubleValue];
}
return error;
}
Expand Down Expand Up @@ -248,9 +269,9 @@ - (void)setStatus:(NSDictionary *)parameters
NSNumber *currentPositionMillis = parameters[EXAVPlayerDataStatusPositionMillisKeyPath];

// We only seek if the new position is different from _currentPosition by a whole number of milliseconds.
mustSeek = currentPositionMillis.longValue != [self _getRoundedMillisFromCMTime:_currentPosition].longValue;
mustSeek = [self _hasZeroTolerance:parameters] || currentPositionMillis.longValue != [self _getRoundedMillisFromCMTime:_currentPosition].longValue;
if (mustSeek) {
newPosition = CMTimeMakeWithSeconds(currentPositionMillis.floatValue / 1000, NSEC_PER_SEC);
newPosition = CMTimeMakeWithSeconds(currentPositionMillis.doubleValue / 1000, NSEC_PER_SEC);
}
}

Expand All @@ -261,13 +282,13 @@ - (void)setStatus:(NSDictionary *)parameters
// We need to set toleranceBefore only if we will seek
if (mustSeek && [parameters objectForKey:EXAVPlayerDataStatusSeekMillisToleranceBeforeKeyPath] != nil) {
NSNumber *seekMillisToleranceBefore = parameters[EXAVPlayerDataStatusSeekMillisToleranceBeforeKeyPath];
toleranceBefore = CMTimeMakeWithSeconds(seekMillisToleranceBefore.floatValue / 1000, NSEC_PER_SEC);
toleranceBefore = CMTimeMakeWithSeconds(seekMillisToleranceBefore.doubleValue / 1000, NSEC_PER_SEC);
}

// We need to set toleranceAfter only if we will seek
if (mustSeek && [parameters objectForKey:EXAVPlayerDataStatusSeekMillisToleranceAfterKeyPath] != nil) {
NSNumber *seekMillisToleranceAfter = parameters[EXAVPlayerDataStatusSeekMillisToleranceAfterKeyPath];
toleranceAfter = CMTimeMakeWithSeconds(seekMillisToleranceAfter.floatValue / 1000, NSEC_PER_SEC);
toleranceAfter = CMTimeMakeWithSeconds(seekMillisToleranceAfter.doubleValue / 1000, NSEC_PER_SEC);
}

if ([parameters objectForKey:EXAVPlayerDataStatusShouldPlayKeyPath] != nil) {
Expand Down Expand Up @@ -318,7 +339,7 @@ - (void)setStatus:(NSDictionary *)parameters
_player.currentItem.audioTimePitchAlgorithm = AVAudioTimePitchAlgorithmVarispeed;
}

_player.volume = _volume.floatValue;
_player.volume = _volume.doubleValue;

// Apply parameters necessary after seek.
EX_WEAKIFY(self);
Expand Down Expand Up @@ -385,7 +406,7 @@ - (BOOL)_isPlayerPlaying

- (NSNumber *)_getRoundedMillisFromCMTime:(CMTime)time
{
return CMTIME_IS_INVALID(time) || CMTIME_IS_INDEFINITE(time) ? nil : @((long) (CMTimeGetSeconds(time) * 1000));
return CMTIME_IS_INVALID(time) || CMTIME_IS_INDEFINITE(time) ? nil : @((long) round((CMTimeGetSeconds(time) * 1000)));
}

- (NSNumber *)_getClippedValueForValue:(NSNumber *)value withMin:(NSNumber *)min withMax:(NSNumber *)max
Expand Down Expand Up @@ -664,7 +685,7 @@ - (void)_updateTimeObserver

EX_WEAKIFY(self);

CMTime interval = CMTimeMakeWithSeconds(_progressUpdateIntervalMillis.floatValue / 1000.0, NSEC_PER_SEC);
CMTime interval = CMTimeMakeWithSeconds(_progressUpdateIntervalMillis.doubleValue / 1000.0, NSEC_PER_SEC);

void (^timeObserverBlock)(CMTime time) = ^(CMTime time) {
EX_ENSURE_STRONGIFY(self);
Expand Down Expand Up @@ -768,7 +789,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
strongSelf.replayResolve = nil;
}

int observedRate = strongSelf.observedRate.floatValue * 1000;
int observedRate = strongSelf.observedRate.doubleValue * 1000;
int currentRate = strongSelf.player.rate * 1000;

if (abs(observedRate - currentRate) > 1) {
Expand Down

0 comments on commit 6fc7e2f

Please sign in to comment.