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