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"); |
| 125 | } |
braveyao@webrtc.org | 9bfa5f0 | 2015-03-11 03:20:59 +0000 | [diff] [blame] | 126 | else |
| 127 | { |
| 128 | constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, |
| 129 | "false"); |
| 130 | } |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 131 | |
| 132 | peer_connection_ = |
| 133 | peer_connection_factory_->CreatePeerConnection(servers, |
| 134 | &constraints, |
| 135 | NULL, |
| 136 | NULL, |
| 137 | this); |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 138 | return peer_connection_.get() != NULL; |
| 139 | } |
| 140 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | void Conductor::DeletePeerConnection() { |
| 142 | peer_connection_ = NULL; |
| 143 | active_streams_.clear(); |
| 144 | main_wnd_->StopLocalRenderer(); |
| 145 | main_wnd_->StopRemoteRenderer(); |
| 146 | peer_connection_factory_ = NULL; |
| 147 | peer_id_ = -1; |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 148 | loopback_ = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void Conductor::EnsureStreamingUI() { |
| 152 | ASSERT(peer_connection_.get() != NULL); |
| 153 | if (main_wnd_->IsWindow()) { |
| 154 | if (main_wnd_->current_ui() != MainWindow::STREAMING) |
| 155 | main_wnd_->SwitchToStreamingUI(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // |
| 160 | // PeerConnectionObserver implementation. |
| 161 | // |
| 162 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | // Called when a remote stream is added |
| 164 | void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) { |
| 165 | LOG(INFO) << __FUNCTION__ << " " << stream->label(); |
| 166 | |
| 167 | stream->AddRef(); |
| 168 | main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED, |
| 169 | stream); |
| 170 | } |
| 171 | |
| 172 | void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) { |
| 173 | LOG(INFO) << __FUNCTION__ << " " << stream->label(); |
| 174 | stream->AddRef(); |
| 175 | main_wnd_->QueueUIThreadCallback(STREAM_REMOVED, |
| 176 | stream); |
| 177 | } |
| 178 | |
| 179 | void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) { |
| 180 | LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index(); |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 181 | // For loopback test. To save some connecting delay. |
| 182 | if (loopback_) { |
| 183 | if (!peer_connection_->AddIceCandidate(candidate)) { |
| 184 | LOG(WARNING) << "Failed to apply the received candidate"; |
| 185 | } |
| 186 | return; |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 187 | } |
| 188 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 189 | Json::StyledWriter writer; |
| 190 | Json::Value jmessage; |
| 191 | |
| 192 | jmessage[kCandidateSdpMidName] = candidate->sdp_mid(); |
| 193 | jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index(); |
| 194 | std::string sdp; |
| 195 | if (!candidate->ToString(&sdp)) { |
| 196 | LOG(LS_ERROR) << "Failed to serialize candidate"; |
| 197 | return; |
| 198 | } |
| 199 | jmessage[kCandidateSdpName] = sdp; |
| 200 | SendMessage(writer.write(jmessage)); |
| 201 | } |
| 202 | |
| 203 | // |
| 204 | // PeerConnectionClientObserver implementation. |
| 205 | // |
| 206 | |
| 207 | void Conductor::OnSignedIn() { |
| 208 | LOG(INFO) << __FUNCTION__; |
| 209 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 210 | } |
| 211 | |
| 212 | void Conductor::OnDisconnected() { |
| 213 | LOG(INFO) << __FUNCTION__; |
| 214 | |
| 215 | DeletePeerConnection(); |
| 216 | |
| 217 | if (main_wnd_->IsWindow()) |
| 218 | main_wnd_->SwitchToConnectUI(); |
| 219 | } |
| 220 | |
| 221 | void Conductor::OnPeerConnected(int id, const std::string& name) { |
| 222 | LOG(INFO) << __FUNCTION__; |
| 223 | // Refresh the list if we're showing it. |
| 224 | if (main_wnd_->current_ui() == MainWindow::LIST_PEERS) |
| 225 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 226 | } |
| 227 | |
| 228 | void Conductor::OnPeerDisconnected(int id) { |
| 229 | LOG(INFO) << __FUNCTION__; |
| 230 | if (id == peer_id_) { |
| 231 | LOG(INFO) << "Our peer disconnected"; |
| 232 | main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_CLOSED, NULL); |
| 233 | } else { |
| 234 | // Refresh the list if we're showing it. |
| 235 | if (main_wnd_->current_ui() == MainWindow::LIST_PEERS) |
| 236 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) { |
| 241 | ASSERT(peer_id_ == peer_id || peer_id_ == -1); |
| 242 | ASSERT(!message.empty()); |
| 243 | |
| 244 | if (!peer_connection_.get()) { |
| 245 | ASSERT(peer_id_ == -1); |
| 246 | peer_id_ = peer_id; |
| 247 | |
| 248 | if (!InitializePeerConnection()) { |
| 249 | LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance"; |
| 250 | client_->SignOut(); |
| 251 | return; |
| 252 | } |
| 253 | } else if (peer_id != peer_id_) { |
| 254 | ASSERT(peer_id_ != -1); |
| 255 | LOG(WARNING) << "Received a message from unknown peer while already in a " |
| 256 | "conversation with a different peer."; |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | Json::Reader reader; |
| 261 | Json::Value jmessage; |
| 262 | if (!reader.parse(message, jmessage)) { |
| 263 | LOG(WARNING) << "Received unknown message. " << message; |
| 264 | return; |
| 265 | } |
| 266 | std::string type; |
| 267 | std::string json_object; |
| 268 | |
Thiago Farina | cb76b89 | 2015-04-02 09:59:15 +0000 | [diff] [blame] | 269 | rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 270 | if (!type.empty()) { |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 271 | if (type == "offer-loopback") { |
| 272 | // This is a loopback call. |
| 273 | // Recreate the peerconnection with DTLS disabled. |
| 274 | if (!ReinitializePeerConnectionForLoopback()) { |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 275 | LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance"; |
| 276 | DeletePeerConnection(); |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 277 | client_->SignOut(); |
| 278 | } |
| 279 | return; |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 280 | } |
| 281 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 282 | std::string sdp; |
Thiago Farina | cb76b89 | 2015-04-02 09:59:15 +0000 | [diff] [blame] | 283 | if (!rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName, |
| 284 | &sdp)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | LOG(WARNING) << "Can't parse received session description message."; |
| 286 | return; |
| 287 | } |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 288 | webrtc::SdpParseError error; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 289 | webrtc::SessionDescriptionInterface* session_description( |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 290 | webrtc::CreateSessionDescription(type, sdp, &error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 291 | if (!session_description) { |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 292 | LOG(WARNING) << "Can't parse received session description message. " |
| 293 | << "SdpParseError was: " << error.description; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | LOG(INFO) << " Received session description :" << message; |
| 297 | peer_connection_->SetRemoteDescription( |
| 298 | DummySetSessionDescriptionObserver::Create(), session_description); |
| 299 | if (session_description->type() == |
| 300 | webrtc::SessionDescriptionInterface::kOffer) { |
| 301 | peer_connection_->CreateAnswer(this, NULL); |
| 302 | } |
| 303 | return; |
| 304 | } else { |
| 305 | std::string sdp_mid; |
| 306 | int sdp_mlineindex = 0; |
| 307 | std::string sdp; |
Thiago Farina | cb76b89 | 2015-04-02 09:59:15 +0000 | [diff] [blame] | 308 | if (!rtc::GetStringFromJsonObject(jmessage, kCandidateSdpMidName, |
| 309 | &sdp_mid) || |
| 310 | !rtc::GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName, |
| 311 | &sdp_mlineindex) || |
| 312 | !rtc::GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 313 | LOG(WARNING) << "Can't parse received message."; |
| 314 | return; |
| 315 | } |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 316 | webrtc::SdpParseError error; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 317 | rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate( |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 318 | webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp, &error)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 319 | if (!candidate.get()) { |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 320 | LOG(WARNING) << "Can't parse received candidate message. " |
| 321 | << "SdpParseError was: " << error.description; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 322 | return; |
| 323 | } |
| 324 | if (!peer_connection_->AddIceCandidate(candidate.get())) { |
| 325 | LOG(WARNING) << "Failed to apply the received candidate"; |
| 326 | return; |
| 327 | } |
| 328 | LOG(INFO) << " Received candidate :" << message; |
| 329 | return; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void Conductor::OnMessageSent(int err) { |
| 334 | // Process the next pending message if any. |
| 335 | main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL); |
| 336 | } |
| 337 | |
| 338 | void Conductor::OnServerConnectionFailure() { |
| 339 | main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(), |
| 340 | true); |
| 341 | } |
| 342 | |
| 343 | // |
| 344 | // MainWndCallback implementation. |
| 345 | // |
| 346 | |
| 347 | void Conductor::StartLogin(const std::string& server, int port) { |
| 348 | if (client_->is_connected()) |
| 349 | return; |
| 350 | server_ = server; |
| 351 | client_->Connect(server, port, GetPeerName()); |
| 352 | } |
| 353 | |
| 354 | void Conductor::DisconnectFromServer() { |
| 355 | if (client_->is_connected()) |
| 356 | client_->SignOut(); |
| 357 | } |
| 358 | |
| 359 | void Conductor::ConnectToPeer(int peer_id) { |
| 360 | ASSERT(peer_id_ == -1); |
| 361 | ASSERT(peer_id != -1); |
| 362 | |
| 363 | if (peer_connection_.get()) { |
| 364 | main_wnd_->MessageBox("Error", |
| 365 | "We only support connecting to one peer at a time", true); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | if (InitializePeerConnection()) { |
| 370 | peer_id_ = peer_id; |
| 371 | peer_connection_->CreateOffer(this, NULL); |
| 372 | } else { |
| 373 | main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | cricket::VideoCapturer* Conductor::OpenVideoCaptureDevice() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 378 | rtc::scoped_ptr<cricket::DeviceManagerInterface> dev_manager( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 379 | cricket::DeviceManagerFactory::Create()); |
| 380 | if (!dev_manager->Init()) { |
| 381 | LOG(LS_ERROR) << "Can't create device manager"; |
| 382 | return NULL; |
| 383 | } |
| 384 | std::vector<cricket::Device> devs; |
| 385 | if (!dev_manager->GetVideoCaptureDevices(&devs)) { |
| 386 | LOG(LS_ERROR) << "Can't enumerate video devices"; |
| 387 | return NULL; |
| 388 | } |
| 389 | std::vector<cricket::Device>::iterator dev_it = devs.begin(); |
| 390 | cricket::VideoCapturer* capturer = NULL; |
| 391 | for (; dev_it != devs.end(); ++dev_it) { |
| 392 | capturer = dev_manager->CreateVideoCapturer(*dev_it); |
| 393 | if (capturer != NULL) |
| 394 | break; |
| 395 | } |
| 396 | return capturer; |
| 397 | } |
| 398 | |
| 399 | void Conductor::AddStreams() { |
| 400 | if (active_streams_.find(kStreamLabel) != active_streams_.end()) |
| 401 | return; // Already added. |
| 402 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 403 | rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 404 | peer_connection_factory_->CreateAudioTrack( |
| 405 | kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL))); |
| 406 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 407 | rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 408 | peer_connection_factory_->CreateVideoTrack( |
| 409 | kVideoLabel, |
| 410 | peer_connection_factory_->CreateVideoSource(OpenVideoCaptureDevice(), |
| 411 | NULL))); |
| 412 | main_wnd_->StartLocalRenderer(video_track); |
| 413 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 414 | rtc::scoped_refptr<webrtc::MediaStreamInterface> stream = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 415 | peer_connection_factory_->CreateLocalMediaStream(kStreamLabel); |
| 416 | |
| 417 | stream->AddTrack(audio_track); |
| 418 | stream->AddTrack(video_track); |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 419 | if (!peer_connection_->AddStream(stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 420 | LOG(LS_ERROR) << "Adding stream to PeerConnection failed"; |
| 421 | } |
| 422 | typedef std::pair<std::string, |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 423 | rtc::scoped_refptr<webrtc::MediaStreamInterface> > |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 424 | MediaStreamPair; |
| 425 | active_streams_.insert(MediaStreamPair(stream->label(), stream)); |
| 426 | main_wnd_->SwitchToStreamingUI(); |
| 427 | } |
| 428 | |
| 429 | void Conductor::DisconnectFromCurrentPeer() { |
| 430 | LOG(INFO) << __FUNCTION__; |
| 431 | if (peer_connection_.get()) { |
| 432 | client_->SendHangUp(peer_id_); |
| 433 | DeletePeerConnection(); |
| 434 | } |
| 435 | |
| 436 | if (main_wnd_->IsWindow()) |
| 437 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 438 | } |
| 439 | |
| 440 | void Conductor::UIThreadCallback(int msg_id, void* data) { |
| 441 | switch (msg_id) { |
| 442 | case PEER_CONNECTION_CLOSED: |
| 443 | LOG(INFO) << "PEER_CONNECTION_CLOSED"; |
| 444 | DeletePeerConnection(); |
| 445 | |
| 446 | ASSERT(active_streams_.empty()); |
| 447 | |
| 448 | if (main_wnd_->IsWindow()) { |
| 449 | if (client_->is_connected()) { |
| 450 | main_wnd_->SwitchToPeerList(client_->peers()); |
| 451 | } else { |
| 452 | main_wnd_->SwitchToConnectUI(); |
| 453 | } |
| 454 | } else { |
| 455 | DisconnectFromServer(); |
| 456 | } |
| 457 | break; |
| 458 | |
| 459 | case SEND_MESSAGE_TO_PEER: { |
| 460 | LOG(INFO) << "SEND_MESSAGE_TO_PEER"; |
| 461 | std::string* msg = reinterpret_cast<std::string*>(data); |
| 462 | if (msg) { |
| 463 | // For convenience, we always run the message through the queue. |
| 464 | // This way we can be sure that messages are sent to the server |
| 465 | // in the same order they were signaled without much hassle. |
| 466 | pending_messages_.push_back(msg); |
| 467 | } |
| 468 | |
| 469 | if (!pending_messages_.empty() && !client_->IsSendingMessage()) { |
| 470 | msg = pending_messages_.front(); |
| 471 | pending_messages_.pop_front(); |
| 472 | |
| 473 | if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) { |
| 474 | LOG(LS_ERROR) << "SendToPeer failed"; |
| 475 | DisconnectFromServer(); |
| 476 | } |
| 477 | delete msg; |
| 478 | } |
| 479 | |
| 480 | if (!peer_connection_.get()) |
| 481 | peer_id_ = -1; |
| 482 | |
| 483 | break; |
| 484 | } |
| 485 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 486 | case NEW_STREAM_ADDED: { |
| 487 | webrtc::MediaStreamInterface* stream = |
| 488 | reinterpret_cast<webrtc::MediaStreamInterface*>( |
| 489 | data); |
| 490 | webrtc::VideoTrackVector tracks = stream->GetVideoTracks(); |
| 491 | // Only render the first track. |
| 492 | if (!tracks.empty()) { |
| 493 | webrtc::VideoTrackInterface* track = tracks[0]; |
| 494 | main_wnd_->StartRemoteRenderer(track); |
| 495 | } |
| 496 | stream->Release(); |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | case STREAM_REMOVED: { |
| 501 | // Remote peer stopped sending a stream. |
| 502 | webrtc::MediaStreamInterface* stream = |
| 503 | reinterpret_cast<webrtc::MediaStreamInterface*>( |
| 504 | data); |
| 505 | stream->Release(); |
| 506 | break; |
| 507 | } |
| 508 | |
| 509 | default: |
| 510 | ASSERT(false); |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void Conductor::OnSuccess(webrtc::SessionDescriptionInterface* desc) { |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 516 | peer_connection_->SetLocalDescription( |
| 517 | DummySetSessionDescriptionObserver::Create(), desc); |
| 518 | |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 519 | std::string sdp; |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 520 | desc->ToString(&sdp); |
| 521 | |
| 522 | // For loopback test. To save some connecting delay. |
| 523 | if (loopback_) { |
| 524 | // Replace message type from "offer" to "answer" |
| 525 | webrtc::SessionDescriptionInterface* session_description( |
jbauch | fabe2c9 | 2015-07-16 13:43:14 -0700 | [diff] [blame] | 526 | webrtc::CreateSessionDescription("answer", sdp, nullptr)); |
braveyao@webrtc.org | c68e0c9 | 2015-02-27 09:51:25 +0000 | [diff] [blame] | 527 | peer_connection_->SetRemoteDescription( |
| 528 | DummySetSessionDescriptionObserver::Create(), session_description); |
| 529 | return; |
| 530 | } |
braveyao@webrtc.org | a742cb1 | 2015-01-29 04:23:01 +0000 | [diff] [blame] | 531 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 532 | Json::StyledWriter writer; |
| 533 | Json::Value jmessage; |
| 534 | jmessage[kSessionDescriptionTypeName] = desc->type(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 535 | jmessage[kSessionDescriptionSdpName] = sdp; |
| 536 | SendMessage(writer.write(jmessage)); |
| 537 | } |
| 538 | |
| 539 | void Conductor::OnFailure(const std::string& error) { |
| 540 | LOG(LERROR) << error; |
| 541 | } |
| 542 | |
| 543 | void Conductor::SendMessage(const std::string& json_object) { |
| 544 | std::string* msg = new std::string(json_object); |
| 545 | main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg); |
| 546 | } |