Skip to content

Commit

Permalink
apply swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Nov 3, 2023
1 parent 4b2df99 commit 4a026c3
Show file tree
Hide file tree
Showing 157 changed files with 2,703 additions and 3,106 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.7 # Xcode 14
2 changes: 2 additions & 0 deletions .swiftformat
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 */"
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ let package = Package(
name: "LiveKit",
platforms: [
.iOS(.v13),
.macOS(.v10_15)
.macOS(.v10_15),
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "LiveKit",
targets: ["LiveKit"]
)
),
],
dependencies: [
.package(name: "WebRTC", url: "https://github.com/livekit/webrtc-xcframework-static.git", .exact("114.5735.09")),
.package(name: "SwiftProtobuf", url: "https://github.com/apple/swift-protobuf.git", .upToNextMajor(from: "1.21.0")),
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.5.2"))
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.5.2")),
],
targets: [
.systemLibrary(name: "CHeaders"),
Expand All @@ -36,6 +36,6 @@ let package = Package(
.testTarget(
name: "LiveKitTests",
dependencies: ["LiveKit"]
)
),
]
)
149 changes: 77 additions & 72 deletions Sources/LiveKit/Broadcast/BroadcastScreenCapturer.swift
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
30 changes: 19 additions & 11 deletions Sources/LiveKit/Broadcast/BroadcastServerSocketConnection.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
//
// SocketConnection.swift
// Broadcast Extension
//
// Created by Alex-Dan Bumbu on 22/03/2021.
// Copyright © 2021 Atlassian Inc. All rights reserved.
//
/*
* 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
import Darwin
import Foundation

#if canImport(CHeaders)
import CHeaders
import CHeaders
#endif

class BroadcastServerSocketConnection: NSObject {
Expand Down Expand Up @@ -152,7 +160,7 @@ class BroadcastServerSocketConnection: NSObject {
CFStreamCreatePairWithSocket(kCFAllocatorDefault, clientSocket, &readStream, &writeStream)

inputStream = readStream?.takeRetainedValue()
inputStream?.delegate = self.streamDelegate
inputStream?.delegate = streamDelegate
inputStream?.setProperty(kCFBooleanTrue, forKey: Stream.PropertyKey(kCFStreamPropertyShouldCloseNativeSocket as String))

outputStream = writeStream?.takeRetainedValue()
Expand All @@ -174,7 +182,7 @@ class BroadcastServerSocketConnection: NSObject {

repeat {
isRunning = self?.shouldKeepRunning ?? false && RunLoop.current.run(mode: .default, before: .distantFuture)
} while (isRunning)
} while isRunning

logger.log(level: .debug, "streams stopped")
}
Expand Down
Loading

0 comments on commit 4a026c3

Please sign in to comment.