Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1052)

Unified Diff: media/capture/video/chromeos/camera_device_delegate.cc

Issue 2837273004: media: add video capture device for ARC++ camera HAL v3 (Closed)
Patch Set: add more device delegate test cases Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/capture/video/chromeos/camera_device_delegate.cc
diff --git a/media/capture/video/chromeos/camera_device_delegate.cc b/media/capture/video/chromeos/camera_device_delegate.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d1959a223f91e6788eae993cef953dfb0f5b9581
--- /dev/null
+++ b/media/capture/video/chromeos/camera_device_delegate.cc
@@ -0,0 +1,372 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/capture/video/chromeos/camera_device_delegate.h"
+
+#include "media/base/bind_to_current_loop.h"
+#include "media/capture/video/chromeos/camera_hal_delegate.h"
+#include "media/capture/video/chromeos/camera_metadata_utils.h"
+#include "media/capture/video/chromeos/pixel_format_utils.h"
+#include "mojo/edk/embedder/embedder.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
+#include "third_party/libsync/include/sync/sync.h"
+
+namespace media {
+
+CameraDeviceDelegate::CameraDeviceDelegate(
+ VideoCaptureDeviceDescriptor device_descriptor,
+ scoped_refptr<CameraHalDelegate> camera_hal_delegate,
+ scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner)
+ : device_descriptor_(device_descriptor),
+ camera_hal_delegate_(std::move(camera_hal_delegate)),
+ rotation_(0),
+ state_(kStopped),
+ ipc_task_runner_(std::move(ipc_task_runner)) {}
+
+void CameraDeviceDelegate::AllocateAndStart(
+ const VideoCaptureParams& params,
+ std::unique_ptr<VideoCaptureDevice::Client> client) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ DCHECK(!client_);
+ DCHECK_EQ(state_, kStopped);
+
+ chrome_capture_params_ = params;
+ client_ = std::move(client);
+ SetState(kStarting);
+
+ int32_t camera_id = std::stoi(device_descriptor_.device_id);
+ // We need to get the static camera metadata of the camera deivce first.
+ camera_hal_delegate_->GetCameraInfo(
+ camera_id, BindToCurrentLoop(
+ base::Bind(&CameraDeviceDelegate::OnGotCameraInfo, this)));
+}
+
+void CameraDeviceDelegate::StopAndDeAllocate() {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ // StopAndDeAllocate may be called at any state except kStopping.
+ DCHECK_NE(state_, kStopping);
+
+ if (state_ == kStopped) {
+ // In case of Mojo connection error the device may be stopped before
+ // StopAndDeAllocate is called.
+ return;
+ }
+
+ SetState(kStopping);
+ if (!device_ops_.is_bound()) {
+ // The device delegate is in the process of opening the camera device.
+ return;
+ }
+ stream_buffer_manager_->StopCapture();
+ device_ops_->Close(base::Bind(&CameraDeviceDelegate::OnClosed, this));
+}
+
+void CameraDeviceDelegate::TakePhoto(
+ VideoCaptureDevice::TakePhotoCallback callback) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ // TODO(jcliang): Implement TakePhoto.
+}
+
+void CameraDeviceDelegate::GetPhotoCapabilities(
+ VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ // TODO(jcliang): Implement GetPhotoCapabilities.
+}
+
+void CameraDeviceDelegate::SetPhotoOptions(
+ mojom::PhotoSettingsPtr settings,
+ VideoCaptureDevice::SetPhotoOptionsCallback callback) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ // TODO(jcliang): Implement SetPhotoOptions.
+}
+
+void CameraDeviceDelegate::SetRotation(int rotation) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ DCHECK(rotation >= 0 && rotation < 360 && rotation % 90 == 0);
+ rotation_ = rotation;
+}
+
+void CameraDeviceDelegate::SetState(State state) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ state_ = state;
+}
+
+void CameraDeviceDelegate::SetErrorState(
+ const tracked_objects::Location& from_here,
+ const std::string& reason) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ state_ = kError;
+ LOG(ERROR) << reason;
+ client_->OnError(from_here, reason);
+}
+
+void CameraDeviceDelegate::LogToClient(std::string message) {
+ if (client_) {
+ client_->OnLog(message);
+ }
+}
+
+void CameraDeviceDelegate::SubmitCapturedData(
+ const uint8_t* data,
+ int length,
+ const VideoCaptureFormat& frame_format,
+ base::TimeTicks reference_time,
+ base::TimeDelta timestamp) {
+ if (client_) {
+ client_->OnIncomingCapturedData(data, length, frame_format, rotation_,
+ reference_time, timestamp);
+ }
+}
+
+void CameraDeviceDelegate::OnMojoConnectionError() {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+
+ if (state_ == kStopping) {
+ // When in stopping state the camera HAL adapter may terminate the Mojo
+ // channel before we do, in which case the OnClosed callback would not be
+ // called.
+ OnClosed(0);
+ } else {
+ // The Mojo channel terminated unexpectedly.
+ stream_buffer_manager_->StopCapture();
+ ResetMojoInterface();
+ SetState(kStopped);
+ SetErrorState(FROM_HERE, "Mojo connection error");
+ }
+}
+
+void CameraDeviceDelegate::OnClosed(int32_t result) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ DCHECK_EQ(state_, kStopping);
+
+ SetState(kStopped);
+ if (result) {
+ client_->OnLog(std::string("Failed to close device: ") +
+ std::string(strerror(result)));
+ }
+ ResetMojoInterface();
+ client_.reset();
+}
+
+void CameraDeviceDelegate::ResetMojoInterface() {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+
+ device_ops_.reset();
+ stream_buffer_manager_ = nullptr;
+}
+
+void CameraDeviceDelegate::OnGotCameraInfo(
+ int32_t result,
+ arc::mojom::CameraInfoPtr camera_info) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+
+ if (state_ != kStarting) {
+ DCHECK_EQ(state_, kStopping);
+ OnClosed(0);
+ return;
+ }
+
+ if (result) {
+ std::string error_msg = "Failed to get camera info";
+ LOG(ERROR) << error_msg;
+ client_->OnError(FROM_HERE, error_msg);
+ return;
+ }
+ static_metadata_ = std::move(camera_info->static_camera_characteristics);
+ int32_t camera_id = std::stoi(device_descriptor_.device_id);
+ arc::mojom::Camera3DeviceOpsRequest device_ops_request =
+ mojo::MakeRequest(&device_ops_);
+ device_ops_.set_connection_error_handler(
+ base::Bind(&CameraDeviceDelegate::OnMojoConnectionError, this));
+ camera_hal_delegate_->OpenDevice(
+ camera_id, std::move(device_ops_request),
+ BindToCurrentLoop(
+ base::Bind(&CameraDeviceDelegate::OnOpenedDevice, this)));
+}
+
+void CameraDeviceDelegate::OnOpenedDevice(int32_t result) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+
+ if (state_ != kStarting) {
+ DCHECK_EQ(state_, kStopping);
+ OnClosed(0);
+ return;
+ }
+
+ if (result) {
+ std::string error_msg = "Failed to open camera device";
+ LOG(ERROR) << error_msg;
+ client_->OnError(FROM_HERE, error_msg);
+ return;
+ }
+ Initialize();
+}
+
+void CameraDeviceDelegate::Initialize() {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ DCHECK_EQ(state_, kStarting);
+
+ arc::mojom::Camera3CallbackOpsPtr callback_ops_ptr;
+ arc::mojom::Camera3CallbackOpsRequest callback_ops_request =
+ mojo::MakeRequest(&callback_ops_ptr);
+ stream_buffer_manager_ = new StreamBufferManager(
+ std::move(callback_ops_request), this, ipc_task_runner_);
+ device_ops_->Initialize(
+ std::move(callback_ops_ptr),
+ base::Bind(&CameraDeviceDelegate::OnInitialized, this));
+}
+
+void CameraDeviceDelegate::OnInitialized(int32_t result) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+
+ if (state_ != kStarting) {
+ DCHECK_EQ(state_, kStopping);
+ return;
+ }
+ if (result) {
+ SetErrorState(FROM_HERE, std::string("Failed to initialize camera device") +
+ std::string(strerror(result)));
+ return;
+ }
+ SetState(kInitialized);
+ ConfigureStreams();
+}
+
+void CameraDeviceDelegate::ConfigureStreams() {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ DCHECK_EQ(state_, kInitialized);
+
+ // Set up context for preview stream.
+ arc::mojom::Camera3StreamPtr preview_stream =
+ arc::mojom::Camera3Stream::New();
+ preview_stream->id = static_cast<uint64_t>(
+ arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW);
+ preview_stream->stream_type =
+ arc::mojom::Camera3StreamType::CAMERA3_STREAM_OUTPUT;
+ preview_stream->width =
+ chrome_capture_params_.requested_format.frame_size.width();
+ preview_stream->height =
+ chrome_capture_params_.requested_format.frame_size.height();
+ preview_stream->format =
+ arc::mojom::HalPixelFormat::HAL_PIXEL_FORMAT_YCbCr_420_888;
+ preview_stream->data_space = 0;
+ preview_stream->rotation =
+ arc::mojom::Camera3StreamRotation::CAMERA3_STREAM_ROTATION_0;
+
+ arc::mojom::Camera3StreamConfigurationPtr stream_config =
+ arc::mojom::Camera3StreamConfiguration::New();
+ stream_config->streams.push_back(std::move(preview_stream));
+ stream_config->operation_mode = arc::mojom::Camera3StreamConfigurationMode::
+ CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
+ device_ops_->ConfigureStreams(
+ std::move(stream_config),
+ base::Bind(&CameraDeviceDelegate::OnConfiguredStreams, this));
+}
+
+void CameraDeviceDelegate::OnConfiguredStreams(
+ int32_t result,
+ arc::mojom::Camera3StreamConfigurationPtr updated_config) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+
+ if (state_ != kInitialized) {
+ DCHECK_EQ(state_, kStopping);
+ return;
+ }
+ if (result) {
+ SetErrorState(FROM_HERE, std::string("Failed to configure streams: ") +
+ std::string(strerror(result)));
+ return;
+ }
+ if (!updated_config || updated_config->streams.size() != 1) {
+ SetErrorState(FROM_HERE,
+ std::string("Wrong number of streams configured: ") +
+ std::to_string(updated_config->streams.size()));
+ return;
+ }
+
+ VideoCaptureFormat capture_format = chrome_capture_params_.requested_format;
+ // TODO(jcliang): Determine the best format from metadata.
+ capture_format.pixel_format = PIXEL_FORMAT_NV12;
+
+ // The partial result count metadata is optional; defaults to 1 in case it
+ // is not set in the static metadata.
+ uint32_t partial_result_count = 1;
+ const arc::mojom::CameraMetadataEntryPtr* partial_count = GetMetadataEntry(
+ static_metadata_,
+ arc::mojom::CameraMetadataTag::ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
+ if (partial_count) {
+ partial_result_count =
+ *reinterpret_cast<int32_t*>((*partial_count)->data.data());
+ }
+ stream_buffer_manager_->SetUpStreamAndBuffers(
+ capture_format, partial_result_count,
+ std::move(updated_config->streams[0]));
+
+ SetState(kStreamConfigured);
+ ConstructDefaultRequestSettings();
+}
+
+void CameraDeviceDelegate::ConstructDefaultRequestSettings() {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ DCHECK_EQ(state_, kStreamConfigured);
+
+ device_ops_->ConstructDefaultRequestSettings(
+ arc::mojom::Camera3RequestTemplate::CAMERA3_TEMPLATE_PREVIEW,
+ base::Bind(&CameraDeviceDelegate::OnConstructedDefaultRequestSettings,
+ this));
+}
+
+void CameraDeviceDelegate::OnConstructedDefaultRequestSettings(
+ arc::mojom::CameraMetadataPtr settings) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+ DCHECK_EQ(state_, kStreamConfigured);
+
+ if (state_ != kStreamConfigured) {
+ DCHECK_EQ(state_, kStopping);
+ return;
+ }
+ if (!settings) {
+ SetErrorState(FROM_HERE, "Failed to get default request settings");
+ return;
+ }
+ client_->OnStarted();
+ SetState(kCapturing);
+ stream_buffer_manager_->StartCapture(std::move(settings));
+}
+
+void CameraDeviceDelegate::RegisterBuffer(
+ uint64_t buffer_id,
+ arc::mojom::Camera3DeviceOps::BufferType type,
+ std::vector<mojo::ScopedHandle> fds,
+ uint32_t drm_format,
+ arc::mojom::HalPixelFormat hal_pixel_format,
+ uint32_t width,
+ uint32_t height,
+ std::vector<uint32_t> strides,
+ std::vector<uint32_t> offsets,
+ base::OnceCallback<void(int32_t)> callback) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+
+ if (state_ != kCapturing) {
+ DCHECK_EQ(state_, kStopping);
+ return;
+ }
+ device_ops_->RegisterBuffer(
+ buffer_id, type, std::move(fds), drm_format, hal_pixel_format, width,
+ height, std::move(strides), std::move(offsets), std::move(callback));
+}
+
+void CameraDeviceDelegate::ProcessCaptureRequest(
+ arc::mojom::Camera3CaptureRequestPtr request,
+ base::OnceCallback<void(int32_t)> callback) {
+ DCHECK(ipc_task_runner_->BelongsToCurrentThread());
+
+ if (state_ != kCapturing) {
+ DCHECK_EQ(state_, kStopping);
+ return;
+ }
+ device_ops_->ProcessCaptureRequest(std::move(request), std::move(callback));
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698