Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-53457] Standalone client #115

Merged
merged 30 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
30e3304
Support Bearer auth type in WS client
streamer45 Jun 30, 2023
a51810b
Implement basic WS client
streamer45 Jun 30, 2023
93ae6d2
CI
streamer45 Jul 7, 2023
94da8bd
Use current branch if exists
streamer45 Jul 7, 2023
a0c96e6
Test helper
streamer45 Jul 7, 2023
3013ddc
Make client concurrency safe
streamer45 Jul 10, 2023
8a2a449
Bump CI instance size
streamer45 Jul 10, 2023
227f09b
Fix CI
streamer45 Jul 10, 2023
964ed6a
Restructuring
streamer45 Jul 10, 2023
f1ed9ca
ws reconnection support
streamer45 Jul 10, 2023
3c7fc68
Add generic context to events
streamer45 Jul 11, 2023
48a4db4
Join/Leave/Reconnect
streamer45 Jul 11, 2023
f172dfa
Fix config patching
streamer45 Jul 11, 2023
8758881
Fix again
streamer45 Jul 12, 2023
f37f5ca
RTC connectivity
streamer45 Jul 12, 2023
e651838
RTCP handler
streamer45 Jul 12, 2023
f6bb4b2
More tests
streamer45 Jul 12, 2023
8bf22a7
Bump timeouts
streamer45 Jul 12, 2023
fd05e9d
Merge remote-tracking branch 'origin/master' into MM-53457
streamer45 Jul 13, 2023
e07b104
Close receiver when sending session disconnects
streamer45 Aug 2, 2023
668ec74
Close client on call end
streamer45 Aug 3, 2023
dbce24b
Merge remote-tracking branch 'origin/master' into MM-53457
streamer45 Aug 17, 2023
b220713
Pass context ID on join
streamer45 Aug 15, 2023
655cb1f
ContextID --> JobID
streamer45 Aug 28, 2023
b4082bf
Merge remote-tracking branch 'origin/master' into MM-53457
streamer45 Sep 11, 2023
e4a1011
WSCallRecordingState
streamer45 Sep 13, 2023
bdaaa43
Fix parsing
streamer45 Sep 13, 2023
3bc451c
Fix typo
streamer45 Oct 9, 2023
6fcc49a
Merge remote-tracking branch 'origin/master' into MM-53457
streamer45 Nov 14, 2023
6c7d5b3
[MM-55415] Fix race condition on stop (#124)
streamer45 Nov 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ jobs:
matrix:
go-version: [1.19.x]

runs-on: ubuntu-latest
runs-on: ubuntu-latest-4-cores

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Install Node
uses: actions/setup-node@v2
with:
node-version: '16.10.0'
- name: Checkout Code
uses: actions/checkout@v2
with:
Expand Down
31 changes: 30 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,42 @@ go-run: ## to run locally for development
$(AT)$(GO) run ${GO_BUILD_OPTS} ${CONFIG_APP_CODE} || ${FAIL}
@$(OK) running locally

.PHONY: prepare-plugin
prepare-plugin: ## prepare Calls plugin for testing
ifeq (${CI}, true)
@$(INFO) preparing Calls plugin...
./build/test/prepare-plugin.sh && \
curl http://localhost:8065/plugins/com.mattermost.calls/version
else
@$(INFO) skipping prepare-plugin target, not on CI
endif

.PHONY: start-mm
start-mm: ## start MM server
ifeq (${CI}, true)
@$(INFO) starting up MM server...
docker-compose -p mmserver -f ./build/test/docker-compose.yaml up -d
else
@$(INFO) skipping start-mm target, not on CI
endif

.PHONY: stop-mm
stop-mm: ## stop MM server
ifeq (${CI}, true)
@$(INFO) stopping up MM server...
docker-compose -p mmserver -f ./build/test/docker-compose.yaml down
else
@$(INFO) skipping stop-mm target, not on CI
endif

.PHONY: go-test
go-test: ## to run tests
go-test: start-mm prepare-plugin ## to run tests
@$(INFO) testing...
$(AT)$(DOCKER) run ${DOCKER_OPTS} \
-v $(PWD):/app -w /app \
-e GOCACHE="/tmp" \
-e CI=${CI} \
--network host \
$(DOCKER_IMAGE_GO) \
/bin/sh -c \
"cd /app && \
Expand Down
9 changes: 9 additions & 0 deletions build/test/config_patch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"PluginSettings": {
"Plugins": {
"com.mattermost.calls": {
"defaultenabled": true
}
}
}
}
49 changes: 49 additions & 0 deletions build/test/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: '2.4'
services:

