| | |
| | | class LiveViewModel: ObservableObject { |
| | | @Published var pixelBuffer: CVPixelBuffer? |
| | | |
| | | let encoder = H264Encoder(width: 1080, height: 1920, fps: 30, bitrate: 1_000_000) |
| | | //let encoder = H264Encoder(width: 1080, height: 1920, fps: 30, bitrate: 1_000_000) |
| | | var frameIndex: Int64 = 0 |
| | | let encodeQueue = DispatchQueue(label: "encoder.queue") |
| | | //let encodeQueue = DispatchQueue(label: "encoder.queue") |
| | | |
| | | lazy var camera = CameraCapture() |
| | | var timestamp = Int(Date().timeIntervalSince1970 * 1000) |
| | | |
| | | func newWindowAction(device:DeviceInfo,completion: @escaping (Bool) -> Void = {b in}){ |
| | | switch device.type{ |
| | | func newWindowAction(minidata:MiniWindowData,completion: @escaping (Bool) -> Void = {b in}){ |
| | | switch minidata.streamType{ |
| | | case StreamType.CAMERA: |
| | | requestCameraPermission(mediaType: .video){ staus in |
| | | if(staus){ |
| | |
| | | print("Invalid pixel buffer size: \(width)x\(height)") |
| | | return |
| | | } |
| | | |
| | | if(minidata.size.width != CGFloat(width) || minidata.size.height != CGFloat(height)){ |
| | | minidata.size = CGSize(width:width,height:height); |
| | | } |
| | | self.frameIndex += 1 |
| | | let ts = Int(Date().timeIntervalSince1970 * 1000) |
| | | |
| | | self.timestamp = ts; |
| | | let cmTime = CMTimeMake(value: Int64(CACurrentMediaTime() * 1000), timescale: 1000); |
| | | self.encoder.encode(pixelBuffer: buffer, pts: cmTime) |
| | | //self.encoder.encode(pixelBuffer: buffer, pts: cmTime) |
| | | DispatchQueue.main.async { |
| | | self.pixelBuffer = buffer; |
| | | minidata.pixelBuffer = buffer; |
| | | |
| | | } |
| | | //print("画面更新") |
| | | } |
| | |
| | | self.camera.start() |
| | | } |
| | | print("启动相机") |
| | | self.encoder.onEncoded = { (data: Data, ctime: CMTime, isKey: Bool) in |
| | | /*self.encoder.onEncoded = { (data: Data, ctime: CMTime, isKey: Bool) in |
| | | let timestamp2 = Int(Date().timeIntervalSince1970 * 1000) |
| | | print("编码时间2 \(timestamp2 - self.timestamp)") |
| | | print("Encoded NALU size: \(data.count), key frame: \(isKey)") |
| | | |
| | | } |
| | | }*/ |
| | | }else{ |
| | | |
| | | } |
| | |
| | | } |
| | | break; |
| | | default: |
| | | completion(true) |
| | | break; |
| | | } |
| | | } |
| | | |
| | | func closeWindowAction(device:DeviceInfo){ |
| | | switch device.type{ |
| | | func closeWindowAction(miniData:MiniWindowData){ |
| | | switch miniData.streamType{ |
| | | case StreamType.CAMERA: |
| | | print("关闭相机") |
| | | camera.stop(); |
| | | break; |
| | | default: |
| | | break; |