def test_camera_stream_frames_trunc_left(): camera = mock.Mock() encoder = mock.Mock() camera._encoders = {1: encoder} stream = PiCameraCircularIO(camera, size=10) frames = [] for data, frame in generate_frames('hkffkffhkff'): encoder.frame = frame if frame.complete: frames.append(frame) stream.write(data) del frames[:3] # As we've gotten rid of the start of the stream we need to re-calc the # video and split sizes in the comparison meta-data sizes = accumulate(f.frame_size for f in frames) frames = [ PiVideoFrame( f.index, f.frame_type, f.frame_size, size, size, f.timestamp, f.complete ) for f, size in zip(frames, sizes) ] assert stream.getvalue() == b'fkkffhkkff' assert list(stream.frames) == frames assert list(reversed(stream.frames)) == frames[::-1]
def test_camera_stream_init(): camera = mock.Mock() encoder = mock.Mock() camera._encoders = {1: encoder} with pytest.raises(ValueError): PiCameraCircularIO(None, size=1) with pytest.raises(ValueError): PiCameraCircularIO(camera) with pytest.raises(ValueError): PiCameraCircularIO(camera, size=1, seconds=1) assert PiCameraCircularIO(camera, seconds=1, bitrate=1024).size == 1024 // 8
def test_camera_stream_frames(): camera = mock.Mock() encoder = mock.Mock() camera._encoders = {1: encoder} stream = PiCameraCircularIO(camera, size=10) frames = [] for data, frame in generate_frames('hkffkff'): encoder.frame = frame if frame.complete: frames.append(frame) stream.write(data) assert stream.getvalue() == b'hkkffkkff' assert list(stream.frames) == frames assert list(reversed(stream.frames)) == frames[::-1]
def test_camera_stream_copy_frames(): camera = mock.Mock() encoder = mock.Mock() camera._encoders = {1: encoder} stream = PiCameraCircularIO(camera, size=10) for data, frame in generate_frames('hkffkff'): encoder.frame = frame stream.write(data) output = io.BytesIO() stream.copy_to(output, frames=1) assert output.getvalue() == b'' stream.copy_to(output, frames=10) assert output.getvalue() == b'hkkffkkff'
def test_camera_stream_clear(): camera = mock.Mock() encoder = mock.Mock() camera._encoders = {1: encoder} stream = PiCameraCircularIO(camera, size=10) for data, frame in generate_frames('hkffkff'): encoder.frame = frame stream.write(data) stream.clear() assert stream.getvalue() == b'' assert list(stream.frames) == [] assert list(reversed(stream.frames)) == []
def test_camera_stream_frames_trunc_right(): # We don't officially support this but the code should work if entire # frames are truncated (without leaving partial frame data) which is what # we're testing for here (of course, the resulting H.264 stream won't be # valid, but we're not testing that...) camera = mock.Mock() encoder = mock.Mock() camera._encoders = {1: encoder} stream = PiCameraCircularIO(camera, size=10) frames = [] for data, frame in generate_frames('hkffkff'): encoder.frame = frame if frame.complete: frames.append(frame) stream.write(data) stream.seek(7) stream.truncate() del frames[-2:] assert stream.getvalue() == b'hkkffkk' assert list(stream.frames) == frames assert list(reversed(stream.frames)) == frames[::-1]
def test_camera_stream_copy_bad(): camera = mock.Mock() encoder = mock.Mock() camera._encoders = {1: encoder} with pytest.raises(ValueError): PiCameraCircularIO(camera, size=10).copy_to('foo', size=1000, seconds=10)