zhibo/live-streaming/server/routes/srs.js
2025-12-15 11:21:21 +08:00

54 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const roomStore = require('../store/roomStore');
// POST /api/srs/on_publish - 推流开始回调
router.post('/on_publish', (req, res) => {
const { app, stream } = req.body;
console.log(`[SRS] 推流开始: app=${app}, stream=${stream}`);
// stream 就是 streamKey
const room = roomStore.setLiveStatus(stream, true);
if (room) {
console.log(`[SRS] 房间 "${room.title}" 开始直播`);
} else {
console.log(`[SRS] 未找到对应房间streamKey=${stream}`);
}
// SRS 要求返回 code: 0 表示成功
res.json({ code: 0 });
});
// POST /api/srs/on_unpublish - 推流结束回调
router.post('/on_unpublish', (req, res) => {
const { app, stream } = req.body;
console.log(`[SRS] 推流结束: app=${app}, stream=${stream}`);
const room = roomStore.setLiveStatus(stream, false);
if (room) {
console.log(`[SRS] 房间 "${room.title}" 停止直播`);
}
res.json({ code: 0 });
});
// POST /api/srs/on_play - 观看回调 (可选)
router.post('/on_play', (req, res) => {
const { app, stream } = req.body;
console.log(`[SRS] 观众进入: app=${app}, stream=${stream}`);
res.json({ code: 0 });
});
// POST /api/srs/on_stop - 停止观看回调 (可选)
router.post('/on_stop', (req, res) => {
const { app, stream } = req.body;
console.log(`[SRS] 观众离开: app=${app}, stream=${stream}`);
res.json({ code: 0 });
});
module.exports = router;