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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 base::TimeTicks expected_execution_time) { | 257 base::TimeTicks expected_execution_time) { |
255 DCHECK(thread_checker_.CalledOnValidThread()); | 258 DCHECK(thread_checker_.CalledOnValidThread()); |
256 | 259 |
257 std::unique_ptr<VideoCaptureDevice::Client::Buffer> capture_buffer( | 260 std::unique_ptr<VideoCaptureDevice::Client::Buffer> capture_buffer( |
258 client_->ReserveOutputBuffer(capture_format_.frame_size, | 261 client_->ReserveOutputBuffer(capture_format_.frame_size, |
259 capture_format_.pixel_format, | 262 capture_format_.pixel_format, |
260 capture_format_.pixel_storage)); | 263 capture_format_.pixel_storage)); |
261 DLOG_IF(ERROR, !capture_buffer) << "Couldn't allocate Capture Buffer"; | 264 DLOG_IF(ERROR, !capture_buffer) << "Couldn't allocate Capture Buffer"; |
262 DCHECK(capture_buffer->data()) << "Buffer has NO backing memory"; | 265 DCHECK(capture_buffer->data()) << "Buffer has NO backing memory"; |
263 | 266 |
264 if (capture_format_.pixel_storage == PIXEL_STORAGE_GPUMEMORYBUFFER && | 267 if (capture_format_.pixel_storage == PIXEL_STORAGE_GPUMEMORYBUFFER) { |
265 capture_format_.pixel_format == media::PIXEL_FORMAT_I420) { | |
266 // Since SkBitmap expects a packed&continuous memory region for I420, we | 268 // Since SkBitmap expects a packed&continuous memory region for I420, we |
267 // need to use |fake_frame_| to draw onto. | 269 // need to use |fake_frame_| to draw onto. |
268 memset(fake_frame_.get(), 0, capture_format_.ImageAllocationSize()); | 270 memset(fake_frame_.get(), 0, capture_format_.ImageAllocationSize()); |
269 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, | 271 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, |
270 fake_capture_rate_, capture_format_.frame_size, current_zoom_); | 272 fake_capture_rate_, capture_format_.frame_size, current_zoom_); |
271 | 273 |
272 // Copy data from |fake_frame_| into the reserved planes of GpuMemoryBuffer. | 274 // Copy data from |fake_frame_| into the reserved planes of GpuMemoryBuffer. |
273 size_t offset = 0; | 275 size_t offset = 0; |
274 for (size_t i = 0; i < VideoFrame::NumPlanes(PIXEL_FORMAT_I420); ++i) { | 276 for (size_t i = 0; i < VideoFrame::NumPlanes(capture_format_.pixel_format); |
| 277 ++i) { |
275 const size_t plane_size = | 278 const size_t plane_size = |
276 VideoFrame::PlaneSize(PIXEL_FORMAT_I420, i, | 279 VideoFrame::PlaneSize(capture_format_.pixel_format, i, |
277 capture_format_.frame_size) | 280 capture_format_.frame_size) |
278 .GetArea(); | 281 .GetArea(); |
279 memcpy(capture_buffer->data(i), fake_frame_.get() + offset, plane_size); | 282 memcpy(capture_buffer->data(i), fake_frame_.get() + offset, plane_size); |
280 offset += plane_size; | 283 offset += plane_size; |
281 } | 284 } |
282 } else { | 285 } else { |
283 DCHECK_EQ(capture_format_.pixel_storage, PIXEL_STORAGE_CPU); | 286 DCHECK_EQ(capture_format_.pixel_storage, PIXEL_STORAGE_CPU); |
284 DCHECK_EQ(capture_format_.pixel_format, PIXEL_FORMAT_ARGB); | 287 DCHECK_EQ(capture_format_.pixel_format, PIXEL_FORMAT_ARGB); |
285 uint8_t* data_ptr = static_cast<uint8_t*>(capture_buffer->data()); | 288 uint8_t* data_ptr = static_cast<uint8_t*>(capture_buffer->data()); |
286 memset(data_ptr, 0, capture_buffer->mapped_size()); | 289 memset(data_ptr, 0, capture_buffer->mapped_size()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 // Don't accumulate any debt if we are lagging behind - just post the next | 325 // Don't accumulate any debt if we are lagging behind - just post the next |
323 // frame immediately and continue as normal. | 326 // frame immediately and continue as normal. |
324 const base::TimeTicks next_execution_time = | 327 const base::TimeTicks next_execution_time = |
325 std::max(current_time, expected_execution_time + frame_interval); | 328 std::max(current_time, expected_execution_time + frame_interval); |
326 const base::TimeDelta delay = next_execution_time - current_time; | 329 const base::TimeDelta delay = next_execution_time - current_time; |
327 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 330 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
328 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); | 331 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); |
329 } | 332 } |
330 | 333 |
331 } // namespace media | 334 } // namespace media |
OLD | NEW |