summaryrefslogtreecommitdiffstats
path: root/media/video/capture/fake_video_capture_device_factory.cc
blob: 76de761c8fc71210efbbc126cfe459b28971085d (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2014 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.

#include "media/video/capture/fake_video_capture_device_factory.h"

#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "media/base/media_switches.h"
#include "media/video/capture/fake_video_capture_device.h"

namespace media {

FakeVideoCaptureDeviceFactory::FakeVideoCaptureDeviceFactory()
    : number_of_devices_(1) {
}

scoped_ptr<VideoCaptureDevice> FakeVideoCaptureDeviceFactory::Create(
    const VideoCaptureDevice::Name& device_name) {
  DCHECK(thread_checker_.CalledOnValidThread());

  const std::string option = base::CommandLine::ForCurrentProcess()->
     GetSwitchValueASCII(switches::kUseFakeDeviceForMediaStream);

  FakeVideoCaptureDevice::FakeVideoCaptureDeviceType fake_vcd_type;
  if (option.empty())
    fake_vcd_type = FakeVideoCaptureDevice::USING_OWN_BUFFERS;
  else if (base:: strcasecmp(option.c_str(), "triplanar") == 0)
    fake_vcd_type = FakeVideoCaptureDevice::USING_OWN_BUFFERS_TRIPLANAR;
  else if (base:: strcasecmp(option.c_str(), "gpu") == 0)
    fake_vcd_type = FakeVideoCaptureDevice::USING_CLIENT_BUFFERS_GPU;
  else
    fake_vcd_type = FakeVideoCaptureDevice::USING_CLIENT_BUFFERS_I420;

  for (int n = 0; n < number_of_devices_; ++n) {
    std::string possible_id = base::StringPrintf("/dev/video%d", n);
    if (device_name.id().compare(possible_id) == 0) {
      return scoped_ptr<VideoCaptureDevice>(
          new FakeVideoCaptureDevice(fake_vcd_type));
    }
  }
  return scoped_ptr<VideoCaptureDevice>();
}

void FakeVideoCaptureDeviceFactory::GetDeviceNames(
    VideoCaptureDevice::Names* const device_names) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK(device_names->empty());
  for (int n = 0; n < number_of_devices_; ++n) {
    VideoCaptureDevice::Name name(base::StringPrintf("fake_device_%d", n),
                                  base::StringPrintf("/dev/video%d", n)
#if defined(OS_LINUX)
                                  , VideoCaptureDevice::Name::V4L2_SINGLE_PLANE
#elif defined(OS_MACOSX)
                                  , VideoCaptureDevice::Name::AVFOUNDATION
#elif defined(OS_WIN)
                                  , VideoCaptureDevice::Name::DIRECT_SHOW
#elif defined(OS_ANDROID)
                                  , VideoCaptureDevice::Name::API2_LEGACY
#endif
    );
    device_names->push_back(name);
  }
}

void FakeVideoCaptureDeviceFactory::GetDeviceSupportedFormats(
    const VideoCaptureDevice::Name& device,
    VideoCaptureFormats* supported_formats) {
  DCHECK(thread_checker_.CalledOnValidThread());
  const int frame_rate = 1000 / FakeVideoCaptureDevice::FakeCapturePeriodMs();
  const gfx::Size supported_sizes[] = {gfx::Size(320, 240),
                                       gfx::Size(640, 480),
                                       gfx::Size(1280, 720),
                                       gfx::Size(1920, 1080)};
  supported_formats->clear();
  for (const auto& size : supported_sizes) {
    supported_formats->push_back(
        VideoCaptureFormat(size, frame_rate, media::PIXEL_FORMAT_I420));
  }
}

}  // namespace media