| 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/stream_buffer_manager.h" |
| 6 |
| 7 #include "media/capture/video/chromeos/camera_device_context.h" |
| 8 #include "media/capture/video/chromeos/camera_device_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 StreamBufferManager::StreamBufferManager( |
| 18 arc::mojom::Camera3CallbackOpsRequest callback_ops_request, |
| 19 scoped_refptr<CameraDeviceDelegate> camera_device_delegate, |
| 20 CameraDeviceContext* device_context, |
| 21 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) |
| 22 : callback_ops_(this, std::move(callback_ops_request)), |
| 23 camera_device_delegate_(std::move(camera_device_delegate)), |
| 24 device_context_(device_context), |
| 25 ipc_task_runner_(std::move(ipc_task_runner)), |
| 26 capturing_(false), |
| 27 frame_number_(0), |
| 28 partial_result_count_(1), |
| 29 first_frame_shutter_time_(base::TimeTicks::Now()) { |
| 30 DCHECK(callback_ops_.is_bound()); |
| 31 DCHECK(device_context_); |
| 32 } |
| 33 |
| 34 void StreamBufferManager::SetUpStreamAndBuffers( |
| 35 VideoCaptureFormat capture_format, |
| 36 uint32_t partial_result_count, |
| 37 arc::mojom::Camera3StreamPtr stream) { |
| 38 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 39 DCHECK(!stream_context_); |
| 40 |
| 41 VLOG(2) << "Stream " << stream->id << " configured: usage=" << stream->usage |
| 42 << " max_buffers=" << stream->max_buffers; |
| 43 |
| 44 const size_t kMaximumAllowedBuffers = 15; |
| 45 if (stream->max_buffers > kMaximumAllowedBuffers) { |
| 46 device_context_->SetErrorState( |
| 47 FROM_HERE, std::string("Camera HAL requested ") + |
| 48 std::to_string(stream->max_buffers) + |
| 49 std::string(" buffers which exceeds the allowed maximum " |
| 50 "number of buffers")); |
| 51 return; |
| 52 } |
| 53 |
| 54 partial_result_count_ = partial_result_count; |
| 55 stream_context_.reset(new StreamContext{capture_format, std::move(stream)}); |
| 56 |
| 57 // Allocate buffers. |
| 58 size_t num_buffers = stream_context_->stream->max_buffers; |
| 59 stream_context_->buffers.resize(num_buffers); |
| 60 for (size_t j = 0; j < num_buffers; ++j) { |
| 61 const VideoCaptureFormat frame_format( |
| 62 gfx::Size(stream_context_->stream->width, |
| 63 stream_context_->stream->height), |
| 64 0.0, stream_context_->capture_format.pixel_format); |
| 65 std::unique_ptr<base::SharedMemory> buffer(new base::SharedMemory()); |
| 66 base::SharedMemoryCreateOptions options; |
| 67 options.size = frame_format.ImageAllocationSize(); |
| 68 options.share_read_only = false; |
| 69 bool ret = buffer->Create(options); |
| 70 if (!ret) { |
| 71 device_context_->SetErrorState(FROM_HERE, |
| 72 "Failed to create SharedMemory buffer"); |
| 73 return; |
| 74 } |
| 75 ret = buffer->Map(buffer->requested_size()); |
| 76 if (!ret) { |
| 77 device_context_->SetErrorState(FROM_HERE, |
| 78 "Failed to map SharedMemory buffer"); |
| 79 return; |
| 80 } |
| 81 stream_context_->buffers[j] = std::move(buffer); |
| 82 stream_context_->free_buffers.push(j); |
| 83 } |
| 84 VLOG(2) << "Allocated " << stream_context_->stream->max_buffers << " buffers"; |
| 85 } |
| 86 |
| 87 void StreamBufferManager::StartCapture(arc::mojom::CameraMetadataPtr settings) { |
| 88 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 89 DCHECK(stream_context_); |
| 90 DCHECK(stream_context_->request_settings.is_null()); |
| 91 |
| 92 capturing_ = true; |
| 93 stream_context_->request_settings = std::move(settings); |
| 94 // We cannot use a loop to register all the free buffers in one shot here |
| 95 // because the camera HAL v3 API specifies that the client cannot call |
| 96 // ProcessCaptureRequest before the previous one returns. |
| 97 RegisterBuffer(); |
| 98 } |
| 99 |
| 100 void StreamBufferManager::StopCapture() { |
| 101 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 102 capturing_ = false; |
| 103 } |
| 104 |
| 105 void StreamBufferManager::RegisterBuffer() { |
| 106 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 107 DCHECK(stream_context_); |
| 108 |
| 109 if (!capturing_) { |
| 110 return; |
| 111 } |
| 112 |
| 113 if (stream_context_->free_buffers.empty()) { |
| 114 return; |
| 115 } |
| 116 |
| 117 size_t buffer_id = stream_context_->free_buffers.front(); |
| 118 stream_context_->free_buffers.pop(); |
| 119 const base::SharedMemory* buffer = stream_context_->buffers[buffer_id].get(); |
| 120 |
| 121 VideoPixelFormat buffer_format = stream_context_->capture_format.pixel_format; |
| 122 uint32_t drm_format = PixFormatChromiumToDrm(buffer_format); |
| 123 if (!drm_format) { |
| 124 device_context_->SetErrorState( |
| 125 FROM_HERE, std::string("Unsupported video pixel format") + |
| 126 VideoPixelFormatToString(buffer_format)); |
| 127 return; |
| 128 } |
| 129 arc::mojom::HalPixelFormat hal_pixel_format = stream_context_->stream->format; |
| 130 |
| 131 size_t num_planes = VideoFrame::NumPlanes(buffer_format); |
| 132 std::vector<mojo::ScopedHandle> fds(num_planes); |
| 133 std::vector<uint32_t> strides(num_planes); |
| 134 std::vector<uint32_t> offsets(num_planes); |
| 135 for (size_t i = 0; i < num_planes; ++i) { |
| 136 base::SharedMemoryHandle shm_handle = buffer->handle(); |
| 137 // Wrap the platform handle. |
| 138 MojoHandle wrapped_handle; |
| 139 MojoResult result = mojo::edk::CreatePlatformHandleWrapper( |
| 140 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( |
| 141 base::SharedMemory::DuplicateHandle(shm_handle).GetHandle())), |
| 142 &wrapped_handle); |
| 143 if (result != MOJO_RESULT_OK) { |
| 144 device_context_->SetErrorState(FROM_HERE, |
| 145 "Failed to wrap shared memory handle"); |
| 146 return; |
| 147 } |
| 148 fds[i].reset(mojo::Handle(wrapped_handle)); |
| 149 strides[i] = VideoFrame::RowBytes( |
| 150 i, buffer_format, stream_context_->capture_format.frame_size.width()); |
| 151 if (!i) { |
| 152 offsets[i] = 0; |
| 153 } else { |
| 154 offsets[i] = |
| 155 offsets[i - 1] + |
| 156 VideoFrame::PlaneSize(buffer_format, i - 1, |
| 157 stream_context_->capture_format.frame_size) |
| 158 .GetArea(); |
| 159 } |
| 160 } |
| 161 camera_device_delegate_->RegisterBuffer( |
| 162 buffer_id, arc::mojom::Camera3DeviceOps::BufferType::SHM, std::move(fds), |
| 163 drm_format, hal_pixel_format, stream_context_->stream->width, |
| 164 stream_context_->stream->height, std::move(strides), std::move(offsets), |
| 165 base::Bind(&StreamBufferManager::OnRegisteredBuffer, this, buffer_id)); |
| 166 VLOG(2) << "Registered buffer " << buffer_id; |
| 167 } |
| 168 |
| 169 void StreamBufferManager::OnRegisteredBuffer(size_t buffer_id, int32_t result) { |
| 170 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 171 |
| 172 if (!capturing_) { |
| 173 return; |
| 174 } |
| 175 if (result) { |
| 176 device_context_->SetErrorState(FROM_HERE, |
| 177 std::string("Failed to register buffer: ") + |
| 178 std::string(strerror(result))); |
| 179 return; |
| 180 } |
| 181 ProcessCaptureRequest(buffer_id); |
| 182 } |
| 183 |
| 184 void StreamBufferManager::ProcessCaptureRequest(size_t buffer_id) { |
| 185 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 186 DCHECK(stream_context_); |
| 187 |
| 188 arc::mojom::Camera3StreamBufferPtr buffer = |
| 189 arc::mojom::Camera3StreamBuffer::New(); |
| 190 buffer->stream_id = static_cast<uint64_t>( |
| 191 arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW); |
| 192 buffer->buffer_id = buffer_id; |
| 193 buffer->status = arc::mojom::Camera3BufferStatus::CAMERA3_BUFFER_STATUS_OK; |
| 194 |
| 195 arc::mojom::Camera3CaptureRequestPtr request = |
| 196 arc::mojom::Camera3CaptureRequest::New(); |
| 197 request->frame_number = frame_number_; |
| 198 request->settings = stream_context_->request_settings.Clone(); |
| 199 request->output_buffers.push_back(std::move(buffer)); |
| 200 |
| 201 camera_device_delegate_->ProcessCaptureRequest( |
| 202 std::move(request), |
| 203 base::Bind(&StreamBufferManager::OnProcessedCaptureRequest, this)); |
| 204 VLOG(2) << "Requested capture for frame " << frame_number_ << " with buffer " |
| 205 << buffer_id; |
| 206 frame_number_++; |
| 207 // In case |frame_number_| wraps around, we start at 1 to avoid resetting |
| 208 // |first_frame_shutter_time_|. |
| 209 if (!frame_number_) { |
| 210 frame_number_++; |
| 211 } |
| 212 } |
| 213 |
| 214 void StreamBufferManager::OnProcessedCaptureRequest(int32_t result) { |
| 215 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 216 |
| 217 if (!capturing_) { |
| 218 return; |
| 219 } |
| 220 if (result) { |
| 221 device_context_->SetErrorState( |
| 222 FROM_HERE, std::string("Process capture request failed") + |
| 223 std::string(strerror(result))); |
| 224 return; |
| 225 } |
| 226 RegisterBuffer(); |
| 227 } |
| 228 |
| 229 void StreamBufferManager::ProcessCaptureResult( |
| 230 arc::mojom::Camera3CaptureResultPtr result) { |
| 231 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 232 |
| 233 if (!capturing_) { |
| 234 return; |
| 235 } |
| 236 uint32_t frame_number = result->frame_number; |
| 237 // A new partial result may be created in either ProcessCaptureResult or |
| 238 // Notify. |
| 239 CaptureResult& partial_result = partial_results_[frame_number]; |
| 240 if (partial_results_.size() > stream_context_->stream->max_buffers) { |
| 241 device_context_->SetErrorState( |
| 242 FROM_HERE, |
| 243 "Received more capture results than the maximum number of buffers"); |
| 244 return; |
| 245 } |
| 246 if (result->output_buffers) { |
| 247 if (result->output_buffers->size() != 1) { |
| 248 device_context_->SetErrorState( |
| 249 FROM_HERE, |
| 250 std::string("Incorrect number of output buffers received: ") + |
| 251 std::to_string(result->output_buffers->size())); |
| 252 return; |
| 253 } |
| 254 arc::mojom::Camera3StreamBufferPtr& stream_buffer = |
| 255 result->output_buffers.value()[0]; |
| 256 VLOG(2) << "Received capture result for frame " << frame_number |
| 257 << " stream_id: " << stream_buffer->stream_id; |
| 258 // The camera HAL v3 API specifies that only one capture result can carry |
| 259 // the result buffer for any given frame number. |
| 260 if (!partial_result.buffer.is_null()) { |
| 261 device_context_->SetErrorState( |
| 262 FROM_HERE, |
| 263 std::string("Received multiple result buffers for frame ") + |
| 264 std::to_string(frame_number)); |
| 265 return; |
| 266 } else { |
| 267 partial_result.buffer = std::move(stream_buffer); |
| 268 } |
| 269 } |
| 270 |
| 271 // |result->partial_result| is set to 0 if the capture result contains only |
| 272 // the result buffer handles and no result metadata. |
| 273 if (result->partial_result) { |
| 274 uint32_t result_id = result->partial_result; |
| 275 if (result_id > partial_result_count_) { |
| 276 device_context_->SetErrorState( |
| 277 FROM_HERE, std::string("Invalid partial_result id: ") + |
| 278 std::to_string(result_id)); |
| 279 return; |
| 280 } |
| 281 if (partial_result.partial_metadata_received.find(result_id) != |
| 282 partial_result.partial_metadata_received.end()) { |
| 283 device_context_->SetErrorState( |
| 284 FROM_HERE, std::string("Received duplicated partial metadata: ") + |
| 285 std::to_string(result_id)); |
| 286 return; |
| 287 } |
| 288 partial_result.partial_metadata_received.insert(result_id); |
| 289 MergeMetadata(&partial_result.metadata, result->result); |
| 290 } |
| 291 |
| 292 if (partial_result.partial_metadata_received.size() == |
| 293 partial_result_count_ && |
| 294 !partial_result.buffer.is_null()) { |
| 295 // We can only submit the result buffer after we receive the shutter time. |
| 296 if (partial_result.reference_time != base::TimeTicks()) { |
| 297 SubmitCaptureResult(frame_number); |
| 298 } |
| 299 } |
| 300 } |
| 301 |
| 302 void StreamBufferManager::Notify(arc::mojom::Camera3NotifyMsgPtr message) { |
| 303 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 304 |
| 305 if (!capturing_) { |
| 306 return; |
| 307 } |
| 308 if (message->type == arc::mojom::Camera3MsgType::CAMERA3_MSG_ERROR) { |
| 309 uint32_t frame_number = message->message->get_error()->frame_number; |
| 310 uint64_t error_stream_id = message->message->get_error()->error_stream_id; |
| 311 arc::mojom::Camera3ErrorMsgCode error_code = |
| 312 message->message->get_error()->error_code; |
| 313 HandleNotifyError(frame_number, error_stream_id, error_code); |
| 314 } else { // arc::mojom::Camera3MsgType::CAMERA3_MSG_SHUTTER |
| 315 uint32_t frame_number = message->message->get_shutter()->frame_number; |
| 316 uint64_t shutter_time = message->message->get_shutter()->timestamp; |
| 317 // A new partial result may be created in either ProcessCaptureResult or |
| 318 // Notify. |
| 319 VLOG(2) << "Received shutter time for frame " << frame_number; |
| 320 if (!shutter_time) { |
| 321 device_context_->SetErrorState( |
| 322 FROM_HERE, std::string("Received invalid shutter time: ") + |
| 323 std::to_string(shutter_time)); |
| 324 return; |
| 325 } |
| 326 CaptureResult& partial_result = partial_results_[frame_number]; |
| 327 if (partial_results_.size() > stream_context_->stream->max_buffers) { |
| 328 device_context_->SetErrorState( |
| 329 FROM_HERE, |
| 330 "Received more capture results than the maximum number of buffers"); |
| 331 return; |
| 332 } |
| 333 // Shutter timestamp is in ns. |
| 334 base::TimeTicks reference_time = |
| 335 base::TimeTicks::FromInternalValue(shutter_time / 1000); |
| 336 partial_result.reference_time = reference_time; |
| 337 if (!frame_number) { |
| 338 // Record the shutter time of the first frame for calculating the |
| 339 // timestamp. |
| 340 first_frame_shutter_time_ = reference_time; |
| 341 } |
| 342 partial_result.timestamp = reference_time - first_frame_shutter_time_; |
| 343 if (partial_result.partial_metadata_received.size() == |
| 344 partial_result_count_ && |
| 345 !partial_result.buffer.is_null()) { |
| 346 SubmitCaptureResult(frame_number); |
| 347 } |
| 348 } |
| 349 } |
| 350 |
| 351 void StreamBufferManager::HandleNotifyError( |
| 352 uint32_t frame_number, |
| 353 uint64_t error_stream_id, |
| 354 arc::mojom::Camera3ErrorMsgCode error_code) { |
| 355 switch (error_code) { |
| 356 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_DEVICE: |
| 357 // Fatal error and no more frames will be produced by the device. |
| 358 device_context_->SetErrorState(FROM_HERE, "Fatal device error"); |
| 359 break; |
| 360 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_REQUEST: { |
| 361 // An error has occurred in processing the request; the request |
| 362 // specified by |frame_number| has been dropped by the camera device. |
| 363 // Subsequent requests are unaffected. |
| 364 // |
| 365 // The HAL will call ProcessCaptureResult with the buffers' state set to |
| 366 // STATUS_ERROR. The content of the buffers will be dropped and the |
| 367 // buffers will be reused in SubmitCaptureResult. |
| 368 std::string warning_msg = |
| 369 std::string("An error occurred while processing request for frame ") + |
| 370 std::to_string(frame_number); |
| 371 LOG(WARNING) << warning_msg; |
| 372 device_context_->LogToClient(warning_msg); |
| 373 break; |
| 374 } |
| 375 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_RESULT: { |
| 376 // An error has occurred in producing the output metadata buffer for a |
| 377 // result; the output metadata will not be available for the frame |
| 378 // specified by |frame_number|. Subsequent requests are unaffected. |
| 379 std::string warning_msg = std::string( |
| 380 "An error occurred while producing result " |
| 381 "metadata for frame ") + |
| 382 std::to_string(frame_number); |
| 383 LOG(WARNING) << warning_msg; |
| 384 device_context_->LogToClient(warning_msg); |
| 385 // The result metadata will not be complete so we don't need to wait for |
| 386 // partial results on frame |frame_number|. |
| 387 partial_results_[frame_number].partial_metadata_received.clear(); |
| 388 for (uint32_t i = 0; i < partial_result_count_; ++i) { |
| 389 partial_results_[frame_number].partial_metadata_received.insert(i); |
| 390 } |
| 391 break; |
| 392 } |
| 393 case arc::mojom::Camera3ErrorMsgCode::CAMERA3_MSG_ERROR_BUFFER: |
| 394 // An error has occurred in placing the output buffer into a stream for |
| 395 // a request. |frame_number| specifies the request for which the buffer |
| 396 // was dropped, and |error_stream_id| specifies the stream that dropped |
| 397 // the buffer. |
| 398 // |
| 399 // The HAL will call ProcessCaptureResult with the buffer's state set to |
| 400 // STATUS_ERROR. The content of the buffer will be dropped and the |
| 401 // buffer will be reused in SubmitCaptureResult. |
| 402 device_context_->LogToClient( |
| 403 std::string( |
| 404 "An error occurred while filling output buffer of stream ") + |
| 405 std::to_string(error_stream_id) + std::string(" in frame ") + |
| 406 std::to_string(frame_number)); |
| 407 break; |
| 408 default: |
| 409 // To eliminate the warning for not handling CAMERA3_MSG_NUM_ERRORS |
| 410 break; |
| 411 } |
| 412 } |
| 413 |
| 414 void StreamBufferManager::SubmitCaptureResult(uint32_t frame_number) { |
| 415 DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 416 |
| 417 if (partial_results_.begin()->first != frame_number) { |
| 418 device_context_->SetErrorState( |
| 419 FROM_HERE, std::string("Received frame is out-of-order; expect ") + |
| 420 std::to_string(partial_results_.begin()->first) + |
| 421 std::string(" but got ") + std::to_string(frame_number)); |
| 422 return; |
| 423 } |
| 424 |
| 425 VLOG(2) << "Submit capture result of frame " << frame_number; |
| 426 CaptureResult& partial_result = partial_results_[frame_number]; |
| 427 DCHECK_EQ(partial_result.partial_metadata_received.size(), |
| 428 partial_result_count_); |
| 429 DCHECK(partial_result.buffer); |
| 430 uint32_t buffer_id = partial_result.buffer->buffer_id; |
| 431 |
| 432 // Wait on release fence before delivering the result buffer to client. |
| 433 if (partial_result.buffer->release_fence.is_valid()) { |
| 434 const int kSyncWaitTimeoutMs = 1000; |
| 435 mojo::edk::ScopedPlatformHandle fence; |
| 436 MojoResult result = mojo::edk::PassWrappedPlatformHandle( |
| 437 partial_result.buffer->release_fence.release().value(), &fence); |
| 438 if (result != MOJO_RESULT_OK) { |
| 439 device_context_->SetErrorState(FROM_HERE, |
| 440 "Failed to unwrap release fence fd"); |
| 441 return; |
| 442 } |
| 443 if (!sync_wait(fence.get().handle, kSyncWaitTimeoutMs)) { |
| 444 device_context_->SetErrorState(FROM_HERE, |
| 445 "Sync wait on release fence timed out"); |
| 446 return; |
| 447 } |
| 448 } |
| 449 |
| 450 // Deliver the captured data to client and then re-queue the buffer. |
| 451 if (partial_result.buffer->status != |
| 452 arc::mojom::Camera3BufferStatus::CAMERA3_BUFFER_STATUS_ERROR) { |
| 453 const base::SharedMemory* shm_buffer = |
| 454 stream_context_->buffers[buffer_id].get(); |
| 455 device_context_->SubmitCapturedData( |
| 456 reinterpret_cast<uint8_t*>(shm_buffer->memory()), |
| 457 shm_buffer->mapped_size(), stream_context_->capture_format, |
| 458 partial_result.reference_time, partial_result.timestamp); |
| 459 } |
| 460 stream_context_->free_buffers.push(buffer_id); |
| 461 partial_results_.erase(frame_number); |
| 462 RegisterBuffer(); |
| 463 } |
| 464 |
| 465 } // namespace media |
| OLD | NEW |