【m】修改为服务器录屏

This commit is contained in:
2026-06-02 02:34:40 +08:00
parent d74a0c8121
commit 66d6f92d1e
21 changed files with 4053 additions and 32 deletions

View File

@@ -0,0 +1,114 @@
import {
incrementRecordingTrackPackets,
registerRecordingPeerCandidate,
registerRecordingPeerOffer,
registerRecordingPeerTrack,
resetRecordingAgents,
startRecordingAgent,
stopRecordingAgent
} from '../src/recording/agent';
import { RecordingSession } from '../src/recording/session-manager';
const session: RecordingSession = {
id: 'recording-1',
connectionId: 'room-1',
status: 'recording',
layout: 'grid',
format: 'webm',
createdAt: '2026-06-01T00:00:00.000Z',
startedAt: '2026-06-01T00:00:00.000Z',
updatedAt: '2026-06-01T00:00:00.000Z'
};
describe('recording agent', () => {
beforeEach(() => {
resetRecordingAgents();
});
test('starts an awaiting media adapter agent', () => {
const agent = startRecordingAgent(session);
expect(agent).toEqual(expect.objectContaining({
id: 'recorder_recording-1',
recordingId: 'recording-1',
connectionId: 'room-1',
status: 'awaiting-media-adapter',
mediaMode: 'webrtc-sendonly'
}));
});
test('stores peer offers for an active agent', () => {
startRecordingAgent(session);
const offer = registerRecordingPeerOffer({
recordingId: 'recording-1',
connectionId: 'room-1',
participantId: 'participant-1',
sdp: 'test-sdp'
});
expect(offer).toEqual(expect.objectContaining({
recordingId: 'recording-1',
connectionId: 'room-1',
participantId: 'participant-1',
sdp: 'test-sdp'
}));
});
test('stores peer candidates for an active agent', () => {
const agent = startRecordingAgent(session);
const candidate = registerRecordingPeerCandidate({
recordingId: 'recording-1',
connectionId: 'room-1',
participantId: 'participant-1',
candidate: 'candidate:1',
sdpMid: '0',
sdpMLineIndex: 0
});
expect(candidate).toEqual(expect.objectContaining({
recordingId: 'recording-1',
connectionId: 'room-1',
participantId: 'participant-1',
candidate: 'candidate:1'
}));
expect(agent.peerCandidates.get('participant-1')).toEqual([candidate]);
});
test('tracks received media and packet counts', () => {
const agent = startRecordingAgent(session);
const track = registerRecordingPeerTrack({
recordingId: 'recording-1',
connectionId: 'room-1',
participantId: 'participant-1',
kind: 'video',
trackId: 'track-1'
});
incrementRecordingTrackPackets({
recordingId: 'recording-1',
participantId: 'participant-1',
trackId: 'track-1'
});
expect(agent.status).toBe('receiving-media');
expect(track).toEqual(expect.objectContaining({
recordingId: 'recording-1',
participantId: 'participant-1',
kind: 'video',
trackId: 'track-1',
rtpPackets: 1
}));
});
test('rejects offers when the agent is stopped', () => {
startRecordingAgent(session);
stopRecordingAgent('recording-1');
expect(registerRecordingPeerOffer({
recordingId: 'recording-1',
connectionId: 'room-1',
participantId: 'participant-1',
sdp: 'test-sdp'
})).toBeNull();
});
});

View File

