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

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: tommi@ and mlamouri@ 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
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 DoTakeFakePhoto(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::unique_ptr<std::vector<uint8_t>> encoded_data(
89 new std::vector<uint8_t>());
90 const bool result = gfx::PNGCodec::Encode(
91 buffer.get(), gfx::PNGCodec::FORMAT_RGBA, capture_format.frame_size,
92 capture_format.frame_size.width() * 4, true /* discard_transparency */,
93 std::vector<gfx::PNGCodec::Comment>(), encoded_data.get());
94 DCHECK(result);
95
96 callback.Run("image/png", std::move(encoded_data));
97 }
98
75 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership, 99 FakeVideoCaptureDevice::FakeVideoCaptureDevice(BufferOwnership buffer_ownership,
76 float fake_capture_rate) 100 float fake_capture_rate)
77 : buffer_ownership_(buffer_ownership), 101 : buffer_ownership_(buffer_ownership),
78 fake_capture_rate_(fake_capture_rate), 102 fake_capture_rate_(fake_capture_rate),
79 weak_factory_(this) {} 103 weak_factory_(this) {}
80 104
81 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { 105 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() {
82 DCHECK(thread_checker_.CalledOnValidThread()); 106 DCHECK(thread_checker_.CalledOnValidThread());
83 } 107 }
84 108
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 base::TimeTicks::Now(), 154 base::TimeTicks::Now(),
131 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers, 155 base::Bind(&FakeVideoCaptureDevice::CaptureUsingOwnBuffers,
132 weak_factory_.GetWeakPtr())); 156 weak_factory_.GetWeakPtr()));
133 } 157 }
134 158
135 void FakeVideoCaptureDevice::StopAndDeAllocate() { 159 void FakeVideoCaptureDevice::StopAndDeAllocate() {
136 DCHECK(thread_checker_.CalledOnValidThread()); 160 DCHECK(thread_checker_.CalledOnValidThread());
137 client_.reset(); 161 client_.reset();
138 } 162 }
139 163
164 bool FakeVideoCaptureDevice::TakePhoto(
165 const TakePhotoCallback& photo_callback) {
166 base::MessageLoop::current()->PostTask(
167 FROM_HERE, base::Bind(&DoTakeFakePhoto, photo_callback, capture_format_,
168 elapsed_time_, fake_capture_rate_));
169 return true;
170 }
171
140 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers( 172 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers(
141 base::TimeTicks expected_execution_time) { 173 base::TimeTicks expected_execution_time) {
142 DCHECK(thread_checker_.CalledOnValidThread()); 174 DCHECK(thread_checker_.CalledOnValidThread());
143 const size_t frame_size = capture_format_.ImageAllocationSize(); 175 const size_t frame_size = capture_format_.ImageAllocationSize();
144 memset(fake_frame_.get(), 0, frame_size); 176 memset(fake_frame_.get(), 0, frame_size);
145 177
146 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, 178 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_,
147 fake_capture_rate_, capture_format_.frame_size); 179 fake_capture_rate_, capture_format_.frame_size);
148 180
149 // Give the captured frame to the client. 181 // 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 257 // Don't accumulate any debt if we are lagging behind - just post the next
226 // frame immediately and continue as normal. 258 // frame immediately and continue as normal.
227 const base::TimeTicks next_execution_time = 259 const base::TimeTicks next_execution_time =
228 std::max(current_time, expected_execution_time + frame_interval); 260 std::max(current_time, expected_execution_time + frame_interval);
229 const base::TimeDelta delay = next_execution_time - current_time; 261 const base::TimeDelta delay = next_execution_time - current_time;
230 base::MessageLoop::current()->PostDelayedTask( 262 base::MessageLoop::current()->PostDelayedTask(
231 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); 263 FROM_HERE, base::Bind(next_capture, next_execution_time), delay);
232 } 264 }
233 265
234 } // namespace media 266 } // 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