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/html_video_element_capturer_source.h" | 5 #include "content/renderer/media/html_video_element_capturer_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" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 0xFF /* alpha */, SkXfermode::kSrc_Mode); | 140 0xFF /* alpha */, SkXfermode::kSrc_Mode); |
141 DCHECK_NE(kUnknown_SkColorType, canvas->imageInfo().colorType()); | 141 DCHECK_NE(kUnknown_SkColorType, canvas->imageInfo().colorType()); |
142 DCHECK_EQ(canvas->imageInfo().width(), resolution.width); | 142 DCHECK_EQ(canvas->imageInfo().width(), resolution.width); |
143 DCHECK_EQ(canvas->imageInfo().height(), resolution.height); | 143 DCHECK_EQ(canvas->imageInfo().height(), resolution.height); |
144 | 144 |
145 const SkBitmap bitmap = skia::ReadPixels(canvas); | 145 const SkBitmap bitmap = skia::ReadPixels(canvas); |
146 DCHECK_NE(kUnknown_SkColorType, bitmap.colorType()); | 146 DCHECK_NE(kUnknown_SkColorType, bitmap.colorType()); |
147 DCHECK(!bitmap.drawsNothing()); | 147 DCHECK(!bitmap.drawsNothing()); |
148 DCHECK(bitmap.getPixels()); | 148 DCHECK(bitmap.getPixels()); |
149 if (bitmap.colorType() != kN32_SkColorType) { | 149 if (bitmap.colorType() != kN32_SkColorType) { |
150 DLOG(ERROR) << "Only supported capture format is ARGB"; | 150 DLOG(ERROR) << "Only supported color type is kN32_SkColorType (ARGB/ABGR)"; |
151 return; | 151 return; |
152 } | 152 } |
153 | 153 |
154 scoped_refptr<media::VideoFrame> frame = frame_pool_.CreateFrame( | 154 scoped_refptr<media::VideoFrame> frame = frame_pool_.CreateFrame( |
155 media::PIXEL_FORMAT_I420, resolution, gfx::Rect(resolution), resolution, | 155 media::PIXEL_FORMAT_I420, resolution, gfx::Rect(resolution), resolution, |
156 base::TimeTicks::Now() - base::TimeTicks()); | 156 base::TimeTicks::Now() - base::TimeTicks()); |
157 DCHECK(frame); | 157 DCHECK(frame); |
158 | 158 |
| 159 const uint32 source_pixel_format = |
| 160 (kN32_SkColorType == kRGBA_8888_SkColorType) ? libyuv::FOURCC_ABGR |
| 161 : libyuv::FOURCC_ARGB; |
| 162 |
159 if (libyuv::ConvertToI420(static_cast<uint8*>(bitmap.getPixels()), | 163 if (libyuv::ConvertToI420(static_cast<uint8*>(bitmap.getPixels()), |
160 bitmap.getSize(), | 164 bitmap.getSize(), |
161 frame->data(media::VideoFrame::kYPlane), | 165 frame->data(media::VideoFrame::kYPlane), |
162 frame->stride(media::VideoFrame::kYPlane), | 166 frame->stride(media::VideoFrame::kYPlane), |
163 frame->data(media::VideoFrame::kUPlane), | 167 frame->data(media::VideoFrame::kUPlane), |
164 frame->stride(media::VideoFrame::kUPlane), | 168 frame->stride(media::VideoFrame::kUPlane), |
165 frame->data(media::VideoFrame::kVPlane), | 169 frame->data(media::VideoFrame::kVPlane), |
166 frame->stride(media::VideoFrame::kVPlane), | 170 frame->stride(media::VideoFrame::kVPlane), |
167 0 /* crop_x */, | 171 0 /* crop_x */, |
168 0 /* crop_y */, | 172 0 /* crop_y */, |
169 bitmap.info().width(), | 173 bitmap.info().width(), |
170 bitmap.info().height(), | 174 bitmap.info().height(), |
171 frame->coded_size().width(), | 175 frame->coded_size().width(), |
172 frame->coded_size().height(), | 176 frame->coded_size().height(), |
173 libyuv::kRotate0, | 177 libyuv::kRotate0, |
174 libyuv::FOURCC_ARGB) == 0) { | 178 source_pixel_format) == 0) { |
175 // Success! | 179 // Success! |
176 io_task_runner_->PostTask( | 180 io_task_runner_->PostTask( |
177 FROM_HERE, base::Bind(new_frame_callback_, frame, current_time)); | 181 FROM_HERE, base::Bind(new_frame_callback_, frame, current_time)); |
178 } | 182 } |
179 | 183 |
180 // Calculate the time in the future where the next frame should be created. | 184 // Calculate the time in the future where the next frame should be created. |
181 const base::TimeDelta frame_interval = | 185 const base::TimeDelta frame_interval = |
182 base::TimeDelta::FromMicroseconds(1E6 / capture_frame_rate_); | 186 base::TimeDelta::FromMicroseconds(1E6 / capture_frame_rate_); |
183 if (next_capture_time_.is_null()) { | 187 if (next_capture_time_.is_null()) { |
184 next_capture_time_ = current_time + frame_interval; | 188 next_capture_time_ = current_time + frame_interval; |
185 } else { | 189 } else { |
186 next_capture_time_ += frame_interval; | 190 next_capture_time_ += frame_interval; |
187 // Don't accumulate any debt if we are lagging behind - just post next frame | 191 // Don't accumulate any debt if we are lagging behind - just post next frame |
188 // immediately and continue as normal. | 192 // immediately and continue as normal. |
189 if (next_capture_time_ < current_time) | 193 if (next_capture_time_ < current_time) |
190 next_capture_time_ = current_time; | 194 next_capture_time_ = current_time; |
191 } | 195 } |
192 // Schedule next capture. | 196 // Schedule next capture. |
193 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 197 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
194 FROM_HERE, base::Bind(&HtmlVideoElementCapturerSource::sendNewFrame, | 198 FROM_HERE, base::Bind(&HtmlVideoElementCapturerSource::sendNewFrame, |
195 weak_factory_.GetWeakPtr()), | 199 weak_factory_.GetWeakPtr()), |
196 next_capture_time_ - current_time); | 200 next_capture_time_ - current_time); |
197 } | 201 } |
198 | 202 |
199 } // namespace content | 203 } // namespace content |
OLD | NEW |