| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/canvas_capture_handler.h" | 5 #include "content/renderer/media/canvas_capture_handler.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "content/common/frame_messages.h" |
| 14 #include "content/public/renderer/render_thread.h" | 15 #include "content/public/renderer/render_thread.h" |
| 15 #include "content/renderer/media/media_stream_video_capturer_source.h" | 16 #include "content/renderer/media/media_stream_video_capturer_source.h" |
| 16 #include "content/renderer/media/media_stream_video_source.h" | 17 #include "content/renderer/media/media_stream_video_source.h" |
| 17 #include "content/renderer/media/media_stream_video_track.h" | 18 #include "content/renderer/media/media_stream_video_track.h" |
| 18 #include "content/renderer/media/webrtc_uma_histograms.h" | 19 #include "content/renderer/media/webrtc_uma_histograms.h" |
| 19 #include "content/renderer/render_thread_impl.h" | 20 #include "content/renderer/render_view_impl.h" |
| 20 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 21 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 21 #include "third_party/WebKit/public/platform/WebString.h" | 22 #include "third_party/WebKit/public/platform/WebString.h" |
| 23 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 22 #include "third_party/libyuv/include/libyuv.h" | 24 #include "third_party/libyuv/include/libyuv.h" |
| 23 | 25 |
| 24 namespace content { | 26 namespace content { |
| 25 | 27 |
| 26 class CanvasCaptureHandler::VideoCapturerSource | 28 class CanvasCaptureHandler::VideoCapturerSource |
| 27 : public media::VideoCapturerSource { | 29 : public media::VideoCapturerSource { |
| 28 public: | 30 public: |
| 29 explicit VideoCapturerSource(base::WeakPtr<CanvasCaptureHandler> | 31 explicit VideoCapturerSource(base::WeakPtr<CanvasCaptureHandler> |
| 30 canvas_handler, | 32 canvas_handler, |
| 31 double frame_rate) | 33 double frame_rate) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 DISALLOW_COPY_AND_ASSIGN(CanvasCaptureHandlerDelegate); | 97 DISALLOW_COPY_AND_ASSIGN(CanvasCaptureHandlerDelegate); |
| 96 }; | 98 }; |
| 97 | 99 |
| 98 CanvasCaptureHandler::CanvasCaptureHandler( | 100 CanvasCaptureHandler::CanvasCaptureHandler( |
| 99 const blink::WebSize& size, | 101 const blink::WebSize& size, |
| 100 double frame_rate, | 102 double frame_rate, |
| 101 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 103 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 102 blink::WebMediaStreamTrack* track) | 104 blink::WebMediaStreamTrack* track) |
| 103 : ask_for_new_frame_(false), | 105 : ask_for_new_frame_(false), |
| 104 size_(size), | 106 size_(size), |
| 107 capturer_count_incremented_(false), |
| 105 io_task_runner_(io_task_runner), | 108 io_task_runner_(io_task_runner), |
| 106 weak_ptr_factory_(this) { | 109 weak_ptr_factory_(this) { |
| 110 blink::WebLocalFrame* const web_frame = |
| 111 blink::WebLocalFrame::frameForCurrentContext(); |
| 112 render_view_ = |
| 113 web_frame ? RenderViewImpl::FromWebView(web_frame->view()) : nullptr; |
| 114 |
| 107 scoped_ptr<media::VideoCapturerSource> video_source( | 115 scoped_ptr<media::VideoCapturerSource> video_source( |
| 108 new CanvasCaptureHandler::VideoCapturerSource( | 116 new CanvasCaptureHandler::VideoCapturerSource( |
| 109 weak_ptr_factory_.GetWeakPtr(), frame_rate)); | 117 weak_ptr_factory_.GetWeakPtr(), frame_rate)); |
| 110 AddVideoCapturerSourceToVideoTrack(std::move(video_source), track); | 118 AddVideoCapturerSourceToVideoTrack(std::move(video_source), track); |
| 111 } | 119 } |
| 112 | 120 |
| 113 CanvasCaptureHandler::~CanvasCaptureHandler() { | 121 CanvasCaptureHandler::~CanvasCaptureHandler() { |
| 114 DVLOG(3) << __FUNCTION__; | 122 DVLOG(3) << __FUNCTION__; |
| 115 DCHECK(thread_checker_.CalledOnValidThread()); | 123 DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 io_task_runner_->DeleteSoon(FROM_HERE, delegate_.release()); | 124 io_task_runner_->DeleteSoon(FROM_HERE, delegate_.release()); |
| 125 |
| 126 // Handle when this class is destructed before StopVideoCapture() call. |
| 127 if (render_view_ && capturer_count_incremented_) { |
| 128 RenderThread::Get()->Send( |
| 129 new FrameHostMsg_DecrementCapturerCount(render_view_->GetRoutingID())); |
| 130 capturer_count_incremented_ = false; |
| 131 } |
| 132 DCHECK(!capturer_count_incremented_); |
| 117 } | 133 } |
| 118 | 134 |
| 119 // static | 135 // static |
| 120 CanvasCaptureHandler* CanvasCaptureHandler::CreateCanvasCaptureHandler( | 136 CanvasCaptureHandler* CanvasCaptureHandler::CreateCanvasCaptureHandler( |
| 121 const blink::WebSize& size, | 137 const blink::WebSize& size, |
| 122 double frame_rate, | 138 double frame_rate, |
| 123 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 139 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
| 124 blink::WebMediaStreamTrack* track) { | 140 blink::WebMediaStreamTrack* track) { |
| 125 // Save histogram data so we can see how much CanvasCapture is used. | 141 // Save histogram data so we can see how much CanvasCapture is used. |
| 126 // The histogram counts the number of calls to the JS API. | 142 // The histogram counts the number of calls to the JS API. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 147 DVLOG(3) << __FUNCTION__ << " requested " | 163 DVLOG(3) << __FUNCTION__ << " requested " |
| 148 << media::VideoCaptureFormat::ToString(params.requested_format); | 164 << media::VideoCaptureFormat::ToString(params.requested_format); |
| 149 DCHECK(params.requested_format.IsValid()); | 165 DCHECK(params.requested_format.IsValid()); |
| 150 DCHECK(thread_checker_.CalledOnValidThread()); | 166 DCHECK(thread_checker_.CalledOnValidThread()); |
| 151 | 167 |
| 152 // TODO(emircan): Accomodate to the given frame rate constraints here. | 168 // TODO(emircan): Accomodate to the given frame rate constraints here. |
| 153 capture_format_ = params.requested_format; | 169 capture_format_ = params.requested_format; |
| 154 delegate_.reset(new CanvasCaptureHandlerDelegate(new_frame_callback)); | 170 delegate_.reset(new CanvasCaptureHandlerDelegate(new_frame_callback)); |
| 155 DCHECK(delegate_); | 171 DCHECK(delegate_); |
| 156 ask_for_new_frame_ = true; | 172 ask_for_new_frame_ = true; |
| 173 |
| 174 if (render_view_) { |
| 175 RenderThread::Get()->Send( |
| 176 new FrameHostMsg_IncrementCapturerCount(render_view_->GetRoutingID())); |
| 177 capturer_count_incremented_ = true; |
| 178 } |
| 179 |
| 157 running_callback.Run(true); | 180 running_callback.Run(true); |
| 158 } | 181 } |
| 159 | 182 |
| 160 void CanvasCaptureHandler::StopVideoCapture() { | 183 void CanvasCaptureHandler::StopVideoCapture() { |
| 161 DVLOG(3) << __FUNCTION__; | 184 DVLOG(3) << __FUNCTION__; |
| 162 DCHECK(thread_checker_.CalledOnValidThread()); | 185 DCHECK(thread_checker_.CalledOnValidThread()); |
| 163 ask_for_new_frame_ = false; | 186 ask_for_new_frame_ = false; |
| 164 io_task_runner_->DeleteSoon(FROM_HERE, delegate_.release()); | 187 io_task_runner_->DeleteSoon(FROM_HERE, delegate_.release()); |
| 188 |
| 189 if (render_view_) { |
| 190 RenderThread::Get()->Send( |
| 191 new FrameHostMsg_DecrementCapturerCount(render_view_->GetRoutingID())); |
| 192 capturer_count_incremented_ = false; |
| 193 } |
| 165 } | 194 } |
| 166 | 195 |
| 167 void CanvasCaptureHandler::CreateNewFrame(const SkImage* image) { | 196 void CanvasCaptureHandler::CreateNewFrame(const SkImage* image) { |
| 168 DCHECK(thread_checker_.CalledOnValidThread()); | 197 DCHECK(thread_checker_.CalledOnValidThread()); |
| 169 | 198 |
| 170 DCHECK(image); | 199 DCHECK(image); |
| 171 const gfx::Size size(image->width(), image->height()); | 200 const gfx::Size size(image->width(), image->height()); |
| 172 if (size != last_size) { | 201 if (size != last_size) { |
| 173 temp_data_.resize( | 202 temp_data_.resize( |
| 174 media::VideoFrame::AllocationSize(media::PIXEL_FORMAT_ARGB, size)); | 203 media::VideoFrame::AllocationSize(media::PIXEL_FORMAT_ARGB, size)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 247 |
| 219 web_track->initialize(webkit_source); | 248 web_track->initialize(webkit_source); |
| 220 blink::WebMediaConstraints constraints; | 249 blink::WebMediaConstraints constraints; |
| 221 constraints.initialize(); | 250 constraints.initialize(); |
| 222 web_track->setExtraData(new MediaStreamVideoTrack( | 251 web_track->setExtraData(new MediaStreamVideoTrack( |
| 223 media_stream_source.release(), constraints, | 252 media_stream_source.release(), constraints, |
| 224 MediaStreamVideoSource::ConstraintsCallback(), true)); | 253 MediaStreamVideoSource::ConstraintsCallback(), true)); |
| 225 } | 254 } |
| 226 | 255 |
| 227 } // namespace content | 256 } // namespace content |
| OLD | NEW |