Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Remove video ambiance decoration in new Playlist UI #26704

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions ios/brave-ios/Sources/PlaylistUI/PlayerModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -332,43 +332,6 @@ public final class PlayerModel: ObservableObject {
return item.presentationSize.width / item.presentationSize.height
}

/// A stream that yields downsampled thumbnails of the item currently playing.
var videoAmbianceImageStream: AsyncStream<UIImage> {
return .init { [weak self] continuation in
guard let self else { return }
let timeObserver = player.addCancellablePeriodicTimeObserver(
forInterval: 150,
queue: .global()
) { [weak self] time in
guard let self,
self.videoDecorationOutput.hasNewPixelBuffer(forItemTime: time),
let buffer = self.videoDecorationOutput.copyPixelBuffer(
forItemTime: time,
itemTimeForDisplay: nil
)
else {
DispatchQueue.main.async {
continuation.yield(.init())
}
return
}
let ciImage = CIImage(cvPixelBuffer: buffer)
.transformed(by: .init(scaleX: 0.1, y: 0.1), highQualityDownsample: false)
if let cgImage = CIContext().createCGImage(ciImage, from: ciImage.extent) {
let uiImage = UIImage(cgImage: cgImage)
DispatchQueue.main.async {
continuation.yield(uiImage)
}
}
}
// Should be tied to the View, but adding one extra killswitch
self.cancellables.insert(timeObserver)
continuation.onTermination = { _ in
timeObserver.cancel()
}
}
}

@MainActor @Published public var isPlayerInForeground: Bool = true

// MARK: - Picture in Picture
Expand Down Expand Up @@ -709,15 +672,11 @@ public final class PlayerModel: ObservableObject {
// MARK: -

private let player: AVPlayer = .init()
private let videoDecorationOutput = AVPlayerItemVideoOutput(pixelBufferAttributes: [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA
])
private var currentItem: AVPlayerItem? {
player.currentItem
}
nonisolated private func updateCurrentItem(_ item: AVPlayerItem?) async {
player.replaceCurrentItem(with: item)
item?.add(videoDecorationOutput)
await MainActor.run {
setupPlayerItemKeyPathObservation()
updateSystemPlayer()
Expand Down Expand Up @@ -804,15 +763,7 @@ public final class PlayerModel: ObservableObject {
if isPictureInPictureActive || playerLayer.player != nil {
return
}
// There is a bug in iOS that breaks restoring an AVPlayer to an AVPlayerLayer while its
// playing in the background, so we have to first pause it before restoring it and resume
// playback after.
let isPlayingDurationRestoration = isPlaying
pause()
playerLayer.player = player
if isPlayingDurationRestoration {
play()
}
}

cancellables.formUnion([
Expand Down
38 changes: 0 additions & 38 deletions ios/brave-ios/Sources/PlaylistUI/PlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,10 @@ 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 isVideoAmbianceBackgroundEnabled {
VideoAmbianceBackground(playerModel: playerModel)
.transition(.opacity.animation(.snappy))
.opacity(playerModel.isPlaying ? 1 : 0.5)
.animation(.default, value: playerModel.isPlaying)
}
}
.allowsHitTesting(false)
// For some reason this is required or the status bar breaks when touching anything on the
// screen on an iPad...
Expand Down Expand Up @@ -312,31 +299,6 @@ extension PlayerView {
}
}

struct VideoAmbianceBackground: View {
var playerModel: PlayerModel

@State private var videoAmbianceDecorationImage: UIImage?

var body: some View {
VStack {
if let videoAmbianceDecorationImage {
Image(uiImage: videoAmbianceDecorationImage)
.resizable()
.blur(radius: 30)
.id(videoAmbianceDecorationImage)
.transition(.opacity)
}
}
.task(priority: .medium) {
for await image in playerModel.videoAmbianceImageStream {
withAnimation {
videoAmbianceDecorationImage = image.size == .zero ? nil : image
}
}
}
}
}

struct CompactMediaScrubberLabel: View {
var currentTime: TimeInterval
var duration: PlayerModel.ItemDuration
Expand Down
Loading