blob: f4fe202bdf97122ada7418fb7c369c03bf2f4488 [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[];
66extern const char kInvalidCandidates[];
67extern const char kInvalidSdp[];
68extern const char kMlineMismatch[];
69extern const char kSdpWithoutCrypto[];
70extern const char kSessionError[];
71extern const char kUpdateStateFailed[];
72extern const char kPushDownOfferTDFailed[];
73extern const char kPushDownPranswerTDFailed[];
74extern const char kPushDownAnswerTDFailed[];
75
76// ICE state callback interface.
77class IceObserver {
78 public:
79 // Called any time the IceConnectionState changes
80 virtual void OnIceConnectionChange(
81 PeerConnectionInterface::IceConnectionState new_state) {}
82 // Called any time the IceGatheringState changes
83 virtual void OnIceGatheringChange(
84 PeerConnectionInterface::IceGatheringState new_state) {}
85 // New Ice candidate have been found.
86 virtual void OnIceCandidate(const IceCandidateInterface* candidate) = 0;
87 // All Ice candidates have been found.
88 // TODO(bemasc): Remove this once callers transition to OnIceGatheringChange.
89 // (via PeerConnectionObserver)
90 virtual void OnIceComplete() {}
91
92 protected:
93 ~IceObserver() {}
94};
95
96class WebRtcSession : public cricket::BaseSession,
97 public AudioProviderInterface,
98 public DataChannelFactory,
99 public VideoProviderInterface,
100 public DtmfProviderInterface {
101 public:
102 WebRtcSession(cricket::ChannelManager* channel_manager,
103 talk_base::Thread* signaling_thread,
104 talk_base::Thread* worker_thread,
105 cricket::PortAllocator* port_allocator,
106 MediaStreamSignaling* mediastream_signaling);
107 virtual ~WebRtcSession();
108
109 bool Initialize(const MediaConstraintsInterface* constraints);
110 // Deletes the voice, video and data channel and changes the session state
111 // to STATE_RECEIVEDTERMINATE.
112 void Terminate();
113
114 void RegisterIceObserver(IceObserver* observer) {
115 ice_observer_ = observer;
116 }
117
118 virtual cricket::VoiceChannel* voice_channel() {
119 return voice_channel_.get();
120 }
121 virtual cricket::VideoChannel* video_channel() {
122 return video_channel_.get();
123 }
124 virtual cricket::DataChannel* data_channel() {
125 return data_channel_.get();
126 }
127
128 void set_secure_policy(cricket::SecureMediaPolicy secure_policy);
129 cricket::SecureMediaPolicy secure_policy() const {
130 return session_desc_factory_.secure();
131 }
132
133 // Generic error message callback from WebRtcSession.
134 // TODO - It may be necessary to supply error code as well.
135 sigslot::signal0<> SignalError;
136
137 SessionDescriptionInterface* CreateOffer(
138 const MediaConstraintsInterface* constraints);
139
140 SessionDescriptionInterface* CreateAnswer(
141 const MediaConstraintsInterface* constraints);
142
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000143 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 bool SetLocalDescription(SessionDescriptionInterface* desc,
145 std::string* err_desc);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000146 // The ownership of |desc| will be transferred after this call.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 bool SetRemoteDescription(SessionDescriptionInterface* desc,
148 std::string* err_desc);
149 bool ProcessIceMessage(const IceCandidateInterface* ice_candidate);
150 const SessionDescriptionInterface* local_description() const {
151 return local_desc_.get();
152 }
153 const SessionDescriptionInterface* remote_description() const {
154 return remote_desc_.get();
155 }
156
157 // Get the id used as a media stream track's "id" field from ssrc.
158 virtual bool GetTrackIdBySsrc(uint32 ssrc, std::string* id);
159
160 // AudioMediaProviderInterface implementation.
161 virtual void SetAudioPlayout(uint32 ssrc, bool enable) OVERRIDE;
162 virtual void SetAudioSend(uint32 ssrc, bool enable,
163 const cricket::AudioOptions& options) OVERRIDE;
164 virtual bool SetAudioRenderer(uint32 ssrc,
165 cricket::AudioRenderer* renderer) OVERRIDE;
166
167 // Implements VideoMediaProviderInterface.
168 virtual bool SetCaptureDevice(uint32 ssrc,
169 cricket::VideoCapturer* camera) OVERRIDE;
170 virtual void SetVideoPlayout(uint32 ssrc,
171 bool enable,
172 cricket::VideoRenderer* renderer) OVERRIDE;
173 virtual void SetVideoSend(uint32 ssrc, bool enable,
174 const cricket::VideoOptions* options) OVERRIDE;
175
176 // Implements DtmfProviderInterface.
177 virtual bool CanInsertDtmf(const std::string& track_id);
178 virtual bool InsertDtmf(const std::string& track_id,
179 int code, int duration);
180 virtual sigslot::signal0<>* GetOnDestroyedSignal();
181
182 talk_base::scoped_refptr<DataChannel> CreateDataChannel(
183 const std::string& label,
184 const DataChannelInit* config);
185
186 cricket::DataChannelType data_channel_type() const;
187
188 private:
189 // Indicates the type of SessionDescription in a call to SetLocalDescription
190 // and SetRemoteDescription.
191 enum Action {
192 kOffer,
193 kPrAnswer,
194 kAnswer,
195 };
196 // Invokes ConnectChannels() on transport proxies, which initiates ice
197 // candidates allocation.
198 bool StartCandidatesAllocation();
199 bool UpdateSessionState(Action action, cricket::ContentSource source,
200 const cricket::SessionDescription* desc,
201 std::string* err_desc);
202 static Action GetAction(const std::string& type);
203
204 // Transport related callbacks, override from cricket::BaseSession.
205 virtual void OnTransportRequestSignaling(cricket::Transport* transport);
206 virtual void OnTransportConnecting(cricket::Transport* transport);
207 virtual void OnTransportWritable(cricket::Transport* transport);
208 virtual void OnTransportProxyCandidatesReady(
209 cricket::TransportProxy* proxy,
210 const cricket::Candidates& candidates);
211 virtual void OnCandidatesAllocationDone();
212
213 // Check if a call to SetLocalDescription is acceptable with |action|.
214 bool ExpectSetLocalDescription(Action action);
215 // Check if a call to SetRemoteDescription is acceptable with |action|.
216 bool ExpectSetRemoteDescription(Action action);
217 // Creates local session description with audio and video contents.
218 bool CreateDefaultLocalDescription();
219 // Enables media channels to allow sending of media.
220 void EnableChannels();
221 // Creates a JsepIceCandidate and adds it to the local session description
222 // and notify observers. Called when a new local candidate have been found.
223 void ProcessNewLocalCandidate(const std::string& content_name,
224 const cricket::Candidates& candidates);
225 // Returns the media index for a local ice candidate given the content name.
226 // Returns false if the local session description does not have a media
227 // content called |content_name|.
228 bool GetLocalCandidateMediaIndex(const std::string& content_name,
229 int* sdp_mline_index);
230 // Uses all remote candidates in |remote_desc| in this session.
231 bool UseCandidatesInSessionDescription(
232 const SessionDescriptionInterface* remote_desc);
233 // Uses |candidate| in this session.
234 bool UseCandidate(const IceCandidateInterface* candidate);
235 // Deletes the corresponding channel of contents that don't exist in |desc|.
236 // |desc| can be null. This means that all channels are deleted.
237 void RemoveUnusedChannelsAndTransports(
238 const cricket::SessionDescription* desc);
239
240 // Allocates media channels based on the |desc|. If |desc| doesn't have
241 // the BUNDLE option, this method will disable BUNDLE in PortAllocator.
242 // This method will also delete any existing media channels before creating.
243 bool CreateChannels(const cricket::SessionDescription* desc);
244
245 // Helper methods to create media channels.
246 bool CreateVoiceChannel(const cricket::SessionDescription* desc);
247 bool CreateVideoChannel(const cricket::SessionDescription* desc);
248 bool CreateDataChannel(const cricket::SessionDescription* desc);
249 // Copy the candidates from |saved_candidates_| to |dest_desc|.
250 // The |saved_candidates_| will be cleared after this function call.
251 void CopySavedCandidates(SessionDescriptionInterface* dest_desc);
252
253 // Forces |desc->crypto_required| to the appropriate state based on the
254 // current security policy, to ensure a failure occurs if there is an error
255 // in crypto negotiation.
256 // Called when processing the local session description.
257 void UpdateSessionDescriptionSecurePolicy(cricket::SessionDescription* desc);
258
259 bool GetLocalTrackId(uint32 ssrc, std::string* track_id);
260 bool GetRemoteTrackId(uint32 ssrc, std::string* track_id);
261
262 std::string BadStateErrMsg(const std::string& type, State state);
263 void SetIceConnectionState(PeerConnectionInterface::IceConnectionState state);
264
265 talk_base::scoped_ptr<cricket::VoiceChannel> voice_channel_;
266 talk_base::scoped_ptr<cricket::VideoChannel> video_channel_;
267 talk_base::scoped_ptr<cricket::DataChannel> data_channel_;
268 cricket::ChannelManager* channel_manager_;
269 cricket::TransportDescriptionFactory transport_desc_factory_;
270 cricket::MediaSessionDescriptionFactory session_desc_factory_;
271 MediaStreamSignaling* mediastream_signaling_;
272 IceObserver* ice_observer_;
273 PeerConnectionInterface::IceConnectionState ice_connection_state_;
274 talk_base::scoped_ptr<SessionDescriptionInterface> local_desc_;
275 talk_base::scoped_ptr<SessionDescriptionInterface> remote_desc_;
276 // Candidates that arrived before the remote description was set.
277 std::vector<IceCandidateInterface*> saved_candidates_;
278 uint64 session_version_;
279 // If the remote peer is using a older version of implementation.
280 bool older_version_remote_peer_;
281 // Specifies which kind of data channel is allowed. This is controlled
282 // by the chrome command-line flag and constraints:
283 // 1. If chrome command-line switch 'enable-sctp-data-channels' is enabled,
284 // constraint kEnableDtlsSrtp is true, and constaint kEnableRtpDataChannels is
285 // not set or false, SCTP is allowed (DCT_SCTP);
286 // 2. If constraint kEnableRtpDataChannels is true, RTP is allowed (DCT_RTP);
287 // 3. If both 1&2 are false, data channel is not allowed (DCT_NONE).
288 cricket::DataChannelType data_channel_type_;
289 talk_base::scoped_ptr<IceRestartAnswerLatch> ice_restart_latch_;
290 sigslot::signal0<> SignalVoiceChannelDestroyed;
291 sigslot::signal0<> SignalVideoChannelDestroyed;
292 sigslot::signal0<> SignalDataChannelDestroyed;
293};
294
295} // namespace webrtc
296
297#endif // TALK_APP_WEBRTC_WEBRTCSESSION_H_