@@ -0,0 +1,85 @@
import { buildFfmpegCompositionArgs } from '../src/recording/composer';
import { ServerTrackRecordingFile } from '../src/recording/storage';
function file(filename: string, trackKind: string, participantId: string, role = 'participant'): ServerTrackRecordingFile {
return {
meetingId: 'room-1',
directory: 'recordings/room-1',
filename,
filePath: `recordings/room-1/${filename}`,
metadataPath: `recordings/room-1/${filename}.json`,
recordingId: 'recording-1',
participantId,
trackId: `${participantId}-${trackKind}`,
trackKind,
uploadedAt: '2026-06-01T00:00:00.000Z',
metadata: { role }
};
}
describe('recording composer', () => {
test('builds ffmpeg args for host-led video layout and mixed audio', () => {
const args = buildFfmpegCompositionArgs({
videoInputs: [
file('p1-video.webm', 'video', 'p1', 'host'),
file('p2-video.webm', 'video', 'p2')
],
audioInputs: [
file('p1-audio.webm', 'audio', 'p1'),
file('p2-audio.webm', 'audio', 'p2')
],
outputPath: 'recordings/room-1/output.webm',
format: 'webm'
});
expect(args).toContain('-filter_complex');
expect(args.join(' ')).toContain('xstack=inputs=2');
expect(args.join(' ')).toContain('scale=1280:540');
expect(args.join(' ')).toContain('scale=1280:180');
expect(args.join(' ')).toContain('layout=0_0|0_540');
expect(args.join(' ')).toContain('amix=inputs=2');
expect(args).toContain('libvpx-vp9');
expect(args).toContain('libopus');
expect(args[args.length - 1]).toBe('recordings/room-1/output.webm');
});
test('places host in the first row even when host input is not first', () => {
const args = buildFfmpegCompositionArgs({
videoInputs: [
file('p1-video.webm', 'video', 'p1'),
file('host-video.webm', 'video', 'host', 'host'),
file('p2-video.webm', 'video', 'p2')
],
audioInputs: [],
outputPath: 'recordings/room-1/output.webm',
format: 'webm'
});
const filter = args[args.indexOf('-filter_complex') + 1];
expect(args.slice(0, 7)).toEqual([
'-y',
'-i',
'recordings/room-1/host-video.webm',
'-i',
'recordings/room-1/p1-video.webm',
'-i',
'recordings/room-1/p2-video.webm'
]);
expect(filter).toContain('scale=1280:540');
expect(filter).toContain('scale=640:180');
expect(filter).toContain('layout=0_0|0_540|640_540');
});
test('builds mp4 encoder args', () => {
const args = buildFfmpegCompositionArgs({
videoInputs: [file('p1-video.webm', 'video', 'p1')],
audioInputs: [],
outputPath: 'recordings/room-1/output.mp4',
format: 'mp4'
});
expect(args).toContain('libx264');
expect(args).toContain('-pix_fmt');
expect(args).not.toContain('libopus');
});
});

View File

@@ -0,0 +1,46 @@
import {
getRecordingSession,
listRecordingSessions,
resetRecordingSessions,
startRecordingSession,
stopRecordingSession
} from '../src/recording/session-manager';
describe('recording session manager', () => {
beforeEach(() => {
resetRecordingSessions();
});
test('starts and lists a recording session', () => {
const session = startRecordingSession({
connectionId: 'room-1',
layout: 'speaker',
format: 'mp4'
});
expect(session).toEqual(expect.objectContaining({
connectionId: 'room-1',
status: 'recording',
layout: 'speaker',
format: 'mp4'
}));
expect(getRecordingSession(session.id)).toEqual(session);
expect(listRecordingSessions('room-1')).toEqual([session]);
});
test('stops an existing recording session', () => {
const session = startRecordingSession({ connectionId: 'room-1' });
const stopped = stopRecordingSession(session.id);
expect(stopped).toEqual(expect.objectContaining({
id: session.id,
connectionId: 'room-1',
status: 'stopped'
}));
expect(stopped?.stoppedAt).toEqual(expect.any(String));
});
test('rejects missing connection id', () => {
expect(() => startRecordingSession({ connectionId: '' })).toThrow('connectionId is required');
});
});

View File

