uh did some stuff to make packets, now i gotta actually transmit

This commit is contained in:
neon443
2026-02-27 22:29:58 +00:00
parent 8eeca981d9
commit 5c589e1fb2
5 changed files with 45 additions and 12 deletions

View File

@@ -10,8 +10,8 @@ import Cocoa
@main
class AppDelegate: NSObject, NSApplicationDelegate {
let sr = ScreenRecorder()
let udpclient = UDPClientImplementation(port: 03067)
let udpserver = UDPServerImplementation(host: "localhost", port: 03067, initialMessage: "kys")
// let udpclient = UDPClientImplementation(port: 03067)
// let udpserver = UDPServerImplementation(host: "localhost", port: 03067, initialMessage: "kys")
@IBOutlet var window: NSWindow!
@@ -20,10 +20,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
await sr.start()
}
}
@IBAction func Button2(_ sender: Any) {
udpclient.start()
udpserver.start()
}
// @IBAction func Button2(_ sender: Any) {
// udpclient.start()
// udpserver.start()
// }
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
@@ -31,7 +31,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
preview.frame.size = window.contentView!.frame.size
window.contentView?.addSubview(preview)
Button(self)
Button2(self)
// Button2(self)
}
func applicationWillTerminate(_ aNotification: Notification) {

View File

@@ -31,6 +31,8 @@ class CaptureEngine: NSObject {
let audioSampleBufferQueue = DispatchQueue(label: "audioSampleBufferQueue")
let micSampleBufferQueue = DispatchQueue(label: "micSampleBufferQueue")
let udpServer = UDPServerImplementation(host: "localhost", port: 03067, initialMessage: nil)
private var continuation: AsyncThrowingStream<CapturedFrame, Error>.Continuation?
func startCapture(config: SCStreamConfiguration, filter: SCContentFilter) -> AsyncThrowingStream<CapturedFrame, Error> {
@@ -71,8 +73,17 @@ class CaptureEngine: NSObject {
frameProperties: nil,
infoFlagsOut: nil
) { status, infoFlags, sampleBuffer in
// print()
guard let sampleBuffer else { return }
let packet = ScreamPacket(
timestamp: sampleBuffer.presentationTimeStamp.seconds,
data: try! sampleBuffer.dataBuffer!.dataBytes(),
index: 1,
packetsInChunk: 1,
isKeyframe: true
)
let data = try! JSONEncoder().encode(packet)
self.udpServer.send(data)
print(packet)
}
// outputHandler: self.outputHandler)
continuation.yield(frame)
@@ -150,10 +161,10 @@ class CaptureEngine: NSObject {
err = VTSessionSetProperty(session, key: kVTCompressionPropertyKey_AverageBitRate, value: 10 as CFNumber)
if err != noErr { print("failed to set to framerte \(err)") }
err = VTSessionSetProperty(session, key: kVTCompressionPropertyKey_MaxKeyFrameInterval, value: 60 as CFNumber)
err = VTSessionSetProperty(session, key: kVTCompressionPropertyKey_MaxKeyFrameInterval, value: 0 as CFNumber)
if err != noErr { print("failed to set to keyframe interval \(err)") }
err = VTSessionSetProperty(session, key: kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, value: 1 as CFNumber)
err = VTSessionSetProperty(session, key: kVTCompressionPropertyKey_MaxKeyFrameIntervalDuration, value: 0 as CFNumber)
if err != noErr { print("failed to set to keyframe interval duratation \(err)") }
// err = VTSessionSetProperty(session, key: kVTCompressionPropertyKey_SuggestedLookAheadFrameCount, value: 1 as CFNumber)

View File

@@ -16,4 +16,12 @@ struct ScreamPacket: Codable, Identifiable {
var index: Int
var packetsInChunk: Int
var isKeyframe: Bool
init(timestamp: Double, data: Data, index: Int, packetsInChunk: Int, isKeyframe: Bool) {
self.id = timestamp
self.data = data
self.index = index
self.packetsInChunk = packetsInChunk
self.isKeyframe = isKeyframe
}
}

View File

@@ -9,6 +9,7 @@ import Foundation
import Network
final class UDPClientImplementation: Sendable {
// public static let shared: UDPClientImplementation = .init(port: 03067)
private let connectionListener: NWListener
//init a udp client at x port
@@ -26,6 +27,7 @@ final class UDPClientImplementation: Sendable {
connectionListener.stateUpdateHandler = { state in
print("state: \(state)")
}
start()
}
}

View File

@@ -15,9 +15,10 @@ protocol UDPServer {
}
final class UDPServerImplementation: Sendable {
// public static let shared: UDPServerImplementation = .init(host: "localhost", port: 03067, initialMessage: nil)
private let connection: NWConnection
init(host: String, port: UInt16, initialMessage: String) {
init(host: String, port: UInt16, initialMessage: String?) {
connection = NWConnection(
host: NWEndpoint.Host(host),
port: NWEndpoint.Port(integerLiteral: port),
@@ -26,6 +27,7 @@ final class UDPServerImplementation: Sendable {
connection.stateUpdateHandler = { [weak self] state in
print("server state is \(state)")
if state == .ready {
guard let initialMessage else { return }
self?.send(message: initialMessage)
}
}
@@ -43,6 +45,16 @@ extension UDPServerImplementation: UDPServer {
print("server stopped")
}
func send(_ data: Data) {
connection.send(content: data, completion: .contentProcessed({ error in
if let error {
print("server: fialed to send \(error)")
} else {
print("server: message sent")
}
}))
}
func send(message: String) {
guard let data = message.data(using: .utf8) else {
print("server: encode message failed")