| 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 "base/threading/platform_thread.h" |
| 8 #include "media/capture/video/chromeos/camera_device_delegate.h" |
| 9 #include "media/capture/video/chromeos/camera_hal_delegate.h" |
| 10 #include "ui/display/display.h" |
| 11 #include "ui/display/display_observer.h" |
| 12 #include "ui/display/screen.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 VideoCaptureDeviceArcChromeOS::VideoCaptureDeviceArcChromeOS( |
| 17 scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_screen_observer, |
| 18 const VideoCaptureDeviceDescriptor& device_descriptor, |
| 19 scoped_refptr<CameraHalDelegate> camera_hal_delegate) |
| 20 : device_descriptor_(device_descriptor), |
| 21 camera_hal_delegate_(std::move(camera_hal_delegate)), |
| 22 capture_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 23 camera_device_ipc_thread_(std::string("CameraDeviceIpcThread") + |
| 24 device_descriptor.device_id), |
| 25 screen_observer_delegate_(new ScreenObserverDelegate( |
| 26 this, |
| 27 std::move(task_runner_for_screen_observer))), |
| 28 lens_facing_(device_descriptor.facing), |
| 29 camera_orientation_(0), |
| 30 // External cameras have lens_facing as MEDIA_VIDEO_FACING_NONE. |
| 31 // We don't want to rotate the frame even if the device rotates. |
| 32 rotates_with_device_(lens_facing_ != |
| 33 VideoFacingMode::MEDIA_VIDEO_FACING_NONE), |
| 34 rotation_(0) {} |
| 35 |
| 36 VideoCaptureDeviceArcChromeOS::~VideoCaptureDeviceArcChromeOS() { |
| 37 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 38 DCHECK(!camera_device_ipc_thread_.IsRunning()); |
| 39 screen_observer_delegate_->RemoveObserver(); |
| 40 } |
| 41 |
| 42 // VideoCaptureDevice implementation. |
| 43 void VideoCaptureDeviceArcChromeOS::AllocateAndStart( |
| 44 const VideoCaptureParams& params, |
| 45 std::unique_ptr<Client> client) { |
| 46 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 47 DCHECK(!camera_device_delegate_); |
| 48 |
| 49 if (!camera_device_ipc_thread_.Start()) { |
| 50 std::string error_msg = "Failed to start device thread"; |
| 51 LOG(ERROR) << error_msg; |
| 52 client->OnError(FROM_HERE, error_msg); |
| 53 return; |
| 54 } |
| 55 camera_device_delegate_ = |
| 56 new CameraDeviceDelegate(device_descriptor_, camera_hal_delegate_, |
| 57 camera_device_ipc_thread_.task_runner()); |
| 58 camera_device_ipc_thread_.task_runner()->PostTask( |
| 59 FROM_HERE, |
| 60 base::Bind(&CameraDeviceDelegate::AllocateAndStart, |
| 61 camera_device_delegate_, params, base::Passed(&client))); |
| 62 camera_device_ipc_thread_.task_runner()->PostTask( |
| 63 FROM_HERE, base::Bind(&CameraDeviceDelegate::SetRotation, |
| 64 camera_device_delegate_, rotation_)); |
| 65 } |
| 66 |
| 67 void VideoCaptureDeviceArcChromeOS::StopAndDeAllocate() { |
| 68 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 69 |
| 70 if (!camera_device_delegate_) { |
| 71 return; |
| 72 } |
| 73 camera_device_ipc_thread_.task_runner()->PostTask( |
| 74 FROM_HERE, base::Bind(&CameraDeviceDelegate::StopAndDeAllocate, |
| 75 camera_device_delegate_)); |
| 76 // Wait until all other references to |camera_device_delegate_| are dropped to |
| 77 // make sure all the IPC calls are done. |
| 78 base::TimeTicks wait_until = |
| 79 base::TimeTicks() + base::TimeDelta::FromSeconds(3); |
| 80 while (!camera_device_delegate_->HasOneRef()) { |
| 81 if (base::TimeTicks() >= wait_until) { |
| 82 LOG(ERROR) << "Timed out waiting capture device to stop"; |
| 83 break; |
| 84 } |
| 85 base::PlatformThread::YieldCurrentThread(); |
| 86 } |
| 87 camera_device_ipc_thread_.Stop(); |
| 88 camera_device_delegate_ = nullptr; |
| 89 } |
| 90 |
| 91 void VideoCaptureDeviceArcChromeOS::TakePhoto(TakePhotoCallback callback) { |
| 92 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 93 DCHECK(camera_device_delegate_); |
| 94 camera_device_ipc_thread_.task_runner()->PostTask( |
| 95 FROM_HERE, base::Bind(&CameraDeviceDelegate::TakePhoto, |
| 96 camera_device_delegate_, base::Passed(&callback))); |
| 97 } |
| 98 |
| 99 void VideoCaptureDeviceArcChromeOS::GetPhotoCapabilities( |
| 100 GetPhotoCapabilitiesCallback callback) { |
| 101 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 102 camera_device_ipc_thread_.task_runner()->PostTask( |
| 103 FROM_HERE, base::Bind(&CameraDeviceDelegate::GetPhotoCapabilities, |
| 104 camera_device_delegate_, base::Passed(&callback))); |
| 105 } |
| 106 |
| 107 void VideoCaptureDeviceArcChromeOS::SetPhotoOptions( |
| 108 mojom::PhotoSettingsPtr settings, |
| 109 SetPhotoOptionsCallback callback) { |
| 110 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 111 camera_device_ipc_thread_.task_runner()->PostTask( |
| 112 FROM_HERE, base::Bind(&CameraDeviceDelegate::SetPhotoOptions, |
| 113 camera_device_delegate_, base::Passed(&settings), |
| 114 base::Passed(&callback))); |
| 115 } |
| 116 |
| 117 void VideoCaptureDeviceArcChromeOS::SetDisplayRotation( |
| 118 const display::Display& display) { |
| 119 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 120 if (display.IsInternal()) |
| 121 SetRotation(display.rotation() * 90); |
| 122 } |
| 123 |
| 124 void VideoCaptureDeviceArcChromeOS::SetRotation(int rotation) { |
| 125 DCHECK(capture_task_runner_->BelongsToCurrentThread()); |
| 126 if (!rotates_with_device_) { |
| 127 rotation = 0; |
| 128 } else if (lens_facing_ == VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT) { |
| 129 // Original frame when |rotation| = 0 |
| 130 // ----------------------- |
| 131 // | * | |
| 132 // | * * | |
| 133 // | * * | |
| 134 // | ******* | |
| 135 // | * * | |
| 136 // | * * | |
| 137 // ----------------------- |
| 138 // |
| 139 // |rotation| = 90, this is what back camera sees |
| 140 // ----------------------- |
| 141 // | ******** | |
| 142 // | * **** | |
| 143 // | * *** | |
| 144 // | * *** | |
| 145 // | * **** | |
| 146 // | ******** | |
| 147 // ----------------------- |
| 148 // |
| 149 // |rotation| = 90, this is what front camera sees |
| 150 // ----------------------- |
| 151 // | ******** | |
| 152 // | **** * | |
| 153 // | *** * | |
| 154 // | *** * | |
| 155 // | **** * | |
| 156 // | ******** | |
| 157 // ----------------------- |
| 158 // |
| 159 // Therefore, for back camera, we need to rotate (360 - |rotation|). |
| 160 rotation = (360 - rotation) % 360; |
| 161 } |
| 162 // Take into account camera orientation w.r.t. the display. External cameras |
| 163 // would have camera_orientation_ as 0. |
| 164 rotation_ = (rotation + camera_orientation_) % 360; |
| 165 if (camera_device_ipc_thread_.IsRunning()) { |
| 166 camera_device_ipc_thread_.task_runner()->PostTask( |
| 167 FROM_HERE, base::Bind(&CameraDeviceDelegate::SetRotation, |
| 168 camera_device_delegate_, rotation_)); |
| 169 } |
| 170 } |
| 171 |
| 172 } // namespace media |
| OLD | NEW |