Skip to content

Commit

Permalink
Added better video player
Browse files Browse the repository at this point in the history
  • Loading branch information
robrotheram committed Jan 19, 2024
1 parent 283ee61 commit ca7f7a3
Show file tree
Hide file tree
Showing 32 changed files with 772 additions and 451 deletions.
3 changes: 3 additions & 0 deletions pkg/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ func NewApp(config utils.Config, hub *controllers.Hub) App {
router.HandleFunc("/api/channel/{id}", handlers.handleCreateChannel).Methods("POST")
router.HandleFunc("/api/channel/{id}/skip", handlers.handleNextVideo).Methods("POST")
router.HandleFunc("/api/channel/{id}/shuffle", handlers.handleShuffleVideo).Methods("POST")
router.HandleFunc("/api/channel/{id}/clear", handlers.handleClearVideo).Methods("POST")
router.HandleFunc("/api/channel/{id}/loop", handlers.handleLoopVideo).Methods("POST")
router.HandleFunc("/api/channel/{id}/play", handlers.handlePlayVideo).Methods("POST")
router.HandleFunc("/api/channel/{id}/pause", handlers.handlePauseVideo).Methods("POST")
router.HandleFunc("/api/channel/{id}/queue", handlers.handleUpateQueue).Methods("POST")
router.HandleFunc("/api/channel/{id}/seek", handlers.handleSeekVideo).Methods("POST")
router.HandleFunc("/api/channel/{id}/add", handlers.handleAddVideo).Methods("PUT")
router.HandleFunc("/api/channel/{id}/players", handlers.handleGetPlayers).Methods("GET")

router.HandleFunc("/api/channel/{id}/playlist", handlers.handleGetPlaylistsByChannel).Methods("GET")
router.HandleFunc("/api/channel/{id}/add/playlist/{playlist_id}", handlers.handleAddFromPlaylist).Methods("PUT")
Expand Down
62 changes: 59 additions & 3 deletions pkg/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"
"w2g/pkg/api/ui"
"w2g/pkg/controllers"
"w2g/pkg/media"
Expand Down Expand Up @@ -103,6 +104,25 @@ func (h *handler) handleShuffleVideo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(controller.State())
}

func (h *handler) handleClearVideo(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
controller, err := h.Get(id)
if err != nil {
WriteError(w, channelNotFound)
return
}
user, err := h.getUser(r)
if err != nil {
WriteError(w, userNotFound)
return
}
controller.UpdateQueue([]media.Media{}, user.Username)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(controller.State())
}

func (h *handler) handleLoopVideo(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
Expand Down Expand Up @@ -137,6 +157,31 @@ func (h *handler) handlePlayVideo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(controller.State())
}

func (h *handler) handleSeekVideo(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
var seconds time.Duration
decoder := json.NewDecoder(r.Body)
if decoder.Decode(&seconds) != nil {
return
}

controller, err := h.Get(id)
if err != nil {
WriteError(w, channelNotFound)
return
}
user, err := h.getUser(r)
if err != nil {
WriteError(w, userNotFound)
return
}
controller.Seek(seconds, user.Username)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(controller.State())
}

func (h *handler) handlePauseVideo(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
Expand Down Expand Up @@ -221,9 +266,8 @@ func (h *handler) notify() http.Handler {
w.Write([]byte("connection is not using the websocket protocol"))
return
}
player := NewWebPlayer()
controller.Join(player, user.Username)
client := NewClient(socket, controller, player)
client := NewClient(socket, controller, user)
controller.Join(client, user.Username)
controller.AddListner(client.id, client)
})
}
Expand Down Expand Up @@ -339,3 +383,15 @@ func (h *handler) handleDeletePlaylist(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(id)
}

