-
Notifications
You must be signed in to change notification settings - Fork 0
/
media-server.js
69 lines (57 loc) · 1.85 KB
/
media-server.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Media server
var mongoose = require('mongoose');
var path = require('path');
var dotenv = require('dotenv').config({path:path.join(__dirname,"env/"+process.env.NODE_ENV+".env")});
global.db = (global.db ? global.db : mongoose.createConnection(process.env.DB_STRING));
var NodeMediaServer = require('node-media-server');
var path = require('path');
var config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 60,
ping_timeout: 30
},
http: {
port: 8000,
mediaroot:path.join(require('app-root-dir').get(),"media"),
allow_origin: '*'
},
trans: {
ffmpeg: '/usr/bin/ffmpeg',
tasks: [
{
app: 'live',
ac: 'aac',
hls: true,
hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]',
dash: true,
dashFlags: '[f=dash:window_size=3:extra_window_size=5]'
}
]
}
/*auth: {
play: true,
publish: true,
secret: process.env.MEDIA_KEY
}*/
};
var nms = new NodeMediaServer(config);
var Streaming = require('./models/Streaming');
nms.on('prePublish', (id, StreamPath, args) => {
Streaming.create({id:id,path:StreamPath,args:args},function (err,result) {})
});
nms.on('donePublish', (id, StreamPath, args) => {
Streaming.remove({id:id},function (err,result) {})
});
nms.on('doneConnect', (id, args) => {
Streaming.remove({id:id},function (err,result) {})
});
nms.on('prePlay', (id, StreamPath, args) => {
Streaming.update({path: StreamPath}, { $inc: { views: 1 }},function (err,result) {console.log(err);console.log(result);})
});
nms.on('donePlay', (id, StreamPath, args) => {
Streaming.update({path: StreamPath,views:{ $gt:0 }}, { $inc: { views: -1 }},function (err,result) {})
});
nms.run();