-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
275 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// AVPIPKitRenderer.swift | ||
// PIPKit | ||
// | ||
// Created by Kofktu on 2022/01/08. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import UIKit | ||
|
||
@available(iOS 15.0, *) | ||
public protocol AVPIPKitRenderer { | ||
|
||
var policy: AVPIPKitRenderPolicy { get } | ||
var renderPublisher: AnyPublisher<UIImage, Never> { get } | ||
|
||
func start() | ||
func stop() | ||
|
||
} | ||
|
||
@available(iOS 15.0, *) | ||
final class AVPIPUIKitRenderer: AVPIPKitRenderer { | ||
|
||
let policy: AVPIPKitRenderPolicy | ||
var renderPublisher: AnyPublisher<UIImage, Never> { | ||
render.eraseToAnyPublisher() | ||
} | ||
|
||
private var isRunning: Bool = false | ||
private weak var targetView: UIView? | ||
private var displayLink: CADisplayLink? | ||
private let render = PassthroughSubject<UIImage, Never>() | ||
|
||
deinit { | ||
stop() | ||
} | ||
|
||
init(targetView: UIView, policy: AVPIPKitRenderPolicy) { | ||
self.targetView = targetView | ||
self.policy = policy | ||
} | ||
|
||
func start() { | ||
if isRunning { | ||
return | ||
} | ||
|
||
isRunning = true | ||
onRender() | ||
|
||
guard case .preferredFramesPerSecond(let preferredFramesPerSecond) = policy else { | ||
return | ||
} | ||
|
||
displayLink = CADisplayLink(target: self, selector: #selector(onRender)) | ||
displayLink?.preferredFrameRateRange = CAFrameRateRange(minimum: 1, maximum: Float(preferredFramesPerSecond), preferred: 0) | ||
displayLink?.add(to: .main, forMode: .default) | ||
} | ||
|
||
func stop() { | ||
guard isRunning else { | ||
return | ||
} | ||
|
||
displayLink?.invalidate() | ||
displayLink = nil | ||
isRunning = false | ||
} | ||
|
||
// MARK: - Private | ||
@objc private func onRender() { | ||
guard let targetView = targetView else { | ||
stop() | ||
return | ||
} | ||
|
||
render.send(targetView.uiImage) | ||
} | ||
|
||
} | ||
|
||
@available(iOS 15.0, *) | ||
private extension UIView { | ||
|
||
var uiImage: UIImage { | ||
UIGraphicsImageRenderer(bounds: bounds).image { context in | ||
layer.render(in: context.cgContext) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// AVPIPKitUsable+UIKit.swift | ||
// PIPKit | ||
// | ||
// Created by Kofktu on 2022/01/08. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
@available(iOS 15.0, *) | ||
public protocol AVPIPUIKitUsable: AVPIPKitUsable { | ||
|
||
var pipTargetView: UIView { get } | ||
var renderPolicy: AVPIPKitRenderPolicy { get } | ||
|
||
} | ||
|
||
@available(iOS 15.0, *) | ||
public extension AVPIPUIKitUsable { | ||
|
||
var renderPolicy: AVPIPKitRenderPolicy { | ||
.preferredFramesPerSecond(UIScreen.main.maximumFramesPerSecond) | ||
} | ||
|
||
var renderer: AVPIPKitRenderer { | ||
AVPIPUIKitRenderer(targetView: pipTargetView, policy: renderPolicy) | ||
} | ||
|
||
} | ||
|
||
@available(iOS 15.0, *) | ||
public extension AVPIPUIKitUsable where Self: UIViewController { | ||
|
||
var pipTargetView: UIView { view } | ||
|
||
func startPictureInPicture() { | ||
setupIfNeeded() | ||
videoController?.start() | ||
} | ||
|
||
func stopPictureInPicture() { | ||
assert(videoController != nil) | ||
videoController?.stop() | ||
} | ||
|
||
// MARK: - Private | ||
private func setupIfNeeded() { | ||
guard videoController == nil else { | ||
return | ||
} | ||
|
||
videoController = createVideoController() | ||
} | ||
|
||
} | ||
|
||
@available(iOS 15.0, *) | ||
public extension AVPIPUIKitUsable where Self: UIView { | ||
|
||
var pipTargetView: UIView { self } | ||
|
||
func startPictureInPicture() { | ||
setupIfNeeded() | ||
videoController?.start() | ||
} | ||
|
||
func stopPictureInPicture() { | ||
assert(videoController != nil) | ||
videoController?.stop() | ||
} | ||
|
||
// MARK: - Private | ||
private func setupIfNeeded() { | ||
guard videoController == nil else { | ||
return | ||
} | ||
|
||
videoController = createVideoController() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.