summaryrefslogtreecommitdiff
path: root/chromium/media/capture/video/chromeos/camera_3a_controller.h
blob: 3a62006b55566d8f3e7da851524bc99a67801ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2018 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.

#ifndef MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_3A_CONTROLLER_H_
#define MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_3A_CONTROLLER_H_

#include <unordered_set>

#include "base/cancelable_callback.h"
#include "media/base/media_export.h"
#include "media/capture/video/chromeos/mojom/camera3.mojom.h"
#include "media/capture/video/chromeos/request_manager.h"

namespace media {

// A class to control the auto-exposure, auto-focus, and auto-white-balancing
// operations and modes of the camera.  For the detailed state transitions for
// auto-exposure, auto-focus, and auto-white-balancing, see
// https://source.android.com/devices/camera/camera3_3Amodes
class CAPTURE_EXPORT Camera3AController
    : public CaptureMetadataDispatcher::ResultMetadataObserver {
 public:
  Camera3AController(const cros::mojom::CameraMetadataPtr& static_metadata,
                     CaptureMetadataDispatcher* capture_metadata_dispatcher,
                     scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  ~Camera3AController() final;

  // Trigger the camera to start exposure, focus, and white-balance metering and
  // lock them for still capture.
  void Stabilize3AForStillCapture(base::OnceClosure on_3a_stabilized_callback);

  // CaptureMetadataDispatcher::ResultMetadataObserver implementation.
  void OnResultMetadataAvailable(
      uint32_t frame_number,
      const cros::mojom::CameraMetadataPtr& result_metadata) final;

  // Enable the auto-focus mode suitable for still capture.
  void SetAutoFocusModeForStillCapture();

  // TODO(shik): This function is unused now.
  // Enable the auto-focus mode suitable for video recording.
  void SetAutoFocusModeForVideoRecording();

  // Set auto white balance mode.
  void SetAutoWhiteBalanceMode(cros::mojom::AndroidControlAwbMode mode);

  bool IsPointOfInterestSupported();

  // Set point of interest. The coordinate system is based on the active
  // pixel array.
  void SetPointOfInterest(gfx::Point point);

  // Updates the availability of Zero-Shutter Lag (ZSL). We skip 3A (AE, AF,
  // AWB) if ZSL is enabled.
  void UpdateZeroShutterLagAvailability(bool enabled);

  base::WeakPtr<Camera3AController> GetWeakPtr();

 private:
  void Set3AMode(cros::mojom::CameraMetadataTag tag, uint8_t target_mode);

  // Sometimes it might take long time to stabilize 3A.  Fire the
  // callback artificially after |time_limit| passed.
  void Set3aStabilizedCallback(base::OnceClosure callback,
                               base::TimeDelta time_limit);

  bool Is3AStabilized();

  // Intermediate steps of setting point of interest.
  void SetPointOfInterestOn3AModeSet();
  void SetPointOfInterestOn3AStabilized();
  void SetPointOfInterestUnlockAe();

  // Helper functions for manipulating metadata.
  template <typename T>
  void SetCaptureMetadata(cros::mojom::CameraMetadataTag tag, T value);

  template <typename T>
  void SetCaptureMetadata(cros::mojom::CameraMetadataTag tag,
                          const std::vector<T>& value);

  template <typename T>
  void SetRepeatingCaptureMetadata(cros::mojom::CameraMetadataTag tag, T value);

  template <typename T>
  void SetRepeatingCaptureMetadata(cros::mojom::CameraMetadataTag tag,
                                   const std::vector<T>& value);

  void ClearRepeatingCaptureMetadata();

  const cros::mojom::CameraMetadataPtr& static_metadata_;
  bool ae_region_supported_;
  bool af_region_supported_;
  bool point_of_interest_supported_;

  CaptureMetadataDispatcher* capture_metadata_dispatcher_;
  const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

  std::unordered_set<cros::mojom::AndroidControlAfMode> available_af_modes_;
  cros::mojom::AndroidControlAfMode af_mode_;
  cros::mojom::AndroidControlAfState af_state_;
  // |af_mode_set_| is set to true when the AF mode is synchronized between
  // the HAL and the Camera3AController.
  bool af_mode_set_;

  std::unordered_set<cros::mojom::AndroidControlAeMode> available_ae_modes_;
  cros::mojom::AndroidControlAeMode ae_mode_;
  cros::mojom::AndroidControlAeState ae_state_;
  // |ae_mode_set_| is set to true when the AE mode is synchronized between
  // the HAL and the Camera3AController.
  bool ae_mode_set_;

  std::unordered_set<cros::mojom::AndroidControlAwbMode> available_awb_modes_;
  cros::mojom::AndroidControlAwbMode awb_mode_;
  cros::mojom::AndroidControlAwbState awb_state_;
  // |awb_mode_set_| is set to true when the AWB mode is synchronized between
  // the HAL and the Camera3AController.
  bool awb_mode_set_;

  bool set_point_of_interest_running_;

  bool ae_locked_for_point_of_interest_;

  bool zero_shutter_lag_enabled_;

  base::TimeDelta latest_sensor_timestamp_;

  std::unordered_set<cros::mojom::CameraMetadataTag> repeating_metadata_tags_;

  // TODO(shik): There are potential races in |on.*callback_| due to processing
  // pipeline.  Here are some possible solutions/mitgations used in GCA:
  // 1. Record the frame number.
  // 2. Wait for some frames before checking.
  // 3. Wait for more complex patterns (like TriggerStateMachine.java in GCA).
  base::OnceClosure on_3a_mode_set_callback_;

  base::OnceClosure on_3a_stabilized_callback_;
  base::TimeDelta artificial_3a_stabilized_deadline_;

  base::OnceClosure on_ae_locked_for_point_of_interest_callback_;

  base::OnceClosure on_af_trigger_cancelled_callback_;

  base::CancelableOnceClosure delayed_ae_unlock_callback_;

  base::WeakPtrFactory<Camera3AController> weak_ptr_factory_{this};

  DISALLOW_IMPLICIT_CONSTRUCTORS(Camera3AController);
};

}  // namespace media

#endif  // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_3A_CONTROLLER_H_