| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/capture/video/chromeos/video_capture_device_arc_chromeos.h" |
| 6 |
| 7 #include <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/threading/platform_thread.h" |
| 14 #include "media/capture/video/chromeos/camera_device_delegate.h" |
| 15 #include "media/capture/video/chromeos/camera_hal_delegate.h" |
| 16 #include "ui/display/display.h" |
| 17 #include "ui/display/display_observer.h" |
| 18 #include "ui/display/screen.h" |
| 19 |
| 20 namespace media { |
| 21 |
| 22 VideoCaptureDeviceArcChromeOS::VideoCaptureDeviceArcChromeOS( |
| 23 scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_screen_observer, |
| 24 const VideoCaptureDeviceDescriptor& device_descriptor, |
| 25 scoped_refptr<CameraHalDelegate> camera_hal_delegate) |
| 26 : device_descriptor_(device_descriptor), |
| 27 camera_hal_delegate_(std::move(camera_hal_delegate)), |
| 28 capture_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 29 camera_device_ipc_thread_(std::string("CameraDeviceIpcThread") + |
| 30 device_descriptor.device_id), |
| 31 screen_observer_delegate_(new ScreenObserverDelegate( |
| 32 this, |
| 33 std::move(task_runner_for_screen_observer))), |
| 34 lens_facing_(device_descriptor.facing), |
| 35 camera_orientation_(0), |
| 36 // External cameras have lens_facing as MEDIA_VIDEO_FACING_NONE. |
| 37 // We don't want to rotate the frame even if the device rotates. |
| 38 rotates_with_device_(lens_facing_ != |
| 39 VideoFacingMode::MEDIA_VIDEO_FACING_NONE), |
| 40 rotation_(0) {} |
| 41 |
| 42 VideoCaptureDeviceArcChromeOS::~VideoCaptureDeviceArcChromeOS() { |
| 43 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 44 DCHECK(!camera_device_ipc_thread_.IsRunning()); |
| 45 screen_observer_delegate_->RemoveObserver(); |
| 46 } |
| 47 |
| 48 // VideoCaptureDevice implementation. |
| 49 void VideoCaptureDeviceArcChromeOS::AllocateAndStart( |
| 50 const VideoCaptureParams& params, |
| 51 std::unique_ptr<Client> client) { |
| 52 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 53 DCHECK(!camera_device_delegate_); |
| 54 |
| 55 if (!camera_device_ipc_thread_.Start()) { |
| 56 std::string error_msg = "Failed to start device thread"; |
| 57 LOG(ERROR) << error_msg; |
| 58 client->OnError(FROM_HERE, error_msg); |
| 59 return; |
| 60 } |
| 61 camera_device_delegate_ = base::MakeUnique<CameraDeviceDelegate>( |
| 62 device_descriptor_, camera_hal_delegate_, |
| 63 camera_device_ipc_thread_.task_runner()); |
| 64 camera_device_ipc_thread_.task_runner()->PostTask( |
| 65 FROM_HERE, base::Bind(&CameraDeviceDelegate::AllocateAndStart, |
| 66 camera_device_delegate_->GetWeakPtr(), params, |
| 67 base::Passed(&client))); |
| 68 camera_device_ipc_thread_.task_runner()->PostTask( |
| 69 FROM_HERE, base::Bind(&CameraDeviceDelegate::SetRotation, |
| 70 camera_device_delegate_->GetWeakPtr(), rotation_)); |
| 71 } |
| 72 |
| 73 void VideoCaptureDeviceArcChromeOS::StopAndDeAllocate() { |
| 74 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 75 |
| 76 if (!camera_device_delegate_) { |
| 77 return; |
| 78 } |
| 79 |
| 80 // We do our best to allow the camera HAL cleanly shut down the device. In |
| 81 // general we don't trust the camera HAL so if the device does not close in |
| 82 // time we simply terminate the Mojo channel by resetting |
| 83 // |camera_device_delegate_|. |
| 84 base::WaitableEvent device_closed( |
| 85 base::WaitableEvent::ResetPolicy::MANUAL, |
| 86 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 87 camera_device_ipc_thread_.task_runner()->PostTask( |
| 88 FROM_HERE, base::Bind(&CameraDeviceDelegate::StopAndDeAllocate, |
| 89 camera_device_delegate_->GetWeakPtr(), |
| 90 base::Bind( |
| 91 [](base::WaitableEvent* device_closed) { |
| 92 device_closed->Signal(); |
| 93 }, |
| 94 base::Unretained(&device_closed)))); |
| 95 base::TimeDelta kWaitTimeoutSecs = base::TimeDelta::FromSeconds(3); |
| 96 device_closed.TimedWait(kWaitTimeoutSecs); |
| 97 |
| 98 camera_device_ipc_thread_.Stop(); |
| 99 camera_device_delegate_.reset(); |
| 100 } |
| 101 |
| 102 void VideoCaptureDeviceArcChromeOS::TakePhoto(TakePhotoCallback callback) { |
| 103 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 104 DCHECK(camera_device_delegate_); |
| 105 camera_device_ipc_thread_.task_runner()->PostTask( |
| 106 FROM_HERE, base::Bind(&CameraDeviceDelegate::TakePhoto, |
| 107 camera_device_delegate_->GetWeakPtr(), |
| 108 base::Passed(&callback))); |
| 109 } |
| 110 |
| 111 void VideoCaptureDeviceArcChromeOS::GetPhotoState( |
| 112 GetPhotoStateCallback callback) { |
| 113 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 114 camera_device_ipc_thread_.task_runner()->PostTask( |
| 115 FROM_HERE, base::Bind(&CameraDeviceDelegate::GetPhotoState, |
| 116 camera_device_delegate_->GetWeakPtr(), |
| 117 base::Passed(&callback))); |
| 118 } |
| 119 |
| 120 void VideoCaptureDeviceArcChromeOS::SetPhotoOptions( |
| 121 mojom::PhotoSettingsPtr settings, |
| 122 SetPhotoOptionsCallback callback) { |
| 123 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 124 camera_device_ipc_thread_.task_runner()->PostTask( |
| 125 FROM_HERE, base::Bind(&CameraDeviceDelegate::SetPhotoOptions, |
| 126 camera_device_delegate_->GetWeakPtr(), |
| 127 base::Passed(&settings), base::Passed(&callback))); |
| 128 } |
| 129 |
| 130 void VideoCaptureDeviceArcChromeOS::SetDisplayRotation( |
| 131 const display::Display& display) { |
| 132 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 133 if (display.IsInternal()) |
| 134 SetRotation(display.rotation() * 90); |
| 135 } |
| 136 |
| 137 void VideoCaptureDeviceArcChromeOS::SetRotation(int rotation) { |
| 138 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 139 if (!rotates_with_device_) { |
| 140 rotation = 0; |
| 141 } else if (lens_facing_ == VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT) { |
| 142 // Original frame when |rotation| = 0 |
| 143 // ----------------------- |
| 144 // | * | |
| 145 // | * * | |
| 146 // | * * | |
| 147 // | ******* | |
| 148 // | * * | |
| 149 // | * * | |
| 150 // ----------------------- |
| 151 // |
| 152 // |rotation| = 90, this is what back camera sees |
| 153 // ----------------------- |
| 154 // | ******** | |
| 155 // | * **** | |
| 156 // | * *** | |
| 157 // | * *** | |
| 158 // | * **** | |
| 159 // | ******** | |
| 160 // ----------------------- |
| 161 // |
| 162 // |rotation| = 90, this is what front camera sees |
| 163 // ----------------------- |
| 164 // | ******** | |
| 165 // | **** * | |
| 166 // | *** * | |
| 167 // | *** * | |
| 168 // | **** * | |
| 169 // | ******** | |
| 170 // ----------------------- |
| 171 // |
| 172 // Therefore, for back camera, we need to rotate (360 - |rotation|). |
| 173 rotation = (360 - rotation) % 360; |
| 174 } |
| 175 // Take into account camera orientation w.r.t. the display. External cameras |
| 176 // would have camera_orientation_ as 0. |
| 177 rotation_ = (rotation + camera_orientation_) % 360; |
| 178 if (camera_device_ipc_thread_.IsRunning()) { |
| 179 camera_device_ipc_thread_.task_runner()->PostTask( |
| 180 FROM_HERE, |
| 181 base::Bind(&CameraDeviceDelegate::SetRotation, |
| 182 camera_device_delegate_->GetWeakPtr(), rotation_)); |
| 183 } |
| 184 } |
| 185 |
| 186 } // namespace media |
| OLD | NEW |