2013-07-10 00:45:36 +00:00
|
|
|
/*
|
2015-08-05 15:48:13 -07:00
|
|
|
* Copyright 2012 The WebRTC Project Authors. All rights reserved.
|
2013-07-10 00:45:36 +00:00
|
|
|
*
|
2015-08-05 15:48:13 -07:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2013-07-10 00:45:36 +00:00
|
|
|
*/
|
|
|
|
|
|
2015-08-05 15:48:13 -07:00
|
|
|
#include "webrtc/examples/peerconnection/client/conductor.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
#include <utility>
|
2015-02-27 09:51:25 +00:00
|
|
|
#include <vector>
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-02-10 10:53:12 +01:00
|
|
|
#include "webrtc/api/test/fakeconstraints.h"
|
2014-07-29 17:36:52 +00:00
|
|
|
#include "webrtc/base/common.h"
|
|
|
|
|
#include "webrtc/base/json.h"
|
|
|
|
|
#include "webrtc/base/logging.h"
|
Move talk/media to webrtc/media
I removed the 'libjingle' target in talk/libjingle.gyp and replaced
all users of it with base/base.gyp:rtc_base. It seems the jsoncpp
and expat dependencies were not used by it's previous references.
The files in talk/media/testdata were uploaded to Google Storage and
added .sha1 files in resources/media instead of simply moving them.
The previously disabled warnings that were inherited from
talk/build/common.gypi are now replaced by target-specific disabling
of only the failing warnings. Additional disabling was needed since the stricter
compilation warnings that applies to code in webrtc/.
License headers will be updated in a follow-up CL in order to not
break Git history.
Other modifications:
* Updated the header guards.
* Sorted the includes using chromium/src/tools/sort-headers.py
except for these files:
talk/app/webrtc/peerconnectionendtoend_unittest.cc
talk/app/webrtc/java/jni/androidmediadecoder_jni.cc
talk/app/webrtc/java/jni/androidmediaencoder_jni.cc
webrtc/media/devices/win32devicemanager.cc.
* Unused GYP reference to libjingle_tests_additional_deps was removed.
* Removed duplicated GYP entries of
webrtc/base/testutils.cc
webrtc/base/testutils.h
The HAVE_WEBRTC_VIDEO and HAVE_WEBRTC_VOICE defines were used by only talk/media,
so they were moved to the media.gyp.
I also checked that none of
EXPAT_RELATIVE_PATH,
FEATURE_ENABLE_VOICEMAIL,
GTEST_RELATIVE_PATH,
JSONCPP_RELATIVE_PATH,
LOGGING=1,
SRTP_RELATIVE_PATH,
FEATURE_ENABLE_SSL,
FEATURE_ENABLE_VOICEMAIL,
FEATURE_ENABLE_PSTN,
HAVE_SCTP,
HAVE_SRTP,
are used by the talk/media code.
For Chromium, the following changes will need to be applied to the roll CL that updates the
DEPS for WebRTC and libjingle: https://codereview.chromium.org/1604303002/
BUG=webrtc:5420
NOPRESUBMIT=True
TBR=tommi@webrtc.org
Review URL: https://codereview.webrtc.org/1587193006
Cr-Commit-Position: refs/heads/master@{#11495}
2016-02-04 23:52:28 -08:00
|
|
|
#include "webrtc/examples/peerconnection/client/defaults.h"
|
2016-03-16 09:34:56 -07:00
|
|
|
#include "webrtc/media/engine/webrtcvideocapturerfactory.h"
|
|
|
|
|
#include "webrtc/modules/video_capture/video_capture_factory.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
// Names used for a IceCandidate JSON object.
|
|
|
|
|
const char kCandidateSdpMidName[] = "sdpMid";
|
|
|
|
|
const char kCandidateSdpMlineIndexName[] = "sdpMLineIndex";
|
|
|
|
|
const char kCandidateSdpName[] = "candidate";
|
|
|
|
|
|
|
|
|
|
// Names used for a SessionDescription JSON object.
|
|
|
|
|
const char kSessionDescriptionTypeName[] = "type";
|
|
|
|
|
const char kSessionDescriptionSdpName[] = "sdp";
|
|
|
|
|
|
2015-01-29 04:23:01 +00:00
|
|
|
#define DTLS_ON true
|
|
|
|
|
#define DTLS_OFF false
|
|
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
class DummySetSessionDescriptionObserver
|
|
|
|
|
: public webrtc::SetSessionDescriptionObserver {
|
|
|
|
|
public:
|
|
|
|
|
static DummySetSessionDescriptionObserver* Create() {
|
|
|
|
|
return
|
2014-07-29 17:36:52 +00:00
|
|
|
new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
virtual void OnSuccess() {
|
|
|
|
|
LOG(INFO) << __FUNCTION__;
|
|
|
|
|
}
|
|
|
|
|
virtual void OnFailure(const std::string& error) {
|
|
|
|
|
LOG(INFO) << __FUNCTION__ << " " << error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
DummySetSessionDescriptionObserver() {}
|
|
|
|
|
~DummySetSessionDescriptionObserver() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Conductor::Conductor(PeerConnectionClient* client, MainWindow* main_wnd)
|
|
|
|
|
: peer_id_(-1),
|
2015-01-29 04:23:01 +00:00
|
|
|
loopback_(false),
|
2013-07-10 00:45:36 +00:00
|
|
|
client_(client),
|
|
|
|
|
main_wnd_(main_wnd) {
|
|
|
|
|
client_->RegisterObserver(this);
|
|
|
|
|
main_wnd->RegisterObserver(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Conductor::~Conductor() {
|
|
|
|
|
ASSERT(peer_connection_.get() == NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Conductor::connection_active() const {
|
|
|
|
|
return peer_connection_.get() != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::Close() {
|
|
|
|
|
client_->SignOut();
|
|
|
|
|
DeletePeerConnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Conductor::InitializePeerConnection() {
|
|
|
|
|
ASSERT(peer_connection_factory_.get() == NULL);
|
|
|
|
|
ASSERT(peer_connection_.get() == NULL);
|
|
|
|
|
|
|
|
|
|
peer_connection_factory_ = webrtc::CreatePeerConnectionFactory();
|
|
|
|
|
|
|
|
|
|
if (!peer_connection_factory_.get()) {
|
|
|
|
|
main_wnd_->MessageBox("Error",
|
|
|
|
|
"Failed to initialize PeerConnectionFactory", true);
|
|
|
|
|
DeletePeerConnection();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 09:51:25 +00:00
|
|
|
if (!CreatePeerConnection(DTLS_ON)) {
|
2013-07-10 00:45:36 +00:00
|
|
|
main_wnd_->MessageBox("Error",
|
|
|
|
|
"CreatePeerConnection failed", true);
|
|
|
|
|
DeletePeerConnection();
|
|
|
|
|
}
|
|
|
|
|
AddStreams();
|
|
|
|
|
return peer_connection_.get() != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-29 04:23:01 +00:00
|
|
|
bool Conductor::ReinitializePeerConnectionForLoopback() {
|
2015-02-27 09:51:25 +00:00
|
|
|
loopback_ = true;
|
|
|
|
|
rtc::scoped_refptr<webrtc::StreamCollectionInterface> streams(
|
|
|
|
|
peer_connection_->local_streams());
|
|
|
|
|
peer_connection_ = NULL;
|
2015-01-29 04:23:01 +00:00
|
|
|
if (CreatePeerConnection(DTLS_OFF)) {
|
2015-02-27 09:51:25 +00:00
|
|
|
for (size_t i = 0; i < streams->count(); ++i)
|
|
|
|
|
peer_connection_->AddStream(streams->at(i));
|
|
|
|
|
peer_connection_->CreateOffer(this, NULL);
|
2015-01-29 04:23:01 +00:00
|
|
|
}
|
|
|
|
|
return peer_connection_.get() != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Conductor::CreatePeerConnection(bool dtls) {
|
|
|
|
|
ASSERT(peer_connection_factory_.get() != NULL);
|
|
|
|
|
ASSERT(peer_connection_.get() == NULL);
|
|
|
|
|
|
2015-12-29 14:14:52 -08:00
|
|
|
webrtc::PeerConnectionInterface::RTCConfiguration config;
|
2015-02-27 09:51:25 +00:00
|
|
|
webrtc::PeerConnectionInterface::IceServer server;
|
|
|
|
|
server.uri = GetPeerConnectionString();
|
2015-12-29 14:14:52 -08:00
|
|
|
config.servers.push_back(server);
|
2015-02-27 09:51:25 +00:00
|
|
|
|
2015-01-29 04:23:01 +00:00
|
|
|
webrtc::FakeConstraints constraints;
|
|
|
|
|
if (dtls) {
|
2015-02-27 09:51:25 +00:00
|
|
|
constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
|
|
|
|
|
"true");
|
2015-12-09 14:18:14 -08:00
|
|
|
} else {
|
2015-03-11 03:20:59 +00:00
|
|
|
constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,
|
|
|
|
|
"false");
|
|
|
|
|
}
|
2015-02-27 09:51:25 +00:00
|
|
|
|
2015-12-29 14:14:52 -08:00
|
|
|
peer_connection_ = peer_connection_factory_->CreatePeerConnection(
|
|
|
|
|
config, &constraints, NULL, NULL, this);
|
2015-01-29 04:23:01 +00:00
|
|
|
return peer_connection_.get() != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
void Conductor::DeletePeerConnection() {
|
|
|
|
|
peer_connection_ = NULL;
|
|
|
|
|
active_streams_.clear();
|
|
|
|
|
main_wnd_->StopLocalRenderer();
|
|
|
|
|
main_wnd_->StopRemoteRenderer();
|
|
|
|
|
peer_connection_factory_ = NULL;
|
|
|
|
|
peer_id_ = -1;
|
2015-01-29 04:23:01 +00:00
|
|
|
loopback_ = false;
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::EnsureStreamingUI() {
|
|
|
|
|
ASSERT(peer_connection_.get() != NULL);
|
|
|
|
|
if (main_wnd_->IsWindow()) {
|
|
|
|
|
if (main_wnd_->current_ui() != MainWindow::STREAMING)
|
|
|
|
|
main_wnd_->SwitchToStreamingUI();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// PeerConnectionObserver implementation.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Called when a remote stream is added
|
|
|
|
|
void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) {
|
|
|
|
|
LOG(INFO) << __FUNCTION__ << " " << stream->label();
|
|
|
|
|
|
|
|
|
|
stream->AddRef();
|
|
|
|
|
main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED,
|
|
|
|
|
stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) {
|
|
|
|
|
LOG(INFO) << __FUNCTION__ << " " << stream->label();
|
|
|
|
|
stream->AddRef();
|
|
|
|
|
main_wnd_->QueueUIThreadCallback(STREAM_REMOVED,
|
|
|
|
|
stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
|
|
|
|
|
LOG(INFO) << __FUNCTION__ << " " << candidate->sdp_mline_index();
|
2015-02-27 09:51:25 +00:00
|
|
|
// For loopback test. To save some connecting delay.
|
|
|
|
|
if (loopback_) {
|
|
|
|
|
if (!peer_connection_->AddIceCandidate(candidate)) {
|
|
|
|
|
LOG(WARNING) << "Failed to apply the received candidate";
|
|
|
|
|
}
|
|
|
|
|
return;
|
2015-01-29 04:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
Json::StyledWriter writer;
|
|
|
|
|
Json::Value jmessage;
|
|
|
|
|
|
|
|
|
|
jmessage[kCandidateSdpMidName] = candidate->sdp_mid();
|
|
|
|
|
jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index();
|
|
|
|
|
std::string sdp;
|
|
|
|
|
if (!candidate->ToString(&sdp)) {
|
|
|
|
|
LOG(LS_ERROR) << "Failed to serialize candidate";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
jmessage[kCandidateSdpName] = sdp;
|
|
|
|
|
SendMessage(writer.write(jmessage));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// PeerConnectionClientObserver implementation.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
void Conductor::OnSignedIn() {
|
|
|
|
|
LOG(INFO) << __FUNCTION__;
|
|
|
|
|
main_wnd_->SwitchToPeerList(client_->peers());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnDisconnected() {
|
|
|
|
|
LOG(INFO) << __FUNCTION__;
|
|
|
|
|
|
|
|
|
|
DeletePeerConnection();
|
|
|
|
|
|
|
|
|
|
if (main_wnd_->IsWindow())
|
|
|
|
|
main_wnd_->SwitchToConnectUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnPeerConnected(int id, const std::string& name) {
|
|
|
|
|
LOG(INFO) << __FUNCTION__;
|
|
|
|
|
// Refresh the list if we're showing it.
|
|
|
|
|
if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
|
|
|
|
|
main_wnd_->SwitchToPeerList(client_->peers());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnPeerDisconnected(int id) {
|
|
|
|
|
LOG(INFO) << __FUNCTION__;
|
|
|
|
|
if (id == peer_id_) {
|
|
|
|
|
LOG(INFO) << "Our peer disconnected";
|
|
|
|
|
main_wnd_->QueueUIThreadCallback(PEER_CONNECTION_CLOSED, NULL);
|
|
|
|
|
} else {
|
|
|
|
|
// Refresh the list if we're showing it.
|
|
|
|
|
if (main_wnd_->current_ui() == MainWindow::LIST_PEERS)
|
|
|
|
|
main_wnd_->SwitchToPeerList(client_->peers());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnMessageFromPeer(int peer_id, const std::string& message) {
|
|
|
|
|
ASSERT(peer_id_ == peer_id || peer_id_ == -1);
|
|
|
|
|
ASSERT(!message.empty());
|
|
|
|
|
|
|
|
|
|
if (!peer_connection_.get()) {
|
|
|
|
|
ASSERT(peer_id_ == -1);
|
|
|
|
|
peer_id_ = peer_id;
|
|
|
|
|
|
|
|
|
|
if (!InitializePeerConnection()) {
|
|
|
|
|
LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
|
|
|
|
|
client_->SignOut();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else if (peer_id != peer_id_) {
|
|
|
|
|
ASSERT(peer_id_ != -1);
|
|
|
|
|
LOG(WARNING) << "Received a message from unknown peer while already in a "
|
|
|
|
|
"conversation with a different peer.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json::Reader reader;
|
|
|
|
|
Json::Value jmessage;
|
|
|
|
|
if (!reader.parse(message, jmessage)) {
|
|
|
|
|
LOG(WARNING) << "Received unknown message. " << message;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
std::string type;
|
|
|
|
|
std::string json_object;
|
|
|
|
|
|
2015-04-02 09:59:15 +00:00
|
|
|
rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type);
|
2013-07-10 00:45:36 +00:00
|
|
|
if (!type.empty()) {
|
2015-02-27 09:51:25 +00:00
|
|
|
if (type == "offer-loopback") {
|
|
|
|
|
// This is a loopback call.
|
|
|
|
|
// Recreate the peerconnection with DTLS disabled.
|
|
|
|
|
if (!ReinitializePeerConnectionForLoopback()) {
|
2015-01-29 04:23:01 +00:00
|
|
|
LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
|
|
|
|
|
DeletePeerConnection();
|
2015-02-27 09:51:25 +00:00
|
|
|
client_->SignOut();
|
|
|
|
|
}
|
|
|
|
|
return;
|
2015-01-29 04:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
std::string sdp;
|
2015-04-02 09:59:15 +00:00
|
|
|
if (!rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionSdpName,
|
|
|
|
|
&sdp)) {
|
2013-07-10 00:45:36 +00:00
|
|
|
LOG(WARNING) << "Can't parse received session description message.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-16 13:43:14 -07:00
|
|
|
webrtc::SdpParseError error;
|
2013-07-10 00:45:36 +00:00
|
|
|
webrtc::SessionDescriptionInterface* session_description(
|
2015-07-16 13:43:14 -07:00
|
|
|
webrtc::CreateSessionDescription(type, sdp, &error));
|
2013-07-10 00:45:36 +00:00
|
|
|
if (!session_description) {
|
2015-07-16 13:43:14 -07:00
|
|
|
LOG(WARNING) << "Can't parse received session description message. "
|
|
|
|
|
<< "SdpParseError was: " << error.description;
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LOG(INFO) << " Received session description :" << message;
|
|
|
|
|
peer_connection_->SetRemoteDescription(
|
|
|
|
|
DummySetSessionDescriptionObserver::Create(), session_description);
|
|
|
|
|
if (session_description->type() ==
|
|
|
|
|
webrtc::SessionDescriptionInterface::kOffer) {
|
|
|
|
|
peer_connection_->CreateAnswer(this, NULL);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
std::string sdp_mid;
|
|
|
|
|
int sdp_mlineindex = 0;
|
|
|
|
|
std::string sdp;
|
2015-04-02 09:59:15 +00:00
|
|
|
if (!rtc::GetStringFromJsonObject(jmessage, kCandidateSdpMidName,
|
|
|
|
|
&sdp_mid) ||
|
|
|
|
|
!rtc::GetIntFromJsonObject(jmessage, kCandidateSdpMlineIndexName,
|
|
|
|
|
&sdp_mlineindex) ||
|
|
|
|
|
!rtc::GetStringFromJsonObject(jmessage, kCandidateSdpName, &sdp)) {
|
2013-07-10 00:45:36 +00:00
|
|
|
LOG(WARNING) << "Can't parse received message.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-16 13:43:14 -07:00
|
|
|
webrtc::SdpParseError error;
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
|
2015-07-16 13:43:14 -07:00
|
|
|
webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp, &error));
|
2013-07-10 00:45:36 +00:00
|
|
|
if (!candidate.get()) {
|
2015-07-16 13:43:14 -07:00
|
|
|
LOG(WARNING) << "Can't parse received candidate message. "
|
|
|
|
|
<< "SdpParseError was: " << error.description;
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!peer_connection_->AddIceCandidate(candidate.get())) {
|
|
|
|
|
LOG(WARNING) << "Failed to apply the received candidate";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LOG(INFO) << " Received candidate :" << message;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnMessageSent(int err) {
|
|
|
|
|
// Process the next pending message if any.
|
|
|
|
|
main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnServerConnectionFailure() {
|
|
|
|
|
main_wnd_->MessageBox("Error", ("Failed to connect to " + server_).c_str(),
|
|
|
|
|
true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// MainWndCallback implementation.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
void Conductor::StartLogin(const std::string& server, int port) {
|
|
|
|
|
if (client_->is_connected())
|
|
|
|
|
return;
|
|
|
|
|
server_ = server;
|
|
|
|
|
client_->Connect(server, port, GetPeerName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::DisconnectFromServer() {
|
|
|
|
|
if (client_->is_connected())
|
|
|
|
|
client_->SignOut();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::ConnectToPeer(int peer_id) {
|
|
|
|
|
ASSERT(peer_id_ == -1);
|
|
|
|
|
ASSERT(peer_id != -1);
|
|
|
|
|
|
|
|
|
|
if (peer_connection_.get()) {
|
|
|
|
|
main_wnd_->MessageBox("Error",
|
|
|
|
|
"We only support connecting to one peer at a time", true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (InitializePeerConnection()) {
|
|
|
|
|
peer_id_ = peer_id;
|
|
|
|
|
peer_connection_->CreateOffer(this, NULL);
|
|
|
|
|
} else {
|
|
|
|
|
main_wnd_->MessageBox("Error", "Failed to initialize PeerConnection", true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cricket::VideoCapturer* Conductor::OpenVideoCaptureDevice() {
|
2016-03-16 09:34:56 -07:00
|
|
|
std::vector<std::string> device_names;
|
|
|
|
|
{
|
|
|
|
|
std::unique_ptr<webrtc::VideoCaptureModule::DeviceInfo> info(
|
|
|
|
|
webrtc::VideoCaptureFactory::CreateDeviceInfo(0));
|
|
|
|
|
if (!info) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
int num_devices = info->NumberOfDevices();
|
|
|
|
|
for (int i = 0; i < num_devices; ++i) {
|
|
|
|
|
const uint32_t kSize = 256;
|
|
|
|
|
char name[kSize] = {0};
|
|
|
|
|
char id[kSize] = {0};
|
|
|
|
|
if (info->GetDeviceName(i, name, kSize, id, kSize) != -1) {
|
|
|
|
|
device_names.push_back(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
2016-03-16 09:34:56 -07:00
|
|
|
|
|
|
|
|
cricket::WebRtcVideoDeviceCapturerFactory factory;
|
|
|
|
|
cricket::VideoCapturer* capturer = nullptr;
|
|
|
|
|
for (const auto& name : device_names) {
|
|
|
|
|
capturer = factory.Create(cricket::Device(name, 0));
|
|
|
|
|
if (capturer) {
|
2013-07-10 00:45:36 +00:00
|
|
|
break;
|
2016-03-16 09:34:56 -07:00
|
|
|
}
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
return capturer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::AddStreams() {
|
|
|
|
|
if (active_streams_.find(kStreamLabel) != active_streams_.end())
|
|
|
|
|
return; // Already added.
|
|
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
|
2013-07-10 00:45:36 +00:00
|
|
|
peer_connection_factory_->CreateAudioTrack(
|
|
|
|
|
kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL)));
|
|
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
|
2013-07-10 00:45:36 +00:00
|
|
|
peer_connection_factory_->CreateVideoTrack(
|
|
|
|
|
kVideoLabel,
|
|
|
|
|
peer_connection_factory_->CreateVideoSource(OpenVideoCaptureDevice(),
|
|
|
|
|
NULL)));
|
|
|
|
|
main_wnd_->StartLocalRenderer(video_track);
|
|
|
|
|
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
|
2013-07-10 00:45:36 +00:00
|
|
|
peer_connection_factory_->CreateLocalMediaStream(kStreamLabel);
|
|
|
|
|
|
|
|
|
|
stream->AddTrack(audio_track);
|
|
|
|
|
stream->AddTrack(video_track);
|
2014-11-04 11:31:29 +00:00
|
|
|
if (!peer_connection_->AddStream(stream)) {
|
2013-07-10 00:45:36 +00:00
|
|
|
LOG(LS_ERROR) << "Adding stream to PeerConnection failed";
|
|
|
|
|
}
|
|
|
|
|
typedef std::pair<std::string,
|
2014-07-29 17:36:52 +00:00
|
|
|
rtc::scoped_refptr<webrtc::MediaStreamInterface> >
|
2013-07-10 00:45:36 +00:00
|
|
|
MediaStreamPair;
|
|
|
|
|
active_streams_.insert(MediaStreamPair(stream->label(), stream));
|
|
|
|
|
main_wnd_->SwitchToStreamingUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::DisconnectFromCurrentPeer() {
|
|
|
|
|
LOG(INFO) << __FUNCTION__;
|
|
|
|
|
if (peer_connection_.get()) {
|
|
|
|
|
client_->SendHangUp(peer_id_);
|
|
|
|
|
DeletePeerConnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (main_wnd_->IsWindow())
|
|
|
|
|
main_wnd_->SwitchToPeerList(client_->peers());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::UIThreadCallback(int msg_id, void* data) {
|
|
|
|
|
switch (msg_id) {
|
|
|
|
|
case PEER_CONNECTION_CLOSED:
|
|
|
|
|
LOG(INFO) << "PEER_CONNECTION_CLOSED";
|
|
|
|
|
DeletePeerConnection();
|
|
|
|
|
|
|
|
|
|
ASSERT(active_streams_.empty());
|
|
|
|
|
|
|
|
|
|
if (main_wnd_->IsWindow()) {
|
|
|
|
|
if (client_->is_connected()) {
|
|
|
|
|
main_wnd_->SwitchToPeerList(client_->peers());
|
|
|
|
|
} else {
|
|
|
|
|
main_wnd_->SwitchToConnectUI();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
DisconnectFromServer();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEND_MESSAGE_TO_PEER: {
|
|
|
|
|
LOG(INFO) << "SEND_MESSAGE_TO_PEER";
|
|
|
|
|
std::string* msg = reinterpret_cast<std::string*>(data);
|
|
|
|
|
if (msg) {
|
|
|
|
|
// For convenience, we always run the message through the queue.
|
|
|
|
|
// This way we can be sure that messages are sent to the server
|
|
|
|
|
// in the same order they were signaled without much hassle.
|
|
|
|
|
pending_messages_.push_back(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pending_messages_.empty() && !client_->IsSendingMessage()) {
|
|
|
|
|
msg = pending_messages_.front();
|
|
|
|
|
pending_messages_.pop_front();
|
|
|
|
|
|
|
|
|
|
if (!client_->SendToPeer(peer_id_, *msg) && peer_id_ != -1) {
|
|
|
|
|
LOG(LS_ERROR) << "SendToPeer failed";
|
|
|
|
|
DisconnectFromServer();
|
|
|
|
|
}
|
|
|
|
|
delete msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!peer_connection_.get())
|
|
|
|
|
peer_id_ = -1;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case NEW_STREAM_ADDED: {
|
|
|
|
|
webrtc::MediaStreamInterface* stream =
|
|
|
|
|
reinterpret_cast<webrtc::MediaStreamInterface*>(
|
|
|
|
|
data);
|
|
|
|
|
webrtc::VideoTrackVector tracks = stream->GetVideoTracks();
|
|
|
|
|
// Only render the first track.
|
|
|
|
|
if (!tracks.empty()) {
|
|
|
|
|
webrtc::VideoTrackInterface* track = tracks[0];
|
|
|
|
|
main_wnd_->StartRemoteRenderer(track);
|
|
|
|
|
}
|
|
|
|
|
stream->Release();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case STREAM_REMOVED: {
|
|
|
|
|
// Remote peer stopped sending a stream.
|
|
|
|
|
webrtc::MediaStreamInterface* stream =
|
|
|
|
|
reinterpret_cast<webrtc::MediaStreamInterface*>(
|
|
|
|
|
data);
|
|
|
|
|
stream->Release();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
ASSERT(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnSuccess(webrtc::SessionDescriptionInterface* desc) {
|
2015-02-27 09:51:25 +00:00
|
|
|
peer_connection_->SetLocalDescription(
|
|
|
|
|
DummySetSessionDescriptionObserver::Create(), desc);
|
|
|
|
|
|
2015-01-29 04:23:01 +00:00
|
|
|
std::string sdp;
|
2015-02-27 09:51:25 +00:00
|
|
|
desc->ToString(&sdp);
|
|
|
|
|
|
|
|
|
|
// For loopback test. To save some connecting delay.
|
|
|
|
|
if (loopback_) {
|
|
|
|
|
// Replace message type from "offer" to "answer"
|
|
|
|
|
webrtc::SessionDescriptionInterface* session_description(
|
2015-07-16 13:43:14 -07:00
|
|
|
webrtc::CreateSessionDescription("answer", sdp, nullptr));
|
2015-02-27 09:51:25 +00:00
|
|
|
peer_connection_->SetRemoteDescription(
|
|
|
|
|
DummySetSessionDescriptionObserver::Create(), session_description);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-01-29 04:23:01 +00:00
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
Json::StyledWriter writer;
|
|
|
|
|
Json::Value jmessage;
|
|
|
|
|
jmessage[kSessionDescriptionTypeName] = desc->type();
|
|
|
|
|
jmessage[kSessionDescriptionSdpName] = sdp;
|
|
|
|
|
SendMessage(writer.write(jmessage));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnFailure(const std::string& error) {
|
|
|
|
|
LOG(LERROR) << error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::SendMessage(const std::string& json_object) {
|
|
|
|
|
std::string* msg = new std::string(json_object);
|
|
|
|
|
main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg);
|
|
|
|
|
}
|