# 全局记录与回放

为了简化应用的开发以及后续测试和调试，您可以记录并回放测试数据。无需修改代码，只需使用环境变量和配置文件即可实现。您可以使用
[holistic_record.py](https://docs.luxonis.com/software-v3/depthai/examples/record_replay/holistic_record.md) 脚本创建全局记录，然后通过将
DEPTHAI_REPLAY 环境变量设置为记录路径来回放该记录。

全局记录与回放的原理是：记录应用中使用的源节点（[Camera](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/camera.md) 和
[IMU](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/imu.md)）的校准数据、相机特征信息以及数据（消息），然后在回放时模拟源节点再次发送相同数据。

## 全局记录

全局记录会将启用的源节点流记录到一个 tar 文件中。可以通过代码启用：

#### Python

```python
with dai.Pipeline as pipeline:
    config = dai.RecordConfig()
    config.outputDir = "./recordings";
    config.videoEncoding.enabled = True # 使用视频编码
    config.videoEncoding.profile = dai.VideoEncoderProperties.Profile.H264_MAIN

    pipeline.enableHolisticRecord(config)
```

#### C++

```cpp
dai::Pipeline pipeline;
dai::RecordConfig config;
config.outputDir = "./recordings";
config.videoEncoding.enabled = true;  // 使用视频编码
config.videoEncoding.profile = dai::VideoEncoderProperties::Profile::H264_MAIN;

pipeline.enableHolisticRecord(config);
```

或使用 DEPTHAI_RECORD 环境变量：

```bash
DEPTHAI_RECORD="./recordings" python3 application.py
```

### 环境变量配置

在上述示例中，DEPTHAI_RECORD 环境变量被设置为记录输出目录。这会使用默认配置选项启用全局记录。如需更精细的控制，可以将变量设置为配置文件的路径：

```json
{
    "outputDir": "recordings/",
    "compressionLevel": 3,
    "syncCameraOutputs": true,
    "videoEncoding": {
	"enabled": true,
        "bitrate": 0,
        "lossless": false,
        "profile": "MJPEG",
        "quality": 80
    }
}
```

配置参数说明如下：

 * outputDir 设置记录输出目录
 * compressionLevel 配置元数据的压缩级别。取值范围 0 到 5（包含），对应以下压缩级别：NONE、FASTEST、FAST、DEFAULT、SLOW、SLOWEST
 * syncCameraOutputs 启用相机输出同步。这可以防止丢弃的消息在回放到
   [StereoDepth](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/stereo_depth.md) 或类似需要同步帧的节点时引起问题。
 * videoEncoding 配置编码相机流的 [VideoEncoder](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/video_encoder.md)：
   * enabled 启用 [VideoEncoder](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/video_encoder.md)。当为 false
     时，会记录 [Camera](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/camera.md) 节点的原始输出，这可能会降低设备 CPU
     使用率，但会增加带宽消耗。
   * profile 设置编码配置文件
   * bitrate 设置 [VideoEncoder](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/video_encoder.md) 的比特率（设置为 0
     时自动确定）
   * lossless 决定是否在适用时使用无损编码
   * quality 决定适用时的编码质量

## 功能示例

 * [全局记录](https://docs.luxonis.com/software-v3/depthai/examples/record_replay/holistic_record.md)

## 全局回放

全局回放会回放已记录的源流。它会自动循环输入流，并相应地更新消息时间戳和序列号。与全局记录类似，可以通过代码启用：

#### Python

```python
with dai.Pipeline as pipeline:
    pipeline.enableHolisticReplay("./recordings/recording.tar")
```

#### C++

```cpp
dai::Pipeline pipeline;
pipeline.enableHolisticReplay("./recordings/recording.tar");
```

或使用 DEPTHAI_REPLAY 环境变量：

```bash
DEPTHAI_REPLAY="./recordings/recording.tar" python3 application.py
```

## 功能示例

 * [全局回放](https://docs.luxonis.com/software-v3/depthai/examples/record_replay/holistic_replay.md)

## 局限性

此功能仍在开发中，存在一些局限性：

 * 由于回放需要将消息从主机发送到设备，使用此功能会显著降低管道的帧率。
 * 回放时，帧时间戳在每次循环中可能会漂移，因为循环时间戳偏移量是独立为每个相机流计算的。这可能会在运行一段时间后导致消息同步问题。
 * 目前，只能全局记录和回放单设备管道。
