WebRTC 的画中画功能

问题描述 投票:0回答:1

Apple 从 14.0 版开始为 iOS 引入 画中画 功能,而该功能更早可用于 iPadOS。使用

AVFoundation
API 时,该功能可以正常工作。例如,

import AVFoundation

func viewDidLoad() {
    let playerController = AVPlayerViewController()
    playerController.allowsPictureInPicturePlayback = true
}

我正在寻找的是,是否有任何解决方案/API可以在iOS和iPadOS中实现WebRTC视频流的画中画,特别是

RTCVideoRenderer

ios objective-c swift webrtc picture-in-picture
1个回答
0
投票
import WebRTC
import AVKit

class ViewController: UIViewController, AVPictureInPictureControllerDelegate {

    var pipController: AVPictureInPictureController?
    var videoRenderer: RTCVideoRenderer! // Your WebRTC video renderer
    var playerLayer: AVPlayerLayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Create a view for your video rendering
        let videoView = UIView(frame: self.view.bounds)
        self.view.addSubview(videoView)

        // Setup WebRTC video rendering (this assumes you are using WebRTC's RTCVideoRenderer)
        setupWebRTCVideoStream(on: videoView)

        // Create the player layer to host the WebRTC video
        playerLayer = AVPlayerLayer(player: nil) // No AVPlayer, but we need this layer
        playerLayer.frame = videoView.bounds
        videoView.layer.addSublayer(playerLayer)

        // Enable Picture-in-Picture
        setupPictureInPicture(for: videoView)
    }

    func setupWebRTCVideoStream(on view: UIView) {
        // Setup your WebRTC video renderer here
        // For example, if you are using RTCMTLVideoView:
        let webRTCVideoView = RTCMTLVideoView(frame: view.bounds)
        webRTCVideoView.videoContentMode = .scaleAspectFill
        self.videoRenderer = webRTCVideoView
        view.addSubview(webRTCVideoView)
        
        // Attach your WebRTC video track to the renderer
        // Example:
        // videoTrack.add(webRTCVideoView)
    }

    func setupPictureInPicture(for view: UIView) {
        let pipContentSource = AVPictureInPictureController.ContentSource(playerLayer: playerLayer, videoGravity: .resizeAspect)
        pipController = AVPictureInPictureController(contentSource: pipContentSource)
        pipController?.delegate = self

        if AVPictureInPictureController.isPictureInPictureSupported() {
            // Start PiP
            pipController?.startPictureInPicture()
        }
    }

    // Implement delegate methods if needed
    func pictureInPictureControllerWillStartPictureInPicture(_ controller: AVPictureInPictureController) {
        // Handle PiP starting
    }

    func pictureInPictureControllerDidStopPictureInPicture(_ controller: AVPictureInPictureController) {
        // Handle PiP stopping
    }
}

WebRTC 中没有直接用于 iOS/iPadOS 中 PiP 的 API,但使用 AVPictureInPictureController 与 AVPlayerLayer 或自定义 UIView 可以启用此功能。

© www.soinside.com 2019 - 2024. All rights reserved.