mirror of
				https://gitlab.crans.org/nounous/ghostream.git
				synced 2025-11-04 07:42:10 +01:00 
			
		
		
		
	Test WebRTC Serve
This commit is contained in:
		@@ -2,7 +2,6 @@
 | 
			
		||||
package webrtc
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
	"strings"
 | 
			
		||||
@@ -166,7 +165,7 @@ func newPeerHandler(localSdpChan chan webrtc.SessionDescription, remoteSdp struc
 | 
			
		||||
	<-gatherComplete
 | 
			
		||||
 | 
			
		||||
	// Send answer to client
 | 
			
		||||
	localSdpChan <- answer
 | 
			
		||||
	localSdpChan <- *peerConnection.LocalDescription()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Search for Codec PayloadType
 | 
			
		||||
@@ -178,7 +177,8 @@ func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecNa
 | 
			
		||||
			return codec, codec.PayloadType
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	panic(fmt.Sprintf("Remote peer does not support %s", codecName))
 | 
			
		||||
	log.Printf("Remote peer does not support %s", codecName)
 | 
			
		||||
	return nil, 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Serve WebRTC media streaming server
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1,69 @@
 | 
			
		||||
package webrtc
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"math/rand"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/pion/webrtc/v3"
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/stream/srt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestServe(t *testing.T) {
 | 
			
		||||
	// Serve WebRTC server
 | 
			
		||||
	remoteSdpChan := make(chan struct {
 | 
			
		||||
		StreamID          string
 | 
			
		||||
		RemoteDescription webrtc.SessionDescription
 | 
			
		||||
	})
 | 
			
		||||
	localSdpChan := make(chan webrtc.SessionDescription)
 | 
			
		||||
	webrtcChannel := make(chan srt.Packet, 64)
 | 
			
		||||
	cfg := Options{
 | 
			
		||||
		Enabled:     true,
 | 
			
		||||
		MinPortUDP:  10000,
 | 
			
		||||
		MaxPortUDP:  10005,
 | 
			
		||||
		STUNServers: []string{"stun:stun.l.google.com:19302"},
 | 
			
		||||
	}
 | 
			
		||||
	go Serve(remoteSdpChan, localSdpChan, webrtcChannel, &cfg)
 | 
			
		||||
 | 
			
		||||
	// 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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user