RTCPeerConnection.h 17 KB

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