1# Copyright 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""CameraITS test for unified timestamp for image and motion sensor events."""
15
16import logging
17import time
18
19
20from mobly import test_runner
21
22import its_base_test
23import camera_properties_utils
24import capture_request_utils
25import its_session_utils
26
27_OPTIONAL_SENSOR_CHECK = ('rv')
28_REQUIRED_SENSOR_CHECK = ('accel', 'gyro', 'mag')
29_SENSOR_EVENTS_WAIT_TIME = 2  # seconds
30
31
32class UnifiedTimeStampTest(its_base_test.ItsBaseTest):
33  """Test if image and motion sensor events are in the same time domain.
34  """
35
36  def test_unified_timestamps(self):
37    with its_session_utils.ItsSession(
38        device_id=self.dut.serial,
39        camera_id=self.camera_id,
40        hidden_physical_id=self.hidden_physical_id) as cam:
41      props = cam.get_camera_properties()
42      props = cam.override_with_hidden_physical_camera_props(props)
43
44      # Only run test if the appropriate properties are claimed.
45      camera_properties_utils.skip_unless(
46          camera_properties_utils.sensor_fusion(props) and
47          camera_properties_utils.backward_compatible(props))
48
49      # Get the timestamp of a captured image.
50      if camera_properties_utils.manual_sensor(props):
51        req, fmt = capture_request_utils.get_fastest_manual_capture_settings(
52            props)
53      else:
54        req, fmt = capture_request_utils.get_fastest_auto_capture_settings(
55            props)
56      cap = cam.do_capture(req, fmt)
57      ts_image0 = cap['metadata']['android.sensor.timestamp']
58
59      # Get the timestamps of motion events.
60      logging.debug('Reading sensor measurements')
61      sensors = cam.get_sensors()
62      cam.start_sensor_events()
63      time.sleep(_SENSOR_EVENTS_WAIT_TIME)  # run sensors to get events
64      events = cam.get_sensor_events()
65      ts_sensor_first = {}
66      ts_sensor_last = {}
67      for sensor, existing in sensors.items():
68      # Vibrator doesn't generate outputs: b/142653973
69        if existing and sensor != 'vibrator':
70          if not events[sensor]:
71            raise AssertionError(f'{sensor} has no events!')
72          ts_sensor_first[sensor] = events[sensor][0]['time']
73          ts_sensor_last[sensor] = events[sensor][-1]['time']
74
75      # Get the timestamp of another image.
76      cap = cam.do_capture(req, fmt)
77      ts_image1 = cap['metadata']['android.sensor.timestamp']
78
79      logging.debug('Image timestamps: %s , %s', ts_image0, ts_image1)
80
81      # The motion timestamps must be between the two image timestamps.
82      for sensor, existing in sensors.items():
83        if existing and sensor != 'vibrator':
84          logging.debug('%s timestamps: %d %d', sensor, ts_sensor_first[sensor],
85                        ts_sensor_last[sensor])
86          if (not ts_image0 < ts_sensor_first[sensor] < ts_image1 or
87              not ts_image0 < ts_sensor_last[sensor] < ts_image1):
88            e_msg = (
89                f'{sensor} times not bounded by camera! camera: '
90                f'{ts_image0}:{ts_image1}, {sensor}: '
91                f'{ts_sensor_first[sensor]}:{ts_sensor_last[sensor]}'
92            )
93            if sensor in _REQUIRED_SENSOR_CHECK:
94              raise AssertionError(e_msg)
95            elif sensor in _OPTIONAL_SENSOR_CHECK:
96              raise AssertionError(
97                  f'{its_session_utils.NOT_YET_MANDATED_MESSAGE}\n\n{e_msg}'
98              )
99
100if __name__ == '__main__':
101  test_runner.main()
102