-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement websockets support for faster and better signalling
- Loading branch information
Showing
9 changed files
with
1,020 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
Copyright (c) 2024 Neeraj Jakhar | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
const ( | ||
clientReadLimit = 512 | ||
pongWait = 10 * time.Second | ||
pingInterval = (pongWait * 9) / 10 | ||
) | ||
|
||
type ClientList map[*Client]bool | ||
|
||
type Client struct { | ||
authorized bool | ||
authDeadline time.Time | ||
sdp string | ||
connection *websocket.Conn | ||
manager *Manager | ||
egress chan Event | ||
} | ||
|
||
func NewClient(conn *websocket.Conn, manager *Manager) *Client { | ||
return &Client{ | ||
authorized: false, | ||
connection: conn, | ||
manager: manager, | ||
egress: make(chan Event), | ||
authDeadline: time.Now().Add(time.Second * time.Duration(60)), | ||
} | ||
} | ||
|
||
func (c *Client) hasAuthTimedOut() bool { | ||
return time.Now().After(c.authDeadline) | ||
} | ||
|
||
func (c *Client) readMessages() { | ||
defer func() { | ||
log.Println("Exiting read message") | ||
c.manager.removeClient(c) | ||
}() | ||
|
||
if err := c.connection.SetReadDeadline(time.Now().Add(pongWait)); err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
c.connection.SetPongHandler(func(pongMsg string) error { | ||
log.Println("Received pong message") | ||
return c.connection.SetReadDeadline(time.Now().Add(pongWait)) | ||
}) | ||
|
||
log.Println("Entering client read loop") | ||
|
||
for { | ||
_, payload, err := c.connection.ReadMessage() | ||
if err != nil { | ||
log.Printf("Error reading message: %v\n", err) | ||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { | ||
log.Printf("error reading message: %v", err) | ||
} | ||
break | ||
} | ||
|
||
log.Println("Payload received") | ||
|
||
var request Event | ||
if err := json.Unmarshal(payload, &request); err != nil { | ||
log.Printf("Error processing event: %v", err) | ||
break | ||
} | ||
|
||
if err := c.manager.routeEvent(request, c); err != nil { | ||
log.Println("Error processing event payload: ", err) | ||
} | ||
} | ||
} | ||
|
||
func (c *Client) writeMessages() { | ||
ticker := time.NewTicker(pingInterval) | ||
defer func() { | ||
log.Println("Exiting write message") | ||
ticker.Stop() | ||
c.manager.removeClient(c) | ||
}() | ||
|
||
log.Println("Entering client write loop") | ||
|
||
for { | ||
select { | ||
case message, ok := <-c.egress: | ||
if !ok { | ||
log.Println("connection closed") | ||
if err := c.connection.WriteMessage(websocket.CloseMessage, nil); err != nil { | ||
log.Println("connection closed: ", err) | ||
} | ||
return | ||
} | ||
|
||
log.Println("Egress event") | ||
data, err := json.Marshal(message) | ||
if err != nil { | ||
// This should never happen | ||
log.Println(err) | ||
return | ||
} | ||
|
||
log.Println("Sending message") | ||
if err := c.connection.WriteMessage(websocket.TextMessage, data); err != nil { | ||
log.Println(err) | ||
} | ||
case <-ticker.C: | ||
log.Println("Sending ping message") | ||
if err := c.connection.WriteMessage(websocket.PingMessage, []byte{}); err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright (c) 2024 Neeraj Jakhar | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package main | ||
|
||
import "github.com/pion/webrtc/v3" | ||
|
||
type OpenRelay struct { | ||
AppName string `yaml:"app_name"` | ||
ApiKey string `yaml:"api_key"` | ||
} | ||
|
||
type ICEServer struct { | ||
URLs string `json:"urls"` | ||
Username string `json:"username,omitempty"` | ||
Credential interface{} `json:"credential,omitempty"` | ||
CredentialType webrtc.ICECredentialType `json:"credentialType,omitempty"` | ||
} | ||
|
||
type UserCredentials struct { | ||
User string `yaml:"user" validate:"required"` | ||
Password string `yaml:"password" validate:"required"` | ||
} | ||
|
||
type TurnConfiguration struct { | ||
PublicIp string `yaml:"public_ip" validate:"required"` | ||
UdpPort int `yaml:"port" validate:"required,number,gte=1,lte=65535" default:"8080"` | ||
Users []UserCredentials `yaml:"users" validate:"required"` | ||
Realm string `yaml:"realm" validate:"required"` | ||
Threads int `yaml:"threads" validate:"required,gte=1,lte=20"` | ||
} | ||
|
||
type Configuration struct { | ||
Port int `yaml:"port" validate:"number,gte=1,lte=65535" default:"8080"` | ||
Url string `yaml:"url" default:"/stream"` | ||
ImageWidth uint `yaml:"image_width" default:"640"` | ||
ImageHeight uint `yaml:"image_height" default:"480"` | ||
FrameRate uint `yaml:"framerate" default:"30"` | ||
LogFile string `yaml:"log_file" default:"none"` | ||
AudioDevice string `yaml:"audio_device" validate:"required"` | ||
VideoDevice string `yaml:"video_device" validate:"required"` | ||
Signalling string `yaml:"signalling" validate:"oneof=http websocket" default:"websocket"` | ||
SignallingUsesTls bool `yaml:"signalling_uses_tls" default:"false"` | ||
SignallingTlsCert string `yaml:"signalling_tls_cert"` | ||
SignallingTlsKey string `yaml:"signalling_tls_key"` | ||
SignallingCredentials []UserCredentials `yaml:"signalling_credentials"` | ||
SignallingOrigin string `yaml:"signalling_origin" default:""` | ||
IceTrickling bool `yaml:"ice_trickling" default:"false"` | ||
DisconnectOnReconnect bool `yaml:"disconnect_on_reconnect" default:"false"` | ||
IceServers []webrtc.ICEServer `yaml:"ice_servers,omitempty"` | ||
OpenRelayConfig *OpenRelay `yaml:"open_relay_config,omitempty"` | ||
UseInternalTurn bool `yaml:"use_internal_turn" default:"false"` | ||
TurnConfiguration *TurnConfiguration `yaml:"turn_configuration"` | ||
} |
Oops, something went wrong.