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