Skip to content

Commit

Permalink
backend: Improved the recording streaming
Browse files Browse the repository at this point in the history
Send  chunks instead of entire file allowing release the file when stop playing
  • Loading branch information
CSantosM committed Sep 25, 2024
1 parent 5d79a2c commit b772cb5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 2 additions & 3 deletions backend/src/controllers/recording.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ export const streamRecording = async (req: Request, res: Response) => {
logger.info(`Streaming recording ${recordingId}`);
const { fileSize, fileStream, start, end } = await recordingService.getRecordingAsStream(recordingId, range);

res.setHeader('Accept-Ranges', 'bytes');

if (range && fileSize && start !== undefined && end !== undefined) {
const contentLength = end - start + 1;

res.writeHead(206, {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': contentLength,
'Content-Type': 'video/webm'
'Content-Type': 'video/mp4'
});

fileStream.on('error', (streamError) => {
Expand All @@ -104,6 +102,7 @@ export const streamRecording = async (req: Request, res: Response) => {

fileStream.pipe(res).on('finish', () => res.end());
} else {
res.setHeader('Accept-Ranges', 'bytes');
res.setHeader('Content-Type', 'video/mp4');

if (fileSize) res.setHeader('Content-Length', fileSize);
Expand Down
4 changes: 3 additions & 1 deletion backend/src/services/recording.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export class RecordingService {
recordingId: string,
range?: string
): Promise<{ fileSize: number | undefined; fileStream: Readable; start?: number; end?: number }> {
const RECORDING_FILE_PORTION_SIZE = 5 * 1024 * 1024; // 5MB
const egressInfo = await this.getRecording(recordingId);
const recordingPath = RecordingHelper.extractFileNameFromUrl(egressInfo.location);

Expand All @@ -218,7 +219,8 @@ export class RecordingService {
// Parse the range header
const parts = range.replace(/bytes=/, '').split('-');
const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const endRange = parts[1] ? parseInt(parts[1], 10) : start + RECORDING_FILE_PORTION_SIZE;
const end = Math.min(endRange, fileSize - 1);
const fileStream = await this.s3Service.getObjectAsStream(recordingPath, CALL_S3_BUCKET, {
start,
end
Expand Down

0 comments on commit b772cb5

Please sign in to comment.