diff --git a/Scream/AppDelegate.swift b/Scream/AppDelegate.swift index 3635aee..8af2e89 100644 --- a/Scream/AppDelegate.swift +++ b/Scream/AppDelegate.swift @@ -13,6 +13,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { lazy var preview = CaptureVideoPreview(layer: sr.contentLayer) let button = NSButton(title: "Start Stream", target: self, action: #selector(Button)) var isRunning = false + static let controller = StreamWindowController() // let udpclient = UDPClientImplementation(port: 03067) // let udpserver = UDPServerImplementation(host: "localhost", port: 03067, initialMessage: "kys") @@ -42,6 +43,11 @@ class AppDelegate: NSObject, NSApplicationDelegate { preview.topAnchor.constraint(equalTo: window.contentView!.topAnchor).isActive = true preview.bottomAnchor.constraint(equalTo: window.contentView!.bottomAnchor).isActive = true + AppDelegate.controller.showWindow(nil) + +// controller.streamView.display(sampleBuffer: sampleBuffer) + + window.addChildWindow(AppDelegate.controller.window!, ordered: .above) } func applicationWillTerminate(_ aNotification: Notification) { } @@ -50,3 +56,82 @@ class AppDelegate: NSObject, NSApplicationDelegate { return true } } + + +import Cocoa +import AVFoundation +import VideoToolbox + +final class StreamWindowView: NSView { + + private let displayLayer = AVSampleBufferDisplayLayer() + + override init(frame frameRect: NSRect) { + super.init(frame: frameRect) + + wantsLayer = true + + displayLayer.videoGravity = .resizeAspect + displayLayer.frame = bounds + displayLayer.autoresizingMask = [ + .layerWidthSizable, + .layerHeightSizable + ] + + layer?.addSublayer(displayLayer) + } + + required init?(coder: NSCoder) { + fatalError() + } + + // Feed packets here. + func enqueue(packet: ScreamPacket) { + + // POC: + // Pretend packet.data already contains one complete CMSampleBuffer. + // + // Later this will become: + // packet -> NAL units -> CMBlockBuffer -> + // CMSampleBuffer -> displayLayer.enqueue() + + } + + // POC helper + func display(sampleBuffer: CMSampleBuffer) { + displayLayer.enqueue(sampleBuffer) + } + + func flush() { + displayLayer.flushAndRemoveImage() + } +} +import Cocoa + +final class StreamWindowController: NSWindowController { + + let streamView = StreamWindowView(frame: .zero) + + convenience init() { + + let window = NSWindow( + contentRect: NSRect(x: 100, y: 100, width: 1280, height: 720), + styleMask: [ + .titled, + .closable, + .miniaturizable, + .resizable + ], + backing: .buffered, + defer: false + ) + + self.init(window: window) + + window.title = "Remote Stream" + window.contentView = streamView + + // Hardcoded black background + streamView.layer?.backgroundColor = NSColor.black.cgColor + } +} diff --git a/Scream/CaptureEngine.swift b/Scream/CaptureEngine.swift index e7098a3..dbd56de 100644 --- a/Scream/CaptureEngine.swift +++ b/Scream/CaptureEngine.swift @@ -74,6 +74,7 @@ class CaptureEngine: NSObject { infoFlagsOut: nil ) { status, infoFlags, sampleBuffer in guard let sampleBuffer else { return } + AppDelegate.controller.streamView.display(sampleBuffer: sampleBuffer) let packet = ScreamPacket( timestamp: sampleBuffer.presentationTimeStamp.seconds, data: try! sampleBuffer.dataBuffer!.dataBytes(), @@ -84,6 +85,7 @@ class CaptureEngine: NSObject { let data = try! JSONEncoder().encode(packet) self.udpServer.send(data) print(packet) + AppDelegate.controller.streamView.enqueue(packet: packet) } // outputHandler: self.outputHandler) continuation.yield(frame)