├── client ├── config.json ├── api │ ├── getRawChunk.js │ ├── getLive.js │ ├── getPlaylist.js │ └── getRanges.js ├── package.json ├── static │ ├── index.html │ ├── styles.css │ └── script.js ├── server.js ├── utils │ └── async.js └── package-lock.json ├── recorder ├── package.json ├── config.json ├── recorder.js └── package-lock.json ├── configurator ├── static │ ├── index.html │ ├── styles.css │ └── script.js └── server.js ├── LICENSE ├── .gitignore └── README.md /client/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "redisEndpoint": "redis://:@:", 3 | "telegramToken": "", 4 | "port": 8080 5 | } 6 | -------------------------------------------------------------------------------- /client/api/getRawChunk.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fetch = require('node-fetch'); 3 | module.exports = async(filename) => 4 | fetch( 5 | `https://api.telegram.org/file/bot${global.config.telegramToken}/documents/${filename}` 6 | ).then((res) => res.buffer()); 7 | -------------------------------------------------------------------------------- /recorder/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "recorder", 3 | "version": "1.0.0", 4 | "description": "hls recorder", 5 | "main": "recorder.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Mike Zhukov", 10 | "license": "MIT", 11 | "dependencies": { 12 | "ioredis": "^4.14.1", 13 | "telegraf": "^3.33.3" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "1.0.0", 4 | "description": "hls client", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node server.js" 9 | }, 10 | "author": "Mike Zhukov", 11 | "license": "MIT", 12 | "dependencies": { 13 | "ioredis": "^4.14.1", 14 | "telegraf": "^3.33.3" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /client/api/getLive.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = async(camera, isProxied = false) => { 3 | const [id, timestamp] = await global.redis.zrange( 4 | camera, 5 | -1, 6 | -1, 7 | 'WITHSCORES' 8 | ); 9 | return `#EXTM3U 10 | #EXT-X-VERSION:3 11 | #EXT-X-TARGETDURATION:1 12 | #EXT-X-MEDIA-SEQUENCE:${timestamp} 13 | #EXTINF:1.000000, 14 | ${ 15 | isProxied ? 16 | `/api/getRawChunk/${(await global.telegram.getFileLink(id)) 17 | .split('/') 18 | .pop()}` : 19 | await global.telegram.getFileLink(id) 20 | }`; 21 | }; 22 | -------------------------------------------------------------------------------- /recorder/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "redisEndpoint": "redis://:@:", 3 | "telegramToken": "", 4 | "telegramChats": [0, 1, 2, 3], 5 | "inputSettings": "-f video4linux2 -input_format yuyv422 -video_size 640x480 -i /dev/video0", 6 | "codecSettings": "-c:v libx264 -crf 21 -preset veryfast -g 30 -sc_threshold 0", 7 | "hlsSettings": "-f hls -hls_time 1 -hls_start_number_source epoch -hls_flags omit_endlist -hls_list_size 1", 8 | "overlaySettings": "-vf drawtext=text=%{localtime}:borderw=2:bordercolor=white", 9 | "internalPort": 8000, 10 | "cameraName": "webcam" 11 | } 12 | -------------------------------------------------------------------------------- /configurator/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 |

10 |

11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
ProgramConfiguration
21 |
22 |
23 | 24 | 25 |
26 |
27 |
28 | 29 | -------------------------------------------------------------------------------- /client/api/getPlaylist.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const start = `#EXTM3U 3 | #EXT-X-VERSION:3 4 | #EXT-X-TARGETDURATION:1`; 5 | const end = '#EXT-X-ENDLIST'; 6 | const chunk = (url, isProxied) => 7 | `#EXTINF:1.000000,\n${ 8 | isProxied ? `/api/getRawChunk/${url.split('/').pop()}` : url 9 | }`; 10 | module.exports = async( 11 | camera, 12 | startTimestamp, 13 | endTimestamp, 14 | isProxied = false 15 | ) => { 16 | const chunks = await Promise.all( 17 | ( 18 | await global.redis.zrangebyscore( 19 | camera, 20 | parseInt(startTimestamp), 21 | parseInt(endTimestamp) 22 | ) 23 | ).map((id) => 24 | global.telegram.getFileLink(id).then((url) => chunk(url, isProxied)) 25 | ) 26 | ); 27 | chunks.unshift(start); 28 | chunks.push(end); 29 | return chunks.join('\n'); 30 | }; 31 | -------------------------------------------------------------------------------- /client/api/getRanges.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = async(camera, start, end) => 3 | (start && end ? 4 | global.redis.zrangebyscore( 5 | camera, 6 | parseInt(start), 7 | parseInt(end), 8 | 'WITHSCORES' 9 | ) : 10 | global.redis.zrange(camera, 0, 2e9, 'WITHSCORES') 11 | ) 12 | .then((arr) => 13 | arr 14 | .filter((value, index) => index % 2) 15 | .map((timestamp) => parseInt(timestamp)) 16 | ) 17 | .then((arr) => 18 | arr 19 | .map((v, k) => 20 | (v - 1 === arr[k - 1] ? 21 | arr[k + 1] === v + 1 ? 22 | '' : 23 | `-${v},` : 24 | v + ',') 25 | ) 26 | .join('') 27 | .split(',-') 28 | .join('-') 29 | ) 30 | .then((str) => str.substring(0, str.length - 1)) 31 | .then((arr) => 32 | arr 33 | .split(',') 34 | .map((range) => 35 | range.split('-').map((timestamp) => parseInt(timestamp)) 36 | ) 37 | ) 38 | .then(JSON.stringify); 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Mike Zhukov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /configurator/static/styles.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto:400,500,300,700); 2 | body{ 3 | background: -webkit-linear-gradient(left, #25c481, #25b7c4); 4 | background: linear-gradient(to right, #25c481, #25b7c4); 5 | font-family: 'Roboto', sans-serif; 6 | } 7 | section{ 8 | margin: 50px; 9 | } 10 | h1{ 11 | font-size: 30px; 12 | color: #fff; 13 | text-transform: uppercase; 14 | font-weight: 300; 15 | text-align: center; 16 | margin-bottom: 15px; 17 | } 18 | table{ 19 | width:100%; 20 | table-layout: fixed; 21 | } 22 | .tableHeader{ 23 | background-color: rgba(255,255,255,0.3); 24 | } 25 | .tableBody{ 26 | height:300px; 27 | overflow-x:auto; 28 | margin-top: 0px; 29 | border: 1px solid rgba(255,255,255,0.3); 30 | } 31 | th{ 32 | padding: 20px 15px; 33 | text-align: left; 34 | font-weight: 500; 35 | font-size: 12px; 36 | color: #fff; 37 | text-transform: uppercase; 38 | } 39 | td{ 40 | padding: 15px; 41 | text-align: left; 42 | vertical-align:middle; 43 | font-weight: 300; 44 | font-size: 12px; 45 | color: #fff; 46 | border-bottom: solid 1px rgba(255,255,255,0.1); 47 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /client/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 |

13 | 14 | 15 |

16 |
17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
StartEndPlay
27 |
28 |
29 | 30 | 31 |
32 |
33 |
34 | 35 | -------------------------------------------------------------------------------- /client/static/styles.css: -------------------------------------------------------------------------------- 1 | @import url(https://fonts.googleapis.com/css?family=Roboto:400,500,300,700); 2 | body{ 3 | background: -webkit-linear-gradient(left, #25c481, #25b7c4); 4 | background: linear-gradient(to right, #25c481, #25b7c4); 5 | font-family: 'Roboto', sans-serif; 6 | } 7 | section{ 8 | margin: 50px; 9 | } 10 | h1{ 11 | font-size: 30px; 12 | color: #fff; 13 | text-transform: uppercase; 14 | font-weight: 300; 15 | text-align: center; 16 | margin-bottom: 15px; 17 | } 18 | table{ 19 | width:100%; 20 | table-layout: fixed; 21 | } 22 | .playerContainer{ 23 | text-align: center; 24 | margin: 15px; 25 | } 26 | #player{ 27 | display: inline-block; 28 | } 29 | .tableHeader{ 30 | background-color: rgba(255,255,255,0.3); 31 | } 32 | .tableBody{ 33 | height:300px; 34 | overflow-x:auto; 35 | margin-top: 0px; 36 | border: 1px solid rgba(255,255,255,0.3); 37 | } 38 | th{ 39 | padding: 20px 15px; 40 | text-align: left; 41 | font-weight: 500; 42 | font-size: 12px; 43 | color: #fff; 44 | text-transform: uppercase; 45 | } 46 | td{ 47 | padding: 15px; 48 | text-align: left; 49 | vertical-align:middle; 50 | font-weight: 300; 51 | font-size: 12px; 52 | color: #fff; 53 | border-bottom: solid 1px rgba(255,255,255,0.1); 54 | } -------------------------------------------------------------------------------- /configurator/static/script.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const callApi = async(method, args) => 3 | fetch('/', { 4 | method: 'POST', 5 | body: JSON.stringify({ 6 | method, 7 | args, 8 | }), 9 | }); 10 | const createManager = async(name) => { 11 | const line = document.createElement('tr'); 12 | const nameCell = document.createElement('td'); 13 | nameCell.innerHTML = name; 14 | const config = await callApi('fetchConfig', { name }).then((res) => 15 | res.text() 16 | ); 17 | const textarea = document.createElement('textarea'); 18 | textarea.value = config; 19 | const configCell = document.createElement('td'); 20 | configCell.appendChild(textarea); 21 | const applyButton = document.createElement('button'); 22 | applyButton.innerHTML = 'Apply config'; 23 | applyButton.onclick = () => 24 | callApi('setConfig', { name, config: JSON.parse(textarea.value) }); 25 | const startButton = document.createElement('button'); 26 | startButton.innerHTML = 'Start'; 27 | startButton.onclick = () => callApi('startProcess', { name }); 28 | const buttonsCell = document.createElement('td'); 29 | buttonsCell.appendChild(applyButton); 30 | buttonsCell.appendChild(startButton); 31 | line.appendChild(nameCell); 32 | line.appendChild(configCell); 33 | line.appendChild(buttonsCell); 34 | document.getElementById('programs').appendChild(line); 35 | }; 36 | window.onload = () => { 37 | createManager('client'); 38 | createManager('recorder'); 39 | }; 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Streaming and recording application with Telegram-based video storage and Redis-based metadata storage 2 | #### Usage 3 | Run "npm i" inside relevant application folder before usage 4 | ##### Recorder 5 | Recording application relies on ffmpeg as hls stream source. Once the chunk is encoded and sent to local HTTP server using PUT method it's uploaded to Telegram servers using Bot API by sending to several private chats(it's better to use more than one to prevent "429 Too Many Requests" errors). Each fragment is expected to last 1 second and contain UNIX timestamp in it's name. 6 | It's required to fill config with your Telegram Bot API token, chat identificators and Redis endpoint URL. You can also adjust other settings to better fit your device: 7 | ```javascript 8 | { 9 | "redisEndpoint": "redis://:@:", 10 | "telegramToken": "", 11 | "telegramChats": [0, 1, 2, 3], 12 | "inputSettings": "-f video4linux2 -input_format yuyv422 -video_size 640x480 -i /dev/video0", 13 | "codecSettings": "-c:v libx264 -crf 21 -preset veryfast -g 30 -sc_threshold 0", 14 | "hlsSettings": "-f hls -hls_time 1 -hls_start_number_source epoch -hls_flags omit_endlist -hls_list_size 1", 15 | "overlaySettings": "-vf drawtext=text=%{localtime}:borderw=2:bordercolor=white", 16 | "internalPort": 8000, 17 | "cameraName": "webcam" 18 | } 19 | ``` 20 | ##### Client 21 | Client requires only Telegram Bot API token and Redis endpoint URL 22 | ```javascript 23 | { 24 | "redisEndpoint": "redis://:@:", 25 | "telegramToken": "", 26 | "port": 8080 27 | } 28 | ``` 29 | With server running, client web application is available at port 8000. Playlist links are intended to be used in media players(e.g VLC) 30 | -------------------------------------------------------------------------------- /configurator/server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fs = require('fs'); 3 | const path = require('path'); 4 | const http = require('http'); 5 | const { exec } = require('child_process'); 6 | 7 | const httpError = (res, status, message) => { 8 | res.statusCode = status; 9 | res.end(message); 10 | }; 11 | const processes = new Map(); 12 | const methods = new Map(); 13 | methods.set('fetchConfig', async({ name }) => 14 | fs.promises.readFile(path.join('..', name, 'config.json')) 15 | ); 16 | methods.set('setConfig', async({ name, config }) => 17 | fs.promises.writeFile( 18 | path.join('..', name, 'config.json'), 19 | JSON.stringify(config) 20 | ) 21 | ); 22 | methods.set('startProcess', async({ name }) => { 23 | if (!processes.has(name)) { 24 | const filename = (await fs.promises.readdir(path.join('..', name))) 25 | .filter((filename) => filename.endsWith('.js')) 26 | .pop(); 27 | processes.set( 28 | name, 29 | exec(`cd ../${name};node ${filename}`, console.log) 30 | ); 31 | } 32 | }); 33 | 34 | http 35 | .createServer((req, res) => { 36 | if (req.method === 'GET') { 37 | const url = req.url === '/' ? 'index.html' : req.url; 38 | const filepath = path.join('static', url); 39 | fs.exists(filepath, (exists) => { 40 | if (exists) fs.createReadStream(filepath).pipe(res); 41 | else httpError(res, 404, 'File is not found'); 42 | }); 43 | } else if (req.method === 'POST') { 44 | try { 45 | const chunks = []; 46 | req.on('data', (data) => chunks.push(data)); 47 | req.on('end', async() => { 48 | const { method, args } = JSON.parse(chunks.join('')); 49 | res.end(await methods.get(method)(args)); 50 | }); 51 | } catch (e) { 52 | httpError(500, 'Server error'); 53 | } 54 | } 55 | }) 56 | .listen(3000); 57 | -------------------------------------------------------------------------------- /recorder/recorder.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const http = require('http'); 6 | const { exec } = require('child_process'); 7 | 8 | const Redis = require('ioredis'); 9 | const Telegram = require('telegraf/telegram'); 10 | 11 | const config = JSON.parse(fs.readFileSync('config.json')); 12 | 13 | const rotate = arr => { 14 | let i = -1; 15 | return () => { 16 | i++; 17 | i %= arr.length; 18 | return arr[i]; 19 | }; 20 | }; 21 | const chatId = rotate(config.telegramChats); 22 | 23 | const redis = new Redis(config.redisEndpoint); 24 | const telegram = new Telegram(config.telegramToken); 25 | 26 | http 27 | .createServer(async (req, res) => { 28 | const { url } = req; 29 | res.writeHead(200, {}); 30 | if (req.method === 'PUT') { 31 | try { 32 | if (url.endsWith('.ts')) { 33 | const [camera, key] = path 34 | .basename(url, '.ts') 35 | .match(/[a-z]+|[^a-z]+/g); 36 | const timestamp = parseInt(key); 37 | if ((await redis.zrangebyscore(camera, timestamp, timestamp)).length) 38 | await redis.zremrangebyscore(camera, timestamp, timestamp); 39 | else { 40 | const id = await telegram 41 | .sendDocument(chatId(), { 42 | source: req, 43 | filename: path.basename(url) 44 | }) 45 | .then(msg => msg.document.file_id); 46 | redis.zadd(camera, timestamp, id); 47 | } 48 | } 49 | } catch (err) { 50 | console.log(err.message); 51 | } 52 | } 53 | }) 54 | .listen(config.internalPort); 55 | const { 56 | inputSettings, 57 | codecSettings, 58 | hlsSettings, 59 | overlaySettings, 60 | internalPort, 61 | cameraName 62 | } = config; 63 | const method = `-method PUT http://127.0.0.1:${internalPort}/${cameraName}.m3u8`; 64 | const videoConfig = `${inputSettings} ${codecSettings} ${hlsSettings}`; 65 | exec(`ffmpeg ${videoConfig} ${overlaySettings} ${method}`); 66 | -------------------------------------------------------------------------------- /client/server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const http = require('http'); 6 | 7 | const Redis = require('ioredis'); 8 | const Telegram = require('telegraf/telegram'); 9 | 10 | const { setMaximalConcurrency } = require('./utils/async'); 11 | 12 | const api = new Map(); 13 | const apiPath = './api/'; 14 | const httpError = (res, status, message) => { 15 | res.statusCode = status; 16 | res.end(message); 17 | }; 18 | 19 | global.config = JSON.parse(fs.readFileSync('config.json')); 20 | 21 | global.redis = new Redis(global.config.redisEndpoint); 22 | global.telegram = new Telegram(global.config.telegramToken); 23 | global.telegram.getFileLink = setMaximalConcurrency( 24 | global.telegram.getFileLink.bind(global.telegram), 25 | 16 26 | ); 27 | 28 | fs.readdirSync(apiPath).forEach((name) => { 29 | const filePath = apiPath + name; 30 | const key = path.basename(filePath, '.js'); 31 | try { 32 | const libPath = require.resolve(filePath); 33 | delete require.cache[libPath]; 34 | } catch (e) { 35 | return; 36 | } 37 | try { 38 | const method = require(filePath); 39 | api.set(key, method); 40 | } catch (e) { 41 | api.delete(name); 42 | } 43 | }); 44 | 45 | http 46 | .createServer(async(req, res) => { 47 | const url = req.url === '/' ? '/index.html' : req.url; 48 | const [first, second, ...args] = url.substring(1).split('/'); 49 | if (first === 'api') { 50 | const method = api.get(second); 51 | try { 52 | const result = await method(...args); 53 | if (typeof result !== 'string' && !(result instanceof Buffer)) { 54 | httpError(res, 500, 'Server error'); 55 | return; 56 | } 57 | res.end(result); 58 | } catch (e) { 59 | httpError(res, 500, 'Server error'); 60 | } 61 | } else { 62 | const path = `./static${url}`; 63 | fs.exists(path, (exists) => { 64 | if (exists) fs.createReadStream(path).pipe(res); 65 | else httpError(res, 404, 'File is not found'); 66 | }); 67 | } 68 | }) 69 | .listen(global.config.port); 70 | -------------------------------------------------------------------------------- /client/static/script.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const callApi = async(method, ...args) => 3 | fetch(`/api/${method}/${args.join('/')}`).then((res) => res.text()); 4 | const playSource = (src) => { 5 | const player = videojs('player'); 6 | player.src({ 7 | src, 8 | type: 'application/x-mpegURL', 9 | }); 10 | player.play(); 11 | }; 12 | const createRangeItem = ([start, end]) => { 13 | const line = document.createElement('tr'); 14 | const startCell = document.createElement('td'); 15 | startCell.innerHTML = new Date(start * 1000).toUTCString(); 16 | const endCell = document.createElement('td'); 17 | endCell.innerHTML = new Date(end * 1000).toUTCString(); 18 | const playButtonCell = document.createElement('td'); 19 | const playButton = document.createElement('button'); 20 | playButton.innerHTML = 'Play'; 21 | playButton.onclick = () => 22 | playSource( 23 | `/api/getPlaylist/${localStorage.getItem( 24 | 'camera' 25 | )}/${start}/${end}/proxied.m3u8` 26 | ); 27 | playButtonCell.appendChild(playButton); 28 | line.appendChild(startCell); 29 | line.appendChild(endCell); 30 | line.appendChild(playButtonCell); 31 | return line; 32 | }; 33 | window.onload = () => { 34 | if (!localStorage.getItem('camera')) 35 | localStorage.setItem('camera', prompt('Enter camera name:')); 36 | document.getElementById('changeButton').onclick = () => { 37 | localStorage.setItem('camera', prompt('Enter camera name:')); 38 | document.location.reload(true); 39 | }; 40 | document.getElementById('cameraName').innerHTML = localStorage.getItem( 41 | 'camera' 42 | ); 43 | const rangeItems = document.getElementById('rangeItems'); 44 | const rangesPromise = callApi( 45 | 'getRanges', 46 | localStorage.getItem('camera') 47 | ).then(JSON.parse); 48 | rangesPromise.then((arr) => 49 | arr.map(createRangeItem).forEach(rangeItems.appendChild.bind(rangeItems)) 50 | ); 51 | rangesPromise.then((arr) => { 52 | if (new Date() / 1000 - arr[arr.length - 1][1] < 30) { 53 | document.getElementById('liveButton').onclick = () => 54 | playSource(`/api/getLive/${localStorage.getItem('camera')}/proxied.m3u8`); 55 | } else { 56 | document.getElementById('liveButton').style.display = 'none'; 57 | } 58 | }); 59 | }; 60 | -------------------------------------------------------------------------------- /client/utils/async.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | const setMaximalConcurrency = (asyncFunction, threads) => { 3 | let free = threads; 4 | const queue = []; 5 | const run = () => { 6 | if (free) { 7 | const task = queue.shift(); 8 | if (task) { 9 | free--; 10 | const {args, resolve, reject} = task; 11 | asyncFunction(...args) 12 | .then(resolve) 13 | .catch(reject); 14 | } 15 | } 16 | }; 17 | return (...args) => 18 | new Promise((res, rej) => { 19 | queue.push({ 20 | args, 21 | resolve: value => { 22 | free++; 23 | run(); 24 | return res(value); 25 | }, 26 | reject: reason => { 27 | free++; 28 | run(); 29 | return rej(reason); 30 | } 31 | }); 32 | run(); 33 | }); 34 | }; 35 | const setThrottling = (asyncFunction, timeout) => { 36 | let allowed = true; 37 | const queue = []; 38 | const run = () => { 39 | if (allowed) { 40 | const task = queue.shift(); 41 | if (task) { 42 | allowed = false; 43 | setTimeout(() => { 44 | allowed = true; 45 | run(); 46 | }, timeout); 47 | const {args, resolve, reject} = task; 48 | asyncFunction(...args) 49 | .then(resolve) 50 | .catch(reject); 51 | } 52 | } 53 | }; 54 | return (...args) => 55 | new Promise((resolve, reject) => { 56 | queue.push({ 57 | args, 58 | resolve, 59 | reject 60 | }); 61 | run(); 62 | }); 63 | }; 64 | const setRetryCount = (asyncFunction, count = Infinity) => (...args) => 65 | count 66 | ? asyncFunction(...args).catch(() => 67 | setRetryCount(asyncFunction, count - 1)(...args) 68 | ) 69 | : asyncFunction(...args); 70 | module.exports = { 71 | setMaximalConcurrency, 72 | setThrottling, 73 | setRetryCount 74 | }; 75 | -------------------------------------------------------------------------------- /client/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@types/node": { 8 | "version": "12.12.7", 9 | "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.7.tgz", 10 | "integrity": "sha512-E6Zn0rffhgd130zbCbAr/JdXfXkoOUFAKNs/rF8qnafSJ8KYaA/j3oz7dcwal+lYjLA7xvdd5J4wdYpCTlP8+w==" 11 | }, 12 | "cluster-key-slot": { 13 | "version": "1.1.0", 14 | "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz", 15 | "integrity": "sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==" 16 | }, 17 | "debug": { 18 | "version": "4.1.1", 19 | "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", 20 | "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", 21 | "requires": { 22 | "ms": "^2.1.1" 23 | } 24 | }, 25 | "denque": { 26 | "version": "1.4.1", 27 | "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", 28 | "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" 29 | }, 30 | "ioredis": { 31 | "version": "4.14.1", 32 | "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.14.1.tgz", 33 | "integrity": "sha512-94W+X//GHM+1GJvDk6JPc+8qlM7Dul+9K+lg3/aHixPN7ZGkW6qlvX0DG6At9hWtH2v3B32myfZqWoANUJYGJA==", 34 | "requires": { 35 | "cluster-key-slot": "^1.1.0", 36 | "debug": "^4.1.1", 37 | "denque": "^1.1.0", 38 | "lodash.defaults": "^4.2.0", 39 | "lodash.flatten": "^4.4.0", 40 | "redis-commands": "1.5.0", 41 | "redis-errors": "^1.2.0", 42 | "redis-parser": "^3.0.0", 43 | "standard-as-callback": "^2.0.1" 44 | } 45 | }, 46 | "lodash.defaults": { 47 | "version": "4.2.0", 48 | "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", 49 | "integrity": "sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=" 50 | }, 51 | "lodash.flatten": { 52 | "version": "4.4.0", 53 | "resolved": "https://registry.npmjs.org/lodash.flatten/-/lodash.flatten-4.4.0.tgz", 54 | "integrity": "sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=" 55 | }, 56 | "ms": { 57 | "version": "2.1.2", 58 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", 59 | "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" 60 | }, 61 | "node-fetch": { 62 | "version": "2.6.1", 63 | "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", 64 | "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" 65 | }, 66 | "redis-commands": { 67 | "version": "1.5.0", 68 | "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.5.0.tgz", 69 | "integrity": "sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==" 70 | }, 71 | "redis-errors": { 72 | "version": "1.2.0", 73 | "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", 74 | "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=" 75 | }, 76 | "redis-parser": { 77 | "version": "3.0.0", 78 | "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", 79 | "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", 80 | "requires": { 81 | "redis-errors": "^1.0.0" 82 | } 83 | }, 84 | "sandwich-stream": { 85 | "version": "2.0.2", 86 | "resolved": "https://registry.npmjs.org/sandwich-stream/-/sandwich-stream-2.0.2.tgz", 87 | "integrity": "sha512-jLYV0DORrzY3xaz/S9ydJL6Iz7essZeAfnAavsJ+zsJGZ1MOnsS52yRjU3uF3pJa/lla7+wisp//fxOwOH8SKQ==" 88 | }, 89 | "standard-as-callback": { 90 | "version": "2.0.1", 91 | "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.0.1.tgz", 92 | "integrity": "sha512-NQOxSeB8gOI5WjSaxjBgog2QFw55FV8TkS6Y07BiB3VJ8xNTvUYm0wl0s8ObgQ5NhdpnNfigMIKjgPESzgr4tg==" 93 | }, 94 | "telegraf": { 95 | "version": "3.33.3", 96 | "resolved": "https://registry.npmjs.org/telegraf/-/telegraf-3.33.3.tgz", 97 | "integrity": "sha512-ItSAeE9OjFH+X0rS8DeErccoUZRy2hBl+mDjFWDqyZWyRElxA5L178UpJV7tM6hCVN/leFY+9orfra2JtX4AyQ==", 98 | "requires": { 99 | "@types/node": "^12.0.4", 100 | "debug": "^4.0.1", 101 | "node-fetch": "^2.2.0", 102 | "sandwich-stream": "^2.0.1", 103 | "telegram-typings": "^3.6.0" 104 | } 105 | }, 106 | "telegram-typings": { 107 | "version": "3.6.1", 108 | "resolved": "https://registry.npmjs.org/telegram-typings/-/telegram-typings-3.6.1.tgz", 109 | "integrity": "sha512-njVv1EAhIZnmQVLocZEADYUyqA1WIXuVcDYlsp+mXua/XB0pxx+PKtMSPeZ/EE4wPWTw9h/hA9ASTT6yQelkiw==" 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /recorder/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "recorder", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@types/node": { 8 | "version": "12.12.7", 9 | "resolved": "https://registry.npmjs.org/@types/node/-/node-12.12.7.tgz", 10 | "integrity": "sha512-E6Zn0rffhgd130zbCbAr/JdXfXkoOUFAKNs/rF8qnafSJ8KYaA/j3oz7dcwal+lYjLA7xvdd5J4wdYpCTlP8+w==" 11 | }, 12 | "cluster-key-slot": { 13 | "version": "1.1.0", 14 | "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz", 15 | "integrity": "sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==" 16 | }, 17 | "debug": { 18 | "version": "4.1.1", 19 | "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", 20 | "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", 21 | "requires": { 22 | "ms": "^2.1.1" 23 | } 24 | }, 25 | "denque": { 26 | "version": "1.4.1", 27 | "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", 28 | "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" 29 | }, 30 | "ioredis": { 31 | "version": "4.14.1", 32 | "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-4.14.1.tgz", 33 | "integrity": "sha512-94W+X//GHM+1GJvDk6JPc+8qlM7Dul+9K+lg3/aHixPN7ZGkW6qlvX0DG6At9hWtH2v3B32myfZqWoANUJYGJA==", 34 | "requires": { 35 | "cluster-key-slot": "^1.1.0", 36 | "debug": "^4.1.1", 37 | "denque": "^1.1.0", 38 | "lodash.defaults": "^4.2.0", 39 | "lodash.flatten": "^4.4.0", 40 | "redis-commands": "1.5.0", 41 | "redis-errors": "^1.2.0", 42 | "redis-parser": "^3.0.0", 43 | "standard-as-callback": "^2.0.1" 44 | } 45 | }, 46 | "lodash.defaults": { 47 | "version": "4.2.0", 48 | "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz", 49 | "integrity": "sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=" 50 | }, 51 | "lodash.flatten": { 52 | "version": "4.4.0", 53 | "resolved": "https://registry.npmjs.org/lodash.flatten/-/lodash.flatten-4.4.0.tgz", 54 | "integrity": "sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=" 55 | }, 56 | "ms": { 57 | "version": "2.1.2", 58 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", 59 | "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" 60 | }, 61 | "node-fetch": { 62 | "version": "2.6.1", 63 | "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", 64 | "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" 65 | }, 66 | "redis-commands": { 67 | "version": "1.5.0", 68 | "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.5.0.tgz", 69 | "integrity": "sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==" 70 | }, 71 | "redis-errors": { 72 | "version": "1.2.0", 73 | "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", 74 | "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=" 75 | }, 76 | "redis-parser": { 77 | "version": "3.0.0", 78 | "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", 79 | "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", 80 | "requires": { 81 | "redis-errors": "^1.0.0" 82 | } 83 | }, 84 | "sandwich-stream": { 85 | "version": "2.0.2", 86 | "resolved": "https://registry.npmjs.org/sandwich-stream/-/sandwich-stream-2.0.2.tgz", 87 | "integrity": "sha512-jLYV0DORrzY3xaz/S9ydJL6Iz7essZeAfnAavsJ+zsJGZ1MOnsS52yRjU3uF3pJa/lla7+wisp//fxOwOH8SKQ==" 88 | }, 89 | "standard-as-callback": { 90 | "version": "2.0.1", 91 | "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.0.1.tgz", 92 | "integrity": "sha512-NQOxSeB8gOI5WjSaxjBgog2QFw55FV8TkS6Y07BiB3VJ8xNTvUYm0wl0s8ObgQ5NhdpnNfigMIKjgPESzgr4tg==" 93 | }, 94 | "telegraf": { 95 | "version": "3.33.3", 96 | "resolved": "https://registry.npmjs.org/telegraf/-/telegraf-3.33.3.tgz", 97 | "integrity": "sha512-ItSAeE9OjFH+X0rS8DeErccoUZRy2hBl+mDjFWDqyZWyRElxA5L178UpJV7tM6hCVN/leFY+9orfra2JtX4AyQ==", 98 | "requires": { 99 | "@types/node": "^12.0.4", 100 | "debug": "^4.0.1", 101 | "node-fetch": "^2.2.0", 102 | "sandwich-stream": "^2.0.1", 103 | "telegram-typings": "^3.6.0" 104 | } 105 | }, 106 | "telegram-typings": { 107 | "version": "3.6.1", 108 | "resolved": "https://registry.npmjs.org/telegram-typings/-/telegram-typings-3.6.1.tgz", 109 | "integrity": "sha512-njVv1EAhIZnmQVLocZEADYUyqA1WIXuVcDYlsp+mXua/XB0pxx+PKtMSPeZ/EE4wPWTw9h/hA9ASTT6yQelkiw==" 110 | } 111 | } 112 | } 113 | --------------------------------------------------------------------------------