| 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/video/capture/fake_video_capture_device.h" | 5 #include "media/video/capture/fake_video_capture_device.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "media/audio/fake_audio_input_stream.h" | 12 #include "media/audio/fake_audio_input_stream.h" |
| 13 #include "media/base/video_frame.h" | 13 #include "media/base/video_frame.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "third_party/skia/include/core/SkCanvas.h" | 15 #include "third_party/skia/include/core/SkCanvas.h" |
| 16 #include "third_party/skia/include/core/SkPaint.h" | 16 #include "third_party/skia/include/core/SkPaint.h" |
| 17 | 17 |
| 18 namespace media { | 18 namespace media { |
| 19 | 19 |
| 20 static const int kFakeCaptureTimeoutMs = 50; | |
| 21 static const int kFakeCaptureBeepCycle = 10; // Visual beep every 0.5s. | 20 static const int kFakeCaptureBeepCycle = 10; // Visual beep every 0.5s. |
| 22 static const int kFakeCaptureCapabilityChangePeriod = 30; | 21 static const int kFakeCaptureCapabilityChangePeriod = 30; |
| 23 enum { kNumberOfFakeDevices = 2 }; | |
| 24 | |
| 25 bool FakeVideoCaptureDevice::fail_next_create_ = false; | |
| 26 base::subtle::Atomic32 FakeVideoCaptureDevice::number_of_devices_ = | |
| 27 kNumberOfFakeDevices; | |
| 28 | |
| 29 // static | |
| 30 size_t FakeVideoCaptureDevice::NumberOfFakeDevices(void) { | |
| 31 return number_of_devices_; | |
| 32 } | |
| 33 | |
| 34 // static | |
| 35 void FakeVideoCaptureDevice::GetDeviceNames(Names* const device_names) { | |
| 36 // Empty the name list. | |
| 37 device_names->erase(device_names->begin(), device_names->end()); | |
| 38 | |
| 39 int number_of_devices = base::subtle::NoBarrier_Load(&number_of_devices_); | |
| 40 for (int32 n = 0; n < number_of_devices; n++) { | |
| 41 Name name(base::StringPrintf("fake_device_%d", n), | |
| 42 base::StringPrintf("/dev/video%d", n)); | |
| 43 device_names->push_back(name); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 // static | |
| 48 void FakeVideoCaptureDevice::GetDeviceSupportedFormats( | |
| 49 const Name& device, | |
| 50 VideoCaptureFormats* supported_formats) { | |
| 51 | |
| 52 const size_t supported_sizes_length = 3; | |
| 53 const gfx::Size supported_sizes[] = {gfx::Size(320, 240), | |
| 54 gfx::Size(640, 480), | |
| 55 gfx::Size(1280, 720)}; | |
| 56 int frame_rate = 1000 / kFakeCaptureTimeoutMs; | |
| 57 supported_formats->clear(); | |
| 58 for (size_t i=0; i < supported_sizes_length; ++i) { | |
| 59 supported_formats->push_back(VideoCaptureFormat(supported_sizes[i], | |
| 60 frame_rate, | |
| 61 media::PIXEL_FORMAT_I420)); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 // static | |
| 66 VideoCaptureDevice* FakeVideoCaptureDevice::Create(const Name& device_name) { | |
| 67 if (fail_next_create_) { | |
| 68 fail_next_create_ = false; | |
| 69 return NULL; | |
| 70 } | |
| 71 int number_of_devices = base::subtle::NoBarrier_Load(&number_of_devices_); | |
| 72 for (int32 n = 0; n < number_of_devices; ++n) { | |
| 73 std::string possible_id = base::StringPrintf("/dev/video%d", n); | |
| 74 if (device_name.id().compare(possible_id) == 0) { | |
| 75 return new FakeVideoCaptureDevice(); | |
| 76 } | |
| 77 } | |
| 78 return NULL; | |
| 79 } | |
| 80 | |
| 81 // static | |
| 82 void FakeVideoCaptureDevice::SetFailNextCreate() { | |
| 83 fail_next_create_ = true; | |
| 84 } | |
| 85 | |
| 86 // static | |
| 87 void FakeVideoCaptureDevice::SetNumberOfFakeDevices(size_t number_of_devices) { | |
| 88 base::subtle::NoBarrier_AtomicExchange(&number_of_devices_, | |
| 89 number_of_devices); | |
| 90 } | |
| 91 | 22 |
| 92 FakeVideoCaptureDevice::FakeVideoCaptureDevice() | 23 FakeVideoCaptureDevice::FakeVideoCaptureDevice() |
| 93 : capture_thread_("CaptureThread"), | 24 : capture_thread_("CaptureThread"), |
| 94 frame_count_(0), | 25 frame_count_(0), |
| 95 format_roster_index_(0) {} | 26 format_roster_index_(0) {} |
| 96 | 27 |
| 97 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { | 28 FakeVideoCaptureDevice::~FakeVideoCaptureDevice() { |
| 98 DCHECK(thread_checker_.CalledOnValidThread()); | 29 DCHECK(thread_checker_.CalledOnValidThread()); |
| 99 DCHECK(!capture_thread_.IsRunning()); | 30 DCHECK(!capture_thread_.IsRunning()); |
| 100 } | 31 } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 fake_frame_.reset(new uint8[fake_frame_size]); | 181 fake_frame_.reset(new uint8[fake_frame_size]); |
| 251 } | 182 } |
| 252 | 183 |
| 253 void FakeVideoCaptureDevice::PopulateFormatRoster() { | 184 void FakeVideoCaptureDevice::PopulateFormatRoster() { |
| 254 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); | 185 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); |
| 255 GetDeviceSupportedFormats(Name(), &format_roster_); | 186 GetDeviceSupportedFormats(Name(), &format_roster_); |
| 256 format_roster_index_ = 0; | 187 format_roster_index_ = 0; |
| 257 } | 188 } |
| 258 | 189 |
| 259 } // namespace media | 190 } // namespace media |
| OLD | NEW |