Chromium Code Reviews| Index: media/capture/video/chromeos/video_capture_device_arc_chromeos.cc |
| diff --git a/media/capture/video/chromeos/video_capture_device_arc_chromeos.cc b/media/capture/video/chromeos/video_capture_device_arc_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..229adfe4a13a6ebc668c95df61c22f6dbc8c1f59 |
| --- /dev/null |
| +++ b/media/capture/video/chromeos/video_capture_device_arc_chromeos.cc |
| @@ -0,0 +1,175 @@ |
| +// Copyright 2017 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/capture/video/chromeos/video_capture_device_arc_chromeos.h" |
| + |
| +#include "base/threading/platform_thread.h" |
| +#include "media/capture/video/chromeos/camera_device_delegate.h" |
| +#include "media/capture/video/chromeos/camera_hal_delegate.h" |
| +#include "ui/display/display.h" |
| +#include "ui/display/display_observer.h" |
| +#include "ui/display/screen.h" |
| + |
| +namespace media { |
| + |
| +VideoCaptureDeviceArcChromeOS::VideoCaptureDeviceArcChromeOS( |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_screen_observer, |
| + const VideoCaptureDeviceDescriptor& device_descriptor, |
| + scoped_refptr<CameraHalDelegate> camera_hal_delegate) |
| + : device_descriptor_(device_descriptor), |
| + camera_hal_delegate_(std::move(camera_hal_delegate)), |
| + capture_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| + camera_device_ipc_thread_(std::string("CameraDeviceIpcThread") + |
| + device_descriptor.device_id), |
| + screen_observer_delegate_(new ScreenObserverDelegate( |
| + this, |
| + std::move(task_runner_for_screen_observer))), |
| + lens_facing_(device_descriptor.facing), |
| + camera_orientation_(0), |
| + // External cameras have lens_facing as MEDIA_VIDEO_FACING_NONE. |
| + // We don't want to rotate the frame even if the device rotates. |
| + rotates_with_device_(lens_facing_ != |
| + VideoFacingMode::MEDIA_VIDEO_FACING_NONE), |
| + rotation_(0) {} |
| + |
| +VideoCaptureDeviceArcChromeOS::~VideoCaptureDeviceArcChromeOS() { |
| + DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| + DCHECK(!camera_device_ipc_thread_.IsRunning()); |
| + screen_observer_delegate_->RemoveObserver(); |
| +} |
| + |
| +// VideoCaptureDevice implementation. |
| +void VideoCaptureDeviceArcChromeOS::AllocateAndStart( |
| + const VideoCaptureParams& params, |
| + std::unique_ptr<Client> client) { |
| + DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| + DCHECK(!camera_device_delegate_); |
| + |
| + if (!camera_device_ipc_thread_.Start()) { |
| + std::string error_msg = "Failed to start device thread"; |
| + LOG(ERROR) << error_msg; |
| + client->OnError(FROM_HERE, error_msg); |
| + return; |
| + } |
| + camera_device_delegate_ = |
| + new CameraDeviceDelegate(device_descriptor_, camera_hal_delegate_, |
| + camera_device_ipc_thread_.task_runner()); |
| + camera_device_ipc_thread_.task_runner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CameraDeviceDelegate::AllocateAndStart, |
| + camera_device_delegate_, params, base::Passed(&client))); |
| + camera_device_ipc_thread_.task_runner()->PostTask( |
| + FROM_HERE, base::Bind(&CameraDeviceDelegate::SetRotation, |
| + camera_device_delegate_, rotation_)); |
| +} |
| + |
| +void VideoCaptureDeviceArcChromeOS::StopAndDeAllocate() { |
| + DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| + |
| + if (!camera_device_delegate_) { |
| + return; |
| + } |
| + camera_device_ipc_thread_.task_runner()->PostTask( |
| + FROM_HERE, base::Bind(&CameraDeviceDelegate::StopAndDeAllocate, |
| + camera_device_delegate_)); |
| + // Wait until all other references to |camera_device_delegate_| are dropped to |
|
Pawel Osciak
2017/06/13 08:40:16
Is it required for HAL IPC to finish all IPCs, or
jcliang
2017/06/14 04:46:07
In theory we can just kill the Mojo channel here a
|
| + // make sure all the IPC calls are done. This is best-effort as the camera |
| + // HAL may never return, say, due to a deadlock, in which case the loop simply |
| + // times out and we destroy everything. |
| + const int kWaitTimeoutSecs = 3; |
| + base::TimeTicks wait_until = |
| + base::TimeTicks() + base::TimeDelta::FromSeconds(kWaitTimeoutSecs); |
| + while (!camera_device_delegate_->HasOneRef()) { |
| + if (base::TimeTicks() >= wait_until) { |
| + LOG(ERROR) << "Timed out waiting capture device to stop"; |
| + break; |
| + } |
| + base::PlatformThread::YieldCurrentThread(); |
| + } |
| + camera_device_ipc_thread_.Stop(); |
| + camera_device_delegate_ = nullptr; |
| +} |
| + |
| +void VideoCaptureDeviceArcChromeOS::TakePhoto(TakePhotoCallback callback) { |
| + DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| + DCHECK(camera_device_delegate_); |
| + camera_device_ipc_thread_.task_runner()->PostTask( |
| + FROM_HERE, base::Bind(&CameraDeviceDelegate::TakePhoto, |
| + camera_device_delegate_, base::Passed(&callback))); |
| +} |
| + |
| +void VideoCaptureDeviceArcChromeOS::GetPhotoCapabilities( |
| + GetPhotoCapabilitiesCallback callback) { |
| + DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| + camera_device_ipc_thread_.task_runner()->PostTask( |
| + FROM_HERE, base::Bind(&CameraDeviceDelegate::GetPhotoCapabilities, |
| + camera_device_delegate_, base::Passed(&callback))); |
| +} |
| + |
| +void VideoCaptureDeviceArcChromeOS::SetPhotoOptions( |
| + mojom::PhotoSettingsPtr settings, |
| + SetPhotoOptionsCallback callback) { |
| + DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| + camera_device_ipc_thread_.task_runner()->PostTask( |
| + FROM_HERE, base::Bind(&CameraDeviceDelegate::SetPhotoOptions, |
| + camera_device_delegate_, base::Passed(&settings), |
| + base::Passed(&callback))); |
| +} |
| + |
| +void VideoCaptureDeviceArcChromeOS::SetDisplayRotation( |
| + const display::Display& display) { |
| + DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| + if (display.IsInternal()) |
| + SetRotation(display.rotation() * 90); |
| +} |
| + |
| +void VideoCaptureDeviceArcChromeOS::SetRotation(int rotation) { |
| + DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| + if (!rotates_with_device_) { |
| + rotation = 0; |
| + } else if (lens_facing_ == VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT) { |
| + // Original frame when |rotation| = 0 |
| + // ----------------------- |
| + // | * | |
| + // | * * | |
| + // | * * | |
| + // | ******* | |
| + // | * * | |
| + // | * * | |
| + // ----------------------- |
| + // |
| + // |rotation| = 90, this is what back camera sees |
| + // ----------------------- |
| + // | ******** | |
| + // | * **** | |
| + // | * *** | |
| + // | * *** | |
| + // | * **** | |
| + // | ******** | |
| + // ----------------------- |
| + // |
| + // |rotation| = 90, this is what front camera sees |
| + // ----------------------- |
| + // | ******** | |
| + // | **** * | |
| + // | *** * | |
| + // | *** * | |
| + // | **** * | |
| + // | ******** | |
| + // ----------------------- |
| + // |
| + // Therefore, for back camera, we need to rotate (360 - |rotation|). |
| + rotation = (360 - rotation) % 360; |
| + } |
| + // Take into account camera orientation w.r.t. the display. External cameras |
| + // would have camera_orientation_ as 0. |
| + rotation_ = (rotation + camera_orientation_) % 360; |
| + if (camera_device_ipc_thread_.IsRunning()) { |
| + camera_device_ipc_thread_.task_runner()->PostTask( |
| + FROM_HERE, base::Bind(&CameraDeviceDelegate::SetRotation, |
| + camera_device_delegate_, rotation_)); |
| + } |
| +} |
| + |
| +} // namespace media |