diff --git a/submodules/TelegramCallsUI/Sources/Components/MediaStreamComponent.swift b/submodules/TelegramCallsUI/Sources/Components/MediaStreamComponent.swift index 2f35b76ba3..2348399df3 100644 --- a/submodules/TelegramCallsUI/Sources/Components/MediaStreamComponent.swift +++ b/submodules/TelegramCallsUI/Sources/Components/MediaStreamComponent.swift @@ -493,6 +493,9 @@ public final class MediaStreamComponent: CombinedComponent { let isPictureInPictureSupported: Bool + private(set) var isVisibleInHierarchy: Bool = false + private var isVisibleInHierarchyDisposable: Disposable? + private var scheduledDismissUITimer: SwiftSignalKit.Timer? init(call: PresentationGroupCallImpl) { @@ -543,11 +546,23 @@ public final class MediaStreamComponent: CombinedComponent { }) let _ = call.accountContext.engine.calls.getGroupCallStreamCredentials(peerId: call.peerId, revokePreviousCredentials: false).start() + + self.isVisibleInHierarchyDisposable = (call.accountContext.sharedContext.applicationBindings.applicationInForeground + |> deliverOnMainQueue).start(next: { [weak self] inForeground in + guard let strongSelf = self else { + return + } + if strongSelf.isVisibleInHierarchy != inForeground { + strongSelf.isVisibleInHierarchy = inForeground + strongSelf.updated(transition: .immediate) + } + }) } deinit { self.stateDisposable?.dispose() self.infoDisposable?.dispose() + self.isVisibleInHierarchyDisposable?.dispose() } func toggleDisplayUI() { @@ -617,6 +632,7 @@ public final class MediaStreamComponent: CombinedComponent { component: MediaStreamVideoComponent( call: context.component.call, hasVideo: context.state.hasVideo, + isVisible: environment.isVisible && context.state.isVisibleInHierarchy, activatePictureInPicture: activatePictureInPicture, bringBackControllerForPictureInPictureDeactivation: { [weak call] completed in guard let call = call else { diff --git a/submodules/TelegramCallsUI/Sources/Components/MediaStreamVideoComponent.swift b/submodules/TelegramCallsUI/Sources/Components/MediaStreamVideoComponent.swift index e5bc8d8446..c9eab446c2 100644 --- a/submodules/TelegramCallsUI/Sources/Components/MediaStreamVideoComponent.swift +++ b/submodules/TelegramCallsUI/Sources/Components/MediaStreamVideoComponent.swift @@ -8,12 +8,14 @@ import AVKit final class MediaStreamVideoComponent: Component { let call: PresentationGroupCallImpl let hasVideo: Bool + let isVisible: Bool let activatePictureInPicture: ActionSlot> let bringBackControllerForPictureInPictureDeactivation: (@escaping () -> Void) -> Void - init(call: PresentationGroupCallImpl, hasVideo: Bool, activatePictureInPicture: ActionSlot>, bringBackControllerForPictureInPictureDeactivation: @escaping (@escaping () -> Void) -> Void) { + init(call: PresentationGroupCallImpl, hasVideo: Bool, isVisible: Bool, activatePictureInPicture: ActionSlot>, bringBackControllerForPictureInPictureDeactivation: @escaping (@escaping () -> Void) -> Void) { self.call = call self.hasVideo = hasVideo + self.isVisible = isVisible self.activatePictureInPicture = activatePictureInPicture self.bringBackControllerForPictureInPictureDeactivation = bringBackControllerForPictureInPictureDeactivation } @@ -25,6 +27,9 @@ final class MediaStreamVideoComponent: Component { if lhs.hasVideo != rhs.hasVideo { return false } + if lhs.isVisible != rhs.isVisible { + return false + } return true } @@ -54,6 +59,8 @@ final class MediaStreamVideoComponent: Component { private var component: MediaStreamVideoComponent? private var hadVideo: Bool = false + private weak var state: State? + override init(frame: CGRect) { self.blurTintView = UIView() self.blurTintView.backgroundColor = UIColor(white: 0.0, alpha: 0.55) @@ -82,6 +89,8 @@ final class MediaStreamVideoComponent: Component { } func update(component: MediaStreamVideoComponent, availableSize: CGSize, state: State, transition: Transition) -> CGSize { + self.state = state + if component.hasVideo, self.videoView == nil { if let input = component.call.video(endpointId: "unified") { if let videoBlurView = self.videoRenderingContext.makeView(input: input, blur: true) { @@ -122,7 +131,15 @@ final class MediaStreamVideoComponent: Component { } if let videoView = self.videoView { - videoView.updateIsEnabled(true) + var isVideoVisible = component.isVisible + if let pictureInPictureController = self.pictureInPictureController { + if pictureInPictureController.isPictureInPictureActive { + isVideoVisible = true + } + } + + videoView.updateIsEnabled(isVideoVisible) + var aspect = videoView.getAspect() if aspect <= 0.01 { aspect = 3.0 / 4.0 @@ -134,7 +151,8 @@ final class MediaStreamVideoComponent: Component { transition.withAnimation(.none).setFrame(view: videoView, frame: CGRect(origin: CGPoint(x: floor((availableSize.width - videoSize.width) / 2.0), y: floor((availableSize.height - videoSize.height) / 2.0)), size: videoSize), completion: nil) if let videoBlurView = self.videoBlurView { - videoBlurView.updateIsEnabled(true) + videoBlurView.updateIsEnabled(component.isVisible) + transition.withAnimation(.none).setFrame(view: videoBlurView, frame: CGRect(origin: CGPoint(x: floor((availableSize.width - blurredVideoSize.width) / 2.0), y: floor((availableSize.height - blurredVideoSize.height) / 2.0)), size: blurredVideoSize), completion: nil) } } @@ -207,6 +225,14 @@ final class MediaStreamVideoComponent: Component { completionHandler(true) } } + + func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) { + self.state?.updated(transition: .immediate) + } + + func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) { + self.state?.updated(transition: .immediate) + } } public func makeView() -> View {