-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide WHIP endpoint for external audio devices
- Loading branch information
Showing
10 changed files
with
394 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { FastifyPluginCallback } from 'fastify'; | ||
import bearerAuthPlugin from '@fastify/bearer-auth'; | ||
import { ErrorResponse } from './models'; | ||
import { Type } from '@sinclair/typebox'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { CoreFunctions } from './api_productions_core_functions'; | ||
import { SmbProtocol } from './smb'; | ||
|
||
export interface ApiWhipOptions { | ||
whipApiKey: string; | ||
smbServerBaseUrl: string; | ||
endpointIdleTimeout: string; | ||
smbServerApiKey?: string; | ||
coreFunctions: CoreFunctions; | ||
} | ||
|
||
const apiWhip: FastifyPluginCallback<ApiWhipOptions> = ( | ||
fastify, | ||
opts, | ||
next | ||
) => { | ||
fastify.addContentTypeParser( | ||
'application/sdp', | ||
{ parseAs: 'string' }, | ||
(req, body, done) => { | ||
done(null, body); | ||
} | ||
); | ||
|
||
fastify.addContentTypeParser( | ||
'application/trickle-ice-sdpfrag', | ||
{ parseAs: 'string' }, | ||
(req, body, done) => { | ||
done(null, body); | ||
} | ||
); | ||
|
||
const smbServerUrl = new URL( | ||
'/conferences/', | ||
opts.smbServerBaseUrl | ||
).toString(); | ||
const smb = new SmbProtocol(); | ||
const smbServerApiKey = opts.smbServerApiKey || ''; | ||
const coreFunctions = opts.coreFunctions; | ||
|
||
fastify.post<{ | ||
Params: { productionId: string; lineId: string; username: string }; | ||
Body: string; | ||
}>( | ||
'/production/:productionId/line/:lineId/username/:username', | ||
{ | ||
schema: { | ||
description: | ||
'Initiate ingest of an external source to a production and line', | ||
body: Type.String(), | ||
params: { | ||
productionId: Type.String(), | ||
lineId: Type.String(), | ||
username: Type.String() | ||
}, | ||
response: { | ||
201: Type.String(), | ||
500: ErrorResponse | ||
} | ||
} | ||
}, | ||
async (request, reply) => { | ||
try { | ||
const { productionId, lineId, username } = request.params; | ||
const sdpOffer = request.body; | ||
const sessionId: string = uuidv4(); | ||
|
||
const smbConferenceId = await coreFunctions.createConferenceForLine( | ||
smb, | ||
smbServerUrl, | ||
smbServerApiKey, | ||
productionId, | ||
lineId | ||
); | ||
const endpointId: string = uuidv4(); | ||
const endpoint = await coreFunctions.createEndpoint( | ||
smb, | ||
smbServerUrl, | ||
smbServerApiKey, | ||
smbConferenceId, | ||
endpointId, | ||
true, | ||
true, | ||
parseInt(opts.endpointIdleTimeout, 10) | ||
); | ||
if (!endpoint.audio) { | ||
throw new Error('Missing audio when creating sdp offer for endpoint'); | ||
} | ||
if (!endpoint.audio.ssrcs) { | ||
throw new Error('Missing ssrcs when creating sdp offer for endpoint'); | ||
} | ||
|
||
const sdpAnswer = await coreFunctions.createConnectionFromOffer( | ||
productionId, | ||
lineId, | ||
endpoint, | ||
username, | ||
endpointId, | ||
sessionId, | ||
sdpOffer, | ||
smb, | ||
smbServerUrl, | ||
smbServerApiKey, | ||
smbConferenceId | ||
); | ||
const locationUrl = `/whip/session/${sessionId}`; | ||
reply.headers({ | ||
'Content-Type': 'application/sdp', | ||
Location: locationUrl | ||
}); | ||
reply.code(201).send(sdpAnswer); | ||
} catch (err) { | ||
console.error(err); | ||
reply.code(500).send({ message: 'Unhandled error: ' + err }); | ||
} | ||
} | ||
); | ||
next(); | ||
}; | ||
|
||
export default apiWhip; |
Oops, something went wrong.