# 5.3.6 Stream Sessions and Signalling

## Stream Request and WebRTC Signalling Sequence

```mermaid
sequenceDiagram
    autonumber
    participant Client as Client Browser
    participant Backend as Backend /streams
    participant Links as device_links
    participant Session as stream_sessions
    participant Camera as Camera Browser
    participant RT as Socket.IO Relay

    Client->>Backend: POST /streams/request
    Backend->>Links: verify active camera-client link
    Backend->>Session: insert requested session
    Backend->>Camera: emit stream:requested
    Camera->>Backend: POST /streams/:id/accept
    Backend->>Session: update status to streaming
    Backend->>Client: emit stream:started
    Camera->>RT: webrtc:signal offer
    RT->>Client: relay offer
    Client->>RT: webrtc:signal answer
    RT->>Camera: relay answer
    Client->>RT: ICE candidates
    RT->>Camera: relay ICE candidates
    Camera->>RT: ICE candidates
    RT->>Client: relay ICE candidates
```

## Stream Session State Diagram

```mermaid
stateDiagram-v2
    [*] --> requested
    requested --> streaming: camera accepts
    requested --> cancelled: requester cancels
    requested --> failed: validation or delivery failure
    streaming --> completed: normal end
    streaming --> cancelled: manual stop
    streaming --> failed: connection or upload failure
    completed --> [*]
    cancelled --> [*]
    failed --> [*]
```
