| 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 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) |
| 21 : device_descriptor_(device_descriptor), |
| 22 camera_hal_delegate_(std::move(camera_hal_delegate)), |
| 23 state_(kStopped), |
| 24 rotation_(0), |
| 25 callback_ops_(this), |
| 26 ipc_task_runner_(std::move(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 arc::mojom::Camera3DeviceOpsRequest device_ops_request = |
| 178 mojo::MakeRequest(&device_ops_); |
| 179 device_ops_.set_connection_error_handler( |
| 180 base::Bind(&CameraDeviceDelegate::OnMojoConnectionError, this)); |
| 181 camera_hal_delegate_->OpenDevice( |
| 182 camera_id, std::move(device_ops_request), |
| 183 base::Bind(&CameraDeviceDelegate::OnOpenedDeviceOnModuleDelegate, this)); |
| 184 } |
| 185 |
| 186 void CameraDeviceDelegate::OnOpenedDeviceOnModuleDelegate(int32_t result) { |
| 187 // This method runs on |module_task_runner_| of |camera_hal_delegate_|. |
| 188 ipc_task_runner_->PostTask( |
| 189 FROM_HERE, |
| 190 base::Bind(&CameraDeviceDelegate::OnOpenedDevice, this, result)); |
| 191 } |
| 192 |
| 193 void CameraDeviceDelegate::OnOpenedDevice(int32_t result) { |
| 194 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 195 |
| 196 if (state_ != kStarting) { |
| 197 DCHECK_EQ(state_, kStopping); |
| 198 OnClosed(0); |
| 199 return; |
| 200 } |
| 201 |
| 202 if (result) { |
| 203 std::string error_msg = "Failed to open camera device"; |
| 204 LOG(ERROR) << error_msg; |
| 205 client_->OnError(FROM_HERE, error_msg); |
| 206 return; |
| 207 } |
| 208 Initialize(); |
| 209 } |
| 210 |
| 211 void CameraDeviceDelegate::Initialize() { |
| 212 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 213 DCHECK_EQ(state_, kStarting); |
| 214 |
| 215 // Set up context for preview stream. |
| 216 arc::mojom::Camera3StreamPtr preview_stream = |
| 217 arc::mojom::Camera3Stream::New(); |
| 218 preview_stream->id = static_cast<uint64_t>( |
| 219 arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW); |
| 220 preview_stream->stream_type = |
| 221 arc::mojom::Camera3StreamType::CAMERA3_STREAM_OUTPUT; |
| 222 preview_stream->width = |
| 223 chrome_capture_params_.requested_format.frame_size.width(); |
| 224 preview_stream->height = |
| 225 chrome_capture_params_.requested_format.frame_size.height(); |
| 226 preview_stream->format = |
| 227 arc::mojom::HalPixelFormat::HAL_PIXEL_FORMAT_YCbCr_420_888; |
| 228 preview_stream->data_space = 0; |
| 229 preview_stream->rotation = |
| 230 arc::mojom::Camera3StreamRotation::CAMERA3_STREAM_ROTATION_0; |
| 231 |
| 232 stream_context_.reset(new StreamContext); |
| 233 stream_context_->stream = std::move(preview_stream); |
| 234 |
| 235 device_ops_->Initialize( |
| 236 callback_ops_.CreateInterfacePtrAndBind(), |
| 237 base::Bind(&CameraDeviceDelegate::OnInitialized, this)); |
| 238 callback_ops_.set_connection_error_handler( |
| 239 base::Bind(&CameraDeviceDelegate::OnMojoConnectionError, this)); |
| 240 } |
| 241 |
| 242 void CameraDeviceDelegate::OnInitialized(int32_t result) { |
| 243 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 244 |
| 245 if (state_ != kStarting) { |
| 246 DCHECK_EQ(state_, kStopping); |
| 247 return; |
| 248 } |
| 249 if (result) { |
| 250 SetErrorState(FROM_HERE, std::string("Failed to initialize camera device") + |
| 251 std::to_string(result)); |
| 252 return; |
| 253 } |
| 254 SetState(kInitialized); |
| 255 ConfigureStreams(); |
| 256 } |
| 257 |
| 258 void CameraDeviceDelegate::ConfigureStreams() { |
| 259 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 260 DCHECK_EQ(state_, kInitialized); |
| 261 |
| 262 arc::mojom::Camera3StreamConfigurationPtr stream_config = |
| 263 arc::mojom::Camera3StreamConfiguration::New(); |
| 264 stream_config->streams.push_back(stream_context_->stream.Clone()); |
| 265 stream_config->operation_mode = arc::mojom::Camera3StreamConfigurationMode:: |
| 266 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE; |
| 267 device_ops_->ConfigureStreams( |
| 268 std::move(stream_config), |
| 269 base::Bind(&CameraDeviceDelegate::OnConfiguredStreams, this)); |
| 270 } |
| 271 |
| 272 void CameraDeviceDelegate::OnConfiguredStreams( |
| 273 int32_t result, |
| 274 arc::mojom::Camera3StreamConfigurationPtr updated_config) { |
| 275 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 276 |
| 277 if (state_ != kInitialized) { |
| 278 DCHECK_EQ(state_, kStopping); |
| 279 return; |
| 280 } |
| 281 if (result) { |
| 282 SetErrorState(FROM_HERE, std::string("Failed to configure streams") + |
| 283 std::to_string(result)); |
| 284 return; |
| 285 } |
| 286 if (!updated_config || updated_config->streams.size() != 1) { |
| 287 SetErrorState(FROM_HERE, |
| 288 std::string("Wrong number of streams configured") + |
| 289 std::to_string(updated_config->streams.size())); |
| 290 return; |
| 291 } |
| 292 auto& updated_stream = updated_config->streams[0]; |
| 293 |
| 294 const size_t kMaximumAllowedBuffers = 15; |
| 295 if (updated_stream->max_buffers > kMaximumAllowedBuffers) { |
| 296 SetErrorState(FROM_HERE, |
| 297 std::string("Camera HAL requested ") + |
| 298 std::to_string(updated_stream->max_buffers) + |
| 299 std::string(" buffers which exceeds the allowed maximum " |
| 300 "number of buffers")); |
| 301 return; |
| 302 } |
| 303 |
| 304 VideoCaptureFormat capture_format = chrome_capture_params_.requested_format; |
| 305 // TODO(jcliang): Determine the best format from metadata. |
| 306 capture_format.pixel_format = PIXEL_FORMAT_NV12; |
| 307 stream_context_->capture_format = capture_format; |
| 308 stream_context_->stream->usage = updated_stream->usage; |
| 309 stream_context_->stream->max_buffers = updated_stream->max_buffers; |
| 310 |
| 311 VLOG(2) << "Stream " << updated_stream->id |
| 312 << " configured: usage=" << updated_stream->usage |
| 313 << " max_buffers=" << updated_stream->max_buffers; |
| 314 |
| 315 // Allocate buffers. |
| 316 size_t num_buffers = stream_context_->stream->max_buffers; |
| 317 stream_context_->buffers.resize(num_buffers); |
| 318 for (size_t j = 0; j < num_buffers; ++j) { |
| 319 const VideoCaptureFormat frame_format( |
| 320 gfx::Size(stream_context_->stream->width, |
| 321 stream_context_->stream->height), |
| 322 0.0, stream_context_->capture_format.pixel_format); |
| 323 std::unique_ptr<base::SharedMemory> buffer(new base::SharedMemory()); |
| 324 base::SharedMemoryCreateOptions options; |
| 325 options.size = frame_format.ImageAllocationSize(); |
| 326 options.share_read_only = false; |
| 327 bool ret = buffer->Create(options); |
| 328 if (!ret) { |
| 329 SetErrorState(FROM_HERE, "Failed to create SharedMemory buffer"); |
| 330 return; |
| 331 } |
| 332 ret = buffer->Map(buffer->requested_size()); |
| 333 if (!ret) { |
| 334 SetErrorState(FROM_HERE, "Failed to map SharedMemory buffer"); |
| 335 return; |
| 336 } |
| 337 stream_context_->buffers[j] = std::move(buffer); |
| 338 stream_context_->free_buffers.push(j); |
| 339 } |
| 340 VLOG(2) << "Allocated " << stream_context_->stream->max_buffers << " buffers"; |
| 341 ConstructDefaultRequestSettings(); |
| 342 client_->OnStarted(); |
| 343 } |
| 344 |
| 345 void CameraDeviceDelegate::ConstructDefaultRequestSettings() { |
| 346 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 347 DCHECK_EQ(state_, kInitialized); |
| 348 DCHECK(stream_context_); |
| 349 |
| 350 device_ops_->ConstructDefaultRequestSettings( |
| 351 arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW, |
| 352 base::Bind(&CameraDeviceDelegate::OnConstructedDefaultRequestSettings, |
| 353 this)); |
| 354 } |
| 355 |
| 356 void CameraDeviceDelegate::OnConstructedDefaultRequestSettings( |
| 357 arc::mojom::CameraMetadataPtr settings) { |
| 358 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 359 |
| 360 if (state_ != kInitialized) { |
| 361 DCHECK_EQ(state_, kStopping); |
| 362 return; |
| 363 } |
| 364 DCHECK(stream_context_); |
| 365 stream_context_->request_settings = std::move(settings); |
| 366 SetState(kStreamConfigured); |
| 367 StartCapture(); |
| 368 } |
| 369 |
| 370 void CameraDeviceDelegate::StartCapture() { |
| 371 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 372 DCHECK_EQ(state_, kStreamConfigured); |
| 373 DCHECK(stream_context_); |
| 374 DCHECK(!stream_context_->request_settings.is_null()); |
| 375 |
| 376 SetState(kCapturing); |
| 377 // We cannot use a loop to register all the free buffers in one shot here |
| 378 // because the camera HAL v3 API specifies that the client cannot call |
| 379 // ProcessCaptureRequest before the previous one returns. |
| 380 RegisterBuffer(); |
| 381 } |
| 382 |
| 383 void CameraDeviceDelegate::RegisterBuffer() { |
| 384 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 385 DCHECK_EQ(state_, kCapturing); |
| 386 DCHECK(stream_context_); |
| 387 |
| 388 if (stream_context_->free_buffers.empty()) { |
| 389 return; |
| 390 } |
| 391 |
| 392 size_t buffer_id = stream_context_->free_buffers.front(); |
| 393 stream_context_->free_buffers.pop(); |
| 394 const base::SharedMemory* buffer = stream_context_->buffers[buffer_id].get(); |
| 395 |
| 396 VideoPixelFormat buffer_format = stream_context_->capture_format.pixel_format; |
| 397 uint32_t drm_format = PixFormatChromiumToDrm(buffer_format); |
| 398 if (!drm_format) { |
| 399 SetErrorState(FROM_HERE, std::string("Unsupported video pixel format") + |
| 400 VideoPixelFormatToString(buffer_format)); |
| 401 return; |
| 402 } |
| 403 arc::mojom::HalPixelFormat hal_pixel_format = stream_context_->stream->format; |
| 404 |
| 405 size_t num_planes = VideoFrame::NumPlanes(buffer_format); |
| 406 std::vector<mojo::ScopedHandle> fds(num_planes); |
| 407 std::vector<uint32_t> strides(num_planes); |
| 408 std::vector<uint32_t> offsets(num_planes); |
| 409 for (size_t i = 0; i < num_planes; ++i) { |
| 410 base::SharedMemoryHandle shm_handle = buffer->handle(); |
| 411 // Wrap the platform handle. |
| 412 MojoHandle wrapped_handle; |
| 413 MojoResult result = mojo::edk::CreatePlatformHandleWrapper( |
| 414 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( |
| 415 base::SharedMemory::DuplicateHandle(shm_handle).GetHandle())), |
| 416 &wrapped_handle); |
| 417 if (result != MOJO_RESULT_OK) { |
| 418 SetErrorState(FROM_HERE, "Failed to wrap shared memory handle"); |
| 419 return; |
| 420 } |
| 421 fds[i].reset(mojo::Handle(wrapped_handle)); |
| 422 strides[i] = VideoFrame::RowBytes( |
| 423 i, buffer_format, |
| 424 chrome_capture_params_.requested_format.frame_size.width()); |
| 425 if (!i) { |
| 426 offsets[i] = 0; |
| 427 } else { |
| 428 offsets[i] = offsets[i - 1] + |
| 429 VideoFrame::PlaneSize( |
| 430 buffer_format, i - 1, |
| 431 chrome_capture_params_.requested_format.frame_size) |
| 432 .GetArea(); |
| 433 } |
| 434 } |
| 435 device_ops_->RegisterBuffer( |
| 436 buffer_id, arc::mojom::Camera3DeviceOps::BufferType::SHM, std::move(fds), |
| 437 drm_format, hal_pixel_format, stream_context_->stream->width, |
| 438 stream_context_->stream->height, std::move(strides), std::move(offsets), |
| 439 base::Bind(&CameraDeviceDelegate::OnRegisteredBuffer, this, buffer_id)); |
| 440 VLOG(2) << "Registered buffer " << buffer_id; |
| 441 } |
| 442 |
| 443 void CameraDeviceDelegate::OnRegisteredBuffer(size_t buffer_id, |
| 444 int32_t result) { |
| 445 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 446 |
| 447 if (state_ != kCapturing) { |
| 448 DCHECK_EQ(state_, kStopping); |
| 449 return; |
| 450 } |
| 451 if (result) { |
| 452 SetErrorState(FROM_HERE, std::string("Failed to register buffer: ") + |
| 453 std::to_string(result)); |
| 454 return; |
| 455 } |
| 456 ProcessCaptureRequest(buffer_id); |
| 457 } |
| 458 |
| 459 void CameraDeviceDelegate::ProcessCaptureRequest(size_t buffer_id) { |
| 460 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 461 DCHECK_EQ(state_, kCapturing); |
| 462 DCHECK(stream_context_); |
| 463 |
| 464 arc::mojom::Camera3StreamBufferPtr buffer = |
| 465 arc::mojom::Camera3StreamBuffer::New(); |
| 466 buffer->stream_id = static_cast<uint64_t>( |
| 467 arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW); |
| 468 buffer->buffer_id = buffer_id; |
| 469 buffer->status = arc::mojom::Camera3BufferStatus::CAMERA3_BUFFER_STATUS_OK; |
| 470 |
| 471 arc::mojom::Camera3CaptureRequestPtr request = |
| 472 arc::mojom::Camera3CaptureRequest::New(); |
| 473 request->frame_number = frame_number_; |
| 474 request->settings = stream_context_->request_settings.Clone(); |
| 475 request->output_buffers.push_back(std::move(buffer)); |
| 476 |
| 477 device_ops_->ProcessCaptureRequest( |
| 478 std::move(request), |
| 479 base::Bind(&CameraDeviceDelegate::OnProcessedCaptureRequest, this)); |
| 480 VLOG(2) << "Requested capture for frame " << frame_number_ << " with buffer " |
| 481 << buffer_id; |
| 482 frame_number_++; |
| 483 // In case |frame_number_| wraps around, we start at 1 to avoid resetting |
| 484 // |first_frame_shutter_time_|. |
| 485 if (!frame_number_) { |
| 486 frame_number_++; |
| 487 } |
| 488 } |
| 489 |
| 490 void CameraDeviceDelegate::OnProcessedCaptureRequest(int32_t result) { |
| 491 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 492 |
| 493 if (state_ != kCapturing) { |
| 494 DCHECK_EQ(state_, kStopping); |
| 495 return; |
| 496 } |
| 497 if (result) { |
| 498 SetErrorState(FROM_HERE, std::string("Process capture request failed") + |
| 499 std::to_string(result)); |
| 500 return; |
| 501 } |
| 502 RegisterBuffer(); |
| 503 } |
| 504 |
| 505 void CameraDeviceDelegate::ProcessCaptureResult( |
| 506 arc::mojom::Camera3CaptureResultPtr result) { |
| 507 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 508 |
| 509 if (state_ != kCapturing) { |
| 510 DCHECK_EQ(state_, kStopping); |
| 511 return; |
| 512 } |
| 513 uint32_t frame_number = result->frame_number; |
| 514 // A new partial result may be created in either ProcessCaptureResult or |
| 515 // Notify. |
| 516 CaptureResult& partial_result = partial_results_[frame_number]; |
| 517 if (partial_results_.size() > stream_context_->stream->max_buffers) { |
| 518 SetErrorState( |
| 519 FROM_HERE, |
| 520 "Received more capture results than the maximum number of buffers"); |
| 521 return; |
| 522 } |
| 523 if (result->output_buffers) { |
| 524 if (result->output_buffers->size() != 1) { |
| 525 SetErrorState( |
| 526 FROM_HERE, |
| 527 std::string("Incorrect number of output buffers received: ") + |
| 528 std::to_string(result->output_buffers->size())); |
| 529 return; |
| 530 } |
| 531 arc::mojom::Camera3StreamBufferPtr& stream_buffer = |
| 532 result->output_buffers.value()[0]; |
| 533 VLOG(2) << "Received capture result for frame " << frame_number |
| 534 << " stream_id: " << stream_buffer->stream_id; |
| 535 // The camera HAL v3 API specifies that only one capture result can carry |
| 536 // the result buffer for any given frame number. |
| 537 if (!partial_result.buffer.is_null()) { |
| 538 SetErrorState(FROM_HERE, |
| 539 std::string("Received multiple result buffers for frame ") + |
| 540 std::to_string(frame_number)); |
| 541 return; |
| 542 } else { |
| 543 partial_result.buffer = std::move(stream_buffer); |
| 544 } |
| 545 } |
| 546 |
| 547 // |result->partial_result| is set to 0 if the capture result contains only |
| 548 // the result buffer handles and no result metadata. |
| 549 if (result->partial_result) { |
| 550 uint32_t result_id = result->partial_result; |
| 551 if (result_id > partial_result_count_) { |
| 552 SetErrorState(FROM_HERE, std::string("Invalid partial_result id: ") + |
| 553 std::to_string(result_id)); |
| 554 return; |
| 555 } |
| 556 if (partial_result.partial_metadata_received.find(result_id) != |
| 557 partial_result.partial_metadata_received.end()) { |
| 558 SetErrorState(FROM_HERE, |
| 559 std::string("Received duplicated partial metadata: ") + |
| 560 std::to_string(result_id)); |
| 561 return; |
| 562 } |
| 563 partial_result.partial_metadata_received.insert(result_id); |
| 564 MergeMetadata(&partial_result.metadata, result->result); |
| 565 } |
| 566 |
| 567 if (partial_result.partial_metadata_received.size() == |
| 568 partial_result_count_ && |
| 569 !partial_result.buffer.is_null()) { |
| 570 // We can only submit the result buffer after we receive the shutter time. |
| 571 if (partial_result.reference_time != base::TimeTicks()) { |
| 572 SubmitCaptureResult(frame_number); |
| 573 } |
| 574 } |
| 575 } |
| 576 |
| 577 void CameraDeviceDelegate::Notify(arc::mojom::Camera3NotifyMsgPtr message) { |
| 578 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 579 |
| 580 if (state_ != kCapturing) { |
| 581 DCHECK_EQ(state_, kStopping); |
| 582 return; |
| 583 } |
| 584 if (message->type == arc::mojom::Camera3MsgType::CAMERA3_MSG_ERROR) { |
| 585 uint32_t frame_number = message->message->get_error()->frame_number; |
| 586 uint64_t error_stream_id = message->message->get_error()->error_stream_id; |
| 587 arc::mojom::Camera3ErrorMsgCode error_code = |
| 588 message->message->get_error()->error_code; |
| 589 HandleNotifyError(frame_number, error_stream_id, error_code); |
| 590 } else { // arc::mojom::Camera3MsgType::CAMERA3_MSG_SHUTTER |
| 591 uint32_t frame_number = message->message->get_shutter()->frame_number; |
| 592 uint64_t shutter_time = message->message->get_shutter()->timestamp; |
| 593 // A new partial result may be created in either ProcessCaptureResult or |
| 594 // Notify. |
| 595 VLOG(2) << "Received shutter time for frame " << frame_number; |
| 596 if (!shutter_time) { |
| 597 SetErrorState(FROM_HERE, std::string("Received invalid shutter time: ") + |
| 598 std::to_string(shutter_time)); |
| 599 return; |
| 600 } |
| 601 CaptureResult& partial_result = partial_results_[frame_number]; |
| 602 if (partial_results_.size() > stream_context_->stream->max_buffers) { |
| 603 SetErrorState( |
| 604 FROM_HERE, |
| 605 "Received more capture results than the maximum number of buffers"); |
| 606 return; |
| 607 } |
| 608 // Shutter timestamp is in ns. |
| 609 base::TimeTicks reference_time = |
| 610 base::TimeTicks::FromInternalValue(shutter_time / 1000); |
| 611 partial_result.reference_time = reference_time; |
| 612 if (!frame_number) { |
| 613 // Record the shutter time of the first frame for calculating the |
| 614 // timestamp. |
| 615 first_frame_shutter_time_ = reference_time; |
| 616 } |
| 617 partial_result.timestamp = reference_time - first_frame_shutter_time_; |
| 618 if (partial_result.partial_metadata_received.size() == |
| 619 partial_result_count_ && |
| 620 !partial_result.buffer.is_null()) { |
| 621 SubmitCaptureResult(frame_number); |
| 622 } |
| 623 } |
| 624 } |
| 625 |
| 626 void CameraDeviceDelegate::HandleNotifyError( |
| 627 uint32_t frame_number, |
| 628 uint64_t error_stream_id, |
| 629 arc::mojom::Camera3ErrorMsgCode error_code) { |
| 630 switch (error_code) { |
| 631 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_DEVICE: |
| 632 // Fatal error and no more frames will be produced by the device. |
| 633 SetErrorState(FROM_HERE, "Fatal device error"); |
| 634 break; |
| 635 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_REQUEST: { |
| 636 // An error has occurred in processing the request; the request |
| 637 // specified by |frame_number| has been dropped by the camera device. |
| 638 // Subsequent requests are unaffected. |
| 639 // |
| 640 // The HAL will call ProcessCaptureResult with the buffers' state set to |
| 641 // STATUS_ERROR. The content of the buffers will be dropped and the |
| 642 // buffers will be reused in SubmitCaptureResult. |
| 643 std::string warning_msg = |
| 644 std::string("An error occurred while processing request for frame ") + |
| 645 std::to_string(frame_number); |
| 646 LOG(WARNING) << warning_msg; |
| 647 client_->OnLog(warning_msg); |
| 648 break; |
| 649 } |
| 650 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_RESULT: { |
| 651 // An error has occurred in producing the output metadata buffer for a |
| 652 // result; the output metadata will not be available for the frame |
| 653 // specified by |frame_number|. Subsequent requests are unaffected. |
| 654 std::string warning_msg = std::string( |
| 655 "An error occurred while producing result " |
| 656 "metadata for frame ") + |
| 657 std::to_string(frame_number); |
| 658 LOG(WARNING) << warning_msg; |
| 659 client_->OnLog(warning_msg); |
| 660 // The result metadata will not be complete so we don't need to wait for |
| 661 // partial results on frame |frame_number|. |
| 662 partial_results_[frame_number].partial_metadata_received.clear(); |
| 663 for (uint32_t i = 0; i < partial_result_count_; ++i) { |
| 664 partial_results_[frame_number].partial_metadata_received.insert(i); |
| 665 } |
| 666 break; |
| 667 } |
| 668 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_BUFFER: |
| 669 // An error has occurred in placing the output buffer into a stream for |
| 670 // a request. |frame_number| specifies the request for which the buffer |
| 671 // was dropped, and |error_stream_id| specifies the stream that dropped |
| 672 // the buffer. |
| 673 // |
| 674 // The HAL will call ProcessCaptureResult with the buffer's state set to |
| 675 // STATUS_ERROR. The content of the buffer will be dropped and the |
| 676 // buffer will be reused in SubmitCaptureResult. |
| 677 client_->OnLog( |
| 678 std::string( |
| 679 "An error occurred while filling output buffer of stream ") + |
| 680 std::to_string(error_stream_id) + std::string(" in frame ") + |
| 681 std::to_string(frame_number)); |
| 682 break; |
| 683 default: |
| 684 // To eliminate the warning for not handling CAMERA3_MSG_NUM_ERRORS |
| 685 break; |
| 686 } |
| 687 } |
| 688 |
| 689 void CameraDeviceDelegate::SubmitCaptureResult(uint32_t frame_number) { |
| 690 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 691 DCHECK_EQ(state_, kCapturing); |
| 692 |
| 693 if (partial_results_.begin()->first != frame_number) { |
| 694 SetErrorState(FROM_HERE, |
| 695 std::string("Received frame is out-of-order; expect ") + |
| 696 std::to_string(partial_results_.begin()->first) + |
| 697 std::string(" but got ") + std::to_string(frame_number)); |
| 698 return; |
| 699 } |
| 700 |
| 701 VLOG(2) << "Submit capture result of frame " << frame_number; |
| 702 CaptureResult& partial_result = partial_results_[frame_number]; |
| 703 DCHECK_EQ(partial_result.partial_metadata_received.size(), |
| 704 partial_result_count_); |
| 705 DCHECK(partial_result.buffer); |
| 706 uint32_t buffer_id = partial_result.buffer->buffer_id; |
| 707 |
| 708 // Wait on release fence before delivering the result buffer to client. |
| 709 if (partial_result.buffer->release_fence.is_valid()) { |
| 710 const int kSyncWaitTimeoutMs = 1000; |
| 711 mojo::edk::ScopedPlatformHandle fence; |
| 712 MojoResult result = mojo::edk::PassWrappedPlatformHandle( |
| 713 partial_result.buffer->release_fence.release().value(), &fence); |
| 714 if (result != MOJO_RESULT_OK) { |
| 715 SetErrorState(FROM_HERE, "Failed to unwrap release fence fd"); |
| 716 return; |
| 717 } |
| 718 if (!sync_wait(fence.get().handle, kSyncWaitTimeoutMs)) { |
| 719 SetErrorState(FROM_HERE, "Sync wait on release fence timed out"); |
| 720 return; |
| 721 } |
| 722 } |
| 723 |
| 724 // Deliver the captured data to client and then re-queue the buffer. |
| 725 if (partial_result.buffer->status != |
| 726 arc::mojom::Camera3BufferStatus::CAMERA3_BUFFER_STATUS_ERROR) { |
| 727 const base::SharedMemory* shm_buffer = |
| 728 stream_context_->buffers[buffer_id].get(); |
| 729 client_->OnIncomingCapturedData( |
| 730 reinterpret_cast<uint8_t*>(shm_buffer->memory()), |
| 731 shm_buffer->mapped_size(), stream_context_->capture_format, rotation_, |
| 732 partial_result.reference_time, partial_result.timestamp); |
| 733 } |
| 734 stream_context_->free_buffers.push(buffer_id); |
| 735 partial_results_.erase(frame_number); |
| 736 RegisterBuffer(); |
| 737 } |
| 738 |
| 739 } // namespace media |
| OLD | NEW |