Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: talk/examples/peerconnection/client/conductor.cc

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

Powered by Google App Engine
This is Rietveld 408576698