Skip to content

Commit

Permalink
Support app sharing in screen capture (livekit#276)
Browse files Browse the repository at this point in the history
Hello everyone, my issue revolves around the inability to share app
content during screen sharing. I attempted to use
MacOSScreenCapturer.sources with the parameter includeCurrentApplication
set to true, but unfortunately, it did not yield the desired outcome.

In order to address this issue, I made the following pull request (PR)
changes. In these changes, I introduced the includeCurrentApplication
parameter to ScreenShareCaptureOptions and utilized it to exclude the
app filter in MacOSScreenCapturer. This modification enables the
retrieval of sources as demonstrated in the following code snippet:

```
_ = MacOSScreenCapturer.mainDisplaySource().then { displaySource in
    Task {
        let options = ScreenShareCaptureOptions(dimensions: .h720_43,
                                                fps: 5,
                                                includeCurrentApplication: true)
        
        let mainTrack = LocalVideoTrack.createMacOSScreenShareTrack(source: displaySource, options: options)
        
        Task { @mainactor in
            self.tracks = [mainTrack]
        }
        
        await withThrowingTaskGroup(of: Void.self) { group in
            group.addTask {
                try await mainTrack.start()
            }
        }
    }
}

```

This solution consistently works and provides a convenient resolution.
If there is a misunderstanding on my part, and the issue can be resolved
without these changes, please reach out to me and provide an
explanation.

Thank you in advance!
  • Loading branch information
creativecbr authored Nov 27, 2023
1 parent 08229eb commit 8bcfd9d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Sources/LiveKit/Track/Capturers/MacOSScreenCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ import Foundation
let content = displaySource.scContent as? SCShareableContent,
let nativeDisplay = displaySource.nativeType as? SCDisplay
{
let excludedApps = content.applications.filter { app in
let excludedApps = !self.options.includeCurrentApplication ? content.applications.filter { app in
Bundle.main.bundleIdentifier == app.bundleIdentifier
}
} : []

filter = SCContentFilter(display: nativeDisplay, excludingApplications: excludedApps, exceptingWindows: [])
} else {
throw TrackError.capturer(message: "Unable to resolve SCContentFilter")
Expand Down
12 changes: 9 additions & 3 deletions Sources/LiveKit/Types/Options/ScreenShareCaptureOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ public class ScreenShareCaptureOptions: NSObject, VideoCaptureOptions {

@objc
public let useBroadcastExtension: Bool

@objc
public let includeCurrentApplication: Bool

public init(dimensions: Dimensions = .h1080_169,
fps: Int = 15,
showCursor: Bool = true,
useBroadcastExtension: Bool = false)
{
useBroadcastExtension: Bool = false,
includeCurrentApplication: Bool = false) {
self.dimensions = dimensions
self.fps = fps
self.showCursor = showCursor
self.useBroadcastExtension = useBroadcastExtension
self.includeCurrentApplication = includeCurrentApplication
}

// MARK: - Equal
Expand All @@ -49,7 +53,8 @@ public class ScreenShareCaptureOptions: NSObject, VideoCaptureOptions {
return dimensions == other.dimensions &&
fps == other.fps &&
showCursor == other.showCursor &&
useBroadcastExtension == other.useBroadcastExtension
useBroadcastExtension == other.useBroadcastExtension &&
includeCurrentApplication == other.includeCurrentApplication
}

override public var hash: Int {
Expand All @@ -58,6 +63,7 @@ public class ScreenShareCaptureOptions: NSObject, VideoCaptureOptions {
hasher.combine(fps)
hasher.combine(showCursor)
hasher.combine(useBroadcastExtension)
hasher.combine(includeCurrentApplication)
return hasher.finalize()
}
}

0 comments on commit 8bcfd9d

Please sign in to comment.