Skip to content

Commit 75b4a05

Browse files
committed
HeadlessTransport: Callback on track
1 parent f137a98 commit 75b4a05

File tree

5 files changed

+32
-34
lines changed

5 files changed

+32
-34
lines changed

Sources/alloclient/UIWebRTCTransport.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,6 @@ class UIWebRTCTransport: NSObject, Transport, LKRTCPeerConnectionDelegate, LKRTC
457457
micTrack?.isEnabled = newValue
458458
}
459459
}
460-
461-
var outgoingStreamSender: LKRTCRtpSender?
462-
public func addOutgoing(stream: LKRTCMediaStream)
463-
{
464-
outgoingStreamSender = peer.add(stream.audioTracks[0], streamIds: [stream.streamId])
465-
print("Forwarding audio stream with sender \(outgoingStreamSender!)")
466-
}
467460
}
468461

469462
// MARK: - Wrapper classes
@@ -473,10 +466,10 @@ private class ClientDataChannel: DataChannel {
473466

474467
init(channel: LKRTCDataChannel) {
475468
self.channel = channel
476-
self.label = DataChannelLabel(rawValue: channel.label)!
469+
self.alloLabel = DataChannelLabel(rawValue: channel.label)!
477470
}
478471

479-
var label: DataChannelLabel
472+
var alloLabel: DataChannelLabel
480473
var isOpen: Bool { (channel?.readyState ?? .closed) == .open }
481474
}
482475
extension LKRTCDataChannel {
@@ -500,7 +493,7 @@ private class ClientMediaStream: MediaStream {
500493
self.stream = stream
501494
}
502495

503-
var streamId: String { stream.streamId }
496+
var mediaId: String { stream.streamId }
504497
}
505498
extension LKRTCMediaStream {
506499
static var wrapperKey: Void = ()

Sources/alloheadless/HeadlessWebRTCTransport.swift

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class HeadlessWebRTCTransport: Transport
1616
public private(set) var clientId: ClientId?
1717

1818
private var peer: AlloWebRTCPeer
19-
private var channels: [String: ServerDataChannel] = [:] // track which channels are created
19+
private var channels: [String: AlloWebRTCPeer.DataChannel] = [:] // track which channels are created
2020
private var connectionStatus: ConnectionStatus
2121
private var cancellables = Set<AnyCancellable>()
2222

@@ -36,6 +36,12 @@ public class HeadlessWebRTCTransport: Transport
3636
}
3737
}.store(in: &cancellables)
3838

39+
peer.$tracks.sinkChanges(added: { track in
40+
self.delegate?.transport(self, didReceiveMediaStream: track)
41+
}, removed: { track in
42+
// TODO: notify track removals too
43+
}).store(in: &cancellables)
44+
3945
// TODO: subscribe to more callbacks and match UIWebRTCTransport's behavior
4046
// TODO: Populate connectionStatus
4147
// TODO: Manage renegotiation
@@ -51,6 +57,8 @@ public class HeadlessWebRTCTransport: Transport
5157
{
5258
Task { @MainActor in self.connectionStatus.signalling = .connecting }
5359

60+
61+
5462
try peer.lockLocalDescription(type: .offer)
5563
let offerSdp = try peer.createOffer()
5664
print("Generated Offer: \(offerSdp)")
@@ -104,26 +112,26 @@ public class HeadlessWebRTCTransport: Transport
104112
{
105113
peer.close()
106114
clientId = nil
115+
cancellables.forEach { $0.cancel() }
107116
}
108117

109118
public func createDataChannel(label: DataChannelLabel, reliable: Bool) -> DataChannel?
110119
{
111-
let achannel = try! peer.createDataChannel(label: label.rawValue, reliable: reliable, streamId: UInt16(label.channelId), negotiated: true)
112-
let channel = ServerDataChannel(label: label, channel: achannel)
120+
let channel = try! peer.createDataChannel(label: label.rawValue, reliable: reliable, streamId: UInt16(label.channelId), negotiated: true)
113121
channels[label.rawValue] = channel
114122

115-
achannel.$lastMessage.sink { [weak self] message in
123+
channel.$lastMessage.sink { [weak self] message in
116124
guard let self = self, let message = message else { return }
117125
self.delegate?.transport(self, didReceiveData: message, on: channel)
118126
}.store(in: &cancellables)
119-
127+
120128
return channel
121129
}
122130

123131
public func send(data: Data, on channelLabel: DataChannelLabel)
124132
{
125133
let ch = channels[channelLabel.rawValue]!
126-
try! ch.channel.send(data: data)
134+
try! ch.send(data: data)
127135
}
128136

129137
// Media operations - server can forward but not create
@@ -149,28 +157,23 @@ public class HeadlessWebRTCTransport: Transport
149157
}
150158
}
151159

152-
private class ServerDataChannel: DataChannel
160+
extension AlloWebRTCPeer.DataChannel : DataChannel
153161
{
154-
let label: DataChannelLabel
155-
let channel: AlloWebRTCPeer.Channel
156-
var isOpen: Bool { return channel.open }
157-
158-
init(label: DataChannelLabel, channel: AlloWebRTCPeer.Channel)
162+
public var alloLabel: DataChannelLabel
159163
{
160-
self.label = label
161-
self.channel = channel
164+
return DataChannelLabel(rawValue: self.label)!
162165
}
163166
}
164167

165-
private class ServerMediaStream: MediaStream
168+
extension AlloWebRTCPeer.Track : MediaStream
166169
{
167-
let streamId: String
168-
169-
init(streamId: String) {
170-
self.streamId = streamId
170+
public var mediaId: String
171+
{
172+
"\(self.streamId).\(self.trackId)"
171173
}
172174
}
173175

176+
174177
extension SignallingPayload
175178
{
176179
public func adcCandidates() -> [AlloWebRTCPeer.ICECandidate]

Sources/allonet2/AlloSession.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public protocol AlloSessionDelegate: AnyObject
2020
func session(_: AlloSession, didReceiveIntent intent: Intent)
2121

2222
func session(_: AlloSession, didReceiveMediaStream: MediaStream)
23+
// TODO: handle losing a media stream too
24+
//func session(_: AlloSession, didRemoveMediaStream: MediaStream)
2325
}
2426

2527
/// Wrapper of Transport, adding Alloverse-specific channels and data types
@@ -116,7 +118,7 @@ public class AlloSession : NSObject, TransportDelegate
116118
let decoder = BinaryDecoder()
117119
public func transport(_ transport: Transport, didReceiveData data: Data, on channel: DataChannel)
118120
{
119-
switch channel.label
121+
switch channel.alloLabel
120122
{
121123
case .interactions:
122124
do {
@@ -233,7 +235,7 @@ public class AlloSession : NSObject, TransportDelegate
233235
// MARK: - Audio
234236
public func transport(_ transport: Transport, didReceiveMediaStream stream: MediaStream)
235237
{
236-
incomingStreams[stream.streamId] = stream
238+
incomingStreams[stream.mediaId] = stream
237239
delegate?.session(self, didReceiveMediaStream: stream)
238240
}
239241
}

Sources/allonet2/TransportProtocol.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ extension DataChannelLabel
8787
}
8888

8989
public protocol DataChannel {
90-
var label: DataChannelLabel { get }
90+
var alloLabel: DataChannelLabel { get }
9191
var isOpen: Bool { get }
9292
}
9393

9494
public protocol MediaStream {
95-
var streamId: String { get }
95+
var mediaId: String { get }
9696
}
9797

9898
public protocol AudioTrack {

0 commit comments

Comments
 (0)