blob: 0cb049f4983a748433fe3ad2d5ba2f69ed9df33d [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#ifndef TALK_APP_WEBRTC_WEBRTCSESSION_H_
29#define TALK_APP_WEBRTC_WEBRTCSESSION_H_
30
31#include <string>
32
33#include "talk/app/webrtc/peerconnectioninterface.h"
34#include "talk/app/webrtc/dtmfsender.h"
35#include "talk/app/webrtc/mediastreamprovider.h"
36#include "talk/app/webrtc/datachannel.h"
37#include "talk/app/webrtc/statstypes.h"
38#include "talk/base/sigslot.h"
39#include "talk/base/thread.h"
40#include "talk/media/base/mediachannel.h"
41#include "talk/p2p/base/session.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include "talk/session/media/mediasession.h"
43
44namespace cricket {
45
46class ChannelManager;
47class DataChannel;
48class StatsReport;
49class Transport;
50class VideoCapturer;
51class BaseChannel;
52class VideoChannel;
53class VoiceChannel;
54
55} // namespace cricket
56
57namespace webrtc {
58
59class IceRestartAnswerLatch;
60class MediaStreamSignaling;
wu@webrtc.org91053e72013-08-10 07:18:04 +000061class WebRtcSessionDescriptionFactory;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
63extern const char kSetLocalSdpFailed[];
64extern const char kSetRemoteSdpFailed[];
65extern const char kCreateChannelFailed[];
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000066extern const char kBundleWithoutRtcpMux[];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067extern const char kInvalidCandidates[];
68extern const char kInvalidSdp[];
69extern const char kMlineMismatch[];
70extern const char kSdpWithoutCrypto[];
71extern const char kSessionError[];
72extern const char kUpdateStateFailed[];
73extern const char kPushDownOfferTDFailed[];
74extern const char kPushDownPranswerTDFailed[];
75extern const char kPushDownAnswerTDFailed[];
76
77// ICE state callback interface.
78class IceObserver {
79 public:
80 // Called any time the IceConnectionState changes
81 virtual void OnIceConnectionChange(
82 PeerConnectionInterface::IceConnectionState new_state) {}
83 // Called any time the IceGatheringState changes
84 virtual void OnIceGatheringChange(
85 PeerConnectionInterface::IceGatheringState new_state) {}
86 // New Ice candidate have been found.
87 virtual void OnIceCandidate(const IceCandidateInterface* candidate) = 0;
88 // All Ice candidates have been found.
89 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
90 // (via PeerConnectionObserver)
91 virtual void OnIceComplete() {}
92
93 protected:
94 ~IceObserver() {}
95};
96
97class WebRtcSession : public cricket::BaseSession,
98 public AudioProviderInterface,
99 public DataChannelFactory,
100 public VideoProviderInterface,
101 public DtmfProviderInterface {
102 public:
103 WebRtcSession(cricket::ChannelManager* channel_manager,
104 talk_base::Thread* signaling_thread,
105 talk_base::Thread* worker_thread,
106 cricket::PortAllocator* port_allocator,
107 MediaStreamSignaling* mediastream_signaling);
108 virtual ~WebRtcSession();
109
wu@webrtc.org91053e72013-08-10 07:18:04 +0000110 bool Initialize(const MediaConstraintsInterface* constraints,
111 DTLSIdentityServiceInterface* dtls_identity_service);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 // Deletes the voice, video and data channel and changes the session state
113 // to STATE_RECEIVEDTERMINATE.
114 void Terminate();
115
116 void RegisterIceObserver(IceObserver* observer) {
117 ice_observer_ = observer;
118 }
119
120 virtual cricket::VoiceChannel* voice_channel() {
121 return voice_channel_.get();
122 }
123 virtual cricket::VideoChannel* video_channel() {
124 return video_channel_.get();
125 }
126 virtual cricket::DataChannel* data_channel() {
127 return data_channel_.get();
128 }
129
130 void set_secure_policy(cricket::SecureMediaPolicy secure_policy);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000131 cricket::SecureMediaPolicy secure_policy() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000133 // Get current ssl role from transport.
134 bool GetSslRole(talk_base::SSLRole* role);
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 // Generic error message callback from WebRtcSession.
137 // TODO - It may be necessary to supply error code as well.
138 sigslot::signal0<> SignalError;
139
wu@webrtc.org91053e72013-08-10 07:18:04 +0000140 void CreateOffer(CreateSessionDescriptionObserver* observer,
141 const MediaConstraintsInterface* constraints);
142 void CreateAnswer(CreateSessionDescriptionObserver* observer,
143 const MediaConstraintsInterface* constraints);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000144 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 bool SetLocalDescription(SessionDescriptionInterface* desc,
146 std::string* err_desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000147 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 bool SetRemoteDescription(SessionDescriptionInterface* desc,
149 std::string* err_desc);
150 bool ProcessIceMessage(const IceCandidateInterface* ice_candidate);
151 const SessionDescriptionInterface* local_description() const {
152 return local_desc_.get();
153 }
154 const SessionDescriptionInterface* remote_description() const {
155 return remote_desc_.get();
156 }
157
158 // Get the id used as a media stream track's "id" field from ssrc.
159 virtual bool GetTrackIdBySsrc(uint32 ssrc, std::string* id);
160
161 // AudioMediaProviderInterface implementation.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000162 virtual void SetAudioPlayout(uint32 ssrc, bool enable,
163 cricket::AudioRenderer* renderer) OVERRIDE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 virtual void SetAudioSend(uint32 ssrc, bool enable,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000165 const cricket::AudioOptions& options,
166 cricket::AudioRenderer* renderer) OVERRIDE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167
168 // Implements VideoMediaProviderInterface.
169 virtual bool SetCaptureDevice(uint32 ssrc,
170 cricket::VideoCapturer* camera) OVERRIDE;
171 virtual void SetVideoPlayout(uint32 ssrc,
172 bool enable,
173 cricket::VideoRenderer* renderer) OVERRIDE;
174 virtual void SetVideoSend(uint32 ssrc, bool enable,
175 const cricket::VideoOptions* options) OVERRIDE;
176
177 // Implements DtmfProviderInterface.
178 virtual bool CanInsertDtmf(const std::string& track_id);
179 virtual bool InsertDtmf(const std::string& track_id,
180 int code, int duration);
181 virtual sigslot::signal0<>* GetOnDestroyedSignal();
182
183 talk_base::scoped_refptr<DataChannel> CreateDataChannel(
184 const std::string& label,
185 const DataChannelInit* config);
186
187 cricket::DataChannelType data_channel_type() const;
188
wu@webrtc.org91053e72013-08-10 07:18:04 +0000189 bool IceRestartPending() const;
190
191 void ResetIceRestartLatch();
192
193 // Called when an SSLIdentity is generated or retrieved by
194 // WebRTCSessionDescriptionFactory. Should happen before setLocalDescription.
195 void OnIdentityReady(talk_base::SSLIdentity* identity);
196
197 // For unit test.
198 bool waiting_for_identity() const;
199
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 private:
201 // Indicates the type of SessionDescription in a call to SetLocalDescription
202 // and SetRemoteDescription.
203 enum Action {
204 kOffer,
205 kPrAnswer,
206 kAnswer,
207 };
wu@webrtc.org91053e72013-08-10 07:18:04 +0000208
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 // Invokes ConnectChannels() on transport proxies, which initiates ice
210 // candidates allocation.
211 bool StartCandidatesAllocation();
212 bool UpdateSessionState(Action action, cricket::ContentSource source,
213 const cricket::SessionDescription* desc,
214 std::string* err_desc);
215 static Action GetAction(const std::string& type);
216
217 // Transport related callbacks, override from cricket::BaseSession.
218 virtual void OnTransportRequestSignaling(cricket::Transport* transport);
219 virtual void OnTransportConnecting(cricket::Transport* transport);
220 virtual void OnTransportWritable(cricket::Transport* transport);
221 virtual void OnTransportProxyCandidatesReady(
222 cricket::TransportProxy* proxy,
223 const cricket::Candidates& candidates);
224 virtual void OnCandidatesAllocationDone();
225
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 // Creates local session description with audio and video contents.
227 bool CreateDefaultLocalDescription();
228 // Enables media channels to allow sending of media.
229 void EnableChannels();
230 // Creates a JsepIceCandidate and adds it to the local session description
231 // and notify observers. Called when a new local candidate have been found.
232 void ProcessNewLocalCandidate(const std::string& content_name,
233 const cricket::Candidates& candidates);
234 // Returns the media index for a local ice candidate given the content name.
235 // Returns false if the local session description does not have a media
236 // content called |content_name|.
237 bool GetLocalCandidateMediaIndex(const std::string& content_name,
238 int* sdp_mline_index);
239 // Uses all remote candidates in |remote_desc| in this session.
240 bool UseCandidatesInSessionDescription(
241 const SessionDescriptionInterface* remote_desc);
242 // Uses |candidate| in this session.
243 bool UseCandidate(const IceCandidateInterface* candidate);
244 // Deletes the corresponding channel of contents that don't exist in |desc|.
245 // |desc| can be null. This means that all channels are deleted.
246 void RemoveUnusedChannelsAndTransports(
247 const cricket::SessionDescription* desc);
248
249 // Allocates media channels based on the |desc|. If |desc| doesn't have
250 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
251 // This method will also delete any existing media channels before creating.
252 bool CreateChannels(const cricket::SessionDescription* desc);
253
254 // Helper methods to create media channels.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000255 bool CreateVoiceChannel(const cricket::ContentInfo* content);
256 bool CreateVideoChannel(const cricket::ContentInfo* content);
257 bool CreateDataChannel(const cricket::ContentInfo* content);
258
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 // Copy the candidates from |saved_candidates_| to |dest_desc|.
260 // The |saved_candidates_| will be cleared after this function call.
261 void CopySavedCandidates(SessionDescriptionInterface* dest_desc);
262
wu@webrtc.org91053e72013-08-10 07:18:04 +0000263 void OnDataReceived(
264 cricket::DataChannel* channel,
265 const cricket::ReceiveDataParams& params,
266 const talk_base::Buffer& payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267
268 bool GetLocalTrackId(uint32 ssrc, std::string* track_id);
269 bool GetRemoteTrackId(uint32 ssrc, std::string* track_id);
270
271 std::string BadStateErrMsg(const std::string& type, State state);
272 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
273
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000274 bool ValidateBundleSettings(const cricket::SessionDescription* desc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000275 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000276 // Below methods are helper methods which verifies SDP.
277 bool ValidateSessionDescription(const SessionDescriptionInterface* sdesc,
278 cricket::ContentSource source,
279 std::string* error_desc);
280
281 // Check if a call to SetLocalDescription is acceptable with |action|.
282 bool ExpectSetLocalDescription(Action action);
283 // Check if a call to SetRemoteDescription is acceptable with |action|.
284 bool ExpectSetRemoteDescription(Action action);
285 // Verifies a=setup attribute as per RFC 5763.
286 bool ValidateDtlsSetupAttribute(const cricket::SessionDescription* desc,
287 Action action);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000288
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289 talk_base::scoped_ptr<cricket::VoiceChannel> voice_channel_;
290 talk_base::scoped_ptr<cricket::VideoChannel> video_channel_;
291 talk_base::scoped_ptr<cricket::DataChannel> data_channel_;
292 cricket::ChannelManager* channel_manager_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 MediaStreamSignaling* mediastream_signaling_;
294 IceObserver* ice_observer_;
295 PeerConnectionInterface::IceConnectionState ice_connection_state_;
296 talk_base::scoped_ptr<SessionDescriptionInterface> local_desc_;
297 talk_base::scoped_ptr<SessionDescriptionInterface> remote_desc_;
298 // Candidates that arrived before the remote description was set.
299 std::vector<IceCandidateInterface*> saved_candidates_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 // If the remote peer is using a older version of implementation.
301 bool older_version_remote_peer_;
302 // Specifies which kind of data channel is allowed. This is controlled
303 // by the chrome command-line flag and constraints:
304 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
305 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
306 // not set or false, SCTP is allowed (DCT_SCTP);
307 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
308 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
309 cricket::DataChannelType data_channel_type_;
310 talk_base::scoped_ptr<IceRestartAnswerLatch> ice_restart_latch_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000311
312 talk_base::scoped_ptr<WebRtcSessionDescriptionFactory>
313 webrtc_session_desc_factory_;
314
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 sigslot::signal0<> SignalVoiceChannelDestroyed;
316 sigslot::signal0<> SignalVideoChannelDestroyed;
317 sigslot::signal0<> SignalDataChannelDestroyed;
318};
319
320} // namespace webrtc
321
322#endif // TALK_APP_WEBRTC_WEBRTCSESSION_H_