-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJAHPeerConnection.m
132 lines (104 loc) · 5.48 KB
/
JAHPeerConnection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// JAHPeerConnection.m
//
// Copyright (c) 2015 Jon Hjelle. All rights reserved.
//
#import "JAHPeerConnection.h"
#import "RTCSessionDescriptionDelegate.h"
#import "RTCStatsDelegate.h"
#import "RTCPeerConnectionFactory.h"
#import "RTCMediaConstraints.h"
#import "RTCPair.h"
@interface JAHPeerConnection () <RTCPeerConnectionDelegate, RTCSessionDescriptionDelegate, RTCStatsDelegate>
@property (nonatomic, strong) RTCPeerConnection *peerConnection;
@property (nonatomic, strong) NSMutableArray* operationBlocks;
@property (nonatomic, strong) NSMutableArray* statBlocks;
@end
@implementation JAHPeerConnection
- (instancetype)initWithICEServers:(NSArray*)servers constraints:(RTCMediaConstraints*)constraints peerConnectionFactory:(RTCPeerConnectionFactory*)peerConnectionFactory {
self = [super init];
if (self) {
if (!constraints) {
RTCPair* sctpConstraint = [[RTCPair alloc] initWithKey:@"internalSctpDataChannels" value:@"true"];
RTCPair* dtlsConstraint = [[RTCPair alloc] initWithKey:@"DtlsSrtpKeyAgreement" value:@"true"];
constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:@[sctpConstraint, dtlsConstraint]];
}
_peerConnection = [peerConnectionFactory peerConnectionWithICEServers:servers constraints:constraints delegate:self];
_operationBlocks = [NSMutableArray array];
_statBlocks = [NSMutableArray array];
}
return self;
}
- (id)forwardingTargetForSelector:(SEL)aSelector {
return self.peerConnection;
}
#pragma mark - Wrapper API
- (void)createOfferWithConstraints:(RTCMediaConstraints*)constraints completionHandler:(void (^)(RTCSessionDescription* sessionDescription, NSError* error))completionHandler {
[self.operationBlocks addObject:completionHandler];
[self.peerConnection createOfferWithDelegate:self constraints:constraints];
}
- (void)createAnswerWithConstraints:(RTCMediaConstraints*)constraints completionHandler:(void (^)(RTCSessionDescription* sessionDescription, NSError* error))completionHandler {
[self.operationBlocks addObject:completionHandler];
[self.peerConnection createAnswerWithDelegate:self constraints:constraints];
}
- (void)setLocalDescription:(RTCSessionDescription*)sdp completionHandler:(void (^)(NSError* error))completionHandler {
[self.operationBlocks addObject:completionHandler];
[self.peerConnection setLocalDescriptionWithDelegate:self sessionDescription:sdp];
}
- (void)setRemoteDescription:(RTCSessionDescription*)sdp completionHandler:(void (^)(NSError* error))completionHandler {
[self.operationBlocks addObject:completionHandler];
[self.peerConnection setRemoteDescriptionWithDelegate:self sessionDescription:sdp];
}
- (BOOL)getStatsForMediaStreamTrack:(RTCMediaStreamTrack*)mediaStreamTrack statsOutputLevel:(RTCStatsOutputLevel)statsOutputLevel completionHandler:(void (^)(NSArray* stats))completionHandler {
[self.statBlocks addObject:completionHandler];
return [self.peerConnection getStatsWithDelegate:self mediaStreamTrack:mediaStreamTrack statsOutputLevel:statsOutputLevel];
}
#pragma mark - RTCSessionDescriptionDelegate methods
- (void)peerConnection:(RTCPeerConnection*)peerConnection didCreateSessionDescription:(RTCSessionDescription*)sdp error:(NSError*)error {
void (^completion)(RTCSessionDescription* sessionDescription, NSError* error) = [self.operationBlocks firstObject];
if (completion) {
[self.operationBlocks removeObjectAtIndex:0];
completion(sdp, error);
}
}
- (void)peerConnection:(RTCPeerConnection*)peerConnection didSetSessionDescriptionWithError:(NSError*)error {
void (^completion)(NSError* error) = [self.operationBlocks firstObject];
if (completion) {
[self.operationBlocks removeObjectAtIndex:0];
completion(error);
}
}
#pragma mark - RTCStatsDelegate methods
- (void)peerConnection:(RTCPeerConnection*)peerConnection didGetStats:(NSArray*)stats {
void (^completion)(NSArray* stats) = [self.statBlocks firstObject];
if (completion) {
[self.statBlocks removeObjectAtIndex:0];
completion(stats);
}
}
#pragma mark - RTCPeerConnectionDelegate methods
- (void)peerConnection:(RTCPeerConnection*)peerConnection signalingStateChanged:(RTCSignalingState)stateChanged {
[self.delegate peerConnection:self signalingStateChanged:stateChanged];
}
- (void)peerConnection:(RTCPeerConnection*)peerConnection addedStream:(RTCMediaStream*)stream {
[self.delegate peerConnection:self addedStream:stream];
}
- (void)peerConnection:(RTCPeerConnection*)peerConnection removedStream:(RTCMediaStream*)stream {
[self.delegate peerConnection:self removedStream:stream];
}
- (void)peerConnectionOnRenegotiationNeeded:(RTCPeerConnection*)peerConnection {
[self.delegate peerConnectionOnRenegotiationNeeded:self];
}
- (void)peerConnection:(RTCPeerConnection*)peerConnection iceConnectionChanged:(RTCICEConnectionState)newState {
[self.delegate peerConnection:self iceConnectionChanged:newState];
}
- (void)peerConnection:(RTCPeerConnection*)peerConnection iceGatheringChanged:(RTCICEGatheringState)newState {
[self.delegate peerConnection:self iceGatheringChanged:newState];
}
- (void)peerConnection:(RTCPeerConnection*)peerConnection gotICECandidate:(RTCICECandidate*)candidate {
[self.delegate peerConnection:self gotICECandidate:candidate];
}
- (void)peerConnection:(RTCPeerConnection*)peerConnection didOpenDataChannel:(RTCDataChannel*)dataChannel {
[self.delegate peerConnection:self didOpenDataChannel:dataChannel];
}
@end