blob: 83e34daa617695f40e1e70c6b669d22a04721b44 [file] [log] [blame]
// 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 <memory>
#include <string>
#include "base/sequence_checker.h"
#include "media/capture/video/video_capture_device.h"
namespace media {
// A class storing the context of a running CameraDeviceDelegate.
// The class is also used to forward/translate events and method calls to a
// given VideoCaptureDevice::Client.
class CAPTURE_EXPORT CameraDeviceContext {
// The internal state of the running CameraDeviceDelegate. The state
// transition happens when the corresponding methods are called inside
// CameraDeviceDelegate.
enum class State {
// The camera device is completely stopped. This is the initial state, and
// is also set in OnClosed().
// The camera device is starting and waiting to be initialized.
// The kStarting state is set in AllocateAndStart().
// The camera device is initialized and can accept stream configuration
// requests.
// The state is transitioned to kInitialized through:
// |hal_delegate_|->GetCameraInfo() -> OnGotCameraInfo() ->
// |hal_delegate_|->OpenDevice() -> OnOpenedDevice() ->
// Initialize() -> OnInitialized()
// The various capture streams are configured and the camera device is ready
// to process capture requests.
// The state is transitioned to kStreamConfigured through:
// ConfigureStreams() -> OnConfiguredStreams()
// The camera device is capturing video streams.
// The state is transitioned to kCapturing through:
// ConstructDefaultRequestSettings() ->
// OnConstructedDefaultRequestSettings() ->
// |stream_buffer_manager_|->StartPreview()
// In the kCapturing state the |stream_buffer_manager_| runs the capture
// loop to send capture requests and process capture results.
// When the camera device is in the kCapturing state, a capture loop is
// constantly running in |stream_buffer_manager_|:
// On the StreamBufferManager side, we register and submit a capture
// request whenever a free buffer is available:
// RegisterBuffer() -> OnRegisteredBuffer() ->
// ProcessCaptureRequest() -> OnProcessedCaptureRequest()
// We get various capture metadata and error notifications from the camera
// HAL through the following callbacks:
// ProcessCaptureResult()
// Notify()
// The camera device is going through the shut down process; in order to
// avoid race conditions, no new Mojo messages may be sent to camera HAL in
// the kStopping state.
// The kStopping state is set in StopAndDeAllocate().
// The camera device encountered an unrecoverable error and needs to be
// StopAndDeAllocate()'d.
// The kError state is set in SetErrorState().
explicit CameraDeviceContext(
std::unique_ptr<VideoCaptureDevice::Client> client);
void SetState(State state);
State GetState();
// Sets state to kError and call |client_->OnError| to tear down the
// VideoCaptureDevice.
void SetErrorState(media::VideoCaptureError error,
const base::Location& from_here,
const std::string& reason);
// Logs |message| to |client_|.
void LogToClient(std::string message);
// Submits the capture data to |client_->OnIncomingCapturedData|.
void SubmitCapturedData(gfx::GpuMemoryBuffer* buffer,
const VideoCaptureFormat& frame_format,
base::TimeTicks reference_time,
base::TimeDelta timestamp);
void SetSensorOrientation(int sensor_orientation);
void SetScreenRotation(int screen_rotation);
int GetCameraFrameOrientation();
friend class StreamBufferManagerTest;
// The state the CameraDeviceDelegate currently is in.
State state_;
// Clockwise angle through which the output image needs to be rotated to be
// upright on the device screen in its native orientation. This value should
// be 0, 90, 180, or 270.
int sensor_orientation_;
// Clockwise screen rotation in degrees. This value should be 0, 90, 180, or
// 270.
int screen_rotation_;
std::unique_ptr<VideoCaptureDevice::Client> client_;
} // namespace media