summaryrefslogtreecommitdiff
path: root/chromium/media/capture/video/linux/fake_v4l2_impl.h
blob: 0a035a97dd8761b08eb4cfdbe2865efc346f0e23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_CAPTURE_VIDEO_LINUX_FAKE_V4L2_IMPL_H_
#define MEDIA_CAPTURE_VIDEO_LINUX_FAKE_V4L2_IMPL_H_

#include <map>
#include <string>

#include <linux/videodev2.h>

#include "media/capture/capture_export.h"
#include "media/capture/video/linux/v4l2_capture_device.h"
#include "media/capture/video/video_capture_device_descriptor.h"

namespace media {

struct FakeV4L2DeviceConfig {
  FakeV4L2DeviceConfig(const VideoCaptureDeviceDescriptor& descriptor)
      : descriptor(descriptor) {}

  const VideoCaptureDeviceDescriptor descriptor;
};

// Implementation of V4L2CaptureDevice interface that allows configuring fake
// devices useful for testing.
class CAPTURE_EXPORT FakeV4L2Impl : public V4L2CaptureDevice {
 public:
  FakeV4L2Impl();

  void AddDevice(const std::string& device_name,
                 const FakeV4L2DeviceConfig& config);

  // Implementation of V4L2CaptureDevice interface:
  int open(const char* device_name, int flags) override;
  int close(int fd) override;
  int ioctl(int fd, int request, void* argp) override;
  void* mmap(void* start,
             size_t length,
             int prot,
             int flags,
             int fd,
             off_t offset) override;

  int munmap(void* start, size_t length) override;
  int poll(struct pollfd* ufds, unsigned int nfds, int timeout) override;

 protected:
  ~FakeV4L2Impl() override;

 private:
  class OpenedDevice;

  int next_id_to_return_from_open_;
  std::map<std::string, FakeV4L2DeviceConfig> device_configs_;
  std::map<std::string, int> device_name_to_open_id_map_;
  std::map<int /*value returned by open()*/, std::unique_ptr<OpenedDevice>>
      opened_devices_;
};

}  // namespace media

#endif  // MEDIA_CAPTURE_VIDEO_LINUX_FAKE_V4L2_IMPL_H_