> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Media Routes

> Process barcodes and video streams with encoding, decoding, and streaming capabilities

## Media Routes Overview

Media routes enable processing of visual data including barcode scanning and video streaming. These routes bridge the gap between image/video sources and your MQTT infrastructure.

> [!TIP]
>
> **Like adding eyes to your broker.** Media routes let your MQTT broker see and interpret visual data — scanning barcodes off a camera feed or streaming video to a dashboard — without any external processing code.

***

## Barcode Route

The `BARCODE` route enables encoding and decoding of various barcode formats including QR codes, Code 128, EAN-13, and more.

### Basic Syntax

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE BarcodeProcessor WITH TYPE BARCODE
    ADD BARCODE_CONFIG
        WITH SUPPORTED_FORMATS "QR_CODE, CODE_128, EAN_13"
    ADD EVENT DecodeBarcode
        WITH SOURCE_TOPIC "barcode/scan"
        WITH DESTINATION_TOPIC "barcode/result"
        WITH MODE "DECODE"
```

### Configuration Parameters

**SUPPORTED\_FORMATS** `string`

Comma-separated barcode formats to support (e.g., `QR_CODE, CODE_128, EAN_13`).

### Event Parameters

**SOURCE\_TOPIC** `string`

MQTT topic with barcode image (base64) or data to encode.

**DESTINATION\_TOPIC** `string`

MQTT topic to publish barcode results.

**MODE** `string`

Operation mode: `ENCODE` (create barcode) or `DECODE` (read barcode).

**ENABLE\_ANNOTATION** `boolean`

Annotate decoded barcode location on image. Default: false.

**ANNOTATED\_IMAGE\_TOPIC** `string`

Topic to publish annotated image (for DECODE mode).

### Supported Formats

| Format | Description |
| - | - |
| `QR_CODE` | QR Code (2D) |
| `CODE_128` | Code 128 (1D) |
| `CODE_39` | Code 39 (1D) |
| `EAN_13` | EAN-13 (1D retail) |
| `EAN_8` | EAN-8 (1D retail) |
| `UPC_A` | UPC-A (1D retail) |
| `UPC_E` | UPC-E (1D retail) |
| `DATA_MATRIX` | Data Matrix (2D) |
| `PDF_417` | PDF417 (2D stacked) |

### Barcode Examples

**Decode Barcode**

Scan barcodes from images:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE BarcodeScanner WITH TYPE BARCODE
    ADD BARCODE_CONFIG
        WITH SUPPORTED_FORMATS "QR_CODE, CODE_128, EAN_13, UPC_A"
    ADD EVENT ScanBarcode
        WITH SOURCE_TOPIC "camera/frame"
        WITH DESTINATION_TOPIC "barcode/decoded"
        WITH MODE "DECODE"
        WITH ENABLE_ANNOTATION "true"
        WITH ANNOTATED_IMAGE_TOPIC "barcode/annotated"
```

**Input:** Base64-encoded image on `camera/frame`

**Output:** JSON with decoded data on `barcode/decoded`:

```json wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
{
  "format": "QR_CODE",
  "data": "https://example.com",
  "timestamp": "2024-01-15T10:30:00Z"
}
```

**Encode Barcode**

Generate barcode images:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE BarcodeGenerator WITH TYPE BARCODE
    ADD BARCODE_CONFIG
        WITH SUPPORTED_FORMATS "QR_CODE, CODE_128"
    ADD EVENT GenerateBarcode
        WITH SOURCE_TOPIC "barcode/generate"
        WITH DESTINATION_TOPIC "barcode/image"
        WITH MODE "ENCODE"
```

**Input:** JSON with data and format on `barcode/generate`:

```json wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
{
  "format": "QR_CODE",
  "data": "Product-12345",
  "width": 300,
  "height": 300
}
```

**Output:** Base64-encoded barcode image on `barcode/image`

**Production Line Scanner**

Industrial barcode scanning:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE ProductionScanner WITH TYPE BARCODE
    ADD BARCODE_CONFIG
        WITH SUPPORTED_FORMATS "CODE_128, DATA_MATRIX"
    ADD EVENT ScanProduct
        WITH SOURCE_TOPIC "line/camera/frame"
        WITH DESTINATION_TOPIC "line/product/scanned"
        WITH MODE "DECODE"
    ADD EVENT ScanSerial
        WITH SOURCE_TOPIC "line/serial/frame"
        WITH DESTINATION_TOPIC "line/serial/decoded"
        WITH MODE "DECODE"
        WITH ENABLE_ANNOTATION "true"
        WITH ANNOTATED_IMAGE_TOPIC "line/serial/annotated"
```

***

## Video Input Route

