blob: 14d84dc0ac73de589df99084d8a03d7af8e4d64a [file] [log] [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/video_capture/ash/video_capture_device_factory_ash.h"
#include <memory>
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "services/video_capture/ash/video_capture_device_ash.h"
#include "services/video_capture/device_factory.h"
namespace crosapi {
VideoCaptureDeviceFactoryAsh::VideoCaptureDeviceFactoryAsh(
raw_ptr<video_capture::DeviceFactory> device_factory)
: device_factory_(device_factory) {}
VideoCaptureDeviceFactoryAsh::~VideoCaptureDeviceFactoryAsh() = default;
void VideoCaptureDeviceFactoryAsh::GetDeviceInfos(
GetDeviceInfosCallback callback) {
device_factory_->GetDeviceInfos(std::move(callback));
}
void VideoCaptureDeviceFactoryAsh::CreateDevice(
const std::string& device_id,
mojo::PendingReceiver<crosapi::mojom::VideoCaptureDevice> device_receiver,
CreateDeviceCallback callback) {
mojo::PendingRemote<video_capture::mojom::Device> proxy_remote;
auto proxy_receiver = proxy_remote.InitWithNewPipeAndPassReceiver();
auto device_proxy = std::make_unique<VideoCaptureDeviceAsh>(
std::move(device_receiver), std::move(proxy_remote),
base::BindOnce(
&VideoCaptureDeviceFactoryAsh::OnClientConnectionErrorOrClose,
base::Unretained(this), device_id));
auto wrapped_callback = base::BindOnce(
[](CreateDeviceCallback callback, media::VideoCaptureError error_code) {
crosapi::mojom::DeviceAccessResultCode crosapi_result_code;
switch (error_code) {
case media::VideoCaptureError::
kCrosHalV3DeviceDelegateFailedToInitializeCameraDevice:
crosapi_result_code =
crosapi::mojom::DeviceAccessResultCode::NOT_INITIALIZED;
break;
case media::VideoCaptureError::kNone:
crosapi_result_code =
crosapi::mojom::DeviceAccessResultCode::SUCCESS;
break;
default:
crosapi_result_code =
crosapi::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND;
}
std::move(callback).Run(crosapi_result_code);
},
std::move(callback));
devices_.emplace(device_id, std::move(device_proxy));
device_factory_->CreateDevice(device_id, std::move(proxy_receiver),
std::move(wrapped_callback));
}
void VideoCaptureDeviceFactoryAsh::OnClientConnectionErrorOrClose(
const std::string& device_id) {
devices_.erase(device_id);
}
} // namespace crosapi