Skip to content

Commit

Permalink
Add keepalive server route to Webrtc Nuxt demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitritz committed Jul 23, 2024
1 parent a482c44 commit 4fa828a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 20 deletions.
7 changes: 7 additions & 0 deletions apps/ingest-demo-nuxt/composables/use-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export default async function useStream() {
)
}
cookie.value = data.value.body
} else {
await useFetch('/api/keepalive', {
method: 'POST',
body: {
stream: cookie.value.id,
},
})
}
return cookie.value
}
8 changes: 4 additions & 4 deletions apps/ingest-demo-nuxt/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/ingest-demo-nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@gcorevideo/rtckit": "^0.55.0",
"@gcorevideo/rtckit-node": "^0.3.0",
"@gcorevideo/rtckit-node": "^0.4.0",
"@heroicons/vue": "^2.1.5",
"nuxt": "^3.12.4",
"vue": "latest"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import { defineEventHandler } from "h3"
import { ApiKey, GcoreApi } from '@gcorevideo/rtckit-node'

import gcore from '../utils/gcore-api'

// Generate a live stream
export default defineEventHandler(
async (event) => {
const config = useRuntimeConfig()
if (event.method === 'POST') {
const { name } = await readBody(event)
const api = new GcoreApi(
new ApiKey(config.apiKey),
)
api.webrtc.setCustomOptions({
const { name } = await readBody(event)
const webrtc = gcore().webrtc
webrtc.setCustomOptions({
qualitySetId: Number(config.qualitySetId) || null,
})
const { whipEndpoint, whepEndpoint, playerUrl } =
await api.webrtc.createStream(
const { id, whipEndpoint, whepEndpoint, playerUrl } =
await webrtc.createStream(
name,
)
return {
status: 201,
body: {
id,
playerUrl,
whepEndpoint,
whipEndpoint,
},
}
}
},
)
17 changes: 17 additions & 0 deletions apps/ingest-demo-nuxt/server/api/keepalive.post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineEventHandler } from "h3";

import gcore from "../utils/gcore-api";

export default defineEventHandler(async event => {
const body = await readBody(event);
const id = body?.id;
if (!id) {
throw createError({
status: 400,
});
}
await gcore().webrtc.toggleStream(id, true);
return {
status: 204,
};
})
8 changes: 8 additions & 0 deletions apps/ingest-demo-nuxt/server/utils/gcore-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiKey, GcoreApi } from '@gcorevideo/rtckit-node'

export default function gcoreApi() {
const config = useRuntimeConfig()
return new GcoreApi(
new ApiKey(config.apiKey),
)
}
1 change: 1 addition & 0 deletions apps/ingest-demo-nuxt/types/stream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type StreamInfo = {
id: number
whipEndpoint: string
whepEndpoint: string
playerUrl: string
Expand Down
4 changes: 2 additions & 2 deletions packages/rtckit-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/rtckit-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gcorevideo/rtckit-node",
"version": "0.3.0",
"version": "0.4.0",
"description": "Node.js SDK for the Gcore WebRTC",
"main": "dist/index.js",
"type": "module",
Expand Down
15 changes: 13 additions & 2 deletions packages/rtckit-node/src/WebrtcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { LiveStreamDto } from "./types.js";
import { logger } from "./logger.js";
import { RTSP_PULL_URL, WHEP_ENDPOINT_URL, WHIP_ENDPOINT_URL } from "./settings.js";

type StreamId = number;

export type WebrtcStream = {
active: boolean;
id: number;
Expand Down Expand Up @@ -40,7 +42,6 @@ export class WebrtcApi {
},
},
)) as LiveStreamDto;
logger.debug("Created a stream: %o", r);
await this.service.request(
`streams/${r.id}`,
{
Expand All @@ -67,7 +68,17 @@ export class WebrtcApi {
);
}

// TODO switch off webrtc
async toggleStream(id: StreamId, active: boolean) {
await this.service.request(
`streams/${id}`,
{
method: "patch",
data: {
active,
},
},
)
}

setCustomOptions(options: CustomOptions) {
this.customOptions = options;
Expand Down

0 comments on commit 4fa828a

Please sign in to comment.