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
48 changes: 48 additions & 0 deletions tests/media-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,52 @@ test.describe('device and media lifecycle', () => {
await expect(toggle).toHaveAttribute('data-active', 'true')
await expect(toggle).toBeEnabled()
})

// https://github.com/binbat/woom/issues/47
// A stops sharing while B is still sharing must not lose B's presentation:
// the slot is derived from everyone currently sharing, not last-writer-wins.
test('stopping the second share keeps the first presentation', async ({
browser,
}) => {
const aliceContext = await browser.newContext()
const bobContext = await browser.newContext()
const alice = await aliceContext.newPage()
const bob = await bobContext.newPage()
try {
await mockDisplayMedia(alice)
await mockDisplayMedia(bob)
const roomId = await createMeeting(alice)
await joinRoom(alice, 'Alice')
await joinMeetingFromWelcome(bob, roomId)
await joinRoom(bob, 'Bob')

const alicePresentation = alice
.getByTestId('presentation-player')
.getByTestId('video-remote')
const bobPresentation = bob
.getByTestId('presentation-player')
.getByTestId('video-remote')

const aliceToggle = alice.getByTestId('toggle-screen')
await aliceToggle.click()
await expect(aliceToggle).toHaveAttribute('data-active', 'true')
await expectVideoPlaying(alicePresentation)
await expectVideoPlaying(bobPresentation)

const bobToggle = bob.getByTestId('toggle-screen')
await bobToggle.click()
await expect(bobToggle).toHaveAttribute('data-active', 'true')
await expectVideoPlaying(alicePresentation)
await expectVideoPlaying(bobPresentation)

await bobToggle.click()
await expect(bobToggle).toHaveAttribute('data-active', 'false')
await expect(aliceToggle).toHaveAttribute('data-active', 'true')
await expectVideoPlaying(alicePresentation)
await expectVideoPlaying(bobPresentation)
} finally {
await aliceContext.close()
await bobContext.close()
}
})
})
26 changes: 7 additions & 19 deletions webapp/components/player/whep-player.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { onMounted, onUnmounted, watch } from 'vue'
import { Stream, StreamState } from '../../lib/api'
import { presentationStream } from '../../store/atoms'
import { registerPresentation, unregisterPresentation } from '../../store/atoms'
import SvgVideo from '../svg/video.vue'
import useWhepClient from '../use/whep'
import Detail from './detail.vue'
Expand Down Expand Up @@ -49,14 +49,7 @@ watch(
// module-level WHEPContext would keep receiving media forever
onUnmounted(() => {
whep.value.stop()

if (presentationStream.value.streamId === props.streamId) {
presentationStream.value = {
streamId: '',
name: '',
stream: new MediaStream(),
}
}
unregisterPresentation(props.streamId)
})

watch(
Expand All @@ -70,19 +63,14 @@ watch(
watch(
[() => props.userStatus.screen, () => whep.value.stream],
() => {
const stream = whep.value.stream
if (props.userStatus.screen) {
presentationStream.value = {
registerPresentation(props.streamId, {
streamId: props.streamId,
name: `${props.userStatus.name}_Presentation`,
stream,
}
} else if (presentationStream.value.streamId === props.streamId) {
presentationStream.value = {
streamId: '',
name: '',
stream: new MediaStream(),
}
stream: whep.value.stream,
})
} else {
unregisterPresentation(props.streamId)
}
},
{ immediate: true }
Expand Down
21 changes: 9 additions & 12 deletions webapp/components/player/whip-player.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { watch } from 'vue'
import { presentationStream } from '../../store/atoms'
import { onUnmounted, watch } from 'vue'
import { registerPresentation, unregisterPresentation } from '../../store/atoms'
import useWhipClient from '../use/whip'
import Detail from './detail.vue'
import Player from './player.vue'
Expand All @@ -12,23 +12,20 @@ const whip = useWhipClient(props.streamId)
watch(
[() => whip.value.userStatus.screen, () => whip.value.stream],
() => {
const stream = whip.value.stream
if (whip.value.userStatus.screen) {
presentationStream.value = {
registerPresentation(props.streamId, {
streamId: props.streamId,
name: `${whip.value.userStatus.name}_Presentation`,
stream,
}
} else if (presentationStream.value.streamId === props.streamId) {
presentationStream.value = {
streamId: '',
name: '',
stream: new MediaStream(),
}
stream: whip.value.stream,
})
} else {
unregisterPresentation(props.streamId)
}
},
{ immediate: true }
)

onUnmounted(() => unregisterPresentation(props.streamId))
</script>

<template>
Expand Down
31 changes: 30 additions & 1 deletion webapp/store/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,37 @@ const adminMode = ref(false)
/** non-null while the current meeting is being recorded (server snapshot) */
const recordingAt = ref<number | null>(null)

const presentationStream = shallowRef<UserStream>({
// Streams currently sharing their screen register here; the presentation
// slot is derived from the registry instead of players pushing/clearing a
// single global stream, so one sharer stopping can never clear another
// sharer's presentation (https://github.com/binbat/woom/issues/47).
// Map order is insertion order: the newest registrant takes the stage, and
// earlier ones resume when it unregisters. Re-registering an existing id
// keeps its position, so media refreshes never steal the stage.
const presentationCandidates = shallowRef(new Map<string, UserStream>())

const registerPresentation = (streamId: string, candidate: UserStream) => {
const next = new Map(presentationCandidates.value)
next.set(streamId, candidate)
presentationCandidates.value = next
}

const unregisterPresentation = (streamId: string) => {
if (!presentationCandidates.value.has(streamId)) return
const next = new Map(presentationCandidates.value)
next.delete(streamId)
presentationCandidates.value = next
}

const emptyPresentation: UserStream = {
streamId: '',
stream: new MediaStream(),
name: 'Presentation',
}

const presentationStream = computed<UserStream>(() => {
const candidates = [...presentationCandidates.value.values()]
return candidates[candidates.length - 1] ?? emptyPresentation
})

const enabledPresentation = computed(
Expand Down Expand Up @@ -59,7 +86,9 @@ export {
meetingJoined,
presentationStream,
recordingAt,
registerPresentation,
roomInfo,
roomParticipants,
speakerStatus,
unregisterPresentation,
}
Loading