test-got-playback-frames.py
Go to the documentation of this file.
1 # License: Apache 2.0. See LICENSE file in root directory.
2 # Copyright(c) 2021 Intel Corporation. All Rights Reserved.
3 
4 #test:device L500*
5 #test:device D400*
6 
7 import pyrealsense2 as rs, os, time, tempfile, sys
8 from rspy import devices, log, test
9 
10 cp = dp = None
11 color_format = depth_format = None
12 color_fps = depth_fps = None
13 color_width = depth_width = None
14 color_height = depth_height = None
15 previous_depth_frame_number = -1
16 previous_color_frame_number = -1
17 got_frames = False
18 
19 dev = test.find_first_device_or_exit()
20 depth_sensor = dev.first_depth_sensor()
21 color_sensor = dev.first_color_sensor()
22 
23 # finding the wanted profile settings. We want to use default settings except for color fps where we want
24 # the lowest value available
25 for p in color_sensor.profiles:
26  if p.is_default() and p.stream_type() == rs.stream.color:
27  color_format = p.format()
28  color_fps = p.fps()
29  color_width = p.as_video_stream_profile().width()
30  color_height = p.as_video_stream_profile().height()
31  break
32 for p in color_sensor.profiles:
33  if p.stream_type() == rs.stream.color and p.format() == color_format and \
34  p.fps() < color_fps and\
35  p.as_video_stream_profile().width() == color_width and \
36  p.as_video_stream_profile().height() == color_height:
37  color_fps = p.fps()
38 for p in depth_sensor.profiles:
39  if p.is_default() and p.stream_type() == rs.stream.depth:
40  depth_format = p.format()
41  depth_fps = p.fps()
42  depth_width = p.as_video_stream_profile().width()
43  depth_height = p.as_video_stream_profile().height()
44  break
45 
46 def got_frame():
47  global got_frames
48  got_frames = True
49 
50 def color_frame_call_back( frame ):
51  global previous_color_frame_number
52  got_frame()
53  test.check_frame_drops( frame, previous_color_frame_number )
54  previous_color_frame_number = frame.get_frame_number()
55 
56 def depth_frame_call_back( frame ):
57  global previous_depth_frame_number
58  got_frame()
59  test.check_frame_drops( frame, previous_depth_frame_number )
60  previous_depth_frame_number = frame.get_frame_number()
61 
63  """
64  You can't use the same profile twice, but we need the same profile several times. So this function resets the
65  profiles with the given parameters to allow quick profile creation
66  """
67  global cp, dp, color_sensor, depth_sensor
68  global color_format, color_fps, color_width, color_height
69  global depth_format, depth_fps, depth_width, depth_height
70  cp = next( p for p in color_sensor.profiles if p.fps() == color_fps
71  and p.stream_type() == rs.stream.color
72  and p.format() == color_format
73  and p.as_video_stream_profile().width() == color_width
74  and p.as_video_stream_profile().height() == color_height )
75 
76  dp = next( p for p in depth_sensor.profiles if p.fps() == depth_fps
77  and p.stream_type() == rs.stream.depth
78  and p.format() == p.format() == depth_format
79  and p.as_video_stream_profile().width() == depth_width
80  and p.as_video_stream_profile().height() == depth_height )
81 
82 def stop_pipeline( pipeline ):
83  if pipeline:
84  try:
85  pipeline.stop()
86  except RuntimeError as rte:
87  # if the error Occurred because the pipeline wasn't started we ignore it
88  if str( rte ) != "stop() cannot be called before start()":
89  test.unexpected_exception()
90  except Exception:
91  test.unexpected_exception()
92 
93 def stop_sensor( sensor ):
94  if sensor:
95  # if the sensor is already closed get_active_streams returns an empty list
96  if sensor.get_active_streams():
97  try:
98  sensor.stop()
99  except RuntimeError as rte:
100  if str( rte ) != "stop_streaming() failed. UVC device is not streaming!":
101  test.unexpected_exception()
102  except Exception:
103  test.unexpected_exception()
104  sensor.close()
105 
106 # create temporary folder to record to that will be deleted automatically at the end of the script
107 # (requires that no files are being held open inside this directory. Important to not keep any handle open to a file
108 # in this directory, any handle as such must be set to None)
109 temp_dir = tempfile.TemporaryDirectory( prefix='recordings_' )
110 file_name = temp_dir.name + os.sep + 'rec.bag'
111 
112 ################################################################################################
113 test.start("Trying to record and playback using pipeline interface")
114 
115 cfg = pipeline = None
116 try:
117  # creating a pipeline and recording to a file
118  pipeline = rs.pipeline()
119  cfg = rs.config()
120  cfg.enable_record_to_file( file_name )
121  pipeline.start( cfg )
122  time.sleep(3)
123  pipeline.stop()
124  # we create a new pipeline and use it to playback from the file we just recoded to
125  pipeline = rs.pipeline()
126  cfg = rs.config()
127  cfg.enable_device_from_file(file_name)
128  pipeline.start(cfg)
129  # if the record-playback worked we will get frames, otherwise the next line will timeout and throw
130  pipeline.wait_for_frames()
131 except Exception:
132  test.unexpected_exception()
133 finally: # we must remove all references to the file so we can use it again in the next test
134  cfg = None
135  stop_pipeline( pipeline )
136 
137 test.finish()
138 
139 ################################################################################################
140 test.start("Trying to record and playback using sensor interface")
141 
142 recorder = depth_sensor = color_sensor = playback = None
143 try:
144  dev = test.find_first_device_or_exit()
145  recorder = rs.recorder( file_name, dev )
146  depth_sensor = dev.first_depth_sensor()
147  color_sensor = dev.first_color_sensor()
148 
150 
151  depth_sensor.open( dp )
152  depth_sensor.start( lambda f: None )
153  color_sensor.open( cp )
154  color_sensor.start( lambda f: None )
155 
156  time.sleep(3)
157 
158  recorder.pause()
159  recorder = None
160  color_sensor.stop()
161  color_sensor.close()
162  depth_sensor.stop()
163  depth_sensor.close()
164 
165  ctx = rs.context()
166  playback = ctx.load_device( file_name )
167 
168  depth_sensor = playback.first_depth_sensor()
169  color_sensor = playback.first_color_sensor()
170 
172 
173  depth_sensor.open( dp )
174  depth_sensor.start( depth_frame_call_back )
175  color_sensor.open( cp )
176  color_sensor.start( color_frame_call_back )
177 
178  time.sleep(3)
179 
180  # if record and playback worked we will receive frames, the callback functions will be called and got-frames
181  # will be True. If the record and playback failed it will be false
182  test.check( got_frames )
183 except Exception:
184  test.unexpected_exception()
185 finally: # we must remove all references to the file so we can use it again in the next test
186  if recorder:
187  recorder.pause()
188  recorder = None
189  if playback:
190  playback.pause()
191  playback = None
192  stop_sensor( depth_sensor )
193  depth_sensor = None
194  stop_sensor( color_sensor )
195  color_sensor = None
196 
197 test.finish()
198 
199 #####################################################################################################
200 test.start("Trying to record and playback using sensor interface with syncer")
201 
202 try:
203  sync = rs.syncer()
204  dev = test.find_first_device_or_exit()
205  recorder = rs.recorder( file_name, dev )
206  depth_sensor = dev.first_depth_sensor()
207  color_sensor = dev.first_color_sensor()
208 
210 
211  depth_sensor.open( dp )
212  depth_sensor.start( sync )
213  color_sensor.open( cp )
214  color_sensor.start( sync )
215 
216  time.sleep(3)
217 
218  recorder.pause()
219  recorder = None
220  color_sensor.stop()
221  color_sensor.close()
222  depth_sensor.stop()
223  depth_sensor.close()
224 
225  ctx = rs.context()
226  playback = ctx.load_device( file_name )
227 
228  depth_sensor = playback.first_depth_sensor()
229  color_sensor = playback.first_color_sensor()
230 
232 
233  depth_sensor.open( dp )
234  depth_sensor.start( sync )
235  color_sensor.open( cp )
236  color_sensor.start( sync )
237 
238  # if the record-playback worked we will get frames, otherwise the next line will timeout and throw
239  sync.wait_for_frames()
240 except Exception:
241  test.unexpected_exception()
242 finally: # we must remove all references to the file so the temporary folder can be deleted
243  if recorder:
244  recorder.pause()
245  recorder = None
246  if playback:
247  playback.pause()
248  playback = None
249  stop_sensor( depth_sensor )
250  depth_sensor = None
251  stop_sensor( color_sensor )
252  color_sensor = None
253 
254 test.finish()
255 
256 #############################################################################################
257 test.print_results_and_exit()
virtual frame finish(frame f)
void next(auto_any_t cur, type2type< T, C > *)
Definition: foreach.hpp:757


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