Skip to content

Commit d70ab48

Browse files
committed
more work in progress
+(cleaned commit)
1 parent 5928db2 commit d70ab48

File tree

9 files changed

+181
-151
lines changed

9 files changed

+181
-151
lines changed

Sources/PrivMXEndpointStreamsLow/WebRtcInterfaceInstance.cpp

Lines changed: 0 additions & 14 deletions
This file was deleted.

Sources/PrivMXEndpointStreamsLow/include/WebRtcInterfaceInstance.hpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,6 @@ typedef void(*UpdateSessionIdCallback)(const std::string&,const int64_t, const s
2828
typedef void(*CloseCallback)(const std::string&);
2929
typedef void(*UpdateKeysCallback)(const std::string&,const KeyVector&);
3030

31-
typedef int(*ConvertToRGBAImpl)(uint8_t*,int,int,int);
32-
33-
class FrameImpl : public endpoint::stream::Frame{
34-
ConvertToRGBAImpl cb;
35-
public:
36-
37-
FrameImpl(ConvertToRGBAImpl _imp) : cb(_imp) {}
38-
39-
int ConvertToRGBA(uint8_t* dst_argb, int dst_stride_argb,int dest_width, int dest_height) override{
40-
return cb(dst_argb,dst_stride_argb,dest_width,dest_height);
41-
}
42-
};
43-
4431
class WebRtcInterfaceInstance: public privmx::endpoint::stream::WebRTCInterface{
4532
public:
4633
virtual std::string createOfferAndSetLocalDescription(const std::string& streamRoomId) override {

Sources/PrivMXEndpointSwift/Streams/JanusConnection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import WebRTC
1515
import PrivMXEndpointStreamsLow
1616

1717
public final class JanusConnection: @unchecked Sendable{
18-
var peerConnection : PeerConnection
18+
var peerConnection : RTCPeerConnection
1919
var sessionId: Int64
2020
var hasSubscriptions : Bool
2121

2222
init(
23-
peerConnection: PeerConnection,
23+
peerConnection: RTCPeerConnection,
2424
sessionId: Int64,
2525
hasSubscriptions: Bool
2626
) {

Sources/PrivMXEndpointSwift/Streams/PeerConnection.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class PeerConnection: @unchecked Sendable{
3737
) {
3838
self.rtcPeerConnection = rtcPeerConnection
3939
self.rtcPeerConnectionObserver = rtcPeerConnectionObserver
40+
self.rtcPeerConnection.delegate = self.rtcPeerConnectionObserver
4041
self.keys = keys
4142
}
4243

Sources/PrivMXEndpointSwift/Streams/PeerConnectionManager.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ import WebRTC
1616
import os.lock
1717

1818
public final class PeerConnectionManager: Sendable {
19+
20+
private let _createPeerConnection : (@Sendable (String) -> RTCPeerConnection)
21+
nonisolated(unsafe) var _onTrickle : (@Sendable (Int64,String) throws -> Void)
22+
nonisolated(unsafe) private var _connections : [String : [ConnectionType:JanusConnection]] = [:]
23+
1924
enum State{
2025
case reading,writing
2126
case idle
2227
}
2328
private let mutex = OSAllocatedUnfairLock(initialState: State.idle)
24-
private let _createPeerConnection : (@Sendable (String) -> PeerConnection)
25-
private let _onTrickle : (@Sendable (Int64,String) throws -> Void)
26-
nonisolated(unsafe) private var _connections : [String : [ConnectionType:JanusConnection]] = [:]
2729
nonisolated private var connections : [String : [ConnectionType:JanusConnection]]{
2830
set(val) {
2931
mutex.withLockUnchecked{
@@ -47,8 +49,8 @@ public final class PeerConnectionManager: Sendable {
4749
}
4850

4951
init(
50-
_createPeerConnection: @Sendable @escaping (String) -> PeerConnection,
51-
_onTrickle: (@Sendable @escaping (Int64,String) throws -> Void)
52+
_createPeerConnection: @Sendable @escaping (String) -> RTCPeerConnection,
53+
_onTrickle: @escaping (@Sendable (Int64,String) throws -> Void)
5254
) {
5355
self._createPeerConnection = _createPeerConnection
5456
self._onTrickle = _onTrickle
@@ -72,14 +74,18 @@ public final class PeerConnectionManager: Sendable {
7274
let pc = _createPeerConnection(streamRoomId)
7375

7476

75-
pc.rtcPeerConnectionObserver.setIceCandidateGeneratedCallback({
77+
(pc.delegate as? PmxPeerConnectionObserver)?.setIceCandidateGeneratedCallback({
7678
peerConnection,candidate in
7779

7880
let roomConnections = self.connections[streamRoomId] ?? [:]
7981
let roomConnection = roomConnections[type]
8082
if !candidate.sdp.isEmpty,let sessionId = roomConnection?.sessionId, sessionId > -1{
8183
var iceCandidate = candidate.sdp
82-
try? self._onTrickle(sessionId,iceCandidate)
84+
do{
85+
try self._onTrickle(sessionId,iceCandidate)
86+
}catch{
87+
print("Failed to trickle candidate", error)
88+
}
8389
}
8490
})
8591

Sources/PrivMXEndpointSwift/Streams/PmxPeerConnectionObserver.swift

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,49 @@
1212
#if Streams
1313
import Foundation
1414
import WebRTC
15+
//import PrivMXEndpointStreamsLow
1516
import Synchronization
17+
import os.lock
1618

1719

1820
public final class PmxPeerConnectionObserver:NSObject,RTCPeerConnectionDelegate, @unchecked Sendable{
1921
private var streamRoomId: String
2022
private var currentKeys = PMXKeyStore()
21-
22-
private var onFrameCallback: (Int64, Int64) -> Void
23+
private var peerConnectionFactory : RTCPeerConnectionFactory
24+
25+
enum State{
26+
case reading,writing
27+
case idle
28+
}
29+
nonisolated(unsafe) private var _cryptors : [String : PMXFrameCryptorTransformer] = [:]
30+
private let mutex = OSAllocatedUnfairLock(initialState: State.idle)
31+
private var cryptors : [String : PMXFrameCryptorTransformer]{
32+
set(val) {
33+
mutex.withLockUnchecked{
34+
state in
35+
state = .writing
36+
_cryptors = val
37+
state = .idle
38+
}
39+
}
40+
get {
41+
mutex.withLockUnchecked{
42+
state in
43+
defer{
44+
state = .idle
45+
}
46+
state = .reading
47+
return _cryptors
48+
}
49+
50+
}
51+
}
52+
53+
private var onFrameCallback:((Int64, Int64) -> Void)?
2354

2455
init(
2556
streamRoomId: String,
57+
peerConnectionFactory: RTCPeerConnectionFactory,
2658
currentKeys: PMXKeyStore = PMXKeyStore(),
2759
onConnectionSignalingStateChanged: ((RTCPeerConnection, RTCSignalingState) -> Void)? = nil,
2860
onConnectionPeerStateChanged: ((RTCPeerConnection, RTCPeerConnectionState) -> Void)? = nil,
@@ -41,6 +73,8 @@ public final class PmxPeerConnectionObserver:NSObject,RTCPeerConnectionDelegate,
4173
onTracksRemoved: ((RTCPeerConnection, RTCRtpReceiver) -> Void)? = nil,
4274
onLocalCandidateChanged: ((RTCPeerConnection, RTCIceCandidate, RTCIceCandidate, Int32, String) -> Void)? = nil
4375
) {
76+
self.peerConnectionFactory = peerConnectionFactory
77+
4478
self.streamRoomId = streamRoomId
4579
self.currentKeys = currentKeys
4680
self.onConnectionSignalingStateChanged = onConnectionSignalingStateChanged
@@ -84,6 +118,14 @@ public final class PmxPeerConnectionObserver:NSObject,RTCPeerConnectionDelegate,
84118

85119
private var onLocalCandidateChanged:((RTCPeerConnection,RTCIceCandidate,RTCIceCandidate,Int32,String)->Void)?
86120

121+
private var onVideoTrack: ((String) -> Void)?
122+
123+
public func setOnVideoTrackCallback(
124+
_ cb: ((String) -> Void)?
125+
) {
126+
onVideoTrack = cb
127+
}
128+
87129
public func setConnectionSignalingStateChangedCallback(
88130
_ cb: (@Sendable (RTCPeerConnection,RTCSignalingState)->Void)?
89131
) {
@@ -246,6 +288,13 @@ public final class PmxPeerConnectionObserver:NSObject,RTCPeerConnectionDelegate,
246288
streams mediaStreams: [RTCMediaStream]
247289
) {
248290
onTracksAdded?(peerConnection,rtpReceiver,mediaStreams)
291+
if let track = rtpReceiver.track {
292+
_cryptors[track.trackId] = PMXFrameCryptorTransformer(for: rtpReceiver, with: peerConnectionFactory, pmxKeyStore: currentKeys)
293+
if track.kind == "video" {
294+
onVideoTrack?("\(streamRoomId)-\(track.trackId)")
295+
}
296+
}
297+
249298
}
250299

251300
public func peerConnection(

Sources/PrivMXEndpointSwift/Streams/StreamApi.swift

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,64 +16,36 @@ import Foundation
1616
import WebRTC
1717

1818

19-
public actor StreamApi: @unchecked Sendable{
19+
public class StreamApi: @unchecked Sendable{
2020
// MARK: Fields
2121
private var api: privmx.NativeStreamApiLowWrapper
22-
private var peerConnectionFactory: RTCPeerConnectionFactory
23-
private var webRtcInstance: privmx.WebRtcInterfaceInstance
22+
private var rtcClient: WebRTCClient
2423

25-
26-
27-
//private var notificationListenerId : Int
28-
//private var connectedListenerId : Int
29-
//private var disconnectedListenerId : Int
30-
//
31-
//private var isStreamOnline: Bool = false
32-
//
33-
//private var frameCryptorOptions: Bool
34-
//private var configuration: WebRTC.RTCConfiguration
35-
//private var constraints: WebRTC.RTCMediaConstraints = RTCMediaConstraints(mandatoryConstraints: nil, optionalConstraints: nil)
36-
init(
24+
required init(
3725
api: privmx.NativeStreamApiLowWrapper,
38-
webRtcInstance: privmx.WebRtcInterfaceInstance,
39-
peerConnectionFactory: RTCPeerConnectionFactory,
26+
rtcClient: WebRTCClient
4027
) {
4128
self.api = api
42-
self.webRtcInstance = webRtcInstance
43-
self.peerConnectionFactory = peerConnectionFactory
29+
self.rtcClient = rtcClient
30+
31+
self.rtcClient.bindTrickleImpl(){ sessionId, candidate in
32+
self.api.trickle(sessionId, std.string(candidate))}
4433
}
4534

4635
static func create(
4736
connection: Connection,
4837
eventApi: inout EventApi
4938
) async throws -> StreamApi{
50-
let res = privmx.NativeStreamApiLowWrapper.create(connection.api, &eventApi.api)
51-
let peerConnectionFactory = RTCPeerConnectionFactory()
52-
guard var api = res.result.value
39+
let low = privmx.NativeStreamApiLowWrapper.create(connection.api, &eventApi.api)
40+
guard var api = low.result.value
5341
else {
5442
throw PrivMXEndpointError.otherFailure(privmx.InternalError())
5543
}
56-
let creds = api.getTurnCredentials()
57-
RTCInitializeSSL()
44+
5845
return Self(
5946
api: api,
60-
webRtcInstance: privmx.WebRtcInterfaceInstance.init(
61-
{ roomId in
62-
return ""
63-
},
64-
{ _, _, _ in return ""},
65-
{ _, _, _ in},
66-
{ _, _, _ in},
67-
{ _, _ in},
68-
{ _ in}),
69-
peerConnectionFactory: peerConnectionFactory,
70-
//notificationListenerId: Int(),
71-
//connectedListenerId: Int(),
72-
//disconnectedListenerId: Int(),
73-
//frameCryptorOptions: Bool(),
74-
//configuration: RTCConfiguration(),
75-
//constraints: RTCMediaConstraints()
76-
)
47+
rtcClient: WebRTCClient()
48+
)
7749
}
7850

7951
// MARK: - Rooms
@@ -88,13 +60,13 @@ public actor StreamApi: @unchecked Sendable{
8860
var uv = privmx.UserWithPubKeyVector()
8961
uv.reserve(users.count)
9062
for u in users{
91-
uv.push_back(consuming: u)
63+
uv.push_back(u)
9264
}
9365

9466
var mv = privmx.UserWithPubKeyVector()
9567
mv.reserve(managers.count)
9668
for m in managers{
97-
mv.push_back(consuming: m)
69+
mv.push_back(m)
9870
}
9971
var op = privmx.OptionalContainerPolicy()
10072
if let policies{
@@ -285,7 +257,7 @@ public actor StreamApi: @unchecked Sendable{
285257
}
286258

287259

288-
/*public func listStreams(
260+
public func listStreams(
289261
in streamRoomId: String
290262
) throws -> privmx.StreamVector {
291263
let res = api.listStreams(std.string(streamRoomId))
@@ -300,7 +272,6 @@ public actor StreamApi: @unchecked Sendable{
300272
}
301273
return result
302274
}
303-
*/
304275

305276
public func unpublishStream(
306277
localStreamId: Int64
@@ -507,4 +478,28 @@ public extension EventHandler{
507478
return result
508479
}
509480
}
481+
482+
import os.lock
483+
public final class Mutex<T>:Sendable{
484+
init(value: T.Type) {
485+
self._value = value
486+
}
487+
let lock = OSAllocatedUnfairLock()
488+
nonisolated(unsafe) var _value : T.Type
489+
490+
var value: T.Type {
491+
get {
492+
_value
493+
494+
}
495+
set {
496+
lock.lock()
497+
_value = newValue
498+
lock.unlock()
499+
}
500+
}
501+
502+
}
503+
504+
510505
#endif // Streams

0 commit comments

Comments
 (0)