Skip to content

VIDCS-3699: Implement server-side controls of captions #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"express": "^4.21.2",
"form-data": "^4.0.1",
"opentok": "^2.16.0",
"tsx": "^4.10.5"
"tsx": "^4.10.5",
"opentok-jwt": "^0.1.5"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
Expand Down
38 changes: 38 additions & 0 deletions backend/routes/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,42 @@ sessionRouter.get('/:room/archives', async (req: Request<{ room: string }>, res:
}
});

sessionRouter.post(
'/:room/enableCaptions',
async (req: Request<{ room: string }>, res: Response) => {
try {
const { room: roomName } = req.params;
const sessionId = await sessionService.getSession(roomName);
if (sessionId) {
const captions = await videoService.enableCaptions(sessionId);
res.json({
captions,
status: 200,
});
} else {
res.status(404).json({ message: 'Room not found' });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message might be misleading if the room exists but captions couldn't be enabled for some reason.

(Unless enableCaptions() can never fail....)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps but I'd like to keep it as-is to be consistent with the other routes we have.
though, let me know if I'm wrong -
if this line throws:

const captions = await videoService.enableCaptions(sessionId);

then it will be caught by:

catch (error: unknown) {
      const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
      res.status(500).json({ message: errorMessage });
    }

however, if it so happens that sessionId is undefined, instead of making the enableCaptions request, it will go straight to:

 res.status(404).json({ message: 'Room not found' });

which seems to be covering all of our scenarios, right?

}
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
res.status(500).json({ message: errorMessage });
}
}
);

sessionRouter.post(
'/:room/:captionId/disableCaptions',
async (req: Request<{ room: string; captionId: string }>, res: Response) => {
try {
const { captionId } = req.params;
const responseCaptionId = await videoService.disableCaptions(captionId);
res.json({
captionId: responseCaptionId,
status: 200,
});
} catch (error: unknown) {
res.status(500).send({ message: (error as Error).message ?? error });
}
}
);

