forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (109 loc) · 2.58 KB
/
main.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
package main
import (
"fmt"
"time"
janus "github.com/notedit/janus-go"
"github.com/pions/webrtc"
"github.com/pions/webrtc/pkg/ice"
"github.com/pions/webrtc/pkg/media/ivfwriter"
)
func watchHandle(handle *janus.Handle) {
// wait for event
for {
msg := <-handle.Events
switch msg := msg.(type) {
case *janus.SlowLinkMsg:
fmt.Print("SlowLinkMsg type ", handle.Id)
case *janus.MediaMsg:
fmt.Print("MediaEvent type", msg.Type, " receiving ", msg.Receiving)
case *janus.WebRTCUpMsg:
fmt.Print("WebRTCUp type ", handle.Id)
case *janus.HangupMsg:
fmt.Print("HangupEvent type ", handle.Id)
case *janus.EventMsg:
fmt.Printf("EventMsg %+v", msg.Plugindata.Data)
}
}
}
func main() {
webrtc.RegisterDefaultCodecs()
// Create a new RTCPeerConnection
peerConnection, err := webrtc.New(webrtc.RTCConfiguration{})
if err != nil {
panic(err)
}
peerConnection.OnICEConnectionStateChange = func(connectionState ice.ConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
}
peerConnection.OnTrack = func(track *webrtc.RTCTrack) {
if track.Codec.Name == webrtc.Opus {
return
}
fmt.Println("Got VP8 track, saving to disk as output.ivf")
i, err := ivfwriter.New("output.ivf")
if err != nil {
panic(err)
}
for {
if err := i.AddPacket(<-track.Packets); err != nil {
panic(err)
}
}
}
// Janus
gateway, err := janus.Connect("ws://localhost:8188/")
if err != nil {
panic(err)
}
// Create session
session, err := gateway.Create()
if err != nil {
panic(err)
}
// Create handle
handle, err := session.Attach("janus.plugin.streaming")
go watchHandle(handle)
// Get streaming list
if _, err := handle.Request(map[string]interface{}{
"request": "list",
}); err != nil {
panic(err)
}
// Watch the second stream
msg, err := handle.Message(map[string]interface{}{
"request": "watch",
"id": 1,
}, nil)
if err != nil {
fmt.Print("message", msg)
panic(err)
}
if msg.Jsep != nil {
if err := peerConnection.SetRemoteDescription(webrtc.RTCSessionDescription{
Type: webrtc.RTCSdpTypeOffer,
Sdp: msg.Jsep["sdp"].(string),
}); err != nil {
panic(err)
}
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
}
// now we start
if _, err := handle.Message(map[string]interface{}{
"request": "start",
}, map[string]interface{}{
"type": "answer",
"sdp": answer.Sdp,
"trickle": false,
}); err != nil {
panic(err)
}
}
for {
if _, err := session.KeepAlive(); err != nil {
panic(err)
}
time.Sleep(5 * time.Second)
}
}