forked from dwoja22/ffmpeg-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventlistener.go
147 lines (119 loc) · 3.33 KB
/
eventlistener.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
package main
import (
"encoding/json"
"fmt"
"math/rand"
"github.com/pion/webrtc/v2"
)
var (
offer = "1"
answer = "2"
off = "3"
iceCandidate = "4"
)
func (c *Camera) EventListener(done chan bool, room *Room) {
for {
select {
case <-done:
if c.on {
c.Stop()
}
return
case m := <-room.Inbound:
switch m.Type {
case offer:
remoteSdp := webrtc.SessionDescription{}
remoteSDP, err := json.Marshal(m.Message)
if err != nil {
fmt.Println("could not marshal offer. ", err)
continue
}
if err := json.Unmarshal(remoteSDP, &remoteSdp); err != nil {
fmt.Println("could not unmarshal sdp offer. ", err)
continue
}
fmt.Printf("%+v", remoteSdp)
mediaEngine := webrtc.MediaEngine{}
if err := mediaEngine.PopulateFromSDP(remoteSdp); err != nil {
fmt.Println("webrtc could not create media engine.", err)
continue
}
var h264PayloadType uint8
for _, videoCodec := range mediaEngine.GetCodecsByKind(webrtc.RTPCodecTypeVideo) {
if videoCodec.Name == "H264" {
h264PayloadType = videoCodec.PayloadType
break
}
}
if h264PayloadType == 0 {
fmt.Println("Remote peer does not support H264")
continue
}
fmt.Println("received offer")
peerConnection, err := c.NewPeerConnection(mediaEngine)
if err != nil {
fmt.Println(err)
continue
}
peerConnection.OnICECandidate(func(ice *webrtc.ICECandidate) {
if ice == nil {
return
}
fmt.Println("trickle ice candidate: ", ice)
room.Outbound <- &Message{
Type: iceCandidate,
Message: ice.ToJSON(),
}
})
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("connection state has changed %s \n", connectionState.String())
})
videoTrack, err := peerConnection.NewTrack(h264PayloadType, rand.Uint32(), "usb_cam", "usb_cam")
if err != nil {
fmt.Println("webrtc could not create video track.", err)
continue
}
direction := webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
}
_, err = peerConnection.AddTransceiverFromTrack(videoTrack, direction)
if err != nil {
fmt.Println("webrtc could not set transceiver direction. ", err)
continue
}
if err := peerConnection.SetRemoteDescription(remoteSdp); err != nil {
fmt.Println("could not set remote description.", err)
}
answr, err := peerConnection.CreateAnswer(nil)
if err != nil {
fmt.Println("webrtc could not create answer. ", err)
continue
}
if err = peerConnection.SetLocalDescription(answr); err != nil {
fmt.Println("webrtc could not set local description")
continue
}
room.Outbound <- &Message{
Type: answer,
Message: answr,
}
c.Stream(videoTrack)
case iceCandidate:
fmt.Println("received ice candidate")
candidate := webrtc.ICECandidateInit{}
bytes, err := json.Marshal(m.Message)
if err != nil {
fmt.Println("could not marshal ice candidate. ", err)
continue
}
if err := json.Unmarshal(bytes, &candidate); err != nil {
fmt.Println("could not unmarshal ice candidate.", err)
}
fmt.Println("candidate:", candidate)
c.AddICECandidate(candidate)
case off:
c.Stop()
}
}
}
}