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

Commit

Permalink
remove trackIdToMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
karkakol committed Jan 18, 2024
1 parent be72cb7 commit 815f929
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 37 deletions.
8 changes: 1 addition & 7 deletions Sources/MembraneRTC/Events/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ struct EndpointAddedEvent: ReceivableEvent, Codable {
let id: String
let type: String
let metadata: Metadata?
let trackIdToMetadata: [String: Metadata]?
let tracks: [String: TrackData]?
}

let type: ReceivableEventType
Expand Down Expand Up @@ -351,16 +351,10 @@ struct OfferDataEvent: ReceivableEvent, Codable {

public struct TracksAddedEvent: ReceivableEvent, Codable {
public struct Data: Codable {
public struct TrackData: Codable{
let metadata: Metadata
let simulcastConfig: SimulcastConfig?
}
let endpointId: String
let trackIdToMetadata: [String: Metadata]
let tracks: [String: TrackData]
}


public let type: ReceivableEventType
public let data: Data
}
Expand Down
20 changes: 11 additions & 9 deletions Sources/MembraneRTC/MembraneRTC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

private var localTracks: [LocalTrack] = []

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

// mapping from peer's id to itself
private var remoteEndpoints: [String: Endpoint] = [:]
Expand Down Expand Up @@ -469,8 +469,8 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
self.remoteEndpoints[endpoint.id] = endpoint

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

self.trackContexts[trackId] = context

Expand Down Expand Up @@ -512,7 +512,7 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
remoteEndpoints.removeValue(forKey: endpoint.id)

// for a leaving peer clear his track contexts
if let trackIds = endpoint.trackIdToMetadata?.keys {
if let trackIds = endpoint.tracks?.keys {
let contexts = trackIds.compactMap { id in
self.trackContexts[id]
}
Expand Down Expand Up @@ -560,7 +560,9 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje

if let sdp = sdp, let midToTrackId = midToTrackId {
self.engineCommunication.sdpOffer(
sdp: sdp, trackIdToTrackMetadata: self.localEndpoint.trackIdToMetadata ?? [:],
sdp: sdp, trackIdToTrackMetadata: self.localEndpoint.tracks?.mapValues({ trackData in
trackData.metadata
}) ?? [:],
midToTrackId: midToTrackId)
}

Expand All @@ -577,7 +579,7 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
peerConnectionManager.onRemoteCandidate(candidate: candidate)
}

func onTracksAdded(endpointId: String, trackIdToMetadata: [String: Metadata], tracks: [String: TracksAddedEvent.Data.TrackData]) {
func onTracksAdded(endpointId: String, tracks: [String: TrackData]) {
// ignore local participant
guard localEndpoint.id != endpointId else {
return
Expand All @@ -589,12 +591,12 @@ public class MembraneRTC: MulticastDelegate<MembraneRTCDelegate>, ObservableObje
}

// update tracks of the remote peer
endpoint = endpoint.with(trackIdToMetadata: trackIdToMetadata, tracks: tracks)
endpoint = endpoint.with(tracks: tracks)
remoteEndpoints[endpoint.id] = endpoint

// 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, simulcastConfig: endpoint.tracks?[trackId]?.simulcastConfig )
endpoint.tracks?.forEach { trackId, trackData in
let context = TrackContext(track: nil, enpoint: endpoint, trackId: trackId, metadata: trackData.metadata, simulcastConfig: trackData.simulcastConfig)

self.trackContexts[trackId] = context

Expand Down
5 changes: 3 additions & 2 deletions Sources/MembraneRTC/RTCEngineCommunication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ internal class RTCEngineCommunication {
engineListener.onEndpointAdded(
endpoint: Endpoint(
id: endpointAdded.data.id, type: endpointAdded.data.type, metadata: endpointAdded.data.metadata,
trackIdToMetadata: endpointAdded.data.trackIdToMetadata))
tracks: endpointAdded.data.tracks)
)
case .EndpointRemoved:
let endpointRemoved = event as! EndpointRemovedEvent
engineListener.onEndpointRemoved(endpointId: endpointRemoved.data.id)
Expand All @@ -80,7 +81,7 @@ internal class RTCEngineCommunication {
case .TracksAdded:
let tracksAdded = event as! TracksAddedEvent
engineListener.onTracksAdded(
endpointId: tracksAdded.data.endpointId, trackIdToMetadata: tracksAdded.data.trackIdToMetadata, tracks: tracksAdded.data.tracks)
endpointId: tracksAdded.data.endpointId, tracks: tracksAdded.data.tracks)
case .TracksRemoved:
let tracksRemoved = event as! TracksRemovedEvent
engineListener.onTracksRemoved(
Expand Down
2 changes: 1 addition & 1 deletion Sources/MembraneRTC/RTCEngineListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal protocol RTCEngineListener {
func onOfferData(integratedTurnServers: [OfferDataEvent.TurnServer], tracksTypes: [String: Int])
func onSdpAnswer(type: String, sdp: String, midToTrackId: [String: String?])
func onRemoteCandidate(candidate: String, sdpMLineIndex: Int32, sdpMid: String?)
func onTracksAdded(endpointId: String, trackIdToMetadata: [String: Metadata], tracks: [String: TracksAddedEvent.Data.TrackData])
func onTracksAdded(endpointId: String, tracks: [String: TrackData])
func onTracksRemoved(endpointId: String, trackIds: [String])
func onTrackUpdated(endpointId: String, trackId: String, metadata: Metadata)
func onTrackEncodingChanged(endpointId: String, trackId: String, encoding: String, encodingReason: String)
Expand Down
26 changes: 8 additions & 18 deletions Sources/MembraneRTC/Types/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,50 @@ public struct Endpoint: Codable {
public let id: String
public let type: String
public let metadata: Metadata
public let trackIdToMetadata: [String: Metadata]?
public let tracks: [String: TracksAddedEvent.Data.TrackData]?
public let tracks: [String: TrackData]?

public init(id: String, type: String, metadata: Metadata?, trackIdToMetadata: [String: Metadata]?, tracks: [String: TracksAddedEvent.Data.TrackData]? = nil) {
public init(id: String, type: String, metadata: Metadata?, tracks: [String: TrackData]? = nil) {
self.id = id
self.type = type
self.metadata = metadata ?? Metadata()
self.trackIdToMetadata = trackIdToMetadata
self.tracks = tracks
}

public func with(
id: String? = nil, type: String? = nil, metadata: Metadata? = nil, trackIdToMetadata: [String: Metadata]? = nil, tracks: [String: TracksAddedEvent.Data.TrackData]? = nil
id: String? = nil, type: String? = nil, metadata: Metadata? = nil, tracks: [String: TrackData]? = nil
) -> Self {
return Endpoint(
id: id ?? self.id,
type: type ?? self.type,
metadata: metadata ?? self.metadata,
trackIdToMetadata: trackIdToMetadata ?? self.trackIdToMetadata,
tracks: tracks ?? self.tracks
)
}

public func withTrack(trackId: String, metadata: Metadata?) -> Self {
var newTrackIdToMetadata = self.trackIdToMetadata
newTrackIdToMetadata?[trackId] = metadata ?? Metadata()

var newTracks = self.tracks
let simulcastConfig = newTracks?[trackId]?.simulcastConfig
newTracks?[trackId] = TracksAddedEvent.Data.TrackData(metadata: metadata ?? Metadata(), simulcastConfig: simulcastConfig)
newTracks?[trackId] = TrackData(metadata: metadata ?? Metadata(), simulcastConfig: simulcastConfig)

return Endpoint(id: self.id, type: self.type, metadata: self.metadata, trackIdToMetadata: newTrackIdToMetadata, tracks: newTracks)
return Endpoint(id: self.id, type: self.type, metadata: self.metadata, tracks: newTracks)
}

public func withoutTrack(trackId: String) -> Self {
var newTrackIdToMetadata = self.trackIdToMetadata
newTrackIdToMetadata?.removeValue(forKey: trackId)

var newTracks = self.tracks
newTracks?.removeValue(forKey: trackId)

return Endpoint(id: self.id, type: self.type, metadata: self.metadata, trackIdToMetadata: newTrackIdToMetadata, tracks: newTracks)
return Endpoint(id: self.id, type: self.type, metadata: self.metadata, tracks: newTracks)
}

enum CodingKeys: String, CodingKey {
case id, type, metadata, trackIdToMetadata, tracks
case id, type, metadata, tracks
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.type = try container.decode(String.self, forKey: .type)
self.metadata = try container.decodeIfPresent(Metadata.self, forKey: .metadata) ?? Metadata()
self.trackIdToMetadata = try container.decodeIfPresent([String: Metadata].self, forKey: .trackIdToMetadata)
self.tracks = try container.decodeIfPresent([String: TracksAddedEvent.Data.TrackData].self, forKey: .tracks)
self.tracks = try container.decodeIfPresent([String: TrackData].self, forKey: .tracks)
}
}
4 changes: 4 additions & 0 deletions Sources/MembraneRTC/Types/TrackData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public struct TrackData: Codable{
let metadata: Metadata
let simulcastConfig: SimulcastConfig?
}

0 comments on commit 815f929

Please sign in to comment.