blob: 54d34175359aadf7a598fc17bc972ac78be7f910 [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"
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +000079#include "talk/app/webrtc/umametrics.h"
wu@webrtc.orga8910d22014-01-23 22:12:45 +000080#include "talk/base/fileutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081#include "talk/base/socketaddress.h"
82
83namespace talk_base {
84class Thread;
85}
86
87namespace cricket {
88class PortAllocator;
89class WebRtcVideoDecoderFactory;
90class WebRtcVideoEncoderFactory;
91}
92
93namespace webrtc {
94class AudioDeviceModule;
95class MediaConstraintsInterface;
96
97// MediaStream container interface.
98class StreamCollectionInterface : public talk_base::RefCountInterface {
99 public:
100 // TODO(ronghuawu): Update the function names to c++ style, e.g. find -> Find.
101 virtual size_t count() = 0;
102 virtual MediaStreamInterface* at(size_t index) = 0;
103 virtual MediaStreamInterface* find(const std::string& label) = 0;
104 virtual MediaStreamTrackInterface* FindAudioTrack(
105 const std::string& id) = 0;
106 virtual MediaStreamTrackInterface* FindVideoTrack(
107 const std::string& id) = 0;
108
109 protected:
110 // Dtor protected as objects shouldn't be deleted via this interface.
111 ~StreamCollectionInterface() {}
112};
113
114class StatsObserver : public talk_base::RefCountInterface {
115 public:
116 virtual void OnComplete(const std::vector<StatsReport>& reports) = 0;
117
118 protected:
119 virtual ~StatsObserver() {}
120};
121
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000122class UMAObserver : public talk_base::RefCountInterface {
123 public:
124 virtual void IncrementCounter(UMAMetricsCounter type) = 0;
125 virtual void AddHistogramSample(UMAMetricsName type, int value) = 0;
126
127 protected:
128 virtual ~UMAObserver() {}
129};
130
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131class PeerConnectionInterface : public talk_base::RefCountInterface {
132 public:
133 // See http://dev.w3.org/2011/webrtc/editor/webrtc.html#state-definitions .
134 enum SignalingState {
135 kStable,
136 kHaveLocalOffer,
137 kHaveLocalPrAnswer,
138 kHaveRemoteOffer,
139 kHaveRemotePrAnswer,
140 kClosed,
141 };
142
143 // TODO(bemasc): Remove IceState when callers are changed to
144 // IceConnection/GatheringState.
145 enum IceState {
146 kIceNew,
147 kIceGathering,
148 kIceWaiting,
149 kIceChecking,
150 kIceConnected,
151 kIceCompleted,
152 kIceFailed,
153 kIceClosed,
154 };
155
156 enum IceGatheringState {
157 kIceGatheringNew,
158 kIceGatheringGathering,
159 kIceGatheringComplete
160 };
161
162 enum IceConnectionState {
163 kIceConnectionNew,
164 kIceConnectionChecking,
165 kIceConnectionConnected,
166 kIceConnectionCompleted,
167 kIceConnectionFailed,
168 kIceConnectionDisconnected,
169 kIceConnectionClosed,
170 };
171
172 struct IceServer {
173 std::string uri;
174 std::string username;
175 std::string password;
176 };
177 typedef std::vector<IceServer> IceServers;
178
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000179 enum IceTransportsType {
180 kNone,
181 kRelay,
182 kNoHost,
183 kAll
184 };
185
186 struct RTCConfiguration {
187 IceTransportsType type;
188 IceServers servers;
189
190 RTCConfiguration() : type(kAll) {}
191 explicit RTCConfiguration(IceTransportsType type) : type(type) {}
192 };
193
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000194 // Used by GetStats to decide which stats to include in the stats reports.
195 // |kStatsOutputLevelStandard| includes the standard stats for Javascript API;
196 // |kStatsOutputLevelDebug| includes both the standard stats and additional
197 // stats for debugging purposes.
198 enum StatsOutputLevel {
199 kStatsOutputLevelStandard,
200 kStatsOutputLevelDebug,
201 };
202
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 // Accessor methods to active local streams.
204 virtual talk_base::scoped_refptr<StreamCollectionInterface>
205 local_streams() = 0;
206
207 // Accessor methods to remote streams.
208 virtual talk_base::scoped_refptr<StreamCollectionInterface>
209 remote_streams() = 0;
210
211 // Add a new MediaStream to be sent on this PeerConnection.
212 // Note that a SessionDescription negotiation is needed before the
213 // remote peer can receive the stream.
214 virtual bool AddStream(MediaStreamInterface* stream,
215 const MediaConstraintsInterface* constraints) = 0;
216
217 // Remove a MediaStream from this PeerConnection.
218 // Note that a SessionDescription negotiation is need before the
219 // remote peer is notified.
220 virtual void RemoveStream(MediaStreamInterface* stream) = 0;
221
222 // Returns pointer to the created DtmfSender on success.
223 // Otherwise returns NULL.
224 virtual talk_base::scoped_refptr<DtmfSenderInterface> CreateDtmfSender(
225 AudioTrackInterface* track) = 0;
226
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000227 virtual bool GetStats(StatsObserver* observer,
228 MediaStreamTrackInterface* track,
229 StatsOutputLevel level) = 0;
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 virtual talk_base::scoped_refptr<DataChannelInterface> CreateDataChannel(
232 const std::string& label,
233 const DataChannelInit* config) = 0;
234
235 virtual const SessionDescriptionInterface* local_description() const = 0;
236 virtual const SessionDescriptionInterface* remote_description() const = 0;
237
238 // Create a new offer.
239 // The CreateSessionDescriptionObserver callback will be called when done.
240 virtual void CreateOffer(CreateSessionDescriptionObserver* observer,
241 const MediaConstraintsInterface* constraints) = 0;
242 // Create an answer to an offer.
243 // The CreateSessionDescriptionObserver callback will be called when done.
244 virtual void CreateAnswer(CreateSessionDescriptionObserver* observer,
245 const MediaConstraintsInterface* constraints) = 0;
246 // Sets the local session description.
247 // JsepInterface takes the ownership of |desc| even if it fails.
248 // The |observer| callback will be called when done.
249 virtual void SetLocalDescription(SetSessionDescriptionObserver* observer,
250 SessionDescriptionInterface* desc) = 0;
251 // Sets the remote session description.
252 // JsepInterface takes the ownership of |desc| even if it fails.
253 // The |observer| callback will be called when done.
254 virtual void SetRemoteDescription(SetSessionDescriptionObserver* observer,
255 SessionDescriptionInterface* desc) = 0;
256 // Restarts or updates the ICE Agent process of gathering local candidates
257 // and pinging remote candidates.
258 virtual bool UpdateIce(const IceServers& configuration,
259 const MediaConstraintsInterface* constraints) = 0;
260 // Provides a remote candidate to the ICE Agent.
261 // A copy of the |candidate| will be created and added to the remote
262 // description. So the caller of this method still has the ownership of the
263 // |candidate|.
264 // TODO(ronghuawu): Consider to change this so that the AddIceCandidate will
265 // take the ownership of the |candidate|.
266 virtual bool AddIceCandidate(const IceCandidateInterface* candidate) = 0;
267
buildbot@webrtc.org1567b8c2014-05-08 19:54:16 +0000268 virtual void RegisterUMAObserver(UMAObserver* observer) = 0;
269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 // Returns the current SignalingState.
271 virtual SignalingState signaling_state() = 0;
272
273 // TODO(bemasc): Remove ice_state when callers are changed to
274 // IceConnection/GatheringState.
275 // Returns the current IceState.
276 virtual IceState ice_state() = 0;
277 virtual IceConnectionState ice_connection_state() = 0;
278 virtual IceGatheringState ice_gathering_state() = 0;
279
280 // Terminates all media and closes the transport.
281 virtual void Close() = 0;
282
283 protected:
284 // Dtor protected as objects shouldn't be deleted via this interface.
285 ~PeerConnectionInterface() {}
286};
287
288// PeerConnection callback interface. Application should implement these
289// methods.
290class PeerConnectionObserver {
291 public:
292 enum StateType {
293 kSignalingState,
294 kIceState,
295 };
296
297 virtual void OnError() = 0;
298
299 // Triggered when the SignalingState changed.
300 virtual void OnSignalingChange(
301 PeerConnectionInterface::SignalingState new_state) {}
302
303 // Triggered when SignalingState or IceState have changed.
304 // TODO(bemasc): Remove once callers transition to OnSignalingChange.
305 virtual void OnStateChange(StateType state_changed) {}
306
307 // Triggered when media is received on a new stream from remote peer.
308 virtual void OnAddStream(MediaStreamInterface* stream) = 0;
309
310 // Triggered when a remote peer close a stream.
311 virtual void OnRemoveStream(MediaStreamInterface* stream) = 0;
312
313 // Triggered when a remote peer open a data channel.
314 // TODO(perkj): Make pure virtual.
315 virtual void OnDataChannel(DataChannelInterface* data_channel) {}
316
mallinath@webrtc.org0d92ef62014-01-22 02:21:22 +0000317 // Triggered when renegotiation is needed, for example the ICE has restarted.
fischman@webrtc.orgd7568a02014-01-13 22:04:12 +0000318 virtual void OnRenegotiationNeeded() = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319
320 // Called any time the IceConnectionState changes
321 virtual void OnIceConnectionChange(
322 PeerConnectionInterface::IceConnectionState new_state) {}
323
324 // Called any time the IceGatheringState changes
325 virtual void OnIceGatheringChange(
326 PeerConnectionInterface::IceGatheringState new_state) {}
327
328 // New Ice candidate have been found.
329 virtual void OnIceCandidate(const IceCandidateInterface* candidate) = 0;
330
331 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
332 // All Ice candidates have been found.
333 virtual void OnIceComplete() {}
334
335 protected:
336 // Dtor protected as objects shouldn't be deleted via this interface.
337 ~PeerConnectionObserver() {}
338};
339
340// Factory class used for creating cricket::PortAllocator that is used
341// for ICE negotiation.
342class PortAllocatorFactoryInterface : public talk_base::RefCountInterface {
343 public:
344 struct StunConfiguration {
345 StunConfiguration(const std::string& address, int port)
346 : server(address, port) {}
347 // STUN server address and port.
348 talk_base::SocketAddress server;
349 };
350
351 struct TurnConfiguration {
352 TurnConfiguration(const std::string& address,
353 int port,
354 const std::string& username,
355 const std::string& password,
wu@webrtc.org91053e72013-08-10 07:18:04 +0000356 const std::string& transport_type,
357 bool secure)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 : server(address, port),
359 username(username),
360 password(password),
wu@webrtc.org91053e72013-08-10 07:18:04 +0000361 transport_type(transport_type),
362 secure(secure) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 talk_base::SocketAddress server;
364 std::string username;
365 std::string password;
366 std::string transport_type;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000367 bool secure;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 };
369
370 virtual cricket::PortAllocator* CreatePortAllocator(
371 const std::vector<StunConfiguration>& stun_servers,
372 const std::vector<TurnConfiguration>& turn_configurations) = 0;
373
374 protected:
375 PortAllocatorFactoryInterface() {}
376 ~PortAllocatorFactoryInterface() {}
377};
378
379// Used to receive callbacks of DTLS identity requests.
380class DTLSIdentityRequestObserver : public talk_base::RefCountInterface {
381 public:
382 virtual void OnFailure(int error) = 0;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000383 virtual void OnSuccess(const std::string& der_cert,
384 const std::string& der_private_key) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385 protected:
386 virtual ~DTLSIdentityRequestObserver() {}
387};
388
389class DTLSIdentityServiceInterface {
390 public:
391 // Asynchronously request a DTLS identity, including a self-signed certificate
392 // and the private key used to sign the certificate, from the identity store
393 // for the given identity name.
394 // DTLSIdentityRequestObserver::OnSuccess will be called with the identity if
395 // the request succeeded; DTLSIdentityRequestObserver::OnFailure will be
396 // called with an error code if the request failed.
397 //
398 // Only one request can be made at a time. If a second request is called
399 // before the first one completes, RequestIdentity will abort and return
400 // false.
401 //
402 // |identity_name| is an internal name selected by the client to identify an
403 // identity within an origin. E.g. an web site may cache the certificates used
404 // to communicate with differnent peers under different identity names.
405 //
406 // |common_name| is the common name used to generate the certificate. If the
407 // certificate already exists in the store, |common_name| is ignored.
408 //
409 // |observer| is the object to receive success or failure callbacks.
410 //
411 // Returns true if either OnFailure or OnSuccess will be called.
412 virtual bool RequestIdentity(
413 const std::string& identity_name,
414 const std::string& common_name,
415 DTLSIdentityRequestObserver* observer) = 0;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000416
417 virtual ~DTLSIdentityServiceInterface() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418};
419
420// PeerConnectionFactoryInterface is the factory interface use for creating
421// PeerConnection, MediaStream and media tracks.
422// PeerConnectionFactoryInterface will create required libjingle threads,
423// socket and network manager factory classes for networking.
424// If an application decides to provide its own threads and network
425// implementation of these classes it should use the alternate
426// CreatePeerConnectionFactory method which accepts threads as input and use the
427// CreatePeerConnection version that takes a PortAllocatorFactoryInterface as
428// argument.
429class PeerConnectionFactoryInterface : public talk_base::RefCountInterface {
430 public:
wu@webrtc.org97077a32013-10-25 21:18:33 +0000431 class Options {
432 public:
433 Options() :
wu@webrtc.org97077a32013-10-25 21:18:33 +0000434 disable_encryption(false),
435 disable_sctp_data_channels(false) {
436 }
wu@webrtc.org97077a32013-10-25 21:18:33 +0000437 bool disable_encryption;
438 bool disable_sctp_data_channels;
439 };
440
441 virtual void SetOptions(const Options& options) = 0;
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000442
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 virtual talk_base::scoped_refptr<PeerConnectionInterface>
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000444 CreatePeerConnection(
445 const PeerConnectionInterface::RTCConfiguration& configuration,
446 const MediaConstraintsInterface* constraints,
447 PortAllocatorFactoryInterface* allocator_factory,
448 DTLSIdentityServiceInterface* dtls_identity_service,
449 PeerConnectionObserver* observer) = 0;
450
451 // TODO(mallinath) : Remove below versions after clients are updated
452 // to above method.
453 // In latest W3C WebRTC draft, PC constructor will take RTCConfiguration,
454 // and not IceServers. RTCConfiguration is made up of ice servers and
455 // ice transport type.
456 // http://dev.w3.org/2011/webrtc/editor/webrtc.html
457 inline talk_base::scoped_refptr<PeerConnectionInterface>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 CreatePeerConnection(
459 const PeerConnectionInterface::IceServers& configuration,
460 const MediaConstraintsInterface* constraints,
461 PortAllocatorFactoryInterface* allocator_factory,
462 DTLSIdentityServiceInterface* dtls_identity_service,
buildbot@webrtc.org41451d42014-05-03 05:39:45 +0000463 PeerConnectionObserver* observer) {
464 PeerConnectionInterface::RTCConfiguration rtc_config;
465 rtc_config.servers = configuration;
466 return CreatePeerConnection(rtc_config, constraints, allocator_factory,
467 dtls_identity_service, observer);
468 }
469
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 virtual talk_base::scoped_refptr<MediaStreamInterface>
471 CreateLocalMediaStream(const std::string& label) = 0;
472
473 // Creates a AudioSourceInterface.
474 // |constraints| decides audio processing settings but can be NULL.
475 virtual talk_base::scoped_refptr<AudioSourceInterface> CreateAudioSource(
476 const MediaConstraintsInterface* constraints) = 0;
477
478 // Creates a VideoSourceInterface. The new source take ownership of
479 // |capturer|. |constraints| decides video resolution and frame rate but can
480 // be NULL.
481 virtual talk_base::scoped_refptr<VideoSourceInterface> CreateVideoSource(
482 cricket::VideoCapturer* capturer,
483 const MediaConstraintsInterface* constraints) = 0;
484
485 // Creates a new local VideoTrack. The same |source| can be used in several
486 // tracks.
487 virtual talk_base::scoped_refptr<VideoTrackInterface>
488 CreateVideoTrack(const std::string& label,
489 VideoSourceInterface* source) = 0;
490
491 // Creates an new AudioTrack. At the moment |source| can be NULL.
492 virtual talk_base::scoped_refptr<AudioTrackInterface>
493 CreateAudioTrack(const std::string& label,
494 AudioSourceInterface* source) = 0;
495
wu@webrtc.orga9890802013-12-13 00:21:03 +0000496 // Starts AEC dump using existing file. Takes ownership of |file| and passes
497 // it on to VoiceEngine (via other objects) immediately, which will take
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000498 // the ownerhip. If the operation fails, the file will be closed.
wu@webrtc.orga9890802013-12-13 00:21:03 +0000499 // TODO(grunell): Remove when Chromium has started to use AEC in each source.
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000500 // http://crbug.com/264611.
501 virtual bool StartAecDump(talk_base::PlatformFile file) = 0;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000502
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 protected:
504 // Dtor and ctor protected as objects shouldn't be created or deleted via
505 // this interface.
506 PeerConnectionFactoryInterface() {}
507 ~PeerConnectionFactoryInterface() {} // NOLINT
508};
509
510// Create a new instance of PeerConnectionFactoryInterface.
511talk_base::scoped_refptr<PeerConnectionFactoryInterface>
512CreatePeerConnectionFactory();
513
514// Create a new instance of PeerConnectionFactoryInterface.
515// Ownership of |factory|, |default_adm|, and optionally |encoder_factory| and
516// |decoder_factory| transferred to the returned factory.
517talk_base::scoped_refptr<PeerConnectionFactoryInterface>
518CreatePeerConnectionFactory(
519 talk_base::Thread* worker_thread,
520 talk_base::Thread* signaling_thread,
521 AudioDeviceModule* default_adm,
522 cricket::WebRtcVideoEncoderFactory* encoder_factory,
523 cricket::WebRtcVideoDecoderFactory* decoder_factory);
524
525} // namespace webrtc
526
527#endif // TALK_APP_WEBRTC_PEERCONNECTIONINTERFACE_H_