blob: 012295c24151e5443bbe2cba7ca7248c9495a250 [file] [log] [blame]
Anders Carlsson7bca8ca2018-08-30 09:30:29 +02001/*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import <Foundation/Foundation.h>
12
13#import "RTCMacros.h"
14
15@class RTCConfiguration;
16@class RTCDataChannel;
17@class RTCDataChannelConfiguration;
18@class RTCIceCandidate;
19@class RTCMediaConstraints;
20@class RTCMediaStream;
21@class RTCMediaStreamTrack;
22@class RTCPeerConnectionFactory;
23@class RTCRtpReceiver;
24@class RTCRtpSender;
25@class RTCRtpTransceiver;
26@class RTCRtpTransceiverInit;
27@class RTCSessionDescription;
Peter Hanspersbed86042019-02-21 17:27:09 +010028@class RTCStatisticsReport;
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020029@class RTCLegacyStatsReport;
30
31typedef NS_ENUM(NSInteger, RTCRtpMediaType);
32
33NS_ASSUME_NONNULL_BEGIN
34
35extern NSString *const kRTCPeerConnectionErrorDomain;
36extern int const kRTCSessionDescriptionErrorCode;
37
38/** Represents the signaling state of the peer connection. */
39typedef NS_ENUM(NSInteger, RTCSignalingState) {
40 RTCSignalingStateStable,
41 RTCSignalingStateHaveLocalOffer,
42 RTCSignalingStateHaveLocalPrAnswer,
43 RTCSignalingStateHaveRemoteOffer,
44 RTCSignalingStateHaveRemotePrAnswer,
45 // Not an actual state, represents the total number of states.
46 RTCSignalingStateClosed,
47};
48
49/** Represents the ice connection state of the peer connection. */
50typedef NS_ENUM(NSInteger, RTCIceConnectionState) {
51 RTCIceConnectionStateNew,
52 RTCIceConnectionStateChecking,
53 RTCIceConnectionStateConnected,
54 RTCIceConnectionStateCompleted,
55 RTCIceConnectionStateFailed,
56 RTCIceConnectionStateDisconnected,
57 RTCIceConnectionStateClosed,
58 RTCIceConnectionStateCount,
59};
60
Jonas Olssoncfddbb72018-11-15 16:52:45 +010061/** Represents the combined ice+dtls connection state of the peer connection. */
62typedef NS_ENUM(NSInteger, RTCPeerConnectionState) {
63 RTCPeerConnectionStateNew,
64 RTCPeerConnectionStateConnecting,
65 RTCPeerConnectionStateConnected,
66 RTCPeerConnectionStateDisconnected,
67 RTCPeerConnectionStateFailed,
68 RTCPeerConnectionStateClosed,
69};
70
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020071/** Represents the ice gathering state of the peer connection. */
72typedef NS_ENUM(NSInteger, RTCIceGatheringState) {
73 RTCIceGatheringStateNew,
74 RTCIceGatheringStateGathering,
75 RTCIceGatheringStateComplete,
76};
77
78/** Represents the stats output level. */
79typedef NS_ENUM(NSInteger, RTCStatsOutputLevel) {
80 RTCStatsOutputLevelStandard,
81 RTCStatsOutputLevelDebug,
82};
83
84@class RTCPeerConnection;
85
Mirko Bonadeie8d57242018-09-17 10:22:56 +020086RTC_OBJC_EXPORT
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020087@protocol RTCPeerConnectionDelegate <NSObject>
88
89/** Called when the SignalingState changed. */
90- (void)peerConnection:(RTCPeerConnection *)peerConnection
91 didChangeSignalingState:(RTCSignalingState)stateChanged;
92
93/** Called when media is received on a new stream from remote peer. */
94- (void)peerConnection:(RTCPeerConnection *)peerConnection didAddStream:(RTCMediaStream *)stream;
95
96/** Called when a remote peer closes a stream.
97 * This is not called when RTCSdpSemanticsUnifiedPlan is specified.
98 */
99- (void)peerConnection:(RTCPeerConnection *)peerConnection didRemoveStream:(RTCMediaStream *)stream;
100
101/** Called when negotiation is needed, for example ICE has restarted. */
102- (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection;
103
104/** Called any time the IceConnectionState changes. */
105- (void)peerConnection:(RTCPeerConnection *)peerConnection
106 didChangeIceConnectionState:(RTCIceConnectionState)newState;
107
108/** Called any time the IceGatheringState changes. */
109- (void)peerConnection:(RTCPeerConnection *)peerConnection
110 didChangeIceGatheringState:(RTCIceGatheringState)newState;
111
112/** New ice candidate has been found. */
113- (void)peerConnection:(RTCPeerConnection *)peerConnection
114 didGenerateIceCandidate:(RTCIceCandidate *)candidate;
115
116/** Called when a group of local Ice candidates have been removed. */
117- (void)peerConnection:(RTCPeerConnection *)peerConnection
118 didRemoveIceCandidates:(NSArray<RTCIceCandidate *> *)candidates;
119
120/** New data channel has been opened. */
121- (void)peerConnection:(RTCPeerConnection *)peerConnection
122 didOpenDataChannel:(RTCDataChannel *)dataChannel;
123
124/** Called when signaling indicates a transceiver will be receiving media from
125 * the remote endpoint.
126 * This is only called with RTCSdpSemanticsUnifiedPlan specified.
127 */
128@optional
Qingsi Wang36e31472019-05-29 11:37:26 -0700129/** Called any time the IceConnectionState changes following standardized
130 * transition. */
131- (void)peerConnection:(RTCPeerConnection *)peerConnection
132 didChangeStandardizedIceConnectionState:(RTCIceConnectionState)newState;
133
Jonas Olssoncfddbb72018-11-15 16:52:45 +0100134/** Called any time the PeerConnectionState changes. */
135- (void)peerConnection:(RTCPeerConnection *)peerConnection
136 didChangeConnectionState:(RTCPeerConnectionState)newState;
137
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200138- (void)peerConnection:(RTCPeerConnection *)peerConnection
139 didStartReceivingOnTransceiver:(RTCRtpTransceiver *)transceiver;
140
141/** Called when a receiver and its track are created. */
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200142- (void)peerConnection:(RTCPeerConnection *)peerConnection
143 didAddReceiver:(RTCRtpReceiver *)rtpReceiver
144 streams:(NSArray<RTCMediaStream *> *)mediaStreams;
145
146/** Called when the receiver and its track are removed. */
147- (void)peerConnection:(RTCPeerConnection *)peerConnection
148 didRemoveReceiver:(RTCRtpReceiver *)rtpReceiver;
149
Alex Drake43faee02019-08-12 16:27:34 -0700150/** Called when the selected ICE candidate pair is changed. */
151- (void)peerConnection:(RTCPeerConnection *)peerConnection
152 didChangeLocalCandidate:(RTCIceCandidate *)local
153 remoteCandidate:(RTCIceCandidate *)remote
154 lastReceivedMs:(int)lastDataReceivedMs
155 changeReason:(NSString *)reason;
156
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200157@end
158
Mirko Bonadeie8d57242018-09-17 10:22:56 +0200159RTC_OBJC_EXPORT
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200160@interface RTCPeerConnection : NSObject
161
162/** The object that will be notifed about events such as state changes and
163 * streams being added or removed.
164 */
165@property(nonatomic, weak, nullable) id<RTCPeerConnectionDelegate> delegate;
166/** This property is not available with RTCSdpSemanticsUnifiedPlan. Please use
167 * |senders| instead.
168 */
169@property(nonatomic, readonly) NSArray<RTCMediaStream *> *localStreams;
170@property(nonatomic, readonly, nullable) RTCSessionDescription *localDescription;
171@property(nonatomic, readonly, nullable) RTCSessionDescription *remoteDescription;
172@property(nonatomic, readonly) RTCSignalingState signalingState;
173@property(nonatomic, readonly) RTCIceConnectionState iceConnectionState;
Jonas Olssoncfddbb72018-11-15 16:52:45 +0100174@property(nonatomic, readonly) RTCPeerConnectionState connectionState;
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200175@property(nonatomic, readonly) RTCIceGatheringState iceGatheringState;
176@property(nonatomic, readonly, copy) RTCConfiguration *configuration;
177
178/** Gets all RTCRtpSenders associated with this peer connection.
179 * Note: reading this property returns different instances of RTCRtpSender.
180 * Use isEqual: instead of == to compare RTCRtpSender instances.
181 */
182@property(nonatomic, readonly) NSArray<RTCRtpSender *> *senders;
183
184/** Gets all RTCRtpReceivers associated with this peer connection.
185 * Note: reading this property returns different instances of RTCRtpReceiver.
186 * Use isEqual: instead of == to compare RTCRtpReceiver instances.
187 */
188@property(nonatomic, readonly) NSArray<RTCRtpReceiver *> *receivers;
189
190/** Gets all RTCRtpTransceivers associated with this peer connection.
191 * Note: reading this property returns different instances of
192 * RTCRtpTransceiver. Use isEqual: instead of == to compare RTCRtpTransceiver
193 * instances.
194 * This is only available with RTCSdpSemanticsUnifiedPlan specified.
195 */
196@property(nonatomic, readonly) NSArray<RTCRtpTransceiver *> *transceivers;
197
198- (instancetype)init NS_UNAVAILABLE;
199
200/** Sets the PeerConnection's global configuration to |configuration|.
201 * Any changes to STUN/TURN servers or ICE candidate policy will affect the
202 * next gathering phase, and cause the next call to createOffer to generate
203 * new ICE credentials. Note that the BUNDLE and RTCP-multiplexing policies
204 * cannot be changed with this method.
205 */
206- (BOOL)setConfiguration:(RTCConfiguration *)configuration;
207
208/** Terminate all media and close the transport. */
209- (void)close;
210
211/** Provide a remote candidate to the ICE Agent. */
212- (void)addIceCandidate:(RTCIceCandidate *)candidate;
213
214/** Remove a group of remote candidates from the ICE Agent. */
215- (void)removeIceCandidates:(NSArray<RTCIceCandidate *> *)candidates;
216
217/** Add a new media stream to be sent on this peer connection.
218 * This method is not supported with RTCSdpSemanticsUnifiedPlan. Please use
219 * addTrack instead.
220 */
221- (void)addStream:(RTCMediaStream *)stream;
222
223/** Remove the given media stream from this peer connection.
224 * This method is not supported with RTCSdpSemanticsUnifiedPlan. Please use
225 * removeTrack instead.
226 */
227- (void)removeStream:(RTCMediaStream *)stream;
228
229/** Add a new media stream track to be sent on this peer connection, and return
230 * the newly created RTCRtpSender. The RTCRtpSender will be associated with
231 * the streams specified in the |streamIds| list.
232 *
233 * Errors: If an error occurs, returns nil. An error can occur if:
234 * - A sender already exists for the track.
235 * - The peer connection is closed.
236 */
237- (RTCRtpSender *)addTrack:(RTCMediaStreamTrack *)track streamIds:(NSArray<NSString *> *)streamIds;
238
239/** With PlanB semantics, removes an RTCRtpSender from this peer connection.
240 *
241 * With UnifiedPlan semantics, sets sender's track to null and removes the
242 * send component from the associated RTCRtpTransceiver's direction.
243 *
244 * Returns YES on success.
245 */
246- (BOOL)removeTrack:(RTCRtpSender *)sender;
247
248/** addTransceiver creates a new RTCRtpTransceiver and adds it to the set of
249 * transceivers. Adding a transceiver will cause future calls to CreateOffer
250 * to add a media description for the corresponding transceiver.
251 *
252 * The initial value of |mid| in the returned transceiver is nil. Setting a
253 * new session description may change it to a non-nil value.
254 *
255 * https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
256 *
257 * Optionally, an RtpTransceiverInit structure can be specified to configure
258 * the transceiver from construction. If not specified, the transceiver will
259 * default to having a direction of kSendRecv and not be part of any streams.
260 *
261 * These methods are only available when Unified Plan is enabled (see
262 * RTCConfiguration).
263 */
264
265/** Adds a transceiver with a sender set to transmit the given track. The kind
266 * of the transceiver (and sender/receiver) will be derived from the kind of
267 * the track.
268 */
269- (RTCRtpTransceiver *)addTransceiverWithTrack:(RTCMediaStreamTrack *)track;
270- (RTCRtpTransceiver *)addTransceiverWithTrack:(RTCMediaStreamTrack *)track
271 init:(RTCRtpTransceiverInit *)init;
272
273/** Adds a transceiver with the given kind. Can either be RTCRtpMediaTypeAudio
274 * or RTCRtpMediaTypeVideo.
275 */
276- (RTCRtpTransceiver *)addTransceiverOfType:(RTCRtpMediaType)mediaType;
277- (RTCRtpTransceiver *)addTransceiverOfType:(RTCRtpMediaType)mediaType
278 init:(RTCRtpTransceiverInit *)init;
279
280/** Generate an SDP offer. */
281- (void)offerForConstraints:(RTCMediaConstraints *)constraints
282 completionHandler:(nullable void (^)(RTCSessionDescription *_Nullable sdp,
283 NSError *_Nullable error))completionHandler;
284
285/** Generate an SDP answer. */
286- (void)answerForConstraints:(RTCMediaConstraints *)constraints
287 completionHandler:(nullable void (^)(RTCSessionDescription *_Nullable sdp,
288 NSError *_Nullable error))completionHandler;
289
290/** Apply the supplied RTCSessionDescription as the local description. */
291- (void)setLocalDescription:(RTCSessionDescription *)sdp
292 completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler;
293
294/** Apply the supplied RTCSessionDescription as the remote description. */
295- (void)setRemoteDescription:(RTCSessionDescription *)sdp
296 completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler;
297
298/** Limits the bandwidth allocated for all RTP streams sent by this
299 * PeerConnection. Nil parameters will be unchanged. Setting
300 * |currentBitrateBps| will force the available bitrate estimate to the given
301 * value. Returns YES if the parameters were successfully updated.
302 */
303- (BOOL)setBweMinBitrateBps:(nullable NSNumber *)minBitrateBps
304 currentBitrateBps:(nullable NSNumber *)currentBitrateBps
305 maxBitrateBps:(nullable NSNumber *)maxBitrateBps;
306
307/** Start or stop recording an Rtc EventLog. */
308- (BOOL)startRtcEventLogWithFilePath:(NSString *)filePath maxSizeInBytes:(int64_t)maxSizeInBytes;
309- (void)stopRtcEventLog;
310
311@end
312
313@interface RTCPeerConnection (Media)
314
315/** Create an RTCRtpSender with the specified kind and media stream ID.
316 * See RTCMediaStreamTrack.h for available kinds.
317 * This method is not supported with RTCSdpSemanticsUnifiedPlan. Please use
318 * addTransceiver instead.
319 */
320- (RTCRtpSender *)senderWithKind:(NSString *)kind streamId:(NSString *)streamId;
321
322@end
323
324@interface RTCPeerConnection (DataChannel)
325
326/** Create a new data channel with the given label and configuration. */
327- (nullable RTCDataChannel *)dataChannelForLabel:(NSString *)label
328 configuration:(RTCDataChannelConfiguration *)configuration;
329
330@end
331
Peter Hanspersbed86042019-02-21 17:27:09 +0100332typedef void (^RTCStatisticsCompletionHandler)(RTCStatisticsReport *);
333
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200334@interface RTCPeerConnection (Stats)
335
336/** Gather stats for the given RTCMediaStreamTrack. If |mediaStreamTrack| is nil
337 * statistics are gathered for all tracks.
338 */
339- (void)statsForTrack:(nullable RTCMediaStreamTrack *)mediaStreamTrack
340 statsOutputLevel:(RTCStatsOutputLevel)statsOutputLevel
341 completionHandler:(nullable void (^)(NSArray<RTCLegacyStatsReport *> *stats))completionHandler;
342
Peter Hanspersbed86042019-02-21 17:27:09 +0100343/** Gather statistic through the v2 statistics API. */
344- (void)statisticsWithCompletionHandler:(RTCStatisticsCompletionHandler)completionHandler;
345
Peter Hansperse12a1c72019-02-26 16:39:48 +0100346/** Spec-compliant getStats() performing the stats selection algorithm with the
347 * sender.
348 */
349- (void)statisticsForSender:(RTCRtpSender *)sender
350 completionHandler:(RTCStatisticsCompletionHandler)completionHandler;
351
352/** Spec-compliant getStats() performing the stats selection algorithm with the
353 * receiver.
354 */
355- (void)statisticsForReceiver:(RTCRtpReceiver *)receiver
356 completionHandler:(RTCStatisticsCompletionHandler)completionHandler;
357
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200358@end
359
360NS_ASSUME_NONNULL_END