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_capture_from_element/html_video_element_capture
r_source.h" | 5 #include "content/renderer/media_capture_from_element/html_video_element_capture
r_source.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "cc/paint/paint_canvas.h" | 12 #include "cc/paint/skia_paint_canvas.h" |
13 #include "cc/paint/paint_surface.h" | |
14 #include "content/public/renderer/render_thread.h" | 13 #include "content/public/renderer/render_thread.h" |
15 #include "content/renderer/media/media_stream_video_source.h" | 14 #include "content/renderer/media/media_stream_video_source.h" |
16 #include "content/renderer/media/webrtc_uma_histograms.h" | 15 #include "content/renderer/media/webrtc_uma_histograms.h" |
17 #include "media/base/limits.h" | 16 #include "media/base/limits.h" |
18 #include "media/blink/webmediaplayer_impl.h" | 17 #include "media/blink/webmediaplayer_impl.h" |
19 #include "skia/ext/platform_canvas.h" | 18 #include "skia/ext/platform_canvas.h" |
20 #include "third_party/WebKit/public/platform/WebMediaPlayer.h" | 19 #include "third_party/WebKit/public/platform/WebMediaPlayer.h" |
21 #include "third_party/WebKit/public/platform/WebRect.h" | 20 #include "third_party/WebKit/public/platform/WebRect.h" |
22 #include "third_party/WebKit/public/platform/WebSize.h" | 21 #include "third_party/WebKit/public/platform/WebSize.h" |
23 #include "third_party/libyuv/include/libyuv.h" | 22 #include "third_party/libyuv/include/libyuv.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 << media::VideoCaptureFormat::ToString(params.requested_format); | 86 << media::VideoCaptureFormat::ToString(params.requested_format); |
88 DCHECK(params.requested_format.IsValid()); | 87 DCHECK(params.requested_format.IsValid()); |
89 DCHECK(thread_checker_.CalledOnValidThread()); | 88 DCHECK(thread_checker_.CalledOnValidThread()); |
90 | 89 |
91 running_callback_ = running_callback; | 90 running_callback_ = running_callback; |
92 if (!web_media_player_ || !web_media_player_->hasVideo()) { | 91 if (!web_media_player_ || !web_media_player_->hasVideo()) { |
93 running_callback_.Run(false); | 92 running_callback_.Run(false); |
94 return; | 93 return; |
95 } | 94 } |
96 const blink::WebSize resolution = web_media_player_->naturalSize(); | 95 const blink::WebSize resolution = web_media_player_->naturalSize(); |
97 surface_ = cc::PaintSurface::MakeRasterN32Premul(resolution.width, | 96 if (!bitmap_.tryAllocPixels( |
98 resolution.height); | 97 SkImageInfo::MakeN32Premul(resolution.width, resolution.height))) { |
99 if (!surface_) { | |
100 running_callback_.Run(false); | 98 running_callback_.Run(false); |
101 return; | 99 return; |
102 } | 100 } |
| 101 canvas_ = base::MakeUnique<cc::SkiaPaintCanvas>(bitmap_); |
103 | 102 |
104 new_frame_callback_ = new_frame_callback; | 103 new_frame_callback_ = new_frame_callback; |
105 // Force |capture_frame_rate_| to be in between k{Min,Max}FramesPerSecond. | 104 // Force |capture_frame_rate_| to be in between k{Min,Max}FramesPerSecond. |
106 capture_frame_rate_ = | 105 capture_frame_rate_ = |
107 std::max(kMinFramesPerSecond, | 106 std::max(kMinFramesPerSecond, |
108 std::min(static_cast<float>(media::limits::kMaxFramesPerSecond), | 107 std::min(static_cast<float>(media::limits::kMaxFramesPerSecond), |
109 params.requested_format.frame_rate)); | 108 params.requested_format.frame_rate)); |
110 | 109 |
111 running_callback_.Run(true); | 110 running_callback_.Run(true); |
112 base::ThreadTaskRunnerHandle::Get()->PostTask( | 111 base::ThreadTaskRunnerHandle::Get()->PostTask( |
(...skipping 13 matching lines...) Expand all Loading... |
126 DVLOG(3) << __func__; | 125 DVLOG(3) << __func__; |
127 TRACE_EVENT0("video", "HtmlVideoElementCapturerSource::sendNewFrame"); | 126 TRACE_EVENT0("video", "HtmlVideoElementCapturerSource::sendNewFrame"); |
128 DCHECK(thread_checker_.CalledOnValidThread()); | 127 DCHECK(thread_checker_.CalledOnValidThread()); |
129 | 128 |
130 if (!web_media_player_ || new_frame_callback_.is_null()) | 129 if (!web_media_player_ || new_frame_callback_.is_null()) |
131 return; | 130 return; |
132 | 131 |
133 const base::TimeTicks current_time = base::TimeTicks::Now(); | 132 const base::TimeTicks current_time = base::TimeTicks::Now(); |
134 const blink::WebSize resolution = web_media_player_->naturalSize(); | 133 const blink::WebSize resolution = web_media_player_->naturalSize(); |
135 | 134 |
136 cc::PaintCanvas* canvas = surface_->getCanvas(); | |
137 cc::PaintFlags flags; | 135 cc::PaintFlags flags; |
138 flags.setBlendMode(SkBlendMode::kSrc); | 136 flags.setBlendMode(SkBlendMode::kSrc); |
139 flags.setFilterQuality(kLow_SkFilterQuality); | 137 flags.setFilterQuality(kLow_SkFilterQuality); |
140 web_media_player_->paint( | 138 web_media_player_->paint( |
141 canvas, blink::WebRect(0, 0, resolution.width, resolution.height), flags); | 139 canvas_.get(), blink::WebRect(0, 0, resolution.width, resolution.height), |
142 DCHECK_NE(kUnknown_SkColorType, canvas->imageInfo().colorType()); | 140 flags); |
143 DCHECK_EQ(canvas->imageInfo().width(), resolution.width); | 141 DCHECK_NE(kUnknown_SkColorType, canvas_->imageInfo().colorType()); |
144 DCHECK_EQ(canvas->imageInfo().height(), resolution.height); | 142 DCHECK_EQ(canvas_->imageInfo().width(), resolution.width); |
| 143 DCHECK_EQ(canvas_->imageInfo().height(), resolution.height); |
145 | 144 |
146 SkBitmap bitmap; | 145 DCHECK_NE(kUnknown_SkColorType, bitmap_.colorType()); |
147 bitmap.setInfo(canvas->imageInfo()); | 146 DCHECK(!bitmap_.drawsNothing()); |
148 canvas->readPixels(&bitmap, 0, 0); | 147 DCHECK(bitmap_.getPixels()); |
149 | 148 if (bitmap_.colorType() != kN32_SkColorType) { |
150 DCHECK_NE(kUnknown_SkColorType, bitmap.colorType()); | |
151 DCHECK(!bitmap.drawsNothing()); | |
152 DCHECK(bitmap.getPixels()); | |
153 if (bitmap.colorType() != kN32_SkColorType) { | |
154 DLOG(ERROR) << "Only supported color type is kN32_SkColorType (ARGB/ABGR)"; | 149 DLOG(ERROR) << "Only supported color type is kN32_SkColorType (ARGB/ABGR)"; |
155 return; | 150 return; |
156 } | 151 } |
157 | 152 |
158 scoped_refptr<media::VideoFrame> frame = frame_pool_.CreateFrame( | 153 scoped_refptr<media::VideoFrame> frame = frame_pool_.CreateFrame( |
159 media::PIXEL_FORMAT_I420, resolution, gfx::Rect(resolution), resolution, | 154 media::PIXEL_FORMAT_I420, resolution, gfx::Rect(resolution), resolution, |
160 base::TimeTicks::Now() - base::TimeTicks()); | 155 base::TimeTicks::Now() - base::TimeTicks()); |
161 DCHECK(frame); | 156 DCHECK(frame); |
162 | 157 |
163 const uint32 source_pixel_format = | 158 const uint32 source_pixel_format = |
164 (kN32_SkColorType == kRGBA_8888_SkColorType) ? libyuv::FOURCC_ABGR | 159 (kN32_SkColorType == kRGBA_8888_SkColorType) ? libyuv::FOURCC_ABGR |
165 : libyuv::FOURCC_ARGB; | 160 : libyuv::FOURCC_ARGB; |
166 | 161 |
167 if (libyuv::ConvertToI420(static_cast<uint8*>(bitmap.getPixels()), | 162 if (libyuv::ConvertToI420( |
168 bitmap.getSize(), | 163 static_cast<uint8*>(bitmap_.getPixels()), bitmap_.getSize(), |
169 frame->data(media::VideoFrame::kYPlane), | 164 frame->data(media::VideoFrame::kYPlane), |
170 frame->stride(media::VideoFrame::kYPlane), | 165 frame->stride(media::VideoFrame::kYPlane), |
171 frame->data(media::VideoFrame::kUPlane), | 166 frame->data(media::VideoFrame::kUPlane), |
172 frame->stride(media::VideoFrame::kUPlane), | 167 frame->stride(media::VideoFrame::kUPlane), |
173 frame->data(media::VideoFrame::kVPlane), | 168 frame->data(media::VideoFrame::kVPlane), |
174 frame->stride(media::VideoFrame::kVPlane), | 169 frame->stride(media::VideoFrame::kVPlane), 0 /* crop_x */, |
175 0 /* crop_x */, | 170 0 /* crop_y */, bitmap_.info().width(), bitmap_.info().height(), |
176 0 /* crop_y */, | 171 frame->coded_size().width(), frame->coded_size().height(), |
177 bitmap.info().width(), | 172 libyuv::kRotate0, source_pixel_format) == 0) { |
178 bitmap.info().height(), | |
179 frame->coded_size().width(), | |
180 frame->coded_size().height(), | |
181 libyuv::kRotate0, | |
182 source_pixel_format) == 0) { | |
183 // Success! | 173 // Success! |
184 io_task_runner_->PostTask( | 174 io_task_runner_->PostTask( |
185 FROM_HERE, base::Bind(new_frame_callback_, frame, current_time)); | 175 FROM_HERE, base::Bind(new_frame_callback_, frame, current_time)); |
186 } | 176 } |
187 | 177 |
188 // Calculate the time in the future where the next frame should be created. | 178 // Calculate the time in the future where the next frame should be created. |
189 const base::TimeDelta frame_interval = | 179 const base::TimeDelta frame_interval = |
190 base::TimeDelta::FromMicroseconds(1E6 / capture_frame_rate_); | 180 base::TimeDelta::FromMicroseconds(1E6 / capture_frame_rate_); |
191 if (next_capture_time_.is_null()) { | 181 if (next_capture_time_.is_null()) { |
192 next_capture_time_ = current_time + frame_interval; | 182 next_capture_time_ = current_time + frame_interval; |
193 } else { | 183 } else { |
194 next_capture_time_ += frame_interval; | 184 next_capture_time_ += frame_interval; |
195 // Don't accumulate any debt if we are lagging behind - just post next frame | 185 // Don't accumulate any debt if we are lagging behind - just post next frame |
196 // immediately and continue as normal. | 186 // immediately and continue as normal. |
197 if (next_capture_time_ < current_time) | 187 if (next_capture_time_ < current_time) |
198 next_capture_time_ = current_time; | 188 next_capture_time_ = current_time; |
199 } | 189 } |
200 // Schedule next capture. | 190 // Schedule next capture. |
201 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 191 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
202 FROM_HERE, base::Bind(&HtmlVideoElementCapturerSource::sendNewFrame, | 192 FROM_HERE, base::Bind(&HtmlVideoElementCapturerSource::sendNewFrame, |
203 weak_factory_.GetWeakPtr()), | 193 weak_factory_.GetWeakPtr()), |
204 next_capture_time_ - current_time); | 194 next_capture_time_ - current_time); |
205 } | 195 } |
206 | 196 |
207 } // namespace content | 197 } // namespace content |
OLD | NEW |