添加Subject头域

This commit is contained in:
yangjiechina
2024-07-07 23:00:30 +08:00
parent 0cfb2c14df
commit 6e84e3c4f2
5 changed files with 83 additions and 25 deletions

19
api.go
View File

@@ -35,7 +35,7 @@ type eventInfo struct {
func withCheckParams(f func(streamId, protocol string, w http.ResponseWriter, req *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
if "" != req.URL.RawQuery {
logger.Infof("on request %s?%s", req.URL.Path, req.URL.RawQuery)
Sugar.Infof("on request %s?%s", req.URL.Path, req.URL.RawQuery)
}
info := eventInfo{}
@@ -102,7 +102,6 @@ func (api *ApiServer) OnPlay(streamId, protocol string, w http.ResponseWriter, r
return
}
//发送invite
split := strings.Split(streamId, "/")
if len(split) != 2 {
w.WriteHeader(http.StatusOK)
@@ -153,28 +152,28 @@ func (api *ApiServer) OnPlay(streamId, protocol string, w http.ResponseWriter, r
var startTimeSeconds string
var endTimeSeconds string
var err error
var ssrc uint32
var ssrc string
if "playback" == streamType || "download" == streamType {
startTime, err := time.ParseInLocation("2006-01-02t15:04:05", startTimeStr, time.Local)
if err != nil {
logger.Errorf("解析开始时间失败 err:%s start_time:%s", err.Error(), startTimeStr)
Sugar.Errorf("解析开始时间失败 err:%s start_time:%s", err.Error(), startTimeStr)
return
}
endTime, err := time.ParseInLocation("2006-01-02t15:04:05", endTimeStr, time.Local)
if err != nil {
logger.Errorf("解析开始时间失败 err:%s start_time:%s", err.Error(), startTimeStr)
Sugar.Errorf("解析开始时间失败 err:%s start_time:%s", err.Error(), startTimeStr)
return
}
startTimeSeconds = strconv.FormatInt(startTime.Unix(), 10)
endTimeSeconds = strconv.FormatInt(endTime.Unix(), 10)
ssrc = GetVodSSRC()
} else {
ssrc = GetLiveSSRC()
}
ip, port, err := CreateGBSource(streamId, setup, ssrc)
ssrcValue, _ := strconv.Atoi(ssrc)
ip, port, err := CreateGBSource(streamId, setup, uint32(ssrcValue))
if err != nil {
Sugar.Errorf("创建GBSource失败 err:%s", err.Error())
return
@@ -239,19 +238,19 @@ func (api *ApiServer) OnPlay(streamId, protocol string, w http.ResponseWriter, r
parse, err := sdp.Parse(answer)
if err != nil {
inviteOk = false
logger.Errorf("解析应答sdp失败 err:%s sdp:%s", err.Error(), answer)
Sugar.Errorf("解析应答sdp失败 err:%s sdp:%s", err.Error(), answer)
return
}
if parse.Video == nil || parse.Video.Port == 0 {
inviteOk = false
logger.Errorf("应答没有视频连接地址 sdp:%s", answer)
Sugar.Errorf("应答没有视频连接地址 sdp:%s", answer)
return
}
addr := fmt.Sprintf("%s:%d", parse.Addr, parse.Video.Port)
if err = ConnectGBSource(streamId, addr); err != nil {
inviteOk = false
logger.Errorf("设置GB28181连接地址失败 err:%s addr:%s", err.Error(), addr)
Sugar.Errorf("设置GB28181连接地址失败 err:%s addr:%s", err.Error(), addr)
}
}

View File

@@ -24,7 +24,7 @@ const (
var (
XmlMessageType sip.ContentType = "Application/MANSCDP+xml"
SDPMessageType sip.ContentType = "Application/SDP"
SDPMessageType sip.ContentType = "application/sdp"
)
type DBDevice struct {
@@ -120,7 +120,7 @@ func (d *DBDevice) NewRequestBuilder(method sip.RequestMethod, from, realm, to s
return builder
}
func (d *DBDevice) BuildSDP(userName, sessionName, ip string, port uint16, startTime, stopTime, setup string, speed int, ssrc uint32) string {
func (d *DBDevice) BuildSDP(userName, sessionName, ip string, port uint16, startTime, stopTime, setup string, speed int, ssrc string) string {
format := "v=0\r\n" +
"o=%s 0 0 IN IP4 %s\r\n" +
"s=%s\r\n" +
@@ -151,28 +151,36 @@ func (d *DBDevice) BuildSDP(userName, sessionName, ip string, port uint16, start
sdp += fmt.Sprintf("a=downloadspeed:%d\r\n", speed)
}
sdp += fmt.Sprintf("y=%s", fmt.Sprintf("%0*d", 10, ssrc))
sdp += fmt.Sprintf("y=%s\r\n", ssrc)
return sdp
}
func (d *DBDevice) BuildInviteRequest(sessionName, channelId, ip string, port uint16, startTime, stopTime, setup string, speed int, ssrc uint32) (sip.Request, error) {
func (d *DBDevice) BuildInviteRequest(sessionName, channelId, ip string, port uint16, startTime, stopTime, setup string, speed int, ssrc string) (sip.Request, error) {
builder := d.NewRequestBuilder(sip.INVITE, Config.SipId, Config.SipRealm, channelId)
sdp := d.BuildSDP(Config.SipId, sessionName, ip, port, startTime, stopTime, setup, speed, ssrc)
builder.SetContentType(&SDPMessageType)
builder.SetContact(globalContactAddress)
builder.SetBody(sdp)
return builder.Build()
request, err := builder.Build()
if err != nil {
return nil, err
}
var subjectHeader = Subject(channelId + ":" + channelId + "," + Config.SipId + ":" + ssrc)
request.AppendHeader(subjectHeader)
return request, err
}
func (d *DBDevice) BuildLiveRequest(channelId, ip string, port uint16, setup string, ssrc uint32) (sip.Request, error) {
func (d *DBDevice) BuildLiveRequest(channelId, ip string, port uint16, setup string, ssrc string) (sip.Request, error) {
return d.BuildInviteRequest("Play", channelId, ip, port, "0", "0", setup, 0, ssrc)
}
func (d *DBDevice) BuildPlaybackRequest(channelId, ip string, port uint16, startTime, stopTime, setup string, ssrc uint32) (sip.Request, error) {
func (d *DBDevice) BuildPlaybackRequest(channelId, ip string, port uint16, startTime, stopTime, setup string, ssrc string) (sip.Request, error) {
return d.BuildInviteRequest("Playback", channelId, ip, port, startTime, stopTime, setup, 0, ssrc)
}
func (d *DBDevice) BuildDownloadRequest(channelId, ip string, port uint16, startTime, stopTime, setup string, speed int, ssrc uint32) (sip.Request, error) {
func (d *DBDevice) BuildDownloadRequest(channelId, ip string, port uint16, startTime, stopTime, setup string, speed int, ssrc string) (sip.Request, error) {
return d.BuildInviteRequest("Download", channelId, ip, port, startTime, stopTime, setup, speed, ssrc)
}

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"sync"
)
@@ -9,17 +10,21 @@ const (
)
var (
ssrc uint32
lock sync.Mutex
ssrcCount uint32
lock sync.Mutex
)
func GetLiveSSRC() uint32 {
func NextSSRC() uint32 {
lock.Lock()
defer lock.Unlock()
ssrc = (ssrc + 1) % SsrcMaxValue
return ssrc
ssrcCount = (ssrcCount + 1) % SsrcMaxValue
return ssrcCount
}
func GetVodSSRC() uint32 {
return 1000000000 + GetLiveSSRC()
func GetLiveSSRC() string {
return fmt.Sprintf("0%09d", NextSSRC())
}
func GetVodSSRC() string {
return fmt.Sprintf("%d", 1000000000+NextSSRC())
}

10
ssrc_test.go Normal file
View File

@@ -0,0 +1,10 @@
package main
import "testing"
func TestName(t *testing.T) {
for i := 0; i < 10; i++ {
println(GetLiveSSRC())
println(GetVodSSRC())
}
}

36
subject.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"fmt"
"github.com/ghettovoice/gosip/sip"
)
type Subject string
func (subject Subject) String() string {
return fmt.Sprintf("%s: %s", subject.Name(), subject.Value())
}
func (subject Subject) Name() string { return "Subject" }
func (subject Subject) Value() string { return string(subject) }
func (subject Subject) Clone() sip.Header { return subject }
func (subject Subject) Equals(other interface{}) bool {
//if h, ok := other.(Subject); ok {
// return subject == h
//}
//if h, ok := other.(*Subject); ok {
// if subject == h {
// return true
// }
// if subject == nil && h != nil || subject != nil && h == nil {
// return false
// }
//
// return *subject == *h
//}
return false
}