| 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/camera_device_delegate.h" |
| 6 |
| 7 #include "media/base/bind_to_current_loop.h" |
| 8 #include "media/capture/video/chromeos/camera_hal_delegate.h" |
| 9 #include "media/capture/video/chromeos/camera_metadata_utils.h" |
| 10 #include "media/capture/video/chromeos/pixel_format_utils.h" |
| 11 #include "mojo/edk/embedder/embedder.h" |
| 12 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 13 #include "third_party/libsync/include/sync/sync.h" |
| 14 |
| 15 namespace media { |
| 16 |
| 17 CameraDeviceDelegate::CameraDeviceDelegate( |
| 18 VideoCaptureDeviceDescriptor device_descriptor, |
| 19 scoped_refptr<CameraHalDelegate> camera_hal_delegate, |
| 20 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) |
| 21 : device_descriptor_(device_descriptor), |
| 22 camera_hal_delegate_(camera_hal_delegate), |
| 23 state_(kStopped), |
| 24 rotation_(0), |
| 25 callback_ops_(this), |
| 26 ipc_task_runner_(ipc_task_runner), |
| 27 frame_number_(0), |
| 28 partial_result_count_(1), |
| 29 first_frame_shutter_time_(base::TimeTicks::Now()) {} |
| 30 |
| 31 void CameraDeviceDelegate::AllocateAndStart( |
| 32 const VideoCaptureParams& params, |
| 33 std::unique_ptr<VideoCaptureDevice::Client> client) { |
| 34 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 35 DCHECK(!client_); |
| 36 DCHECK_EQ(state_, kStopped); |
| 37 |
| 38 chrome_capture_params_ = params; |
| 39 client_ = std::move(client); |
| 40 frame_number_ = 0; |
| 41 partial_results_.clear(); |
| 42 first_frame_shutter_time_ = base::TimeTicks::Now(); |
| 43 SetState(kStarting); |
| 44 |
| 45 int32_t camera_id = std::stoi(device_descriptor_.device_id); |
| 46 // We need to get the static camera metadata of the camera deivce first. |
| 47 camera_hal_delegate_->GetCameraInfo( |
| 48 camera_id, BindToCurrentLoop( |
| 49 base::Bind(&CameraDeviceDelegate::OnGotCameraInfo, this))); |
| 50 } |
| 51 |
| 52 void CameraDeviceDelegate::StopAndDeAllocate() { |
| 53 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 54 // StopAndDeAllocate may be called at any state except kStopping. |
| 55 DCHECK_NE(state_, kStopping); |
| 56 |
| 57 if (state_ == kStopped) { |
| 58 // In case of Mojo connection error the device may be stopped before |
| 59 // StopAndDeAllocate is called. |
| 60 return; |
| 61 } |
| 62 |
| 63 SetState(kStopping); |
| 64 if (!device_ops_.is_bound()) { |
| 65 // The device delegate is in the process of opening the camera device. |
| 66 return; |
| 67 } |
| 68 device_ops_->Close(base::Bind(&CameraDeviceDelegate::OnClosed, this)); |
| 69 } |
| 70 |
| 71 void CameraDeviceDelegate::TakePhoto( |
| 72 VideoCaptureDevice::TakePhotoCallback callback) { |
| 73 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 74 // TODO(jcliang): Implement TakePhoto. |
| 75 } |
| 76 |
| 77 void CameraDeviceDelegate::GetPhotoCapabilities( |
| 78 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) { |
| 79 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 80 // TODO(jcliang): Implement GetPhotoCapabilities. |
| 81 } |
| 82 |
| 83 void CameraDeviceDelegate::SetPhotoOptions( |
| 84 mojom::PhotoSettingsPtr settings, |
| 85 VideoCaptureDevice::SetPhotoOptionsCallback callback) { |
| 86 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 87 // TODO(jcliang): Implement SetPhotoOptions. |
| 88 } |
| 89 |
| 90 void CameraDeviceDelegate::SetRotation(int rotation) { |
| 91 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 92 DCHECK(rotation >= 0 && rotation < 360 && rotation % 90 == 0); |
| 93 rotation_ = rotation; |
| 94 } |
| 95 |
| 96 void CameraDeviceDelegate::SetState(State state) { |
| 97 state_ = state; |
| 98 } |
| 99 |
| 100 void CameraDeviceDelegate::SetErrorState( |
| 101 const tracked_objects::Location& from_here, |
| 102 const std::string& reason) { |
| 103 state_ = kError; |
| 104 LOG(ERROR) << reason; |
| 105 client_->OnError(from_here, reason); |
| 106 } |
| 107 |
| 108 void CameraDeviceDelegate::ResetMojoInterface() { |
| 109 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 110 |
| 111 device_ops_.reset(); |
| 112 if (callback_ops_.is_bound()) { |
| 113 callback_ops_.Unbind(); |
| 114 } |
| 115 } |
| 116 |
| 117 void CameraDeviceDelegate::OnMojoConnectionError() { |
| 118 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 119 |
| 120 if (state_ == kStopping) { |
| 121 // When in stopping state the camera HAL adapter may terminate the Mojo |
| 122 // channel before we do, in which case the OnClosed callback would not be |
| 123 // called. |
| 124 OnClosed(0); |
| 125 } else { |
| 126 // The Mojo channel terminated unexpectedly. |
| 127 ResetMojoInterface(); |
| 128 SetState(kStopped); |
| 129 SetErrorState(FROM_HERE, "Mojo connection error"); |
| 130 } |
| 131 } |
| 132 |
| 133 void CameraDeviceDelegate::OnClosed(int32_t result) { |
| 134 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 135 DCHECK_EQ(state_, kStopping); |
| 136 |
| 137 if (result) { |
| 138 client_->OnLog(std::string("Failed to close device: ") + |
| 139 std::string(strerror(result))); |
| 140 } |
| 141 ResetMojoInterface(); |
| 142 // Only after the Mojo channel is closed can we be sure that |stream_context_| |
| 143 // is not accessed anymore. |
| 144 stream_context_.reset(); |
| 145 client_.reset(); |
| 146 SetState(kStopped); |
| 147 } |
| 148 |
| 149 void CameraDeviceDelegate::OnGotCameraInfo( |
| 150 int32_t result, |
| 151 arc::mojom::CameraInfoPtr camera_info) { |
| 152 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 153 |
| 154 if (state_ != kStarting) { |
| 155 DCHECK_EQ(state_, kStopping); |
| 156 OnClosed(0); |
| 157 return; |
| 158 } |
| 159 |
| 160 if (result) { |
| 161 std::string error_msg = "Failed to get camera info"; |
| 162 LOG(ERROR) << error_msg; |
| 163 client_->OnError(FROM_HERE, error_msg); |
| 164 return; |
| 165 } |
| 166 static_metadata_ = std::move(camera_info->static_camera_characteristics); |
| 167 const arc::mojom::CameraMetadataEntryPtr* partial_count = GetMetadataEntry( |
| 168 static_metadata_, |
| 169 arc::mojom::CameraMetadataTag::ANDROID_REQUEST_PARTIAL_RESULT_COUNT); |
| 170 // The partial result count metadata is optional; defaults to 1 in case it |
| 171 // is not set in the static metadata. |
| 172 if (partial_count) { |
| 173 partial_result_count_ = |
| 174 *reinterpret_cast<int32_t*>((*partial_count)->data.data()); |
| 175 } |
| 176 int32_t camera_id = std::stoi(device_descriptor_.device_id); |
| 177 camera_hal_delegate_->OpenDevice( |
| 178 camera_id, |
| 179 base::Bind(&CameraDeviceDelegate::OnOpenedDeviceOnModuleDelegate, this)); |
| 180 } |
| 181 |
| 182 void CameraDeviceDelegate::OnOpenedDeviceOnModuleDelegate( |
| 183 int32_t result, |
| 184 arc::mojom::Camera3DeviceOpsPtr device_ops) { |
| 185 // This method runs on |module_task_runner_| of |camera_hal_delegate_|. |
| 186 // |device_ops.PassInterface| needs to be called on |module_task_runner_| |
| 187 // hence we need this wrapper method and cannot use BindToCurrentLoop. |
| 188 mojo::InterfacePtrInfo<arc::mojom::Camera3DeviceOps> device_ops_info = |
| 189 device_ops.PassInterface(); |
| 190 ipc_task_runner_->PostTask( |
| 191 FROM_HERE, base::Bind(&CameraDeviceDelegate::OnOpenedDevice, this, result, |
| 192 base::Passed(&device_ops_info))); |
| 193 } |
| 194 |
| 195 void CameraDeviceDelegate::OnOpenedDevice( |
| 196 int32_t result, |
| 197 mojo::InterfacePtrInfo<arc::mojom::Camera3DeviceOps> device_ops_info) { |
| 198 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 199 |
| 200 if (state_ != kStarting) { |
| 201 DCHECK_EQ(state_, kStopping); |
| 202 // |device_ops_info| is destroyed after return and hence terminates the Mojo |
| 203 // connection to the camera device. |
| 204 OnClosed(0); |
| 205 return; |
| 206 } |
| 207 |
| 208 if (result) { |
| 209 std::string error_msg = "Failed to open camera device"; |
| 210 LOG(ERROR) << error_msg; |
| 211 client_->OnError(FROM_HERE, error_msg); |
| 212 return; |
| 213 } |
| 214 if (!device_ops_info.is_valid()) { |
| 215 std::string error_msg = "Invalid device_ops_info"; |
| 216 LOG(ERROR) << error_msg; |
| 217 client_->OnError(FROM_HERE, error_msg); |
| 218 return; |
| 219 } |
| 220 device_ops_.Bind(std::move(device_ops_info)); |
| 221 device_ops_.set_connection_error_handler( |
| 222 base::Bind(&CameraDeviceDelegate::OnMojoConnectionError, this)); |
| 223 Initialize(); |
| 224 } |
| 225 |
| 226 void CameraDeviceDelegate::Initialize() { |
| 227 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 228 DCHECK_EQ(state_, kStarting); |
| 229 |
| 230 // Set up context for preview stream. |
| 231 arc::mojom::Camera3StreamPtr preview_stream = |
| 232 arc::mojom::Camera3Stream::New(); |
| 233 preview_stream->id = static_cast<uint64_t>( |
| 234 arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW); |
| 235 preview_stream->stream_type = |
| 236 arc::mojom::Camera3StreamType::CAMERA3_STREAM_OUTPUT; |
| 237 preview_stream->width = |
| 238 chrome_capture_params_.requested_format.frame_size.width(); |
| 239 preview_stream->height = |
| 240 chrome_capture_params_.requested_format.frame_size.height(); |
| 241 preview_stream->format = |
| 242 arc::mojom::HalPixelFormat::HAL_PIXEL_FORMAT_YCbCr_420_888; |
| 243 preview_stream->data_space = 0; |
| 244 preview_stream->rotation = |
| 245 arc::mojom::Camera3StreamRotation::CAMERA3_STREAM_ROTATION_0; |
| 246 |
| 247 stream_context_.reset(new StreamContext); |
| 248 stream_context_->stream = std::move(preview_stream); |
| 249 |
| 250 device_ops_->Initialize( |
| 251 callback_ops_.CreateInterfacePtrAndBind(), |
| 252 base::Bind(&CameraDeviceDelegate::OnInitialized, this)); |
| 253 callback_ops_.set_connection_error_handler( |
| 254 base::Bind(&CameraDeviceDelegate::OnMojoConnectionError, this)); |
| 255 } |
| 256 |
| 257 void CameraDeviceDelegate::OnInitialized(int32_t result) { |
| 258 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 259 |
| 260 if (state_ != kStarting) { |
| 261 DCHECK_EQ(state_, kStopping); |
| 262 return; |
| 263 } |
| 264 if (result) { |
| 265 SetErrorState(FROM_HERE, std::string("Failed to initialize camera device") + |
| 266 std::to_string(result)); |
| 267 return; |
| 268 } |
| 269 SetState(kInitialized); |
| 270 ConfigureStreams(); |
| 271 } |
| 272 |
| 273 void CameraDeviceDelegate::ConfigureStreams() { |
| 274 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 275 DCHECK_EQ(state_, kInitialized); |
| 276 |
| 277 arc::mojom::Camera3StreamConfigurationPtr stream_config = |
| 278 arc::mojom::Camera3StreamConfiguration::New(); |
| 279 stream_config->streams.push_back(stream_context_->stream.Clone()); |
| 280 stream_config->operation_mode = arc::mojom::Camera3StreamConfigurationMode:: |
| 281 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE; |
| 282 device_ops_->ConfigureStreams( |
| 283 std::move(stream_config), |
| 284 base::Bind(&CameraDeviceDelegate::OnConfiguredStreams, this)); |
| 285 } |
| 286 |
| 287 void CameraDeviceDelegate::OnConfiguredStreams( |
| 288 int32_t result, |
| 289 arc::mojom::Camera3StreamConfigurationPtr updated_config) { |
| 290 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 291 |
| 292 if (state_ != kInitialized) { |
| 293 DCHECK_EQ(state_, kStopping); |
| 294 return; |
| 295 } |
| 296 if (result) { |
| 297 SetErrorState(FROM_HERE, std::string("Failed to configure streams") + |
| 298 std::to_string(result)); |
| 299 return; |
| 300 } |
| 301 if (!updated_config || updated_config->streams.size() != 1) { |
| 302 SetErrorState(FROM_HERE, |
| 303 std::string("Wrong number of streams configured") + |
| 304 std::to_string(updated_config->streams.size())); |
| 305 return; |
| 306 } |
| 307 auto& updated_stream = updated_config->streams[0]; |
| 308 |
| 309 const size_t kMaximumAllowedBuffers = 15; |
| 310 if (updated_stream->max_buffers > kMaximumAllowedBuffers) { |
| 311 SetErrorState(FROM_HERE, |
| 312 std::string("Camera HAL requested ") + |
| 313 std::to_string(updated_stream->max_buffers) + |
| 314 std::string(" buffers which exceeds the allowed maximum " |
| 315 "number of buffers")); |
| 316 return; |
| 317 } |
| 318 |
| 319 VideoCaptureFormat capture_format = chrome_capture_params_.requested_format; |
| 320 // TODO(jcliang): Determine the best format from metadata. |
| 321 capture_format.pixel_format = PIXEL_FORMAT_NV12; |
| 322 stream_context_->capture_format = capture_format; |
| 323 stream_context_->stream->usage = updated_stream->usage; |
| 324 stream_context_->stream->max_buffers = updated_stream->max_buffers; |
| 325 |
| 326 VLOG(2) << "Stream " << updated_stream->id |
| 327 << " configured: usage=" << updated_stream->usage |
| 328 << " max_buffers=" << updated_stream->max_buffers; |
| 329 |
| 330 // Allocate buffers. |
| 331 size_t num_buffers = stream_context_->stream->max_buffers; |
| 332 stream_context_->buffers.resize(num_buffers); |
| 333 for (size_t j = 0; j < num_buffers; ++j) { |
| 334 const VideoCaptureFormat frame_format( |
| 335 gfx::Size(stream_context_->stream->width, |
| 336 stream_context_->stream->height), |
| 337 0.0, stream_context_->capture_format.pixel_format); |
| 338 std::unique_ptr<base::SharedMemory> buffer(new base::SharedMemory()); |
| 339 base::SharedMemoryCreateOptions options; |
| 340 options.size = frame_format.ImageAllocationSize(); |
| 341 options.share_read_only = false; |
| 342 bool ret = buffer->Create(options); |
| 343 if (!ret) { |
| 344 SetErrorState(FROM_HERE, "Failed to create SharedMemory buffer"); |
| 345 return; |
| 346 } |
| 347 ret = buffer->Map(buffer->requested_size()); |
| 348 if (!ret) { |
| 349 SetErrorState(FROM_HERE, "Failed to map SharedMemory buffer"); |
| 350 return; |
| 351 } |
| 352 stream_context_->buffers[j] = std::move(buffer); |
| 353 stream_context_->free_buffers.push(j); |
| 354 } |
| 355 VLOG(2) << "Allocated " << stream_context_->stream->max_buffers << " buffers"; |
| 356 ConstructDefaultRequestSettings(); |
| 357 client_->OnStarted(); |
| 358 } |
| 359 |
| 360 void CameraDeviceDelegate::ConstructDefaultRequestSettings() { |
| 361 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 362 DCHECK_EQ(state_, kInitialized); |
| 363 DCHECK(stream_context_); |
| 364 |
| 365 device_ops_->ConstructDefaultRequestSettings( |
| 366 arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW, |
| 367 base::Bind(&CameraDeviceDelegate::OnConstructedDefaultRequestSettings, |
| 368 this)); |
| 369 } |
| 370 |
| 371 void CameraDeviceDelegate::OnConstructedDefaultRequestSettings( |
| 372 arc::mojom::CameraMetadataPtr settings) { |
| 373 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 374 |
| 375 if (state_ != kInitialized) { |
| 376 DCHECK_EQ(state_, kStopping); |
| 377 return; |
| 378 } |
| 379 DCHECK(stream_context_); |
| 380 stream_context_->request_settings = std::move(settings); |
| 381 SetState(kStreamConfigured); |
| 382 StartCapture(); |
| 383 } |
| 384 |
| 385 void CameraDeviceDelegate::StartCapture() { |
| 386 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 387 DCHECK_EQ(state_, kStreamConfigured); |
| 388 DCHECK(stream_context_); |
| 389 DCHECK(!stream_context_->request_settings.is_null()); |
| 390 |
| 391 SetState(kCapturing); |
| 392 // We cannot use a loop to register all the free buffers in one shot here |
| 393 // because the camera HAL v3 API specifies that the client cannot call |
| 394 // ProcessCaptureRequest before the previous one returns. |
| 395 RegisterBuffer(); |
| 396 } |
| 397 |
| 398 void CameraDeviceDelegate::RegisterBuffer() { |
| 399 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 400 DCHECK_EQ(state_, kCapturing); |
| 401 DCHECK(stream_context_); |
| 402 |
| 403 if (stream_context_->free_buffers.empty()) { |
| 404 return; |
| 405 } |
| 406 |
| 407 size_t buffer_id = stream_context_->free_buffers.front(); |
| 408 stream_context_->free_buffers.pop(); |
| 409 const base::SharedMemory* buffer = stream_context_->buffers[buffer_id].get(); |
| 410 |
| 411 VideoPixelFormat buffer_format = stream_context_->capture_format.pixel_format; |
| 412 uint32_t drm_format = PixFormatChromiumToDrm(buffer_format); |
| 413 if (!drm_format) { |
| 414 SetErrorState(FROM_HERE, std::string("Unsupported video pixel format") + |
| 415 VideoPixelFormatToString(buffer_format)); |
| 416 return; |
| 417 } |
| 418 arc::mojom::HalPixelFormat hal_pixel_format = stream_context_->stream->format; |
| 419 |
| 420 size_t num_planes = VideoFrame::NumPlanes(buffer_format); |
| 421 std::vector<mojo::ScopedHandle> fds(num_planes); |
| 422 std::vector<uint32_t> strides(num_planes); |
| 423 std::vector<uint32_t> offsets(num_planes); |
| 424 for (size_t i = 0; i < num_planes; ++i) { |
| 425 base::SharedMemoryHandle shm_handle = buffer->handle(); |
| 426 // Wrap the platform handle. |
| 427 MojoHandle wrapped_handle; |
| 428 MojoResult result = mojo::edk::CreatePlatformHandleWrapper( |
| 429 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( |
| 430 base::SharedMemory::DuplicateHandle(shm_handle).GetHandle())), |
| 431 &wrapped_handle); |
| 432 if (result != MOJO_RESULT_OK) { |
| 433 SetErrorState(FROM_HERE, "Failed to wrap shared memory handle"); |
| 434 return; |
| 435 } |
| 436 fds[i].reset(mojo::Handle(wrapped_handle)); |
| 437 strides[i] = VideoFrame::RowBytes( |
| 438 i, buffer_format, |
| 439 chrome_capture_params_.requested_format.frame_size.width()); |
| 440 if (!i) { |
| 441 offsets[i] = 0; |
| 442 } else { |
| 443 offsets[i] = offsets[i - 1] + |
| 444 VideoFrame::PlaneSize( |
| 445 buffer_format, i - 1, |
| 446 chrome_capture_params_.requested_format.frame_size) |
| 447 .GetArea(); |
| 448 } |
| 449 } |
| 450 device_ops_->RegisterBuffer( |
| 451 buffer_id, arc::mojom::Camera3DeviceOps::BufferType::SHM, std::move(fds), |
| 452 drm_format, hal_pixel_format, stream_context_->stream->width, |
| 453 stream_context_->stream->height, std::move(strides), std::move(offsets), |
| 454 base::Bind(&CameraDeviceDelegate::OnRegisteredBuffer, this, buffer_id)); |
| 455 VLOG(2) << "Registered buffer " << buffer_id; |
| 456 } |
| 457 |
| 458 void CameraDeviceDelegate::OnRegisteredBuffer(size_t buffer_id, |
| 459 int32_t result) { |
| 460 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 461 |
| 462 if (state_ != kCapturing) { |
| 463 DCHECK_EQ(state_, kStopping); |
| 464 return; |
| 465 } |
| 466 if (result) { |
| 467 SetErrorState(FROM_HERE, std::string("Failed to register buffer: ") + |
| 468 std::to_string(result)); |
| 469 return; |
| 470 } |
| 471 ProcessCaptureRequest(buffer_id); |
| 472 } |
| 473 |
| 474 void CameraDeviceDelegate::ProcessCaptureRequest(size_t buffer_id) { |
| 475 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 476 DCHECK_EQ(state_, kCapturing); |
| 477 DCHECK(stream_context_); |
| 478 |
| 479 arc::mojom::Camera3StreamBufferPtr buffer = |
| 480 arc::mojom::Camera3StreamBuffer::New(); |
| 481 buffer->stream_id = static_cast<uint64_t>( |
| 482 arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW); |
| 483 buffer->buffer_id = buffer_id; |
| 484 buffer->status = arc::mojom::Camera3BufferStatus::CAMERA3_BUFFER_STATUS_OK; |
| 485 |
| 486 arc::mojom::Camera3CaptureRequestPtr request = |
| 487 arc::mojom::Camera3CaptureRequest::New(); |
| 488 request->frame_number = frame_number_; |
| 489 request->settings = stream_context_->request_settings.Clone(); |
| 490 request->output_buffers.push_back(std::move(buffer)); |
| 491 |
| 492 device_ops_->ProcessCaptureRequest( |
| 493 std::move(request), |
| 494 base::Bind(&CameraDeviceDelegate::OnProcessedCaptureRequest, this)); |
| 495 VLOG(2) << "Requested capture for frame " << frame_number_ << " with buffer " |
| 496 << buffer_id; |
| 497 frame_number_++; |
| 498 // In case |frame_number_| wraps around, we start at 1 to avoid resetting |
| 499 // |first_frame_shutter_time_|. |
| 500 if (!frame_number_) { |
| 501 frame_number_++; |
| 502 } |
| 503 } |
| 504 |
| 505 void CameraDeviceDelegate::OnProcessedCaptureRequest(int32_t result) { |
| 506 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 507 |
| 508 if (state_ != kCapturing) { |
| 509 DCHECK_EQ(state_, kStopping); |
| 510 return; |
| 511 } |
| 512 if (result) { |
| 513 SetErrorState(FROM_HERE, std::string("Process capture request failed") + |
| 514 std::to_string(result)); |
| 515 return; |
| 516 } |
| 517 RegisterBuffer(); |
| 518 } |
| 519 |
| 520 void CameraDeviceDelegate::ProcessCaptureResult( |
| 521 arc::mojom::Camera3CaptureResultPtr result) { |
| 522 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 523 |
| 524 if (state_ != kCapturing) { |
| 525 DCHECK_EQ(state_, kStopping); |
| 526 return; |
| 527 } |
| 528 uint32_t frame_number = result->frame_number; |
| 529 // A new partial result may be created in either ProcessCaptureResult or |
| 530 // Notify. |
| 531 CaptureResult& partial_result = partial_results_[frame_number]; |
| 532 if (partial_results_.size() > stream_context_->stream->max_buffers) { |
| 533 SetErrorState( |
| 534 FROM_HERE, |
| 535 "Received more capture results than the maximum number of buffers"); |
| 536 return; |
| 537 } |
| 538 if (result->output_buffers) { |
| 539 if (result->output_buffers->size() != 1) { |
| 540 SetErrorState( |
| 541 FROM_HERE, |
| 542 std::string("Incorrect number of output buffers received: ") + |
| 543 std::to_string(result->output_buffers->size())); |
| 544 return; |
| 545 } |
| 546 arc::mojom::Camera3StreamBufferPtr& stream_buffer = |
| 547 result->output_buffers.value()[0]; |
| 548 VLOG(2) << "Received capture result for frame " << frame_number |
| 549 << " stream_id: " << stream_buffer->stream_id; |
| 550 // The camera HAL v3 API specifies that only one capture result can carry |
| 551 // the result buffer for any given frame number. |
| 552 if (!partial_result.buffer.is_null()) { |
| 553 SetErrorState(FROM_HERE, |
| 554 std::string("Received multiple result buffers for frame ") + |
| 555 std::to_string(frame_number)); |
| 556 return; |
| 557 } else { |
| 558 partial_result.buffer = std::move(stream_buffer); |
| 559 } |
| 560 } |
| 561 |
| 562 // |result->partial_result| is set to 0 if the capture result contains only |
| 563 // the result buffer handles and no result metadata. |
| 564 if (result->partial_result) { |
| 565 uint32_t result_id = result->partial_result; |
| 566 if (result_id > partial_result_count_) { |
| 567 SetErrorState(FROM_HERE, std::string("Invalid partial_result id: ") + |
| 568 std::to_string(result_id)); |
| 569 return; |
| 570 } |
| 571 if (partial_result.partial_metadata_received.find(result_id) != |
| 572 partial_result.partial_metadata_received.end()) { |
| 573 SetErrorState(FROM_HERE, |
| 574 std::string("Received duplicated partial metadata: ") + |
| 575 std::to_string(result_id)); |
| 576 return; |
| 577 } |
| 578 partial_result.partial_metadata_received.insert(result_id); |
| 579 MergeMetadata(&partial_result.metadata, result->result); |
| 580 } |
| 581 |
| 582 if (partial_result.partial_metadata_received.size() == |
| 583 partial_result_count_ && |
| 584 !partial_result.buffer.is_null()) { |
| 585 // We can only submit the result buffer after we receive the shutter time. |
| 586 if (partial_result.reference_time != base::TimeTicks()) { |
| 587 SubmitCaptureResult(frame_number); |
| 588 } |
| 589 } |
| 590 } |
| 591 |
| 592 void CameraDeviceDelegate::Notify(arc::mojom::Camera3NotifyMsgPtr message) { |
| 593 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 594 |
| 595 if (state_ != kCapturing) { |
| 596 DCHECK_EQ(state_, kStopping); |
| 597 return; |
| 598 } |
| 599 if (message->type == arc::mojom::Camera3MsgType::CAMERA3_MSG_ERROR) { |
| 600 uint32_t frame_number = message->message->get_error()->frame_number; |
| 601 uint64_t error_stream_id = message->message->get_error()->error_stream_id; |
| 602 arc::mojom::Camera3ErrorMsgCode error_code = |
| 603 message->message->get_error()->error_code; |
| 604 HandleNotifyError(frame_number, error_stream_id, error_code); |
| 605 } else { // arc::mojom::Camera3MsgType::CAMERA3_MSG_SHUTTER |
| 606 uint32_t frame_number = message->message->get_shutter()->frame_number; |
| 607 uint64_t shutter_time = message->message->get_shutter()->timestamp; |
| 608 // A new partial result may be created in either ProcessCaptureResult or |
| 609 // Notify. |
| 610 VLOG(2) << "Received shutter time for frame " << frame_number; |
| 611 if (!shutter_time) { |
| 612 SetErrorState(FROM_HERE, std::string("Received invalid shutter time: ") + |
| 613 std::to_string(shutter_time)); |
| 614 return; |
| 615 } |
| 616 CaptureResult& partial_result = partial_results_[frame_number]; |
| 617 if (partial_results_.size() > stream_context_->stream->max_buffers) { |
| 618 SetErrorState( |
| 619 FROM_HERE, |
| 620 "Received more capture results than the maximum number of buffers"); |
| 621 return; |
| 622 } |
| 623 // Shutter timestamp is in ns. |
| 624 base::TimeTicks reference_time = |
| 625 base::TimeTicks::FromInternalValue(shutter_time / 1000); |
| 626 partial_result.reference_time = reference_time; |
| 627 if (!frame_number) { |
| 628 // Record the shutter time of the first frame for calculating the |
| 629 // timestamp. |
| 630 first_frame_shutter_time_ = reference_time; |
| 631 } |
| 632 partial_result.timestamp = reference_time - first_frame_shutter_time_; |
| 633 if (partial_result.partial_metadata_received.size() == |
| 634 partial_result_count_ && |
| 635 !partial_result.buffer.is_null()) { |
| 636 SubmitCaptureResult(frame_number); |
| 637 } |
| 638 } |
| 639 } |
| 640 |
| 641 void CameraDeviceDelegate::HandleNotifyError( |
| 642 uint32_t frame_number, |
| 643 uint64_t error_stream_id, |
| 644 arc::mojom::Camera3ErrorMsgCode error_code) { |
| 645 switch (error_code) { |
| 646 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_DEVICE: |
| 647 // Fatal error and no more frames will be produced by the device. |
| 648 SetErrorState(FROM_HERE, "Fatal device error"); |
| 649 break; |
| 650 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_REQUEST: { |
| 651 // An error has occurred in processing the request; the request |
| 652 // specified by |frame_number| has been dropped by the camera device. |
| 653 // Subsequent requests are unaffected. |
| 654 // |
| 655 // The HAL will call ProcessCaptureResult with the buffers' state set to |
| 656 // STATUS_ERROR. The content of the buffers will be dropped and the |
| 657 // buffers will be reused in SubmitCaptureResult. |
| 658 std::string warning_msg = |
| 659 std::string("An error occurred while processing request for frame ") + |
| 660 std::to_string(frame_number); |
| 661 LOG(WARNING) << warning_msg; |
| 662 client_->OnLog(warning_msg); |
| 663 break; |
| 664 } |
| 665 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_RESULT: { |
| 666 // An error has occurred in producing the output metadata buffer for a |
| 667 // result; the output metadata will not be available for the frame |
| 668 // specified by |frame_number|. Subsequent requests are unaffected. |
| 669 std::string warning_msg = std::string( |
| 670 "An error occurred while producing result " |
| 671 "metadata for frame ") + |
| 672 std::to_string(frame_number); |
| 673 LOG(WARNING) << warning_msg; |
| 674 client_->OnLog(warning_msg); |
| 675 // The result metadata will not be complete so we don't need to wait for |
| 676 // partial results on frame |frame_number|. |
| 677 partial_results_[frame_number].partial_metadata_received.clear(); |
| 678 for (uint32_t i = 0; i < partial_result_count_; ++i) { |
| 679 partial_results_[frame_number].partial_metadata_received.insert(i); |
| 680 } |
| 681 break; |
| 682 } |
| 683 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_BUFFER: |
| 684 // An error has occurred in placing the output buffer into a stream for |
| 685 // a request. |frame_number| specifies the request for which the buffer |
| 686 // was dropped, and |error_stream_id| specifies the stream that dropped |
| 687 // the buffer. |
| 688 // |
| 689 // The HAL will call ProcessCaptureResult with the buffer's state set to |
| 690 // STATUS_ERROR. The content of the buffer will be dropped and the |
| 691 // buffer will be reused in SubmitCaptureResult. |
| 692 client_->OnLog( |
| 693 std::string( |
| 694 "An error occurred while filling output buffer of stream ") + |
| 695 std::to_string(error_stream_id) + std::string(" in frame ") + |
| 696 std::to_string(frame_number)); |
| 697 break; |
| 698 default: |
| 699 // To eliminate the warning for not handling CAMERA3_MSG_NUM_ERRORS |
| 700 break; |
| 701 } |
| 702 } |
| 703 |
| 704 void CameraDeviceDelegate::SubmitCaptureResult(uint32_t frame_number) { |
| 705 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 706 DCHECK_EQ(state_, kCapturing); |
| 707 |
| 708 if (partial_results_.begin()->first != frame_number) { |
| 709 SetErrorState(FROM_HERE, |
| 710 std::string("Received frame is out-of-order; expect ") + |
| 711 std::to_string(partial_results_.begin()->first) + |
| 712 std::string(" but got ") + std::to_string(frame_number)); |
| 713 return; |
| 714 } |
| 715 |
| 716 VLOG(2) << "Submit capture result of frame " << frame_number; |
| 717 CaptureResult& partial_result = partial_results_[frame_number]; |
| 718 DCHECK_EQ(partial_result.partial_metadata_received.size(), |
| 719 partial_result_count_); |
| 720 DCHECK(partial_result.buffer); |
| 721 uint32_t buffer_id = partial_result.buffer->buffer_id; |
| 722 |
| 723 // Wait on release fence before delivering the result buffer to client. |
| 724 if (partial_result.buffer->release_fence.is_valid()) { |
| 725 const int kSyncWaitTimeoutMs = 1000; |
| 726 mojo::edk::ScopedPlatformHandle fence; |
| 727 MojoResult result = mojo::edk::PassWrappedPlatformHandle( |
| 728 partial_result.buffer->release_fence.release().value(), &fence); |
| 729 if (result != MOJO_RESULT_OK) { |
| 730 SetErrorState(FROM_HERE, "Failed to unwrap release fence fd"); |
| 731 return; |
| 732 } |
| 733 if (!sync_wait(fence.get().handle, kSyncWaitTimeoutMs)) { |
| 734 SetErrorState(FROM_HERE, "Sync wait on release fence timed out"); |
| 735 return; |
| 736 } |
| 737 } |
| 738 |
| 739 // Deliver the captured data to client and then re-queue the buffer. |
| 740 if (partial_result.buffer->status != |
| 741 arc::mojom::Camera3BufferStatus::CAMERA3_BUFFER_STATUS_ERROR) { |
| 742 const base::SharedMemory* shm_buffer = |
| 743 stream_context_->buffers[buffer_id].get(); |
| 744 client_->OnIncomingCapturedData( |
| 745 reinterpret_cast<uint8_t*>(shm_buffer->memory()), |
| 746 shm_buffer->mapped_size(), stream_context_->capture_format, rotation_, |
| 747 partial_result.reference_time, partial_result.timestamp); |
| 748 } |
| 749 stream_context_->free_buffers.push(buffer_id); |
| 750 partial_results_.erase(frame_number); |
| 751 RegisterBuffer(); |
| 752 } |
| 753 |
| 754 } // namespace media |
| OLD | NEW |