blob: 6956555ce3728b0cc6df971be231316d80be2d8e [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
150@end
151
Mirko Bonadeie8d57242018-09-17 10:22:56 +0200152RTC_OBJC_EXPORT
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200153@interface RTCPeerConnection : NSObject
154
155/** The object that will be notifed about events such as state changes and
156 * streams being added or removed.
157 */
158@property(nonatomic, weak, nullable) id<RTCPeerConnectionDelegate> delegate;
159/** This property is not available with RTCSdpSemanticsUnifiedPlan. Please use
160 * |senders| instead.
161 */
162@property(nonatomic, readonly) NSArray<RTCMediaStream *> *localStreams;
163@property(nonatomic, readonly, nullable) RTCSessionDescription *localDescription;
164@property(nonatomic, readonly, nullable) RTCSessionDescription *remoteDescription;
165@property(nonatomic, readonly) RTCSignalingState signalingState;
166@property(nonatomic, readonly) RTCIceConnectionState iceConnectionState;
Jonas Olssoncfddbb72018-11-15 16:52:45 +0100167@property(nonatomic, readonly) RTCPeerConnectionState connectionState;
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200168@property(nonatomic, readonly) RTCIceGatheringState iceGatheringState;
169@property(nonatomic, readonly, copy) RTCConfiguration *configuration;
170
171/** Gets all RTCRtpSenders associated with this peer connection.
172 * Note: reading this property returns different instances of RTCRtpSender.
173 * Use isEqual: instead of == to compare RTCRtpSender instances.
174 */
175@property(nonatomic, readonly) NSArray<RTCRtpSender *> *senders;
176
177/** Gets all RTCRtpReceivers associated with this peer connection.
178 * Note: reading this property returns different instances of RTCRtpReceiver.
179 * Use isEqual: instead of == to compare RTCRtpReceiver instances.
180 */
181@property(nonatomic, readonly) NSArray<RTCRtpReceiver *> *receivers;
182
183/** Gets all RTCRtpTransceivers associated with this peer connection.
184 * Note: reading this property returns different instances of
185 * RTCRtpTransceiver. Use isEqual: instead of == to compare RTCRtpTransceiver
186 * instances.
187 * This is only available with RTCSdpSemanticsUnifiedPlan specified.
188 */
189@property(nonatomic, readonly) NSArray<RTCRtpTransceiver *> *transceivers;
190
191- (instancetype)init NS_UNAVAILABLE;
192
193/** Sets the PeerConnection's global configuration to |configuration|.
194 * Any changes to STUN/TURN servers or ICE candidate policy will affect the
195 * next gathering phase, and cause the next call to createOffer to generate
196 * new ICE credentials. Note that the BUNDLE and RTCP-multiplexing policies
197 * cannot be changed with this method.
198 */
199- (BOOL)setConfiguration:(RTCConfiguration *)configuration;
200
201/** Terminate all media and close the transport. */
202- (void)close;
203
204/** Provide a remote candidate to the ICE Agent. */
205- (void)addIceCandidate:(RTCIceCandidate *)candidate;
206
207/** Remove a group of remote candidates from the ICE Agent. */
208- (void)removeIceCandidates:(NSArray<RTCIceCandidate *> *)candidates;
209
210/** Add a new media stream to be sent on this peer connection.
211 * This method is not supported with RTCSdpSemanticsUnifiedPlan. Please use
212 * addTrack instead.
213 */
214- (void)addStream:(RTCMediaStream *)stream;
215
216/** Remove the given media stream from this peer connection.
217 * This method is not supported with RTCSdpSemanticsUnifiedPlan. Please use
218 * removeTrack instead.
219 */
220- (void)removeStream:(RTCMediaStream *)stream;
221
222/** Add a new media stream track to be sent on this peer connection, and return
223 * the newly created RTCRtpSender. The RTCRtpSender will be associated with
224 * the streams specified in the |streamIds| list.
225 *
226 * Errors: If an error occurs, returns nil. An error can occur if:
227 * - A sender already exists for the track.
228 * - The peer connection is closed.
229 */
230- (RTCRtpSender *)addTrack:(RTCMediaStreamTrack *)track streamIds:(NSArray<NSString *> *)streamIds;
231
232/** With PlanB semantics, removes an RTCRtpSender from this peer connection.
233 *
234 * With UnifiedPlan semantics, sets sender's track to null and removes the
235 * send component from the associated RTCRtpTransceiver's direction.
236 *
237 * Returns YES on success.
238 */
239- (BOOL)removeTrack:(RTCRtpSender *)sender;
240
241/** addTransceiver creates a new RTCRtpTransceiver and adds it to the set of
242 * transceivers. Adding a transceiver will cause future calls to CreateOffer
243 * to add a media description for the corresponding transceiver.
244 *
245 * The initial value of |mid| in the returned transceiver is nil. Setting a
246 * new session description may change it to a non-nil value.
247 *
248 * https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-addtransceiver
249 *
250 * Optionally, an RtpTransceiverInit structure can be specified to configure
251 * the transceiver from construction. If not specified, the transceiver will
252 * default to having a direction of kSendRecv and not be part of any streams.
253 *
254 * These methods are only available when Unified Plan is enabled (see
255 * RTCConfiguration).
256 */
257
258/** Adds a transceiver with a sender set to transmit the given track. The kind
259 * of the transceiver (and sender/receiver) will be derived from the kind of
260 * the track.
261 */
262- (RTCRtpTransceiver *)addTransceiverWithTrack:(RTCMediaStreamTrack *)track;
263- (RTCRtpTransceiver *)addTransceiverWithTrack:(RTCMediaStreamTrack *)track
264 init:(RTCRtpTransceiverInit *)init;
265
266/** Adds a transceiver with the given kind. Can either be RTCRtpMediaTypeAudio
267 * or RTCRtpMediaTypeVideo.
268 */
269- (RTCRtpTransceiver *)addTransceiverOfType:(RTCRtpMediaType)mediaType;
270- (RTCRtpTransceiver *)addTransceiverOfType:(RTCRtpMediaType)mediaType
271 init:(RTCRtpTransceiverInit *)init;
272
273/** Generate an SDP offer. */
274- (void)offerForConstraints:(RTCMediaConstraints *)constraints
275 completionHandler:(nullable void (^)(RTCSessionDescription *_Nullable sdp,
276 NSError *_Nullable error))completionHandler;
277
278/** Generate an SDP answer. */
279- (void)answerForConstraints:(RTCMediaConstraints *)constraints
280 completionHandler:(nullable void (^)(RTCSessionDescription *_Nullable sdp,
281 NSError *_Nullable error))completionHandler;
282
283/** Apply the supplied RTCSessionDescription as the local description. */
284- (void)setLocalDescription:(RTCSessionDescription *)sdp
285 completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler;
286
287/** Apply the supplied RTCSessionDescription as the remote description. */
288- (void)setRemoteDescription:(RTCSessionDescription *)sdp
289 completionHandler:(nullable void (^)(NSError *_Nullable error))completionHandler;
290
291/** Limits the bandwidth allocated for all RTP streams sent by this
292 * PeerConnection. Nil parameters will be unchanged. Setting
293 * |currentBitrateBps| will force the available bitrate estimate to the given
294 * value. Returns YES if the parameters were successfully updated.
295 */
296- (BOOL)setBweMinBitrateBps:(nullable NSNumber *)minBitrateBps
297 currentBitrateBps:(nullable NSNumber *)currentBitrateBps
298 maxBitrateBps:(nullable NSNumber *)maxBitrateBps;
299
300/** Start or stop recording an Rtc EventLog. */
301- (BOOL)startRtcEventLogWithFilePath:(NSString *)filePath maxSizeInBytes:(int64_t)maxSizeInBytes;
302- (void)stopRtcEventLog;
303
304@end
305
306@interface RTCPeerConnection (Media)
307
308/** Create an RTCRtpSender with the specified kind and media stream ID.
309 * See RTCMediaStreamTrack.h for available kinds.
310 * This method is not supported with RTCSdpSemanticsUnifiedPlan. Please use
311 * addTransceiver instead.
312 */
313- (RTCRtpSender *)senderWithKind:(NSString *)kind streamId:(NSString *)streamId;
314
315@end
316
317@interface RTCPeerConnection (DataChannel)
318
319/** Create a new data channel with the given label and configuration. */
320- (nullable RTCDataChannel *)dataChannelForLabel:(NSString *)label
321 configuration:(RTCDataChannelConfiguration *)configuration;
322
323@end
324
Peter Hanspersbed86042019-02-21 17:27:09 +0100325typedef void (^RTCStatisticsCompletionHandler)(RTCStatisticsReport *);
326
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200327@interface RTCPeerConnection (Stats)
328
329/** Gather stats for the given RTCMediaStreamTrack. If |mediaStreamTrack| is nil
330 * statistics are gathered for all tracks.
331 */
332- (void)statsForTrack:(nullable RTCMediaStreamTrack *)mediaStreamTrack
333 statsOutputLevel:(RTCStatsOutputLevel)statsOutputLevel
334 completionHandler:(nullable void (^)(NSArray<RTCLegacyStatsReport *> *stats))completionHandler;
335
Peter Hanspersbed86042019-02-21 17:27:09 +0100336/** Gather statistic through the v2 statistics API. */
337- (void)statisticsWithCompletionHandler:(RTCStatisticsCompletionHandler)completionHandler;
338
Peter Hansperse12a1c72019-02-26 16:39:48 +0100339/** Spec-compliant getStats() performing the stats selection algorithm with the
340 * sender.
341 */
342- (void)statisticsForSender:(RTCRtpSender *)sender
343 completionHandler:(RTCStatisticsCompletionHandler)completionHandler;
344
345/** Spec-compliant getStats() performing the stats selection algorithm with the
346 * receiver.
347 */
348- (void)statisticsForReceiver:(RTCRtpReceiver *)receiver
349 completionHandler:(RTCStatisticsCompletionHandler)completionHandler;
350
Anders Carlsson7bca8ca2018-08-30 09:30:29 +0200351@end
352
353NS_ASSUME_NONNULL_END