The `VIDEO_INPUT` route captures video frames from RTSP streams, webcams, or video files and publishes them to MQTT topics.

### Basic Syntax

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE VideoCapture WITH TYPE VIDEO_INPUT
    ADD VIDEO_INPUT_CONFIG
        WITH SOURCE "rtsp://192.168.1.100:554/stream"
        WITH DESTINATION_TOPIC "video/frames"
        WITH FORMAT "JPEG"
        WITH FRAME_RATE '10'
```

### Configuration Parameters

**SOURCE** `string`

Video source: RTSP URL, webcam identifier, or file path.

**DESTINATION\_TOPIC** `string`

MQTT topic to publish video frames.

**FORMAT** `string`

Output format: `JPEG`, `MJPEG`, `H264`, `H265`, `MP4`, `MPEGTS`.

**FRAME\_RATE** `integer`

Frame rate for capture (1-60 fps).

**WIDTH** `integer`

Frame width in pixels.

**HEIGHT** `integer`

Frame height in pixels.

**QUALITY** `integer`

JPEG quality (1-100).

### Event Parameters

**SOURCE\_TOPIC** `string`

MQTT topic to control video capture.

**QUERY** `string`

Control command: `START`, `STOP`, or `CAPTURE <duration>`.

### Video Input Examples

**RTSP Stream**

Capture from IP camera:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE IPCamera WITH TYPE VIDEO_INPUT
    ADD VIDEO_INPUT_CONFIG
        WITH SOURCE "rtsp://admin:password@192.168.1.100:554/stream1"
        WITH DESTINATION_TOPIC "camera/entrance/frames"
        WITH FORMAT "JPEG"
        WITH FRAME_RATE '5'
        WITH WIDTH '1280'
        WITH HEIGHT '720'
        WITH QUALITY '80'
    ADD EVENT CameraControl
        WITH SOURCE_TOPIC "camera/entrance/control"
        WITH QUERY "START"
```

**Webcam**

Capture from local webcam:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE Webcam WITH TYPE VIDEO_INPUT
    ADD VIDEO_INPUT_CONFIG
        WITH SOURCE "webcam:0"
        WITH DESTINATION_TOPIC "webcam/frames"
        WITH FORMAT "JPEG"
        WITH FRAME_RATE '15'
        WITH WIDTH '640'
        WITH HEIGHT '480'
    ADD EVENT WebcamControl
        WITH SOURCE_TOPIC "webcam/control"
        WITH QUERY "START"
```

**Video File**

Process a video file:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE VideoFile WITH TYPE VIDEO_INPUT
    ADD VIDEO_INPUT_CONFIG
        WITH SOURCE "/videos/sample.mp4"
        WITH DESTINATION_TOPIC "video/file/frames"
        WITH FORMAT "JPEG"
        WITH FRAME_RATE '30'
    ADD EVENT FileControl
        WITH SOURCE_TOPIC "video/file/control"
        WITH QUERY "START"
```

**Motion Detection**

Low frame rate for motion detection:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE MotionDetection WITH TYPE VIDEO_INPUT
    ADD VIDEO_INPUT_CONFIG
        WITH SOURCE "rtsp://192.168.1.50:554/stream"
        WITH DESTINATION_TOPIC "security/motion/frames"
        WITH FORMAT "JPEG"
        WITH FRAME_RATE '2'
        WITH WIDTH '640'
        WITH HEIGHT '480'
        WITH QUALITY '60'
    ADD EVENT MotionControl
        WITH SOURCE_TOPIC "security/motion/control"
        WITH QUERY "START"
```

***

## Video Output Route

The `VIDEO_OUTPUT` route streams video frames from MQTT to various output protocols.

### Basic Syntax

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE VideoStreamer WITH TYPE VIDEO_OUTPUT
    ADD VIDEO_OUTPUT_CONFIG
        WITH SOURCE_TOPIC "video/frames"
        WITH OUTPUT_PROTOCOL "HTTP"
        WITH OUTPUT_PATH "/stream"
```

### Configuration Parameters

**SOURCE\_TOPIC** `string`

MQTT topic with video frames (base64).

**OUTPUT\_PROTOCOL** `string`

Output protocol: `HTTP`, `RTSP`, `UDP`, `TCP`.

**OUTPUT\_PATH** `string`

Output path or port for streaming.

### Event Parameters

**QUERY** `string`

Control command: `START` or `STOP`.

### Video Output Examples

**HTTP Stream**

Stream via HTTP (MJPEG):

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE HTTPStream WITH TYPE VIDEO_OUTPUT
    ADD VIDEO_OUTPUT_CONFIG
        WITH SOURCE_TOPIC "camera/frames"
        WITH OUTPUT_PROTOCOL "HTTP"
        WITH OUTPUT_PATH ":8081/video"
    ADD EVENT StreamControl
        WITH QUERY "START"
