swifthomekit

HomeKit Camera Streaming Not Working in Swift


I've tried building a simple HomeKit app to stream video from a HomeKit Camera and while it appears to be working - the stream is not showing on the View Controller. Any help appreciated.

@IBOutlet weak var liveStreamView: HMCameraView!
// var liveStreamView: HMCameraView?

    func startCameraStream(for accessory: HMAccessory) {
        // Ensure this is a camera accessory
        guard let cameraStreamControl = accessory.cameraProfiles?.first?.streamControl else
        { return }
        cameraStreamControl.delegate = self
        cameraStreamControl.startStream()
        let liveStreamView = HMCameraView()
        self.view.addSubview(liveStreamView)
        self.liveStreamView = liveStreamView
        
        self.liveStreamView.cameraSource = cameraStreamControl.cameraStream
        
        self.liveStreamView?.setNeedsDisplay()
        self.view.layoutIfNeeded()
    }
    
    
    
    extension ViewController: HMCameraStreamControlDelegate {
        func cameraStreamControlDidStartStream(_ cameraStreamControl: HMCameraStreamControl) {
            liveStreamView?.cameraSource = cameraStreamControl.cameraStream
        }
    }

Solution

  • So silly. I forgot to define the liveStreamView attributes including the frame. See below where I set the background color, alpha, tag, etc:

    func startCameraStream( for accessory: HMAccessory) {
        // Ensure this is a camera accessory
        guard let cameraStreamControl = accessory.cameraProfiles?.first?.streamControl else
        { return }
        cameraStreamControl.delegate = self
        cameraStreamControl.startStream ( )
        let liveStreamView = HMCameraView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
    
        liveStreamView.backgroundColor = .clear
        liveStreamView.alpha = 0.5
        liveStreamView.tag = 100
        liveStreamView.isUserInteractionEnabled = true
        
        self.view.addSubview(liveStreamView)
        self.liveStreamView = liveStreamView
    }