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 // Implementation of a fake VideoCaptureDevice class. Used for testing other | 5 // Implementation of a fake VideoCaptureDevice class. Used for testing other |
6 // video capture classes when no real hardware is available. | 6 // video capture classes when no real hardware is available. |
7 | 7 |
8 #ifndef MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_ | 8 #ifndef MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_ |
9 #define MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_ | 9 #define MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_ |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "media/video/capture/video_capture_device.h" | 17 #include "media/video/capture/video_capture_device.h" |
18 | 18 |
19 namespace media { | 19 namespace media { |
20 | 20 |
21 class MEDIA_EXPORT FakeVideoCaptureDevice : public VideoCaptureDevice { | 21 class MEDIA_EXPORT FakeVideoCaptureDevice : public VideoCaptureDevice { |
22 public: | 22 public: |
23 static VideoCaptureDevice* Create(const Name& device_name); | 23 static const int kFakeCaptureTimeoutMs = 50; |
| 24 |
| 25 FakeVideoCaptureDevice(); |
24 virtual ~FakeVideoCaptureDevice(); | 26 virtual ~FakeVideoCaptureDevice(); |
25 // Used for testing. This will make sure the next call to Create will | |
26 // return NULL; | |
27 static void SetFailNextCreate(); | |
28 static void SetNumberOfFakeDevices(size_t number_of_devices); | |
29 static size_t NumberOfFakeDevices(); | |
30 | |
31 static void GetDeviceNames(Names* device_names); | |
32 static void GetDeviceSupportedFormats(const Name& device, | |
33 VideoCaptureFormats* supported_formats); | |
34 | 27 |
35 // VideoCaptureDevice implementation. | 28 // VideoCaptureDevice implementation. |
36 virtual void AllocateAndStart(const VideoCaptureParams& params, | 29 virtual void AllocateAndStart( |
37 scoped_ptr<VideoCaptureDevice::Client> client) | 30 const VideoCaptureParams& params, |
38 OVERRIDE; | 31 scoped_ptr<VideoCaptureDevice::Client> client) OVERRIDE; |
39 virtual void StopAndDeAllocate() OVERRIDE; | 32 virtual void StopAndDeAllocate() OVERRIDE; |
40 | 33 |
41 private: | 34 private: |
42 FakeVideoCaptureDevice(); | |
43 | |
44 // Called on the |capture_thread_| only. | 35 // Called on the |capture_thread_| only. |
45 void OnAllocateAndStart(const VideoCaptureParams& params, | 36 void OnAllocateAndStart(const VideoCaptureParams& params, |
46 scoped_ptr<Client> client); | 37 scoped_ptr<Client> client); |
47 void OnStopAndDeAllocate(); | 38 void OnStopAndDeAllocate(); |
48 void OnCaptureTask(); | 39 void OnCaptureTask(); |
49 void Reallocate(); | 40 void Reallocate(); |
50 void PopulateFormatRoster(); | 41 void PopulateFormatRoster(); |
51 | 42 |
52 // |thread_checker_| is used to check that destructor, AllocateAndStart() and | 43 // |thread_checker_| is used to check that destructor, AllocateAndStart() and |
53 // StopAndDeAllocate() are called in the correct thread that owns the object. | 44 // StopAndDeAllocate() are called in the correct thread that owns the object. |
54 base::ThreadChecker thread_checker_; | 45 base::ThreadChecker thread_checker_; |
55 | 46 |
56 base::Thread capture_thread_; | 47 base::Thread capture_thread_; |
57 // The following members are only used on the |capture_thread_|. | 48 // The following members are only used on the |capture_thread_|. |
58 scoped_ptr<VideoCaptureDevice::Client> client_; | 49 scoped_ptr<VideoCaptureDevice::Client> client_; |
59 scoped_ptr<uint8[]> fake_frame_; | 50 scoped_ptr<uint8[]> fake_frame_; |
60 int frame_count_; | 51 int frame_count_; |
61 VideoCaptureFormat capture_format_; | 52 VideoCaptureFormat capture_format_; |
62 | 53 |
63 // When the device is allowed to change resolution, this vector holds the | 54 // When the device is allowed to change resolution, this vector holds the |
64 // available ones which are used in sequence, restarting at the end. These | 55 // available ones which are used in sequence, restarting at the end. These |
65 // two members belong to and are only used in |capture_thread_|. | 56 // two members belong to and are only used in |capture_thread_|. |
66 std::vector<VideoCaptureFormat> format_roster_; | 57 std::vector<VideoCaptureFormat> format_roster_; |
67 int format_roster_index_; | 58 int format_roster_index_; |
68 | 59 |
69 static bool fail_next_create_; | |
70 // |number_of_devices_| is atomic since tests can call SetNumberOfFakeDevices | |
71 // on the IO thread to set |number_of_devices_|. The variable can be | |
72 // read from a separate thread. | |
73 // TODO(perkj): Make tests independent of global state. crbug/323913 | |
74 static base::subtle::Atomic32 number_of_devices_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureDevice); | 60 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureDevice); |
77 }; | 61 }; |
78 | 62 |
79 } // namespace media | 63 } // namespace media |
80 | 64 |
81 #endif // MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_ | 65 #endif // MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_ |
OLD | NEW |