|
9 | 9 | import pytest |
10 | 10 |
|
11 | 11 | import dlclivegui.services.video_recorder as vr_mod |
| 12 | +from dlclivegui.utils.timestamps import FrameTimestampMetadata |
12 | 13 |
|
13 | 14 | # ---------------------------- |
14 | 15 | # Helpers |
@@ -228,10 +229,14 @@ def test_stop_writes_timestamps_sidecar_json(patch_writegear, output_path, rgb_f |
228 | 229 | data = json.loads(ts_path.read_text()) |
229 | 230 | assert data["video_file"] == output_path.name |
230 | 231 | assert data["num_frames"] == 2 |
231 | | - assert data["timestamps"] == [10.0, 12.0] |
232 | 232 | assert data["start_time"] == 10.0 |
233 | 233 | assert data["end_time"] == 12.0 |
234 | 234 | assert data["duration_seconds"] == 2.0 |
| 235 | + assert data["schema_version"] == 2 |
| 236 | + assert data["timestamps"] == [10.0, 12.0] |
| 237 | + assert data["timestamp_sources"]["hardware_timestamp"] is None |
| 238 | + assert data["frame_timestamps"][0]["software_timestamp"] == 10.0 |
| 239 | + assert data["frame_timestamps"][1]["software_timestamp"] == 12.0 |
235 | 240 |
|
236 | 241 |
|
237 | 242 | def test_encoder_write_error_sets_encode_error_and_future_writes_raise(patch_writegear, output_path, rgb_frame): |
@@ -418,3 +423,113 @@ def close(self): |
418 | 423 | rec.stop() |
419 | 424 |
|
420 | 425 | assert written[0].shape == (10, 20, 3) |
| 426 | + |
| 427 | + |
| 428 | +class TestVideoRecorderTimestampSidecar: |
| 429 | + def test_stop_writes_software_only_timestamp_sidecar_json( |
| 430 | + self, |
| 431 | + patch_writegear, |
| 432 | + output_path, |
| 433 | + rgb_frame, |
| 434 | + ): |
| 435 | + rec = vr_mod.VideoRecorder(output_path, buffer_size=10) |
| 436 | + rec.start() |
| 437 | + |
| 438 | + rec.write(rgb_frame, timestamp=10.0) |
| 439 | + rec.write(rgb_frame, timestamp=12.0) |
| 440 | + |
| 441 | + wait_until(lambda: len(FakeWriteGear.instances[0].frames) >= 2) |
| 442 | + rec.stop() |
| 443 | + |
| 444 | + ts_path = output_path.with_suffix("").with_suffix(output_path.suffix + "_timestamps.json") |
| 445 | + assert ts_path.exists() |
| 446 | + |
| 447 | + data = json.loads(ts_path.read_text()) |
| 448 | + |
| 449 | + assert data["schema_version"] == 2 |
| 450 | + assert data["video_file"] == output_path.name |
| 451 | + assert data["num_frames"] == 2 |
| 452 | + |
| 453 | + # Backward-compatible list. |
| 454 | + assert data["timestamps"] == [10.0, 12.0] |
| 455 | + |
| 456 | + assert data["timestamp_sources"]["software_timestamp"]["kind"] == "software_wall_clock" |
| 457 | + assert data["timestamp_sources"]["hardware_timestamp"] is None |
| 458 | + |
| 459 | + assert data["frame_timestamps"] == [ |
| 460 | + { |
| 461 | + "frame_index": 0, |
| 462 | + "software_timestamp": 10.0, |
| 463 | + }, |
| 464 | + { |
| 465 | + "frame_index": 1, |
| 466 | + "software_timestamp": 12.0, |
| 467 | + }, |
| 468 | + ] |
| 469 | + |
| 470 | + def test_stop_writes_hardware_timestamp_metadata_sidecar_json( |
| 471 | + self, |
| 472 | + patch_writegear, |
| 473 | + output_path, |
| 474 | + rgb_frame, |
| 475 | + ): |
| 476 | + rec = vr_mod.VideoRecorder(output_path, buffer_size=10) |
| 477 | + rec.start() |
| 478 | + |
| 479 | + meta = FrameTimestampMetadata( |
| 480 | + source="grab_result.GetTimeStamp", |
| 481 | + backend="basler", |
| 482 | + default_reported="seconds", |
| 483 | + seconds=0.001, |
| 484 | + raw_value=1_000_000, |
| 485 | + raw_unit="ticks", |
| 486 | + tick_frequency_hz=1_000_000_000.0, |
| 487 | + timebase="Basler camera timestamp counter", |
| 488 | + kind="camera_clock", |
| 489 | + ) |
| 490 | + |
| 491 | + rec.write(rgb_frame, timestamp=10.0, timestamp_metadata=meta) |
| 492 | + |
| 493 | + wait_until(lambda: len(FakeWriteGear.instances[0].frames) >= 1) |
| 494 | + rec.stop() |
| 495 | + |
| 496 | + ts_path = output_path.with_suffix("").with_suffix(output_path.suffix + "_timestamps.json") |
| 497 | + assert ts_path.exists() |
| 498 | + |
| 499 | + data = json.loads(ts_path.read_text()) |
| 500 | + |
| 501 | + assert data["schema_version"] == 2 |
| 502 | + assert data["video_file"] == output_path.name |
| 503 | + assert data["num_frames"] == 1 |
| 504 | + |
| 505 | + # Backward-compatible software timestamp list. |
| 506 | + assert data["timestamps"] == [10.0] |
| 507 | + assert data["start_time"] == 10.0 |
| 508 | + assert data["end_time"] == 10.0 |
| 509 | + assert data["duration_seconds"] == 0.0 |
| 510 | + |
| 511 | + # Static hardware source metadata is written once. |
| 512 | + hw_source = data["timestamp_sources"]["hardware_timestamp"] |
| 513 | + assert hw_source == { |
| 514 | + "source": "grab_result.GetTimeStamp", |
| 515 | + "backend": "basler", |
| 516 | + "default_reported": "seconds", |
| 517 | + "raw_unit": "ticks", |
| 518 | + "tick_frequency_hz": 1_000_000_000.0, |
| 519 | + "timebase": "Basler camera timestamp counter", |
| 520 | + "kind": "camera_clock", |
| 521 | + "extra": {}, |
| 522 | + } |
| 523 | + |
| 524 | + # Per-frame records contain only per-frame values. |
| 525 | + frame_ts = data["frame_timestamps"] |
| 526 | + assert len(frame_ts) == 1 |
| 527 | + |
| 528 | + rec0 = frame_ts[0] |
| 529 | + assert rec0["frame_index"] == 0 |
| 530 | + assert rec0["software_timestamp"] == 10.0 |
| 531 | + assert rec0["hardware_timestamp"] == { |
| 532 | + "seconds": 0.001, |
| 533 | + "raw_value": 1_000_000, |
| 534 | + } |
| 535 | + assert rec0["hardware_timestamp_default"] == 0.001 |
0 commit comments