# Holistic Record and Replay

To simplify development and later testing and debugging of your application, you can record and replay test data. This can be done
without any changes to your code with the use of environmental variables and configuration files. You can create a holistic
recording using the [holistic_record.py](https://docs.luxonis.com/software-v3/depthai/examples/record_replay/holistic_record.md)
script and then replay it by setting the `DEPTHAI_REPLAY` environment variable to the path of the recording.

Holistic record and replay works by recording the calibration, camera features information, and data (messages) of source nodes
used in an application ([Camera](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/camera.md) and
[IMU](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/imu.md)) which can then be replayed as if the source
nodes sent the same data again.

## Holistic Record

Holistic record records enabled source node streams to a `tar` file. This can be enabled in code. The output directory must
already exist:

#### Python

```python
from pathlib import Path
import depthai as dai

Path("./recordings").mkdir(parents=True, exist_ok=True)

with dai.Pipeline() as pipeline:
    config = dai.RecordConfig()
    config.outputDir = "./recordings"
    config.videoEncoding.enabled = True # Use video encoding
    config.videoEncoding.profile = dai.VideoEncoderProperties.Profile.H264_MAIN

    pipeline.enableHolisticRecord(config)
    # Add your source nodes (Camera, IMU), then start the pipeline. Sources are recorded.
```

#### C++

```cpp
dai::Pipeline pipeline;
dai::RecordConfig config;
config.outputDir = "./recordings";
config.videoEncoding.enabled = true;  // Use video encoding
config.videoEncoding.profile = dai::VideoEncoderProperties::Profile::H264_MAIN;

pipeline.enableHolisticRecord(config);
// Add your source nodes (Camera, IMU), then start the pipeline. Sources are recorded.
```

or by using the `DEPTHAI_RECORD` environment variable:

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

Holistic record writes the `tar` file to the output directory when the pipeline object is destroyed, for example when the program
ends.

### Environment variable configuration

In the above example, the `DEPTHAI_RECORD` environment variable is set to the recording output directory. This enables holistic
recording with the default configuration options. In this directory-path form only, DepthAI creates the directory if it does not
exist. If you want more control, set the variable to the path to a configuration file:

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

The configuration parameters are as follows:

 * `outputDir` sets the output directory of the recording. The directory must already exist, otherwise recording is disabled
 * `compressionLevel` configures the compression of the metadata. It ranges from 0 to 5 (inclusive) which corresponds to the
   following compression levels: `NONE`, `FASTEST`, `FAST`, `DEFAULT`, `SLOW`, `SLOWEST`
 * `syncCameraOutputs` enables camera output synchronization. This prevents dropped messages from causing issues when replaying
   into [StereoDepth](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/stereo_depth.md) or similar nodes that
   require synchronized frames.
 * `videoEncoding` configures the
   [VideoEncoder](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/video_encoder.md) that encodes the camera
   stream: * `enabled` enables the
   [VideoEncoder](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/video_encoder.md). When `false`, the
   output of the [Camera](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/camera.md) node is recorded which
   can decrease the device CPU usage while increasing bandwidth usage
    * `profile` sets the encoding profile
    * `bitrate` sets the [VideoEncoder](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/video_encoder.md)
      bitrate (when set to 0 it is determined automatically)
    * `lossless` determines whether the encoding should be lossless where applicable
    * `quality` determines the encoding quality where applicable

## Example of functionality

 * [Holistic Record](https://docs.luxonis.com/software-v3/depthai/examples/record_replay/holistic_record.md)

## Holistic Replay

Holistic replay replays recorded source streams. It automatically loops the input streams and updates message timestamps and
sequence numbers accordingly. Similarly to holistic record it can be enabled in code. Replace `./recordings/recording.tar` with
the path of your recording. Holistic record names the file `recording_<date>_<time>.tar`. Every source node of the replay pipeline
must be in the recording (the same camera sockets, and the IMU if the pipeline uses one), otherwise replay is disabled and the
pipeline uses the live sources:

#### Python

```python
import depthai as dai

with dai.Pipeline() as pipeline:
    pipeline.enableHolisticReplay("./recordings/recording.tar")
    # Use the same source nodes as the recorded pipeline, then start it.
```

#### C++

```cpp
dai::Pipeline pipeline;
pipeline.enableHolisticReplay("./recordings/recording.tar");
// Use the same source nodes as the recorded pipeline, then start it.
```

or by using the `DEPTHAI_REPLAY` environment variable:

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

## Example of functionality

 * [Holistic Replay](https://docs.luxonis.com/software-v3/depthai/examples/record_replay/holistic_replay.md)

## Limitations

This functionality is a work in progress and comes with some limitations:

 * As replaying requires sending messages from the host to the device, using this functionality can significantly reduce the fps
   of the pipeline.
 * The frame timestamps can drift on each loop when replaying, as the loop timestamp offsets are calculated independently for each
   camera stream. This can cause issues with message synchronization after some time.
 * At this point in time, only single device pipelines can be holistically recorded and replayed.