postgres:
image: "postgres:11"
restart: always
tmpfs: /var/lib/postgresql/data
network_mode: "host"
environment:
POSTGRES_USER: mmuser
POSTGRES_PASSWORD: mostest
POSTGRES_DB: mattermost_test
command: postgres -c 'config_file=/etc/postgresql/postgresql.conf'
volumes:
- "./postgres.conf:/etc/postgresql/postgresql.conf"
healthcheck:
test: [ "CMD", "pg_isready", "-h", "localhost" ]
interval: 5s
timeout: 10s
retries: 3

server:
image: "mattermostdevelopment/mm-ee-test:master"
restart: always
network_mode: "host"
environment:
MM_SERVICESETTINGS_SITEURL: "http://localhost:8065"
MM_SERVICESETTINGS_ENABLELOCALMODE: "true"
MM_SERVICESETTINGS_ALLOWCORSFROM: "http://localhost:8065"
MM_PLUGINSETTINGS_ENABLED: "true"
MM_PLUGINSETTINGS_ENABLEUPLOADS: "true"
MM_PLUGINSETTINGS_AUTOMATICPREPACKAGEDPLUGINS: "true"
MM_TEAMSETTINGS_ENABLEOPENSERVER: "true"
MM_ELASTICSEARCHSETTINGS_CONNECTIONURL: "true"
MM_SQLSETTINGS_DATASOURCE: "postgres://mmuser:mostest@localhost:5432/mattermost_test?sslmode=disable&connect_timeout=10&binary_parameters=yes"
MM_SQLSETTINGS_DRIVERNAME: "postgres"
MM_CLUSTERSETTINGS_READONLYCONFIG: "false"
MM_FEATUREFLAGS_USECASEONBOARDING: "false"
MM_SERVICESETTINGS_ENABLEONBOARDINGFLOW: "false"
MM_FEATUREFLAGS_ONBOARDINGTOURTIPS: "false"
MM_SERVICEENVIRONMENT: "test"
volumes:
- "server-config:/mattermost/config"
depends_on:
postgres:
condition: service_healthy

volumes:
server-config:
7 changes: 7 additions & 0 deletions build/test/postgres.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
max_connections = 300
listen_addresses = '*'
fsync = off
full_page_writes = off
default_text_search_config = 'pg_catalog.english'
commit_delay=1000
logging_collector=off
34 changes: 34 additions & 0 deletions build/test/prepare-plugin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#/bin/bash
set -x

GIT_DEFAULT_BRANCH="main"
GIT_REPO="https://github.com/mattermost/mattermost-plugin-calls"
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
PLUGIN_ID="com.mattermost.calls"

if git ls-remote --exit-code --heads ${GIT_REPO} ${GIT_BRANCH} ; then
echo "Remote branch found"
else
echo "Remote branch not found, using default"
GIT_BRANCH=${GIT_DEFAULT_BRANCH}
fi

# Build
cd .. && git clone -b ${GIT_BRANCH} https://github.com/mattermost/mattermost-plugin-calls --depth 1 && \
cd mattermost-plugin-calls &&
cd standalone && npm ci && cd .. && \
cd webapp && npm ci && cd .. && \
echo 'replace github.com/mattermost/rtcd => ../rtcd' >> go.mod && \ # We need to make sure we compile the plugin with the rtcd changes.
go mod tidy && \
make dist MM_SERVICESETTINGS_ENABLEDEVELOPER=true

