-
Notifications
You must be signed in to change notification settings - Fork 6
/
live.go
182 lines (154 loc) · 4.8 KB
/
live.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"fmt"
"gb-cms/sdp"
"github.com/ghettovoice/gosip"
"github.com/ghettovoice/gosip/sip"
"math"
"net"
"strconv"
"time"
)
type InviteType int
const (
InviteTypeLive = InviteType(0)
InviteTypePlayback = InviteType(1)
InviteTypeDownload = InviteType(2)
)
func (i *InviteType) SessionName2Type(name string) {
switch name {
case "download":
*i = InviteTypeDownload
break
case "playback":
*i = InviteTypePlayback
break
//case "play":
default:
*i = InviteTypeLive
break
}
}
func (d *Device) StartStream(inviteType InviteType, streamId StreamID, channelId, startTime, stopTime, setup string, speed int, sync bool) (*Stream, bool) {
stream := &Stream{
ID: streamId,
forwardSinks: map[string]*Sink{},
}
// 先添加占位置, 防止重复请求
if oldStream, b := StreamManager.Add(stream); !b {
return oldStream, true
}
if dialog, ok := d.Invite(inviteType, streamId, channelId, startTime, stopTime, setup, speed); ok {
stream.DialogRequest = dialog
callID, _ := dialog.CallID()
StreamManager.AddWithCallId(callID.Value(), stream)
} else {
StreamManager.Remove(streamId)
return nil, false
}
//开启收流超时
wait := func() bool {
ok := stream.WaitForPublishEvent(10)
if !ok {
Sugar.Infof("收流超时 发送bye请求...")
CloseStream(streamId)
}
return ok
}
if sync {
go wait()
} else if !sync && !wait() {
return nil, false
}
return stream, true
}
func (d *Device) Invite(inviteType InviteType, streamId StreamID, channelId, startTime, stopTime, setup string, speed int) (sip.Request, bool) {
var ok bool
var ssrc string
defer func() {
if !ok {
go CloseGBSource(string(streamId))
}
}()
if InviteTypeLive != inviteType {
ssrc = GetVodSSRC()
} else {
ssrc = GetLiveSSRC()
}
ssrcValue, _ := strconv.Atoi(ssrc)
ip, port, err := CreateGBSource(string(streamId), setup, uint32(ssrcValue))
if err != nil {
Sugar.Errorf("创建GBSource失败 err:%s", err.Error())
return nil, false
}
var inviteRequest sip.Request
if InviteTypePlayback == inviteType {
inviteRequest, err = d.BuildPlaybackRequest(channelId, ip, port, startTime, stopTime, setup, ssrc)
} else if InviteTypeDownload == inviteType {
speed = int(math.Min(4, float64(speed)))
inviteRequest, err = d.BuildDownloadRequest(channelId, ip, port, startTime, stopTime, setup, speed, ssrc)
} else {
inviteRequest, err = d.BuildLiveRequest(channelId, ip, port, setup, ssrc)
}
if err != nil {
Sugar.Errorf("创建invite失败 err:%s", err.Error())
return nil, false
}
var dialogRequest sip.Request
var answer string
reqCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
SipUA.SendRequestWithContext(reqCtx, inviteRequest, gosip.WithResponseHandler(func(res sip.Response, request sip.Request) {
if res.StatusCode() < 200 {
} else if res.StatusCode() == 200 {
answer = res.Body()
ackRequest := sip.NewAckRequest("", inviteRequest, res, "", nil)
ackRequest.AppendHeader(GlobalContactAddress.AsContactHeader())
//手动替换ack请求目标地址, answer的contact可能不对.
recipient := ackRequest.Recipient()
remoteIP, remotePortStr, _ := net.SplitHostPort(d.RemoteAddr)
remotePort, _ := strconv.Atoi(remotePortStr)
sipPort := sip.Port(remotePort)
recipient.SetHost(remoteIP)
recipient.SetPort(&sipPort)
Sugar.Infof("send ack %s", ackRequest.String())
err := SipUA.Send(ackRequest)
if err != nil {
cancel()
Sugar.Errorf("send ack error %s %s", err.Error(), ackRequest.String())
} else {
ok = true
dialogRequest = d.CreateDialogRequestFromAnswer(res, false)
}
} else if res.StatusCode() > 299 {
Sugar.Errorf("invite应答失败 code:%d", res.StatusCode())
cancel()
}
}))
if !ok {
return nil, false
}
if "active" == setup {
parse, err := sdp.Parse(answer)
ok = err == nil && parse.Video != nil && parse.Video.Port != 0
if !ok {
Sugar.Errorf("解析应答sdp失败 err:%v sdp:%s", err, answer)
return nil, false
}
addr := fmt.Sprintf("%s:%d", parse.Addr, parse.Video.Port)
if err = ConnectGBSource(string(streamId), addr); err != nil {
ok = false
Sugar.Errorf("设置GB28181连接地址失败 err:%s addr:%s", err.Error(), addr)
}
}
return dialogRequest, ok
}
func (d *Device) Live(streamId StreamID, channelId, setup string) (sip.Request, bool) {
return d.Invite(InviteTypeLive, streamId, channelId, "", "", setup, 0)
}
func (d *Device) Playback(streamId StreamID, channelId, startTime, stopTime, setup string) (sip.Request, bool) {
return d.Invite(InviteTypePlayback, streamId, channelId, startTime, stopTime, setup, 0)
}
func (d *Device) Download(streamId StreamID, channelId, startTime, stopTime, setup string, speed int) (sip.Request, bool) {
return d.Invite(InviteTypePlayback, streamId, channelId, startTime, stopTime, setup, speed)
}