不同品牌網路攝像機(IPC)的 RTSP 碼流地址格式差異巨大,導致客戶端無法統一接入,需針對各廠商單獨適配。
| 廠商 | 主碼流格式 | 子碼流格式 | 備註 |
|---|---|---|---|
| 海康威視 | rtsp://user:pwd@ip:554/h264/ch1/main/av_stream |
rtsp://user:pwd@ip:554/mpeg4/ch1/sub/av_stream |
支援 H.264/H.265;通道號從 1 開始 |
| 大華股份 | rtsp://user:pwd@ip:554/cam/realmonitor?channel=1&subtype=0 |
rtsp://user:pwd@ip:554/cam/realmonitor?channel=1&subtype=1 |
查詢參數制;subtype=0 主碼流,1 子碼流 |
| 宇視科技 | rtsp://user:pwd@ip:554/media/video1 |
rtsp://user:pwd@ip:554/media/video2 |
路徑簡潔;video1 主,video2 子 |
| 華為/海思 | rtsp://user:pwd@ip:554/live/main |
rtsp://user:pwd@ip:554/live/sub |
標準化程度較高 |
| Onvif 標準 | rtsp://user:pwd@ip:554/onvif/profile1/media.smp |
rtsp://user:pwd@ip:554/onvif/profile2/media.smp |
Profile Token 對應不同編碼配置 |
public class RtspUrlBuilder {
private static final Map TEMPLATES = new HashMap<>();
static {
TEMPLATES.put("hikvision", new BrandTemplate(
"rtsp://{user}:{pwd}@{ip}:{port}/h264/ch{ch}/main/av_stream",
"rtsp://{user}:{pwd}@{ip}:{port}/mpeg4/ch{ch}/sub/av_stream"
));
TEMPLATES.put("dahua", new BrandTemplate(
"rtsp://{user}:{pwd}@{ip}:{port}/cam/realmonitor?channel={ch}&subtype=0",
"rtsp://{user}:{pwd}@{ip}:{port}/cam/realmonitor?channel={ch}&subtype=1"
));
// ... 其他廠商
}
public static String build(String brand, String user, String pwd, String ip,
int port, int channel, boolean isSubStream) {
BrandTemplate t = TEMPLATES.get(brand.toLowerCase());
if (t == null) throw new IllegalArgumentException("Unsupported brand: " + brand);
String template = isSubStream ? t.subTemplate : t.mainTemplate;
return template
.replace("{user}", user)
.replace("{pwd}", pwd)
.replace("{ip}", ip)
.replace("{port}", String.valueOf(port))
.replace("{ch}", String.valueOf(channel));
}
static class BrandTemplate {
String mainTemplate, subTemplate;
BrandTemplate(String main, String sub) { this.mainTemplate = main; this.subTemplate = sub; }
}
}
// 1. WS-Discovery 掃描局域網 Onvif 設備
// 2. 獲取設備資訊:GetDeviceInformation
// 3. 獲取媒體配置:GetProfiles → 篩選 VideoEncoderConfiguration
// 4. 獲取串流 URI:GetStreamUri { Protocol: RTSP, ProfileToken: "profile1" }
// 5. 直接使用返回的 URI,無需關心具體格式
部署媒體伺服器(SRS、MediaMTX、EasyDSS):
rtsp://media-server/live/device_{id}/main/ISAPI/System/deviceInfo(海康)、/cgi-bin/global.cgi(大華)不存在真正通用的 RTSP 地址格式。工程上採用 **「廠商模板庫 + 自動識別 + 媒體伺服器統一轉發」** 三層架構:上層客戶端零感知,中層適配層吸收差異,下層媒體伺服器統一協議分發。這是監控平台接入多品牌 IPC 的標準化最佳實踐。