Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / media / video / capture / fake_video_capture_device.h
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.
4
5 // Implementation of a fake VideoCaptureDevice class. Used for testing other
6 // video capture classes when no real hardware is available.
7
8 #ifndef MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_
9 #define MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_
10
11 #include <string>
12
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"
18
19 namespace media {
20
21 class MEDIA_EXPORT FakeVideoCaptureDevice : public VideoCaptureDevice {
22  public:
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
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
35   // VideoCaptureDevice implementation.
36   virtual void AllocateAndStart(const VideoCaptureParams& params,
37                                 scoped_ptr<VideoCaptureDevice::Client> client)
38       OVERRIDE;
39   virtual void StopAndDeAllocate() OVERRIDE;
40
41  private:
42   FakeVideoCaptureDevice();
43
44   // Called on the |capture_thread_| only.
45   void OnAllocateAndStart(const VideoCaptureParams& params,
46                           scoped_ptr<Client> client);
47   void OnStopAndDeAllocate();
48   void OnCaptureTask();
49   void Reallocate();
50   void PopulateFormatRoster();
51
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_;
55
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_;
60   int frame_count_;
61   VideoCaptureFormat capture_format_;
62
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_;
68
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);
77 };
78
79 }  // namespace media
80
81 #endif  // MEDIA_VIDEO_CAPTURE_FAKE_VIDEO_CAPTURE_DEVICE_H_