Skip to content

Commit

Permalink
[iOS] Improve CPU usage of new playlist when backgrounded (uplift to …
Browse files Browse the repository at this point in the history
…1.74.x) (#26699)

Uplift of #26671 (squashed) to beta
  • Loading branch information
brave-builds authored Nov 22, 2024
1 parent 8d5c916 commit 09914f9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ios/brave-ios/Sources/PlaylistUI/MediaContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ extension MediaContentView {
}
}
}
.task(priority: .low) {
.task(id: model.isPlayerInForeground, priority: .low) {
if !model.isPlayerInForeground { return }
self.currentTime = model.currentTime
for await currentTime in model.currentTimeStream {
self.currentTime = currentTime
Expand Down
10 changes: 9 additions & 1 deletion ios/brave-ios/Sources/PlaylistUI/PlayerModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public final class PlayerModel: ObservableObject {
return .init { [weak self] continuation in
guard let self else { return }
let timeObserver = player.addCancellablePeriodicTimeObserver(
forInterval: 100,
forInterval: 150,
queue: .global()
) { [weak self] time in
guard let self,
Expand Down Expand Up @@ -369,6 +369,8 @@ public final class PlayerModel: ObservableObject {
}
}

@MainActor @Published public var isPlayerInForeground: Bool = true

// MARK: - Picture in Picture

private var pipController: AVPictureInPictureController?
Expand Down Expand Up @@ -780,6 +782,9 @@ public final class PlayerModel: ObservableObject {
queue: .main
) { [weak self] _ in
guard let self else { return }
MainActor.assumeIsolated {
self.isPlayerInForeground = false
}
if isPictureInPictureActive || !isPlaying {
return
}
Expand All @@ -793,6 +798,9 @@ public final class PlayerModel: ObservableObject {
queue: .main
) { [weak self] _ in
guard let self else { return }
MainActor.assumeIsolated {
self.isPlayerInForeground = true
}
if isPictureInPictureActive || playerLayer.player != nil {
return
}
Expand Down
10 changes: 8 additions & 2 deletions ios/brave-ios/Sources/PlaylistUI/PlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ struct PlayerView: View {
}
}

private var isVideoAmbianceBackgroundEnabled: Bool {
!isFullScreen && playerModel.isPlayerInForeground
&& !ProcessInfo.processInfo.isLowPowerModeEnabled
}

var body: some View {
VideoPlayerLayout(aspectRatio: isFullScreen ? nil : 16 / 9) {
VideoPlayer(playerLayer: playerModel.playerLayer)
}
.background {
if !isFullScreen {
if isVideoAmbianceBackgroundEnabled {
VideoAmbianceBackground(playerModel: playerModel)
.transition(.opacity.animation(.snappy))
.opacity(playerModel.isPlaying ? 1 : 0.5)
Expand Down Expand Up @@ -296,7 +301,8 @@ extension PlayerView {
.buttonStyle(.playbackControl)
.tint(Color(braveSystemName: .textPrimary))
.backgroundStyle(Color.white.opacity(0.2))
.task(priority: .low) {
.task(id: model.isPlayerInForeground, priority: .low) {
if !model.isPlayerInForeground { return }
self.currentTime = model.currentTime
for await currentTime in model.currentTimeStream {
self.currentTime = currentTime
Expand Down
2 changes: 1 addition & 1 deletion ios/brave-ios/Sources/PlaylistUI/PlaylistItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ struct LeoPlayingSoundView: View {
.onReceive(
Timer.publish(every: 0.3, on: .main, in: .default).autoconnect(),
perform: { _ in
if !isAnimating { return }
if !isAnimating || UIApplication.shared.applicationState == .background { return }
randomizeBarHeights()
}
)
Expand Down

0 comments on commit 09914f9

Please sign in to comment.