You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
14 KiB

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