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

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: rockot@s nit Created 4 years, 6 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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "media/audio/fake_audio_input_stream.h" 13 #include "media/audio/fake_audio_input_stream.h"
14 #include "media/base/video_frame.h" 14 #include "media/base/video_frame.h"
15 #include "mojo/public/cpp/bindings/string.h"
15 #include "third_party/skia/include/core/SkBitmap.h" 16 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "third_party/skia/include/core/SkCanvas.h" 17 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkPaint.h" 18 #include "third_party/skia/include/core/SkPaint.h"
18 #include "ui/gfx/codec/png_codec.h" 19 #include "ui/gfx/codec/png_codec.h"
19 20
20 namespace media { 21 namespace media {
21 22
22 // Sweep at 600 deg/sec. 23 // Sweep at 600 deg/sec.
23 static const float kPacmanAngularVelocity = 600; 24 static const float kPacmanAngularVelocity = 600;
24 // Beep every 500 ms. 25 // Beep every 500 ms.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 69
69 const std::string time_string = 70 const std::string time_string =
70 base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes, seconds, 71 base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes, seconds,
71 milliseconds, frame_count); 72 milliseconds, frame_count);
72 canvas.scale(3, 3); 73 canvas.scale(3, 3);
73 canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint); 74 canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint);
74 } 75 }
75 76
76 // Creates a PNG-encoded frame and sends it back to |callback|. The other 77 // Creates a PNG-encoded frame and sends it back to |callback|. The other
77 // parameters are used to replicate the PacMan rendering. 78 // parameters are used to replicate the PacMan rendering.
78 void DoTakeFakePhoto(const VideoCaptureDevice::TakePhotoCallback& callback, 79 void DoTakeFakePhoto(
79 const VideoCaptureFormat& capture_format, 80 ScopedResultCallback<VideoCaptureDevice::TakePhotoCallback> callback,
80 base::TimeDelta elapsed_time, 81 const VideoCaptureFormat& capture_format,
81 float fake_capture_rate) { 82 base::TimeDelta elapsed_time,
83 float fake_capture_rate) {
82 std::unique_ptr<uint8_t[]> buffer(new uint8_t[VideoFrame::AllocationSize( 84 std::unique_ptr<uint8_t[]> buffer(new uint8_t[VideoFrame::AllocationSize(
83 PIXEL_FORMAT_ARGB, capture_format.frame_size)]); 85 PIXEL_FORMAT_ARGB, capture_format.frame_size)]);
84 86
85 DrawPacman(true /* use_argb */, buffer.get(), elapsed_time, fake_capture_rate, 87 DrawPacman(true /* use_argb */, buffer.get(), elapsed_time, fake_capture_rate,
86 capture_format.frame_size); 88 capture_format.frame_size);
87 89
88 std::unique_ptr<std::vector<uint8_t>> encoded_data( 90 std::vector<uint8_t> encoded_data;
89 new std::vector<uint8_t>());
90 const bool result = gfx::PNGCodec::Encode( 91 const bool result = gfx::PNGCodec::Encode(
91 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size, 92 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size,
92 capture_format.frame_size.width() * 4, true /* discard_transparency */, 93 capture_format.frame_size.width() * 4, true /* discard_transparency */,
93 std::vector<gfx::PNGCodec::Comment>(), encoded_data.get()); 94 std::vector<gfx::PNGCodec::Comment>(), &encoded_data);
94 DCHECK(result); 95 DCHECK(result);
95 96
96 callback.Run("image/png", std::move(encoded_data)); 97 callback.Run(mojo::String::From("image/png"),
98 mojo::Array<uint8_t>::From(encoded_data));
97 } 99 }
98 100
99 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership, 101 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership,
100 float fake_capture_rate) 102 float fake_capture_rate)
101 : buffer_ownership_(buffer_ownership), 103 : buffer_ownership_(buffer_ownership),
102 fake_capture_rate_(fake_capture_rate), 104 fake_capture_rate_(fake_capture_rate),
103 weak_factory_(this) {} 105 weak_factory_(this) {}
104 106
105 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { 107 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() {
106 DCHECK(thread_checker_.CalledOnValidThread()); 108 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 base::TimeTicks::Now(), 156 base::TimeTicks::Now(),
155 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers, 157 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers,
156 weak_factory_.GetWeakPtr())); 158 weak_factory_.GetWeakPtr()));
157 } 159 }
158 160
159 void FakeVideoCaptureDevice::StopAndDeAllocate() { 161 void FakeVideoCaptureDevice::StopAndDeAllocate() {
160 DCHECK(thread_checker_.CalledOnValidThread()); 162 DCHECK(thread_checker_.CalledOnValidThread());
161 client_.reset(); 163 client_.reset();
162 } 164 }
163 165
164 bool FakeVideoCaptureDevice::TakePhoto( 166 void FakeVideoCaptureDevice::TakePhoto(
165 const TakePhotoCallback& photo_callback) { 167 ScopedResultCallback<TakePhotoCallback> callback) {
166 base::MessageLoop::current()->PostTask( 168 base::MessageLoop::current()->PostTask(
167 FROM_HERE, base::Bind(&DoTakeFakePhoto, photo_callback, capture_format_, 169 FROM_HERE,
168 elapsed_time_, fake_capture_rate_)); 170 base::Bind(&DoTakeFakePhoto, base::Passed(&callback), capture_format_,
169 return true; 171 elapsed_time_, fake_capture_rate_));
170 } 172 }
171 173
172 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers( 174 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers(
173 base::TimeTicks expected_execution_time) { 175 base::TimeTicks expected_execution_time) {
174 DCHECK(thread_checker_.CalledOnValidThread()); 176 DCHECK(thread_checker_.CalledOnValidThread());
175 const size_t frame_size = capture_format_.ImageAllocationSize(); 177 const size_t frame_size = capture_format_.ImageAllocationSize();
176 memset(fake_frame_.get(), 0, frame_size); 178 memset(fake_frame_.get(), 0, frame_size);
177 179
178 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, 180 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_,
179 fake_capture_rate_, capture_format_.frame_size); 181 fake_capture_rate_, capture_format_.frame_size);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 // Don't accumulate any debt if we are lagging behind - just post the next 265 // Don't accumulate any debt if we are lagging behind - just post the next
264 // frame immediately and continue as normal. 266 // frame immediately and continue as normal.
265 const base::TimeTicks next_execution_time = 267 const base::TimeTicks next_execution_time =
266 std::max(current_time, expected_execution_time + frame_interval); 268 std::max(current_time, expected_execution_time + frame_interval);
267 const base::TimeDelta delay = next_execution_time - current_time; 269 const base::TimeDelta delay = next_execution_time - current_time;
268 base::MessageLoop::current()->PostDelayedTask( 270 base::MessageLoop::current()->PostDelayedTask(
269 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); 271 FROM_HERE, base::Bind(next_capture, next_execution_time), delay);
270 } 272 }
271 273
272 } // namespace media 274 } // namespace media
OLDNEW
« no previous file with comments | « media/capture/video/fake_video_capture_device.h ('k') | media/capture/video/fake_video_capture_device_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698