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