StreamingStats.java
Go to the documentation of this file.
1 package com.intel.realsense.camera;
2 
3 import android.util.Log;
4 
13 
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.concurrent.atomic.AtomicLongArray;
17 
18 public class StreamingStats {
19  private static final String TAG = "librs camera streamer";
20  private static final String DATA_LOG_TAG = "FRAME_DATA_LOG";
21 
22  private Map<Integer, Statistics> mStreamsMap = new HashMap<>();
23  private Map<Integer, Statistics> mLastFrames = new HashMap<>();
24 
26  String resolution = "";
27  if(profile.is(Extension.VIDEO_PROFILE)){
28  VideoStreamProfile vsp = profile.as(Extension.VIDEO_PROFILE);
29  resolution = vsp.getWidth() + "x" + vsp.getHeight();
30  }
31 
32  String streamType = profile.getType().name();
33  String format = profile.getFormat().name();
34  String res = resolution;
35  String fps = String.valueOf(profile.getFrameRate());
36 
37  String name = streamType + " | " +
38  format + " | " +
39  res + " | " +
40  fps;
41 
42  Statistics stats = new Statistics(name);
43  stats.mStreamType = streamType;
44  stats.mFormat = format;
45  stats.mResolution = res;
46  stats.mRequestedFps = fps;
47 
48  mStreamsMap.put(profile.getUniqueId(), stats);
49  mLastFrames.put(profile.getUniqueId(), new Statistics(name));
50  }
51 
53  @Override
54  public void onFrame(Frame f) {
55  try(StreamProfile profile = f.getProfile()) {
56  int fn = f.getNumber();
57  int uid = profile.getUniqueId();
58  if (!mLastFrames.containsKey(profile.getUniqueId()))
60  if (mLastFrames.get(uid).mFrameNumber != fn) {
62  mStreamsMap.get(uid).mEmitter = String.valueOf(f.getMetadata(FrameMetadata.FRAME_EMITTER_MODE));
64  mStreamsMap.get(uid).mExposure = String.valueOf(f.getMetadata(FrameMetadata.ACTUAL_EXPOSURE));
66  mStreamsMap.get(uid).mAutoExposureMode = String.valueOf(f.getMetadata(FrameMetadata.AUTO_EXPOSURE));
68  mStreamsMap.get(uid).mGain = String.valueOf(f.getMetadata(FrameMetadata.GAIN_LEVEL));
70  mStreamsMap.get(uid).mLaserPower = String.valueOf(f.getMetadata(FrameMetadata.FRAME_LASER_POWER));
72  mStreamsMap.get(uid).mLedPower = String.valueOf(f.getMetadata(FrameMetadata.FRAME_LED_POWER));
73  mStreamsMap.get(uid).mFrameNumber = fn;
74  mStreamsMap.get(uid).mHWTimestamp = f.getTimestamp();
75  mStreamsMap.get(uid).mSWTimestamp = System.currentTimeMillis();
76  mStreamsMap.get(uid).onFrame(f);
77  mLastFrames.put(uid, mStreamsMap.get(uid).clone());
78  }
79  else
80  mStreamsMap.get(uid).kick();
81  }
82  }
83  };
84 
85  public void onFrameset(FrameSet frames){
86  frames.foreach(mFrameCallback);
87  }
88 
89  public String prettyPrint(){
90  String rv = "";
91  for(Map.Entry e : mStreamsMap.entrySet()){
92  rv += ((Statistics)e.getValue()).prettyPrint() + "\n\n";
93  }
94  return rv;
95  }
96 
97  private class Statistics implements Cloneable {
98 
99  private final String mName;
101  private String mFormat;
104  private long mStartTime = 0;
105  private long mBaseTime = 0;
106  private float mFps = 0;
107  private long mFrameCount = 0;
108  private long mFrameLoss = 0;
109  private double mHWTimestamp = 0;
110  private double mHWTimestampDiff = 0;
111  private long mSWTimestamp = 0;
112  private long mSWTimestampDiff = 0;
113  private int mFrameNumber = 0;
114  private long mTotalFrameCount = 0;
115  private long mFirstFrameLatency = 0;
116  private String mEmitter = "No data";
117  private String mExposure = "No data";
118  private String mGain = "No data";
119  private String mAutoExposureMode = "No data";
120  private String mLaserPower = "No data";
121  private String mLedPower = "No data";
122 
124  mName = name;
125  reset();
126  }
127 
128  public Statistics clone() {
129  Statistics clonedStats = null;
130  try {
131  clonedStats = (Statistics) super.clone();
132  } catch (CloneNotSupportedException e) {
133  e.printStackTrace();
134  }
135  return clonedStats;
136  }
137 
138  public void reset(){
139  mStartTime = mBaseTime = System.currentTimeMillis();
140  mFirstFrameLatency = 0;
141  }
142 
143  public float getFps(){
144  return mFps;
145  }
146 
147  public float getFrameCount(){
148  return mTotalFrameCount;
149  }
150 
151  public String prettyPrint(){
152  long curr = System.currentTimeMillis();
153  int diffInSeconds = (int) ((curr - mStartTime) * 0.001);
154  String hwTimestampAsString = String.format("%.3f", mHWTimestamp);
155  return mName +
156  "\nFrame Rate: " + mFps +
157  "\nFrame Count: " + mTotalFrameCount +
158  "\nFrame Number: " + mFrameNumber +
159  "\nFrame Loss: " + mFrameLoss +
160  "\nHW timestamp: " + hwTimestampAsString +
161  "\nSW timestamp: " + mSWTimestamp +
162  "\nRun Time: " + diffInSeconds + " [sec]" +
163  "\nEmitter Mode: " + mEmitter +
164  "\nExposure: " + mExposure;
165  }
166 
167  public synchronized void kick(){
168  long curr = System.currentTimeMillis();
169  float diffInSeconds = (float) ((curr - mBaseTime) * 0.001);
170  if(diffInSeconds > 2){
171  mFps = mFrameCount / diffInSeconds;
172  mFrameCount = 0;
173  mBaseTime = curr;
174  }
175  }
176 
177  public synchronized void onFrame(Frame f){
178  mFrameCount++;
179  mTotalFrameCount++;
180  long curr = System.currentTimeMillis();
181  float diffInSeconds = (float) ((curr - mBaseTime) * 0.001);
182  if(mFirstFrameLatency == 0){
183  mFirstFrameLatency = curr - mBaseTime;
184  }
185 
186  if(diffInSeconds > 2){
187  mFps = mFrameCount / diffInSeconds;
188  mFrameCount = 0;
189  mBaseTime = curr;
190  }
191  mFrameLoss = mFrameNumber - mTotalFrameCount;
192 
193  try(StreamProfile streamProfile = f.getProfile()){
194  Statistics mLastFrame = mLastFrames.get(streamProfile.getUniqueId());
195  mHWTimestampDiff = mHWTimestamp - mLastFrame.mHWTimestamp;
196  mSWTimestampDiff = mSWTimestamp - mLastFrame.mSWTimestamp;
197  }
198  }
199 
200  private void logFrameData() {
201  String hwTimestampAsString = String.format("%.3f", mHWTimestamp);
202  String hwTimestampDiffAsString = String.format("%.3f", mHWTimestampDiff);
203  String data =
204  mStreamType + ", " +
205  mFormat + ", " +
206  mResolution + ", " +
207  mRequestedFps + ", " +
208  mFrameNumber + ", " +
209  hwTimestampAsString + ", " +
210  hwTimestampDiffAsString + ", " +
211  mSWTimestamp + ", " +
212  mSWTimestampDiff + ", " +
213  mAutoExposureMode + ", " +
214  mExposure + ", " +
215  mGain + ", " +
216  mLaserPower + ", " +
217  mEmitter + ", " +
218  mLedPower;
219  Log.i(DATA_LOG_TAG, data);
220  }
221  }
222 }
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
uvc_xu_option< int > super
Definition: l500-options.h:32
GLuint const GLchar * name
long getMetadata(FrameMetadata type)
Definition: Frame.java:50
e
Definition: rmse.py:177
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
GLdouble f
void initStream(StreamProfile profile)
GLint GLint GLsizei GLint GLenum format
Map< Integer, Statistics > mStreamsMap
void foreach(FrameCallback callback)
Definition: FrameSet.java:27
Map< Integer, Statistics > mLastFrames
GLuint res
Definition: glext.h:8856
boolean supportsMetadata(FrameMetadata type)
Definition: Frame.java:48
Definition: parser.hpp:150


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11