# Live555

## Overview

Live555 is an open-source multimedia streaming library that supports RTP/RTCP and is widely used in streaming-media application development. K230 RTOS SDK integrates Live555 and supports streaming transmission on the K230 platform.

## Functional Description

### Live555 Features

Live555 provides a complete streaming framework:

- **RTP/RTCP**
- **RTSP**
- **partial SIP support**
- **multiple codecs**, including H.264, H.265, and AAC
- **cross-platform support**

### Main Components

Live555 includes the following major components:

- **UsageEnvironment**: base environment for task scheduling and event handling
- **BasicUsageEnvironment**: default environment implementation
- **liveMedia**: media-processing library
- **groupsock**: multicast and unicast networking

## Application Scenarios

On K230, Live555 can be used for:

- **RTSP server**
  `/src/rtsmart/mpp/middleware/src/rtsp_server` provides the RTSP server wrapper based on Live555, and `/src/rtsmart/examples/mpp/sample_rtspserver` is the corresponding demo
- **RTSP client**
  `/src/rtsmart/mpp/middleware/src/rtsp_client` provides the RTSP client wrapper based on Live555, and `/src/rtsmart/examples/mpp/sample_rtspclient` is the corresponding demo

## Build Notes

### Live555 Library Integration

K230 RTOS SDK already includes prebuilt Live555 libraries in the SDK library directory. Applications can link against them directly.

### Link Live555 Libraries

Add the following libraries in Makefile or CMake:

```makefile
LDFLAGS += -lBasicUsageEnvironment -lUsageEnvironment -lgroupsock -lliveMedia
```

```cmake
target_link_libraries(your_target
    BasicUsageEnvironment
    UsageEnvironment
    groupsock
    liveMedia
)
```

## Usage

### Basic RTSP Server Flow

#### Create the usage environment

```cpp
#include <BasicUsageEnvironment.hh>
#include <GroupsockHelper.hh>

TaskScheduler* scheduler = BasicTaskScheduler::createNew();
UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
```

#### Create `ServerMediaSession`

```cpp
#include <OnDemandServerMediaSubsession.hh>
#include <H264VideoFileServerMediaSubsession.hh>

RTSPServer* rtspServer = RTSPServer::createNew(*env, 8554);
if (rtspServer == NULL) {
    *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n";
    exit(1);
}
```

#### Add media subsessions

```cpp
char const* fileName = "test.264";
ServerMediaSession* sms = ServerMediaSession::createNew(*env, fileName, fileName, True);
sms->addSubsession(H264VideoFileServerMediaSubsession::createNew(*env, fileName, False));
rtspServer->addServerMediaSession(sms);
```

#### Start the event loop

```cpp
env->taskScheduler().doEventLoop();
```

### Basic RTSP Client Flow

#### Create the usage environment

```cpp
TaskScheduler* scheduler = BasicTaskScheduler::createNew();
UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
```

#### Create the RTSP client

```cpp
#include <RTSPClient.hh>

RTSPClient* rtspClient = RTSPClient::createNew(*env, "rtsp://example.com/video", 0, "k230_client");
if (rtspClient == NULL) {
    *env << "Failed to create RTSP client: " << env->getResultMsg() << "\n";
    exit(1);
}
```

#### Send the `DESCRIBE` command

```cpp
MediaSession* session = MediaSession::createNew(*env, rtspClient->describeURL());
if (session == NULL) {
    *env << "Failed to create session: " << env->getResultMsg() << "\n";
    exit(1);
}
```

#### Set up RTP reception

```cpp
MediaSubsessionIterator* iter = session->setupSubsessionIterator();
MediaSubsession* subsession;
while ((subsession = iter->next()) != NULL) {
    if (!subsession->initiate()) {
        *env << "Failed to initiate subsession: " << subsession->codecName() << "\n";
    } else {
        subsession->sink = createRTPSink(subsession);
        subsession->sink->startPlaying();
    }
}
```

#### Start the event loop

```cpp
env->taskScheduler().doEventLoop();
```

```{admonition} Tip
The Live555 event loop is blocking and is usually run in a dedicated thread. For more details, refer to the [official Live555 documentation](http://www.live555.com/liveMedia/).
```

```{admonition} Tip
On K230, Live555 can be combined with hardware codecs (`VDEC`/`VENC`) and the media pipeline (`MPP`) to build high-performance streaming applications.
```
