Title : Streaming to Twitch using OpenBSD
Author: Solène
Date  : 06 July 2019
Tags  : openbsd68 gaming

### Introduction

If you ever wanted to make a twitch stream from your OpenBSD system, this is
now possible, thanks to OpenBSD developer thfr@ who made a wrapper named
**fauxstream** using ffmpeg with relevant parameters.

The setup is quite easy, it only requires a few steps and searching on Twitch
website two informations, hopefully, to ease the process, I found the links for

You will need to make an account on twitch, get your api key (a long string of
characters) which **should stay secret** because it allow anyone having it to
stream on your account.

### Preparation steps

1. Register / connect on twitch
2. Get your Stream API key at
   https://www.twitch.tv/YOUR_USERNAME/dashboard/settings (from this page you
   can also choose if twitch should automatically saves streams as videos for
   14 days)
3. [Choose your nearest server from this page](https://stream.twitch.tv/ingests/)
4. Add in your shell environnement a variable TWITCH=rtmp://SERVER_FROM_STEP_3/YOUR_API_KEY
5. Get fauxstream with `cvs -d anoncvs@anoncvs.thfr.info:/cvs checkout -P projects/fauxstream/`
6. `chmod u+x fauxstream/fauxstream`
7. [Allow recording of the microphone](https://www.openbsd.org/faq/faq13.html#recordaudio)
8. [Allow recording of the output sound](https://www.openbsd.org/faq/faq13.html#recordmon)

Once you have all the pieces, start a new shell and check the $TWITCH variable
is correctly set, it should looks like
(this is not a real api key).

### Using fauxstream

**fauxstream** script comes with a README.md file containing some useful
informations, you can also check the usage

View usage:

    $ ./fauxstream

### Starting a stream

When you start a stream, **take care your API key isn't displayed on the
stream**! I redirect stderr to **/dev/null** so all the output containing the
key is not displayed.

Here is the settings I use to stream:

    $ ./fauxstream -m -vmic 5.0 -vmon 0.2 -r 1920x1080 -f 20 -b 4000 $TWITCH 2> /dev/null

If you choose a smaller resolution than your screen, imagine a square of that
resolution starting at the top left corner of your screen, the content of this
square will be streamed.

I recommend **bwm-ng** package (I wrote a ports of the week article about it)
to view your realtime bandwidth usage, if you see the bandwidth reach a fixed
number this mean you reached your bandwidth limit and the stream is certainly
not working correctly, you should lower resolution, fps or bitrate.

I recommend doing a few tries before you want to stream, to be sure it's ok.
Note that the flag `-a` may be be required in case of audio/video
desynchronization, there is no magic value so you should guess and try.

### Adding webcam

I found an easy trick to add webcam on top of a video game.

    $ mpv --no-config --video-sync=display-vdrop --framedrop=vo --ontop av://v4l2:/dev/video1

The trick is to use mpv to display your webcam video on your screen and use the
flag to make it stay on top of any other window (this won't work with cwm(1)
window manager). Then you can resize it and place it where you want. What you
see is what get streamed.

The others mpv flags are to reduce lag between the webcam video stream and the
display, mpv slowly get a delay and after 10 minutes, your webcam will be
lagging by like 10 seconds and will be totally out of sync between the action
and your face.

Don't forget to use chown to change the ownership of your video device to your
user, by default only root has access to video devices. This is reset upon

### Viewing a stream

For less overhead, people can watch a stream using `mpv` software, I think this
will require `youtube-dl` package too.

Example to view me streaming:

    $ mpv https://www.twitch.tv/seriphyde

This would also work with a recorded video:

    $ mpv https://www.twitch.tv/videos/447271018