Skip to content

Commit ba80e67

Browse files
committed
Use MediaEngine in janus-gateway/streaming
Since we are answering we should not depend on hardcoded PayloadTypes. We should explore if we can move this internally, and figure out if we can implement this without copying user input if PayloadType isn't know at track creation time. Resolves pion#35
1 parent 77894a3 commit ba80e67

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

janus-gateway/streaming/main.go

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,52 +53,6 @@ func watchHandle(handle *janus.Handle) {
5353
func main() {
5454
// Everything below is the pion-WebRTC API! Thanks for using it ❤️.
5555

56-
// Prepare the configuration
57-
config := webrtc.Configuration{
58-
ICEServers: []webrtc.ICEServer{
59-
{
60-
URLs: []string{"stun:stun.l.google.com:19302"},
61-
},
62-
},
63-
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
64-
}
65-
66-
// Create a new RTCPeerConnection
67-
peerConnection, err := webrtc.NewPeerConnection(config)
68-
if err != nil {
69-
panic(err)
70-
}
71-
72-
// Allow us to receive 1 audio track, and 1 video track
73-
if _, err = peerConnection.AddTransceiver(webrtc.RTPCodecTypeAudio); err != nil {
74-
panic(err)
75-
} else if _, err = peerConnection.AddTransceiver(webrtc.RTPCodecTypeVideo); err != nil {
76-
panic(err)
77-
}
78-
79-
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
80-
fmt.Printf("Connection State has changed %s \n", connectionState.String())
81-
})
82-
83-
peerConnection.OnTrack(func(track *webrtc.Track, receiver *webrtc.RTPReceiver) {
84-
codec := track.Codec()
85-
if codec.Name == webrtc.Opus {
86-
fmt.Println("Got Opus track, saving to disk as output.ogg")
87-
i, oggNewErr := oggwriter.New("output.ogg", codec.ClockRate, codec.Channels)
88-
if oggNewErr != nil {
89-
panic(oggNewErr)
90-
}
91-
saveToDisk(i, track)
92-
} else if codec.Name == webrtc.VP8 {
93-
fmt.Println("Got VP8 track, saving to disk as output.ivf")
94-
i, ivfNewErr := ivfwriter.New("output.ivf")
95-
if ivfNewErr != nil {
96-
panic(ivfNewErr)
97-
}
98-
saveToDisk(i, track)
99-
}
100-
})
101-
10256
// Janus
10357
gateway, err := janus.Connect("ws://localhost:8188/")
10458
if err != nil {
@@ -137,14 +91,64 @@ func main() {
13791
}
13892

13993
if msg.Jsep != nil {
140-
err = peerConnection.SetRemoteDescription(webrtc.SessionDescription{
94+
offer := webrtc.SessionDescription{
14195
Type: webrtc.SDPTypeOffer,
14296
SDP: msg.Jsep["sdp"].(string),
97+
}
98+
99+
mediaEngine := webrtc.MediaEngine{}
100+
if err = mediaEngine.PopulateFromSDP(offer); err != nil {
101+
panic(err)
102+
}
103+
104+
// Create a new RTCPeerConnection
105+
var peerConnection *webrtc.PeerConnection
106+
peerConnection, err = webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine)).NewPeerConnection(webrtc.Configuration{
107+
ICEServers: []webrtc.ICEServer{
108+
{
109+
URLs: []string{"stun:stun.l.google.com:19302"},
110+
},
111+
},
112+
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
143113
})
144114
if err != nil {
145115
panic(err)
146116
}
147117

118+
// Allow us to receive 1 audio track, and 1 video track
119+
if _, err = peerConnection.AddTransceiver(webrtc.RTPCodecTypeAudio); err != nil {
120+
panic(err)
121+
} else if _, err = peerConnection.AddTransceiver(webrtc.RTPCodecTypeVideo); err != nil {
122+
panic(err)
123+
}
124+
125+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
126+
fmt.Printf("Connection State has changed %s \n", connectionState.String())
127+
})
128+
129+
peerConnection.OnTrack(func(track *webrtc.Track, receiver *webrtc.RTPReceiver) {
130+
codec := track.Codec()
131+
if codec.Name == webrtc.Opus {
132+
fmt.Println("Got Opus track, saving to disk as output.ogg")
133+
i, oggNewErr := oggwriter.New("output.ogg", codec.ClockRate, codec.Channels)
134+
if oggNewErr != nil {
135+
panic(oggNewErr)
136+
}
137+
saveToDisk(i, track)
138+
} else if codec.Name == webrtc.VP8 {
139+
fmt.Println("Got VP8 track, saving to disk as output.ivf")
140+
i, ivfNewErr := ivfwriter.New("output.ivf")
141+
if ivfNewErr != nil {
142+
panic(ivfNewErr)
143+
}
144+
saveToDisk(i, track)
145+
}
146+
})
147+
148+
if err = peerConnection.SetRemoteDescription(offer); err != nil {
149+
panic(err)
150+
}
151+
148152
answer, answerErr := peerConnection.CreateAnswer(nil)
149153
if answerErr != nil {
150154
panic(answerErr)

0 commit comments

Comments
 (0)