blob: a21c48dfa8480213119555a52c1e6f9fde13d7ed [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
28// This file contains the PeerConnection interface as defined in
29// http://dev.w3.org/2011/webrtc/editor/webrtc.html#peer-to-peer-connections.
30// Applications must use this interface to implement peerconnection.
31// PeerConnectionFactory class provides factory methods to create
32// peerconnection, mediastream and media tracks objects.
33//
34// The Following steps are needed to setup a typical call using Jsep.
35// 1. Create a PeerConnectionFactoryInterface. Check constructors for more
36// information about input parameters.
37// 2. Create a PeerConnection object. Provide a configuration string which
38// points either to stun or turn server to generate ICE candidates and provide
39// an object that implements the PeerConnectionObserver interface.
40// 3. Create local MediaStream and MediaTracks using the PeerConnectionFactory
41// and add it to PeerConnection by calling AddStream.
42// 4. Create an offer and serialize it and send it to the remote peer.
43// 5. Once an ice candidate have been found PeerConnection will call the
44// observer function OnIceCandidate. The candidates must also be serialized and
45// sent to the remote peer.
46// 6. Once an answer is received from the remote peer, call
47// SetLocalSessionDescription with the offer and SetRemoteSessionDescription
48// with the remote answer.
49// 7. Once a remote candidate is received from the remote peer, provide it to
50// the peerconnection by calling AddIceCandidate.
51
52
53// The Receiver of a call can decide to accept or reject the call.
54// This decision will be taken by the application not peerconnection.
55// If application decides to accept the call
56// 1. Create PeerConnectionFactoryInterface if it doesn't exist.
57// 2. Create a new PeerConnection.
58// 3. Provide the remote offer to the new PeerConnection object by calling
59// SetRemoteSessionDescription.
60// 4. Generate an answer to the remote offer by calling CreateAnswer and send it
61// back to the remote peer.
62// 5. Provide the local answer to the new PeerConnection by calling
63// SetLocalSessionDescription with the answer.
64// 6. Provide the remote ice candidates by calling AddIceCandidate.
65// 7. Once a candidate have been found PeerConnection will call the observer
66// function OnIceCandidate. Send these candidates to the remote peer.
67
68#ifndef TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_
69#define TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_
70
71#include <string>
72#include <vector>
73
74#include "talk/app/webrtc/datachannelinterface.h"
75#include "talk/app/webrtc/dtmfsenderinterface.h"
76#include "talk/app/webrtc/jsep.h"
77#include "talk/app/webrtc/mediastreaminterface.h"
78#include "talk/app/webrtc/statstypes.h"
wu@webrtc.orga8910d22014-01-23 22:12:45 +000079#include "talk/base/fileutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080#include "talk/base/socketaddress.h"
81
82namespace talk_base {
83class Thread;
84}
85
86namespace cricket {
87class PortAllocator;
88class WebRtcVideoDecoderFactory;
89class WebRtcVideoEncoderFactory;
90}
91
92namespace webrtc {
93class AudioDeviceModule;
94class MediaConstraintsInterface;
95
96// MediaStream container interface.
97class StreamCollectionInterface : public talk_base::RefCountInterface {
98 public:
99 // TODO(ronghuawu): Update the function names to c++ style, e.g. find -> Find.
100 virtual size_t count() = 0;
101 virtual MediaStreamInterface* at(size_t index) = 0;
102 virtual MediaStreamInterface* find(const std::string& label) = 0;
103 virtual MediaStreamTrackInterface* FindAudioTrack(
104 const std::string& id) = 0;
105 virtual MediaStreamTrackInterface* FindVideoTrack(
106 const std::string& id) = 0;
107
108 protected:
109 // Dtor protected as objects shouldn't be deleted via this interface.
110 ~StreamCollectionInterface() {}
111};
112
113class StatsObserver : public talk_base::RefCountInterface {
114 public:
115 virtual void OnComplete(const std::vector<StatsReport>& reports) = 0;
116
117 protected:
118 virtual ~StatsObserver() {}
119};
120
121class PeerConnectionInterface : public talk_base::RefCountInterface {
122 public:
123 // See http://dev.w3.org/2011/webrtc/editor/webrtc.html#state-definitions .
124 enum SignalingState {
125 kStable,
126 kHaveLocalOffer,
127 kHaveLocalPrAnswer,
128 kHaveRemoteOffer,
129 kHaveRemotePrAnswer,
130 kClosed,
131 };
132
133 // TODO(bemasc): Remove IceState when callers are changed to
134 // IceConnection/GatheringState.
135 enum IceState {
136 kIceNew,
137 kIceGathering,
138 kIceWaiting,
139 kIceChecking,
140 kIceConnected,
141 kIceCompleted,
142 kIceFailed,
143 kIceClosed,
144 };
145
146 enum IceGatheringState {
147 kIceGatheringNew,
148 kIceGatheringGathering,
149 kIceGatheringComplete
150 };
151
152 enum IceConnectionState {
153 kIceConnectionNew,
154 kIceConnectionChecking,
155 kIceConnectionConnected,
156 kIceConnectionCompleted,
157 kIceConnectionFailed,
158 kIceConnectionDisconnected,
159 kIceConnectionClosed,
160 };
161
162 struct IceServer {
163 std::string uri;
164 std::string username;
165 std::string password;
166 };
167 typedef std::vector<IceServer> IceServers;
168
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000169 enum IceTransportsType {
170 kNone,
171 kRelay,
172 kNoHost,
173 kAll
174 };
175
176 struct RTCConfiguration {
177 IceTransportsType type;
178 IceServers servers;
179
180 RTCConfiguration() : type(kAll) {}
181 explicit RTCConfiguration(IceTransportsType type) : type(type) {}
182 };
183
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000184 // Used by GetStats to decide which stats to include in the stats reports.
185 // |kStatsOutputLevelStandard| includes the standard stats for Javascript API;
186 // |kStatsOutputLevelDebug| includes both the standard stats and additional
187 // stats for debugging purposes.
188 enum StatsOutputLevel {
189 kStatsOutputLevelStandard,
190 kStatsOutputLevelDebug,
191 };
192
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 // Accessor methods to active local streams.
194 virtual talk_base::scoped_refptr<StreamCollectionInterface>
195 local_streams() = 0;
196
197 // Accessor methods to remote streams.
198 virtual talk_base::scoped_refptr<StreamCollectionInterface>
199 remote_streams() = 0;
200
201 // Add a new MediaStream to be sent on this PeerConnection.
202 // Note that a SessionDescription negotiation is needed before the
203 // remote peer can receive the stream.
204 virtual bool AddStream(MediaStreamInterface* stream,
205 const MediaConstraintsInterface* constraints) = 0;
206
207 // Remove a MediaStream from this PeerConnection.
208 // Note that a SessionDescription negotiation is need before the
209 // remote peer is notified.
210 virtual void RemoveStream(MediaStreamInterface* stream) = 0;
211
212 // Returns pointer to the created DtmfSender on success.
213 // Otherwise returns NULL.
214 virtual talk_base::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
215 AudioTrackInterface* track) = 0;
216
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000217 virtual bool GetStats(StatsObserver* observer,
218 MediaStreamTrackInterface* track,
219 StatsOutputLevel level) = 0;
220
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 virtual talk_base::scoped_refptr<DataChannelInterface> CreateDataChannel(
222 const std::string& label,
223 const DataChannelInit* config) = 0;
224
225 virtual const SessionDescriptionInterface* local_description() const = 0;
226 virtual const SessionDescriptionInterface* remote_description() const = 0;
227
228 // Create a new offer.
229 // The CreateSessionDescriptionObserver callback will be called when done.
230 virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
231 const MediaConstraintsInterface* constraints) = 0;
232 // Create an answer to an offer.
233 // The CreateSessionDescriptionObserver callback will be called when done.
234 virtual void CreateAnswer(CreateSessionDescriptionObserver* observer,
235 const MediaConstraintsInterface* constraints) = 0;
236 // Sets the local session description.
237 // JsepInterface takes the ownership of |desc| even if it fails.
238 // The |observer| callback will be called when done.
239 virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
240 SessionDescriptionInterface* desc) = 0;
241 // Sets the remote session description.
242 // JsepInterface takes the ownership of |desc| even if it fails.
243 // The |observer| callback will be called when done.
244 virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
245 SessionDescriptionInterface* desc) = 0;
246 // Restarts or updates the ICE Agent process of gathering local candidates
247 // and pinging remote candidates.
248 virtual bool UpdateIce(const IceServers& configuration,
249 const MediaConstraintsInterface* constraints) = 0;
250 // Provides a remote candidate to the ICE Agent.
251 // A copy of the |candidate| will be created and added to the remote
252 // description. So the caller of this method still has the ownership of the
253 // |candidate|.
254 // TODO(ronghuawu): Consider to change this so that the AddIceCandidate will
255 // take the ownership of the |candidate|.
256 virtual bool AddIceCandidate(const IceCandidateInterface* candidate) = 0;
257
258 // Returns the current SignalingState.
259 virtual SignalingState signaling_state() = 0;
260
261 // TODO(bemasc): Remove ice_state when callers are changed to
262 // IceConnection/GatheringState.
263 // Returns the current IceState.
264 virtual IceState ice_state() = 0;
265 virtual IceConnectionState ice_connection_state() = 0;
266 virtual IceGatheringState ice_gathering_state() = 0;
267
268 // Terminates all media and closes the transport.
269 virtual void Close() = 0;
270
271 protected:
272 // Dtor protected as objects shouldn't be deleted via this interface.
273 ~PeerConnectionInterface() {}
274};
275
276// PeerConnection callback interface. Application should implement these
277// methods.
278class PeerConnectionObserver {
279 public:
280 enum StateType {
281 kSignalingState,
282 kIceState,
283 };
284
285 virtual void OnError() = 0;
286
287 // Triggered when the SignalingState changed.
288 virtual void OnSignalingChange(
289 PeerConnectionInterface::SignalingState new_state) {}
290
291 // Triggered when SignalingState or IceState have changed.
292 // TODO(bemasc): Remove once callers transition to OnSignalingChange.
293 virtual void OnStateChange(StateType state_changed) {}
294
295 // Triggered when media is received on a new stream from remote peer.
296 virtual void OnAddStream(MediaStreamInterface* stream) = 0;
297
298 // Triggered when a remote peer close a stream.
299 virtual void OnRemoveStream(MediaStreamInterface* stream) = 0;
300
301 // Triggered when a remote peer open a data channel.
302 // TODO(perkj): Make pure virtual.
303 virtual void OnDataChannel(DataChannelInterface* data_channel) {}
304
mallinath@webrtc.org0d92ef62014-01-22 02:21:22 +0000305 // Triggered when renegotiation is needed, for example the ICE has restarted.
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +0000306 virtual void OnRenegotiationNeeded() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307
308 // Called any time the IceConnectionState changes
309 virtual void OnIceConnectionChange(
310 PeerConnectionInterface::IceConnectionState new_state) {}
311
312 // Called any time the IceGatheringState changes
313 virtual void OnIceGatheringChange(
314 PeerConnectionInterface::IceGatheringState new_state) {}
315
316 // New Ice candidate have been found.
317 virtual void OnIceCandidate(const IceCandidateInterface* candidate) = 0;
318
319 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
320 // All Ice candidates have been found.
321 virtual void OnIceComplete() {}
322
323 protected:
324 // Dtor protected as objects shouldn't be deleted via this interface.
325 ~PeerConnectionObserver() {}
326};
327
328// Factory class used for creating cricket::PortAllocator that is used
329// for ICE negotiation.
330class PortAllocatorFactoryInterface : public talk_base::RefCountInterface {
331 public:
332 struct StunConfiguration {
333 StunConfiguration(const std::string& address, int port)
334 : server(address, port) {}
335 // STUN server address and port.
336 talk_base::SocketAddress server;
337 };
338
339 struct TurnConfiguration {
340 TurnConfiguration(const std::string& address,
341 int port,
342 const std::string& username,
343 const std::string& password,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000344 const std::string& transport_type,
345 bool secure)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346 : server(address, port),
347 username(username),
348 password(password),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000349 transport_type(transport_type),
350 secure(secure) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 talk_base::SocketAddress server;
352 std::string username;
353 std::string password;
354 std::string transport_type;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000355 bool secure;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 };
357
358 virtual cricket::PortAllocator* CreatePortAllocator(
359 const std::vector<StunConfiguration>& stun_servers,
360 const std::vector<TurnConfiguration>& turn_configurations) = 0;
361
362 protected:
363 PortAllocatorFactoryInterface() {}
364 ~PortAllocatorFactoryInterface() {}
365};
366
367// Used to receive callbacks of DTLS identity requests.
368class DTLSIdentityRequestObserver : public talk_base::RefCountInterface {
369 public:
370 virtual void OnFailure(int error) = 0;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000371 virtual void OnSuccess(const std::string& der_cert,
372 const std::string& der_private_key) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 protected:
374 virtual ~DTLSIdentityRequestObserver() {}
375};
376
377class DTLSIdentityServiceInterface {
378 public:
379 // Asynchronously request a DTLS identity, including a self-signed certificate
380 // and the private key used to sign the certificate, from the identity store
381 // for the given identity name.
382 // DTLSIdentityRequestObserver::OnSuccess will be called with the identity if
383 // the request succeeded; DTLSIdentityRequestObserver::OnFailure will be
384 // called with an error code if the request failed.
385 //
386 // Only one request can be made at a time. If a second request is called
387 // before the first one completes, RequestIdentity will abort and return
388 // false.
389 //
390 // |identity_name| is an internal name selected by the client to identify an
391 // identity within an origin. E.g. an web site may cache the certificates used
392 // to communicate with differnent peers under different identity names.
393 //
394 // |common_name| is the common name used to generate the certificate. If the
395 // certificate already exists in the store, |common_name| is ignored.
396 //
397 // |observer| is the object to receive success or failure callbacks.
398 //
399 // Returns true if either OnFailure or OnSuccess will be called.
400 virtual bool RequestIdentity(
401 const std::string& identity_name,
402 const std::string& common_name,
403 DTLSIdentityRequestObserver* observer) = 0;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000404
405 virtual ~DTLSIdentityServiceInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406};
407
408// PeerConnectionFactoryInterface is the factory interface use for creating
409// PeerConnection, MediaStream and media tracks.
410// PeerConnectionFactoryInterface will create required libjingle threads,
411// socket and network manager factory classes for networking.
412// If an application decides to provide its own threads and network
413// implementation of these classes it should use the alternate
414// CreatePeerConnectionFactory method which accepts threads as input and use the
415// CreatePeerConnection version that takes a PortAllocatorFactoryInterface as
416// argument.
417class PeerConnectionFactoryInterface : public talk_base::RefCountInterface {
418 public:
wu@webrtc.org97077a32013-10-25 21:18:33 +0000419 class Options {
420 public:
421 Options() :
wu@webrtc.org97077a32013-10-25 21:18:33 +0000422 disable_encryption(false),
423 disable_sctp_data_channels(false) {
424 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000425 bool disable_encryption;
426 bool disable_sctp_data_channels;
427 };
428
429 virtual void SetOptions(const Options& options) = 0;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000430
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 virtual talk_base::scoped_refptr<PeerConnectionInterface>
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000432 CreatePeerConnection(
433 const PeerConnectionInterface::RTCConfiguration& configuration,
434 const MediaConstraintsInterface* constraints,
435 PortAllocatorFactoryInterface* allocator_factory,
436 DTLSIdentityServiceInterface* dtls_identity_service,
437 PeerConnectionObserver* observer) = 0;
438
439 // TODO(mallinath) : Remove below versions after clients are updated
440 // to above method.
441 // In latest W3C WebRTC draft, PC constructor will take RTCConfiguration,
442 // and not IceServers. RTCConfiguration is made up of ice servers and
443 // ice transport type.
444 // http://dev.w3.org/2011/webrtc/editor/webrtc.html
445 inline talk_base::scoped_refptr<PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 CreatePeerConnection(
447 const PeerConnectionInterface::IceServers& configuration,
448 const MediaConstraintsInterface* constraints,
449 PortAllocatorFactoryInterface* allocator_factory,
450 DTLSIdentityServiceInterface* dtls_identity_service,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000451 PeerConnectionObserver* observer) {
452 PeerConnectionInterface::RTCConfiguration rtc_config;
453 rtc_config.servers = configuration;
454 return CreatePeerConnection(rtc_config, constraints, allocator_factory,
455 dtls_identity_service, observer);
456 }
457
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 virtual talk_base::scoped_refptr<MediaStreamInterface>
459 CreateLocalMediaStream(const std::string& label) = 0;
460
461 // Creates a AudioSourceInterface.
462 // |constraints| decides audio processing settings but can be NULL.
463 virtual talk_base::scoped_refptr<AudioSourceInterface> CreateAudioSource(
464 const MediaConstraintsInterface* constraints) = 0;
465
466 // Creates a VideoSourceInterface. The new source take ownership of
467 // |capturer|. |constraints| decides video resolution and frame rate but can
468 // be NULL.
469 virtual talk_base::scoped_refptr<VideoSourceInterface> CreateVideoSource(
470 cricket::VideoCapturer* capturer,
471 const MediaConstraintsInterface* constraints) = 0;
472
473 // Creates a new local VideoTrack. The same |source| can be used in several
474 // tracks.
475 virtual talk_base::scoped_refptr<VideoTrackInterface>
476 CreateVideoTrack(const std::string& label,
477 VideoSourceInterface* source) = 0;
478
479 // Creates an new AudioTrack. At the moment |source| can be NULL.
480 virtual talk_base::scoped_refptr<AudioTrackInterface>
481 CreateAudioTrack(const std::string& label,
482 AudioSourceInterface* source) = 0;
483
wu@webrtc.orga9890802013-12-13 00:21:03 +0000484 // Starts AEC dump using existing file. Takes ownership of |file| and passes
485 // it on to VoiceEngine (via other objects) immediately, which will take
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000486 // the ownerhip. If the operation fails, the file will be closed.
wu@webrtc.orga9890802013-12-13 00:21:03 +0000487 // TODO(grunell): Remove when Chromium has started to use AEC in each source.
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000488 // http://crbug.com/264611.
489 virtual bool StartAecDump(talk_base::PlatformFile file) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000490
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 protected:
492 // Dtor and ctor protected as objects shouldn't be created or deleted via
493 // this interface.
494 PeerConnectionFactoryInterface() {}
495 ~PeerConnectionFactoryInterface() {} // NOLINT
496};
497
498// Create a new instance of PeerConnectionFactoryInterface.
499talk_base::scoped_refptr<PeerConnectionFactoryInterface>
500CreatePeerConnectionFactory();
501
502// Create a new instance of PeerConnectionFactoryInterface.
503// Ownership of |factory|, |default_adm|, and optionally |encoder_factory| and
504// |decoder_factory| transferred to the returned factory.
505talk_base::scoped_refptr<PeerConnectionFactoryInterface>
506CreatePeerConnectionFactory(
507 talk_base::Thread* worker_thread,
508 talk_base::Thread* signaling_thread,
509 AudioDeviceModule* default_adm,
510 cricket::WebRtcVideoEncoderFactory* encoder_factory,
511 cricket::WebRtcVideoDecoderFactory* decoder_factory);
512
513} // namespace webrtc
514
515#endif // TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_