@@ -0,0 +1,131 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {
createComposedRecordingTarget,
createServerTrackRecordingTarget,
deleteServerTrackRecordingFiles,
listServerTrackRecordingFiles,
sanitizeRecordingPathSegment,
updateServerTrackRecordingMetadataSize,
writeComposedRecordingMetadata,
writeServerTrackRecordingMetadata
} from '../src/recording/storage';
describe('recording storage', () => {
const originalRecordingDir = process.env.RECORDING_DIR;
let tempDir: string;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'recording-storage-'));
process.env.RECORDING_DIR = tempDir;
});
afterEach(() => {
process.env.RECORDING_DIR = originalRecordingDir;
fs.rmSync(tempDir, { recursive: true, force: true });
});
test('sanitizes path segments', () => {
expect(sanitizeRecordingPathSegment('../room:name', 'fallback')).toBe('__room_name');
expect(sanitizeRecordingPathSegment('', 'fallback')).toBe('fallback');
});
test('creates server track target and updates metadata size', () => {
const target = createServerTrackRecordingTarget({
recordingId: 'recording/1',
connectionId: 'room:1',
participantId: 'participant-1',
kind: 'video',
trackId: 'track-1'
});
expect(target.meetingId).toBe('room_1');
expect(target.filePath.startsWith(path.join(tempDir, 'room_1'))).toBe(true);
expect(target.filename).toContain('recording_1-participant-1-video-track-1.webm');
writeServerTrackRecordingMetadata({
recordingId: 'recording-1',
connectionId: 'room-1',
participantId: 'participant-1',
kind: 'video',
trackId: 'track-1',
target
});
fs.writeFileSync(target.filePath, Buffer.from('webm'));
updateServerTrackRecordingMetadataSize(target);
const metadata = JSON.parse(fs.readFileSync(target.metadataPath, 'utf8'));
expect(metadata).toEqual(expect.objectContaining({
meetingId: 'room_1',
filename: target.filename,
mimetype: 'video/webm',
size: 4,
userId: 'server-recorder',
recordingSource: 'server',
participantId: 'participant-1',
trackKind: 'video'
}));
const files = listServerTrackRecordingFiles({
meetingId: 'room_1',
recordingId: 'recording-1',
trackKind: 'video'
});
expect(files).toEqual([
expect.objectContaining({
filename: target.filename,
participantId: 'participant-1',
trackKind: 'video'
})
]);
expect(deleteServerTrackRecordingFiles(files)).toEqual([
target.filename,
`${target.filename}.json`
]);
expect(fs.existsSync(target.filePath)).toBe(false);
expect(fs.existsSync(target.metadataPath)).toBe(false);
expect(listServerTrackRecordingFiles({
meetingId: 'room_1',
recordingId: 'recording-1',
trackKind: 'video'
})).toEqual([]);
});
test('writes composed recording metadata', () => {
const target = createComposedRecordingTarget({
meetingId: 'room-1',
recordingId: 'recording-1',
format: 'webm'
});
fs.writeFileSync(target.filePath, Buffer.from('composed'));
writeComposedRecordingMetadata({
target,
recordingId: 'recording-1',
layout: 'grid',
format: 'webm',
inputs: [
{
...target,
filename: 'p1-video.webm',
recordingId: 'recording-1',
participantId: 'p1',
trackId: 'track-1',
trackKind: 'video',
uploadedAt: '2026-06-01T00:00:00.000Z',
metadata: {}
}
]
});
const metadata = JSON.parse(fs.readFileSync(target.metadataPath, 'utf8'));
expect(metadata).toEqual(expect.objectContaining({
meetingId: 'room-1',
filename: target.filename,
recordingSource: 'server-composed',
size: 8,
layout: 'grid',
inputFiles: ['p1-video.webm']
}));
});
});

View File

@@ -190,6 +190,44 @@ describe('websocket signaling test in private mode', () => {
]);
});
test('broadcast recording status to room members', async () => {
const session = {
id: 'recording-1',
connectionId: connectionId,
status: 'recording',
layout: 'grid',
format: 'webm',
createdAt: '2026-06-01T00:00:00.000Z',
startedAt: '2026-06-01T00:00:00.000Z',
updatedAt: '2026-06-01T00:00:00.000Z'
} as any;
const expected = {
type: 'recording-started',
connectionId: connectionId,
recordingId: 'recording-1',
status: 'recording',
layout: 'grid',
format: 'webm',
startedAt: '2026-06-01T00:00:00.000Z'
};
expect(wsHandler.broadcastRecordingStarted(session)).toBe(true);
await expect(server).toReceiveMessage(expected);
await expect(server).toReceiveMessage(expected);
expect(wsHandler.broadcastRecordingPeerRequest(session)).toBe(true);
await expect(server).toReceiveMessage({
...expected,
type: 'recording-peer-request',
mediaMode: 'webrtc-sendonly'
});
await expect(server).toReceiveMessage({
...expected,
type: 'recording-peer-request',
mediaMode: 'webrtc-sendonly'
});
});
test('send offer from session1', async () => {
await wsHandler.onOffer(client, { connectionId: connectionId, sdp: testsdp });
const receiveOffer = new Offer(testsdp, Date.now(), true);