Install a self-hosted matrix server (part 3)

How to install coturn for matrix

Install a self-hosted matrix server (part 3)

A chat system is good, but what's even better is having the possibility to add video and audio, the 3rd part of this series try to explain that.

Install coturn

In order to have the video / audio you need a Turn/Stun server, and coturn seems to be the free software go-to
At the time of writing there was no official image, but it seems to be dealt with in this issue, so you may have to adapt the image name in the compose file.

The installation is very straightforward, create a coturn directory, then edit your turnserver.conf:

use-auth-secret
static-auth-secret=CHANGEME
realm=matrix.YOURDOMAIN.COM
listening-port=3478
tls-listening-port=5349
min-port=49160
max-port=49200
verbose
allow-loopback-peers
cli-password=CHANGEME
external-ip=INTERNALIP/EXTERNALIP

You can get your INTERNALIP with ip route get 1 | awk '{print $7}' and your EXTERNALIP with dig +short matrix.YOURDOMAIN.COM @resolver1.opendns.com

Then in your synapse config homeserver.yaml you just need to edit / uncomment those lines:

turn_uris:
  - "turn:matrix.YOURDOMAIN.COM:3478?transport=udp"
  - "turn:matrix.YOURDOMAIN.COM:3478?transport=tcp"
  - "turns:matrix.YOURDOMAIN.COM:3478?transport=udp"
  - "turns:matrix.YOURDOMAIN.COM:3478?transport=tcp"
turn_shared_secret: "CHANGEME"
turn_user_lifetime: 1h
turn_allow_guests: true

You just have to restart the synapse container now with dcrestart synapse.

The coturn itself in the compose file looks like the below:

  coturn:
    image: instrumentisto/coturn:latest
    restart: unless-stopped
    volumes:
      - ./coturn/turnserver.conf:/etc/coturn/turnserver.conf
    ports:
      - 49160-49200:49160-49200/udp
      - 3478:3478
      - 5349:5349

See the ports ? You'll need 2 things, open your firewall for those, and/or NAT them to your internal IP (in your router).
It looks like this in my router's interface, ymmv !

20210426_0842_1805x145_1619419322

Now restart synapse and coturn containers, you can do it with dcupd --build and you're set !

You have to allow the microphone and camera of course in your browser:

20210426_0845_327x335_1619419542

Oh and finally one thing that bugged me for a while, on my desktop the default settings for camera / mic worked fine in Chrome out of the box:

20210426_0846_478x371_1619419619

But for some reason (there is a pending issue about it) on my laptop it kept telling me in the dev tools:
Call error: Error: Couldn't start capturing media! Is your microphone set up and does this app have permission?: NotReadableError: Could not start audio source while the above settings were exactly the same, the OS is the same and Chrome version is the same too.

20210426_0852_953x124_1619419960

The fix for my laptop is to explicitely not use the default, then all works fine:

20210426_0853_415x364_1619420016

In part 4 we'll install maubot to have more interaction and even write our own ! Stay tuned...