-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathmain.go
165 lines (139 loc) · 3.89 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
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
162
163
164
165
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
// example of how to connect Pion and Janus
package main
import (
"fmt"
"log"
"time"
janus "github.com/notedit/janus-go"
gst "github.com/pion/example-webrtc-applications/v3/internal/gstreamer-src"
"github.com/pion/webrtc/v3"
)
func watchHandle(handle *janus.Handle) {
// wait for event
for {
msg := <-handle.Events
switch msg := msg.(type) {
case *janus.SlowLinkMsg:
log.Println("SlowLinkMsg type ", handle.ID)
case *janus.MediaMsg:
log.Println("MediaEvent type", msg.Type, " receiving ", msg.Receiving)
case *janus.WebRTCUpMsg:
log.Println("WebRTCUp type ", handle.ID)
case *janus.HangupMsg:
log.Println("HangupEvent type ", handle.ID)
case *janus.EventMsg:
log.Printf("EventMsg %+v", msg.Plugindata.Data)
}
}
}
func main() {
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
// Prepare the configuration
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
}
// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config)
if err != nil {
panic(err)
}
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
})
// Create a audio track
opusTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "audio/opus"}, "audio", "pion")
if err != nil {
panic(err)
} else if _, err = peerConnection.AddTrack(opusTrack); err != nil {
panic(err)
}
// Create a video track
vp8Track, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion")
if err != nil {
panic(err)
} else if _, err = peerConnection.AddTrack(vp8Track); err != nil {
panic(err)
}
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
panic(err)
}
// Create channel that is blocked until ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
if err = peerConnection.SetLocalDescription(offer); err != nil {
panic(err)
}
// Block until ICE Gathering is complete, disabling trickle ICE
// we do this because we only can exchange one signaling message
// in a production application you should exchange ICE Candidates via OnICECandidate
<-gatherComplete
gateway, err := janus.Connect("ws://localhost:8188/janus")
if err != nil {
panic(err)
}
session, err := gateway.Create()
if err != nil {
panic(err)
}
handle, err := session.Attach("janus.plugin.videoroom")
if err != nil {
panic(err)
}
go func() {
for {
if _, keepAliveErr := session.KeepAlive(); keepAliveErr != nil {
panic(keepAliveErr)
}
time.Sleep(5 * time.Second)
}
}()
go watchHandle(handle)
_, err = handle.Message(map[string]interface{}{
"request": "join",
"ptype": "publisher",
"room": 1234,
"id": 1,
}, nil)
if err != nil {
panic(err)
}
msg, err := handle.Message(map[string]interface{}{
"request": "publish",
"audio": true,
"video": true,
"data": false,
}, map[string]interface{}{
"type": "offer",
"sdp": peerConnection.LocalDescription().SDP,
"trickle": false,
})
if err != nil {
panic(err)
}
if msg.Jsep != nil {
sdpVal, ok := msg.Jsep["sdp"].(string)
if !ok {
panic("failed to cast")
}
err = peerConnection.SetRemoteDescription(webrtc.SessionDescription{
Type: webrtc.SDPTypeAnswer,
SDP: sdpVal,
})
if err != nil {
panic(err)
}
// Start pushing buffers on these tracks
gst.CreatePipeline("opus", []*webrtc.TrackLocalStaticSample{opusTrack}, "audiotestsrc").Start()
gst.CreatePipeline("vp8", []*webrtc.TrackLocalStaticSample{vp8Track}, "videotestsrc").Start()
}
select {}
}