-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
341ba2a
commit 13b34ca
Showing
13 changed files
with
382 additions
and
130 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// PiPManager.swift | ||
// | ||
|
||
import AVFoundation | ||
import AVKit | ||
import Foundation | ||
import MillicastSDK | ||
import UIKit | ||
|
||
final class PiPManager: NSObject { | ||
static let shared: PiPManager = PiPManager() | ||
|
||
private override init() {} | ||
|
||
private(set) var pipController: AVPictureInPictureController? | ||
private(set) var pipView: MCSampleBufferVideoUIView? | ||
|
||
func set(pipView: MCSampleBufferVideoUIView, with targetView: UIView) { | ||
pipController?.stopPictureInPicture() | ||
|
||
guard AVPictureInPictureController.isPictureInPictureSupported() else { | ||
return | ||
} | ||
|
||
let pipVideoCallViewController = AVPictureInPictureVideoCallViewController() | ||
pipVideoCallViewController.view.addSubview(pipView) | ||
pipView.translatesAutoresizingMaskIntoConstraints = false | ||
|
||
NSLayoutConstraint.activate([ | ||
pipVideoCallViewController.view.topAnchor.constraint(equalTo: pipView.topAnchor), | ||
pipVideoCallViewController.view.leadingAnchor.constraint(equalTo: pipView.leadingAnchor), | ||
pipView.bottomAnchor.constraint(equalTo: pipVideoCallViewController.view.bottomAnchor), | ||
pipView.trailingAnchor.constraint(equalTo: pipVideoCallViewController.view.trailingAnchor) | ||
]) | ||
pipVideoCallViewController.preferredContentSize = targetView.frame.size | ||
|
||
let pipContentSource = AVPictureInPictureController.ContentSource( | ||
activeVideoCallSourceView: targetView, | ||
contentViewController: pipVideoCallViewController | ||
) | ||
|
||
let pipController = AVPictureInPictureController(contentSource: pipContentSource) | ||
pipController.canStartPictureInPictureAutomaticallyFromInline = true | ||
pipController.delegate = self | ||
|
||
NotificationCenter.default | ||
.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: .main) { [weak self] _ in | ||
self?.stopPiP() | ||
} | ||
|
||
self.pipView = pipView | ||
self.pipController = pipController | ||
} | ||
|
||
func stopPiP() { | ||
pipController?.stopPictureInPicture() | ||
} | ||
} | ||
|
||
extension PiPManager: AVPictureInPictureControllerDelegate { | ||
func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) { | ||
} | ||
|
||
func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) { | ||
} | ||
|
||
func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, failedToStartPictureInPictureWithError error: Error) { | ||
} | ||
} |
Oops, something went wrong.