1 Star 1 Fork 0

raininfall/RTSP

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server_test.go 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
Songrq 提交于 2019-08-24 04:14 . impl server
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()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/raininfall/rtsp.git
git@gitee.com:raininfall/rtsp.git
raininfall
rtsp
RTSP
master

搜索帮助