Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually stop the track when the broadcast extension socket is closed #520

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/LiveKit/Broadcast/BroadcastScreenCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class BroadcastScreenCapturer: BufferCapturer {
frameReader.didCapture = { pixelBuffer, rotation in
self.capture(pixelBuffer, rotation: rotation.toLKType())
}
frameReader.didEnd = { [weak self] in
Task {
try? await self?.stopCapture()
}
}
frameReader.startCapture(with: socketConnection)
self.frameReader = frameReader

Expand Down
2 changes: 2 additions & 0 deletions Sources/LiveKit/Broadcast/SocketConnectionFrameReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class SocketConnectionFrameReader: NSObject {

private var message: Message?
var didCapture: ((CVPixelBuffer, RTCVideoRotation) -> Void)?
var didEnd: (() -> Void)?

override init() {}

Expand Down Expand Up @@ -227,6 +228,7 @@ extension SocketConnectionFrameReader: StreamDelegate {
case .endEncountered:
logger.log(level: .debug, "server stream end encountered")
stopCapture()
didEnd?()
case .errorOccurred:
logger.log(level: .debug, "server stream error encountered: \(aStream.streamError?.localizedDescription ?? "")")
default:
Expand Down
14 changes: 14 additions & 0 deletions Sources/LiveKit/TrackPublications/LocalTrackPublication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ extension LocalTrackPublication: VideoCapturerDelegate {
}
}
}

public func capturer(_ capturer: VideoCapturer, didUpdate state: VideoCapturer.CapturerState) {
// Broadcasts can always be stopped from system UI that bypasses our normal disable & unpublish methods.
// This check ensures that when this happens the track gets unpublished as well.
if state == .stopped && capturer is BroadcastScreenCapturer {
Task {
guard let participant = try await self.requireParticipant() as? LocalParticipant else {
return
}

try await participant.unpublish(publication: self)
}
}
}
}

extension LocalTrackPublication {
Expand Down
Loading