-
Notifications
You must be signed in to change notification settings - Fork 360
/
test_record.py
294 lines (271 loc) · 10.8 KB
/
test_record.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# vim: set et sw=4 sts=4 fileencoding=utf-8:
#
# Python camera library for the Rasperry-Pi camera module
# Copyright (c) 2013,2014 Dave Hughes <dave@waveform.org.uk>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import (
unicode_literals,
print_function,
division,
absolute_import,
)
# Make Py2's str equivalent to Py3's
str = type('')
import os
import time
import tempfile
import picamera
import pytest
from collections import namedtuple
from verify import verify_video, verify_image, RAW_FORMATS
RecordingCase = namedtuple('RecordingCase', ('format', 'ext', 'options'))
RECORDING_CASES = (
RecordingCase('h264', '.h264', {'profile': 'baseline'}),
RecordingCase('h264', '.h264', {'profile': 'high'}),
RecordingCase('h264', '.h264', {'resize': (640, 480)}),
RecordingCase('h264', '.h264', {'bitrate': 0, 'quality': 20}),
RecordingCase('h264', '.h264', {'bitrate': 1000000, 'quality': 40}),
RecordingCase('h264', '.h264', {'bitrate': 10000000, 'intra_period': 0}),
RecordingCase('h264', '.h264', {'bitrate': 10000000, 'intra_period': 15}),
RecordingCase('h264', '.h264', {'bitrate': 10000000, 'inline_headers': False}),
RecordingCase('h264', '.h264', {'bitrate': 10000000, 'sei': True}),
RecordingCase('h264', '.h264', {'bitrate': 15000000}),
RecordingCase('h264', '.h264', {'bitrate': 20000000, 'profile': 'main'}),
RecordingCase('mjpeg', '.mjpg', {}),
RecordingCase('mjpeg', '.mjpg', {'bitrate': 10000000}),
RecordingCase('mjpeg', '.mjpg', {'bitrate': 0, 'quality': 20}),
) + tuple(
RecordingCase(fmt, '.data', {})
for fmt in RAW_FORMATS
if not fmt.startswith('bgr')
)
@pytest.fixture(params=RECORDING_CASES)
def filenames_format_options(request):
filename1 = tempfile.mkstemp(suffix=request.param.ext)[1]
filename2 = tempfile.mkstemp(suffix=request.param.ext)[1]
def fin():
os.unlink(filename1)
os.unlink(filename2)
request.addfinalizer(fin)
return filename1, filename2, request.param.format, request.param.options
# Run tests with a variety of format specs
@pytest.fixture(params=RECORDING_CASES)
def format_options(request):
return request.param.format, request.param.options
def expected_failures(resolution, format, options):
if resolution == (2592, 1944) and 'resize' not in options:
pytest.xfail('Cannot encode video at max resolution')
if resolution[1] > 1080 and format == 'mjpeg':
pytest.xfail('Locks up camera')
def test_record_to_file(camera, previewing, mode, filenames_format_options):
filename1, filename2, format, options = filenames_format_options
resolution, framerate = mode
expected_failures(resolution, format, options)
camera.start_recording(
filename1,
# Check that in the case of cooked formats, capture correctly
# derives the format from the extension
format=format if format in RAW_FORMATS else None,
**options)
try:
camera.wait_recording(1)
verify2 = (
format != 'h264' or (
options.get('inline_headers', True) and
options.get('intra_period', 1)
)
)
if verify2:
camera.split_recording(filename2)
camera.wait_recording(1)
else:
with pytest.raises(picamera.PiCameraRuntimeError):
camera.split_recording(filename2)
finally:
camera.stop_recording()
if 'resize' in options:
resolution = options['resize']
verify_video(filename1, format, resolution)
if verify2:
verify_video(filename2, format, resolution)
def test_record_to_stream(camera, previewing, mode, format_options):
format, options = format_options
resolution, framerate = mode
expected_failures(resolution, format, options)
stream1 = tempfile.SpooledTemporaryFile()
stream2 = tempfile.SpooledTemporaryFile()
camera.start_recording(stream1, format, **options)
try:
camera.wait_recording(1)
verify2 = (
format != 'h264' or (
options.get('inline_headers', True) and
options.get('intra_period', 1)
)
)
if verify2:
camera.split_recording(stream2)
camera.wait_recording(1)
else:
with pytest.raises(picamera.PiCameraRuntimeError):
camera.split_recording(stream2)
finally:
camera.stop_recording()
stream1.seek(0)
if 'resize' in options:
resolution = options['resize']
verify_video(stream1, format, resolution)
if verify2:
stream2.seek(0)
verify_video(stream2, format, resolution)
def test_record_sequence_to_file(camera, mode, tempdir):
resolution, framerate = mode
expected_failures(resolution, 'h264', {})
filenames = [os.path.join(tempdir, 'clip%d.h264' % i) for i in range(3)]
for filename in camera.record_sequence(filenames):
camera.wait_recording(1)
for filename in filenames:
verify_video(filename, 'h264', resolution)
def test_record_sequence_to_stream(camera, mode):
resolution, framerate = mode
expected_failures(resolution, 'h264', {})
streams = [tempfile.SpooledTemporaryFile() for i in range(3)]
for stream in camera.record_sequence(streams):
camera.wait_recording(1)
for stream in streams:
stream.seek(0)
verify_video(stream, 'h264', resolution)
def test_circular_record(camera, mode):
resolution, framerate = mode
expected_failures(resolution, 'h264', {})
stream = picamera.PiCameraCircularIO(camera, seconds=4)
camera.start_recording(stream, format='h264')
try:
# Keep recording until the stream is definitely full and starts
# removing earlier bits, or until 20 seconds
start = time.time()
while stream._length < stream._size and time.time() - start < 20:
camera.wait_recording(1)
# Record one more second, then test the result
camera.wait_recording(1)
finally:
camera.stop_recording()
temp = tempfile.SpooledTemporaryFile()
for frame in stream.frames:
if frame.header:
stream.seek(frame.position)
break
while True:
buf = stream.read1()
if not buf:
break
temp.write(buf)
temp.seek(0)
verify_video(temp, 'h264', resolution)
def test_split_and_capture(camera, mode):
resolution, framerate = mode
expected_failures(resolution, 'h264', {})
v_stream1 = tempfile.SpooledTemporaryFile()
v_stream2 = tempfile.SpooledTemporaryFile()
c_stream1 = tempfile.SpooledTemporaryFile()
camera.start_recording(v_stream1, format='h264')
try:
camera.wait_recording(1)
camera.capture(c_stream1, format='jpeg', use_video_port=True)
camera.split_recording(v_stream2)
camera.wait_recording(1)
finally:
camera.stop_recording()
v_stream1.seek(0)
v_stream2.seek(0)
c_stream1.seek(0)
verify_image(c_stream1, 'jpeg', resolution)
verify_video(v_stream1, 'h264', resolution)
verify_video(v_stream2, 'h264', resolution)
def test_multi_res_record(camera, mode):
resolution, framerate = mode
expected_failures(resolution, 'h264', {})
v_stream1 = tempfile.SpooledTemporaryFile()
v_stream2 = tempfile.SpooledTemporaryFile()
new_res = (resolution[0] // 2, resolution[1] // 2)
camera.start_recording(v_stream1, format='h264')
try:
camera.start_recording(v_stream2, format='h264', resize=new_res, splitter_port=2)
try:
camera.wait_recording(1)
camera.wait_recording(1, splitter_port=2)
finally:
camera.stop_recording()
finally:
# Deliberately stop out of order to test #105
camera.stop_recording(splitter_port=2)
v_stream1.seek(0)
v_stream2.seek(0)
verify_video(v_stream1, 'h264', resolution)
verify_video(v_stream2, 'h264', new_res)
class SizeTest(object):
def __init__(self):
self.size = 0
def write(self, s):
self.size += len(s)
def test_multi_res_record_len(camera, mode):
resolution, framerate = mode
expected_failures(resolution, 'h264', {})
output1 = SizeTest()
output2 = SizeTest()
camera.start_recording(output1, 'h264')
try:
camera.start_recording(output2, 'h264', splitter_port=2)
try:
camera.wait_recording(1)
finally:
camera.stop_recording(splitter_port=2)
camera.wait_recording(1)
finally:
camera.stop_recording()
# output1's size should be approximately twice output2's; we give it a bit
# of leeway here (50%) in the test as waiting for an I-frame in slower
# framerates can lead to a failure otherwise
assert output1.size > (output2.size * 1.5)
class MotionTest(object):
def __init__(self, camera):
width, height = camera.resolution
self.rows = (height + 15) // 16
self.cols = (width + 15) // 16
self.cols += 1
def write(self, s):
assert len(s) == self.cols * self.rows * 4
return len(s)
def test_motion_record(camera, mode):
resolution, framerate = mode
expected_failures(resolution, 'h264', {})
camera.start_recording(
'/dev/null', format='h264',
motion_output=MotionTest(camera))
try:
camera.wait_recording(1)
finally:
camera.stop_recording()