-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream.js
More file actions
164 lines (145 loc) · 4.84 KB
/
stream.js
File metadata and controls
164 lines (145 loc) · 4.84 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require('dotenv').config();
const Lesson = require('./models/Lesson');
const Stream = require('./models/Stream');
const axios = require('axios');
const NodeMediaServer = require('node-media-server');
const path = require('path');
const {streamUploadPath} = require('./config');
let ffmpegPath;
if (process.platform === 'win32') {
// Windows path
ffmpegPath = process.env.FFMPEG_WINDOWS_PATH;
} else {
// Linux path
ffmpegPath = process.env.FFMPEG_LINUX_PATH || '/usr/bin/ffmpeg';
}
const nmsConfig = {
// RTMP ingest (from OBS/FFmpeg/etc.)
rtmp: {
port: Number(process.env.NMS_RTMP_PORT || 1935),
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60
},
// HTTP server that serves HLS/DASH files and a simple stat page
http: {
port: Number(process.env.NMS_HTTP_PORT || 8000),
mediaroot: streamUploadPath,
allow_origin: '*'
},
// Transcoding: create HLS and DASH variants from RTMP
trans: {
ffmpeg: ffmpegPath,
tasks: [
{
app: 'live', // publish to rtmp://host/live/STREAM_KEY
hls: true,
hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]',
hlsKeep: false,
mp4: true,
record: true,
ffmpegParams: ["-loglevel", "debug"],
}
],
MediaRoot: streamUploadPath,
},
// Lightweight auth/signed URL features
// auth: {
// api: true, // enable /api endpoints with secret
// play: false, // protect play with signed token (false by default)
// publish: false, // protect publish with signed token (false by default)
// secret: process.env.NMS_SECRET || 'supersecret',
// },
};
const nms = new NodeMediaServer(nmsConfig);
nms.on("preConnect", (id, args) => {
// console.log(
// "[NodeEvent on preConnect]",
// `id=${id} args=${JSON.stringify(args)}`,
// );
// let session = nms.getSession(id);
// session.reject();
});
nms.on("postConnect", (id, args) => {
// console.log(
// "[NodeEvent on postConnect]",
// `id=${id} args=${JSON.stringify(args)}`,
// );
});
nms.on("doneConnect", (id, args) => {
// console.log(
// "[NodeEvent on doneConnect]",
// `id=${id} args=${JSON.stringify(args)}`,
// );
});
nms.on("prePublish", (id, StreamPath, args) => {
// console.log(
// "[NodeEvent on prePublish]",
// `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
// );
// Implement authentication for your streamers...
// let session = nms.getSession(id);
// session.reject();
});
nms.on("postPublish", async (id, StreamPath, args) => {
console.log(
"[NodeEvent on postPublish]",
`id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
);
try {
// Extract stream key from StreamPath, e.g., /live/STREAM_KEY
const parts = StreamPath.split('/');
const streamKey = parts[2];
// Find the Stream document by streamKey
const streamDoc = await Stream.findOne({streamKey: streamKey});
if (!streamDoc){
console.error(`Stream with key ${streamKey} not found.`);
return;
}
// Update the related Lesson
const lesson = await Lesson.findOneAndUpdate(
{_id: streamDoc.lesson},
{
status: 'ongoing',
videoUrl: `${process.env.STREAMING_SERVER_URL}/${nmsConfig.trans.tasks[0].app}/${streamKey}/index.m3u8`,
updatedAt: new Date()
},
{new: true}
);
if (lesson) {
console.log(`Lesson ${lesson._id} updated to ongoing with videoUrl.`);
}else {
console.error("Related lesson not found for the stream.");
}
} catch (err) {
console.error('Error updating lesson on stream publish:', err);
}
});
nms.on("donePublish", (id, StreamPath, args) => {
// console.log(
// "[NodeEvent on donePublish]",
// `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
// );
});
nms.on("prePlay", (id, StreamPath, args) => {
// console.log(
// "[NodeEvent on prePlay]",
// `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
// );
// let session = nms.getSession(id);
// session.reject();
});
nms.on("postPlay", (id, StreamPath, args) => {
// console.log(
// "[NodeEvent on postPlay]",
// `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
// );
});
nms.on("donePlay", (id, StreamPath, args) => {
// console.log(
// "[NodeEvent on donePlay]",
// `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`,
// );
});
module.exports = {nms, nmsConfig};