RTCPeerConnection.h 16 KB

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