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 15import os.path 16 17import its.caps 18import its.device 19import its.image 20import its.objects 21 22import matplotlib 23from matplotlib import pylab 24 25# PASS/FAIL thresholds 26TEST_FPS = 30 27MIN_AVG_FRAME_DELTA = 30 # at least 30ms delta between frames 28MAX_VAR_FRAME_DELTA = 0.01 # variance of frame deltas 29MAX_FRAME_DELTA_JITTER = 0.3 # max ms gap from the average frame delta 30 31NAME = os.path.basename(__file__).split('.')[0] 32 33 34def main(): 35 """Measure jitter in camera timestamps.""" 36 37 with its.device.ItsSession() as cam: 38 props = cam.get_camera_properties() 39 its.caps.skip_unless(its.caps.manual_sensor(props) and 40 its.caps.sensor_fusion(props)) 41 42 req, fmt = its.objects.get_fastest_manual_capture_settings(props) 43 req["android.control.aeTargetFpsRange"] = [TEST_FPS, TEST_FPS] 44 caps = cam.do_capture([req]*50, [fmt]) 45 46 # Print out the millisecond delta between the start of each exposure 47 tstamps = [c['metadata']['android.sensor.timestamp'] for c in caps] 48 deltas = [tstamps[i]-tstamps[i-1] for i in range(1, len(tstamps))] 49 deltas_ms = [d/1000000.0 for d in deltas] 50 avg = sum(deltas_ms) / len(deltas_ms) 51 var = sum([d*d for d in deltas_ms]) / len(deltas_ms) - avg * avg 52 range0 = min(deltas_ms) - avg 53 range1 = max(deltas_ms) - avg 54 print 'Average:', avg 55 print 'Variance:', var 56 print 'Jitter range:', range0, 'to', range1 57 58 # Draw a plot. 59 pylab.plot(range(len(deltas_ms)), deltas_ms) 60 pylab.title(NAME) 61 pylab.xlabel('frame number') 62 pylab.ylabel('jitter (ms)') 63 matplotlib.pyplot.savefig('%s_deltas.png' % (NAME)) 64 65 # Test for pass/fail. 66 emsg = 'avg: %.4fms, TOL: %.fms' % (avg, MIN_AVG_FRAME_DELTA) 67 assert avg > MIN_AVG_FRAME_DELTA, emsg 68 emsg = 'var: %.4fms, TOL: %.2fms' % (var, MAX_VAR_FRAME_DELTA) 69 assert var < MAX_VAR_FRAME_DELTA, emsg 70 emsg = 'range0: %.4fms, range1: %.4fms, TOL: %.2fms' % ( 71 range0, range1, MAX_FRAME_DELTA_JITTER) 72 assert abs(range0) < MAX_FRAME_DELTA_JITTER, emsg 73 assert abs(range1) < MAX_FRAME_DELTA_JITTER, emsg 74 75if __name__ == '__main__': 76 main() 77 78