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
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "examples/peerconnection/client/conductor.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2016-05-01 14:53:46 -07:00
|
|
|
#include <memory>
|
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
|
|
|
|
2017-10-17 14:48:54 +02:00
|
|
|
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
|
|
|
|
|
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/test/fakeconstraints.h"
|
|
|
|
|
#include "examples/peerconnection/client/defaults.h"
|
|
|
|
|
#include "media/engine/webrtcvideocapturerfactory.h"
|
|
|
|
|
#include "modules/video_capture/video_capture_factory.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
|
#include "rtc_base/json.h"
|
|
|
|
|
#include "rtc_base/logging.h"
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2017-12-07 10:27:41 -08:00
|
|
|
using webrtc::SdpType;
|
|
|
|
|
|
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
|
|
|
}
|
2017-11-09 11:09:25 +01:00
|
|
|
virtual void OnSuccess() { RTC_LOG(INFO) << __FUNCTION__; }
|
2013-07-10 00:45:36 +00:00
|
|
|
virtual void OnFailure(const std::string& error) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << __FUNCTION__ << " " << error;
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(peer_connection_.get() == NULL);
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Conductor::connection_active() const {
|
|
|
|
|
return peer_connection_.get() != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::Close() {
|
|
|
|
|
client_->SignOut();
|
|
|
|
|
DeletePeerConnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Conductor::InitializePeerConnection() {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(peer_connection_factory_.get() == NULL);
|
|
|
|
|
RTC_DCHECK(peer_connection_.get() == NULL);
|
2013-07-10 00:45:36 +00:00
|
|
|
|
2017-10-17 14:48:54 +02:00
|
|
|
peer_connection_factory_ = webrtc::CreatePeerConnectionFactory(
|
|
|
|
|
webrtc::CreateBuiltinAudioEncoderFactory(),
|
|
|
|
|
webrtc::CreateBuiltinAudioDecoderFactory());
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
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) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(peer_connection_factory_.get() != NULL);
|
|
|
|
|
RTC_DCHECK(peer_connection_.get() == NULL);
|
2015-01-29 04:23:01 +00:00
|
|
|
|
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(
|
Revert of Replacing DtlsIdentityStoreInterface with RTCCertificateGeneratorInterface. (patchset #2 id:20001 of https://codereview.webrtc.org/2013523002/ )
Reason for revert:
There are more CreatePeerConnection calls than I anticipated/had found in Chromium, like remoting/protocol/webrtc_transport.cc. Reverting due to broken Chromium FYI bots.
Original issue's description:
> Replacing DtlsIdentityStoreInterface with RTCCertificateGeneratorInterface.
>
> The store was used in WebRtcSessionDescriptionFactory to generate certificates,
> now a generator is used instead (new API). PeerConnection[Factory][Interface],
> and WebRtcSession are updated to pass generators all the way down to the
> WebRtcSessionDescriptionFactory instead of stores.
>
> The webrtc implementation of a generator, RTCCertificateGenerator, is used as
> the default generator (peerconnectionfactory.cc:189) instead of the webrtc
> implementation of a store, DtlsIdentityStoreImpl.
> The generator is fully parameterized and does not generate RSA-1024 unless you
> ask for it (which makes sense not to do beforehand since ECDSA is now default).
> The store was not fully parameterized (known filed bug).
>
> The "top" layer, PeerConnectionFactoryInterface::CreatePeerConnection, is
> updated to take a generator instead of a store. But as to not break Chromium,
> the old function signature taking a store is kept. It is implemented to invoke
> the generator version by wrapping the store in an
> RTCCertificateGeneratorStoreWrapper. As soon as Chromium is updated to use the
> new function signature we can remove the old CreatePeerConnection.
> Due to having multiple CreatePeerConnection signatures, some calling places
> are updated to resolve the ambiguity introduced.
>
> BUG=webrtc:5707, webrtc:5708
> R=phoglund@webrtc.org, tommi@webrtc.org
> TBR=tkchin@webrc.org
>
> Committed: https://chromium.googlesource.com/external/webrtc/+/400781a2091d09a725b32c6953247036b22478e8
TBR=tkchin@webrtc.org,tommi@webrtc.org,phoglund@webrtc.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=webrtc:5707, webrtc:5708
Review-Url: https://codereview.webrtc.org/2020633002
Cr-Commit-Position: refs/heads/master@{#12948}
2016-05-27 06:08:53 -07:00
|
|
|
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() {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(peer_connection_.get() != NULL);
|
2013-07-10 00:45:36 +00:00
|
|
|
if (main_wnd_->IsWindow()) {
|
|
|
|
|
if (main_wnd_->current_ui() != MainWindow::STREAMING)
|
|
|
|
|
main_wnd_->SwitchToStreamingUI();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// PeerConnectionObserver implementation.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Called when a remote stream is added
|
2016-05-31 13:02:21 -07:00
|
|
|
void Conductor::OnAddStream(
|
|
|
|
|
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << __FUNCTION__ << " " << stream->label();
|
2016-05-31 13:02:21 -07:00
|
|
|
main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED, stream.release());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-31 13:02:21 -07:00
|
|
|
void Conductor::OnRemoveStream(
|
|
|
|
|
rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << __FUNCTION__ << " " << stream->label();
|
2016-05-31 13:02:21 -07:00
|
|
|
main_wnd_->QueueUIThreadCallback(STREAM_REMOVED, stream.release());
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_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)) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(WARNING) << "Failed to apply the received candidate";
|
2015-02-27 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
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)) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Failed to serialize candidate";
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
jmessage[kCandidateSdpName] = sdp;
|
|
|
|
|
SendMessage(writer.write(jmessage));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// PeerConnectionClientObserver implementation.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
void Conductor::OnSignedIn() {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << __FUNCTION__;
|
2013-07-10 00:45:36 +00:00
|
|
|
main_wnd_->SwitchToPeerList(client_->peers());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnDisconnected() {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << __FUNCTION__;
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
DeletePeerConnection();
|
|
|
|
|
|
|
|
|
|
if (main_wnd_->IsWindow())
|
|
|
|
|
main_wnd_->SwitchToConnectUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnPeerConnected(int id, const std::string& name) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << __FUNCTION__;
|
2013-07-10 00:45:36 +00:00
|
|
|
// 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) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << __FUNCTION__;
|
2013-07-10 00:45:36 +00:00
|
|
|
if (id == peer_id_) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << "Our peer disconnected";
|
2013-07-10 00:45:36 +00:00
|
|
|
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) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(peer_id_ == peer_id || peer_id_ == -1);
|
|
|
|
|
RTC_DCHECK(!message.empty());
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
if (!peer_connection_.get()) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(peer_id_ == -1);
|
2013-07-10 00:45:36 +00:00
|
|
|
peer_id_ = peer_id;
|
|
|
|
|
|
|
|
|
|
if (!InitializePeerConnection()) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
|
2013-07-10 00:45:36 +00:00
|
|
|
client_->SignOut();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else if (peer_id != peer_id_) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(peer_id_ != -1);
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(WARNING)
|
|
|
|
|
<< "Received a message from unknown peer while already in a "
|
|
|
|
|
"conversation with a different peer.";
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Json::Reader reader;
|
|
|
|
|
Json::Value jmessage;
|
|
|
|
|
if (!reader.parse(message, jmessage)) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(WARNING) << "Received unknown message. " << message;
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2017-12-07 10:27:41 -08:00
|
|
|
std::string type_str;
|
2013-07-10 00:45:36 +00:00
|
|
|
std::string json_object;
|
|
|
|
|
|
2017-12-07 10:27:41 -08:00
|
|
|
rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName,
|
|
|
|
|
&type_str);
|
|
|
|
|
if (!type_str.empty()) {
|
|
|
|
|
if (type_str == "offer-loopback") {
|
2015-02-27 09:51:25 +00:00
|
|
|
// This is a loopback call.
|
|
|
|
|
// Recreate the peerconnection with DTLS disabled.
|
|
|
|
|
if (!ReinitializePeerConnectionForLoopback()) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Failed to initialize our PeerConnection instance";
|
2015-01-29 04:23:01 +00:00
|
|
|
DeletePeerConnection();
|
2015-02-27 09:51:25 +00:00
|
|
|
client_->SignOut();
|
|
|
|
|
}
|
|
|
|
|
return;
|
2015-01-29 04:23:01 +00:00
|
|
|
}
|
2017-12-07 10:27:41 -08:00
|
|
|
rtc::Optional<SdpType> type_maybe = webrtc::SdpTypeFromString(type_str);
|
|
|
|
|
if (!type_maybe) {
|
|
|
|
|
RTC_LOG(LS_ERROR) << "Unknown SDP type: " << type_str;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SdpType type = *type_maybe;
|
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)) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(WARNING) << "Can't parse received session description message.";
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2015-07-16 13:43:14 -07:00
|
|
|
webrtc::SdpParseError error;
|
2017-12-07 10:27:41 -08:00
|
|
|
std::unique_ptr<webrtc::SessionDescriptionInterface> session_description =
|
|
|
|
|
webrtc::CreateSessionDescription(type, sdp, &error);
|
2013-07-10 00:45:36 +00:00
|
|
|
if (!session_description) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(WARNING) << "Can't parse received session description message. "
|
|
|
|
|
<< "SdpParseError was: " << error.description;
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << " Received session description :" << message;
|
2013-07-10 00:45:36 +00:00
|
|
|
peer_connection_->SetRemoteDescription(
|
2017-12-07 10:27:41 -08:00
|
|
|
DummySetSessionDescriptionObserver::Create(),
|
|
|
|
|
session_description.release());
|
|
|
|
|
if (type == SdpType::kOffer) {
|
2013-07-10 00:45:36 +00:00
|
|
|
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)) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(WARNING) << "Can't parse received message.";
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2015-07-16 13:43:14 -07:00
|
|
|
webrtc::SdpParseError error;
|
2016-05-01 14:53:46 -07:00
|
|
|
std::unique_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()) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_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())) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(WARNING) << "Failed to apply the received candidate";
|
2013-07-10 00:45:36 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << " Received candidate :" << message;
|
2013-07-10 00:45:36 +00:00
|
|
|
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) {
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(peer_id_ == -1);
|
|
|
|
|
RTC_DCHECK(peer_id != -1);
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 20:13:37 -08:00
|
|
|
std::unique_ptr<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(
|
2016-12-12 00:22:56 -08:00
|
|
|
webrtc::VideoCaptureFactory::CreateDeviceInfo());
|
2016-03-16 09:34:56 -07:00
|
|
|
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;
|
2017-02-10 20:13:37 -08:00
|
|
|
std::unique_ptr<cricket::VideoCapturer> capturer;
|
2016-03-16 09:34:56 -07:00
|
|
|
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)) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "Adding stream to PeerConnection failed";
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << __FUNCTION__;
|
2013-07-10 00:45:36 +00:00
|
|
|
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:
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << "PEER_CONNECTION_CLOSED";
|
2013-07-10 00:45:36 +00:00
|
|
|
DeletePeerConnection();
|
|
|
|
|
|
2017-01-12 05:15:36 -08:00
|
|
|
RTC_DCHECK(active_streams_.empty());
|
2013-07-10 00:45:36 +00:00
|
|
|
|
|
|
|
|
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: {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(INFO) << "SEND_MESSAGE_TO_PEER";
|
2013-07-10 00:45:36 +00:00
|
|
|
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) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LS_ERROR) << "SendToPeer failed";
|
2013-07-10 00:45:36 +00:00
|
|
|
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:
|
2017-01-11 05:56:46 -08:00
|
|
|
RTC_NOTREACHED();
|
2013-07-10 00:45:36 +00:00
|
|
|
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"
|
2017-12-07 10:27:41 -08:00
|
|
|
std::unique_ptr<webrtc::SessionDescriptionInterface> session_description =
|
|
|
|
|
webrtc::CreateSessionDescription(SdpType::kAnswer, sdp);
|
2015-02-27 09:51:25 +00:00
|
|
|
peer_connection_->SetRemoteDescription(
|
2017-12-07 10:27:41 -08:00
|
|
|
DummySetSessionDescriptionObserver::Create(),
|
|
|
|
|
session_description.release());
|
2015-02-27 09:51:25 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2015-01-29 04:23:01 +00:00
|
|
|
|
2013-07-10 00:45:36 +00:00
|
|
|
Json::StyledWriter writer;
|
|
|
|
|
Json::Value jmessage;
|
2017-12-07 10:27:41 -08:00
|
|
|
jmessage[kSessionDescriptionTypeName] =
|
|
|
|
|
webrtc::SdpTypeToString(desc->GetType());
|
2013-07-10 00:45:36 +00:00
|
|
|
jmessage[kSessionDescriptionSdpName] = sdp;
|
|
|
|
|
SendMessage(writer.write(jmessage));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::OnFailure(const std::string& error) {
|
2017-11-09 11:09:25 +01:00
|
|
|
RTC_LOG(LERROR) << error;
|
2013-07-10 00:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Conductor::SendMessage(const std::string& json_object) {
|
|
|
|
|
std::string* msg = new std::string(json_object);
|
|
|
|
|
main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg);
|
|
|
|
|
}
|