OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "media/capture/video/fake_video_capture_device.h" | 5 #include "media/capture/video/fake_video_capture_device.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size, | 106 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size, |
107 capture_format.frame_size.width() * 4, true /* discard_transparency */, | 107 capture_format.frame_size.width() * 4, true /* discard_transparency */, |
108 std::vector<gfx::PNGCodec::Comment>(), &blob->data); | 108 std::vector<gfx::PNGCodec::Comment>(), &blob->data); |
109 DCHECK(result); | 109 DCHECK(result); |
110 | 110 |
111 blob->mime_type = "image/png"; | 111 blob->mime_type = "image/png"; |
112 callback.Run(std::move(blob)); | 112 callback.Run(std::move(blob)); |
113 } | 113 } |
114 | 114 |
115 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership, | 115 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership, |
116 float fake_capture_rate) | 116 float fake_capture_rate, |
| 117 VideoPixelFormat pixel_format) |
117 : buffer_ownership_(buffer_ownership), | 118 : buffer_ownership_(buffer_ownership), |
118 fake_capture_rate_(fake_capture_rate), | 119 fake_capture_rate_(fake_capture_rate), |
| 120 pixel_format_(pixel_format), |
119 current_zoom_(kMinZoom), | 121 current_zoom_(kMinZoom), |
120 weak_factory_(this) {} | 122 weak_factory_(this) {} |
121 | 123 |
122 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { | 124 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { |
123 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
124 } | 126 } |
125 | 127 |
126 void FakeVideoCaptureDevice::AllocateAndStart( | 128 void FakeVideoCaptureDevice::AllocateAndStart( |
127 const VideoCaptureParams& params, | 129 const VideoCaptureParams& params, |
128 std::unique_ptr<VideoCaptureDevice::Client> client) { | 130 std::unique_ptr<VideoCaptureDevice::Client> client) { |
129 DCHECK(thread_checker_.CalledOnValidThread()); | 131 DCHECK(thread_checker_.CalledOnValidThread()); |
130 | 132 |
131 client_ = std::move(client); | 133 client_ = std::move(client); |
132 | 134 |
133 // Incoming |params| can be none of the supported formats, so we get the | 135 // Incoming |params| can be none of the supported formats, so we get the |
134 // closest thing rounded up. TODO(mcasas): Use the |params|, if they belong to | 136 // closest thing rounded up. TODO(mcasas): Use the |params|, if they belong to |
135 // the supported ones, when http://crbug.com/309554 is verified. | 137 // the supported ones, when http://crbug.com/309554 is verified. |
136 capture_format_.frame_rate = fake_capture_rate_; | 138 capture_format_.frame_rate = fake_capture_rate_; |
137 if (params.requested_format.frame_size.width() > 1280) | 139 if (params.requested_format.frame_size.width() > 1280) |
138 capture_format_.frame_size.SetSize(1920, 1080); | 140 capture_format_.frame_size.SetSize(1920, 1080); |
139 else if (params.requested_format.frame_size.width() > 640) | 141 else if (params.requested_format.frame_size.width() > 640) |
140 capture_format_.frame_size.SetSize(1280, 720); | 142 capture_format_.frame_size.SetSize(1280, 720); |
141 else if (params.requested_format.frame_size.width() > 320) | 143 else if (params.requested_format.frame_size.width() > 320) |
142 capture_format_.frame_size.SetSize(640, 480); | 144 capture_format_.frame_size.SetSize(640, 480); |
143 else | 145 else |
144 capture_format_.frame_size.SetSize(320, 240); | 146 capture_format_.frame_size.SetSize(320, 240); |
145 | 147 |
| 148 capture_format_.pixel_format = pixel_format_; |
146 if (buffer_ownership_ == BufferOwnership::CLIENT_BUFFERS) { | 149 if (buffer_ownership_ == BufferOwnership::CLIENT_BUFFERS) { |
147 capture_format_.pixel_storage = PIXEL_STORAGE_CPU; | 150 capture_format_.pixel_storage = PIXEL_STORAGE_CPU; |
148 capture_format_.pixel_format = PIXEL_FORMAT_ARGB; | 151 capture_format_.pixel_format = PIXEL_FORMAT_ARGB; |
149 DVLOG(1) << "starting with client argb buffers"; | 152 DVLOG(1) << "starting with client argb buffers"; |
150 } else if (buffer_ownership_ == BufferOwnership::OWN_BUFFERS) { | 153 } else if (buffer_ownership_ == BufferOwnership::OWN_BUFFERS) { |
151 capture_format_.pixel_storage = PIXEL_STORAGE_CPU; | 154 capture_format_.pixel_storage = PIXEL_STORAGE_CPU; |
152 capture_format_.pixel_format = PIXEL_FORMAT_I420; | 155 DVLOG(1) << "starting with own" << VideoPixelFormatToString(pixel_format_) |
153 DVLOG(1) << "starting with own I420 buffers"; | 156 << "buffers"; |
154 } | 157 } |
155 | 158 |
156 if (capture_format_.pixel_format == PIXEL_FORMAT_I420) { | 159 if (buffer_ownership_ != BufferOwnership::CLIENT_BUFFERS) { |
157 fake_frame_.reset(new uint8_t[VideoFrame::AllocationSize( | 160 fake_frame_.reset(new uint8_t[VideoFrame::AllocationSize( |
158 PIXEL_FORMAT_I420, capture_format_.frame_size)]); | 161 pixel_format_, capture_format_.frame_size)]); |
159 } | 162 } |
160 | 163 |
161 beep_time_ = base::TimeDelta(); | 164 beep_time_ = base::TimeDelta(); |
162 elapsed_time_ = base::TimeDelta(); | 165 elapsed_time_ = base::TimeDelta(); |
163 | 166 |
164 if (buffer_ownership_ == BufferOwnership::CLIENT_BUFFERS) | 167 if (buffer_ownership_ == BufferOwnership::CLIENT_BUFFERS) |
165 BeepAndScheduleNextCapture( | 168 BeepAndScheduleNextCapture( |
166 base::TimeTicks::Now(), | 169 base::TimeTicks::Now(), |
167 base::Bind(&FakeVideoCaptureDevice::CaptureUsingClientBuffers, | 170 base::Bind(&FakeVideoCaptureDevice::CaptureUsingClientBuffers, |
168 weak_factory_.GetWeakPtr())); | 171 weak_factory_.GetWeakPtr())); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 base::TimeTicks expected_execution_time) { | 245 base::TimeTicks expected_execution_time) { |
243 DCHECK(thread_checker_.CalledOnValidThread()); | 246 DCHECK(thread_checker_.CalledOnValidThread()); |
244 | 247 |
245 std::unique_ptr<VideoCaptureDevice::Client::Buffer> capture_buffer( | 248 std::unique_ptr<VideoCaptureDevice::Client::Buffer> capture_buffer( |
246 client_->ReserveOutputBuffer(capture_format_.frame_size, | 249 client_->ReserveOutputBuffer(capture_format_.frame_size, |
247 capture_format_.pixel_format, | 250 capture_format_.pixel_format, |
248 capture_format_.pixel_storage)); | 251 capture_format_.pixel_storage)); |
249 DLOG_IF(ERROR, !capture_buffer) << "Couldn't allocate Capture Buffer"; | 252 DLOG_IF(ERROR, !capture_buffer) << "Couldn't allocate Capture Buffer"; |
250 DCHECK(capture_buffer->data()) << "Buffer has NO backing memory"; | 253 DCHECK(capture_buffer->data()) << "Buffer has NO backing memory"; |
251 | 254 |
252 if (capture_format_.pixel_storage == PIXEL_STORAGE_GPUMEMORYBUFFER && | 255 if (capture_format_.pixel_storage == PIXEL_STORAGE_GPUMEMORYBUFFER) { |
253 capture_format_.pixel_format == media::PIXEL_FORMAT_I420) { | |
254 // Since SkBitmap expects a packed&continuous memory region for I420, we | 256 // Since SkBitmap expects a packed&continuous memory region for I420, we |
255 // need to use |fake_frame_| to draw onto. | 257 // need to use |fake_frame_| to draw onto. |
256 memset(fake_frame_.get(), 0, capture_format_.ImageAllocationSize()); | 258 memset(fake_frame_.get(), 0, capture_format_.ImageAllocationSize()); |
257 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, | 259 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, |
258 fake_capture_rate_, capture_format_.frame_size, current_zoom_); | 260 fake_capture_rate_, capture_format_.frame_size, current_zoom_); |
259 | 261 |
260 // Copy data from |fake_frame_| into the reserved planes of GpuMemoryBuffer. | 262 // Copy data from |fake_frame_| into the reserved planes of GpuMemoryBuffer. |
261 size_t offset = 0; | 263 size_t offset = 0; |
262 for (size_t i = 0; i < VideoFrame::NumPlanes(PIXEL_FORMAT_I420); ++i) { | 264 for (size_t i = 0; i < VideoFrame::NumPlanes(capture_format_.pixel_format); |
| 265 ++i) { |
263 const size_t plane_size = | 266 const size_t plane_size = |
264 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, i, | 267 VideoFrame::PlaneSize(capture_format_.pixel_format, i, |
265 capture_format_.frame_size) | 268 capture_format_.frame_size) |
266 .GetArea(); | 269 .GetArea(); |
267 memcpy(capture_buffer->data(i), fake_frame_.get() + offset, plane_size); | 270 memcpy(capture_buffer->data(i), fake_frame_.get() + offset, plane_size); |
268 offset += plane_size; | 271 offset += plane_size; |
269 } | 272 } |
270 } else { | 273 } else { |
271 DCHECK_EQ(capture_format_.pixel_storage, PIXEL_STORAGE_CPU); | 274 DCHECK_EQ(capture_format_.pixel_storage, PIXEL_STORAGE_CPU); |
272 DCHECK_EQ(capture_format_.pixel_format, PIXEL_FORMAT_ARGB); | 275 DCHECK_EQ(capture_format_.pixel_format, PIXEL_FORMAT_ARGB); |
273 uint8_t* data_ptr = static_cast<uint8_t*>(capture_buffer->data()); | 276 uint8_t* data_ptr = static_cast<uint8_t*>(capture_buffer->data()); |
274 memset(data_ptr, 0, capture_buffer->mapped_size()); | 277 memset(data_ptr, 0, capture_buffer->mapped_size()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 // Don't accumulate any debt if we are lagging behind - just post the next | 313 // Don't accumulate any debt if we are lagging behind - just post the next |
311 // frame immediately and continue as normal. | 314 // frame immediately and continue as normal. |
312 const base::TimeTicks next_execution_time = | 315 const base::TimeTicks next_execution_time = |
313 std::max(current_time, expected_execution_time + frame_interval); | 316 std::max(current_time, expected_execution_time + frame_interval); |
314 const base::TimeDelta delay = next_execution_time - current_time; | 317 const base::TimeDelta delay = next_execution_time - current_time; |
315 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 318 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
316 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); | 319 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); |
317 } | 320 } |
318 | 321 |
319 } // namespace media | 322 } // namespace media |
OLD | NEW |