Runt
2025-06-27 e21b1c797955a231f2bcf71818e0259fbb6aeba1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
//  LiveViewModel.swift
//  LiveProject
//
//  Created by 倪路朋 on 6/27/25.
//
import UIKit
import AVFoundation
 
class LiveViewModel{
    
    lazy var camera = CameraCapture()
    lazy var renderer = MetalRenderer()
    
    func newWindowAction(device:DeviceInfo,completion: @escaping (Bool) -> Void = {b in}){
        switch device.type{
        case StreamType.CAMERA:
            requestCameraPermission(mediaType: .video){ staus in
                if(staus){
                    self.camera.onFrame = { buffer in
                        self.renderer.updateFrame(pixelBuffer: buffer)
                        print("画面更新")
                    }
                    self.camera.start()
                    print("启动相机")
                }else{
                    
                }
                completion(staus)
            }
            break;
        default:
            break;
        }
    }
    
    
    func requestCameraPermission(mediaType: AVMediaType,completion: @escaping (Bool) -> Void) {
        let status = AVCaptureDevice.authorizationStatus(for: mediaType)
        switch status {
        case .authorized:
            completion(true)
        case .notDetermined:
            AVCaptureDevice.requestAccess(for: .video) { granted in
                DispatchQueue.main.async {
                    completion(granted)
                }
            }
        default:
            // denied / restricted
            completion(false)
        }
    }
}