2020-09-29 15:04:23 +00:00
|
|
|
package webrtc
|
2020-10-14 16:57:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pion/webrtc/v3"
|
2020-10-18 14:05:28 +00:00
|
|
|
"gitlab.crans.org/nounous/ghostream/stream"
|
2020-10-14 16:57:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServe(t *testing.T) {
|
2020-10-18 14:05:28 +00:00
|
|
|
// Init streams messaging and WebRTC server
|
|
|
|
streams := make(map[string]*stream.Stream)
|
2020-10-14 16:57:37 +00:00
|
|
|
remoteSdpChan := make(chan struct {
|
|
|
|
StreamID string
|
|
|
|
RemoteDescription webrtc.SessionDescription
|
|
|
|
})
|
|
|
|
localSdpChan := make(chan webrtc.SessionDescription)
|
|
|
|
cfg := Options{
|
|
|
|
Enabled: true,
|
|
|
|
MinPortUDP: 10000,
|
|
|
|
MaxPortUDP: 10005,
|
|
|
|
STUNServers: []string{"stun:stun.l.google.com:19302"},
|
|
|
|
}
|
2020-10-18 14:05:28 +00:00
|
|
|
go Serve(streams, remoteSdpChan, localSdpChan, &cfg)
|
2020-10-14 16:57:37 +00:00
|
|
|
|
|
|
|
// New client connection
|
|
|
|
mediaEngine := webrtc.MediaEngine{}
|
|
|
|
mediaEngine.RegisterDefaultCodecs()
|
|
|
|
api := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine))
|
|
|
|
peerConnection, _ := api.NewPeerConnection(webrtc.Configuration{})
|
|
|
|
|
|
|
|
// Create video track
|
|
|
|
codec, payloadType := getPayloadType(mediaEngine, webrtc.RTPCodecTypeVideo, "VP8")
|
|
|
|
videoTrack, err := webrtc.NewTrack(payloadType, rand.Uint32(), "video", "pion", codec)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to create new video track", err)
|
|
|
|
}
|
|
|
|
if _, err = peerConnection.AddTrack(videoTrack); err != nil {
|
|
|
|
t.Error("Failed to add video track", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create audio track
|
|
|
|
codec, payloadType = getPayloadType(mediaEngine, webrtc.RTPCodecTypeAudio, "opus")
|
|
|
|
audioTrack, err := webrtc.NewTrack(payloadType, rand.Uint32(), "audio", "pion", codec)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Failed to create new audio track", err)
|
|
|
|
}
|
|
|
|
if _, err = peerConnection.AddTrack(audioTrack); err != nil {
|
|
|
|
t.Error("Failed to add audio track", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create offer
|
|
|
|
offer, _ := peerConnection.CreateOffer(nil)
|
|
|
|
|
|
|
|
// Create channel that is blocked until ICE Gathering is complete
|
|
|
|
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
|
|
|
|
peerConnection.SetLocalDescription(offer)
|
|
|
|
<-gatherComplete
|
|
|
|
|
|
|
|
// Send offer to server
|
|
|
|
remoteSdpChan <- struct {
|
|
|
|
StreamID string
|
|
|
|
RemoteDescription webrtc.SessionDescription
|
|
|
|
}{"demo", *peerConnection.LocalDescription()}
|
|
|
|
_ = <-localSdpChan
|
|
|
|
|
|
|
|
// FIXME: verify connection did work
|
|
|
|
}
|