Skip to content

Commit

Permalink
Uplift of #26704 (squashed) to release
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Nov 25, 2024
1 parent 3db2f02 commit a8fd105
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 87 deletions.
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 @@ -265,31 +252,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

0 comments on commit a8fd105

Please sign in to comment.