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

Commit

Permalink
update simulcast for local endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
karkakol committed Jan 24, 2024
1 parent 1e591c3 commit 4a8e818
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
23 changes: 16 additions & 7 deletions Sources/MembraneRTC/MembraneRTC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ 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,
simulcastConfig: SimulcastConfig? = nil
)
-> LocalVideoTrack
{
DispatchQueue.webRTC.sync {
Expand All @@ -191,7 +194,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

localTracks.append(videoTrack)

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

engineCommunication.renegotiateTracks()

Expand Down Expand Up @@ -219,7 +223,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, simulcastConfig: nil)

engineCommunication.renegotiateTracks()

Expand Down Expand Up @@ -252,6 +257,7 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
appGroup: appGroup, videoParameters: videoParameters,
peerConnectionFactoryWrapper: peerConnectionFactoryWrapper)
localTracks.append(screensharingTrack)
let simulcastConfig = videoParameters.simulcastConfig

broadcastScreenshareReceiver = ScreenBroadcastNotificationReceiver(
onStart: { [weak self, weak screensharingTrack] in
Expand All @@ -260,7 +266,7 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
}

DispatchQueue.main.async {
self?.setupScreencastTrack(track: track, metadata: metadata)
self?.setupScreencastTrack(track: track, metadata: metadata, simulcastConfig: simulcastConfig)
onStart(track)
}
},
Expand Down Expand Up @@ -369,7 +375,7 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
public func updateTrackMetadata(trackId: String, trackMetadata: Metadata) {
DispatchQueue.webRTC.sync {
engineCommunication.updateTrackMetadata(trackId: trackId, trackMetadata: trackMetadata)
localEndpoint = localEndpoint.withTrack(trackId: trackId, metadata: trackMetadata)
localEndpoint = localEndpoint.withTrack(trackId: trackId, metadata: trackMetadata, simulcastConfig: nil)
}
}

Expand Down Expand Up @@ -403,12 +409,15 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
}

/// Adds given broadcast track to the peer connection and forces track renegotiation.
private func setupScreencastTrack(track: LocalScreenBroadcastTrack, metadata: Metadata) {
private func setupScreencastTrack(
track: LocalScreenBroadcastTrack, metadata: Metadata, simulcastConfig: SimulcastConfig?
) {
let screencastStreamId = UUID().uuidString

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

localEndpoint = localEndpoint.withTrack(trackId: track.rtcTrack().trackId, metadata: metadata)
localEndpoint = localEndpoint.withTrack(
trackId: track.rtcTrack().trackId, metadata: metadata, simulcastConfig: simulcastConfig)

engineCommunication.renegotiateTracks()
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/MembraneRTC/Types/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct Endpoint: Codable {
}

public func with(
id: String? = nil, type: String? = nil, metadata: Metadata? = nil, tracks: [String: TrackData]? = nil
id: String? = nil, type: String? = nil, metadata: Metadata? = nil, tracks: [String: TrackData]? = nil
) -> Self {
return Endpoint(
id: id ?? self.id,
Expand All @@ -22,10 +22,11 @@ public struct Endpoint: Codable {
)
}

public func withTrack(trackId: String, metadata: Metadata?) -> Self {
public func withTrack(trackId: String, metadata: Metadata?, simulcastConfig: SimulcastConfig?) -> Self {
var newTracks = self.tracks
let simulcastConfig = newTracks?[trackId]?.simulcastConfig
newTracks?[trackId] = TrackData(metadata: metadata ?? Metadata(), simulcastConfig: simulcastConfig)
let oldSimulcastConfig = newTracks?[trackId]?.simulcastConfig
newTracks?[trackId] = TrackData(
metadata: metadata ?? Metadata(), simulcastConfig: simulcastConfig ?? oldSimulcastConfig)

return Endpoint(id: self.id, type: self.type, metadata: self.metadata, tracks: newTracks)
}
Expand Down

0 comments on commit 4a8e818

Please sign in to comment.