export default sessionRouter;
78 changes: 59 additions & 19 deletions backend/tests/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ await jest.unstable_mockModule('../videoService/opentokVideoService.ts', () => {
return {
startArchive: jest.fn<() => Promise<string>>().mockResolvedValue('archiveId'),
stopArchive: jest.fn<() => Promise<string>>().mockRejectedValue('invalid archive'),
enableCaptions: jest.fn<() => Promise<string>>().mockResolvedValue('captionId'),
disableCaptions: jest.fn<() => Promise<string>>().mockResolvedValue('invalid caption'),
generateToken: jest
.fn<() => Promise<{ token: string; apiKey: string }>>()
.mockResolvedValue({
Expand Down Expand Up @@ -70,28 +72,66 @@ describe.each([
});

describe('POST requests', () => {
it('returns a 200 when starting an archive in a room', async () => {
const res = await request(server)
.post(`/session/${roomName}/startArchive`)
.set('Content-Type', 'application/json');
expect(res.statusCode).toEqual(200);
});
describe('archiving', () => {
it('returns a 200 when starting an archive in a room', async () => {
const res = await request(server)
.post(`/session/${roomName}/startArchive`)
.set('Content-Type', 'application/json');
expect(res.statusCode).toEqual(200);
});

it('returns a 404 when starting an archive in a non-existent room', async () => {
const invalidRoomName = 'nonExistingRoomName';
const res = await request(server)
.post(`/session/${invalidRoomName}/startArchive`)
.set('Content-Type', 'application/json');
expect(res.statusCode).toEqual(404);
it('returns a 404 when starting an archive in a non-existent room', async () => {
const invalidRoomName = 'nonExistingRoomName';
const res = await request(server)
.post(`/session/${invalidRoomName}/startArchive`)
.set('Content-Type', 'application/json');
expect(res.statusCode).toEqual(404);
});

it('returns a 500 when stopping an invalid archive in a room', async () => {
const archiveId = 'b8-c9-d10';
const res = await request(server)
.post(`/session/${roomName}/${archiveId}/stopArchive`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json');
expect(res.statusCode).toEqual(500);
});
});

it('returns a 500 when stopping an invalid archive in a room', async () => {
const archiveId = 'b8-c9-d10';
const res = await request(server)
.post(`/session/${roomName}/${archiveId}/stopArchive`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json');
expect(res.statusCode).toEqual(500);
describe('captions', () => {
it('returns a 200 when enabling captions in a room', async () => {
const res = await request(server)
.post(`/session/${roomName}/enableCaptions`)
.set('Content-Type', 'application/json');
expect(res.statusCode).toEqual(200);
});

it('returns a 200 when disabling captions in a room', async () => {
const captionId = 'someCaptionId';
const res = await request(server)
.post(`/session/${roomName}/${captionId}/disableCaptions`)
.set('Content-Type', 'application/json');
expect(res.statusCode).toEqual(200);
});

it('returns a 404 when starting captions in a non-existent room', async () => {
const invalidRoomName = 'randomRoomName';
const res = await request(server)
.post(`/session/${invalidRoomName}/enableCaptions`)
.set('Content-Type', 'application/json');
expect(res.statusCode).toEqual(404);
});

it('returns an invalid caption message when stopping an invalid captions in a room', async () => {
const invalidCaptionId = 'wrongCaptionId';
const res = await request(server)
.post(`/session/${roomName}/${invalidCaptionId}/disableCaptions`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/json');

const responseBody = JSON.parse(res.text);
expect(responseBody.captionId).toEqual('invalid caption');
});
});
});
});
4 changes: 4 additions & 0 deletions backend/types/opentok-jwt-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'opentok-jwt' {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this was needed since I'm using the opentok-jwt package to generate a project token; however, the package is not TypeScript friendly so this was needed. I also cannot it export with a default; therefore, disabling lint since it needs to be imported like this:

import { projectToken } from 'opentok-jwt`

therefore not supporting the default export approach

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's some nice magic here 🪄 💪 !

// eslint-disable-next-line import/prefer-default-export
export function projectToken(apiKey: string, apiSecret: string, expire?: number): string;
}
76 changes: 76 additions & 0 deletions backend/videoService/opentokVideoService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import OpenTok, { Archive, Role } from 'opentok';
import axios from 'axios';
import { projectToken } from 'opentok-jwt';
import { VideoService } from './videoServiceInterface';
import { OpentokConfig } from '../types/config';

export type EnableCaptionResponse = {
captionsId: string;
};

class OpenTokVideoService implements VideoService {
private readonly opentok: OpenTok;

Expand Down Expand Up @@ -87,6 +93,76 @@ class OpenTokVideoService implements VideoService {
});
});
}

// The OpenTok API does not support enabling captions directly through the OpenTok SDK.
// Instead, we need to make a direct HTTP request to the OpenTok API endpoint to enable captions.
// This is not the case for Vonage Video Node SDK, which has a built-in method for enabling captions.
readonly API_URL = 'https://api.opentok.com/v2/project';

async enableCaptions(sessionId: string): Promise<EnableCaptionResponse> {
const expires = Math.floor(new Date().getTime() / 1000) + 24 * 60 * 60;
// Note that the project token is different from the session token.
// The project token is used to authenticate the request to the OpenTok API.
const projectJWT = projectToken(this.config.apiKey, this.config.apiSecret, expires);
const captionURL = `${this.API_URL}/${this.config.apiKey}/captions`;

const { token } = this.generateToken(sessionId);
const captionOptions = {
// The following language codes are supported: en-US, en-AU, en-GB, fr-FR, fr-CA, de-DE, hi-IN, it-IT, pt-BR, ja-JP, ko-KR, zh-CN, zh-TW
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe link to where this is documented instead?

languageCode: 'en-US',
// The maximum duration of the captions in seconds. The default is 14,400 seconds (4 hours).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same nit above applies here too.

maxDuration: 1800,
// Enabling partial captions allows for more frequent updates to the captions.
// This is useful for real-time applications where the captions need to be updated frequently.
// However, it may also increase the number of inaccuracies in the captions.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent comment! 💪

partialCaptions: true,
};

const captionAxiosPostBody = {
sessionId,
token,
...captionOptions,
};

try {
const {
data: { captionsId },
} = await axios.post(captionURL, captionAxiosPostBody, {
headers: {
'X-OPENTOK-AUTH': projectJWT,
'Content-Type': 'application/json',
},
});
return { captionsId };
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
throw new Error(`Failed to enable captions: ${errorMessage}`);
}
}

async disableCaptions(captionId: string): Promise<string> {
const expires = Math.floor(new Date().getTime() / 1000) + 24 * 60 * 60;
// Note that the project token is different from the session token.
// The project token is used to authenticate the request to the OpenTok API.
const projectJWT = projectToken(this.config.apiKey, this.config.apiSecret, expires);
const captionURL = `${this.API_URL}/${this.config.apiKey}/captions/${captionId}/stop`;
try {
await axios.post(
captionURL,
{},
{
headers: {
'X-OPENTOK-AUTH': projectJWT,
'Content-Type': 'application/json',
},
}
);
return 'Captions stopped successfully';
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
throw new Error(`Failed to disable captions: ${errorMessage}`);
}
}
}

export default OpenTokVideoService;
118 changes: 118 additions & 0 deletions backend/videoService/tests/opentokVideoService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { describe, expect, it, jest } from '@jest/globals';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: if you notice some differences between this test file and vonageVideoService.test.ts, no you don't 😆
just kidding - there are some slight difference since the opentok package uses callback and exports itself without a default but with module.exports = OpenTok.

also you'll notice that in this file I'm also mocking axios - that is because we have to use axios in opentokVideoService but not vonageVideoService since the opentok library does not support enableCaptions and disableCaptions methods so we have to do direct REST API requests. I've left some comments about it in the codebase, too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, to be fair, for tests we prefer WET over DRY ;-)


const mockSessionId = 'mockSessionId';
const mockToken = 'mockToken';
const mockApiKey = 'mockApplicationId';
const mockArchiveId = 'mockArchiveId';
const mockRoomName = 'awesomeRoomName';
const mockCaptionId = 'mockCaptionId';
const mockApiSecret = 'mockApiSecret';

await jest.unstable_mockModule('opentok', () => ({
default: jest.fn().mockImplementation(() => ({
createSession: jest.fn(
(_options: unknown, callback: (err: unknown, session: { sessionId: string }) => void) => {
callback(null, { sessionId: mockSessionId });
}
),
generateToken: jest.fn<() => { token: string; apiKey: string }>().mockReturnValue({
token: mockToken,
apiKey: mockApiKey,
}),
startArchive: jest.fn(
(
_sessionId: string,
_options: unknown,
callback: (
err: unknown,
session: {
archive: {
id: string;
};
}
) => void
) => {
callback(null, {
archive: {
id: mockArchiveId,
},
});
}
),
stopArchive: jest.fn(
(_archiveId: string, callback: (err: unknown, archive: { archiveId: string }) => void) => {
callback(null, { archiveId: mockArchiveId });
}
),
listArchives: jest.fn(
(_options: unknown, callback: (err: unknown, archives: [{ archiveId: string }]) => void) => {
callback(null, [{ archiveId: mockArchiveId }]);
}
),
})),
mediaMode: 'routed',
}));

await jest.unstable_mockModule('axios', () => ({
default: {
post: jest.fn<() => Promise<{ data: { captionsId: string } }>>().mockResolvedValue({
data: { captionsId: mockCaptionId },
}),
},
}));

const { default: OpenTokVideoService } = await import('../opentokVideoService');

describe('OpentokVideoService', () => {
let opentokVideoService: typeof OpenTokVideoService.prototype;

beforeEach(() => {
opentokVideoService = new OpenTokVideoService({
apiKey: mockApiKey,
apiSecret: mockApiSecret,
provider: 'opentok',
});
});

it('creates a session', async () => {
const session = await opentokVideoService.createSession();
expect(session).toBe(mockSessionId);
});

it('generates a token', () => {
const result = opentokVideoService.generateToken(mockSessionId);
expect(result.token).toEqual({
apiKey: mockApiKey,
token: mockToken,
});
});

it('starts an archive', async () => {
const response = await opentokVideoService.startArchive(mockRoomName, mockSessionId);
expect(response).toMatchObject({
archive: {
id: mockArchiveId,
},
});
});

it('stops an archive', async () => {
const archiveResponse = await opentokVideoService.stopArchive(mockArchiveId);
expect(archiveResponse).toBe(mockArchiveId);
});

it('generates a list of archives', async () => {
const archiveResponse = await opentokVideoService.listArchives(mockSessionId);
expect(archiveResponse).toEqual([{ archiveId: mockArchiveId }]);
});

it('enables captions', async () => {
const captionResponse = await opentokVideoService.enableCaptions(mockSessionId);
expect(captionResponse.captionsId).toBe(mockCaptionId);
});

it('disables captions', async () => {
const captionResponse = await opentokVideoService.disableCaptions(mockCaptionId);
expect(captionResponse).toBe('Captions stopped successfully');
});
});
Loading
Loading