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
3 // found in the LICENSE file.
5 // Implementation of a fake VideoCaptureDevice class. Used for testing other
6 // video capture classes when no real hardware is available.
8 #ifndef MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_
9 #define MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_
13 #include "base/atomicops.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_checker.h"
17 #include "media/video/capture/video_capture_device.h"
21 class MEDIA_EXPORT FakeVideoCaptureDevice : public VideoCaptureDevice {
23 static VideoCaptureDevice* Create(const Name& device_name);
24 virtual ~FakeVideoCaptureDevice();
25 // Used for testing. This will make sure the next call to Create will
27 static void SetFailNextCreate();
28 static void SetNumberOfFakeDevices(size_t number_of_devices);
29 static size_t NumberOfFakeDevices();
31 static void GetDeviceNames(Names* device_names);
32 static void GetDeviceSupportedFormats(const Name& device,
33 VideoCaptureFormats* supported_formats);
35 // VideoCaptureDevice implementation.
36 virtual void AllocateAndStart(const VideoCaptureParams& params,
37 scoped_ptr<VideoCaptureDevice::Client> client)
39 virtual void StopAndDeAllocate() OVERRIDE;
42 FakeVideoCaptureDevice();
44 // Called on the |capture_thread_| only.
45 void OnAllocateAndStart(const VideoCaptureParams& params,
46 scoped_ptr<Client> client);
47 void OnStopAndDeAllocate();
50 void PopulateFormatRoster();
52 // |thread_checker_| is used to check that destructor, AllocateAndStart() and
53 // StopAndDeAllocate() are called in the correct thread that owns the object.
54 base::ThreadChecker thread_checker_;
56 base::Thread capture_thread_;
57 // The following members are only used on the |capture_thread_|.
58 scoped_ptr<VideoCaptureDevice::Client> client_;
59 scoped_ptr<uint8[]> fake_frame_;
61 VideoCaptureFormat capture_format_;
63 // 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
65 // two members belong to and are only used in |capture_thread_|.
66 std::vector<VideoCaptureFormat> format_roster_;
67 int format_roster_index_;
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_;
76 DISALLOW_COPY_AND_ASSIGN(FakeVideoCaptureDevice);
81 #endif // MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_