diff options
author | sergeyu <sergeyu@chromium.org> | 2016-01-10 00:00:42 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-10 08:02:01 +0000 |
commit | bf81cd6508a642e071cd6b8a8a44ad80edbacc8c (patch) | |
tree | da2e8abf87d898b4db5d0ed21c57b9008565c5ff | |
parent | 731e0e275b0e80df7270ab143597ea4d0d2f1238 (diff) | |
download | chromium_src-bf81cd6508a642e071cd6b8a8a44ad80edbacc8c.zip chromium_src-bf81cd6508a642e071cd6b8a8a44ad80edbacc8c.tar.gz chromium_src-bf81cd6508a642e071cd6b8a8a44ad80edbacc8c.tar.bz2 |
Implement missing features in WebrtcVideoStream.
1. WebrtcVideoStream::Pause() now pauses the stream.
2. WebrtcVideoStream calls SizeCallback to notify about size changes.
BUG=547158
Review URL: https://codereview.chromium.org/1571543002
Cr-Commit-Position: refs/heads/master@{#368531}
-rw-r--r-- | remoting/protocol/webrtc_connection_to_client.cc | 29 | ||||
-rw-r--r-- | remoting/protocol/webrtc_video_capturer_adapter.cc | 16 | ||||
-rw-r--r-- | remoting/protocol/webrtc_video_capturer_adapter.h | 10 | ||||
-rw-r--r-- | remoting/protocol/webrtc_video_stream.cc | 64 | ||||
-rw-r--r-- | remoting/protocol/webrtc_video_stream.h | 26 |
5 files changed, 103 insertions, 42 deletions
diff --git a/remoting/protocol/webrtc_connection_to_client.cc b/remoting/protocol/webrtc_connection_to_client.cc index 3f31804..1fbbad1 100644 --- a/remoting/protocol/webrtc_connection_to_client.cc +++ b/remoting/protocol/webrtc_connection_to_client.cc @@ -31,9 +31,6 @@ namespace remoting { namespace protocol { -const char kStreamLabel[] = "screen_stream"; -const char kVideoLabel[] = "screen_video"; - // Currently the network thread is also used as worker thread for webrtc. // // TODO(sergeyu): Figure out if we would benefit from using a separate @@ -79,31 +76,13 @@ void WebrtcConnectionToClient::OnInputEventReceived(int64_t timestamp) { scoped_ptr<VideoStream> WebrtcConnectionToClient::StartVideoStream( scoped_ptr<webrtc::DesktopCapturer> desktop_capturer) { - scoped_ptr<WebrtcVideoCapturerAdapter> video_capturer_adapter( - new WebrtcVideoCapturerAdapter(std::move(desktop_capturer))); - - // Set video stream constraints. - webrtc::FakeConstraints video_constraints; - video_constraints.AddMandatory( - webrtc::MediaConstraintsInterface::kMinFrameRate, 5); - - rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track = - transport_.peer_connection_factory()->CreateVideoTrack( - kVideoLabel, - transport_.peer_connection_factory()->CreateVideoSource( - video_capturer_adapter.release(), &video_constraints)); - - rtc::scoped_refptr<webrtc::MediaStreamInterface> video_stream = - transport_.peer_connection_factory()->CreateLocalMediaStream( - kStreamLabel); - - if (!video_stream->AddTrack(video_track) || - !transport_.peer_connection()->AddStream(video_stream)) { + scoped_ptr<WebrtcVideoStream> stream(new WebrtcVideoStream()); + if (!stream->Start(std::move(desktop_capturer), transport_.peer_connection(), + transport_.peer_connection_factory())) { return nullptr; } + return std::move(stream); - return make_scoped_ptr( - new WebrtcVideoStream(transport_.peer_connection(), video_stream)); } AudioStub* WebrtcConnectionToClient::audio_stub() { diff --git a/remoting/protocol/webrtc_video_capturer_adapter.cc b/remoting/protocol/webrtc_video_capturer_adapter.cc index a9ee54e..ce6c6f2 100644 --- a/remoting/protocol/webrtc_video_capturer_adapter.cc +++ b/remoting/protocol/webrtc_video_capturer_adapter.cc @@ -18,7 +18,7 @@ const int kFramesPerSec = 30; WebrtcVideoCapturerAdapter::WebrtcVideoCapturerAdapter( scoped_ptr<webrtc::DesktopCapturer> capturer) - : desktop_capturer_(std::move(capturer)) { + : desktop_capturer_(std::move(capturer)), weak_factory_(this) { DCHECK(desktop_capturer_); // Disable video adaptation since we don't intend to use it. @@ -29,6 +29,17 @@ WebrtcVideoCapturerAdapter::~WebrtcVideoCapturerAdapter() { DCHECK(!capture_timer_); } +void WebrtcVideoCapturerAdapter::SetSizeCallback( + const SizeCallback& size_callback) { + DCHECK(thread_checker_.CalledOnValidThread()); + size_callback_ = size_callback; +} + +base::WeakPtr<WebrtcVideoCapturerAdapter> +WebrtcVideoCapturerAdapter::GetWeakPtr() { + return weak_factory_.GetWeakPtr(); +} + bool WebrtcVideoCapturerAdapter::GetBestCaptureFormat( const cricket::VideoFormat& desired, cricket::VideoFormat* best_format) { @@ -160,6 +171,9 @@ void WebrtcVideoCapturerAdapter::OnCaptureCompleted( size_t height = frame->size().height(); if (!yuv_frame_ || yuv_frame_->GetWidth() != width || yuv_frame_->GetHeight() != height) { + if (!size_callback_.is_null()) + size_callback_.Run(frame->size()); + scoped_ptr<cricket::WebRtcVideoFrame> webrtc_frame( new cricket::WebRtcVideoFrame()); webrtc_frame->InitToEmptyBuffer(width, height, 1, 1, 0); diff --git a/remoting/protocol/webrtc_video_capturer_adapter.h b/remoting/protocol/webrtc_video_capturer_adapter.h index 3c5375b..b9f487c 100644 --- a/remoting/protocol/webrtc_video_capturer_adapter.h +++ b/remoting/protocol/webrtc_video_capturer_adapter.h @@ -12,6 +12,7 @@ #include "base/macros.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" #include "base/threading/thread_checker.h" #include "base/timer/timer.h" #include "third_party/libjingle/source/talk/media/base/videocapturer.h" @@ -42,10 +43,15 @@ namespace protocol { class WebrtcVideoCapturerAdapter : public cricket::VideoCapturer, public webrtc::DesktopCapturer::Callback { public: + typedef base::Callback<void(const webrtc::DesktopSize& size)> SizeCallback; + explicit WebrtcVideoCapturerAdapter( scoped_ptr<webrtc::DesktopCapturer> capturer); ~WebrtcVideoCapturerAdapter() override; + void SetSizeCallback(const SizeCallback& size_callback); + base::WeakPtr<WebrtcVideoCapturerAdapter> GetWeakPtr(); + // cricket::VideoCapturer implementation. bool GetBestCaptureFormat(const cricket::VideoFormat& desired, cricket::VideoFormat* best_format) override; @@ -70,6 +76,8 @@ class WebrtcVideoCapturerAdapter : public cricket::VideoCapturer, scoped_ptr<webrtc::DesktopCapturer> desktop_capturer_; + SizeCallback size_callback_; + // Timer to call CaptureNextFrame(). scoped_ptr<base::RepeatingTimer> capture_timer_; @@ -79,6 +87,8 @@ class WebrtcVideoCapturerAdapter : public cricket::VideoCapturer, bool capture_pending_ = false; + base::WeakPtrFactory<WebrtcVideoCapturerAdapter> weak_factory_; + DISALLOW_COPY_AND_ASSIGN(WebrtcVideoCapturerAdapter); }; diff --git a/remoting/protocol/webrtc_video_stream.cc b/remoting/protocol/webrtc_video_stream.cc index 50c6ddd..0b7e862 100644 --- a/remoting/protocol/webrtc_video_stream.cc +++ b/remoting/protocol/webrtc_video_stream.cc @@ -5,28 +5,71 @@ #include "remoting/protocol/webrtc_video_stream.h" #include "base/logging.h" +#include "remoting/protocol/webrtc_video_capturer_adapter.h" #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.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" namespace remoting { namespace protocol { -WebrtcVideoStream::WebrtcVideoStream( - rtc::scoped_refptr<webrtc::PeerConnectionInterface> connection, - rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) - : connection_(connection), stream_(stream) {} +const char kStreamLabel[] = "screen_stream"; +const char kVideoLabel[] = "screen_video"; + +WebrtcVideoStream::WebrtcVideoStream() {} WebrtcVideoStream::~WebrtcVideoStream() { - for (const auto& track : stream_->GetVideoTracks()) { - track->GetSource()->Stop(); - stream_->RemoveTrack(track.get()); + if (stream_) { + for (const auto& track : stream_->GetVideoTracks()) { + track->GetSource()->Stop(); + stream_->RemoveTrack(track.get()); + } + connection_->RemoveStream(stream_.get()); } - connection_->RemoveStream(stream_.get()); + + // MediaStream may still outlive WebrtcVideoStream because it's + // ref-counted. Reset SizeCallback to make sure it won't be called again. + if (capturer_adapter_) + capturer_adapter_->SetSizeCallback(SizeCallback()); +} + +bool WebrtcVideoStream::Start( + scoped_ptr<webrtc::DesktopCapturer> desktop_capturer, + scoped_refptr<webrtc::PeerConnectionInterface> connection, + scoped_refptr<webrtc::PeerConnectionFactoryInterface> + peer_connection_factory) { + scoped_ptr<WebrtcVideoCapturerAdapter> capturer_adapter( + new WebrtcVideoCapturerAdapter(std::move(desktop_capturer))); + capturer_adapter_ = capturer_adapter_->GetWeakPtr(); + + connection_ = connection; + + // Set video stream constraints. + webrtc::FakeConstraints video_constraints; + video_constraints.AddMandatory( + webrtc::MediaConstraintsInterface::kMinFrameRate, 5); + + rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track = + peer_connection_factory->CreateVideoTrack( + kVideoLabel, peer_connection_factory->CreateVideoSource( + capturer_adapter.release(), &video_constraints)); + + stream_ = peer_connection_factory->CreateLocalMediaStream(kStreamLabel); + + if (!stream_->AddTrack(video_track.get()) || + !connection_->AddStream(stream_.get())) { + stream_ = nullptr; + connection_ = nullptr; + return false; + } + + return true; } void WebrtcVideoStream::Pause(bool pause) { - NOTIMPLEMENTED(); + if (capturer_adapter_) + capturer_adapter_->Pause(pause); } void WebrtcVideoStream::OnInputEventReceived(int64_t event_timestamp) { @@ -42,7 +85,8 @@ void WebrtcVideoStream::SetLosslessColor(bool want_lossless) { } void WebrtcVideoStream::SetSizeCallback(const SizeCallback& size_callback) { - NOTIMPLEMENTED(); + if (capturer_adapter_) + capturer_adapter_->SetSizeCallback(size_callback); } } // namespace protocol diff --git a/remoting/protocol/webrtc_video_stream.h b/remoting/protocol/webrtc_video_stream.h index bb52ab8..83d71cd 100644 --- a/remoting/protocol/webrtc_video_stream.h +++ b/remoting/protocol/webrtc_video_stream.h @@ -8,24 +8,35 @@ #include <stdint.h> #include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" #include "remoting/protocol/video_stream.h" -#include "third_party/webrtc/base/scoped_ref_ptr.h" namespace webrtc { +class DesktopSize; +class DesktopCapturer; class MediaStreamInterface; class PeerConnectionInterface; +class PeerConnectionFactoryInterface; +class VideoTrackInterface; } // namespace webrtc namespace remoting { namespace protocol { +class WebrtcVideoCapturerAdapter; + class WebrtcVideoStream : public VideoStream { public: - WebrtcVideoStream( - rtc::scoped_refptr<webrtc::PeerConnectionInterface> connection, - rtc::scoped_refptr<webrtc::MediaStreamInterface> stream); + WebrtcVideoStream(); ~WebrtcVideoStream() override; + bool Start(scoped_ptr<webrtc::DesktopCapturer> desktop_capturer, + scoped_refptr<webrtc::PeerConnectionInterface> connection, + scoped_refptr<webrtc::PeerConnectionFactoryInterface> + peer_connection_factory); + // VideoStream interface. void Pause(bool pause) override; void OnInputEventReceived(int64_t event_timestamp) override; @@ -34,8 +45,11 @@ class WebrtcVideoStream : public VideoStream { void SetSizeCallback(const SizeCallback& size_callback) override; private: - rtc::scoped_refptr<webrtc::PeerConnectionInterface> connection_; - rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_; + scoped_refptr<webrtc::PeerConnectionInterface> connection_; + scoped_refptr<webrtc::MediaStreamInterface> stream_; + + // Owned by the |stream_|. + base::WeakPtr<WebrtcVideoCapturerAdapter> capturer_adapter_; DISALLOW_COPY_AND_ASSIGN(WebrtcVideoStream); }; |