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 |
| 34 // Sets the formats to use sequentially when the device is configured as |
| 35 // variable capture resolution. Works only before AllocateAndStart() or |
| 36 // after StopAndDeallocate(). |
| 37 void PopulateVariableFormatsRoster(const VideoCaptureFormats& formats); |
| 38 |
41 private: | 39 private: |
42 FakeVideoCaptureDevice(); | |
43 | |
44 // Called on the |capture_thread_| only. | 40 // Called on the |capture_thread_| only. |
45 void OnAllocateAndStart(const VideoCaptureParams& params, | 41 void OnAllocateAndStart(const VideoCaptureParams& params, |
46 scoped_ptr<Client> client); | 42 scoped_ptr<Client> client); |
47 void OnStopAndDeAllocate(); | 43 void OnStopAndDeAllocate(); |
48 void OnCaptureTask(); | 44 void OnCaptureTask(); |
49 void Reallocate(); | 45 void Reallocate(); |
50 void PopulateFormatRoster(); | |
51 | 46 |
52 // |thread_checker_| is used to check that destructor, AllocateAndStart() and | 47 // |thread_checker_| is used to check that destructor, AllocateAndStart() and |
53 // StopAndDeAllocate() are called in the correct thread that owns the object. | 48 // StopAndDeAllocate() are called in the correct thread that owns the object. |
54 base::ThreadChecker thread_checker_; | 49 base::ThreadChecker thread_checker_; |
55 | 50 |
56 base::Thread capture_thread_; | 51 base::Thread capture_thread_; |
57 // The following members are only used on the |capture_thread_|. | 52 // The following members are only used on the |capture_thread_|. |
58 scoped_ptr<VideoCaptureDevice::Client> client_; | 53 scoped_ptr<VideoCaptureDevice::Client> client_; |
59 scoped_ptr<uint8[]> fake_frame_; | 54 scoped_ptr<uint8[]> fake_frame_; |
60 int frame_count_; | 55 int frame_count_; |
61 VideoCaptureFormat capture_format_; | 56 VideoCaptureFormat capture_format_; |
62 | 57 |
63 // When the device is allowed to change resolution, this vector holds the | 58 // 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 | 59 // available ones, used sequentially restarting at the end. These two members |
65 // two members belong to and are only used in |capture_thread_|. | 60 // are initialised in PopulateFormatRoster() before |capture_thread_| is |
| 61 // running and are subsequently read-only in that thread. |
66 std::vector<VideoCaptureFormat> format_roster_; | 62 std::vector<VideoCaptureFormat> format_roster_; |
67 int format_roster_index_; | 63 int format_roster_index_; |
68 | 64 |
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); | 65 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureDevice); |
77 }; | 66 }; |
78 | 67 |
79 } // namespace media | 68 } // namespace media |
80 | 69 |
81 #endif // MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_ | 70 #endif // MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_ |
OLD | NEW |