-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b2df99
commit 4a026c3
Showing
157 changed files
with
2,703 additions
and
3,106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5.7 # Xcode 14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--exclude Sources/LiveKit/Protos | ||
--header "/*\n * Copyright {year} LiveKit\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 77 additions & 72 deletions
149
Sources/LiveKit/Broadcast/BroadcastScreenCapturer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,107 @@ | ||
// | ||
// BroadcastScreenCapturer.m | ||
// RCTWebRTC | ||
// | ||
// Created by Alex-Dan Bumbu on 06/01/2021. | ||
// | ||
/* | ||
* Copyright 2023 LiveKit | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Foundation | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
import UIKit | ||
#endif | ||
|
||
@_implementationOnly import WebRTC | ||
|
||
#if os(iOS) | ||
|
||
class BroadcastScreenCapturer: BufferCapturer { | ||
class BroadcastScreenCapturer: BufferCapturer { | ||
static let kRTCScreensharingSocketFD = "rtc_SSFD" | ||
static let kAppGroupIdentifierKey = "RTCAppGroupIdentifier" | ||
static let kRTCScreenSharingExtension = "RTCScreenSharingExtension" | ||
|
||
static let kRTCScreensharingSocketFD = "rtc_SSFD" | ||
static let kAppGroupIdentifierKey = "RTCAppGroupIdentifier" | ||
static let kRTCScreenSharingExtension = "RTCScreenSharingExtension" | ||
var frameReader: SocketConnectionFrameReader? | ||
|
||
var frameReader: SocketConnectionFrameReader? | ||
override func startCapture() async throws -> Bool { | ||
let didStart = try await super.startCapture() | ||
|
||
override func startCapture() async throws -> Bool { | ||
guard didStart else { return false } | ||
|
||
let didStart = try await super.startCapture() | ||
guard let identifier = lookUpAppGroupIdentifier(), | ||
let filePath = filePathForIdentifier(identifier) else { return false } | ||
|
||
guard didStart else { return false } | ||
let bounds = await UIScreen.main.bounds | ||
let width = bounds.size.width | ||
let height = bounds.size.height | ||
let screenDimension = Dimensions(width: Int32(width), height: Int32(height)) | ||
|
||
guard let identifier = lookUpAppGroupIdentifier(), | ||
let filePath = filePathForIdentifier(identifier) else { return false } | ||
// pre fill dimensions, so that we don't have to wait for the broadcast to start to get actual dimensions. | ||
// should be able to safely predict using actual screen dimensions. | ||
let targetDimensions = screenDimension | ||
.aspectFit(size: options.dimensions.max) | ||
.toEncodeSafeDimensions() | ||
|
||
let bounds = await UIScreen.main.bounds | ||
let width = bounds.size.width | ||
let height = bounds.size.height | ||
let screenDimension = Dimensions(width: Int32(width), height: Int32(height)) | ||
|
||
// pre fill dimensions, so that we don't have to wait for the broadcast to start to get actual dimensions. | ||
// should be able to safely predict using actual screen dimensions. | ||
let targetDimensions = screenDimension | ||
.aspectFit(size: self.options.dimensions.max) | ||
.toEncodeSafeDimensions() | ||
|
||
defer { self.dimensions = targetDimensions } | ||
let frameReader = SocketConnectionFrameReader() | ||
guard let socketConnection = BroadcastServerSocketConnection(filePath: filePath, streamDelegate: frameReader) | ||
else { return false } | ||
frameReader.didCapture = { pixelBuffer, rotation in | ||
self.capture(pixelBuffer, rotation: rotation.toLKType()) | ||
defer { self.dimensions = targetDimensions } | ||
let frameReader = SocketConnectionFrameReader() | ||
guard let socketConnection = BroadcastServerSocketConnection(filePath: filePath, streamDelegate: frameReader) | ||
else { return false } | ||
frameReader.didCapture = { pixelBuffer, rotation in | ||
self.capture(pixelBuffer, rotation: rotation.toLKType()) | ||
} | ||
frameReader.startCapture(with: socketConnection) | ||
self.frameReader = frameReader | ||
|
||
return true | ||
} | ||
frameReader.startCapture(with: socketConnection) | ||
self.frameReader = frameReader | ||
|
||
return true | ||
} | ||
|
||
override func stopCapture() async throws -> Bool { | ||
|
||
let didStop = try await super.stopCapture() | ||
override func stopCapture() async throws -> Bool { | ||
let didStop = try await super.stopCapture() | ||
|
||
// Already stopped | ||
guard didStop else { return false } | ||
// Already stopped | ||
guard didStop else { return false } | ||
|
||
self.frameReader?.stopCapture() | ||
self.frameReader = nil | ||
return true | ||
} | ||
frameReader?.stopCapture() | ||
frameReader = nil | ||
return true | ||
} | ||
|
||
private func lookUpAppGroupIdentifier() -> String? { | ||
Bundle.main.infoDictionary?[BroadcastScreenCapturer.kAppGroupIdentifierKey] as? String | ||
} | ||
private func lookUpAppGroupIdentifier() -> String? { | ||
Bundle.main.infoDictionary?[BroadcastScreenCapturer.kAppGroupIdentifierKey] as? String | ||
} | ||
|
||
private func filePathForIdentifier(_ identifier: String) -> String? { | ||
guard let sharedContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: identifier) | ||
else { return nil } | ||
private func filePathForIdentifier(_ identifier: String) -> String? { | ||
guard let sharedContainer = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: identifier) | ||
else { return nil } | ||
|
||
let filePath = sharedContainer.appendingPathComponent(BroadcastScreenCapturer.kRTCScreensharingSocketFD).path | ||
return filePath | ||
let filePath = sharedContainer.appendingPathComponent(BroadcastScreenCapturer.kRTCScreensharingSocketFD).path | ||
return filePath | ||
} | ||
} | ||
|
||
} | ||
|
||
extension LocalVideoTrack { | ||
/// Creates a track that captures screen capture from a broadcast upload extension | ||
public static func createBroadcastScreenCapturerTrack(name: String = Track.screenShareVideoName, | ||
source: VideoTrack.Source = .screenShareVideo, | ||
options: ScreenShareCaptureOptions = ScreenShareCaptureOptions()) -> LocalVideoTrack { | ||
let videoSource = Engine.createVideoSource(forScreenShare: true) | ||
let capturer = BroadcastScreenCapturer(delegate: videoSource, options: BufferCaptureOptions(from: options)) | ||
return LocalVideoTrack( | ||
name: name, | ||
source: source, | ||
capturer: capturer, | ||
videoSource: videoSource | ||
) | ||
public extension LocalVideoTrack { | ||
/// Creates a track that captures screen capture from a broadcast upload extension | ||
static func createBroadcastScreenCapturerTrack(name: String = Track.screenShareVideoName, | ||
source: VideoTrack.Source = .screenShareVideo, | ||
options: ScreenShareCaptureOptions = ScreenShareCaptureOptions()) -> LocalVideoTrack | ||
{ | ||
let videoSource = Engine.createVideoSource(forScreenShare: true) | ||
let capturer = BroadcastScreenCapturer(delegate: videoSource, options: BufferCaptureOptions(from: options)) | ||
return LocalVideoTrack( | ||
name: name, | ||
source: source, | ||
capturer: capturer, | ||
videoSource: videoSource | ||
) | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.