Chromium Code Reviews| Index: remoting/host/cast_extension_session.cc |
| diff --git a/remoting/host/cast_extension_session.cc b/remoting/host/cast_extension_session.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1ee0c9cf03c32bbf79a8eea0b6106923d1f97237 |
| --- /dev/null |
| +++ b/remoting/host/cast_extension_session.cc |
| @@ -0,0 +1,656 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/host/cast_extension_session.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +// #include "remoting/host/cast_video_capturer.h" |
|
aiguha
2014/08/04 17:45:36
As mentioned earlier:
The CastVideoCapturer CL sho
Wez
2014/08/07 01:25:32
when you have dependent CLs, just keep them in dep
aiguha
2014/08/12 01:42:36
Thanks for the advice. Done!
|
| +#include "remoting/host/chromium_port_allocator_factory.h" |
| +#include "remoting/host/client_session.h" |
| +#include "remoting/proto/control.pb.h" |
| +#include "remoting/protocol/client_stub.h" |
| +#include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h" |
| +#include "third_party/libjingle/source/talk/app/webrtc/test/fakeconstraints.h" |
| +#include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h" |
| +#include "third_party/webrtc/modules/desktop_capture/mouse_cursor_shape.h" |
| + |
| +namespace remoting { |
| + |
| +// Constant keys used in JSON messages to the host. |
| +// Must keep synced with webapp. |
| +const char kMessageData[] = "data"; // Use chromoting_data for CCv2. |
|
Wez
2014/08/07 01:25:33
What is CCv2?
aiguha
2014/08/12 01:42:37
This comment shouldn't have made it in, I'll remov
|
| +const char kMessageSubject[] = "subject"; |
| +const char kMessageType[] = "cast_message"; |
| + |
| +const char kSubjectNewCandidate[] = "webrtc_candidate"; |
| +const char kSubjectReady[] = "ready"; |
| +// const char kSubjectSDP[] = "webrtc_sdp"; |
|
Wez
2014/08/07 01:25:31
Why is this commented out?
aiguha
2014/08/12 01:42:37
Not used anymore, removed!
|
| +const char kSubjectTest[] = "test"; |
| +const char kSubjectOffer[] = "webrtc_offer"; |
| +const char kSubjectAnswer[] = "webrtc_answer"; |
| + |
| +const char kWebRtcPrefix[] = "webrtc_"; |
|
Wez
2014/08/07 01:25:31
What's this used for?
aiguha
2014/08/12 01:42:38
It was being used to construct the subject of a me
|
| + |
| +// WebRTC Headers inside cast extension messages. |
|
Wez
2014/08/07 01:25:30
s/Headers/headers
aiguha
2014/08/12 01:42:36
Done.
|
| +const char kWebRtcCandidate[] = "candidate"; |
| +const char kWebRtcSessionDescType[] = "type"; |
| +const char kWebRtcSessionDescSDP[] = "sdp"; |
| +const char kWebRtcSDPMid[] = "sdpMid"; |
| +const char kWebRtcSDPMLineIndex[] = "sdpMLineIndex"; |
| + |
| +// Constants used by PeerConnection. |
|
Wez
2014/08/07 01:25:33
You mean "over the PeerConnection"?
aiguha
2014/08/12 01:42:36
The constants are used by the related objects, but
Wez
2014/08/12 22:14:59
Well, it depends - are these things that get sent
aiguha
2014/08/13 18:33:27
The labels are used over the connection and the St
|
| +const char kVideoLabel[] = "cast_video_label"; |
| +const char kStreamLabel[] = "stream_label"; |
| +const char kDefaultStunURI[] = "stun:stun.l.google.com:19302"; |
|
Wez
2014/08/07 01:25:33
Where does this come from?
aiguha
2014/08/12 01:42:38
I believe this is Google's public STUN server. The
|
| + |
| +const char kWorkerThreadName[] = "CastExtensionSessionWorkerThread"; |
| + |
| +static const int kStatsLogIntervalSec = 10; |
| + |
| +//------------------------------------------------------------------------------ |
|
Wez
2014/08/07 01:25:30
Suggest getting rid of this separator comment.
aiguha
2014/08/12 01:42:36
Done.
|
| + |
| +// webrtc::SetSessionDescriptionObserver implementation. |
|
Wez
2014/08/07 01:25:30
Suggest replacing this with a short description of
aiguha
2014/08/12 01:42:39
Done.
|
| +class CastSetSessionDescriptionObserver |
| + : public webrtc::SetSessionDescriptionObserver { |
| + public: |
| + static CastSetSessionDescriptionObserver* Create() { |
| + return new talk_base::RefCountedObject<CastSetSessionDescriptionObserver>(); |
| + } |
| + virtual void OnSuccess() OVERRIDE { |
| + VLOG(1) << "SetSessionDescriptionObserver success."; |
|
Wez
2014/08/07 01:25:32
Don't you mean "SetSessionDescription succeeded."?
aiguha
2014/08/12 01:42:38
Yes, changed!
|
| + } |
| + virtual void OnFailure(const std::string& error) OVERRIDE { |
| + LOG(ERROR) << "CastSetSessionDescriptionObserver" << __FUNCTION__ << " " |
| + << error; |
|
Wez
2014/08/07 01:25:33
And "SetSessionDescription failed: <error>"?
aiguha
2014/08/12 01:42:36
Done.
|
| + } |
| + |
| + protected: |
| + CastSetSessionDescriptionObserver() {} |
| + virtual ~CastSetSessionDescriptionObserver() {} |
| +}; |
|
Wez
2014/08/07 01:25:32
DISALLOW_COPY_AND_ASSIGN()
aiguha
2014/08/12 01:42:36
Done.
|
| + |
| +// webrtc::CreateSessionDescriptionObserver implementation. |
| +class CastCreateSessionDescriptionObserver |
| + : public webrtc::CreateSessionDescriptionObserver { |
| + public: |
| + static CastCreateSessionDescriptionObserver* Create( |
| + CastExtensionSession* session) { |
| + return new talk_base::RefCountedObject< |
| + CastCreateSessionDescriptionObserver>(session); |
| + } |
| + virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc) OVERRIDE { |
| + if (session_ == NULL) { |
| + LOG(ERROR) << "No Session, cannot create session description."; |
|
Wez
2014/08/07 01:25:33
How can session_ ever be NULL?
aiguha
2014/08/12 01:42:37
Since this is ref-counted and owned by PeerConnect
Wez
2014/08/12 22:14:59
Nothing ever sets it to NULL, though...?
If |sess
aiguha
2014/08/13 18:33:27
Oops, I was mixing up dangling and null pointers,
|
| + return; |
| + } |
| + session_->OnSuccess(desc); |
| + } |
| + virtual void OnFailure(const std::string& error) OVERRIDE { |
| + if (session_ == NULL) { |
| + LOG(ERROR) << "No Session, cannot create session description."; |
| + return; |
| + } |
| + session_->OnFailure(error); |
| + } |
| + |
| + protected: |
| + CastCreateSessionDescriptionObserver(CastExtensionSession* session) |
| + : session_(session) {} |
| + virtual ~CastCreateSessionDescriptionObserver() {} |
| + |
| + private: |
| + CastExtensionSession* session_; |
| +}; |
|
Wez
2014/08/07 01:25:32
DISALLOW_COPY_AND_ASSIGN()
aiguha
2014/08/12 01:42:37
Done.
|
| + |
| +// webrtc::StatsObserver implementation. |
| +class CastStatsObserver : public webrtc::StatsObserver { |
| + public: |
| + static CastStatsObserver* Create() { |
| + return new talk_base::RefCountedObject<CastStatsObserver>(); |
| + } |
| + |
| + virtual void OnComplete( |
| + const std::vector<webrtc::StatsReport>& reports) OVERRIDE { |
| + if (reports.empty()) { |
|
Wez
2014/08/07 01:25:30
Remove this special-case.
aiguha
2014/08/12 01:42:38
Done.
|
| + VLOG(1) << "Received 0 StatsReports."; |
| + } |
| + VLOG(1) << "Received " << reports.size() << " new StatsReports."; |
| + std::vector<webrtc::StatsReport>::const_iterator it; |
| + for (it = reports.begin(); it != reports.end(); ++it) { |
| + LogStatsReport(*it); |
| + } |
| + } |
| + |
| + protected: |
| + CastStatsObserver() {} |
| + virtual ~CastStatsObserver() {} |
| + |
| + void LogStatsReport(const webrtc::StatsReport& report) { |
|
Wez
2014/08/07 01:25:30
This seems simple enough to leave in-line on OnCom
aiguha
2014/08/12 01:42:37
Done.
|
| + typedef webrtc::StatsReport StatsReport; |
|
Wez
2014/08/07 01:25:32
You don't use this typedef?
aiguha
2014/08/12 01:42:37
Acknowledged.
|
| + typedef webrtc::StatsReport::Values::iterator ValuesIterator; |
| + webrtc::StatsReport::Values v = report.values; |
| + for (ValuesIterator it = v.begin(); it != v.end(); ++it) { |
| + VLOG(1) << "Param: " << it->name << "; Value: " << it->value << "."; |
| + } |
| + } |
| +}; |
|
Wez
2014/08/07 01:25:31
DISALLOW_COPY_AND_ASSIGN()
aiguha
2014/08/12 01:42:37
Done.
|
| + |
| +//------------------------------------------------------------------------------ |
|
Wez
2014/08/07 01:25:30
No need for this.
aiguha
2014/08/12 01:42:38
Acknowledged.
|
| + |
| +// static |
| +CastExtensionSession* CastExtensionSession::Create( |
|
Wez
2014/08/07 01:25:31
Generally speaking it's best if you can order the
aiguha
2014/08/12 01:42:36
Done. Only the PeerConnectionObserver impl is stil
|
| + scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| + const protocol::NetworkSettings& network_settings, |
| + ClientSessionControl* client_session_control, |
| + protocol::ClientStub* client_stub) { |
| + scoped_ptr<CastExtensionSession> cast_extension_session( |
| + new CastExtensionSession(network_task_runner, |
| + url_request_context_getter, |
| + network_settings, |
| + client_session_control, |
| + client_stub)); |
| + bool success = cast_extension_session->WrapTasksAndSave(); |
| + success = cast_extension_session->InitializePeerConnection(); |
|
Wez
2014/08/07 01:25:31
You're overwriting the result of WrapTasksAndSave(
aiguha
2014/08/12 01:42:37
Done.
|
| + return (success ? cast_extension_session.release() : NULL); |
| +} |
| + |
| +CastExtensionSession::CastExtensionSession( |
| + scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
| + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter, |
| + const protocol::NetworkSettings& network_settings, |
| + ClientSessionControl* client_session_control, |
| + protocol::ClientStub* client_stub) |
| + : network_task_runner_(network_task_runner), |
| + url_request_context_getter_(url_request_context_getter), |
| + network_settings_(network_settings), |
| + client_session_control_(client_session_control), |
| + client_stub_(client_stub), |
| + stats_observer_(CastStatsObserver::Create()), |
| + received_offer_(false), |
| + has_grabbed_capturer_(false), |
| + network_thread_wrapper_(NULL), |
| + worker_thread_wrapper_(NULL) { |
| + DCHECK(network_task_runner_); |
|
Wez
2014/08/07 01:25:31
nit: if you're going to -> this on the next line,
aiguha
2014/08/12 01:42:38
Done.
|
| + DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| + DCHECK(url_request_context_getter_); |
| + DCHECK(client_session_control_); |
| + DCHECK(client_stub_); |
| + |
| + // Launch the worker thread. |
|
Wez
2014/08/07 01:25:33
nit: This comment adds nothing ;)
aiguha
2014/08/12 01:42:38
Acknowledged.
|
| + worker_thread_.reset(new AutoThread(kWorkerThreadName)); |
| + worker_task_runner_ = |
| + worker_thread_->StartWithType(base::MessageLoop::TYPE_IO); |
|
Wez
2014/08/07 01:25:32
AutoThread is really intended to be created with A
aiguha
2014/08/12 01:42:38
Done.
|
| + |
| + DCHECK(worker_task_runner_); |
| +} |
| + |
| +CastExtensionSession::~CastExtensionSession() { |
| + DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| + VLOG(1) << "CastExtensionSession::DTOR"; |
|
Wez
2014/08/07 01:25:32
nit: Clean up this logging.
aiguha
2014/08/12 01:42:37
Done.
|
| + DeletePeerConnection(); |
| + worker_task_runner_ = NULL; |
| +} |
| + |
| +std::string append_path(const char* first, const char* second) { |
|
Wez
2014/08/07 01:25:30
AppendPath
aiguha
2014/08/12 01:42:38
Removed altogether.
|
| + std::string path(first); |
| + path.append("."); |
| + path.append(second); |
| + return path; |
| +} |
| + |
| +bool CastExtensionSession::ParseAndSetRemoteDescription( |
| + base::DictionaryValue* message) { |
| + DCHECK(peer_connection_.get() != NULL); |
| + std::string webrtc_type; |
| + std::string sdp; |
|
Wez
2014/08/07 01:25:32
Why not just pull out the kMessageData dictionary
aiguha
2014/08/12 01:42:38
Done.
|
| + if (!message->GetString(append_path(kMessageData, kWebRtcSessionDescType), |
| + &webrtc_type)) { |
| + LOG(ERROR) |
| + << "Invalid Cast Extension Message (missing webrtc type header)."; |
| + return false; |
| + } |
| + if (!message->GetString(append_path(kMessageData, kWebRtcSessionDescSDP), |
| + &sdp)) { |
| + LOG(ERROR) << "Invalid Cast Extension Message (missing webrtc sdp header)."; |
| + return false; |
| + } |
| + webrtc::SdpParseError error; |
| + webrtc::SessionDescriptionInterface* session_description( |
| + webrtc::CreateSessionDescription(webrtc_type, sdp, &error)); |
| + |
| + if (!session_description) { |
| + LOG(ERROR) << "Invalid Cast Extension Message (could not parse sdp)."; |
| + VLOG(1) << "SdpParseError was: " << error.description; |
| + return false; |
| + } |
| + |
| + VLOG(1) << "Setting Remote Description."; |
| + peer_connection_->SetRemoteDescription( |
| + CastSetSessionDescriptionObserver::Create(), session_description); |
| + return true; |
| +} |
| + |
| +bool CastExtensionSession::ParseAndAddICECandidate( |
| + base::DictionaryValue* message) { |
| + std::string candidate_str; |
| + std::string sdp_mid; |
| + int sdp_mlineindex = 0; |
| + if (!message->GetString(append_path(kMessageData, kWebRtcSDPMid), &sdp_mid) || |
| + !message->GetInteger(append_path(kMessageData, kWebRtcSDPMLineIndex), |
| + &sdp_mlineindex) || |
| + !message->GetString(append_path(kMessageData, kWebRtcCandidate), |
| + &candidate_str)) { |
| + LOG(ERROR) << "Invalid Cast Extension Message (could not parse)."; |
| + return false; |
| + } |
| + talk_base::scoped_ptr<webrtc::IceCandidateInterface> candidate( |
| + webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, candidate_str)); |
| + if (!candidate.get()) { |
| + LOG(ERROR) |
| + << "Invalid Cast Extension Message (could not create candidate)."; |
| + return false; |
| + } |
| + if (!peer_connection_->AddIceCandidate(candidate.get())) { |
| + LOG(ERROR) << "Failed to apply received ICE Candidate to PeerConnection."; |
| + return false; |
| + } |
| + |
| + VLOG(1) << "Received and Added ICE Candidate: " << candidate_str; |
| + |
| + return true; |
| +} |
| + |
| +// Returns true if the ExtensionMessage was of type |kMessageType|, even if |
| +// it was badly formed or a resulting action failed. This is done so that |
| +// the host does not continue to attempt to pass |message| to other |
| +// HostExtensionSessions. |
| +bool CastExtensionSession::OnExtensionMessage( |
| + ClientSessionControl* client_session_control, |
| + protocol::ClientStub* client_stub, |
| + const protocol::ExtensionMessage& message) { |
| + if (!message.has_type() || message.type().compare(kMessageType) != 0) { |
|
Wez
2014/08/07 01:25:33
Doesn't the HostExtensionSessionManager already ch
aiguha
2014/08/12 01:42:36
That was my Java mind talking. Fixed!
aiguha
2014/08/12 01:42:38
Acknowledged.
|
| + return false; |
| + } |
| + |
| + scoped_ptr<base::Value> value(base::JSONReader::Read(message.data())); |
| + base::DictionaryValue* client_message; |
| + if (!(value && value->GetAsDictionary(&client_message))) { |
| + LOG(ERROR) << "Could not read cast extension message."; |
| + return true; |
| + } |
| + |
| + std::string subject; |
| + if (!client_message->GetString(kMessageSubject, &subject)) { |
| + LOG(ERROR) << "Invalid Cast Extension Message (missing subject header)."; |
| + return true; |
| + } |
| + |
| + if (subject == kSubjectOffer && !received_offer_) { |
| + if (ParseAndSetRemoteDescription(client_message)) { |
| + // Reset the video pipeline so we can grab the screen capturer and setup |
| + // a video stream. |
| + received_offer_ = true; |
| + client_session_control_->ResetVideoPipeline(); |
| + } |
| + } else if (subject == kSubjectAnswer) { |
| + ParseAndSetRemoteDescription(client_message); |
| + } else if (subject == kSubjectNewCandidate) { |
| + ParseAndAddICECandidate(client_message); |
| + } else { |
| + VLOG(1) << "Unexpected CastExtension Message: " << message.data(); |
| + } |
| + return true; |
| +} |
| + |
| +bool CastExtensionSession::ModifiesVideoPipeline() const { |
| + return true; |
| +} |
| + |
| +scoped_ptr<webrtc::ScreenCapturer> CastExtensionSession::OnCreateVideoCapturer( |
| + scoped_ptr<webrtc::ScreenCapturer> capturer) { |
| + // TODO(aiguha): Support the case(s) where we've grabbed the capturer already, |
| + // but another extension reset the video pipeline. We should remove the |
| + // stream from the peer connection here, and then attempt to re-setup the |
| + // peer connection in the OnRenegotiationNeeded() callback. |
| + if (has_grabbed_capturer_) { |
| + LOG(ERROR) << "The video pipeline was reset unexpectedly."; |
| + has_grabbed_capturer_ = false; |
| + peer_connection_->RemoveStream(stream_.release()); |
| + return capturer.Pass(); |
| + } |
| + |
| + if (received_offer_) { |
| + has_grabbed_capturer_ = true; |
| + if (SetupVideoStream(capturer.Pass())) { |
| + peer_connection_->CreateAnswer( |
| + CastCreateSessionDescriptionObserver::Create(this), NULL); |
| + } else { |
| + has_grabbed_capturer_ = false; |
| + // Ignore the received offer, since we failed to setup a video stream. |
| + received_offer_ = false; |
| + } |
| + return scoped_ptr<webrtc::ScreenCapturer>(); |
| + } |
| + |
| + return capturer.Pass(); |
| +} |
| + |
| +// Private Methods ------------------------------------------------------------- |
| + |
| +bool CastExtensionSession::SendMessageToClient(const char* subject, |
| + const std::string& data) { |
| + DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| + if (client_stub_ == NULL) { |
| + LOG(ERROR) << "No Client Stub. Cannot send message to client."; |
| + return false; |
| + } |
| + |
| + base::DictionaryValue message_dict; |
| + message_dict.SetString(kMessageSubject, subject); |
| + message_dict.SetString(kMessageData, data); |
| + std::string message_json; |
| + |
| + if (!base::JSONWriter::Write(&message_dict, &message_json)) { |
| + LOG(ERROR) << "Failed to create message json."; |
| + return false; |
| + } |
| + |
| + protocol::ExtensionMessage message; |
| + message.set_type(kMessageType); |
| + message.set_data(message_json); |
| + client_stub_->DeliverHostMessage(message); |
| + return true; |
| +} |
| + |
| +void CastExtensionSession::SendCursorShape( |
| + scoped_ptr<protocol::CursorShapeInfo> cursor_shape) { |
| + DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| + if (!client_stub_) |
| + return; |
| + |
| + client_stub_->SetCursorShape(*cursor_shape); |
| +} |
| + |
| +void CastExtensionSession::EnsureTaskAndSetSend(talk_base::Thread** ptr, |
| + base::WaitableEvent* event) { |
| + jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| + jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); |
| + *ptr = jingle_glue::JingleThreadWrapper::current(); |
| + |
| + if (event != NULL) { |
| + event->Signal(); |
| + } |
| +} |
| + |
| +bool CastExtensionSession::WrapTasksAndSave() { |
| + DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| + EnsureTaskAndSetSend(&network_thread_wrapper_); |
| + |
| + if (network_thread_wrapper_ == NULL) |
| + return false; |
| + base::WaitableEvent wrap_worker_thread_event(true, false); |
| + worker_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CastExtensionSession::EnsureTaskAndSetSend, |
| + base::Unretained(this), |
| + &worker_thread_wrapper_, |
| + &wrap_worker_thread_event)); |
| + wrap_worker_thread_event.Wait(); |
| + |
| + return (worker_thread_wrapper_ != NULL); |
| +} |
| + |
| +bool CastExtensionSession::InitializePeerConnection() { |
| + DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| + DCHECK(peer_conn_factory_.get() == NULL); |
| + DCHECK(peer_connection_.get() == NULL); |
| + DCHECK(worker_thread_wrapper_ != NULL); |
| + DCHECK(network_thread_wrapper_ != NULL); |
| + |
| + peer_conn_factory_ = webrtc::CreatePeerConnectionFactory( |
| + worker_thread_wrapper_, network_thread_wrapper_, NULL, NULL, NULL); |
| + |
| + if (!peer_conn_factory_.get()) { |
| + LOG(ERROR) << "Failed to initialize PeerConnectionFactory"; |
| + DeletePeerConnection(); |
| + return false; |
| + } |
| + |
| + VLOG(1) << "Created PeerConnectionFactory successfully."; |
| + |
| + webrtc::PeerConnectionInterface::IceServers servers; |
| + webrtc::PeerConnectionInterface::IceServer server; |
| + server.uri = kDefaultStunURI; |
| + servers.push_back(server); |
| + webrtc::PeerConnectionInterface::RTCConfiguration rtc_config; |
| + rtc_config.servers = servers; |
| + webrtc::FakeConstraints constraints; |
| + |
| + // DTLS-SRTP is the preferred encryption method. If set to kValueFalse, the |
| + // peer connection uses SDES. Disabling SDES as well will cause the peer |
| + // connection to fail to connect. |
| + // Note: For protection and unprotection of SRTP packets, the libjingle |
| + // ENABLE_EXTERNAL_AUTH flag must not be set. |
| + constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, |
| + webrtc::MediaConstraintsInterface::kValueTrue); |
| + |
| + peer_connection_ = peer_conn_factory_->CreatePeerConnection( |
| + rtc_config, |
| + &constraints, |
| + ChromiumPortAllocatorFactory::Create(network_settings_, |
| + url_request_context_getter_), |
| + NULL, |
| + this); |
| + |
| + if (!peer_connection_.get()) { |
| + LOG(ERROR) << "Failed to initialize PeerConnection."; |
| + DeletePeerConnection(); |
| + return false; |
| + } |
| + |
| + VLOG(1) << "Created PeerConnection successfully."; |
| + |
| + if (!SendMessageToClient(kSubjectTest, "Hello, client.")) { |
| + LOG(ERROR) << "Failed to send test message to client."; |
| + return false; |
| + } |
| + // Sending this message triggers the client to start the peer connection |
| + // offer/answer negotiation. |
| + if (!SendMessageToClient(kSubjectReady, "Host ready to receive offers.")) { |
| + LOG(ERROR) << "Failed to send ready message to client."; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool CastExtensionSession::SetupVideoStream( |
| + scoped_ptr<webrtc::ScreenCapturer> screen_capturer) { |
| + DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| + DCHECK(screen_capturer); |
| + |
| + if (stream_) { |
| + VLOG(1) << "Already added MediaStream. Aborting Setup."; |
| + return false; // Already added. |
| + } |
| + |
| + // scoped_ptr<CastVideoCapturer> cast_video_capturer( |
| + // new CastVideoCapturer(screen_capturer.Pass())); |
| + |
| + // Set video stream constraints. |
| + std::string minFrameRate = "5"; |
| + webrtc::FakeConstraints video_constraints; |
| + video_constraints.AddMandatory( |
| + webrtc::MediaConstraintsInterface::kMinFrameRate, minFrameRate); |
| + |
| + talk_base::scoped_refptr<webrtc::VideoTrackInterface> video_track = |
| + peer_conn_factory_->CreateVideoTrack( |
| + kVideoLabel, |
| + peer_conn_factory_->CreateVideoSource(NULL, |
| + &video_constraints)); |
| + |
| + stream_ = peer_conn_factory_->CreateLocalMediaStream(kStreamLabel); |
| + |
| + if (!stream_->AddTrack(video_track)) |
| + return false; |
| + |
| + if (!peer_connection_->AddStream(stream_, NULL)) |
| + return false; |
| + |
| + VLOG(1) << "Setup video stream successfully."; |
| + |
| + return true; |
| +} |
| + |
| +void CastExtensionSession::PollPeerConnectionStats() { |
| + if (!connection_active()) { |
| + VLOG(1) << "Cannot poll stats while PeerConnection is inactive."; |
| + } |
| + talk_base::scoped_refptr<webrtc::MediaStreamTrackInterface> video_track = |
| + stream_->FindVideoTrack(kVideoLabel); |
| + peer_connection_->GetStats( |
| + stats_observer_, |
| + video_track.release(), |
| + webrtc::PeerConnectionInterface::kStatsOutputLevelStandard); |
| +} |
| + |
| +void CastExtensionSession::DeletePeerConnection() { |
| + VLOG(1) << "DeletePeerConnection called."; |
| + peer_connection_->Close(); |
| + peer_connection_ = NULL; |
| + stream_ = NULL; |
| + peer_conn_factory_ = NULL; |
| +} |
| + |
| +bool CastExtensionSession::connection_active() const { |
| + return peer_connection_.get() != NULL; |
| +} |
| + |
| +// MouseShapeObserver implementation ------------------------------------------- |
| + |
| +// TODO(aiguha): To reduce duplication, it would perhaps be |
| +// better to create a single MouseShapeObserver outside of VideoScheduler |
| +// that can be attached to the ScreenCapturer on its creation in |
| +// ClientSession. |
| +void CastExtensionSession::OnCursorShapeChanged( |
| + webrtc::MouseCursorShape* cursor_shape) { |
| + DCHECK(worker_task_runner_->BelongsToCurrentThread()); |
| + scoped_ptr<webrtc::MouseCursorShape> owned_cursor(cursor_shape); |
| + |
| + scoped_ptr<protocol::CursorShapeInfo> cursor_proto( |
| + new protocol::CursorShapeInfo()); |
| + cursor_proto->set_width(cursor_shape->size.width()); |
| + cursor_proto->set_height(cursor_shape->size.height()); |
| + cursor_proto->set_hotspot_x(cursor_shape->hotspot.x()); |
| + cursor_proto->set_hotspot_y(cursor_shape->hotspot.y()); |
| + cursor_proto->set_data(cursor_shape->data); |
| + |
| + network_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CastExtensionSession::SendCursorShape, |
| + base::Unretained(this), |
| + base::Passed(&cursor_proto))); |
| +} |
| + |
| +// CreateSessionDescriptionObserver related methods ---------------------------- |
| + |
| +void CastExtensionSession::OnSuccess( |
| + webrtc::SessionDescriptionInterface* desc) { |
| + if (!network_task_runner_->BelongsToCurrentThread()) { |
| + network_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind( |
| + &CastExtensionSession::OnSuccess, base::Unretained(this), desc)); |
| + return; |
| + } |
| + peer_connection_->SetLocalDescription( |
| + CastSetSessionDescriptionObserver::Create(), desc); |
| + scoped_ptr<base::DictionaryValue> json(new base::DictionaryValue()); |
| + json->SetString(kWebRtcSessionDescType, desc->type()); |
| + std::string subject = kWebRtcPrefix + desc->type(); |
| + std::string desc_str; |
| + desc->ToString(&desc_str); |
| + json->SetString(kWebRtcSessionDescSDP, desc_str); |
| + std::string json_str; |
| + base::JSONWriter::Write(json.get(), &json_str); |
| + SendMessageToClient(subject.c_str(), json_str); |
| +} |
| + |
| +void CastExtensionSession::OnFailure(const std::string& error) { |
| + VLOG(1) << __FUNCTION__ << " called with: " << error; |
| +} |
| + |
| +// PeerConnectionObserver implementation --------------------------------------- |
| + |
| +void CastExtensionSession::OnError() { |
| + VLOG(1) << __FUNCTION__; |
| +} |
| +void CastExtensionSession::OnSignalingChange( |
| + webrtc::PeerConnectionInterface::SignalingState new_state) { |
| + VLOG(1) << "Function CastExtensionSession::OnSignalingChange called with " |
| + << new_state; |
| +} |
| +void CastExtensionSession::OnStateChange( |
| + webrtc::PeerConnectionObserver::StateType state_changed) { |
| + VLOG(1) << __FUNCTION__ << " called with input " << state_changed; |
| +} |
| +void CastExtensionSession::OnAddStream(webrtc::MediaStreamInterface* stream) { |
| + VLOG(1) << __FUNCTION__ << " " << stream->label(); |
| +} |
| +void CastExtensionSession::OnRemoveStream( |
| + webrtc::MediaStreamInterface* stream) { |
| + VLOG(1) << __FUNCTION__ << " " << stream->label(); |
| +} |
| +void CastExtensionSession::OnDataChannel( |
| + webrtc::DataChannelInterface* data_channel) { |
| + VLOG(1) << __FUNCTION__ << " called with " << data_channel->label(); |
| +} |
| +void CastExtensionSession::OnRenegotiationNeeded() { |
| + VLOG(1) << __FUNCTION__ << " called."; |
| +} |
| +void CastExtensionSession::OnIceConnectionChange( |
| + webrtc::PeerConnectionInterface::IceConnectionState new_state) { |
| + VLOG(1) << __FUNCTION__ << " called with new IceConnectionState " |
| + << new_state; |
| +} |
| +void CastExtensionSession::OnIceGatheringChange( |
| + webrtc::PeerConnectionInterface::IceGatheringState new_state) { |
| + VLOG(1) << __FUNCTION__ << " called with new IceGatheringState " << new_state; |
| +} |
| + |
| +void CastExtensionSession::OnIceComplete() { |
| + // TODO(aiguha): Maybe start timer only if enabled by command-line flag or |
| + // at a particular verbosity level. |
| + stats_polling_timer_.Start(FROM_HERE, |
| + base::TimeDelta::FromSeconds(kStatsLogIntervalSec), |
| + this, |
| + &CastExtensionSession::PollPeerConnectionStats); |
| +} |
| + |
| +void CastExtensionSession::OnIceCandidate( |
| + const webrtc::IceCandidateInterface* candidate) { |
| + std::string candidate_str; |
| + if (!candidate->ToString(&candidate_str)) { |
| + LOG(ERROR) << __FUNCTION__ << " called, but could not serialize candidate."; |
| + return; |
| + } |
| + VLOG(1) << __FUNCTION__ << " called with " << candidate_str; |
| + scoped_ptr<base::DictionaryValue> json(new base::DictionaryValue()); |
| + json->SetString(kWebRtcSDPMid, candidate->sdp_mid()); |
| + json->SetInteger(kWebRtcSDPMLineIndex, candidate->sdp_mline_index()); |
| + json->SetString(kWebRtcCandidate, candidate_str); |
| + std::string json_str; |
| + base::JSONWriter::Write(json.get(), &json_str); |
| + SendMessageToClient(kSubjectNewCandidate, json_str); |
| +} |
| + |
| +} // namespace remoting |