blob: 7c4bed4d8fba4484d5d1ea5e84a0a1a28f29954f [file] [log] [blame]
Magnus Jedvert95140712018-11-15 12:07:32 +01001/*
2 * Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11package org.webrtc;
12
13/**
14 * The TimestampAligner class helps translating camera timestamps into the same timescale as is
15 * used by rtc::TimeNanos(). Some cameras have built in timestamping which is more accurate than
16 * reading the system clock, but using a different epoch and unknown clock drift. Frame timestamps
17 * in webrtc should use rtc::TimeNanos (system monotonic time), and this class provides a filter
18 * which lets us use the rtc::TimeNanos timescale, and at the same time take advantage of higher
19 * accuracy of the camera clock. This class is a wrapper on top of rtc::TimestampAligner.
20 */
21public class TimestampAligner {
22 /**
23 * Wrapper around rtc::TimeNanos(). This is normally same as System.nanoTime(), but call this
24 * function to be safe.
25 */
26 public static long getRtcTimeNanos() {
27 return nativeRtcTimeNanos();
28 }
29
30 private volatile long nativeTimestampAligner = nativeCreateTimestampAligner();
31
32 /**
33 * Translates camera timestamps to the same timescale as is used by rtc::TimeNanos().
34 * |cameraTimeNs| is assumed to be accurate, but with an unknown epoch and clock drift. Returns
35 * the translated timestamp.
36 */
37 public long translateTimestamp(long cameraTimeNs) {
38 checkNativeAlignerExists();
39 return nativeTranslateTimestamp(nativeTimestampAligner, cameraTimeNs);
40 }
41
42 /** Dispose native timestamp aligner. */
43 public void dispose() {
44 checkNativeAlignerExists();
45 nativeReleaseTimestampAligner(nativeTimestampAligner);
46 nativeTimestampAligner = 0;
47 }
48
49 private void checkNativeAlignerExists() {
50 if (nativeTimestampAligner == 0) {
51 throw new IllegalStateException("TimestampAligner has been disposed.");
52 }
53 }
54
55 private static native long nativeRtcTimeNanos();
56 private static native long nativeCreateTimestampAligner();
57 private static native void nativeReleaseTimestampAligner(long timestampAligner);
58 private static native long nativeTranslateTimestamp(long timestampAligner, long cameraTimeNs);
59}