mirror of
				https://gitlab.crans.org/nounous/ghostream.git
				synced 2025-11-04 15:42:26 +01:00 
			
		
		
		
	Use messaging in web package
This commit is contained in:
		@@ -13,14 +13,12 @@ import (
 | 
			
		||||
 | 
			
		||||
	"github.com/markbates/pkger"
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/internal/monitoring"
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/stream/srt"
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/stream/telnet"
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/stream/webrtc"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	// Precompile regex
 | 
			
		||||
	validPath = regexp.MustCompile("^/[a-z0-9_-]*$")
 | 
			
		||||
	validPath = regexp.MustCompile("^/[a-z0-9@_-]*$")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Handle WebRTC session description exchange via POST
 | 
			
		||||
@@ -152,14 +150,19 @@ func staticHandler() http.Handler {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	// Display connected users stats, from WebRTC or streaming directly from a video player
 | 
			
		||||
	streamID := strings.Replace(r.URL.Path[7:], "/", "", -1)
 | 
			
		||||
	name := strings.Replace(r.URL.Path[7:], "/", "", -1)
 | 
			
		||||
	userCount := 0
 | 
			
		||||
 | 
			
		||||
	// Get requested stream
 | 
			
		||||
	stream, ok := streams[name]
 | 
			
		||||
	if ok {
 | 
			
		||||
		// Get number of output channels
 | 
			
		||||
		userCount = stream.Count()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Display connected users statistics
 | 
			
		||||
	enc := json.NewEncoder(w)
 | 
			
		||||
	err := enc.Encode(struct {
 | 
			
		||||
		ConnectedViewers int
 | 
			
		||||
	}{webrtc.GetNumberConnectedSessions(streamID) +
 | 
			
		||||
		srt.GetNumberConnectedSessions(streamID) +
 | 
			
		||||
		telnet.GetNumberConnectedSessions(streamID)})
 | 
			
		||||
	err := enc.Encode(struct{ ConnectedViewers int }{userCount})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, "Failed to generate JSON.", http.StatusInternalServerError)
 | 
			
		||||
		log.Printf("Failed to generate JSON: %s", err)
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,7 @@ import (
 | 
			
		||||
 | 
			
		||||
	"github.com/markbates/pkger"
 | 
			
		||||
	"github.com/pion/webrtc/v3"
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/stream"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Options holds web package configuration
 | 
			
		||||
@@ -41,6 +42,9 @@ var (
 | 
			
		||||
 | 
			
		||||
	// Preload templates
 | 
			
		||||
	templates *template.Template
 | 
			
		||||
 | 
			
		||||
	// Streams to get statistics
 | 
			
		||||
	streams map[string]stream.Stream
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Load templates with pkger
 | 
			
		||||
@@ -74,10 +78,11 @@ func loadTemplates() error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Serve HTTP server
 | 
			
		||||
func Serve(rSdpChan chan struct {
 | 
			
		||||
func Serve(s map[string]stream.Stream, rSdpChan chan struct {
 | 
			
		||||
	StreamID          string
 | 
			
		||||
	RemoteDescription webrtc.SessionDescription
 | 
			
		||||
}, lSdpChan chan webrtc.SessionDescription, c *Options) {
 | 
			
		||||
	streams = s
 | 
			
		||||
	remoteSdpChan = rSdpChan
 | 
			
		||||
	localSdpChan = lSdpChan
 | 
			
		||||
	cfg = c
 | 
			
		||||
 
 | 
			
		||||
@@ -4,11 +4,17 @@ import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/stream"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// TestHTTPServe tries to serve a real HTTP server and load some pages
 | 
			
		||||
func TestHTTPServe(t *testing.T) {
 | 
			
		||||
	go Serve(nil, nil, &Options{Enabled: false, ListenAddress: "127.0.0.1:8081"})
 | 
			
		||||
	// Init streams messaging
 | 
			
		||||
	streams := make(map[string]stream.Stream)
 | 
			
		||||
 | 
			
		||||
	// Create a disabled web server
 | 
			
		||||
	go Serve(streams, nil, nil, &Options{Enabled: false, ListenAddress: "127.0.0.1:8081"})
 | 
			
		||||
 | 
			
		||||
	// Sleep 500ms to ensure that the web server is running, to avoid fails because the request came too early
 | 
			
		||||
	time.Sleep(500 * time.Millisecond)
 | 
			
		||||
@@ -20,7 +26,7 @@ func TestHTTPServe(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Now let's really start the web server
 | 
			
		||||
	go Serve(nil, nil, &Options{Enabled: true, ListenAddress: "127.0.0.1:8081"})
 | 
			
		||||
	go Serve(streams, nil, nil, &Options{Enabled: true, ListenAddress: "127.0.0.1:8081"})
 | 
			
		||||
 | 
			
		||||
	// Sleep 500ms to ensure that the web server is running, to avoid fails because the request came too early
 | 
			
		||||
	time.Sleep(500 * time.Millisecond)
 | 
			
		||||
@@ -52,7 +58,7 @@ func TestHTTPServe(t *testing.T) {
 | 
			
		||||
		t.Errorf("Viewer page returned %v != %v on GET", resp.StatusCode, http.StatusOK)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Test viewer statistic endpoint
 | 
			
		||||
	// Test viewer statistics endpoint
 | 
			
		||||
	resp, err = http.Get("http://localhost:8081/_stats/demo/")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Errorf("Error while getting /_stats: %s", err)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user