[cscore] Use frame time in Linux UsbCameraImpl (#7609)

This commit is contained in:
Matt
2025-01-07 09:33:20 -07:00
committed by GitHub
parent 8e459a4f2a
commit 2de03c9601
13 changed files with 267 additions and 29 deletions

View File

@@ -18,6 +18,8 @@ public class RawFrame implements AutoCloseable {
private int m_height;
private int m_stride;
private PixelFormat m_pixelFormat = PixelFormat.kUnknown;
private long m_time;
private TimestampSource m_timeSource = TimestampSource.kUnknown;
/** Construct a new empty RawFrame. */
public RawFrame() {
@@ -43,12 +45,15 @@ public class RawFrame implements AutoCloseable {
* @param stride The number of bytes in each row of image data
* @param pixelFormat The PixelFormat of the frame
*/
void setDataJNI(ByteBuffer data, int width, int height, int stride, int pixelFormat) {
void setDataJNI(
ByteBuffer data, int width, int height, int stride, int pixelFormat, long time, int timeSrc) {
m_data = data;
m_width = width;
m_height = height;
m_stride = stride;
m_pixelFormat = PixelFormat.getFromInt(pixelFormat);
m_time = time;
m_timeSource = TimestampSource.getFromInt(timeSrc);
}
/**
@@ -59,11 +64,13 @@ public class RawFrame implements AutoCloseable {
* @param stride The number of bytes in each row of image data
* @param pixelFormat The PixelFormat of the frame
*/
void setInfoJNI(int width, int height, int stride, int pixelFormat) {
void setInfoJNI(int width, int height, int stride, int pixelFormat, long time, int timeSrc) {
m_width = width;
m_height = height;
m_stride = stride;
m_pixelFormat = PixelFormat.getFromInt(pixelFormat);
m_time = time;
m_timeSource = TimestampSource.getFromInt(timeSrc);
}
/**
@@ -110,6 +117,19 @@ public class RawFrame implements AutoCloseable {
pixelFormat.getValue());
}
/**
* Update this frame's timestamp info.
*
* @param frameTime the time this frame was grabbed at. This uses the same time base as
* wpi::Now(), in us.
* @param frameTimeSource the time source for the timestamp this frame was grabbed at.
*/
public void setTimeInfo(long frameTime, TimestampSource frameTimeSource) {
m_time = frameTime;
m_timeSource = frameTimeSource;
WPIUtilJNI.setRawFrameTime(m_nativeObj, frameTime, frameTimeSource.getValue());
}
/**
* Get the pointer to native representation of this frame.
*
@@ -185,4 +205,22 @@ public class RawFrame implements AutoCloseable {
public PixelFormat getPixelFormat() {
return m_pixelFormat;
}
/**
* Get the time this frame was grabbed at. This uses the same time base as wpi::Now(), in us.
*
* @return Time in 1 us increments.
*/
public long getTimestamp() {
return m_time;
}
/**
* Get the time source for the timestamp this frame was grabbed at.
*
* @return Time source
*/
public TimestampSource getTimestampSource() {
return m_timeSource;
}
}

View File

@@ -0,0 +1,51 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.util;
/**
* Options for where the timestamp an {@link RawFrame} was captured at can be measured relative to.
*/
public enum TimestampSource {
/** unknown. */
kUnknown(0),
/**
* wpi::Now when the new frame was dequeued by CSCore. Does not account for camera exposure time
* or V4L latency.
*/
kFrameDequeue(1),
/** End of Frame. Same as V4L2_BUF_FLAG_TSTAMP_SRC_EOF, translated into wpi::Now's timebase. */
kV4LEOF(2),
/**
* Start of Exposure. Same as V4L2_BUF_FLAG_TSTAMP_SRC_SOE, translated into wpi::Now's timebase.
*/
kV4LSOE(3);
private final int value;
TimestampSource(int value) {
this.value = value;
}
/**
* Gets the integer value of the pixel format.
*
* @return Integer value
*/
public int getValue() {
return value;
}
private static final TimestampSource[] s_values = values();
/**
* Gets a TimestampSource enum value from its integer value.
*
* @param timestampSource integer value
* @return Enum value
*/
public static TimestampSource getFromInt(int timestampSource) {
return s_values[timestampSource];
}
}

View File

@@ -175,6 +175,8 @@ public class WPIUtilJNI {
static native void setRawFrameInfo(
long frame, int size, int width, int height, int stride, int pixelFormat);
static native void setRawFrameTime(long frame, long timestamp, int timeSource);
/**
* Waits for a handle to be signaled.
*