播放 RTSP 视频流

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了播放 RTSP 视频流相关的知识,希望对你有一定的参考价值。

参考技术A 通过接口调用,获取网络摄像头的 RTSP 推流 URL,需要播放此 RTSP 协议传输的视频流

An html5 Flash Video (FLV) Player written in pure javascript without Flash.
FLV 支持格式

动态添加 rtsp | rtmp | hls 拉流代理。只支持 H264 | H265 | AAC | G711 负载

以 rtsp://somedomain.com:554/live/0?token=abcdefg&field=value

通过 流媒体源对应的播放 URL 可知一个 http-flv 的地址 => http://<vhost>/<app>/<streamid>.flv
addStreamProxy response key => <vhost>/<app>/<streamid> => 根据 addStreamProxy response key 得到 http-flv 播放地址 => http://$key.flv

通过 docker 启动 ZLMediaKit 服务

在 ZLMediaKit 配置文件 中详细描述了相关服务器监听端口,flv.js 使用 http 服务器,原视频流是 RTSP 服务器,故端口映射为

通过 Postman 配置文件 导入即可以测试服务

浏览器播放rtsp视频流:3rtsp转webrtc播放

浏览器播放rtsp视频流:3、rtsp转webrtc播放


文章目录

1. 前言

前面我们测试了rtsp转hls方式,发现延迟比较大,不太适合我们的使用需求。接下来我们试一下webrtc的方式看下使用情况。

综合考虑下来,我们最好能找到一个go作为后端,前端兼容性较好的前后端方案来处理webrtc,这样我们就可以结合我们之前的cgo+onvif+gSoap实现方案来获取rtsp流,并且可以根据已经实现的ptz、预置点等功能接口做更多的扩展。

2. rtsp转webRTC

如下是找到的一个比较合适的开源方案,前端使用了jQuery、bootstrap等,后端使用go+gin来实现并将rtsp流解析转换为webRTC协议提供http相关接口给到前端,通过config.json配置rtsp地址和stun地址:

https://github.com/deepch/RTSPtoWebRTC

此外,还带有stun,可以自行配置stun地址,便于进行内网穿透。

初步测试几乎看不出来延迟,符合预期,使用的jQuery+bootstrap+go+gin做的web,也符合我们的项目使用情况。

3. 初步测试结果

结果如下:

4. 结合我们之前的onvif+gSoap+cgo的方案做修改

我们在此项目的基础上,结合我们之前研究的onvif+cgo+gSoap的方案,将onvif获取到的相关数据提供接口到web端,增加ptz、调焦、缩放等功能。

我们在http.go中添加新的post接口:HTTPAPIServerStreamPtz来进行ptz和HTTPAPIServerStreamPreset进行预置点相关操作。

以下是部分代码,没有做太多的优化,也仅仅实现了ptz、调焦和缩放,算是打通了通路,具体项目需要可以再做优化。

4.1 go后端修改

增加了新的接口,并将之前cgo+onvif+gSoap的内容结合了进来,项目整体没有做更多的优化,只是为了演示,提供一个思路:

http.go(增加了两个post接口ptz和preset,采用cgo方式处理):

package main

/*
#cgo CFLAGS: -I ./ -I /usr/local/
#cgo LDFLAGS: -L ./build -lc_onvif_static -lpthread -ldl -lssl -lcrypto
#include "client.h"
#include "malloc.h"
*/
import "C"

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "os"
    "sort"
    "strconv"
    "time"
    "unsafe"

    "github.com/deepch/vdk/av"

    webrtc "github.com/deepch/vdk/format/webrtcv3"
    "github.com/gin-gonic/gin"
)

type JCodec struct 
    Type string


func serveHTTP() 
    gin.SetMode(gin.ReleaseMode)

    router := gin.Default()
    router.Use(CORSMiddleware())

    if _, err := os.Stat("./web"); !os.IsNotExist(err) 
        router.LoadHTMLGlob("web/templates/*")
        router.GET("/", HTTPAPIServerIndex)
        router.GET("/stream/player/:uuid", HTTPAPIServerStreamPlayer)
    
    router.POST("/stream/receiver/:uuid", HTTPAPIServerStreamWebRTC)
    //增加新的post接口
    router.POST("/stream/ptz/", HTTPAPIServerStreamPtz)
    router.POST("/stream/preset/", HTTPAPIServerStreamPreset)
    router.GET("/stream/codec/:uuid", HTTPAPIServerStreamCodec)
    router.POST("/stream", HTTPAPIServerStreamWebRTC2)

    router.StaticFS("/static", http.Dir("web/static"))
    err := router.Run(Config.Server.HTTPPort)
    if err != nil 
        log.Fatalln("Start HTTP Server error", err)
    


//HTTPAPIServerIndex  index
func HTTPAPIServerIndex(c *gin.Context) 
    _, all := Config.list()
    if len(all) > 0 
        c.Header("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store")
        c.Header("Access-Control-Allow-Origin", "*")
        c.Redirect(http.StatusMovedPermanently, "stream/player/"+all[0])
     else 
        c.HTML(http.StatusOK, "index.tmpl", gin.H
            "port":    Config.Server.HTTPPort,
            "version": time.Now().String(),
        )
    


//HTTPAPIServerStreamPlayer stream player
func HTTPAPIServerStreamPlayer(c *gin.Context) 
    _, all := Config.list()
    sort.Strings(all)
    c.HTML(http.StatusOK, "player.tmpl", gin.H
        "port":     Config.Server.HTTPPort,
        "suuid":    c.Param("uuid"),
        "suuidMap": all,
        "version":  time.Now().String(),
    )


//HTTPAPIServerStreamCodec stream codec
func HTTPAPIServerStreamCodec(c *gin.Context) 
    if Config.ext(c.Param("uuid")) 
        Config.RunIFNotRun(c.Param("uuid"))
        codecs := Config.coGe(c.Param("uuid"))
        if codecs == nil 
            return
        
        var tmpCodec []JCodec
        for _, codec := range codecs 
            if codec.Type() != av.H264 && codec.Type() != av.PCM_ALAW && codec.Type() != av.PCM_MULAW && codec.Type() != av.OPUS 
                log.Println("Codec Not Supported WebRTC ignore this track", codec.Type())
                continue
            
            if codec.Type().IsVideo() 
                tmpCodec = append(tmpCodec, JCodecType: "video")
             else 
                tmpCodec = append(tmpCodec, JCodecType: "audio")
            
        
        b, err := json.Marshal(tmpCodec)
        if err == nil 
			_, err = c.Writer.Write(b)
			if err != nil 
				log.Println("Write Codec Info error", err)
				return
			
		
	


//HTTPAPIServerStreamWebRTC stream video over WebRTC
func HTTPAPIServerStreamWebRTC(c *gin.Context) 
	if !Config.ext(c.PostForm("suuid")) 
		log.Println("Stream Not Found")
		return
	
	Config.RunIFNotRun(c.PostForm("suuid"))
	codecs := Config.coGe(c.PostForm("suuid"))
	if codecs == nil 
		log.Println("Stream Codec Not Found")
		return
	
	var AudioOnly bool
	if len(codecs) == 1 && codecs[0].Type().IsAudio() 
		AudioOnly = true
	
	muxerWebRTC := webrtc.NewMuxer(webrtc.OptionsICEServers: Config.GetICEServers(), ICEUsername: Config.GetICEUsername(), ICECredential: Config.GetICECredential(), PortMin: Config.GetWebRTCPortMin(), PortMax: Config.GetWebRTCPortMax())
	answer, err := muxerWebRTC.WriteHeader(codecs, c.PostForm("data"))
	if err != nil 
		log.Println("WriteHeader", err)
		return
	
	_, err = c.Writer.Write([]byte(answer))
	if err != nil 
		log.Println("Write", err)
		return
	
	go func() 
		cid, ch := Config.clAd(c.PostForm("suuid"))
		defer Config.clDe(c.PostForm("suuid"), cid)
		defer muxerWebRTC.Close()
		var videoStart bool
		noVideo := time.NewTimer(10 * time.Second)
		for 
			select 
			case <-noVideo.C:
				log.Println("noVideo")
				return
			case pck := <-ch:
				if pck.IsKeyFrame || AudioOnly 
					noVideo.Reset(10 * time.Second)
					videoStart = true
				
				if !videoStart && !AudioOnly 
					continue
				
				err = muxerWebRTC.WritePacket(pck)
				if err != nil 
					log.Println("WritePacket", err)
					return
				
			
		
	()


func HTTPAPIServerStreamPtz(c *gin.Context) 
	action := c.PostForm("action")
	direction, err := strconv.Atoi(action)
	if err != nil 
		log.Println(err)
		return
	
	var soap C.P_Soap
	soap = C.new_soap(soap)
	username := C.CString("admin")
	password := C.CString("admin")
	serviceAddr := C.CString("http://40.40.40.101:80/onvif/device_service")

	C.get_device_info(soap, username, password, serviceAddr)

	mediaAddr := [200]C.char
	C.get_capabilities(soap, username, password, serviceAddr, &mediaAddr[0])

	profileToken := [200]C.char
	C.get_profiles(soap, username, password, &profileToken[0], &mediaAddr[0])

	videoSourceToken := [200]C.char
	C.get_video_source(soap, username, password, &videoSourceToken[0], &mediaAddr[0])

	switch direction 
	case 0:
		break
	case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11:
		C.ptz(soap, username, password, C.int(direction), C.float(0.5), &profileToken[0], &mediaAddr[0])
	case 12, 13, 14:
		C.focus(soap, username, password, C.int(direction), C.float(0.5), &videoSourceToken[0], &mediaAddr[0])
	default:
		fmt.Println("Unknown direction.")
	
	C.del_soap(soap)

	C.free(unsafe.Pointer(username))
	C.free(unsafe.Pointer(password))
	C.free(unsafe.Pointer(serviceAddr))

	c.JSON(http.StatusOK, gin.H"message":"success")


func HTTPAPIServerStreamPreset(c *gin.Context) 
	var soap C.P_Soap
	soap = C.new_soap(soap)
	username := C.CString("admin")
	password := C.CString("admin")
	serviceAddr := C.CString("http://40.40.40.101:80/onvif/device_service")

	C.get_device_info(soap, username, password, serviceAddr)

	mediaAddr := [200]C.char
	C.get_capabilities(soap, username, password, serviceAddr, &mediaAddr[0])

	profileToken := [200]C.char
	C.get_profiles(soap, username, password, &profileToken[0], &mediaAddr[0])

	videoSourceToken := [200]C.char
	C.get_video_source(soap, username, password, &videoSourceToken[0], &mediaAddr[0])

	action := c.PostForm("action")
	presetAction, err := strconv.Atoi(action)
	if err != nil 
		log.Println(err)
		return
	
	fmt.Println("请输入数字进行preset,1-4分别代表查询、设置、跳转、删除预置点;退出输入0:")
	switch presetAction 
	case 0:
		break
	case 1:
		C.preset(soap, username, password, C.int(presetAction), nil, nil, &profileToken[0], &mediaAddr[0])
	case 2:
		fmt.Println("请输入要设置的预置点token信息:")
		presentToken := ""
		_, _ = fmt.Scanln(&presentToken)
		fmt.Println("请输入要设置的预置点name信息长度不超过200:")
		presentName := ""
		_, _ = fmt.Scanln(&presentName)
		C.preset(soap, username, password, C.int(presetAction), C.CString(presentToken), C.CString(presentName), &profileToken[0], &mediaAddr[0])
	case 3:
		fmt.Println("请输入要跳转的预置点token信息:")
		presentToken := ""
		_, _ = fmt.Scanln(&presentToken)
		C.preset(soap, username, password, C.int(presetAction), C.CString(presentToken), nil, &profileToken[0], &mediaAddr[0])
	case 4:
		fmt.Println("请输入要删除的预置点token信息:")
		presentToken := ""
		_, _ = fmt.Scanln(&presentToken)
		C.preset(soap, username, password, C.int(presetAction), C.CString(presentToken), nil, &profileToken[0], &mediaAddr[0])
	default:
		fmt.Println("unknown present action.")
		break
	

	C.del_soap(soap)

	C.free(unsafe.Pointer(username))
	C.free(unsafe.Pointer(password))
	C.free(unsafe.Pointer(serviceAddr))
	
	c.JSON(http.StatusOK, gin.H"message":"success")


func CORSMiddleware() gin.HandlerFunc 
	return func(c *gin.Context) 
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Credentials", "true")
		c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, x-access-token")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-A

以上是关于播放 RTSP 视频流的主要内容,如果未能解决你的问题,请参考以下文章

记:rtmp,rtsp,http视频流播放

QtAV 播放rtsp视频流

浏览器播放rtsp视频流:4jsmpeg+go实现局域网下的rtsp视频流web端播放

QtAV 播放rtsp视频流

QtAV 播放rtsp视频流

android 怎么播放rtsp流