Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: media/capture/video/fake_video_capture_device.cc

Issue 2005753006: ImageCapture: ScopedResultCallback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reillyg@ second round of comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 68
69 const std::string time_string = 69 const std::string time_string =
70 base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes, seconds, 70 base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes, seconds,
71 milliseconds, frame_count); 71 milliseconds, frame_count);
72 canvas.scale(3, 3); 72 canvas.scale(3, 3);
73 canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint); 73 canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint);
74 } 74 }
75 75
76 // Creates a PNG-encoded frame and sends it back to |callback|. The other 76 // Creates a PNG-encoded frame and sends it back to |callback|. The other
77 // parameters are used to replicate the PacMan rendering. 77 // parameters are used to replicate the PacMan rendering.
78 void DoTakeFakePhoto(const VideoCaptureDevice::TakePhotoCallback& callback, 78 void DoTakeFakePhoto(
79 const VideoCaptureFormat& capture_format, 79 ScopedCallback<VideoCaptureDevice::TakePhotoCallback> callback,
80 base::TimeDelta elapsed_time, 80 const VideoCaptureFormat& capture_format,
81 float fake_capture_rate) { 81 base::TimeDelta elapsed_time,
82 float fake_capture_rate) {
82 std::unique_ptr<uint8_t[]> buffer(new uint8_t[VideoFrame::AllocationSize( 83 std::unique_ptr<uint8_t[]> buffer(new uint8_t[VideoFrame::AllocationSize(
83 PIXEL_FORMAT_ARGB, capture_format.frame_size)]); 84 PIXEL_FORMAT_ARGB, capture_format.frame_size)]);
84 85
85 DrawPacman(true /* use_argb */, buffer.get(), elapsed_time, fake_capture_rate, 86 DrawPacman(true /* use_argb */, buffer.get(), elapsed_time, fake_capture_rate,
86 capture_format.frame_size); 87 capture_format.frame_size);
87 88
88 std::unique_ptr<std::vector<uint8_t>> encoded_data( 89 std::vector<uint8_t> encoded_data;
89 new std::vector<uint8_t>());
90 const bool result = gfx::PNGCodec::Encode( 90 const bool result = gfx::PNGCodec::Encode(
91 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size, 91 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size,
92 capture_format.frame_size.width() * 4, true /* discard_transparency */, 92 capture_format.frame_size.width() * 4, true /* discard_transparency */,
93 std::vector<gfx::PNGCodec::Comment>(), encoded_data.get()); 93 std::vector<gfx::PNGCodec::Comment>(), &encoded_data);
94 DCHECK(result); 94 DCHECK(result);
95 95
96 callback.Run("image/png", std::move(encoded_data)); 96 callback.PassCallback().Run("image/png",
97 mojo::Array<uint8_t>::From(encoded_data));
97 } 98 }
98 99
99 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership, 100 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership,
100 float fake_capture_rate) 101 float fake_capture_rate)
101 : buffer_ownership_(buffer_ownership), 102 : buffer_ownership_(buffer_ownership),
102 fake_capture_rate_(fake_capture_rate), 103 fake_capture_rate_(fake_capture_rate),
103 weak_factory_(this) {} 104 weak_factory_(this) {}
104 105
105 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { 106 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() {
106 DCHECK(thread_checker_.CalledOnValidThread()); 107 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 base::TimeTicks::Now(), 155 base::TimeTicks::Now(),
155 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers, 156 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers,
156 weak_factory_.GetWeakPtr())); 157 weak_factory_.GetWeakPtr()));
157 } 158 }
158 159
159 void FakeVideoCaptureDevice::StopAndDeAllocate() { 160 void FakeVideoCaptureDevice::StopAndDeAllocate() {
160 DCHECK(thread_checker_.CalledOnValidThread()); 161 DCHECK(thread_checker_.CalledOnValidThread());
161 client_.reset(); 162 client_.reset();
162 } 163 }
163 164
164 bool FakeVideoCaptureDevice::TakePhoto( 165 void FakeVideoCaptureDevice::TakePhoto(
165 const TakePhotoCallback& photo_callback) { 166 ScopedCallback<TakePhotoCallback> callback) {
166 base::MessageLoop::current()->PostTask( 167 base::MessageLoop::current()->PostTask(
167 FROM_HERE, base::Bind(&DoTakeFakePhoto, photo_callback, capture_format_, 168 FROM_HERE,
168 elapsed_time_, fake_capture_rate_)); 169 base::Bind(&DoTakeFakePhoto, base::Passed(&callback), capture_format_,
169 return true; 170 elapsed_time_, fake_capture_rate_));
170 } 171 }
171 172
172 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers( 173 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers(
173 base::TimeTicks expected_execution_time) { 174 base::TimeTicks expected_execution_time) {
174 DCHECK(thread_checker_.CalledOnValidThread()); 175 DCHECK(thread_checker_.CalledOnValidThread());
175 const size_t frame_size = capture_format_.ImageAllocationSize(); 176 const size_t frame_size = capture_format_.ImageAllocationSize();
176 memset(fake_frame_.get(), 0, frame_size); 177 memset(fake_frame_.get(), 0, frame_size);
177 178
178 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, 179 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_,
179 fake_capture_rate_, capture_format_.frame_size); 180 fake_capture_rate_, capture_format_.frame_size);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 // Don't accumulate any debt if we are lagging behind - just post the next 258 // Don't accumulate any debt if we are lagging behind - just post the next
258 // frame immediately and continue as normal. 259 // frame immediately and continue as normal.
259 const base::TimeTicks next_execution_time = 260 const base::TimeTicks next_execution_time =
260 std::max(current_time, expected_execution_time + frame_interval); 261 std::max(current_time, expected_execution_time + frame_interval);
261 const base::TimeDelta delay = next_execution_time - current_time; 262 const base::TimeDelta delay = next_execution_time - current_time;
262 base::MessageLoop::current()->PostDelayedTask( 263 base::MessageLoop::current()->PostDelayedTask(
263 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); 264 FROM_HERE, base::Bind(next_capture, next_execution_time), delay);
264 } 265 }
265 266
266 } // namespace media 267 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698