-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnegotiator-web.go
74 lines (55 loc) · 1.47 KB
/
negotiator-web.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"github.com/pion/webrtc/v3"
)
type WebNegotiator struct {
baseURL string
}
func (w *WebNegotiator) recv(id, endpoint string) (*webrtc.SessionDescription, error) {
sdp := webrtc.SessionDescription{}
resp, err := http.Get(fmt.Sprintf("%s/%s/%s", w.baseURL, id, endpoint))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
SDPDecode(string(body), &sdp)
return &sdp, nil
}
func (w *WebNegotiator) send(sdp *webrtc.SessionDescription, id, endpoint string) error {
resp, err := http.Post(
fmt.Sprintf("%s/%s/%s", w.baseURL, id, endpoint),
"application/json",
bytes.NewBuffer([]byte(SDPEncode(sdp))),
)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Received invalid status %d", resp.StatusCode)
}
return nil
}
func (w *WebNegotiator) RecvOffer(id string) (*webrtc.SessionDescription, error) {
return w.recv(id, "offer")
}
func (w *WebNegotiator) SendOffer(id string, sdp *webrtc.SessionDescription) error {
return w.send(sdp, id, "offer")
}
func (w *WebNegotiator) RecvAnswer(id string) (*webrtc.SessionDescription, error) {
return w.recv(id, "answer")
}
func (w *WebNegotiator) SendAnswer(id string, sdp *webrtc.SessionDescription) error {
return w.send(sdp, id, "answer")
}
func (w *WebNegotiator) Complete(id string) error {
// TODO: Remove entries.
return nil
}