Skip to content
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
41 changes: 41 additions & 0 deletions tests/media-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ async function mockDisplayMedia(page: Page, delay = 0) {
}, delay)
}

async function holdFirstCamera(page: Page) {
await page.addInitScript(() => {
const getUserMedia = navigator.mediaDevices.getUserMedia.bind(
navigator.mediaDevices
)
let cameraRequestCount = 0
Object.assign(window, { __cameraRequestCount: 0 })
navigator.mediaDevices.getUserMedia = async constraints => {
const video = constraints?.video
if (typeof video === 'object' && video.deviceId) {
cameraRequestCount++
Object.assign(window, { __cameraRequestCount: cameraRequestCount })
if (cameraRequestCount === 1) {
await new Promise<void>(resolve => {
Object.assign(window, { __releaseCamera: resolve })
})
}
}
return getUserMedia(constraints)
}
})
}

test.describe('device and media lifecycle', () => {
test('stops every temporary device probe track', async ({ page }) => {
await page.addInitScript(() => {
Expand Down Expand Up @@ -146,6 +169,7 @@ test.describe('device and media lifecycle', () => {
await joinMeetingFromWelcome(bob, roomId)
await joinRoom(bob, 'Bob')

await expectVideoPlaying(alice.getByTestId('video-self'))
const toggle = alice.getByTestId('toggle-screen')
await toggle.click()
await expect(toggle).toHaveAttribute('data-active', 'true')
Expand All @@ -156,6 +180,7 @@ test.describe('device and media lifecycle', () => {
})
expect(response.ok()).toBe(true)
await expect(toggle).toHaveAttribute('data-active', 'false')
await expectVideoPlaying(alice.getByTestId('video-self'))
} finally {
await aliceContext.close()
await bobContext.close()
Expand All @@ -171,8 +196,18 @@ test.describe('device and media lifecycle', () => {
const bob = await bobContext.newPage()
try {
await mockDisplayMedia(alice, 1000)
await holdFirstCamera(alice)
const roomId = await createMeeting(alice)
await joinRoom(alice, 'Alice')
await expect
.poll(() =>
alice.evaluate(
() =>
(window as unknown as Window & { __cameraRequestCount: number })
.__cameraRequestCount
)
)
.toBe(1)
await joinMeetingFromWelcome(bob, roomId)
await joinRoom(bob, 'Bob')

Expand All @@ -198,6 +233,12 @@ test.describe('device and media lifecycle', () => {
data: { presenter: bobId },
})
expect(response.ok()).toBe(true)
await alice.evaluate(() => {
const testWindow = window as unknown as Window & {
__releaseCamera: () => void
}
testWindow.__releaseCamera()
})
await expect
.poll(() =>
alice.evaluate(() => {
Expand Down
24 changes: 18 additions & 6 deletions webapp/components/use/whip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class WHIPContext extends Context {
private audioGeneration = 0
private videoGeneration = 0
private pendingScreenGeneration: number | undefined
private normalVideoRequest: {
current: string
constraints?: MediaTrackConstraints
} = { current: deviceNone.deviceId }

toggleEnableAudio = async () =>
this.setCurrentDeviceAudio(
Expand All @@ -88,14 +92,18 @@ class WHIPContext extends Context {
: deviceSegmenter.deviceId
)
stopScreenSharing = async () =>
this.updateVideo(deviceNone.deviceId, undefined, true)
this.updateVideo(
this.normalVideoRequest.current,
this.normalVideoRequest.constraints,
true
)
revokeScreenSharing = async () => {
const pending = this.pendingScreenGeneration
if (pending !== undefined) {
this.pendingScreenGeneration = undefined
if (pending === this.videoGeneration) this.videoGeneration++
if (
this.pendingScreenGeneration === this.videoGeneration ||
this.userStatus.screen
) {
await this.stopScreenSharing()
}
if (this.userStatus.screen) await this.stopScreenSharing()
}

constructor(id: string) {
Expand Down Expand Up @@ -269,6 +277,9 @@ class WHIPContext extends Context {
force = false
) {
const generation = ++this.videoGeneration
if (current !== deviceScreen.deviceId) {
this.normalVideoRequest = { current, constraints }
}
this.pendingScreenGeneration =
current === deviceScreen.deviceId ? generation : undefined
if (
Expand Down Expand Up @@ -297,6 +308,7 @@ class WHIPContext extends Context {
if (this.pendingScreenGeneration === generation)
this.pendingScreenGeneration = undefined
segmentation?.destroyStream()
if (generation !== this.videoGeneration) return
throw e
}

Expand Down
Loading