Skip to main content

RTSP

Overview

PortDIC supports RTSP (Real Time Streaming Protocol) through the handler pattern. Each [RTSP] class gets its own independent RTSP connection, and the operating mode (Client or Server) is selected by calling SetMode in the [Preset] method.

ModeDescription
RtspMode.ClientConnect to a remote RTSP source (default)
RtspMode.ServerStart an RTSP relay server that re-streams to downstream clients

Quick Start

Client Mode (connect to a camera)

using Portdic;
using Portdic.RTSP;

[RTSP]
public class CameraStream
{
[RTSPHandler]
public IRTSPHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetMode(RtspMode.Client);
handler.SetUrl("rtsp://192.168.1.100:554/stream1");
handler.SetOnDemand(true);
handler.SetAudio(true);

handler.OnEvent += OnEvent;
}

private void OnEvent(string name, string eventType, string description)
=> Console.WriteLine($"[{name}] {eventType}: {description}");
}
Port.Add<CameraStream>("camera_01");
Port.Run();

Server Mode (relay / re-streaming)

[RTSP]
public class RtspRelay
{
[RTSPHandler]
public IRTSPHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetMode(RtspMode.Server);
handler.SetHost("0.0.0.0"); // bind all interfaces
handler.SetPort(8554); // listen on this port
handler.SetOnDemand(true);
handler.SetAudio(true);

handler.OnEvent += OnEvent;
}

private void OnEvent(string name, string eventType, string description)
=> Console.WriteLine($"[{name}] {eventType}: {description}");
}
Port.Add<RtspRelay>("relay_01");
Port.Run();

Multiple Streams

Each registration creates its own independent RTSP handler:

Port.Add<CameraEntrance>("cam_entrance");
Port.Add<CameraWarehouse>("cam_warehouse");
Port.Run();
[RTSP]
public class CameraEntrance
{
[RTSPHandler]
public IRTSPHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetMode(RtspMode.Client);
handler.SetUrl("rtsp://10.0.0.10:554/ch0");
handler.SetOnDemand(false);
handler.OnEvent += OnEvent;
}

private void OnEvent(string name, string eventType, string description)
=> Console.WriteLine($"[Entrance] {eventType}: {description}");
}

[RTSP]
public class CameraWarehouse
{
[RTSPHandler]
public IRTSPHandler handler { get; set; } = null!;

[Preset]
private void Preset()
{
handler.SetMode(RtspMode.Client);
handler.SetUrl("rtsp://10.0.0.11:554/ch0");
handler.SetOnDemand(false);
handler.SetAudio(false);
handler.OnEvent += OnEvent;
}

private void OnEvent(string name, string eventType, string description)
=> Console.WriteLine($"[Warehouse] {eventType}: {description}");
}

Explicit Open / Close

// Open the stream (or start the server)
ERROR_CODE code = handler.Open();
if (code != ERROR_CODE.ERR_CODE_NO_ERROR)
Console.WriteLine($"RTSP open failed: {code}");

// Check connection / server state
Console.WriteLine(handler.IsConnected); // true / false

// Close
handler.Close();

API Reference

Attributes

AttributeTargetDescription
[RTSP]ClassMarks the class as an RTSP handler container
[RTSPHandler]PropertyInjects the IRTSPHandler instance
[Preset]MethodCalled before Open() to configure the handler

Mode

MethodDescription
SetMode(RtspMode mode)Select Client (default) or Server mode

Client Configuration

MethodDescription
SetUrl(string url)RTSP stream URL (e.g., rtsp://192.168.1.100:554/stream)
SetOnDemand(bool)Stream only on demand (true) or continuously (false)
SetAudio(bool)Include audio track. Default: true
SetDebug(bool)Enable verbose RTSP debug output. Default: false

Server Configuration

MethodDefaultDescription
SetHost(string host)"0.0.0.0"Bind address for server mode
SetPort(int port)8554Listen port for server mode
SetStreamName(string name)Logical stream name for logging

Connection

MethodReturnsDescription
Open()ERROR_CODEConnect to source or start the relay server
Close()ERROR_CODEDisconnect or stop the relay server
IsConnectedbooltrue if connected / server is running

Logging

MethodDescription
SetLogger(string rootPath)Enable hourly-rotated log files. Format: rtsp_{name}_{date}_{hour}.log
WriteLog(string message)Write a custom entry to the log file

Events

OnEvent

Fired on connection state changes and errors.

handler.OnEvent += (string name, string eventType, string description) =>
{
Console.WriteLine($"[{name}] {eventType}: {description}");
};
eventTypeTriggered When
CONNECTEDClient connected / server started
DISCONNECTEDClient disconnected / server stopped
ERRORError occurred (description contains detail)

RTSP URL Formats

ScenarioURL Example
Anonymous accessrtsp://192.168.1.100:554/stream
Username + passwordrtsp://admin:password@192.168.1.50:554/h264
Custom pathrtsp://camera.company.com:554/live/main
Default RTSP port (554)rtsp://10.0.0.5/channel1

Error Codes

CodeValueMeaning
ERR_CODE_NO_ERROR1Success
ERR_CODE_OPEN-1Open failed
ERR_CODE_DLL_NOT_LOADED-2portrtsp.dll not loaded
ERR_CODE_PORTNAME_EMPTY-3Stream name not set
ERR_CODE_DLL_FUNC_NOT_CONFIRM-4Required DLL function unavailable
ERR_CODE_CONNECT_FAILED-5Connection attempt failed

  • MQTT — Lightweight pub/sub messaging for IoT
  • SECS/GEM — Semiconductor equipment protocol
  • TCP — Raw TCP client/server communication