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