RTCPeerConnection.h 14 KB

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