@@ -39,7 +39,12 @@ void IRToolTracking::initializeFromFile(const std::string& file) {
3939 std::cerr << " Failed to open file " << file << std::endl;
4040 return ;
4141 }
42- k4a_playback_get_calibration (playback, &calibration);
42+ result = k4a_playback_get_calibration (playback, &calibration);
43+ if (result != K4A_RESULT_SUCCEEDED)
44+ {
45+ std::cerr << " Failed to get calibration" << std::endl;
46+ return ;
47+ }
4348 Terminated = false ;
4449 playFromFile = true ;
4550
@@ -68,7 +73,6 @@ void IRToolTracking::initialize(int index, int width, int height) {
6873 config.depth_mode = K4A_DEPTH_MODE_WFOV_2X2BINNED;
6974 config.color_resolution = K4A_COLOR_RESOLUTION_OFF;
7075 // Retrive calibration
71- calibration;
7276 if (K4A_RESULT_SUCCEEDED != k4a_device_get_calibration (device, config.depth_mode , config.color_resolution , &calibration))
7377 {
7478 std::cout << " Failed to get calibration" << std::endl;
@@ -112,10 +116,13 @@ void IRToolTracking::processStreams() {
112116 if (Terminated)
113117 return ;
114118 // Start the pipeline
115- if (K4A_RESULT_SUCCEEDED != k4a_device_start_cameras (device, &config) )
119+ if (!playFromFile )
116120 {
117- std::cout<<" Failed to start device" <<std::endl;
118- return ;
121+ if (K4A_RESULT_SUCCEEDED != k4a_device_start_cameras (device, &config))
122+ {
123+ std::cout << " Failed to start device" << std::endl;
124+ return ;
125+ }
119126 }
120127
121128 k4a_capture_t capture = NULL ;
@@ -128,20 +135,39 @@ void IRToolTracking::processStreams() {
128135
129136 // Continuously capture frames and process them
130137 while (!Terminated) {
131- // Get a depth frame
132- switch (k4a_device_get_capture (device, &capture, TIMEOUT_IN_MS))
133- {
134- case K4A_WAIT_RESULT_SUCCEEDED:
135- break ;
136- case K4A_WAIT_RESULT_TIMEOUT:
137- std::cout<<" Timed out waiting for a capture" <<std::endl;
138- continue ;
139- break ;
140- case K4A_WAIT_RESULT_FAILED:
141- std::cout<<" Failed to read a capture" <<std::endl;
142- k4a_device_close (device);
143- return ;
144- }
138+
139+ if (playFromFile)
140+ {
141+ switch (k4a_playback_get_next_capture (playback, &capture))
142+ {
143+ case K4A_STREAM_RESULT_SUCCEEDED:
144+ break ;
145+ case K4A_STREAM_RESULT_FAILED:
146+ std::cout << " Timed out waiting for a capture" << std::endl;
147+ continue ;
148+ break ;
149+ case K4A_STREAM_RESULT_EOF:
150+ std::cout << " Reached end of the file" << std::endl;
151+ return ;
152+ }
153+ }
154+ else
155+ {
156+ // Get a depth frame
157+ switch (k4a_device_get_capture (device, &capture, TIMEOUT_IN_MS))
158+ {
159+ case K4A_WAIT_RESULT_SUCCEEDED:
160+ break ;
161+ case K4A_WAIT_RESULT_TIMEOUT:
162+ std::cout << " Timed out waiting for a capture" << std::endl;
163+ continue ;
164+ break ;
165+ case K4A_WAIT_RESULT_FAILED:
166+ std::cout << " Failed to read a capture" << std::endl;
167+ k4a_device_close (device);
168+ return ;
169+ }
170+ }
145171
146172 // Retrieve depth image
147173 depth_image = k4a_capture_get_depth_image (capture);
@@ -192,6 +218,12 @@ void IRToolTracking::processStreams() {
192218 k4a_image_release (depth_image);
193219 k4a_image_release (ir_image);
194220 k4a_capture_release (capture);
221+
222+ if (playFromFile)
223+ {
224+ // Sleep for 33ms to simulate 30fps
225+ std::this_thread::sleep_for (std::chrono::milliseconds (33 ));
226+ }
195227 }
196228}
197229
@@ -201,6 +233,10 @@ void IRToolTracking::shutdown() {
201233 {
202234 k4a_device_close (device);
203235 }
236+ if (playback != NULL )
237+ {
238+ k4a_playback_close (playback);
239+ }
204240 trackingFrame.release ();
205241 depthFrame.release ();
206242}
0 commit comments