# RTSP Demo

## Overview

This sample explains how to use RTSP on K230 for real-time audio and video transmission. Through the following examples, you can learn how to use the API interfaces to build an RTSP server, an RTSP client, and an RTSP pusher.

- **RTSP Server**: output the board-side sensor image and audio through RTSP so a client can pull the audio and video stream.
- **RTSP Client**: pull an RTSP audio/video stream from a server and display the video on an HDMI or LCD output.
- **RTSP Pusher**: push the board-side sensor image to a third-party streaming server so clients can pull the RTSP stream from that server.

## Usage

### Source Location

- **RTSP Server**: `/src/rtsmart/examples/mpp/sample_rtspserver`
- **RTSP Client**: `/src/rtsmart/examples/mpp/sample_rtspclient`
- **RTSP Pusher**: `/src/rtsmart/examples/mpp/sample_rtsppusher`

After the demo is built, enter `/sdcard/app/middleware` on the board. You should see:

- `sample_rtspserver.elf`
- `sample_rtspclient.elf`
- `sample_rtsppusher.elf`

## Examples

### Example 1: RTSP Server

This example streams the board-side sensor image and captured audio over RTSP. Clients can pull the RTSP stream to receive real-time media data.

#### Parameters

| Parameter | Description | Range | Default |
| --- | --- | --- | --- |
| `H` | print help information | - | - |
| `t` | encode type | `h264`, `h265` | `h265` |
| `w` | video encode width | `[128, 1920]` | `1280` |
| `h` | video encode height | `[64, 1080]` | `720` |
| `b` | video bitrate | - | `2000` |

#### Run

After the RTSP server starts, the serial log prints a URL similar to `rtsp://<server_ip>:8554/test`. Open that URL with VLC or another RTSP client to get the board-side real-time audio and video stream.

```bash
./sample_rtspserver.elf -t h264 -w 1280 -h 720
```

Make sure the client device and server are on the same network, and replace `<server_ip>` with the actual board IP address. Press `Ctrl+C` to exit.

Representative output includes:

```text
"test" stream
Play this stream using the URL "rtsp://10.100.228.46:8554/test"
vb_set_config ok
kd_mpi_venc_init start 0
venc[0] 1280*720 ...
...
ch 0: 253 pictures encoded. Average FrameRate = 30 Fps
```

### Example 2: RTSP Client

This example acts as an RTSP client, pulls audio and video from an RTSP server, and displays the video on the selected output device.

#### Parameters

| Parameter | Description | Range | Default |
| --- | --- | --- | --- |
| `H` | print help information | - | - |
| `rtsp_url` | RTSP stream URL | - | - |
| `out_type` | output type | `1`: HDMI, `2`: LCD | `1` |

#### Run

Pull the RTSP stream from the server and output video to HDMI:

```bash
./sample_rtspclient.elf rtsp_url 1
# ./sample_rtspclient.elf rtsp_url 2
```

Make sure the client and server are on the same network, and replace `rtsp_url` with the actual RTSP address. Press `Ctrl+C` to exit.

Representative output includes the standard RTSP client flow:

```text
Created new TCP socket ...
Sending request: DESCRIBE ...
Received a complete DESCRIBE response:
...
Sending request: SETUP ...
Received a complete SETUP response:
...
Sending request: PLAY ...
Received a complete PLAY response:
...
235 pictures decoded. Average FrameRate = 29 Fps
```

### Example 3: RTSP Pusher

This example pushes the board-side sensor image to a third-party streaming server through RTSP. Clients can then pull the stream from that server.

Please enable `MPP_ENABLE_MIDDLEWARE_LIB_RTSP_PUSHER` in `make menuconfig`. This option automatically enables `ffmpeg` and `x264`, which are required for RTSP push.

#### Parameters

| Parameter | Description | Range | Default |
| --- | --- | --- | --- |
| `H` | print help information | - | - |
| `w` | video encode width | `[128, 1920]` | `1280` |
| `h` | video encode height | `[64, 1080]` | `720` |
| `b` | video bitrate | - | `2000` |
| `o` | RTSP push URL | - | - |

#### Run

Capture the sensor stream and push it to a third-party streaming server:

```bash
./sample_rtsppusher.elf -w 1280 -h 720 -o <rtsp_url>
```

Make sure the board and server are on the same network, and replace `<rtsp_url>` with the actual RTSP publishing URL. Press `Ctrl+C` to exit.

Representative output includes:

```text
rtsp_pusher url:rtsp://10.10.1.94:10554/live/test
zlmedia url: rtsp://10.10.1.94:10554/live/test, w: 1280, h: 720
...
rtsp pusher init success, url: rtsp://10.10.1.94:10554/live/test
```

Streaming servers such as **EasyDarwin** or **ZLMediaKit** can be used for testing.

```{admonition} Tip
For the detailed RTSP interfaces, refer to the [API documentation](../../api_reference/middleware/multimedia_middleware.md).
```
