~oxide-developers/oxide/oxide.trunk

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// vim:expandtab:shiftwidth=2:tabstop=2:
// Copyright (C) 2015-2016 Canonical Ltd.

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
// USA

#include "oxide_video_capture_device_factory_linux.h"

#include <libintl.h>
#include <utility>

#include "base/logging.h"
#include "media/capture/video/video_capture_device.h"

#if defined(ENABLE_HYBRIS_CAMERA)
#include <algorithm>
#include <hybris/camera/camera_compatibility_layer.h>
#include <hybris/camera/camera_compatibility_layer_capabilities.h>

#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/ptr_util.h"
#include "base/strings/stringprintf.h"

#include "shared/browser/oxide_hybris_utils.h"

#include "oxide_video_capture_device_hybris.h"
#endif

namespace oxide {

namespace {

#if defined(ENABLE_HYBRIS_CAMERA)

const char* GetDeviceNameFromCameraType(CameraType type) {
  switch (type) {
    case BACK_FACING_CAMERA_TYPE:
      return dgettext(OXIDE_GETTEXT_DOMAIN, "Rear camera");
    case FRONT_FACING_CAMERA_TYPE:
      return dgettext(OXIDE_GETTEXT_DOMAIN, "Front camera");
  }

  NOTREACHED();
  return "";
}

std::unique_ptr<media::VideoCaptureDevice::Names> GetDeviceNamesFromHybris() {
  DCHECK(HybrisUtils::IsCameraCompatAvailable());

  int32_t number_of_devices = android_camera_get_number_of_devices();

  std::unique_ptr<media::VideoCaptureDevice::Names> names(
      new media::VideoCaptureDevice::Names());

  for (int32_t camera_id = 0; camera_id < number_of_devices; ++camera_id) {
    CameraType type;
    int orientation;
    if (android_camera_get_device_info(camera_id,
                                       reinterpret_cast<int*>(&type),
                                       &orientation) != 0) {
      LOG(ERROR) <<
          "Failed to get device info for camera with ID " << camera_id;
      continue;
    }

    std::string device_id =
        base::StringPrintf("%s%d",
                           VideoCaptureDeviceHybris::GetDeviceIdPrefix(),
                           camera_id);

    names->push_back(
        media::VideoCaptureDevice::Name(
          // XXX: We should append an integer to this when there are multiple
          // cameras facing the same direction
          GetDeviceNameFromCameraType(type),
          device_id,
          media::VideoCaptureDevice::Name::API_TYPE_UNKNOWN));
  }

  return names;
}

bool IsDeviceNameIn(const media::VideoCaptureDevice::Name& name,
                    media::VideoCaptureDevice::Names* names) {
  return std::find_if(
      names->begin(),
      names->end(),
      [name](const media::VideoCaptureDevice::Name& i) {
        return name.id() == i.id();
      }) != names->end();
}

#endif

}

std::unique_ptr<media::VideoCaptureDevice>
VideoCaptureDeviceFactoryLinux::Create(
    const media::VideoCaptureDevice::Name& device_name) {
#if defined(ENABLE_HYBRIS_CAMERA)
  if (!HybrisUtils::IsCameraCompatAvailable()) {
    return delegate_->Create(device_name);
  }

  std::unique_ptr<media::VideoCaptureDevice::Names> names =
      GetDeviceNamesFromHybris();
  if (!names) {
    return nullptr;
  }

  if (!IsDeviceNameIn(device_name, names.get())) {
    return nullptr;
  }

  return base::WrapUnique(new VideoCaptureDeviceHybris(device_name));
#else
  return delegate_->Create(device_name);
#endif
}

void VideoCaptureDeviceFactoryLinux::EnumerateDeviceNames(
    const EnumerateDevicesCallback& callback) {
#if defined(ENABLE_HYBRIS_CAMERA)
  if (HybrisUtils::IsCameraCompatAvailable()) {
    std::unique_ptr<media::VideoCaptureDevice::Names> names =
        GetDeviceNamesFromHybris();
    callback.Run(std::move(names));
  } else
#endif
  {
    delegate_->EnumerateDeviceNames(callback);
  }
}

void VideoCaptureDeviceFactoryLinux::GetDeviceSupportedFormats(
    const media::VideoCaptureDevice::Name& device,
    media::VideoCaptureFormats* supported_formats) {
#if defined(ENABLE_HYBRIS_CAMERA)
  if (HybrisUtils::IsCameraCompatAvailable()) {
    return;
  }
#endif

  delegate_->GetDeviceSupportedFormats(device, supported_formats);
}

void VideoCaptureDeviceFactoryLinux::GetDeviceNames(
    media::VideoCaptureDevice::Names* device_names) {
  NOTREACHED();
}

VideoCaptureDeviceFactoryLinux::VideoCaptureDeviceFactoryLinux(
    std::unique_ptr<media::VideoCaptureDeviceFactory> delegate)
    : delegate_(std::move(delegate)) {}

VideoCaptureDeviceFactoryLinux::~VideoCaptureDeviceFactoryLinux() {}

} // namespace oxide