From d263f743f7eb3f59220f65a1cb1856e14880189e Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Mon, 19 Oct 2020 19:57:04 +0200 Subject: [PATCH] Update package web with Quality structure --- web/handler.go | 14 +++++--------- web/handler_test.go | 5 +++++ web/web.go | 6 +++--- web/web_test.go | 4 ++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/web/handler.go b/web/handler.go index e8ef2d5..ec903b2 100644 --- a/web/handler.go +++ b/web/handler.go @@ -153,19 +153,15 @@ func statisticsHandler(w http.ResponseWriter, r *http.Request) { name := strings.SplitN(strings.Replace(r.URL.Path[7:], "/", "", -1), "@", 2)[0] userCount := 0 - // Get all substreams - for _, outputType := range []string{"", "@720p", "@480p", "@360p", "@240p", "@text"} { - // Get requested stream - stream, ok := streams[name+outputType] - if ok { - // Get number of output channels - userCount += stream.ClientCount() - } + // Get requested stream + stream, err := streams.Get(name) + if err == nil { + userCount = stream.ClientCount() } // Display connected users statistics enc := json.NewEncoder(w) - err := enc.Encode(struct{ ConnectedViewers int }{userCount}) + 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) diff --git a/web/handler_test.go b/web/handler_test.go index ae96faf..c67b134 100644 --- a/web/handler_test.go +++ b/web/handler_test.go @@ -4,6 +4,8 @@ import ( "net/http" "net/http/httptest" "testing" + + "gitlab.crans.org/nounous/ghostream/messaging" ) func TestViewerPageGET(t *testing.T) { @@ -12,6 +14,9 @@ func TestViewerPageGET(t *testing.T) { t.Errorf("Failed to load templates: %v", err) } + // Init streams messaging + streams = messaging.New() + cfg = &Options{} // Test GET request diff --git a/web/web.go b/web/web.go index 1f10a66..40dc38b 100644 --- a/web/web.go +++ b/web/web.go @@ -11,7 +11,7 @@ import ( "github.com/markbates/pkger" "github.com/pion/webrtc/v3" - "gitlab.crans.org/nounous/ghostream/stream" + "gitlab.crans.org/nounous/ghostream/messaging" ) // Options holds web package configuration @@ -44,7 +44,7 @@ var ( templates *template.Template // Streams to get statistics - streams map[string]*stream.Stream + streams *messaging.Streams ) // Load templates with pkger @@ -78,7 +78,7 @@ func loadTemplates() error { } // Serve HTTP server -func Serve(s map[string]*stream.Stream, rSdpChan chan struct { +func Serve(s *messaging.Streams, rSdpChan chan struct { StreamID string RemoteDescription webrtc.SessionDescription }, lSdpChan chan webrtc.SessionDescription, c *Options) { diff --git a/web/web_test.go b/web/web_test.go index 54378f3..0892cd2 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -5,13 +5,13 @@ import ( "testing" "time" - "gitlab.crans.org/nounous/ghostream/stream" + "gitlab.crans.org/nounous/ghostream/messaging" ) // TestHTTPServe tries to serve a real HTTP server and load some pages func TestHTTPServe(t *testing.T) { // Init streams messaging - streams := make(map[string]*stream.Stream) + streams := messaging.New() // Create a disabled web server go Serve(streams, nil, nil, &Options{Enabled: false, ListenAddress: "127.0.0.1:8081"})