1---
2title: "Custom Streaming Setup"
3description: "My second post of 100 Days To Offload details my custom streaming setup"
4author: Amolith
5date: 2020-04-26T20:24:38-04:00
6draft: false
7cover: /assets/pngs/stream.png
8categories:
9 - Technology
10tags:
11 - Gaming
12 - Streaming
13 - NGINX
14 - OBS
15 - 100 Days To Offload
16toc: true
17---
18
19The other day, I decided that I wanted to start streaming. I'll
20definitely be playing some games but I might also stream some other
21things like me playing music. We'll see where that goes. In any case, I
22don't like relying on third parties for things and didn't want to use
23Twitch so I started figuring out how to build my own open source and
24privacy-friendly "platform" (which is really just a [page.](/live))
25
26## The search for a platform
27Before settling on my own custom thing, I did some digging into
28ready-made platforms I could just throw on one of my servers and run.
29Two of the ones I found were
30[OpenStreamingPlatform](https://openstreamingplatform.com/) and
31[Restreamer.](https://datarhei.github.io/restreamer/) The latter isn't
32exactly what I was looking for but it could have worked quite well. The
33former, at first glance, was absolutely *perfect*. On a functional
34level, it still is. However, take a look at [the installation
35guide.](https://wiki.openstreamingplatform.com/Install/Manual)
36
37`<rant>`
38
39Steps 3 and 7 are unnecessary unless you feel like manually compiling
40your web server; it's already available in the [Debian
41repos](https://packages.debian.org/buster/libnginx-mod-rtmp) and, by
42extension, Ubuntu's. It's even been backported to Stretch. In step 4, he
43has `sed -i 's/appendfsync everysec/appendfsync no/'`. Like so many
44application developers, he's assuming that this is the only project that
45will be installed on the system. If someone is already using redis in
46production and they have a different value there, that command will
47fail. In step 9, the commands are copying the SystemD service files to
48`/lib/systemd/` but this is where the package manager, `apt`, stores its
49services. When you have your own that you're writing or copying from
50somewhere else, best practise is to put them in `/etc/systemd/system`.
51In addition, all of this is scripted for the "standard" install. Yes,
52you're always supposed to review scripts before running them but who
53really does that? When I see a project whose only supported installation
54method is a script, I nope right on out of there for exactly this
55reason. I know how my system *is* set up and I know how I *want* it set
56up. I can't stand it when they assume they know what's best. Just tell
57me what you *recommend* and I'll make decisions from there.
58
59`</rant>`
60
61## NGINX & RTMP
62RTMP stands for [Real-Time Messaging
63Protocol](https://wikipedia.org/wiki/Real-Time_Messaging_Protocol) and
64facilitates streaming audio, video, and other data over the internet in
65real-time. The NGINX module mentioned above adds functionality to NGINX
66that allows it to handle RTMP streams and turn them into something a
67browser or media streaming client can use. Connecting directly via
68`rtmp://example.com/live/stream` is not very widely supported so
69protocols such as
70[MPEG-DASH](https://wikipedia.org/wiki/Dynamic_Adaptive_Streaming_over_HTTP)
71and [HLS](https://wikipedia.org/wiki/HTTP_Live_Streaming) are used
72instead.
73
74On Debian-based systems, adding RTMP functionality to NGINX is as simple
75as `apt install libnginx-mod-rtmp`. After that, you'll need to add some
76things to your `nginx.conf` and whatever host file you're using for your
77website.
78
79``` c
80rtmp {
81 server {
82 listen 1935;
83 application live {
84 deny publish all;
85 allow publish 127.0.0.1;
86 live on;
87 interleave on;
88 hls on;
89 hls_path /tmp/hls;
90 hls_fragment 15s;
91 dash on;
92 dash_path /tmp/dash;
93 dash_fragment 15s;
94 }
95 }
96}
97```
98
99`1935` is the default RTMP port. `deny publish all` means you are
100denying *anyone* from publishing a stream (that includes you. `allow
101publish 127.0.0.1` allows *local* connections to publish content. I'm
102using this as a form of authentication---before streaming anything, I
103have to tunnel my connection to my server via SSH or a VPN. At the
104moment, I'm using SSH:
105
106``` text
107ssh -L 1935:localhost:1935 user@example.com
108```
109
110The other options are just the basics needed to get DASH and HLS to
111work. The only other thing to do is use NGINX as a reverse proxy (sort
112of) to serve the streams. Add this to your site's virtual host.
113
114``` c
115location /dash {
116 root /tmp;
117}
118location /hls {
119 root /tmp;
120}
121```
122
123That's it! Now you'll need to test your stream and verify that it
124actually works.
125
126``` bash
127ffmpeg -re -i video.mp4 -vcodec copy -loop -1 -c:a aac -b:a 160k -ar 44100 -strict -2 -f flv rtmp://example.com/live/stream
128```
129
130This command has FFmpeg play the video and stream it to the server. You
131should then be able to open the stream in something like
132[VLC](https://www.videolan.org/) or [MPV](https://mpv.io/) and watch it
133from anywhere.
134
135``` bash
136mpv https://example.com/dash/stream.mpd
137```
138
139However, I also wanted to embed it in a website and this is where it
140gets a little unstable.
141
142## Browser playback
143`dash.js` is currently one of the best ways to play a live stream in a
144browser plus it's pretty easy to work with. The code can be found [on
145GitHub.](https://github.com/Dash-Industry-Forum/dash.js) Using the setup
146with NGINX I detailed above, this should work perfectly fine out of the
147box.
148
149``` js
150<div>
151 <video id="videoPlayer" poster="/assets/jpgs/stream.jpg" controls></video>
152</div>
153<script src="/assets/js/dash.all.min.js"></script>
154<script>
155(function(){
156 var url = "/dash/stream.mpd";
157 var player = dashjs.MediaPlayer().create();
158 player.initialize(document.querySelector("#videoPlayer"), url, true);
159})();
160</script>
161```
162
163## Web chat
164The last thing every stream needs is something for web chat. I tried a few
165different solutions and had mixed results. The first was
166[KiwiIRC](https://kiwiirc.com/) but the iframe wouldn't even finish loading
167because it connected to so many third parties with a lot of tracking. It
168functions very well and I might set it up on my own site eventually but it was a
169bit much to go through at the time. As an intermediate solution, I embedded my
170instance of [The Lounge,](https://thelounge.chat) a fully-functional web-based
171IRC client. This loaded perfectly right out of the box but it wasn't quite what
172I wanted; there were *too* many options and the friends of mine who tested it
173got frustrated because some of the essential UI elements were hidden due to the
174small viewport. It's just not quite suitable for embedded webchat.
175
176Finally, I landed on [qwebirc](https://qwebirc.org/) and it was pretty
177much *exactly* what I wanted. When the iframe loads, you're prompted to
178enter a nick, you click connect, wait a minute, and done! My one
179complaint is that the theme is very bright but I'll work on that later
180on. It's good enough for now :wink:
181
182**EDIT:** Since the time of writing, I have switched to self-hosting
183[KiwiIRC](https://kiwiirc.com/) so all of the trackers and third parties aren't
184in use. My configs are below but I recommend going through [the
185wiki](https://github.com/kiwiirc/kiwiirc/wiki/Configuration-Options) and making
186your own decisions.
187
188`/etc/kiwiirc/config.conf`
189
190``` ini
191logLevel = 3
192identd = false
193gateway_name = "webircgateway"
194secret = "changeme"
195
196[verify]
197recaptcha_secret = ""
198recaptcha_key = ""
199
200[clients]
201username = "%i"
202realname = "KiwiIRC on secluded.site"
203
204[server.1]
205bind = "0.0.0.0"
206port = 7264
207
208[fileserving]
209enabled = true
210webroot = /usr/share/kiwiirc/
211
212[transports]
213websocket
214sockjs
215kiwiirc
216
217[reverse_proxies]
218127.0.0.0/8
21910.0.0.0/8
220172.16.0.0/12
221192.168.0.0/16
222"::1/128"
223"fd00::/8"
224
225[upstream.1]
226hostname = "irc.nixnet.services"
227port = 6697
228tls = true
229timeout = 5
230throttle = 2
231webirc = ""
232```
233
234`/etc/kiwiirc/client.json`
235
236``` json
237{
238 "windowTitle": "Secluded.Site Chat",
239 "startupScreen": "welcome",
240 "kiwiServer": "/webirc/kiwiirc/",
241 "restricted": true,
242 "hideAdvanced": true,
243 "showAutoComplete": true,
244 "showSendButton": true,
245 "sidebarDefault": "nicklist",
246 "theme": "dark",
247 "themes": [
248 { "name": "Default", "url": "static/themes/default" },
249 { "name": "Dark", "url": "static/themes/dark" },
250 { "name": "Coffee", "url": "static/themes/coffee" },
251 { "name": "GrayFox", "url": "static/themes/grayfox" },
252 { "name": "Nightswatch", "url": "static/themes/nightswatch" },
253 { "name": "Osprey", "url": "static/themes/osprey" },
254 { "name": "Radioactive", "url": "static/themes/radioactive" },
255 { "name": "Sky", "url": "static/themes/sky" }
256 ],
257 "buffers" : {
258 "messageLayout": "compact",
259 "show_timestamps": false,
260 "show_hostnames": false,
261 "show_joinparts": false,
262 "show_topics": true,
263 "show_nick_changes": true,
264 "show_mode_changes": false,
265 "traffic_as_activity": false,
266 "coloured_nicklist": true,
267 "colour_nicknames_in_messages": true,
268 "block_pms": true,
269 "show_emoticons": true,
270 "extra_formatting": true,
271 "mute_sound": false,
272 "hide_message_counts": false,
273 "show_realnames": false,
274 "default_kick_reason": "Your behaviour is not conducive to this environment.",
275 "shared_input": false,
276 "show_message_info": true,
277 "share_typing": true,
278 "flash_title": "off",
279 "nicklist_avatars": true,
280 "show_link_previews": true,
281 "inline_link_previews": true,
282 "inline_link_auto_preview_whitelist": "secluded.site|nixnet.services",
283 "channel": "#secluded"
284 },
285 "startupOptions" : {
286 "server": "irc.nixnet.services",
287 "port": 6697,
288 "tls": true,
289 "direct": false,
290 "channel": "#secluded",
291 "nick": "viewer?",
292 "greetingText": "Welcome!",
293 "infoBackground": "",
294 "infoContent": ""
295 }
296}
297```
298
299## Actually streaming
300Once you're ready to start streaming content, I recommend using [OBS
301Studio.](https://github.com/obsproject/obs-studio/) If you're noticing
302issues with stream performance, play around with your output resolution
303and FPS---those are the biggest factors. To use OBS with NGINX, you'll
304need to go to `Settings`, `Stream`, and set `Server` to
305`rtmp://localhost/live/`. If you're using my configs as they are, the
306key will need to be `stream`. Literally every component requires
307specific paths so, unless you're careful, things will break and you'll
308spend hours trying figure it out like I did. Also don't forget that the
309connection *has* to be tunnelled if you want authentication as I
310mentioned above. If you don't have `localhost:1935` on your streaming
311machine tunnelled to port 1935 on your server, OBS is going to throw
312errors about not being able to connect.
313
314## Summary
315I'm pretty proud of [the set up](/live) I have now but it could still do
316with some improvements. For example, I plan to mess with the CSS and
317make both the video and chat panes *much* wider as well as side-by-side
318rather than on top of each other. Everything is crammed together and
319it's not a very good experience.
320
321## References
322This post has pieces taken from a few other articles and sites that also
323deserve a mention as well as a read. NGINX actually has an [official
324blog
325post](https://www.nginx.com/blog/video-streaming-for-remote-learning-with-nginx/)
326on setting up RTMP streaming (though they compile NGINX from source as
327well) that was a *massive* help. I also found another post that is very
328similar to this one about [HTML5 Live Streaming with
329MPEG-DASH.](https://www.isrv.pw/html5-live-streaming-with-mpeg-dash) A
330good number of the parts are the same but I used the NGINX module in
331Debian repos and they used a fork of it with additional features. My
332NGINX setup was mostly from the NGINX blog post and the embedded stream
333was primarily from Inanity's. I figured out some of the components I
334could use for all of this from [Drew
335DeVault.](https://live.drewdevault.com/)
336
337---
338
339This was posted as part of #100DaysToOffload, an awesome idea (dead link to fedi
340post) from [Kev Quirk.](https://kevq.uk/) If you want to participate, just write
341something every day for 100 days and post a link on social media with the
342hashtag!