Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Add auto formatter for .swift
Browse files Browse the repository at this point in the history
  • Loading branch information
incubo4u committed Dec 21, 2023
1 parent 5eceb72 commit 71ba4b2
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 80 deletions.
16 changes: 8 additions & 8 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

git diff --diff-filter=d --staged --name-only | grep -e '\(.*\).swift$' | while read line; do
swift-format format -i "${line}" --configuration swift-format-config.json;
git add "$line";
done

git diff --diff-filter=d --staged --name-only | grep -e '\(.*\).swift$' | while read line; do
swift-format lint -s "${line}" --configuration swift-format-config.json || exit 1;
done
swift-format --version 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
git diff --diff-filter=d --staged --name-only | grep -e '\(.*\).swift$' | while read line; do
echo "${line}"
swift-format -i "${line}"
git add "$line"
done
fi
28 changes: 19 additions & 9 deletions MembraneRTCTest/PeerConnectionManagerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class PeerConnectionManagerTest: XCTestCase {
self.test = test
super.init(encoder: encoder)
}
override func createPeerConnection(_ configuration: RTCConfiguration, constraints: RTCMediaConstraints)
override func createPeerConnection(
_ configuration: RTCConfiguration, constraints: RTCMediaConstraints
)
-> RTCPeerConnection?
{
test.peerConnection = super.createPeerConnection(configuration, constraints: constraints)
Expand All @@ -33,11 +35,13 @@ class PeerConnectionManagerTest: XCTestCase {

let listener = ListenerImpl()
manager = PeerConnectionManager(
config: config, peerConnectionFactory: peerConnectionFactory, peerConnectionListener: listener)
config: config, peerConnectionFactory: peerConnectionFactory, peerConnectionListener: listener
)

let expectation = XCTestExpectation(description: "Create sdp offer.")

manager.getSdpOffer(integratedTurnServers: [], tracksTypes: [:], localTracks: []) { sdp, midToTrackId, error in
manager.getSdpOffer(integratedTurnServers: [], tracksTypes: [:], localTracks: []) {
sdp, midToTrackId, error in
XCTAssertNotNil(sdp, "Sdp offer wasn't created")
expectation.fulfill()
}
Expand All @@ -56,7 +60,8 @@ class PeerConnectionManagerTest: XCTestCase {

func testAddVideoTrack() throws {
let videoTrack = LocalVideoTrack.create(
for: .camera, videoParameters: .presetHD43, peerConnectionFactoryWrapper: peerConnectionFactory)
for: .camera, videoParameters: .presetHD43,
peerConnectionFactoryWrapper: peerConnectionFactory)

manager.addTrack(track: videoTrack, localStreamId: "id")
XCTAssertFalse(peerConnection.transceivers.isEmpty, "No track added")
Expand All @@ -65,9 +70,11 @@ class PeerConnectionManagerTest: XCTestCase {
func testSimulcastConfig() throws {
let preset: VideoParameters = .presetHD43
let videoParameters = VideoParameters(
dimensions: preset.dimensions, simulcastConfig: SimulcastConfig(enabled: true, activeEncodings: [.h, .l]))
dimensions: preset.dimensions,
simulcastConfig: SimulcastConfig(enabled: true, activeEncodings: [.h, .l]))
let videoTrack = LocalVideoTrack.create(
for: .camera, videoParameters: videoParameters, peerConnectionFactoryWrapper: peerConnectionFactory)
for: .camera, videoParameters: videoParameters,
peerConnectionFactoryWrapper: peerConnectionFactory)

manager.addTrack(track: videoTrack, localStreamId: "id")
let encodings = peerConnection.transceivers[0].sender.parameters.encodings
Expand All @@ -94,7 +101,8 @@ class PeerConnectionManagerTest: XCTestCase {
simulcastConfig: SimulcastConfig(enabled: true, activeEncodings: [.h, .m, .l])
)
let videoTrack = LocalVideoTrack.create(
for: .camera, videoParameters: videoParameters, peerConnectionFactoryWrapper: peerConnectionFactory)
for: .camera, videoParameters: videoParameters,
peerConnectionFactoryWrapper: peerConnectionFactory)

manager.addTrack(track: videoTrack, localStreamId: "id")

Expand All @@ -112,12 +120,14 @@ class PeerConnectionManagerTest: XCTestCase {
simulcastConfig: SimulcastConfig(enabled: true, activeEncodings: [.h, .m, .l])
)
let videoTrack = LocalVideoTrack.create(
for: .camera, videoParameters: videoParameters, peerConnectionFactoryWrapper: peerConnectionFactory)
for: .camera, videoParameters: videoParameters,
peerConnectionFactoryWrapper: peerConnectionFactory)

manager.addTrack(track: videoTrack, localStreamId: "id")

let encodings = peerConnection.transceivers[0].sender.parameters.encodings
XCTAssertEqual(1_536_000, encodings[2].maxBitrateBps, "h layer should have correct maxBitrateBps")
XCTAssertEqual(
1_536_000, encodings[2].maxBitrateBps, "h layer should have correct maxBitrateBps")
XCTAssertEqual(512000, encodings[1].maxBitrateBps, "m layer should have correct maxBitrateBps")
XCTAssertEqual(153600, encodings[0].maxBitrateBps, "l layer should have correct maxBitrateBps")
}
Expand Down
3 changes: 2 additions & 1 deletion MembraneVideoroomDemo/Controllers/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ final class AppController: ObservableObject {

public func connect(room: String, displayName: String) {
self.displayName = displayName
let engineUrl = Constants.getRtcEngineUrl().trimmingCharacters(in: CharacterSet(charactersIn: "/"))
let engineUrl = Constants.getRtcEngineUrl().trimmingCharacters(
in: CharacterSet(charactersIn: "/"))

let transportUrl = "\(engineUrl)"

Expand Down
26 changes: 18 additions & 8 deletions MembraneVideoroomDemo/Controllers/RoomController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ParticipantVideo: Identifiable, ObservableObject {
@Published var vadStatus: VadStatus

init(
id: String, participant: Participant, videoTrack: VideoTrack? = nil, isScreensharing: Bool = false,
id: String, participant: Participant, videoTrack: VideoTrack? = nil,
isScreensharing: Bool = false,
isActive: Bool = false,
mirror: Bool = false
) {
Expand Down Expand Up @@ -264,7 +265,9 @@ class RoomController: ObservableObject {
// set the current primary video
self.primaryVideo = video

if video.participant.id != self.localParticipantId && self.primaryVideo?.isScreensharing == false {
if video.participant.id != self.localParticipantId
&& self.primaryVideo?.isScreensharing == false
{
self.room?.setTargetTrackEncoding(trackId: video.id, encoding: TrackEncoding.h)
}
}
Expand Down Expand Up @@ -325,7 +328,9 @@ class RoomController: ObservableObject {
return participantVideos.first(where: { $0.id == id })
}

func findParticipantVideoByOwner(participantId: String, isScreencast: Bool = false) -> ParticipantVideo? {
func findParticipantVideoByOwner(participantId: String, isScreencast: Bool = false)
-> ParticipantVideo?
{
if let primaryVideo = self.primaryVideo, primaryVideo.participant.id == participantId,
primaryVideo.isScreensharing == isScreencast
{
Expand Down Expand Up @@ -438,9 +443,11 @@ extension RoomController: MembraneRTCDelegate {
let isScreensharing = ctx.metadata["type"] as? String == "screensharing"
let video = ParticipantVideo(
id: ctx.trackId, participant: participant, videoTrack: videoTrack,
isScreensharing: isScreensharing, isActive: ctx.metadata["active"] as? Bool == true || isScreensharing)
isScreensharing: isScreensharing,
isActive: ctx.metadata["active"] as? Bool == true || isScreensharing)

guard let existingVideo = self.findParticipantVideoByOwner(participantId: ctx.endpoint.id) else {
guard let existingVideo = self.findParticipantVideoByOwner(participantId: ctx.endpoint.id)
else {
add(video: video)

if isScreensharing {
Expand Down Expand Up @@ -505,7 +512,8 @@ extension RoomController: MembraneRTCDelegate {
if ctx.endpoint.id == self.primaryVideo?.participant.id {
self.primaryVideo?.participant = p
} else {
self.participantVideos.first(where: { $0.participant.id == ctx.endpoint.id })?.participant = p
self.participantVideos.first(where: { $0.participant.id == ctx.endpoint.id })?
.participant = p
}
}

Expand All @@ -514,9 +522,11 @@ extension RoomController: MembraneRTCDelegate {

func onEndpointAdded(endpoint: Endpoint) {
self.participants[endpoint.id] = Participant(
id: endpoint.id, displayName: endpoint.metadata["displayName"] as? String ?? "", isAudioTrackActive: false)
id: endpoint.id, displayName: endpoint.metadata["displayName"] as? String ?? "",
isAudioTrackActive: false)
let pv =
ParticipantVideo(id: endpoint.id, participant: participants[endpoint.id]!, videoTrack: nil, isActive: false)
ParticipantVideo(
id: endpoint.id, participant: participants[endpoint.id]!, videoTrack: nil, isActive: false)
add(video: pv)

}
Expand Down
3 changes: 2 additions & 1 deletion MembraneVideoroomDemo/Utilities/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Foundation
/// A bunch of application specific constants
struct Constants {
static func getRtcEngineUrl() -> String {
return (Bundle.main.infoDictionary?["videoroom_url"] as! String).replacingOccurrences(of: "\\", with: "")
return (Bundle.main.infoDictionary?["videoroom_url"] as! String).replacingOccurrences(
of: "\\", with: "")
}

/// App Group used for communicating with `Broadcast Upload Extension`
Expand Down
3 changes: 2 additions & 1 deletion MembraneVideoroomDemo/Views/ParticipantVideoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ struct ParticipantVideoView: View {
ZStack(alignment: .topLeading) {
if self.participantVideo.isActive {
SwiftUIVideoView(
(self.participantVideo.videoTrack)!, layout: self.layout, mirror: self.participantVideo.mirror,
(self.participantVideo.videoTrack)!, layout: self.layout,
mirror: self.participantVideo.mirror,
dimensions: $localDimensions
)
.onChange(of: localDimensions) { value in
Expand Down
6 changes: 4 additions & 2 deletions MembraneVideoroomDemo/Views/RoomView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ struct RoomView: View {
} else {
AdaptiveStack(orientation: self.orientationReceiver.orientation) {
if let primaryVideo = room.primaryVideo {
ParticipantVideoView(primaryVideo, height: videoFrameHeight, width: videoFrameWidth)
.padding(.bottom)
ParticipantVideoView(
primaryVideo, height: videoFrameHeight, width: videoFrameWidth
)
.padding(.bottom)
}

VStack {
Expand Down
4 changes: 3 additions & 1 deletion Sources/MembraneRTC/IPC/IPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public class IPCServer: IPC {
port = CFMessagePortCreateLocal(
nil, name as CFString,
{
(port: CFMessagePort?, id: Int32, data: CFData?, _: UnsafeMutableRawPointer?) -> Unmanaged<CFData>? in
(port: CFMessagePort?, id: Int32, data: CFData?, _: UnsafeMutableRawPointer?) -> Unmanaged<
CFData
>? in
guard let selfObj = port?.associatedSelf() as? IPCServer,
let data = data as Data?
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ public class LocalScreenBroadcastTrack: LocalVideoTrack, ScreenBroadcastCapturer

internal init(
appGroup: String, videoParameters: VideoParameters,
delegate _: LocalScreenBroadcastTrackDelegate? = nil, peerConnectionFactoryWrapper: PeerConnectionFactoryWrapper
delegate _: LocalScreenBroadcastTrackDelegate? = nil,
peerConnectionFactoryWrapper: PeerConnectionFactoryWrapper
) {
self.appGroup = appGroup
super.init(parameters: videoParameters, peerConnectionFactoryWrapper: peerConnectionFactoryWrapper)
super.init(
parameters: videoParameters, peerConnectionFactoryWrapper: peerConnectionFactoryWrapper)
}

internal func started() {
Expand Down
4 changes: 3 additions & 1 deletion Sources/MembraneRTC/Media/Tracks/LocalVideoTrack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public class LocalVideoTrack: VideoTrack, LocalTrack {
case camera, file
}

internal init(parameters: VideoParameters, peerConnectionFactoryWrapper: PeerConnectionFactoryWrapper) {
internal init(
parameters: VideoParameters, peerConnectionFactoryWrapper: PeerConnectionFactoryWrapper
) {
let source = peerConnectionFactoryWrapper.createVideoSource()

videoSource = source
Expand Down
46 changes: 32 additions & 14 deletions Sources/MembraneRTC/MembraneRTC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ internal let pcLogPrefix = "[PeerConnection]"
/// Once the user created the MembraneRTC client and connected to the server transport layer they can call the `connect` method to initialize joining the session.
/// After receiving `onConnected` a user will receive notification about various peers joining/leaving the session, new tracks being published and ready for playback
/// or going inactive.
public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObject, RTCEngineListener,
public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObject,
RTCEngineListener,
PeerConnectionListener
{
enum State {
Expand Down Expand Up @@ -58,7 +59,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

private var localTracks: [LocalTrack] = []

private var localEndpoint = Endpoint(id: "", type: "webrtc", metadata: .init([:]), trackIdToMetadata: [:])
private var localEndpoint = Endpoint(
id: "", type: "webrtc", metadata: .init([:]), trackIdToMetadata: [:])

// mapping from peer's id to itself
private var remoteEndpoints: [String: Endpoint] = [:]
Expand All @@ -73,7 +75,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

private lazy var peerConnectionManager: PeerConnectionManager = {
return PeerConnectionManager(
config: self.config, peerConnectionFactory: self.peerConnectionFactoryWrapper, peerConnectionListener: self)
config: self.config, peerConnectionFactory: self.peerConnectionFactoryWrapper,
peerConnectionListener: self)
}()

internal init(config: RTCConfiguration, encoder: Encoder) {
Expand Down Expand Up @@ -101,7 +104,9 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

- Returns: `MembraneRTC` client instance
*/
public static func create(with options: CreateOptions = CreateOptions(), delegate: MembraneRTCDelegate)
public static func create(
with options: CreateOptions = CreateOptions(), delegate: MembraneRTCDelegate
)
-> MembraneRTC
{
DispatchQueue.webRTC.sync {
Expand Down Expand Up @@ -173,7 +178,9 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

- Returns: `LocalCameraVideoTrack` instance that user then can use for things such as front / back camera switch.
*/
public func createVideoTrack(videoParameters: VideoParameters, metadata: Metadata, captureDeviceId: String? = nil)
public func createVideoTrack(
videoParameters: VideoParameters, metadata: Metadata, captureDeviceId: String? = nil
)
-> LocalVideoTrack
{
DispatchQueue.webRTC.sync {
Expand All @@ -183,15 +190,18 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

peerConnectionManager.addTrack(track: videoTrack, localStreamId: localStreamId)

if let captureDeviceId = captureDeviceId, let videoTrack = videoTrack as? LocalCameraVideoTrack {
if let captureDeviceId = captureDeviceId,
let videoTrack = videoTrack as? LocalCameraVideoTrack
{
videoTrack.switchCamera(deviceId: captureDeviceId)
}

videoTrack.start()

localTracks.append(videoTrack)

localEndpoint = localEndpoint.withTrack(trackId: videoTrack.rtcTrack().trackId, metadata: metadata)
localEndpoint = localEndpoint.withTrack(
trackId: videoTrack.rtcTrack().trackId, metadata: metadata)

engineCommunication.renegotiateTracks()

Expand Down Expand Up @@ -219,7 +229,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

localTracks.append(audioTrack)

localEndpoint = localEndpoint.withTrack(trackId: audioTrack.rtcTrack().trackId, metadata: metadata)
localEndpoint = localEndpoint.withTrack(
trackId: audioTrack.rtcTrack().trackId, metadata: metadata)

engineCommunication.renegotiateTracks()

Expand Down Expand Up @@ -398,7 +409,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
*/
public func setEncodingBandwidth(trackId: String, encoding: String, bandwidth: BandwidthLimit) {
DispatchQueue.webRTC.sync {
peerConnectionManager.setEncodingBandwidth(trackId: trackId, encoding: encoding, bandwidth: bandwidth)
peerConnectionManager.setEncodingBandwidth(
trackId: trackId, encoding: encoding, bandwidth: bandwidth)
}
}

Expand Down Expand Up @@ -470,7 +482,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

// initialize peer's track contexts
endpoint.trackIdToMetadata?.forEach { trackId, metadata in
let context = TrackContext(track: nil, enpoint: endpoint, trackId: trackId, metadata: metadata)
let context = TrackContext(
track: nil, enpoint: endpoint, trackId: trackId, metadata: metadata)

self.trackContexts[trackId] = context

Expand Down Expand Up @@ -551,7 +564,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

func onOfferData(integratedTurnServers: [OfferDataEvent.TurnServer], tracksTypes: [String: Int]) {
peerConnectionManager.getSdpOffer(
integratedTurnServers: integratedTurnServers, tracksTypes: tracksTypes, localTracks: localTracks
integratedTurnServers: integratedTurnServers, tracksTypes: tracksTypes,
localTracks: localTracks
) { sdp, midToTrackId, error in
if let err = error {
sdkLogger.error("Failed to create sdp offer: \(err)")
Expand All @@ -568,7 +582,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
}

func onSdpAnswer(type: String, sdp: String, midToTrackId: [String: String?]) {
peerConnectionManager.onSdpAnswer(sdp: sdp, midToTrackId: midToTrackId, localTracks: localTracks)
peerConnectionManager.onSdpAnswer(
sdp: sdp, midToTrackId: midToTrackId, localTracks: localTracks)
}

func onRemoteCandidate(candidate: String, sdpMLineIndex: Int32, sdpMid: String?) {
Expand All @@ -594,7 +609,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

// for each track create a corresponding track context
endpoint.trackIdToMetadata?.forEach { trackId, metadata in
let context = TrackContext(track: nil, enpoint: endpoint, trackId: trackId, metadata: metadata)
let context = TrackContext(
track: nil, enpoint: endpoint, trackId: trackId, metadata: metadata)

self.trackContexts[trackId] = context

Expand Down Expand Up @@ -638,7 +654,9 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
}
}

func onTrackEncodingChanged(endpointId: String, trackId: String, encoding: String, encodingReason: String) {
func onTrackEncodingChanged(
endpointId: String, trackId: String, encoding: String, encodingReason: String
) {
if let trackEncoding = TrackEncoding.fromString(encoding),
let trackContext = self.trackContexts[trackId],
let encodingReasonEnum = EncodingReason(rawValue: encodingReason)
Expand Down
Loading

0 comments on commit 71ba4b2

Please sign in to comment.