/
s1.go
126 lines (101 loc) · 2.57 KB
/
s1.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
package main
import (
"context"
"crypto/tls"
"encoding/binary"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/webtransport-go"
)
const (
HOST = "localhost"
WEBTRANSPORTPORT = "5059"
AUDIOUDPPORT = "5004"
VIDEOUDPPORT = "5006"
MAXPAYLOAD = 1500
)
func main() {
// Step 01: Create a WebTransport client and audio/video stream
tlsConf := &tls.Config{
InsecureSkipVerify: true,
}
quicConfig := &quic.Config{}
d := webtransport.Dialer{
RoundTripper: &http3.RoundTripper{
TLSClientConfig: tlsConf,
QuicConfig: quicConfig,
},
}
resp, conn, err := d.Dial(context.Background(), fmt.Sprintf("https://%s:%s/streaming", HOST, WEBTRANSPORTPORT), nil)
if err != nil {
fmt.Println("Dial failed:", err.Error())
os.Exit(1)
}
defer conn.CloseWithError(0, "")
if resp.StatusCode != http.StatusOK {
fmt.Println("Dial response abnormal")
os.Exit(1)
}
streamAudio, err := conn.OpenStreamSync(context.Background())
if err != nil {
fmt.Println("OpenStreamSync failed:", err.Error())
os.Exit(1)
}
defer streamAudio.Close()
streamVideo, err := conn.OpenStreamSync(context.Background())
if err != nil {
fmt.Println("OpenStreamSync failed:", err.Error())
os.Exit(1)
}
defer streamVideo.Close()
// Step 02: Listen UDP packet and forward packet to WebTransport
udpAudio, err := net.ListenPacket("udp", ":"+AUDIOUDPPORT)
if err != nil {
log.Fatal(err)
}
defer udpAudio.Close()
fmt.Printf("listening on %s for audio\n", udpAudio.LocalAddr().String())
udpVideo, err := net.ListenPacket("udp", ":"+VIDEOUDPPORT)
if err != nil {
log.Fatal(err)
}
defer udpVideo.Close()
fmt.Printf("listening on %s for video\n", udpVideo.LocalAddr().String())
go forwardUDPtoStream(udpAudio, streamAudio)
go forwardUDPtoStream(udpVideo, streamVideo)
// Processing
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
for {
select {
case s := <-ch:
fmt.Println(s)
os.Exit(0)
}
}
}
func forwardUDPtoStream(udpconn net.PacketConn, stream webtransport.Stream) {
for {
buf := make([]byte, MAXPAYLOAD)
n, _, err := udpconn.ReadFrom(buf)
if err != nil {
fmt.Printf("recv udp datagram faild:", err)
return
}
bufCopy := make([]byte, 2+n)
binary.BigEndian.PutUint16(bufCopy, uint16(n))
copy(bufCopy[2:], buf)
_, err = stream.Write(bufCopy)
if err != nil {
fmt.Println("write data to stream failed:", err.Error())
}
fmt.Printf("forward udp to stream: %04d\r", n)
}
}