henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 11 | #include "webrtc/examples/peerconnection/client/conductor.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
| 13 | #include <utility> |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 14 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 15 | |
| 16 | #include "talk/app/webrtc/videosourceinterface.h" |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 17 | #include "webrtc/examples/peerconnection/client/defaults.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 18 | #include "talk/media/devices/devicemanager.h" |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 19 | #include "talk/app/webrtc/test/fakeconstraints.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 20 | #include "webrtc/base/common.h" |
| 21 | #include "webrtc/base/json.h" |
| 22 | #include "webrtc/base/logging.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 23 | |
| 24 | // Names used for a IceCandidate JSON object. |
| 25 | const char kCandidateSdpMidName[] = "sdpMid"; |
| 26 | const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex"; |
| 27 | const char kCandidateSdpName[] = "candidate"; |
| 28 | |
| 29 | // Names used for a SessionDescription JSON object. |
| 30 | const char kSessionDescriptionTypeName[] = "type"; |
| 31 | const char kSessionDescriptionSdpName[] = "sdp"; |
| 32 | |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 33 | #define DTLS_ON true |
| 34 | #define DTLS_OFF false |
| 35 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | class DummySetSessionDescriptionObserver |
| 37 | : public webrtc::SetSessionDescriptionObserver { |
| 38 | public: |
| 39 | static DummySetSessionDescriptionObserver* Create() { |
| 40 | return |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 41 | new rtc::RefCountedObject<DummySetSessionDescriptionObserver>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | } |
| 43 | virtual void OnSuccess() { |
| 44 | LOG(INFO) << __FUNCTION__; |
| 45 | } |
| 46 | virtual void OnFailure(const std::string& error) { |
| 47 | LOG(INFO) << __FUNCTION__ << " " << error; |
| 48 | } |
| 49 | |
| 50 | protected: |
| 51 | DummySetSessionDescriptionObserver() {} |
| 52 | ~DummySetSessionDescriptionObserver() {} |
| 53 | }; |
| 54 | |
| 55 | Conductor::Conductor(PeerConnectionClient* client, MainWindow* main_wnd) |
| 56 | : peer_id_(-1), |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 57 | loopback_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | client_(client), |
| 59 | main_wnd_(main_wnd) { |
| 60 | client_->RegisterObserver(this); |
| 61 | main_wnd->RegisterObserver(this); |
| 62 | } |
| 63 | |
| 64 | Conductor::~Conductor() { |
| 65 | ASSERT(peer_connection_.get() == NULL); |
| 66 | } |
| 67 | |
| 68 | bool Conductor::connection_active() const { |
| 69 | return peer_connection_.get() != NULL; |
| 70 | } |
| 71 | |
| 72 | void Conductor::Close() { |
| 73 | client_->SignOut(); |
| 74 | DeletePeerConnection(); |
| 75 | } |
| 76 | |
| 77 | bool Conductor::InitializePeerConnection() { |
| 78 | ASSERT(peer_connection_factory_.get() == NULL); |
| 79 | ASSERT(peer_connection_.get() == NULL); |
| 80 | |
| 81 | peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(); |
| 82 | |
| 83 | if (!peer_connection_factory_.get()) { |
| 84 | main_wnd_->MessageBox("Error", |
| 85 | "Failed to initialize PeerConnectionFactory", true); |
| 86 | DeletePeerConnection(); |
| 87 | return false; |
| 88 | } |
| 89 | |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 90 | if (!CreatePeerConnection(DTLS_ON)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | main_wnd_->MessageBox("Error", |
| 92 | "CreatePeerConnection failed", true); |
| 93 | DeletePeerConnection(); |
| 94 | } |
| 95 | AddStreams(); |
| 96 | return peer_connection_.get() != NULL; |
| 97 | } |
| 98 | |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 99 | bool Conductor::ReinitializePeerConnectionForLoopback() { |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 100 | loopback_ = true; |
| 101 | rtc::scoped_refptr<webrtc::StreamCollectionInterface> streams( |
| 102 | peer_connection_->local_streams()); |
| 103 | peer_connection_ = NULL; |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 104 | if (CreatePeerConnection(DTLS_OFF)) { |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 105 | for (size_t i = 0; i < streams->count(); ++i) |
| 106 | peer_connection_->AddStream(streams->at(i)); |
| 107 | peer_connection_->CreateOffer(this, NULL); |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 108 | } |
| 109 | return peer_connection_.get() != NULL; |
| 110 | } |
| 111 | |
| 112 | bool Conductor::CreatePeerConnection(bool dtls) { |
| 113 | ASSERT(peer_connection_factory_.get() != NULL); |
| 114 | ASSERT(peer_connection_.get() == NULL); |
| 115 | |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 116 | webrtc::PeerConnectionInterface::IceServers servers; |
| 117 | webrtc::PeerConnectionInterface::IceServer server; |
| 118 | server.uri = GetPeerConnectionString(); |
| 119 | servers.push_back(server); |
| 120 | |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 121 | webrtc::FakeConstraints constraints; |
| 122 | if (dtls) { |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 123 | constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, |
| 124 | "true"); |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame^] | 125 | } else { |
braveyao@webrtc.org | 9bfa5f0 | 2015-03-11 03:20:59 +0000 | [diff] [blame] | 126 | constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, |
| 127 | "false"); |
| 128 | } |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 129 | |
| 130 | peer_connection_ = |
| 131 | peer_connection_factory_->CreatePeerConnection(servers, |
| 132 | &constraints, |
| 133 | NULL, |
| 134 | NULL, |
| 135 | this); |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 136 | return peer_connection_.get() != NULL; |
| 137 | } |
| 138 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | void Conductor::DeletePeerConnection() { |
| 140 | peer_connection_ = NULL; |
| 141 | active_streams_.clear(); |
| 142 | main_wnd_->StopLocalRenderer(); |
| 143 | main_wnd_->StopRemoteRenderer(); |
| 144 | peer_connection_factory_ = NULL; |
| 145 | peer_id_ = -1; |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 146 | loopback_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void Conductor::EnsureStreamingUI() { |
| 150 | ASSERT(peer_connection_.get() != NULL); |
| 151 | if (main_wnd_->IsWindow()) { |
| 152 | if (main_wnd_->current_ui() != MainWindow::STREAMING) |
| 153 | main_wnd_->SwitchToStreamingUI(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // |
| 158 | // PeerConnectionObserver implementation. |
| 159 | // |
| 160 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | // Called when a remote stream is added |
| 162 | void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) { |
| 163 | LOG(INFO) << __FUNCTION__ << " " << stream->label(); |
| 164 | |
| 165 | stream->AddRef(); |
| 166 | main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED, |
| 167 | stream); |
| 168 | } |
| 169 | |
| 170 | void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) { |
| 171 | LOG(INFO) << __FUNCTION__ << " " << stream->label(); |
| 172 | stream->AddRef(); |
| 173 | main_wnd_->QueueUIThreadCallback(STREAM_REMOVED, |
| 174 | stream); |
| 175 | } |
| 176 | |
| 177 | void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) { |
| 178 | LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index(); |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 179 | // For loopback test. To save some connecting delay. |
| 180 | if (loopback_) { |
| 181 | if (!peer_connection_->AddIceCandidate(candidate)) { |
| 182 | LOG(WARNING) << "Failed to apply the received candidate"; |
| 183 | } |
| 184 | return; |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 185 | } |
| 186 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 187 | Json::StyledWriter writer; |
| 188 | Json::Value jmessage; |
| 189 | |
| 190 | jmessage[kCandidateSdpMidName] = candidate->sdp_mid(); |
| 191 | jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index(); |
| 192 | std::string sdp; |
| 193 | if (!candidate->ToString(&sdp)) { |
| 194 | LOG(LS_ERROR) << "Failed to serialize candidate"; |
| 195 | return; |
| 196 | } |
| 197 | jmessage[kCandidateSdpName] = sdp; |
| 198 | SendMessage(writer.write(jmessage)); |
| 199 | } |
| 200 | |
| 201 | // |
| 202 | // PeerConnectionClientObserver implementation. |
| 203 | // |
| 204 | |
| 205 | void Conductor::OnSignedIn() { |
| 206 | LOG(INFO) << __FUNCTION__; |
| 207 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 208 | } |
| 209 | |
| 210 | void Conductor::OnDisconnected() { |
| 211 | LOG(INFO) << __FUNCTION__; |
| 212 | |
| 213 | DeletePeerConnection(); |
| 214 | |
| 215 | if (main_wnd_->IsWindow()) |
| 216 | main_wnd_->SwitchToConnectUI(); |
| 217 | } |
| 218 | |
| 219 | void Conductor::OnPeerConnected(int id, const std::string& name) { |
| 220 | LOG(INFO) << __FUNCTION__; |
| 221 | // Refresh the list if we're showing it. |
| 222 | if (main_wnd_->current_ui() == MainWindow::LIST_PEERS) |
| 223 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 224 | } |
| 225 | |
| 226 | void Conductor::OnPeerDisconnected(int id) { |
| 227 | LOG(INFO) << __FUNCTION__; |
| 228 | if (id == peer_id_) { |
| 229 | LOG(INFO) << "Our peer disconnected"; |
| 230 | main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_CLOSED, NULL); |
| 231 | } else { |
| 232 | // Refresh the list if we're showing it. |
| 233 | if (main_wnd_->current_ui() == MainWindow::LIST_PEERS) |
| 234 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) { |
| 239 | ASSERT(peer_id_ == peer_id || peer_id_ == -1); |
| 240 | ASSERT(!message.empty()); |
| 241 | |
| 242 | if (!peer_connection_.get()) { |
| 243 | ASSERT(peer_id_ == -1); |
| 244 | peer_id_ = peer_id; |
| 245 | |
| 246 | if (!InitializePeerConnection()) { |
| 247 | LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance"; |
| 248 | client_->SignOut(); |
| 249 | return; |
| 250 | } |
| 251 | } else if (peer_id != peer_id_) { |
| 252 | ASSERT(peer_id_ != -1); |
| 253 | LOG(WARNING) << "Received a message from unknown peer while already in a " |
| 254 | "conversation with a different peer."; |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | Json::Reader reader; |
| 259 | Json::Value jmessage; |
| 260 | if (!reader.parse(message, jmessage)) { |
| 261 | LOG(WARNING) << "Received unknown message. " << message; |
| 262 | return; |
| 263 | } |
| 264 | std::string type; |
| 265 | std::string json_object; |
| 266 | |
Thiago Farina | cb76b89 | 2015-04-02 09:59:15 +0000 | [diff] [blame] | 267 | rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 268 | if (!type.empty()) { |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 269 | if (type == "offer-loopback") { |
| 270 | // This is a loopback call. |
| 271 | // Recreate the peerconnection with DTLS disabled. |
| 272 | if (!ReinitializePeerConnectionForLoopback()) { |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 273 | LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance"; |
| 274 | DeletePeerConnection(); |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 275 | client_->SignOut(); |
| 276 | } |
| 277 | return; |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 278 | } |
| 279 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 280 | std::string sdp; |
Thiago Farina | cb76b89 | 2015-04-02 09:59:15 +0000 | [diff] [blame] | 281 | if (!rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName, |
| 282 | &sdp)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 283 | LOG(WARNING) << "Can't parse received session description message."; |
| 284 | return; |
| 285 | } |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 286 | webrtc::SdpParseError error; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 287 | webrtc::SessionDescriptionInterface* session_description( |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 288 | webrtc::CreateSessionDescription(type, sdp, &error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 289 | if (!session_description) { |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 290 | LOG(WARNING) << "Can't parse received session description message. " |
| 291 | << "SdpParseError was: " << error.description; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | LOG(INFO) << " Received session description :" << message; |
| 295 | peer_connection_->SetRemoteDescription( |
| 296 | DummySetSessionDescriptionObserver::Create(), session_description); |
| 297 | if (session_description->type() == |
| 298 | webrtc::SessionDescriptionInterface::kOffer) { |
| 299 | peer_connection_->CreateAnswer(this, NULL); |
| 300 | } |
| 301 | return; |
| 302 | } else { |
| 303 | std::string sdp_mid; |
| 304 | int sdp_mlineindex = 0; |
| 305 | std::string sdp; |
Thiago Farina | cb76b89 | 2015-04-02 09:59:15 +0000 | [diff] [blame] | 306 | if (!rtc::GetStringFromJsonObject(jmessage, kCandidateSdpMidName, |
| 307 | &sdp_mid) || |
| 308 | !rtc::GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName, |
| 309 | &sdp_mlineindex) || |
| 310 | !rtc::GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 311 | LOG(WARNING) << "Can't parse received message."; |
| 312 | return; |
| 313 | } |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 314 | webrtc::SdpParseError error; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 315 | rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate( |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 316 | webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp, &error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 317 | if (!candidate.get()) { |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 318 | LOG(WARNING) << "Can't parse received candidate message. " |
| 319 | << "SdpParseError was: " << error.description; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 320 | return; |
| 321 | } |
| 322 | if (!peer_connection_->AddIceCandidate(candidate.get())) { |
| 323 | LOG(WARNING) << "Failed to apply the received candidate"; |
| 324 | return; |
| 325 | } |
| 326 | LOG(INFO) << " Received candidate :" << message; |
| 327 | return; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void Conductor::OnMessageSent(int err) { |
| 332 | // Process the next pending message if any. |
| 333 | main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL); |
| 334 | } |
| 335 | |
| 336 | void Conductor::OnServerConnectionFailure() { |
| 337 | main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(), |
| 338 | true); |
| 339 | } |
| 340 | |
| 341 | // |
| 342 | // MainWndCallback implementation. |
| 343 | // |
| 344 | |
| 345 | void Conductor::StartLogin(const std::string& server, int port) { |
| 346 | if (client_->is_connected()) |
| 347 | return; |
| 348 | server_ = server; |
| 349 | client_->Connect(server, port, GetPeerName()); |
| 350 | } |
| 351 | |
| 352 | void Conductor::DisconnectFromServer() { |
| 353 | if (client_->is_connected()) |
| 354 | client_->SignOut(); |
| 355 | } |
| 356 | |
| 357 | void Conductor::ConnectToPeer(int peer_id) { |
| 358 | ASSERT(peer_id_ == -1); |
| 359 | ASSERT(peer_id != -1); |
| 360 | |
| 361 | if (peer_connection_.get()) { |
| 362 | main_wnd_->MessageBox("Error", |
| 363 | "We only support connecting to one peer at a time", true); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | if (InitializePeerConnection()) { |
| 368 | peer_id_ = peer_id; |
| 369 | peer_connection_->CreateOffer(this, NULL); |
| 370 | } else { |
| 371 | main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | cricket::VideoCapturer* Conductor::OpenVideoCaptureDevice() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 376 | rtc::scoped_ptr<cricket::DeviceManagerInterface> dev_manager( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 377 | cricket::DeviceManagerFactory::Create()); |
| 378 | if (!dev_manager->Init()) { |
| 379 | LOG(LS_ERROR) << "Can't create device manager"; |
| 380 | return NULL; |
| 381 | } |
| 382 | std::vector<cricket::Device> devs; |
| 383 | if (!dev_manager->GetVideoCaptureDevices(&devs)) { |
| 384 | LOG(LS_ERROR) << "Can't enumerate video devices"; |
| 385 | return NULL; |
| 386 | } |
| 387 | std::vector<cricket::Device>::iterator dev_it = devs.begin(); |
| 388 | cricket::VideoCapturer* capturer = NULL; |
| 389 | for (; dev_it != devs.end(); ++dev_it) { |
| 390 | capturer = dev_manager->CreateVideoCapturer(*dev_it); |
| 391 | if (capturer != NULL) |
| 392 | break; |
| 393 | } |
| 394 | return capturer; |
| 395 | } |
| 396 | |
| 397 | void Conductor::AddStreams() { |
| 398 | if (active_streams_.find(kStreamLabel) != active_streams_.end()) |
| 399 | return; // Already added. |
| 400 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 401 | rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 402 | peer_connection_factory_->CreateAudioTrack( |
| 403 | kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL))); |
| 404 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 405 | rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 406 | peer_connection_factory_->CreateVideoTrack( |
| 407 | kVideoLabel, |
| 408 | peer_connection_factory_->CreateVideoSource(OpenVideoCaptureDevice(), |
| 409 | NULL))); |
| 410 | main_wnd_->StartLocalRenderer(video_track); |
| 411 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 412 | rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 413 | peer_connection_factory_->CreateLocalMediaStream(kStreamLabel); |
| 414 | |
| 415 | stream->AddTrack(audio_track); |
| 416 | stream->AddTrack(video_track); |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 417 | if (!peer_connection_->AddStream(stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 418 | LOG(LS_ERROR) << "Adding stream to PeerConnection failed"; |
| 419 | } |
| 420 | typedef std::pair<std::string, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 421 | rtc::scoped_refptr<webrtc::MediaStreamInterface> > |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 422 | MediaStreamPair; |
| 423 | active_streams_.insert(MediaStreamPair(stream->label(), stream)); |
| 424 | main_wnd_->SwitchToStreamingUI(); |
| 425 | } |
| 426 | |
| 427 | void Conductor::DisconnectFromCurrentPeer() { |
| 428 | LOG(INFO) << __FUNCTION__; |
| 429 | if (peer_connection_.get()) { |
| 430 | client_->SendHangUp(peer_id_); |
| 431 | DeletePeerConnection(); |
| 432 | } |
| 433 | |
| 434 | if (main_wnd_->IsWindow()) |
| 435 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 436 | } |
| 437 | |
| 438 | void Conductor::UIThreadCallback(int msg_id, void* data) { |
| 439 | switch (msg_id) { |
| 440 | case PEER_CONNECTION_CLOSED: |
| 441 | LOG(INFO) << "PEER_CONNECTION_CLOSED"; |
| 442 | DeletePeerConnection(); |
| 443 | |
| 444 | ASSERT(active_streams_.empty()); |
| 445 | |
| 446 | if (main_wnd_->IsWindow()) { |
| 447 | if (client_->is_connected()) { |
| 448 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 449 | } else { |
| 450 | main_wnd_->SwitchToConnectUI(); |
| 451 | } |
| 452 | } else { |
| 453 | DisconnectFromServer(); |
| 454 | } |
| 455 | break; |
| 456 | |
| 457 | case SEND_MESSAGE_TO_PEER: { |
| 458 | LOG(INFO) << "SEND_MESSAGE_TO_PEER"; |
| 459 | std::string* msg = reinterpret_cast<std::string*>(data); |
| 460 | if (msg) { |
| 461 | // For convenience, we always run the message through the queue. |
| 462 | // This way we can be sure that messages are sent to the server |
| 463 | // in the same order they were signaled without much hassle. |
| 464 | pending_messages_.push_back(msg); |
| 465 | } |
| 466 | |
| 467 | if (!pending_messages_.empty() && !client_->IsSendingMessage()) { |
| 468 | msg = pending_messages_.front(); |
| 469 | pending_messages_.pop_front(); |
| 470 | |
| 471 | if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) { |
| 472 | LOG(LS_ERROR) << "SendToPeer failed"; |
| 473 | DisconnectFromServer(); |
| 474 | } |
| 475 | delete msg; |
| 476 | } |
| 477 | |
| 478 | if (!peer_connection_.get()) |
| 479 | peer_id_ = -1; |
| 480 | |
| 481 | break; |
| 482 | } |
| 483 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 484 | case NEW_STREAM_ADDED: { |
| 485 | webrtc::MediaStreamInterface* stream = |
| 486 | reinterpret_cast<webrtc::MediaStreamInterface*>( |
| 487 | data); |
| 488 | webrtc::VideoTrackVector tracks = stream->GetVideoTracks(); |
| 489 | // Only render the first track. |
| 490 | if (!tracks.empty()) { |
| 491 | webrtc::VideoTrackInterface* track = tracks[0]; |
| 492 | main_wnd_->StartRemoteRenderer(track); |
| 493 | } |
| 494 | stream->Release(); |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | case STREAM_REMOVED: { |
| 499 | // Remote peer stopped sending a stream. |
| 500 | webrtc::MediaStreamInterface* stream = |
| 501 | reinterpret_cast<webrtc::MediaStreamInterface*>( |
| 502 | data); |
| 503 | stream->Release(); |
| 504 | break; |
| 505 | } |
| 506 | |
| 507 | default: |
| 508 | ASSERT(false); |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | void Conductor::OnSuccess(webrtc::SessionDescriptionInterface* desc) { |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 514 | peer_connection_->SetLocalDescription( |
| 515 | DummySetSessionDescriptionObserver::Create(), desc); |
| 516 | |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 517 | std::string sdp; |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 518 | desc->ToString(&sdp); |
| 519 | |
| 520 | // For loopback test. To save some connecting delay. |
| 521 | if (loopback_) { |
| 522 | // Replace message type from "offer" to "answer" |
| 523 | webrtc::SessionDescriptionInterface* session_description( |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 524 | webrtc::CreateSessionDescription("answer", sdp, nullptr)); |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 525 | peer_connection_->SetRemoteDescription( |
| 526 | DummySetSessionDescriptionObserver::Create(), session_description); |
| 527 | return; |
| 528 | } |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 529 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 530 | Json::StyledWriter writer; |
| 531 | Json::Value jmessage; |
| 532 | jmessage[kSessionDescriptionTypeName] = desc->type(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 533 | jmessage[kSessionDescriptionSdpName] = sdp; |
| 534 | SendMessage(writer.write(jmessage)); |
| 535 | } |
| 536 | |
| 537 | void Conductor::OnFailure(const std::string& error) { |
| 538 | LOG(LERROR) << error; |
| 539 | } |
| 540 | |
| 541 | void Conductor::SendMessage(const std::string& json_object) { |
| 542 | std::string* msg = new std::string(json_object); |
| 543 | main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg); |
| 544 | } |