采用https://github.com/use-go/onvif得到完整的RTSP流的地址
- 全局变量
- UseGoMain 函数
- GetDeviceInformation 函数
- GetRTSPUri 函数
全局变量
这些变量用来设定 ONVIF 设备的连接信息,包含设备地址、用户名和密码
var deviceXaddr = "*92.1*8.11*.12*:80" // 替换为实际的IP和端口
var username = "cory" // 替换为实际的用户名
var password = "q****ER#" // 替换为实际的密码
UseGoMain 函数
- 建立连接
func UseGoMain() {params := onvif.DeviceParams{Xaddr: deviceXaddr,Username: username,Password: password,}dev, err := onvif.NewDevice(params)if err != nil {log.Fatalf("创建设备连接失败: %v", err)}ctx := context.Background()GetDeviceInformation(ctx, dev)GetRTSPUri(ctx, dev)
}
GetDeviceInformation 函数
- 调用GetDeviceInformation函数读取设备的基础信息。
func GetDeviceInformation(ctx context.Context, dev *onvif.Device) {// 读取设备基础信息getDeviceInformation := device.GetDeviceInformation{}getDeviceInformationResponse, err := sdkdevice.Call_GetDeviceInformation(ctx, dev, getDeviceInformation)if err != nil {panic(err)}//HardwareId string //固件ID/设备编号//SerialNumber string //设备序列号//FirmwareVersion string //固件版本//Model string //设备类型//Manufacturer string //厂家信息fmt.Println("getDeviceInformationResponse:")fmt.Println(getDeviceInformationResponse)
}
GetRTSPUri 函数
- 调用GetRTSPUri函数获取设备的 RTSP 流地址。
func GetRTSPUri(ctx context.Context, dev *onvif.Device) {// 获取媒体配置文件getProfiles := media.GetProfiles{}profilesResponse, err := sdkmedia.Call_GetProfiles(ctx, dev, getProfiles)if err != nil {log.Fatalf("获取媒体配置文件失败: %v", err)}if len(profilesResponse.Profiles) == 0 {log.Println("未找到媒体配置文件")return}// 选择第一个配置文件profileToken := profilesResponse.Profiles[0].Token// 获取流URIgetStreamUri := media.GetStreamUri{XMLName: "trt:GetStreamUri",StreamSetup: lc.StreamSetup{Stream: "RTP-Unicast",Transport: lc.Transport{Protocol: "RTSP",},},ProfileToken: profileToken,}streamUriResponse, err := sdkmedia.Call_GetStreamUri(ctx, dev, getStreamUri)if err != nil {log.Fatalf("获取流URI失败: %v", err)}// 嵌入用户名和密码var rtspStr = ""uri := streamUriResponse.MediaUriparts := strings.SplitN(string(uri.Uri), "://", 2)if len(parts) == 2 {encodedPassword := url.QueryEscape(password)rtspStr = fmt.Sprintf("%s://%s:%s@%s", parts[0], username, encodedPassword, parts[1])}fmt.Println("RTSP地址:")fmt.Println(streamUriResponse.MediaUri)fmt.Println("凭证-RTSP地址:")fmt.Println(rtspStr)
}