Skip to content
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

multistream: atomic add and remove targets from a given stream #1988

Merged
merged 4 commits into from
Dec 7, 2023
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
22 changes: 21 additions & 1 deletion packages/api/src/controllers/stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ describe("controllers/stream", () => {
});
});

it("should create a stream, delete it, and error when attempting additional detele or replace", async () => {
it("should create a stream, delete it, and error when attempting additional delete or replace", async () => {
const res = await client.post("/stream", { ...postMockStream });
expect(res.status).toBe(201);
const stream = await res.json();
Expand Down Expand Up @@ -506,6 +506,26 @@ describe("controllers/stream", () => {
}
});

it("should create a stream and add a multistream target for it", async () => {
const res = await client.post("/stream", { ...postMockStream });
expect(res.status).toBe(201);
const stream = await res.json();
expect(stream.id).toBeDefined();

const document = await server.store.get(`stream/${stream.id}`);
expect(server.db.stream.addDefaultFields(document)).toEqual(stream);

const res2 = await client.post(
`/stream/${stream.id}/create-multistream-target`,
{
profile: "source",
videoOnly: false,
spec: { name: "target-name", url: "rtmp://test/test" },
}
);
expect(res2.status).toBe(204);
});

describe("set active and heartbeat", () => {
const callSetActive = async (
streamId: string,
Expand Down
80 changes: 80 additions & 0 deletions packages/api/src/controllers/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,86 @@
}
);

app.post(
"/:id/create-multistream-target",
authorizer({}),

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
validatePost("target-add-payload"),
async (req, res) => {
const payload = req.body;

const stream = await db.stream.get(req.params.id);

if (!stream || stream.deleted) {
res.status(404);
return res.json({ errors: ["stream not found"] });
}

if (stream.userId !== req.user.id) {
res.status(404);
return res.json({ errors: ["stream not found"] });
}

let multistream: DBStream["multistream"] = {
targets: [...(stream.multistream?.targets ?? []), payload],
};

multistream = await validateMultistreamOpts(
req.user.id,
stream.profiles,
multistream
);

let patch: StreamPatchPayload & Partial<DBStream> = {
multistream,
};

await db.stream.update(stream.id, patch);

await triggerCatalystStreamUpdated(req, stream.playbackId);

res.status(204);
res.end();
}
);

app.delete("/:id/multistream/:targetId", authorizer({}), async (req, res) => {

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
gioelecerati marked this conversation as resolved.
Show resolved Hide resolved
const { id, targetId } = req.params;

const stream = await db.stream.get(id);

if (!stream || stream.deleted) {
res.status(404);
return res.json({ errors: ["stream not found"] });
}

if (stream.userId !== req.user.id) {
res.status(404);
return res.json({ errors: ["stream not found"] });
}

let multistream: DBStream["multistream"] = stream.multistream ?? {
targets: [],
};

multistream.targets = multistream.targets.filter((t) => t.id !== targetId);
gioelecerati marked this conversation as resolved.
Show resolved Hide resolved
multistream = await validateMultistreamOpts(
req.user.id,
stream.profiles,
multistream
);

let patch: StreamPatchPayload & Partial<DBStream> = {
multistream,
};

await db.stream.update(stream.id, patch);

await triggerCatalystStreamUpdated(req, stream.playbackId);

res.status(204);
res.end();
});

app.patch(
"/:id",
authorizer({}),
Expand Down
131 changes: 93 additions & 38 deletions packages/api/src/schema/api-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,44 @@ components:
sessionId:
type: string
description: Session ID of the stream to clip
target:
gioelecerati marked this conversation as resolved.
Show resolved Hide resolved
type: object
required:
- profile
additionalProperties: false
properties:
profile:
type: string
description: |
Name of transcoding profile that should be sent. Use
"source" for pushing source stream data
minLength: 1
maxLength: 500
example: 720p
videoOnly:
type: boolean
description: |
If true, the stream audio will be muted and only silent
video will be pushed to the target.
default: false
id:
type: string
description: ID of multistream target object where to push this stream
spec:
type: object
writeOnly: true
description: |
Inline multistream target object. Will automatically
create the target resource to be used by the created
stream.
required:
- url
additionalProperties: false
properties:
name:
type: string
url:
$ref: "#/components/schemas/multistream-target/properties/url"
stream:
type: object
required:
Expand Down Expand Up @@ -394,44 +432,7 @@ components:
References to targets where this stream will be simultaneously
streamed to
items:
type: object
required:
- profile
additionalProperties: false
properties:
profile:
type: string
description: |
Name of transcoding profile that should be sent. Use
"source" for pushing source stream data
minLength: 1
maxLength: 500
example: 720p
videoOnly:
type: boolean
description: |
If true, the stream audio will be muted and only silent
video will be pushed to the target.
default: false
id:
type: string
description:
ID of multistream target object where to push this stream
spec:
type: object
writeOnly: true
description: |
Inline multistream target object. Will automatically
create the target resource to be used by the created
stream.
required:
- url
additionalProperties: false
properties:
name:
type: string
url:
$ref: "#/components/schemas/multistream-target/properties/url"
$ref: "#/components/schemas/target"
suspended:
type: boolean
description: If currently suspended
Expand Down Expand Up @@ -504,6 +505,10 @@ components:
$ref: "#/components/schemas/playback-policy"
profiles:
$ref: "#/components/schemas/stream/properties/profiles"
target-add-payload:
type: object
additionalProperties: false
$ref: "#/components/schemas/target"
stream-health-payload:
type: object
description: |
Expand Down Expand Up @@ -3010,6 +3015,56 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/error"
"/stream/{id}/create-multistream-target":
post:
summary: Add a multistream target
parameters:
- in: path
name: id
schema:
type: string
description: ID of the parent stream
required: true
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/target-add-payload"
responses:
"204":
description: Success (No content)
default:
description: Error
content:
application/json:
schema:
$ref: "#/components/schemas/error"
"/stream/{id}/multistream/{targetId}":
delete:
summary: Remove a multistream target
parameters:
- in: path
name: id
schema:
type: string
description: ID of the parent stream
required: true
- in: path
name: targetId
schema:
type: string
description: ID of the multistream target
required: true
responses:
"204":
description: Success (No content)
default:
description: Error
content:
application/json:
schema:
$ref: "#/components/schemas/error"
"/session/{id}/clips":
get:
summary: Retrieve clips of a session
Expand Down
Loading