Skip to content

Commit

Permalink
Merge pull request #12 from TUS-OSK/hls
Browse files Browse the repository at this point in the history
HLS
  • Loading branch information
ilim0t authored Jul 20, 2019
2 parents 62575d0 + 4023f7a commit b050001
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 276 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ ngrok で得られる URL はは変動するので,[API Gateway](https://aws.a
│ └─ GET
├─ /oauth-redirect
│ └─ GET
├─ /photo
│ └─ GET
├─ /stream
│ ├─ GET
│ └ /{file+}
│ └─ GET
└─ /viewer
└─ GET
```
Expand Down Expand Up @@ -135,24 +137,31 @@ ffmpeg \
-i /dev/video0 \
-vcodec libx264 \
-preset veryfast \
-tune zerolatency \
-b 8M \
-vf "drawtext=fontfile=/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: \
text='%{localtime\:%T}': [email protected]: x=7: y=700" \
-f flv rtmp://localhost/live/stream;
-hls_flags delete_segments \
-g 20 \
-f hls [Directory of ramdisk]/output.m3u8
```

**Use USB Camera on macOS**

```bash=
ffmpeg \
-f avfoundation \
-framerate 5 \
-i 0 \
-framerate 30 \
-re -i 0 \
-r 5 \
-vcodec libx264 \
-preset veryfast \
-tune zerolatency \
-vf "drawtext=fontfile=/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: \
text='%{localtime\:%T}': [email protected]: x=7: y=700" \
-f flv rtmp://localhost/live/stream
-hls_flags delete_segments \
-g 20 \
-f hls [Directory of ramdisk]/output.m3u8
```

## Usage
Expand All @@ -171,8 +180,6 @@ Remote URL: https://[AWS_REST_API_ID].execute-api.[REGION].amazonaws.com/prod

とあります。それに`/viewer`を付け加えた`https://[AWS_REST_API_ID].execute-api.[REGION].amazonaws.com/prod/viewer`を開いてください。

対応していないブラウザ,または`https://[AWS_REST_API_ID].execute-api.[REGION].amazonaws.com/prod/viewer?photo=force`へのアクセスでは,動画ではなく画像が表示されます。

> この AWS の URL は半永久的に変わりません
すると,初回実行時(過去に Slack で認証をしていなければ) Slack の認証ページへリダイレクトされます。
Expand Down
65 changes: 18 additions & 47 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,31 @@
const childProcess = require('child_process');
const util = require('util');
const ngrok = require('./ngrok');
const Server = require('./server');
const MediaServer = require('./mediaServer');
const aws = require('./aws');

const exec = util.promisify(childProcess.exec);
const Stream = require('./stream');

const config = {
restApiId: process.env.AWS_REST_API_ID,
viewerResourceId: process.env.VIEWER_RESOURCE_ID,
oauthResourceId: process.env.OAUTH_RESOURCE_ID,
httpMethod: 'GET',
slackClientId: process.env.SLACK_CLIENT_ID,
slackClientSecret: process.env.SLACK_CLIENT_SECRET,
wsId: process.env.WORKSTATION_ID,
privateKey: process.env.LIVE_PRIVATE_KEY,
};

new MediaServer(process.env.LIVE_PRIVATE_KEY).run();

const deamon = async (command) => {
// eslint-disable-next-line no-constant-condition
while (true) {
try {
// eslint-disable-next-line no-await-in-loop
await exec(command);
} catch (e) {
console.error(e);
}
}
};

deamon(
// eslint-disable-next-line no-useless-escape
'ffmpeg -framerate 5 -video_size 960x720 -i /dev/video0 -vcodec libx264 -preset veryfast -b 8M -vf "drawtext=fontfile=/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: text="%{localtime:%T}": [email protected]: x=7: y=700" -f flv rtmp://localhost/live/stream',
);
deamon('ffmpeg -y -i rtmp://localhost/live/stream -r 0.1 -f image2 -updatefirst 1 capture.jpg');

ngrok
.run(process.env.NGROK_TOKEN)
.then((urls) => {
const { siteUrl, liveUrl } = urls;
aws
.run(config, siteUrl)
.then((url) => {
console.log(`Remote URL: ${url}`);
})
.catch(e => console.error(e));
const disk = new Stream('bushitsuchan');
disk
.run()
.then(async (mountPath) => {
const ngrokUrl = await ngrok.run(process.env.NGROK_TOKEN);
const awsUrl = await aws.run(config, ngrokUrl);
// console.log(`Remote URL: ${awsUrl}`);

return new Server(
siteUrl,
liveUrl,
process.env.SLACK_CLIENT_ID,
process.env.SLACK_CLIENT_SECRET,
process.env.WORKSTATION_ID,
process.env.LIVE_PRIVATE_KEY,
);
})
.then((server) => {
server.run().then(port => console.log(`Express app listening on port ${port}`));
const server = new Server(ngrokUrl, awsUrl, mountPath, config);
server.run();
})
.catch(e => console.error(e));
.catch((e) => {
disk.close();
console.error(e);
});
16 changes: 12 additions & 4 deletions aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ const util = require('util');

const exec = util.promisify(childProcess.exec);

module.exports.run = async (config, siteUrl) => {
module.exports.run = async (config, url) => {
exec(`aws apigateway get-resources --rest-api-id ${config.restApiId}`)
.then((result) => {
const { stdout } = result;
return JSON.parse(stdout);
})
.then(resources => Promise.all(
resources.items.map(item => exec(
`aws apigateway put-integration --rest-api-id ${config.restApiId} --resource-id ${item.id} --http-method ${config.httpMethod} --type HTTP_PROXY --integration-http-method ${config.httpMethod} --uri ${siteUrl}${item.path}`,
)),
resources.items.map((item) => {
let command = `aws apigateway put-integration --rest-api-id ${config.restApiId} --resource-id ${item.id} --http-method GET --type HTTP_PROXY --integration-http-method GET`;
command += ` --uri ${url}${item.path.replace(/{([^}+]+)\+}/, '{$1}')}`;
const isMatch = item.path.match(/{([^}+]+)\+}/);
if (isMatch) {
command += ` --request-parameters integration.request.path.${
isMatch[1]
}=method.request.path.${isMatch[1]}`;
}
return exec(command);
}),
))
.then(() => {
exec(`aws apigateway create-deployment --rest-api-id ${config.restApiId} --stage-name prod`);
Expand Down
32 changes: 0 additions & 32 deletions mediaServer.js

This file was deleted.

13 changes: 4 additions & 9 deletions ngrok.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
const ngrok = require('ngrok');

module.exports.run = async (token, sitePort = 3000, livePort = 8000) => {
module.exports.run = async (token, port = 3000) => {
await ngrok.authtoken(token);
const siteUrl = await ngrok.connect(sitePort);
const liveUrl = await ngrok.connect(livePort);
const url = await ngrok.connect(port);

console.log(`Forwarding ${siteUrl} -> localhost:${sitePort}`);
console.log(`Forwarding ${liveUrl} -> localhost:${livePort}`);
return {
siteUrl,
liveUrl,
};
console.log(`Forwarding ${url} -> localhost:${port}`);
return url;
};
Loading

0 comments on commit b050001

Please sign in to comment.