diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 839492d..4af0580 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -38,17 +38,16 @@ transcode: # satyr will generate one source quality variant, and the remaining # variants will be of incrementally lower quality and bitrate +# having more than 4-5 variants will start giving diminishing returns on stream quality for cpu load +# if you can't afford to generate at least 3 variants, it's recommended to leave adaptive streaming off + inputflags: "" # additional flags to apply to the input during transcoding outputflags: "" # additional flags to apply to the output during transcoding -# hardware acceleration is a bit difficult to configure programmatically -# this is a good place to do so for your system -# https://trac.ffmpeg.org/wiki/HWAccelIntro is a good place to start - -# having more than 4-5 variants will start giving diminishing returns on stream quality for cpu load -# if you can't afford to generate at least 3 variants, it's recommended to leave adaptive streaming off +hwaccel: +# See HWACCEL.md for information on configuring hardware acceleration. crypto: saltRounds: 12 diff --git a/docs/HWACCEL.md b/docs/HWACCEL.md new file mode 100644 index 0000000..748a7d7 --- /dev/null +++ b/docs/HWACCEL.md @@ -0,0 +1,53 @@ +## Configuration Hardware Acceleration +Satyr supports the NVENC and VA-API hardware acceleration APIs. If you've configured your system correctly (the hard part) it should be enough to set the type and use the default device setting if you only have one hardware acceleration device. + +### System +Configuring the system for any hardware acceleration API involves three main steps: selecting the right drivers, installing the API libraries, and configuring ffmpeg. + +#### NVENC +NVENC in ffmpeg can work with either open-source drivers (nouvea) or nvidia's proprietary drivers. The documentation for your distribution should have instructions for installing these. + +The only system library you should need is the CUDA toolkit, general named cudatoolkit, nvidia-cuda-toolkit, or some variation in your system repositories. +You can also try installing manually from [here](https://developer.nvidia.com/cuda-downloads). + +Most binary distributions provide a version of ffmpeg with NVENC already enabled. If not you can try compiling ffmpeg from source with the `--enable-nvenc` flag. If you use a source based distribution you should be familiar with enabling optional compile flags. + +You can verify that ffmpeg has been set up correctly by checking the output of `ffmpeg -hide_banner -hwaccels | grep cuvid` and `ffmpeg -hide_banner -encoders | grep nvenc`. If you don't see anything, something is wrong. + +#### VA-API +VA-API is an extremely generic API. Although the package names might be different in your distribution, the arch wiki page for hardware acceleration has good information on [driver selection](https://wiki.archlinux.org/index.php/Hardware_video_acceleration#Installation) and [verifying](https://wiki.archlinux.org/index.php/Hardware_video_acceleration#Verifying_VA-API) a VA-API install for a wide range of devices. + +Regardless of driver selection, you will also need libva or the equivalent from your distrubtion, and libva-utils can be helpful as well. + +Most binary distributions provide a version of ffmpeg with VA-API already enabled. If not you can try compiling ffmpeg from source with the `--enable-vaapi` flag. If you use a source based distribution you should be familiar with enabling optional compile flags. + +You can verify that ffmpeg has been set up correctly by checking the output of `ffmpeg -hide_banner -hwaccels | grep vaapi` and `ffmpeg -hide_banner -encoders | grep vaapi`. If you don't see anything, something is wrong. + +### Satyr +``` +# Decoding +hwaccel: +# Enable hardware acceleration for decoding as well as encoding. +# Probably not worth it, hardware decoding won't be any faster compared to software on a vaguely modern CPU +# Hardware decoding also may not support the input format, in which case transcoding will fail + decode: true + +# Only supported for VA-API +# Fall back to software decoding if hardware decoding fails +hwaccel: + decode: 'fallback' + + +# NVENC +hwaccel: + type: 'nvenc' +# device is optional for nvenc + device: 0 +# nvenc wants a device number instead of a path, set to null to use the default + +# VA-API +hwaccel: + type: 'vaapi' +# device is mandatory for va-api + device: '/dev/dri/renderD128' +``` \ No newline at end of file diff --git a/install/config.example.yml b/install/config.example.yml index 4ea6987..67e578c 100644 --- a/install/config.example.yml +++ b/install/config.example.yml @@ -28,10 +28,15 @@ database: transcode: #may result in higher latency if your cpu can't keep up adaptive: false + #more than 3 might cause problems when using hwacceleration variants: 3 #unused right now, will always transcode to dash format: dash +hwaccel: + # see docs/HWACCEL.md for instructions on configuring hardware acceleration + type: null + chat: irc: diff --git a/package-lock.json b/package-lock.json index 813819b..d816fbc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "satyr", - "version": "0.10.1", + "version": "0.10.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1110,9 +1110,9 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "ini": { - "version": "1.3.5", - "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz", - "integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==" + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==" }, "ipaddr.js": { "version": "1.9.0", diff --git a/package.json b/package.json index f11f66b..a97c46d 100644 --- a/package.json +++ b/package.json @@ -10,11 +10,12 @@ "setup": "sh install/setup.sh", "migrate": "ts-node src/migrate.ts", "invite": "ts-node src/cli.ts --invite", + "v3-manual": "ts-node src/v3manual.ts", "make-templates": "nunjucks-precompile -i [\"\\.html$\",\"\\.njk$\"] templates > site/templates.js" }, "repository": { "type": "git", - "url": "https://gitlab.com/knotteye/satyr.git" + "url": "https://git.waldn.net/git/knotteye/satyr.git" }, "dependencies": { "bcrypt": "^5.0.0", diff --git a/site/index.js b/site/index.js index 3cb75d7..56ad90c 100644 --- a/site/index.js +++ b/site/index.js @@ -102,12 +102,15 @@ async function render(path, s){ //root case "/": render('/users/live'); + modifyLinks(); break; case "": render('/users/live'); + modifyLinks(); break; case "/index.html": render('/users/live'); + modifyLinks(); break; //404 default: @@ -215,7 +218,8 @@ async function initPlayer(usr) { onError(e); } } else { - setTimeout(initPlayer, 5000, usr); + if(document.getElementById('video') !== null) + setTimeout(initPlayer, 5000, usr); } } diff --git a/src/config.ts b/src/config.ts index 4c641a4..50669e4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -39,6 +39,11 @@ const config: Object = { connectionTimeout: '1000', insecureAuth: false, debug: false }, localconfig['database']), + hwaccel: Object.assign({ + type: null, + device: null, + decode: false + }, localconfig['hwaccel']), rtmp: Object.assign({ cluster: false, port: 1935, diff --git a/src/database.ts b/src/database.ts index 0d733e7..7a95310 100644 --- a/src/database.ts +++ b/src/database.ts @@ -27,7 +27,7 @@ async function addUser(name: string, password: string){ let dupe = await query('select * from users where username='+raw.escape(name)); if(dupe[0]) return false; await query('INSERT INTO users (username, password_hash, stream_key, record_flag) VALUES ('+raw.escape(name)+', '+raw.escape(hash)+', '+raw.escape(key)+', 0)'); - await query('INSERT INTO user_meta (username, title, about, live) VALUES ('+raw.escape(name)+',\'\',\'\',false)'); + await query('INSERT INTO user_meta (username, title, about, live) VALUES ('+raw.escape(name)+',\' \',\' \',false)'); await query('INSERT INTO chat_integration (username, irc, xmpp, twitch, discord) VALUES ('+raw.escape(name)+',\'\',\'\',\'\',\'\')'); await query('INSERT INTO twitch_mirror (username) VALUES ('+raw.escape(name)+')'); return true; @@ -40,6 +40,7 @@ async function rmUser(name: string){ await query('delete from user_meta where username='+raw.escape(name)+' limit 1'); await query('delete from chat_integration where username='+raw.escape(name)+' limit 1'); await query('delete from twitch_mirror where username='+raw.escape(name)+' limit 1'); + await query('delete from ch_bans where channel='+raw.escape(name)); return true; } diff --git a/src/db/3.ts b/src/db/3.ts new file mode 100644 index 0000000..1606c57 --- /dev/null +++ b/src/db/3.ts @@ -0,0 +1,21 @@ +import * as db from "../database"; +import * as dirty from "dirty"; + +async function run () { + await db.query('CREATE TABLE IF NOT EXISTS ch_bans(channel VARCHAR(25), target VARCHAR(45), time BIGINT, length INT DEFAULT 30)'); + console.log('!!! This migration has a race condition when run from the `npm run migrate` command. If thats how this was called, please re-run this migration manually.\n!!! Run `npm run v3-manual` to do so'); + var bansdb = new dirty('./config/bans.db') + bansdb.on('load', async () => { + bansdb.forEach(async (key, val) => { + let ips = Object.keys(val); + for(var i=0;i {initChat()}); + await initChat(); server.listen(config['http']['port']); } @@ -254,6 +253,7 @@ async function initAPI() { api.register(req.body.username, req.body.password, req.body.confirm, true).then((result) => { if(result[0]) return genToken(req.body.username).then((t) => { res.cookie('Authorization', t, {maxAge: 604800000, httpOnly: true, sameSite: 'Lax'}); + res.cookie('X-Auth-As', req.body.username, {maxAge: 604800000, httpOnly: false, sameSite: 'Lax'}) res.json(result); api.useInvite(req.body.invite); return; @@ -268,6 +268,7 @@ async function initAPI() { api.register(req.body.username, req.body.password, req.body.confirm).then( (result) => { if(result[0]) return genToken(req.body.username).then((t) => { res.cookie('Authorization', t, {maxAge: 604800000, httpOnly: true, sameSite: 'Lax'}); + res.cookie('X-Auth-As', req.body.username, {maxAge: 604800000, httpOnly: false, sameSite: 'Lax'}) res.json(result); return; }); @@ -589,9 +590,10 @@ async function initChat() { socket.on('JOINROOM', async (data) => { let t: any = await db.query('select username from users where username='+db.raw.escape(data)); if(t[0]){ - if(banlist.get(data) && banlist.get(data)[socket['handshake']['address']]){ - if(Math.floor(banlist.get(data)[socket['handshake']['address']]['time'] + (banlist.get(data)[socket['handshake']['address']]['length'] * 60)) < Math.floor(Date.now() / 1000)){ - banlist.set(data, Object.assign({}, banlist.get(data), {[socket['handshake']['address']]: null})); + let b = await db.query('select * from ch_bans where target='+db.raw.escape(socket['handshake']['address'])+' and channel='+db.raw.escape(data)); + if(b[0]){ + if(Math.floor(b[0].time + (b[0].length * 60)) < Math.floor(Date.now() / 1000)){ + await db.query('delete from ch_bans where target='+db.raw.escape(b[0].target)+'and channel='+db.raw.escape(b[0].channel)+' and time='+db.raw.escape(b[0].time)+' and length='+db.raw.escape(b[0].length)); } else { socket.emit('ALERT', 'You are banned from that room'); @@ -680,23 +682,27 @@ async function initChat() { } else socket.emit('ALERT', 'Not authorized to do that.'); }); - socket.on('BAN', (data: Object) => { + socket.on('BAN', async (data: Object) => { if(socket.nick === data['room']){ let id: string = store.get(data['nick']); if(id){ if(Array.isArray(id)) { for(let i=0;i { + socket.on('UNBAN', async (data: Object) => { if(socket.nick === data['room']){ - if(banlist.get(data['room']) && banlist.get(data['room'])[data['ip']]){ - banlist.set(data['room'], Object.assign({}, banlist.get(data['room']), {[data['ip']]: null})); + let b = await db.query('select * from ch_bans where channel='+db.raw.escape(data['room'])+' and target='+db.raw.escape(data['ip'])); + if(b[0]){ + await db.query('delete from ch_bans where channel='+db.raw.escape(data['room'])+' and target='+db.raw.escape(data['ip'])); socket.emit('ALERT', data['ip']+' was unbanned.'); } else @@ -715,13 +722,13 @@ async function initChat() { } else socket.emit('ALERT', 'Not authorized to do that.'); }); - socket.on('LISTBAN', (data: Object) => { + socket.on('LISTBAN', async (data: Object) => { if(socket.nick === data['room']){ - if(banlist.get(data['room'])) { - let bans = Object.keys(banlist.get(data['room'])); + let b = await db.query('select target from ch_bans where channel='+db.raw.escape(data['room'])); + if(b[0]) { let str = ''; - for(let i=0;i { execFile(config['media']['ffmpeg'], r, {maxBuffer: Infinity}, (err, stdout, stderr) => { - /*console.log(err); - console.log(stdout); - console.log(stderr);*/ + //console.log(r); + //console.log(err); + //console.log(stdout); + //console.log(stderr); }); }); break; @@ -126,29 +127,51 @@ function init () { async function transCommand(user: string, key: string): Promise{ let args: string[] = ['-loglevel', 'fatal', '-y']; + let vcodec: string = 'libx264'; + if(config['hwaccel']['type'] === 'nvenc'){ + vcodec = 'h264_nvenc'; + if(config['hwaccel']['decode']){ + args = args.concat(['-hwaccel', 'cuda']); + if(config['hwaccel']['device']) + args = args.concat(['-hwaccel_device', config['hwaccel']['device']]); + args = args.concat(['-hwaccel_output_format', 'cuda']); + } + } + else if (config['hwaccel']['type'] === 'vaapi') { + vcodec = 'h264_vaapi'; + if(config['hwaccel']['decode'] === 'fallback'){ + args = args.concat('init_hw_device', 'vaapi=foo:'+config['hwaccel']['device'], '-hwaccel vaapi', '-hwaccel_output_format', 'vaapi', '-hwaccel_device', 'foo'); + } else if (config['hwaccel']['decode']) { + args = args.concat(['-hwaccel', 'vaapi', '-hwaccel_output_format', 'vaapi', '-vaapi_device', config['hwaccel']['device']]); + } + } if(config['transcode']['inputflags'] !== null && config['transcode']['inputflags'] !== "") args = args.concat(config['transcode']['inputflags'].split(" ")); args = args.concat(['-i', 'rtmp://127.0.0.1:'+config['rtmp']['port']+'/'+config['media']['privateEndpoint']+'/'+key, '-movflags', '+faststart']); if(config['transcode']['adaptive']===true && config['transcode']['variants'] > 1) { for(let i=0;i 51 ? 51 : Math.floor(18 + (i * 7)); - args = args.concat(['-crf:'+i, ''+crf]); + if(!config['hwaccel']['type']){ + for(let i=1;i 51 ? 51 : Math.floor(18 + (i * 7)); + args = args.concat(['-crf:'+i, ''+crf]); + } } for(let i=1;i