blob: d3997f3ff6c8e16beee23841732906b19d640e0a [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"
42#include "talk/p2p/base/transportdescriptionfactory.h"
43#include "talk/session/media/mediasession.h"
44
45namespace cricket {
46
47class ChannelManager;
48class DataChannel;
49class StatsReport;
50class Transport;
51class VideoCapturer;
52class BaseChannel;
53class VideoChannel;
54class VoiceChannel;
55
56} // namespace cricket
57
58namespace webrtc {
59
60class IceRestartAnswerLatch;
61class MediaStreamSignaling;
62
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
110 bool Initialize(const MediaConstraintsInterface* constraints);
111 // Deletes the voice, video and data channel and changes the session state
112 // to STATE_RECEIVEDTERMINATE.
113 void Terminate();
114
115 void RegisterIceObserver(IceObserver* observer) {
116 ice_observer_ = observer;
117 }
118
119 virtual cricket::VoiceChannel* voice_channel() {
120 return voice_channel_.get();
121 }
122 virtual cricket::VideoChannel* video_channel() {
123 return video_channel_.get();
124 }
125 virtual cricket::DataChannel* data_channel() {
126 return data_channel_.get();
127 }
128
129 void set_secure_policy(cricket::SecureMediaPolicy secure_policy);
130 cricket::SecureMediaPolicy secure_policy() const {
131 return session_desc_factory_.secure();
132 }
133
134 // Generic error message callback from WebRtcSession.
135 // TODO - It may be necessary to supply error code as well.
136 sigslot::signal0<> SignalError;
137
138 SessionDescriptionInterface* CreateOffer(
139 const MediaConstraintsInterface* constraints);
140
141 SessionDescriptionInterface* CreateAnswer(
142 const MediaConstraintsInterface* constraints);
143
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
189 private:
190 // Indicates the type of SessionDescription in a call to SetLocalDescription
191 // and SetRemoteDescription.
192 enum Action {
193 kOffer,
194 kPrAnswer,
195 kAnswer,
196 };
197 // Invokes ConnectChannels() on transport proxies, which initiates ice
198 // candidates allocation.
199 bool StartCandidatesAllocation();
200 bool UpdateSessionState(Action action, cricket::ContentSource source,
201 const cricket::SessionDescription* desc,
202 std::string* err_desc);
203 static Action GetAction(const std::string& type);
204
205 // Transport related callbacks, override from cricket::BaseSession.
206 virtual void OnTransportRequestSignaling(cricket::Transport* transport);
207 virtual void OnTransportConnecting(cricket::Transport* transport);
208 virtual void OnTransportWritable(cricket::Transport* transport);
209 virtual void OnTransportProxyCandidatesReady(
210 cricket::TransportProxy* proxy,
211 const cricket::Candidates& candidates);
212 virtual void OnCandidatesAllocationDone();
213
214 // Check if a call to SetLocalDescription is acceptable with |action|.
215 bool ExpectSetLocalDescription(Action action);
216 // Check if a call to SetRemoteDescription is acceptable with |action|.
217 bool ExpectSetRemoteDescription(Action action);
218 // Creates local session description with audio and video contents.
219 bool CreateDefaultLocalDescription();
220 // Enables media channels to allow sending of media.
221 void EnableChannels();
222 // Creates a JsepIceCandidate and adds it to the local session description
223 // and notify observers. Called when a new local candidate have been found.
224 void ProcessNewLocalCandidate(const std::string& content_name,
225 const cricket::Candidates& candidates);
226 // Returns the media index for a local ice candidate given the content name.
227 // Returns false if the local session description does not have a media
228 // content called |content_name|.
229 bool GetLocalCandidateMediaIndex(const std::string& content_name,
230 int* sdp_mline_index);
231 // Uses all remote candidates in |remote_desc| in this session.
232 bool UseCandidatesInSessionDescription(
233 const SessionDescriptionInterface* remote_desc);
234 // Uses |candidate| in this session.
235 bool UseCandidate(const IceCandidateInterface* candidate);
236 // Deletes the corresponding channel of contents that don't exist in |desc|.
237 // |desc| can be null. This means that all channels are deleted.
238 void RemoveUnusedChannelsAndTransports(
239 const cricket::SessionDescription* desc);
240
241 // Allocates media channels based on the |desc|. If |desc| doesn't have
242 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
243 // This method will also delete any existing media channels before creating.
244 bool CreateChannels(const cricket::SessionDescription* desc);
245
246 // Helper methods to create media channels.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000247 bool CreateVoiceChannel(const cricket::ContentInfo* content);
248 bool CreateVideoChannel(const cricket::ContentInfo* content);
249 bool CreateDataChannel(const cricket::ContentInfo* content);
250
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 // Copy the candidates from |saved_candidates_| to |dest_desc|.
252 // The |saved_candidates_| will be cleared after this function call.
253 void CopySavedCandidates(SessionDescriptionInterface* dest_desc);
254
255 // Forces |desc->crypto_required| to the appropriate state based on the
256 // current security policy, to ensure a failure occurs if there is an error
257 // in crypto negotiation.
258 // Called when processing the local session description.
259 void UpdateSessionDescriptionSecurePolicy(cricket::SessionDescription* desc);
260
261 bool GetLocalTrackId(uint32 ssrc, std::string* track_id);
262 bool GetRemoteTrackId(uint32 ssrc, std::string* track_id);
263
264 std::string BadStateErrMsg(const std::string& type, State state);
265 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
266
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000267 bool VerifyBundleSettings(const cricket::SessionDescription* desc);
268 bool HasRtcpMuxEnabled(const cricket::ContentInfo* content);
269
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 talk_base::scoped_ptr<cricket::VoiceChannel> voice_channel_;
271 talk_base::scoped_ptr<cricket::VideoChannel> video_channel_;
272 talk_base::scoped_ptr<cricket::DataChannel> data_channel_;
273 cricket::ChannelManager* channel_manager_;
274 cricket::TransportDescriptionFactory transport_desc_factory_;
275 cricket::MediaSessionDescriptionFactory session_desc_factory_;
276 MediaStreamSignaling* mediastream_signaling_;
277 IceObserver* ice_observer_;
278 PeerConnectionInterface::IceConnectionState ice_connection_state_;
279 talk_base::scoped_ptr<SessionDescriptionInterface> local_desc_;
280 talk_base::scoped_ptr<SessionDescriptionInterface> remote_desc_;
281 // Candidates that arrived before the remote description was set.
282 std::vector<IceCandidateInterface*> saved_candidates_;
283 uint64 session_version_;
284 // If the remote peer is using a older version of implementation.
285 bool older_version_remote_peer_;
286 // Specifies which kind of data channel is allowed. This is controlled
287 // by the chrome command-line flag and constraints:
288 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
289 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
290 // not set or false, SCTP is allowed (DCT_SCTP);
291 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
292 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
293 cricket::DataChannelType data_channel_type_;
294 talk_base::scoped_ptr<IceRestartAnswerLatch> ice_restart_latch_;
295 sigslot::signal0<> SignalVoiceChannelDestroyed;
296 sigslot::signal0<> SignalVideoChannelDestroyed;
297 sigslot::signal0<> SignalDataChannelDestroyed;
298};
299
300} // namespace webrtc
301
302#endif // TALK_APP_WEBRTC_WEBRTCSESSION_H_