首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

網路攝像機 RTSP 碼流地址廠商差異分析與統一接入方案

     編輯:程式設計綜合問答

問題背景

不同品牌網路攝像機(IPC)的 RTSP 碼流地址格式差異巨大,導致客戶端無法統一接入,需針對各廠商單獨適配。

主流廠商 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 對應不同編碼配置

為什麼不存在通用格式?

  1. 歷史包袱:早期廠商自行定義,缺乏統一標準
  2. 功能差異:編碼格式(H.264/H.265/MJPEG)、認證方式、多碼流數量各異
  3. 商業壁壘:廠商傾向鎖定客戶生態,降低互換性
  4. Onvif 推廣不彻底:雖有 Profile S/G/T 規範,但實現程度參差不齊

統一接入解決方案

方案一:廠商識別 + 模板引擎(推薦)

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; }
    }
}

方案二:Onvif 設備發現 + Profile 協商

// 1. WS-Discovery 掃描局域網 Onvif 設備
// 2. 獲取設備資訊:GetDeviceInformation
// 3. 獲取媒體配置:GetProfiles → 篩選 VideoEncoderConfiguration
// 4. 獲取串流 URI:GetStreamUri { Protocol: RTSP, ProfileToken: "profile1" }
// 5. 直接使用返回的 URI,無需關心具體格式

方案三:媒體伺服器轉發(終極解耦)

部署媒體伺服器(SRS、MediaMTX、EasyDSS):

  1. 客戶端統一連接媒體伺服器:rtsp://media-server/live/device_{id}/main
  2. 媒體伺服器作為 Client 拉取各廠商 IPC 流
  3. 轉封裝/轉碼後統一分發給下游
  4. 優勢:客戶端零適配、支援錄像/轉碼/集群、協議轉換(RTSP→WebRTC/HLS/FLV)

廠商自動識別策略

  1. MAC 地址 OUI 查詢:前 3 字節對應廠商(需維護 OUI 資料庫)
  2. HTTP 請求指紋:/ISAPI/System/deviceInfo(海康)、/cgi-bin/global.cgi(大華)
  3. Onvif GetDeviceInformation:標準化獲取 Manufacturer、Model、FirmwareVersion
  4. 用戶手動選擇:作為最後兜底

總結

不存在真正通用的 RTSP 地址格式。工程上採用 **「廠商模板庫 + 自動識別 + 媒體伺服器統一轉發」** 三層架構:上層客戶端零感知,中層適配層吸收差異,下層媒體伺服器統一協議分發。這是監控平台接入多品牌 IPC 的標準化最佳實踐。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved