代码拉取完成,页面将自动刷新
package rtsp_test
import (
"context"
"fmt"
"log"
"sync"
"testing"
"time"
"gitee.com/raininfall/router"
"gitee.com/raininfall/rtsp"
"github.com/stretchr/testify/assert"
)
func TestServer(t *testing.T) {
assert := assert.New(t)
const rawSDP = `v=0
o=- 2256458942 2256458942 IN IP4 0.0.0.0
s=Media Server
c=IN IP4 0.0.0.0
t=0 0
a=control:*
a=packetization-supported:DH
a=rtppayload-supported:DH
a=range:npt=now-
m=video 0 RTP/AVP 96
a=control:trackID=0
a=framerate:25.000000
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=64002A;sprop-parameter-sets=Z2QAKq2EAQwgCGEAQwgCGEAQwgCEO1A8ARPyzcBAQFAAAD6AAAw1CEAA,aO48sAA=
a=recvonly
m=audio 0 RTP/AVP 8
a=control:trackID=1
a=rtpmap:8 PCMA/8000
a=recvonly
`
options := rtsp.DefaultServerOptions()
options.WorkerNumber = 4
server := rtsp.NewServer(options)
server.Handle(rtsp.MethodOptions, "/test", func(_ctx router.Context) {
ctx, ok := _ctx.(rtsp.ServerContext)
assert.True(ok)
response := rtsp.NewResponseOf(ctx.Request())
ctx.SendResponse(response)
})
server.Handle(rtsp.MethodDescribe, "/test", func(_ctx router.Context) {
ctx, ok := _ctx.(rtsp.ServerContext)
assert.True(ok)
response := rtsp.NewResponseOf(ctx.Request())
response.Body = []byte(rawSDP)
ctx.SendResponse(response)
})
interleaved := 0
server.Handle(rtsp.MethodSetup, "/test/{control:[0-9a-zA-Z-_=]+}", func(_ctx router.Context) {
ctx, ok := _ctx.(rtsp.ServerContext)
assert.True(ok)
response := rtsp.NewResponseOf(ctx.Request())
response.Header.Set("Transport", fmt.Sprintf(
"RTP/AVP/TCP;unicast;interleaved=%d-%d;ssrc=%08d",
interleaved, interleaved+1, interleaved*10))
interleaved += 2
ctx.SendResponse(response)
})
server.Handle(rtsp.MethodPlay, "/test", func(_ctx router.Context) {
ctx, ok := _ctx.(rtsp.ServerContext)
assert.True(ok)
response := rtsp.NewResponseOf(ctx.Request())
response.Header.Set("RTP-Info", "url=trackID=0;seq=15160;rtptime=2465670780,url=trackID=1;seq=58242;rtptime=219199092")
interleaved += 2
ctx.SendResponse(response)
})
all := &sync.WaitGroup{}
all.Add(1)
go func() {
err := server.Start(":15554")
assert.Nil(err)
all.Done()
}()
<-time.After(1 * time.Second)
client, err := rtsp.Dial(context.Background(), "rtsp://127.0.0.1:15554/test", rtsp.DefaultClientOptions())
assert.Nil(err)
session, err := rtsp.Play(client, rtsp.DefaultPlayOptions())
assert.Nil(err)
for _, channel := range session.Channels {
log.Println(channel.String())
}
session.Stop()
server.Stop()
all.Wait()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。