Skip to content

Commit

Permalink
Moblink and weak instead of unowned.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Nov 2, 2024
1 parent d869b33 commit 5ce74e7
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 68 deletions.
9 changes: 6 additions & 3 deletions Moblin Screen Recording/Shared/SampleBufferSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,15 @@ class VideoEncoder {
duration: .invalid,
frameProperties: nil,
infoFlagsOut: nil,
outputHandler: { [unowned self] status, _, sampleBuffer in
outputHandler: { [weak self] status, _, sampleBuffer in
guard let self else {
return
}
guard let sampleBuffer, status == noErr else {
return
}
formatDescription = sampleBuffer.formatDescription
delegate?.videoEncoderOutputSampleBuffer(sampleBuffer)
self.formatDescription = sampleBuffer.formatDescription
self.delegate?.videoEncoderOutputSampleBuffer(sampleBuffer)
}
)
}
Expand Down
30 changes: 15 additions & 15 deletions Moblin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@
033FD71B2CC622D80073C6EC /* PadelScoreboardEffect.swift in Sources */ = {isa = PBXBuildFile; fileRef = 033FD71A2CC622D40073C6EC /* PadelScoreboardEffect.swift */; };
033FD71E2CC8A4A50073C6EC /* WidgetScoreboardSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 033FD71D2CC8A4A50073C6EC /* WidgetScoreboardSettingsView.swift */; };
03403DF62AD05CA60079D8E4 /* WidgetBrowserSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03403DF52AD05CA60079D8E4 /* WidgetBrowserSettingsView.swift */; };
0342F1A12C627A70004DD3EA /* IrlClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0342F1A02C627A70004DD3EA /* IrlClient.swift */; };
0342F1A32C627A87004DD3EA /* IrlServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0342F1A22C627A87004DD3EA /* IrlServer.swift */; };
0342F1A52C627A8F004DD3EA /* IrlCommon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0342F1A42C627A8F004DD3EA /* IrlCommon.swift */; };
0342F1A12C627A70004DD3EA /* MoblinkClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0342F1A02C627A70004DD3EA /* MoblinkClient.swift */; };
0342F1A32C627A87004DD3EA /* MoblinkServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0342F1A22C627A87004DD3EA /* MoblinkServer.swift */; };
0342F1A52C627A8F004DD3EA /* MoblinkCommon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0342F1A42C627A8F004DD3EA /* MoblinkCommon.swift */; };
0346B5EC2B9AD1BF00999109 /* MoblinWatch-InfoPlist.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 0346B5EB2B9AD1BF00999109 /* MoblinWatch-InfoPlist.xcstrings */; };
0346B5EE2B9AD1BF00999109 /* InfoPlist.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 0346B5ED2B9AD1BF00999109 /* InfoPlist.xcstrings */; };
0346B5F02B9AD1BF00999109 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 0346B5EF2B9AD1BF00999109 /* Localizable.xcstrings */; };
Expand Down Expand Up @@ -602,9 +602,9 @@
033FD71A2CC622D40073C6EC /* PadelScoreboardEffect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PadelScoreboardEffect.swift; sourceTree = "<group>"; };
033FD71D2CC8A4A50073C6EC /* WidgetScoreboardSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetScoreboardSettingsView.swift; sourceTree = "<group>"; };
03403DF52AD05CA60079D8E4 /* WidgetBrowserSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetBrowserSettingsView.swift; sourceTree = "<group>"; };
0342F1A02C627A70004DD3EA /* IrlClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IrlClient.swift; sourceTree = "<group>"; };
0342F1A22C627A87004DD3EA /* IrlServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IrlServer.swift; sourceTree = "<group>"; };
0342F1A42C627A8F004DD3EA /* IrlCommon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IrlCommon.swift; sourceTree = "<group>"; };
0342F1A02C627A70004DD3EA /* MoblinkClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoblinkClient.swift; sourceTree = "<group>"; };
0342F1A22C627A87004DD3EA /* MoblinkServer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoblinkServer.swift; sourceTree = "<group>"; };
0342F1A42C627A8F004DD3EA /* MoblinkCommon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoblinkCommon.swift; sourceTree = "<group>"; };
0346B5EB2B9AD1BF00999109 /* MoblinWatch-InfoPlist.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = "MoblinWatch-InfoPlist.xcstrings"; sourceTree = "<group>"; };
0346B5ED2B9AD1BF00999109 /* InfoPlist.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = InfoPlist.xcstrings; sourceTree = "<group>"; };
0346B5EF2B9AD1BF00999109 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1401,15 +1401,15 @@
path = Browser;
sourceTree = "<group>";
};
0342F19F2C627A3D004DD3EA /* Irl */ = {
0342F19F2C627A3D004DD3EA /* Moblink */ = {
isa = PBXGroup;
children = (
0342F1A02C627A70004DD3EA /* IrlClient.swift */,
0342F1A42C627A8F004DD3EA /* IrlCommon.swift */,
0342F1A22C627A87004DD3EA /* IrlServer.swift */,
0342F1A02C627A70004DD3EA /* MoblinkClient.swift */,
0342F1A42C627A8F004DD3EA /* MoblinkCommon.swift */,
0342F1A22C627A87004DD3EA /* MoblinkServer.swift */,
03FB42322C68EE6D0009FB26 /* PROTOCOL.md */,
);
path = Irl;
path = Moblink;
sourceTree = "<group>";
};
0346B5F22B9AF4D300999109 /* RemoteControlAssistant */ = {
Expand Down Expand Up @@ -1753,7 +1753,7 @@
03BC115B2AE4BD6200C38FC4 /* Emotes */,
0359F80C2BD3DA4B005BFDA8 /* HaishinKit */,
0391D2012CB2FFFC008AB44B /* Intents */,
0342F19F2C627A3D004DD3EA /* Irl */,
0342F19F2C627A3D004DD3EA /* Moblink */,
03BDE2202B24364700DF193A /* OBS */,
030D487F2B35E4AF003B2014 /* Platforms */,
039ADE452B55C20800FE0F59 /* RemoteControl */,
Expand Down Expand Up @@ -2960,7 +2960,7 @@
03BA3B432C9019D500DEFA3E /* AtkinsonDithering.swift in Sources */,
0359F8AD2BD3DA61005BFDA8 /* AudioCodecRingBuffer.swift in Sources */,
03D3C0512B452D61006BAEAC /* StreamRecordingSettingsView.swift in Sources */,
0342F1A52C627A8F004DD3EA /* IrlCommon.swift in Sources */,
0342F1A52C627A8F004DD3EA /* MoblinkCommon.swift in Sources */,
0325D0B42BB29713007EE85B /* ChatUsernamesToIgnoreSettingsView.swift in Sources */,
03980C552C5E74A9000ED476 /* VoicesView.swift in Sources */,
0302F07F2B320B0500B06FF0 /* StreamWizardChatSettingsView.swift in Sources */,
Expand Down Expand Up @@ -3164,7 +3164,7 @@
035735822BC6303F00F4E87D /* WatchDisplaySettingsView.swift in Sources */,
03E95AC62ADAF58900DAE2DF /* ZoomSwitchToSettingsView.swift in Sources */,
0359F8C62BD3DA61005BFDA8 /* FLVAudioCodec.swift in Sources */,
0342F1A12C627A70004DD3EA /* IrlClient.swift in Sources */,
0342F1A12C627A70004DD3EA /* MoblinkClient.swift in Sources */,
03A983632A9A05D7008FF0DB /* SettingsView.swift in Sources */,
0359F8D42BD3DA61005BFDA8 /* PreviewView.swift in Sources */,
032B4ED42C34910A00204547 /* MediaPlayerControlsView.swift in Sources */,
Expand Down Expand Up @@ -3196,7 +3196,7 @@
031863C72C8C4BEA00CECA33 /* FaxReceiver.swift in Sources */,
0359F8CF2BD3DA61005BFDA8 /* FLVVideoPacketType.swift in Sources */,
034FA1BB2C151E3600695825 /* AdaptiveBitrateRistExperiment.swift in Sources */,
0342F1A32C627A87004DD3EA /* IrlServer.swift in Sources */,
0342F1A32C627A87004DD3EA /* MoblinkServer.swift in Sources */,
0332EF612B3CB1E300BC3C16 /* RtmpServerStreamSettingsView.swift in Sources */,
0359F8EC2BD3DA61005BFDA8 /* RTMPConnection.swift in Sources */,
03D2B0E02C11BD9D0051BFB5 /* SrtlaServerSettingsView.swift in Sources */,
Expand Down
47 changes: 27 additions & 20 deletions Moblin/HaishinKit/Codec/VideoCodec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ class VideoCodec {
imageBuffer,
presentationTimeStamp: presentationTimeStamp,
duration: duration
) { [unowned self] status, _, sampleBuffer in
) { [weak self] status, _, sampleBuffer in
guard let self else {
return
}
guard let sampleBuffer, status == noErr else {
logger
.info(
Expand All @@ -195,26 +198,30 @@ class VideoCodec {
if invalidateSession {
session = makeVideoDecompressionSession(self)
}
let err = session?.decodeFrame(sampleBuffer) { [
unowned self
] status, _, imageBuffer, presentationTimeStamp, duration in
guard let imageBuffer, status == noErr else {
logger.info("video: Failed to decode frame status \(status)")
return
}
guard let formatDescription = CMVideoFormatDescription.create(imageBuffer: imageBuffer) else {
return
}
guard let sampleBuffer = CMSampleBuffer.create(imageBuffer,
formatDescription,
duration,
presentationTimeStamp,
sampleBuffer.decodeTimeStamp)
else {
return
let err = session?
.decodeFrame(sampleBuffer) { [
weak self
] status, _, imageBuffer, presentationTimeStamp, duration in
guard let self else {
return
}
guard let imageBuffer, status == noErr else {
logger.info("video: Failed to decode frame status \(status)")
return
}
guard let formatDescription = CMVideoFormatDescription.create(imageBuffer: imageBuffer) else {
return
}
guard let sampleBuffer = CMSampleBuffer.create(imageBuffer,
formatDescription,
duration,
presentationTimeStamp,
sampleBuffer.decodeTimeStamp)
else {
return
}
delegate?.videoCodecOutputSampleBuffer(self, sampleBuffer)
}
delegate?.videoCodecOutputSampleBuffer(self, sampleBuffer)
}
if err == kVTInvalidSessionErr {
logger.info("video: Decode failed. Resetting session.")
invalidateSession = true
Expand Down
4 changes: 2 additions & 2 deletions Moblin/HaishinKit/IRL/IrlStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import AVFoundation
import Foundation

class IrlStream: NetStream {
var client: IrlClient?
var client: MoblinkClient?

override init() {
client = IrlClient()
client = MoblinkClient()
super.init()
}

Expand Down
24 changes: 12 additions & 12 deletions Moblin/Irl/IrlClient.swift → Moblin/Moblink/MoblinkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@ import AVFAudio
import CoreMedia
import Foundation

private let lockQueue = DispatchQueue(label: "com.eerimoq.moblin.irl", qos: .userInteractive)
private let lockQueue = DispatchQueue(label: "com.eerimoq.moblin.moblink", qos: .userInteractive)

// periphery:ignore
struct IrlClientStats {
struct MoblinkClientStats {
let rtt: Double
let packetsInFlight: Double
}

// periphery:ignore
class IrlClient {
class MoblinkClient {
init() {}

func start() {
lockQueue.async {
logger.info("irl: client: Should start")
logger.info("moblink: client: Should start")
}
}

func stop() {
lockQueue.async {
logger.info("irl: client: Should stop")
logger.info("moblink: client: Should stop")
}
}

func writeVideo(sampleBuffer: CMSampleBuffer) {
lockQueue.async {
logger.info("irl: client: Should write video \(sampleBuffer.presentationTimeStamp.seconds)")
logger.info("moblink: client: Should write video \(sampleBuffer.presentationTimeStamp.seconds)")
}
}

func writeAudio(buffer _: AVAudioBuffer, presentationTimeStamp: CMTime) {
lockQueue.async {
logger.info("irl: client: Should write audio \(presentationTimeStamp.seconds)")
logger.info("moblink: client: Should write audio \(presentationTimeStamp.seconds)")
}
}

func writeVideoFormat(formatDescription: CMFormatDescription) {
lockQueue.async {
logger.info("irl: client: Should write video format \(formatDescription)")
logger.info("moblink: client: Should write video format \(formatDescription)")
}
}

func writeAudioFormat(audioFormat: AVAudioFormat) {
lockQueue.async {
logger.info("irl: client: Should write audio format \(audioFormat)")
logger.info("moblink: client: Should write audio format \(audioFormat)")
}
}

func getStats() -> IrlClientStats {
logger.info("irl: client: Should get stats")
return IrlClientStats(rtt: 0, packetsInFlight: 0)
func getStats() -> MoblinkClientStats {
logger.info("moblink: client: Should get stats")
return MoblinkClientStats(rtt: 0, packetsInFlight: 0)
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

// periphery:ignore
class IrlServer {
class MoblinkServer {
init() {}

func start() {}
Expand Down
12 changes: 6 additions & 6 deletions Moblin/Irl/PROTOCOL.md → Moblin/Moblink/PROTOCOL.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# IRL protocol specification
# Moblink (MK) protocol specification

## Goals
- Bonding.
Expand All @@ -14,16 +14,16 @@
Format:

```
irl://<address>:<port>/<stream-id>
moblink://<address>:<port>/<stream-id>
```

Examples:

```
irl://foobar.org:5566/ce4e0960-af23-40d4-85aa-d5b3245a6c92
irl://kalle.com/1ecee59b-4200-4577-b1f0-b3bdaf6cb81b
irl://184.65.22.133/ac10433a-ae84-4df6-bcb8-218a9488d065
irl://192.168.0.53:5888/a6255ed8-fd3f-45bd-8e66-d144d7267e12
moblink://foobar.org:5566/ce4e0960-af23-40d4-85aa-d5b3245a6c92
moblink://kalle.com/1ecee59b-4200-4577-b1f0-b3bdaf6cb81b
moblink://184.65.22.133/ac10433a-ae84-4df6-bcb8-218a9488d065
moblink://192.168.0.53:5888/a6255ed8-fd3f-45bd-8e66-d144d7267e12
```

## Protocol
Expand Down
22 changes: 13 additions & 9 deletions Moblin/Srtla/Server/SrtServerClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ class SrtServerClient {
}
}
srt_close(clientSocket)
videoDecoder?.stopRunning()
videoDecoder = nil
videoCodecLockQueue.async {
self.videoDecoder?.stopRunning()
self.videoDecoder = nil
}
}

private func handleProgramAssociationTable(packet: MpegTsPacket) throws {
Expand Down Expand Up @@ -212,8 +214,8 @@ class SrtServerClient {
}

private func handleVideoSampleBuffer(_ sampleBuffer: CMSampleBuffer) {
videoCodecLockQueue.async {
self.videoDecoder?.decodeSampleBuffer(sampleBuffer)
videoCodecLockQueue.async { [self] in
videoDecoder?.decodeSampleBuffer(sampleBuffer)
}
}

Expand Down Expand Up @@ -250,11 +252,13 @@ class SrtServerClient {
}

private func handleVideoFormatDescription(_ formatDescription: CMFormatDescription) {
videoDecoder?.stopRunning()
videoDecoder = VideoCodec(lockQueue: videoCodecLockQueue)
videoDecoder?.formatDescription = formatDescription
videoDecoder?.delegate = self
videoDecoder?.startRunning()
videoCodecLockQueue.async { [self] in
videoDecoder?.stopRunning()
videoDecoder = VideoCodec(lockQueue: videoCodecLockQueue)
videoDecoder?.formatDescription = formatDescription
videoDecoder?.delegate = self
videoDecoder?.startRunning()
}
}

private func tryMakeSampleBuffer(packetId: UInt16,
Expand Down

0 comments on commit 5ce74e7

Please sign in to comment.