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