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.

421 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2012, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // This file contains the PeerConnection interface as defined in
  28. // http://dev.w3.org/2011/webrtc/editor/webrtc.html#peer-to-peer-connections.
  29. // Applications must use this interface to implement peerconnection.
  30. // PeerConnectionFactory class provides factory methods to create
  31. // peerconnection, mediastream and media tracks objects.
  32. //
  33. // The Following steps are needed to setup a typical call using Jsep.
  34. // 1. Create a PeerConnectionFactoryInterface. Check constructors for more
  35. // information about input parameters.
  36. // 2. Create a PeerConnection object. Provide a configuration string which
  37. // points either to stun or turn server to generate ICE candidates and provide
  38. // an object that implements the PeerConnectionObserver interface.
  39. // 3. Create local MediaStream and MediaTracks using the PeerConnectionFactory
  40. // and add it to PeerConnection by calling AddStream.
  41. // 4. Create an offer and serialize it and send it to the remote peer.
  42. // 5. Once an ice candidate have been found PeerConnection will call the
  43. // observer function OnIceCandidate. The candidates must also be serialized and
  44. // sent to the remote peer.
  45. // 6. Once an answer is received from the remote peer, call
  46. // SetLocalSessionDescription with the offer and SetRemoteSessionDescription
  47. // with the remote answer.
  48. // 7. Once a remote candidate is received from the remote peer, provide it to
  49. // the peerconnection by calling AddIceCandidate.
  50. // The Receiver of a call can decide to accept or reject the call.
  51. // This decision will be taken by the application not peerconnection.
  52. // If application decides to accept the call
  53. // 1. Create PeerConnectionFactoryInterface if it doesn't exist.
  54. // 2. Create a new PeerConnection.
  55. // 3. Provide the remote offer to the new PeerConnection object by calling
  56. // SetRemoteSessionDescription.
  57. // 4. Generate an answer to the remote offer by calling CreateAnswer and send it
  58. // back to the remote peer.
  59. // 5. Provide the local answer to the new PeerConnection by calling
  60. // SetLocalSessionDescription with the answer.
  61. // 6. Provide the remote ice candidates by calling AddIceCandidate.
  62. // 7. Once a candidate have been found PeerConnection will call the observer
  63. // function OnIceCandidate. Send these candidates to the remote peer.
  64. #ifndef TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_
  65. #define TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_
  66. #include <string>
  67. #include <vector>
  68. #include "talk/app/webrtc/datachannelinterface.h"
  69. #include "talk/app/webrtc/dtmfsenderinterface.h"
  70. #include "talk/app/webrtc/jsep.h"
  71. #include "talk/app/webrtc/statstypes.h"
  72. #include "talk/app/webrtc/mediastreaminterface.h"
  73. #include "talk/base/socketaddress.h"
  74. namespace talk_base {
  75. class Thread;
  76. }
  77. namespace cricket {
  78. class PortAllocator;
  79. }
  80. namespace webrtc {
  81. class AudioDeviceModule;
  82. // MediaStream container interface.
  83. class StreamCollectionInterface : public talk_base::RefCountInterface {
  84. public:
  85. // TODO(ronghuawu): Update the function names to c++ style, e.g. find -> Find.
  86. virtual size_t count() = 0;
  87. virtual MediaStreamInterface* at(size_t index) = 0;
  88. virtual MediaStreamInterface* find(const std::string& label) = 0;
  89. virtual MediaStreamTrackInterface* FindAudioTrack(
  90. const std::string& id) = 0;
  91. virtual MediaStreamTrackInterface* FindVideoTrack(
  92. const std::string& id) = 0;
  93. protected:
  94. // Dtor protected as objects shouldn't be deleted via this interface.
  95. ~StreamCollectionInterface() {}
  96. };
  97. class StatsObserver : public talk_base::RefCountInterface {
  98. public:
  99. virtual void OnComplete(const std::vector<StatsReport>& reports) = 0;
  100. protected:
  101. virtual ~StatsObserver() {}
  102. };
  103. class PeerConnectionInterface : public talk_base::RefCountInterface {
  104. public:
  105. // See http://dev.w3.org/2011/webrtc/editor/webrtc.html#state-definitions .
  106. enum SignalingState {
  107. kStable,
  108. kHaveLocalOffer,
  109. kHaveLocalPrAnswer,
  110. kHaveRemoteOffer,
  111. kHaveRemotePrAnswer,
  112. kClosed,
  113. };
  114. // TODO(bemasc): Remove IceState when callers are changed to
  115. // IceConnection/GatheringState.
  116. enum IceState {
  117. kIceNew,
  118. kIceGathering,
  119. kIceWaiting,
  120. kIceChecking,
  121. kIceConnected,
  122. kIceCompleted,
  123. kIceFailed,
  124. kIceClosed,
  125. };
  126. enum IceGatheringState {
  127. kIceGatheringNew,
  128. kIceGatheringGathering,
  129. kIceGatheringComplete
  130. };
  131. enum IceConnectionState {
  132. kIceConnectionNew,
  133. kIceConnectionChecking,
  134. kIceConnectionConnected,
  135. kIceConnectionCompleted,
  136. kIceConnectionFailed,
  137. kIceConnectionDisconnected,
  138. kIceConnectionClosed,
  139. };
  140. struct IceServer {
  141. std::string uri;
  142. std::string password;
  143. };
  144. typedef std::vector<IceServer> IceServers;
  145. // Accessor methods to active local streams.
  146. virtual talk_base::scoped_refptr<StreamCollectionInterface>
  147. local_streams() = 0;
  148. // Accessor methods to remote streams.
  149. virtual talk_base::scoped_refptr<StreamCollectionInterface>
  150. remote_streams() = 0;
  151. // Add a new MediaStream to be sent on this PeerConnection.
  152. // Note that a SessionDescription negotiation is needed before the
  153. // remote peer can receive the stream.
  154. virtual bool AddStream(MediaStreamInterface* stream,
  155. const MediaConstraintsInterface* constraints) = 0;
  156. // Remove a MediaStream from this PeerConnection.
  157. // Note that a SessionDescription negotiation is need before the
  158. // remote peer is notified.
  159. virtual void RemoveStream(MediaStreamInterface* stream) = 0;
  160. // Returns pointer to the created DtmfSender on success.
  161. // Otherwise returns NULL.
  162. virtual talk_base::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
  163. AudioTrackInterface* track) = 0;
  164. virtual bool GetStats(StatsObserver* observer,
  165. MediaStreamTrackInterface* track) = 0;
  166. virtual talk_base::scoped_refptr<DataChannelInterface> CreateDataChannel(
  167. const std::string& label,
  168. const DataChannelInit* config) = 0;
  169. virtual const SessionDescriptionInterface* local_description() const = 0;
  170. virtual const SessionDescriptionInterface* remote_description() const = 0;
  171. // Create a new offer.
  172. // The CreateSessionDescriptionObserver callback will be called when done.
  173. virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
  174. const MediaConstraintsInterface* constraints) = 0;
  175. // Create an answer to an offer.
  176. // The CreateSessionDescriptionObserver callback will be called when done.
  177. virtual void CreateAnswer(CreateSessionDescriptionObserver* observer,
  178. const MediaConstraintsInterface* constraints) = 0;
  179. // Sets the local session description.
  180. // JsepInterface takes the ownership of |desc| even if it fails.
  181. // The |observer| callback will be called when done.
  182. virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
  183. SessionDescriptionInterface* desc) = 0;
  184. // Sets the remote session description.
  185. // JsepInterface takes the ownership of |desc| even if it fails.
  186. // The |observer| callback will be called when done.
  187. virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
  188. SessionDescriptionInterface* desc) = 0;
  189. // Restarts or updates the ICE Agent process of gathering local candidates
  190. // and pinging remote candidates.
  191. virtual bool UpdateIce(const IceServers& configuration,
  192. const MediaConstraintsInterface* constraints) = 0;
  193. // Provides a remote candidate to the ICE Agent.
  194. // A copy of the |candidate| will be created and added to the remote
  195. // description. So the caller of this method still has the ownership of the
  196. // |candidate|.
  197. // TODO(ronghuawu): Consider to change this so that the AddIceCandidate will
  198. // take the ownership of the |candidate|.
  199. virtual bool AddIceCandidate(const IceCandidateInterface* candidate) = 0;
  200. // Deprecated, please use SignalingState instead.
  201. // TODO(perkj): Remove ready_state when callers are changed.
  202. typedef SignalingState ReadyState;
  203. virtual ReadyState ready_state() = 0;
  204. // Returns the current SignalingState.
  205. virtual SignalingState signaling_state() = 0;
  206. // TODO(bemasc): Remove ice_state when callers are changed to
  207. // IceConnection/GatheringState.
  208. // Returns the current IceState.
  209. virtual IceState ice_state() = 0;
  210. virtual IceConnectionState ice_connection_state() = 0;
  211. virtual IceGatheringState ice_gathering_state() = 0;
  212. protected:
  213. // Dtor protected as objects shouldn't be deleted via this interface.
  214. ~PeerConnectionInterface() {}
  215. };
  216. // PeerConnection callback interface. Application should implement these
  217. // methods.
  218. class PeerConnectionObserver {
  219. public:
  220. enum StateType {
  221. kSignalingState,
  222. kIceState,
  223. };
  224. virtual void OnError() = 0;
  225. // Triggered when the SignalingState changed.
  226. virtual void OnSignalingChange(
  227. PeerConnectionInterface::SignalingState new_state) {}
  228. // Triggered when SignalingState or IceState have changed.
  229. // TODO(bemasc): Remove once callers transition to OnSignalingChange.
  230. virtual void OnStateChange(StateType state_changed) {}
  231. // Triggered when media is received on a new stream from remote peer.
  232. virtual void OnAddStream(MediaStreamInterface* stream) = 0;
  233. // Triggered when a remote peer close a stream.
  234. virtual void OnRemoveStream(MediaStreamInterface* stream) = 0;
  235. // Triggered when a remote peer open a data channel.
  236. // TODO(perkj): Make pure virtual.
  237. virtual void OnDataChannel(DataChannelInterface* data_channel) {}
  238. // Triggered when renegotation is needed, for example the ICE has restarted.
  239. virtual void OnRenegotiationNeeded() {}
  240. // Called any time the IceConnectionState changes
  241. virtual void OnIceConnectionChange(
  242. PeerConnectionInterface::IceConnectionState new_state) {}
  243. // Called any time the IceGatheringState changes
  244. virtual void OnIceGatheringChange(
  245. PeerConnectionInterface::IceGatheringState new_state) {}
  246. // New Ice candidate have been found.
  247. virtual void OnIceCandidate(const IceCandidateInterface* candidate) = 0;
  248. // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
  249. // All Ice candidates have been found.
  250. virtual void OnIceComplete() {}
  251. protected:
  252. // Dtor protected as objects shouldn't be deleted via this interface.
  253. ~PeerConnectionObserver() {}
  254. };
  255. // Factory class used for creating cricket::PortAllocator that is used
  256. // for ICE negotiation.
  257. class PortAllocatorFactoryInterface : public talk_base::RefCountInterface {
  258. public:
  259. struct StunConfiguration {
  260. StunConfiguration(const std::string& address, int port)
  261. : server(address, port) {}
  262. // STUN server address and port.
  263. talk_base::SocketAddress server;
  264. };
  265. struct TurnConfiguration {
  266. TurnConfiguration(const std::string& address,
  267. int port,
  268. const std::string& username,
  269. const std::string& password)
  270. : server(address, port),
  271. username(username),
  272. password(password) {}
  273. talk_base::SocketAddress server;
  274. std::string username;
  275. std::string password;
  276. };
  277. virtual cricket::PortAllocator* CreatePortAllocator(
  278. const std::vector<StunConfiguration>& stun_servers,
  279. const std::vector<TurnConfiguration>& turn_configurations) = 0;
  280. protected:
  281. PortAllocatorFactoryInterface() {}
  282. ~PortAllocatorFactoryInterface() {}
  283. };
  284. // PeerConnectionFactoryInterface is the factory interface use for creating
  285. // PeerConnection, MediaStream and media tracks.
  286. // PeerConnectionFactoryInterface will create required libjingle threads,
  287. // socket and network manager factory classes for networking.
  288. // If an application decides to provide its own threads and network
  289. // implementation of these classes it should use the alternate
  290. // CreatePeerConnectionFactory method which accepts threads as input and use the
  291. // CreatePeerConnection version that takes a PortAllocatorFactoryInterface as
  292. // argument.
  293. class PeerConnectionFactoryInterface : public talk_base::RefCountInterface {
  294. public:
  295. virtual talk_base::scoped_refptr<PeerConnectionInterface>
  296. CreatePeerConnection(
  297. const PeerConnectionInterface::IceServers& configuration,
  298. const MediaConstraintsInterface* constraints,
  299. PeerConnectionObserver* observer) = 0;
  300. virtual talk_base::scoped_refptr<PeerConnectionInterface>
  301. CreatePeerConnection(
  302. const PeerConnectionInterface::IceServers& configuration,
  303. const MediaConstraintsInterface* constraints,
  304. PortAllocatorFactoryInterface* allocator_factory,
  305. PeerConnectionObserver* observer) = 0;
  306. virtual talk_base::scoped_refptr<LocalMediaStreamInterface>
  307. CreateLocalMediaStream(const std::string& label) = 0;
  308. // Creates a AudioSourceInterface.
  309. // |constraints| decides audio processing settings but can be NULL.
  310. virtual talk_base::scoped_refptr<AudioSourceInterface> CreateAudioSource(
  311. const MediaConstraintsInterface* constraints) = 0;
  312. // Creates a VideoSourceInterface. The new source take ownership of
  313. // |capturer|. |constraints| decides video resolution and frame rate but can
  314. // be NULL.
  315. virtual talk_base::scoped_refptr<VideoSourceInterface> CreateVideoSource(
  316. cricket::VideoCapturer* capturer,
  317. const MediaConstraintsInterface* constraints) = 0;
  318. // Creates a new local VideoTrack. The same |source| can be used in several
  319. // tracks.
  320. virtual talk_base::scoped_refptr<VideoTrackInterface>
  321. CreateVideoTrack(const std::string& label,
  322. VideoSourceInterface* source) = 0;
  323. // Creates an new AudioTrack. At the moment |source| can be NULL.
  324. virtual talk_base::scoped_refptr<AudioTrackInterface>
  325. CreateAudioTrack(const std::string& label,
  326. AudioSourceInterface* source) = 0;
  327. // Deprecated: Please use the version that take a source as input.
  328. virtual talk_base::scoped_refptr<LocalVideoTrackInterface>
  329. CreateLocalVideoTrack(const std::string& label,
  330. cricket::VideoCapturer* video_device) = 0;
  331. // Deprecated: Please use the version that take a source as input.
  332. virtual talk_base::scoped_refptr<LocalAudioTrackInterface>
  333. CreateLocalAudioTrack(const std::string& label,
  334. AudioDeviceModule* audio_device) = 0;
  335. protected:
  336. // Dtor and ctor protected as objects shouldn't be created or deleted via
  337. // this interface.
  338. PeerConnectionFactoryInterface() {}
  339. ~PeerConnectionFactoryInterface() {} // NOLINT
  340. };
  341. // Create a new instance of PeerConnectionFactoryInterface.
  342. talk_base::scoped_refptr<PeerConnectionFactoryInterface>
  343. CreatePeerConnectionFactory();
  344. // Create a new instance of PeerConnectionFactoryInterface.
  345. // Ownership of |factory| and |default_adm| is transferred to the returned
  346. // factory.
  347. talk_base::scoped_refptr<PeerConnectionFactoryInterface>
  348. CreatePeerConnectionFactory(talk_base::Thread* worker_thread,
  349. talk_base::Thread* signaling_thread,
  350. AudioDeviceModule* default_adm);
  351. // TODO(perkj): The JsepInterface has been combined with
  352. // PeerConnectionInterface. Remove the below interface once no clients use
  353. // JsepInterface::IceServer.
  354. class JsepInterface {
  355. public:
  356. typedef PeerConnectionInterface::IceServer IceServer;
  357. typedef std::vector<PeerConnectionInterface::IceServer> IceServers;
  358. };
  359. } // namespace webrtc
  360. #endif // TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_