func (h *handler) handleGetPlayers(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
controller, err := h.Get(id)
if err != nil {
WriteError(w, playlistNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(controller.Players().GetProgress())
}
58 changes: 0 additions & 58 deletions pkg/api/webPlayer.go

This file was deleted.

79 changes: 71 additions & 8 deletions pkg/api/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"
"w2g/pkg/controllers"
"w2g/pkg/media"

"github.com/google/uuid"
"github.com/gorilla/websocket"
)

const WEBPLAYER = controllers.PlayerType("WEB_PLAYER")

var Upgrader = &websocket.Upgrader{
ReadBufferSize: SocketBufferSize,
WriteBufferSize: SocketBufferSize,
Expand All @@ -21,12 +25,14 @@ var Upgrader = &websocket.Upgrader{
// client represents a single chatting user.
type Client struct {
id string
user User
contoller *controllers.Controller
player *WebPlayer
// socket is the web socket for this client.
socket *websocket.Conn
// send is a channel on which messages are sent.
send chan []byte
socket *websocket.Conn
send chan []byte

done chan any
progress media.MediaDuration
running bool
}

const (
Expand All @@ -41,12 +47,13 @@ func (c *Client) Read() {
if err != nil {
fmt.Printf("ERROR decoding %v", err)
c.contoller.RemoveListner(c.id)
c.contoller.Leave(c.id, c.user.Username)
return
}
var event controllers.Event
err = json.Unmarshal(msg, &event)
if err == nil {
c.player.UpdateDuration(event.State.Current.Progress)
c.UpdateDuration(event.State.Current.Progress)
}
}
}
Expand All @@ -69,13 +76,69 @@ func (c *Client) Send(event controllers.Event) {
}
}

func NewClient(socket *websocket.Conn, contoller *controllers.Controller, player *WebPlayer) *Client {
func (wb *Client) Type() controllers.PlayerType {
return WEBPLAYER
}

func (wb *Client) Id() string {
return wb.id
}

func (wb *Client) Play(url string, start int) error {
fmt.Println(WEBPLAYER + "_PLAY")
wb.progress = media.MediaDuration{
Progress: 0,
}
wb.running = true
<-wb.done
fmt.Println(WEBPLAYER + "_DONE")
return nil
}

func (wb *Client) Progress() media.MediaDuration {
return wb.progress
}

func (wb *Client) Pause() {
fmt.Println(WEBPLAYER + "_PAUSE")
}
func (wb *Client) Unpause() {
fmt.Println(WEBPLAYER + "_UNPAUSE")
wb.running = true
}

func (wb *Client) Stop() {
fmt.Println(WEBPLAYER + "_STOP")
if wb.running {
wb.running = false
wb.done <- "STOP"
}
}
func (wb *Client) Close() {
fmt.Println(WEBPLAYER + "_CLOSE")
wb.Stop()
}

func (wb *Client) Status() bool {
return wb.running
}

func (wb *Client) UpdateDuration(duration media.MediaDuration) {
wb.progress = duration
}

func (wb *Client) Seek(seconds time.Duration) {
wb.progress.Progress = seconds
}

func NewClient(socket *websocket.Conn, contoller *controllers.Controller, user User) *Client {
client := &Client{
id: uuid.NewString(),
user: user,
socket: socket,
send: make(chan []byte, MessageBufferSize),
done: make(chan any),
contoller: contoller,
player: player,
}
go client.Read()
go client.Write()
Expand Down
35 changes: 25 additions & 10 deletions pkg/controllers/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"fmt"
"time"
"w2g/pkg/media"

Expand Down Expand Up @@ -86,6 +87,12 @@ func (c *Controller) Pause(user string) {
c.Notify(PAUSE_ACTION, user)
}

func (c *Controller) Seek(seconds time.Duration, user string) {
c.players.Seek(seconds)
c.state.Current.Progress.Progress = c.players.Progress().Progress
c.Notify(SEEK, user)
}

func (c *Controller) Add(url string, user string) error {
tracks, err := media.NewVideo(url, user)
if err != nil {
Expand Down Expand Up @@ -136,37 +143,41 @@ func (c *Controller) Update(state PlayerState, user string) {
}

func (c *Controller) Join(player Player, user string) {
if _, ok := c.players.players[player.Type()]; !ok {
if _, ok := c.players.players[player.Id()]; !ok {
c.players.Add(player)
c.Notify(PLAYER_ACTION, user)
}
}

func (c *Controller) Leave(pType PlayerType, user string) {
c.players.Remvoe(pType)
func (c *Controller) Leave(id string, user string) {
c.players.Remvoe(id)
c.Notify(LEAVE_ACTION, user)
}

func (c *Controller) ContainsPlayer(pType PlayerType) bool {
if _, ok := c.players.players[pType]; ok {
func (c *Controller) ContainsPlayer(id string) bool {
if _, ok := c.players.players[id]; ok {
return true
}
return false
}

func (c *Controller) progress() {
defer c.Stop(SYSTEM)
fmt.Println("START")
for {
if len(c.state.Current.Url) == 0 || !c.running || c.players.Empty() {
c.Stop(SYSTEM)
return
}
audio := c.state.Current.GetAudioUrl()
fmt.Println("START_PLAYING")
c.players.Play(audio, 0)
fmt.Println("STOP_PLAYING")
if !c.state.Loop {
c.state.Next()
c.Notify(UPDATE_QUEUE, SYSTEM)
}
if len(c.state.Current.Url) == 0 || c.players.Empty() {
c.Stop(SYSTEM)
fmt.Println("DONE")
return
}
fmt.Println("NEXT")
}
}

Expand Down Expand Up @@ -202,3 +213,7 @@ func (c *Controller) Notify(action ActionType, user string) {
State: state,
}
}

func (c *Controller) Players() *Players {
return c.players
}
1 change: 1 addition & 0 deletions pkg/controllers/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
PLAY_ACTION = ActionType("PLAY")
PAUSE_ACTION = ActionType("PAUSE")
ADD_QUEUE = ActionType("ADD_QUEUE")
SEEK = ActionType("SEEK")
UPDATE_QUEUE = ActionType("UPDATE_QUEUE")
UPDATE = ActionType("UPDATE")
REMOVE_QUEUE = ActionType("REMOVE_QUEUE")
Expand Down
Loading

0 comments on commit ca7f7a3

Please sign in to comment.