forked from deepch/RTSPtoWebRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
161 lines (156 loc) · 3.89 KB
/
http.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
package main
import (
"encoding/base64"
"fmt"
"log"
"math/rand"
"net/http"
"sort"
"time"
"github.com/deepch/vdk/codec/h264parser"
"github.com/gin-gonic/gin"
"github.com/pion/webrtc/v2"
"github.com/pion/webrtc/v2/pkg/media"
)
func serveHTTP() {
router := gin.Default()
router.LoadHTMLGlob("web/templates/*")
router.GET("/", func(c *gin.Context) {
fi, all := Config.list()
sort.Strings(all)
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"port": Config.Server.HTTPPort,
"suuid": fi,
"suuidMap": all,
"version": time.Now().String(),
})
})
router.GET("/player/:suuid", func(c *gin.Context) {
_, all := Config.list()
sort.Strings(all)
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"port": Config.Server.HTTPPort,
"suuid": c.Param("suuid"),
"suuidMap": all,
"version": time.Now().String(),
})
})
router.POST("/recive", reciver)
router.StaticFS("/static", http.Dir("web/static"))
err := router.Run(Config.Server.HTTPPort)
if err != nil {
log.Fatalln(err)
}
}
func reciver(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
data := c.PostForm("data")
suuid := c.PostForm("suuid")
log.Println("Request", suuid)
if Config.ext(suuid) {
codecs := Config.coGe(suuid)
if codecs == nil {
log.Println("No Codec Info")
return
}
//sps, pps := []byte{}, []byte{}
sps := codecs[0].(h264parser.CodecData).SPS()
pps := codecs[0].(h264parser.CodecData).PPS()
//log.Fatalln(ch, codecs)
sd, err := base64.StdEncoding.DecodeString(data)
if err != nil {
log.Println(err)
return
}
log.Println(string(sd))
// webrtc.RegisterDefaultCodecs()
//peerConnection, err := webrtc.New(webrtc.RTCConfiguration{
//var m webrtc.MediaEngine
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
})
if err != nil {
panic(err)
}
videoTrack, err := peerConnection.NewTrack(webrtc.DefaultPayloadTypeH264, rand.Uint32(), "video", suuid+"_video")
if err != nil {
log.Println(err)
return
}
_, err = peerConnection.AddTrack(videoTrack)
if err != nil {
log.Println(err)
return
}
offer := webrtc.SessionDescription{
Type: webrtc.SDPTypeOffer,
SDP: string(sd),
}
if err := peerConnection.SetRemoteDescription(offer); err != nil {
log.Println(err)
return
}
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
log.Println(err)
return
}
c.Writer.Write([]byte(base64.StdEncoding.EncodeToString([]byte(answer.SDP))))
go func() {
control := make(chan bool, 10)
conected := make(chan bool, 10)
defer peerConnection.Close()
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
if connectionState != webrtc.ICEConnectionStateConnected {
log.Println("Client Close Exit")
control <- true
return
}
if connectionState == webrtc.ICEConnectionStateConnected {
conected <- true
}
})
<-conected
cuuid, ch := Config.clAd(suuid)
defer Config.clDe(suuid, cuuid)
var pre uint32
var start bool
for {
select {
case <-control:
return
case pck := <-ch:
if pck.IsKeyFrame {
start = true
}
if !start {
continue
}
if pck.IsKeyFrame {
pck.Data = append([]byte("\000\000\001"+string(sps)+"\000\000\001"+string(pps)+"\000\000\001"), pck.Data[4:]...)
} else {
pck.Data = pck.Data[4:]
}
var ts uint32
if pre != 0 {
ts = uint32(timeToTs(pck.Time)) - pre
}
err := videoTrack.WriteSample(media.Sample{Data: pck.Data, Samples: uint32(ts)})
pre = uint32(timeToTs(pck.Time))
if err != nil {
return
}
}
}
}()
return
}
}
func timeToTs(tm time.Duration) int64 {
return int64(tm * time.Duration(90000) / time.Second)
}