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 16 matching lines...) Expand all Loading... |
117 void FakeVideoCaptureDevice::StopAndDeAllocate() { | 48 void FakeVideoCaptureDevice::StopAndDeAllocate() { |
118 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(thread_checker_.CalledOnValidThread()); |
119 DCHECK(capture_thread_.IsRunning()); | 50 DCHECK(capture_thread_.IsRunning()); |
120 capture_thread_.message_loop()->PostTask( | 51 capture_thread_.message_loop()->PostTask( |
121 FROM_HERE, | 52 FROM_HERE, |
122 base::Bind(&FakeVideoCaptureDevice::OnStopAndDeAllocate, | 53 base::Bind(&FakeVideoCaptureDevice::OnStopAndDeAllocate, |
123 base::Unretained(this))); | 54 base::Unretained(this))); |
124 capture_thread_.Stop(); | 55 capture_thread_.Stop(); |
125 } | 56 } |
126 | 57 |
| 58 void FakeVideoCaptureDevice::PopulateVariableFormatsRoster( |
| 59 const VideoCaptureFormats& formats) { |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); |
| 61 DCHECK(!capture_thread_.IsRunning()); |
| 62 format_roster_ = formats; |
| 63 format_roster_index_ = 0; |
| 64 } |
| 65 |
127 void FakeVideoCaptureDevice::OnAllocateAndStart( | 66 void FakeVideoCaptureDevice::OnAllocateAndStart( |
128 const VideoCaptureParams& params, | 67 const VideoCaptureParams& params, |
129 scoped_ptr<VideoCaptureDevice::Client> client) { | 68 scoped_ptr<VideoCaptureDevice::Client> client) { |
130 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); | 69 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); |
131 client_ = client.Pass(); | 70 client_ = client.Pass(); |
132 | 71 |
133 // Incoming |params| can be none of the supported formats, so we get the | 72 // Incoming |params| can be none of the supported formats, so we get the |
134 // closest thing rounded up. TODO(mcasas): Use the |params|, if they belong to | 73 // closest thing rounded up. TODO(mcasas): Use the |params|, if they belong to |
135 // the supported ones, when http://crbug.com/309554 is verified. | 74 // the supported ones, when http://crbug.com/309554 is verified. |
136 DCHECK_EQ(params.requested_format.pixel_format, PIXEL_FORMAT_I420); | 75 DCHECK_EQ(params.requested_format.pixel_format, PIXEL_FORMAT_I420); |
137 capture_format_.pixel_format = params.requested_format.pixel_format; | 76 capture_format_.pixel_format = params.requested_format.pixel_format; |
138 capture_format_.frame_rate = 30; | 77 capture_format_.frame_rate = 30; |
139 if (params.requested_format.frame_size.width() > 640) | 78 if (params.requested_format.frame_size.width() > 640) |
140 capture_format_.frame_size.SetSize(1280, 720); | 79 capture_format_.frame_size.SetSize(1280, 720); |
141 else if (params.requested_format.frame_size.width() > 320) | 80 else if (params.requested_format.frame_size.width() > 320) |
142 capture_format_.frame_size.SetSize(640, 480); | 81 capture_format_.frame_size.SetSize(640, 480); |
143 else | 82 else |
144 capture_format_.frame_size.SetSize(320, 240); | 83 capture_format_.frame_size.SetSize(320, 240); |
145 if (params.allow_resolution_change) | |
146 PopulateFormatRoster(); | |
147 const size_t fake_frame_size = | 84 const size_t fake_frame_size = |
148 VideoFrame::AllocationSize(VideoFrame::I420, capture_format_.frame_size); | 85 VideoFrame::AllocationSize(VideoFrame::I420, capture_format_.frame_size); |
149 fake_frame_.reset(new uint8[fake_frame_size]); | 86 fake_frame_.reset(new uint8[fake_frame_size]); |
150 | 87 |
151 capture_thread_.message_loop()->PostTask( | 88 capture_thread_.message_loop()->PostTask( |
152 FROM_HERE, | 89 FROM_HERE, |
153 base::Bind(&FakeVideoCaptureDevice::OnCaptureTask, | 90 base::Bind(&FakeVideoCaptureDevice::OnCaptureTask, |
154 base::Unretained(this))); | 91 base::Unretained(this))); |
155 } | 92 } |
156 | 93 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 format_roster_.at(++format_roster_index_ % format_roster_.size()); | 180 format_roster_.at(++format_roster_index_ % format_roster_.size()); |
244 DCHECK_EQ(capture_format_.pixel_format, PIXEL_FORMAT_I420); | 181 DCHECK_EQ(capture_format_.pixel_format, PIXEL_FORMAT_I420); |
245 DVLOG(3) << "Reallocating FakeVideoCaptureDevice, new capture resolution " | 182 DVLOG(3) << "Reallocating FakeVideoCaptureDevice, new capture resolution " |
246 << capture_format_.frame_size.ToString(); | 183 << capture_format_.frame_size.ToString(); |
247 | 184 |
248 const size_t fake_frame_size = | 185 const size_t fake_frame_size = |
249 VideoFrame::AllocationSize(VideoFrame::I420, capture_format_.frame_size); | 186 VideoFrame::AllocationSize(VideoFrame::I420, capture_format_.frame_size); |
250 fake_frame_.reset(new uint8[fake_frame_size]); | 187 fake_frame_.reset(new uint8[fake_frame_size]); |
251 } | 188 } |
252 | 189 |
253 void FakeVideoCaptureDevice::PopulateFormatRoster() { | |
254 DCHECK_EQ(capture_thread_.message_loop(), base::MessageLoop::current()); | |
255 GetDeviceSupportedFormats(Name(), &format_roster_); | |
256 format_roster_index_ = 0; | |
257 } | |
258 | |
259 } // namespace media | 190 } // namespace media |
OLD | NEW |