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

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

Issue 1952463002: Media Stream Image Capture (4): wire takePhoto and implement in FakeVCDevice (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: unittests 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
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 "third_party/skia/include/core/SkBitmap.h" 15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "third_party/skia/include/core/SkCanvas.h" 16 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkPaint.h" 17 #include "third_party/skia/include/core/SkPaint.h"
18 #include "ui/gfx/codec/png_codec.h"
18 19
19 namespace media { 20 namespace media {
20 21
21 // Sweep at 600 deg/sec. 22 // Sweep at 600 deg/sec.
22 static const float kPacmanAngularVelocity = 600; 23 static const float kPacmanAngularVelocity = 600;
23 // Beep every 500 ms. 24 // Beep every 500 ms.
24 static const int kBeepInterval = 500; 25 static const int kBeepInterval = 500;
25 26
26 void DrawPacman(bool use_argb, 27 void DrawPacman(bool use_argb,
27 uint8_t* const data, 28 uint8_t* const data,
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 const int hours = elapsed_time.InHours(); 66 const int hours = elapsed_time.InHours();
66 const int frame_count = elapsed_time.InMilliseconds() * frame_rate / 1000; 67 const int frame_count = elapsed_time.InMilliseconds() * frame_rate / 1000;
67 68
68 const std::string time_string = 69 const std::string time_string =
69 base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes, seconds, 70 base::StringPrintf("%d:%02d:%02d:%03d %d", hours, minutes, seconds,
70 milliseconds, frame_count); 71 milliseconds, frame_count);
71 canvas.scale(3, 3); 72 canvas.scale(3, 3);
72 canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint); 73 canvas.drawText(time_string.data(), time_string.length(), 30, 20, paint);
73 } 74 }
74 75
76 // Creates a PNG-encoded frame and sends it back to |callback|. The other
77 // parameters are used to replicate the PacMan rendering.
78 void RunTakePhotoCallback(const VideoCaptureDevice::TakePhotoCallback& callback,
79 const VideoCaptureFormat& capture_format,
80 base::TimeDelta elapsed_time,
81 float fake_capture_rate) {
82 std::unique_ptr<uint8_t[]> buffer(new uint8_t[VideoFrame::AllocationSize(
83 PIXEL_FORMAT_ARGB, capture_format.frame_size)]);
84
85 DrawPacman(true /* use_argb */, buffer.get(), elapsed_time, fake_capture_rate,
86 capture_format.frame_size);
87
88 std::vector<unsigned char> encoded_data;
89 const bool result = gfx::PNGCodec::Encode(
90 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size,
91 capture_format.frame_size.width() * 4, true /* discard_transparency */,
92 std::vector<gfx::PNGCodec::Comment>(), &encoded_data);
93 DCHECK(result);
94
95 std::unique_ptr<std::string> encoded_data_as_string(
96 new std::string(encoded_data.begin(), encoded_data.end()));
97 callback.Run("image/png", std::move(encoded_data_as_string));
98 }
99
75 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership, 100 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership,
76 float fake_capture_rate) 101 float fake_capture_rate)
77 : buffer_ownership_(buffer_ownership), 102 : buffer_ownership_(buffer_ownership),
78 fake_capture_rate_(fake_capture_rate), 103 fake_capture_rate_(fake_capture_rate),
79 weak_factory_(this) {} 104 weak_factory_(this) {}
80 105
81 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { 106 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() {
82 DCHECK(thread_checker_.CalledOnValidThread()); 107 DCHECK(thread_checker_.CalledOnValidThread());
83 } 108 }
84 109
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 base::TimeTicks::Now(), 155 base::TimeTicks::Now(),
131 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers, 156 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers,
132 weak_factory_.GetWeakPtr())); 157 weak_factory_.GetWeakPtr()));
133 } 158 }
134 159
135 void FakeVideoCaptureDevice::StopAndDeAllocate() { 160 void FakeVideoCaptureDevice::StopAndDeAllocate() {
136 DCHECK(thread_checker_.CalledOnValidThread()); 161 DCHECK(thread_checker_.CalledOnValidThread());
137 client_.reset(); 162 client_.reset();
138 } 163 }
139 164
165 bool FakeVideoCaptureDevice::TakePhoto(
166 const TakePhotoCallback& photo_callback) {
167 base::MessageLoop::current()->PostTask(
168 FROM_HERE,
169 base::Bind(&RunTakePhotoCallback, photo_callback, capture_format_,
170 elapsed_time_, fake_capture_rate_));
171 return true;
172 }
173
140 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers( 174 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers(
141 base::TimeTicks expected_execution_time) { 175 base::TimeTicks expected_execution_time) {
142 DCHECK(thread_checker_.CalledOnValidThread()); 176 DCHECK(thread_checker_.CalledOnValidThread());
143 const size_t frame_size = capture_format_.ImageAllocationSize(); 177 const size_t frame_size = capture_format_.ImageAllocationSize();
144 memset(fake_frame_.get(), 0, frame_size); 178 memset(fake_frame_.get(), 0, frame_size);
145 179
146 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, 180 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_,
147 fake_capture_rate_, capture_format_.frame_size); 181 fake_capture_rate_, capture_format_.frame_size);
148 182
149 // Give the captured frame to the client. 183 // Give the captured frame to the client.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 // Don't accumulate any debt if we are lagging behind - just post the next 259 // Don't accumulate any debt if we are lagging behind - just post the next
226 // frame immediately and continue as normal. 260 // frame immediately and continue as normal.
227 const base::TimeTicks next_execution_time = 261 const base::TimeTicks next_execution_time =
228 std::max(current_time, expected_execution_time + frame_interval); 262 std::max(current_time, expected_execution_time + frame_interval);
229 const base::TimeDelta delay = next_execution_time - current_time; 263 const base::TimeDelta delay = next_execution_time - current_time;
230 base::MessageLoop::current()->PostDelayedTask( 264 base::MessageLoop::current()->PostDelayedTask(
231 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); 265 FROM_HERE, base::Bind(next_capture, next_execution_time), delay);
232 } 266 }
233 267
234 } // namespace media 268 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698