Chromium Code Reviews| 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 |
| 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 Loading... | |
| 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, | |
|
miu
2016/05/05 22:04:24
naming: RunTakePhotoCallback() doesn't really tell
mcasas
2016/05/05 23:05:50
Done.
| |
| 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 Loading... | |
| 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, | |
| 168 base::Bind(&RunTakePhotoCallback, photo_callback, capture_format_, | |
| 169 elapsed_time_, fake_capture_rate_)); | |
| 170 return true; | |
| 171 } | |
| 172 | |
| 140 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers( | 173 void FakeVideoCaptureDevice::CaptureUsingOwnBuffers( |
| 141 base::TimeTicks expected_execution_time) { | 174 base::TimeTicks expected_execution_time) { |
| 142 DCHECK(thread_checker_.CalledOnValidThread()); | 175 DCHECK(thread_checker_.CalledOnValidThread()); |
| 143 const size_t frame_size = capture_format_.ImageAllocationSize(); | 176 const size_t frame_size = capture_format_.ImageAllocationSize(); |
| 144 memset(fake_frame_.get(), 0, frame_size); | 177 memset(fake_frame_.get(), 0, frame_size); |
| 145 | 178 |
| 146 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, | 179 DrawPacman(false /* use_argb */, fake_frame_.get(), elapsed_time_, |
| 147 fake_capture_rate_, capture_format_.frame_size); | 180 fake_capture_rate_, capture_format_.frame_size); |
| 148 | 181 |
| 149 // Give the captured frame to the client. | 182 // Give the captured frame to the client. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 // 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 |
| 226 // frame immediately and continue as normal. | 259 // frame immediately and continue as normal. |
| 227 const base::TimeTicks next_execution_time = | 260 const base::TimeTicks next_execution_time = |
| 228 std::max(current_time, expected_execution_time + frame_interval); | 261 std::max(current_time, expected_execution_time + frame_interval); |
| 229 const base::TimeDelta delay = next_execution_time - current_time; | 262 const base::TimeDelta delay = next_execution_time - current_time; |
| 230 base::MessageLoop::current()->PostDelayedTask( | 263 base::MessageLoop::current()->PostDelayedTask( |
| 231 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); | 264 FROM_HERE, base::Bind(next_capture, next_execution_time), delay); |
| 232 } | 265 } |
| 233 | 266 |
| 234 } // namespace media | 267 } // namespace media |
| OLD | NEW |