```

Access stream at: `http://broker-ip:8081/video`

**RTSP Output**

Re-stream as RTSP:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE RTSPOutput WITH TYPE VIDEO_OUTPUT
    ADD VIDEO_OUTPUT_CONFIG
        WITH SOURCE_TOPIC "processed/frames"
        WITH OUTPUT_PROTOCOL "RTSP"
        WITH OUTPUT_PATH ":8554/stream"
    ADD EVENT RTSPControl
        WITH QUERY "START"
```

Access stream at: `rtsp://broker-ip:8554/stream`

**UDP Multicast**

Multicast video stream:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
DEFINE ROUTE UDPStream WITH TYPE VIDEO_OUTPUT
    ADD VIDEO_OUTPUT_CONFIG
        WITH SOURCE_TOPIC "broadcast/frames"
        WITH OUTPUT_PROTOCOL "UDP"
        WITH OUTPUT_PATH "239.0.0.1:5000"
    ADD EVENT UDPControl
        WITH QUERY "START"
```

***

## Complete Example: Vision System

A complete vision system with camera input, barcode scanning, and video streaming:

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
// Camera input
DEFINE ROUTE InspectionCamera WITH TYPE VIDEO_INPUT
    ADD VIDEO_INPUT_CONFIG
        WITH SOURCE "rtsp://192.168.1.100:554/stream"
        WITH DESTINATION_TOPIC "inspection/camera/raw"
        WITH FORMAT "JPEG"
        WITH FRAME_RATE '10'
        WITH WIDTH '1920'
        WITH HEIGHT '1080'
        WITH QUALITY '90'
    ADD EVENT CameraControl
        WITH SOURCE_TOPIC "inspection/camera/control"
        WITH QUERY "START"

// Barcode scanning
DEFINE ROUTE ProductScanner WITH TYPE BARCODE
    ADD BARCODE_CONFIG
        WITH SUPPORTED_FORMATS "CODE_128, DATA_MATRIX, QR_CODE"
    ADD EVENT ScanProduct
        WITH SOURCE_TOPIC "inspection/camera/raw"
        WITH DESTINATION_TOPIC "inspection/barcode/result"
        WITH MODE "DECODE"
        WITH ENABLE_ANNOTATION "true"
        WITH ANNOTATED_IMAGE_TOPIC "inspection/camera/annotated"

// Live stream for operators
DEFINE ROUTE OperatorStream WITH TYPE VIDEO_OUTPUT
    ADD VIDEO_OUTPUT_CONFIG
        WITH SOURCE_TOPIC "inspection/camera/annotated"
        WITH OUTPUT_PROTOCOL "HTTP"
        WITH OUTPUT_PATH ":8080/inspection"
    ADD EVENT StreamControl
        WITH QUERY "START"
```

***

## Best Practices

> **Optimize Frame Rate**
>
> Use lower frame rates for bandwidth efficiency:
>
> * **Monitoring**: 1-5 fps
> * **Motion detection**: 2-5 fps
> * **Barcode scanning**: 5-10 fps
> * **Live viewing**: 15-30 fps

> **Choose Appropriate Resolution**
>
> Balance quality and performance:
>
> * **Barcode scanning**: 640x480 to 1280x720
> * **Monitoring**: 1280x720
> * **High detail**: 1920x1080

> **Use JPEG for MQTT**
>
> JPEG format is ideal for MQTT transport:
>
> * Reasonable file sizes
> * Wide compatibility
> * Adjustable quality (60-90 recommended)

> **Handle Network Interruptions**
>
> Configure reconnection for RTSP streams and monitor connection status.

***

## Troubleshooting

> **RTSP Connection Failed**
>
> * Verify RTSP URL format and credentials
> * Check camera is accessible on network
> * Ensure port is not blocked by firewall
> * Try alternative RTSP paths (check camera documentation)

> **No Barcode Detected**
>
> * Ensure image quality is sufficient
> * Check barcode format is in SUPPORTED\_FORMATS
> * Verify barcode is properly visible in image
> * Try higher resolution capture

> **High CPU Usage**
>
> * Reduce frame rate
> * Lower resolution
> * Decrease JPEG quality
> * Process fewer simultaneous streams

> **Stream Latency**
>
> * Use UDP for lowest latency
> * Reduce frame size and quality
> * Check network bandwidth
> * Use local network when possible

***

## Next Steps

> **[Industrial Routes](./industrial/overview)**
>
> Connect to PLCs and industrial equipment.

> **[Route Examples](./examples)**
>
> See complete route configuration examples.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.