# Installation
PLUGIN_BUILD_PATH=$(realpath dist/*.tar.gz)
PLUGIN_FILE_NAME=$(basename ${PLUGIN_BUILD_PATH})

docker cp ../rtcd/build/test/config_patch.json mmserver_server_1:/mattermost && \
docker exec mmserver_server_1 bin/mmctl --local config patch config_patch.json && \
docker cp ${PLUGIN_BUILD_PATH} mmserver_server_1:/mattermost && \
docker exec mmserver_server_1 bin/mmctl --local plugin delete ${PLUGIN_ID} && \
docker exec mmserver_server_1 bin/mmctl --local plugin add ${PLUGIN_FILE_NAME} && \
docker exec mmserver_server_1 bin/mmctl --local plugin enable ${PLUGIN_ID}
39 changes: 39 additions & 0 deletions client/call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package client

import (
"fmt"
)

func (c *Client) joinCall() error {
if err := c.wsSend(wsEventJoin, CallJoinMessage{
ChannelID: c.cfg.ChannelID,
JobID: c.cfg.JobID,
}, false); err != nil {
return fmt.Errorf("failed to send ws msg: %w", err)
}

return nil
}

func (c *Client) leaveCall() error {
if err := c.wsSend(wsEventLeave, nil, false); err != nil {
return fmt.Errorf("failed to send ws msg: %w", err)
}

return nil
}

func (c *Client) reconnectCall() error {
if err := c.wsSend(wsEventReconnect, CallReconnectMessage{
ChannelID: c.cfg.ChannelID,
OriginalConnID: c.originalConnID,
PrevConnID: c.currentConnID,
}, false); err != nil {
return fmt.Errorf("failed to send ws msg: %w", err)
}

return nil
}
165 changes: 165 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright (c) 2022-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package client

import (
"fmt"
"log"
"sync"
"sync/atomic"
"time"

"github.com/mattermost/rtcd/service/ws"

"github.com/pion/webrtc/v3"
)

type EventHandler func(ctx any) error

type EventType string

const (
WSConnectEvent EventType = "WSConnect"
WSDisconnectEvent = "WSDisconnect"
WSCallJoinEvent = "WSCallJoin"
WSCallRecordingState = "WSCallRecordingState"
RTCConnectEvent = "RTCConnect"
RTCDisconnectEvent = "RTCDisconnect"
RTCTrackEvent = "RTCTrack"
CloseEvent = "Close"
ErrorEvent = "Error"
)

const (
clientStateNew int32 = iota
clientStateInit
clientStateClosing
clientStateClosed
)

// Client is a Golang implementation of a client for Mattermost Calls.
type Client struct {
cfg Config

handlers map[EventType]EventHandler

// WebSocket
ws *ws.Client
wsDoneCh chan struct{}
wsCloseCh chan struct{}
wsReconnectInterval time.Duration
wsLastDisconnect time.Time
wsClientSeqNo int64
originalConnID string
currentConnID string

// WebRTC
pc *webrtc.PeerConnection
dc *webrtc.DataChannel
iceCh chan webrtc.ICECandidateInit
receivers map[string]*webrtc.RTPReceiver

state int32

mut sync.RWMutex
}

type Option func(c *Client) error

// New initializes and returns a new Calls client.
func New(cfg Config, opts ...Option) (*Client, error) {
if err := cfg.Parse(); err != nil {
return nil, fmt.Errorf("failed to validate config: %w", err)
}

c := &Client{
cfg: cfg,
handlers: make(map[EventType]EventHandler),
wsDoneCh: make(chan struct{}),
wsCloseCh: make(chan struct{}),
wsClientSeqNo: 1,
iceCh: make(chan webrtc.ICECandidateInit, iceChSize),
receivers: make(map[string]*webrtc.RTPReceiver),
}

for _, opt := range opts {
if err := opt(c); err != nil {
return nil, fmt.Errorf("failed to apply option: %w", err)
}
}

return c, nil
}

// Connect connects a call in the configured channel.
func (c *Client) Connect() error {
c.mut.Lock()
defer c.mut.Unlock()

if !atomic.CompareAndSwapInt32(&c.state, clientStateNew, clientStateInit) {
return fmt.Errorf("ws client is already initialized")
}

if err := c.wsOpen(); err != nil {
return err
}

go c.wsReader()

return nil
}

// Close permanently disconnects the client.
func (c *Client) Close() error {
c.mut.RLock()
if !atomic.CompareAndSwapInt32(&c.state, clientStateInit, clientStateClosing) {
c.mut.RUnlock()
return fmt.Errorf("client is not initialized")
}
c.mut.RUnlock()

close(c.wsCloseCh)
<-c.wsDoneCh

if err := c.ws.Close(); err != nil {
return fmt.Errorf("failed to close ws: %w", err)
}

c.close()

return nil
}

// On is used to subscribe to any events fired by the client.
// Note: there can only be one subscriber per event type.
func (c *Client) On(eventType EventType, h EventHandler) {
c.mut.Lock()
defer c.mut.Unlock()
c.handlers[eventType] = h
}

func (c *Client) emit(eventType EventType, ctx any) {
c.mut.RLock()
handler := c.handlers[eventType]
c.mut.RUnlock()
if handler != nil {
if err := handler(ctx); err != nil {
log.Printf("failed to emit event (%s): %s", eventType, err.Error())
}
}
}

func (c *Client) close() {
atomic.StoreInt32(&c.state, clientStateClosed)

if c.pc != nil {
if err := c.pc.Close(); err != nil {
log.Printf("failed to close peer connection: %s", err)
} else {
log.Printf("pc closed successfully")
}
}

c.emit(CloseEvent, nil)
}
Loading