A platform for community discussion. Free, open, simple.
FEATURE: Switch Live rooms to the presentation layout when someone shares their screen (#45518)
Keegan George8838f486ba01
diff --git a/plugins/live/assets/javascripts/discourse/components/live/room-page.gjs b/plugins/live/assets/javascripts/discourse/components/live/room-page.gjsindex 124a8a60e5f..8a836edc026 100644--- a/plugins/live/assets/javascripts/discourse/components/live/room-page.gjs+++ b/plugins/live/assets/javascripts/discourse/components/live/room-page.gjs@@ -6,10 +6,11 @@ import { action } from "@ember/object";import { getOwner } from "@ember/owner";import didInsert from "@ember/render-modifiers/modifiers/did-insert";import didUpdate from "@ember/render-modifiers/modifiers/did-update";-import { cancel, next } from "@ember/runloop";+import { cancel, next, schedule } from "@ember/runloop";import { service } from "@ember/service";import { trustHTML } from "@ember/template";import DMenu from "discourse/float-kit/components/d-menu";+import { isTesting } from "discourse/lib/environment";import { homepageNavigationDestination } from "discourse/lib/homepage-router-overrides";import discourseLater from "discourse/lib/later";import DButton from "discourse/ui-kit/d-button";@@ -20,6 +21,7 @@ import { i18n } from "discourse-i18n";import {toggleFullscreen,trackFullscreen,+ trackFullscreenWithin,} from "../../lib/live/fullscreen.js";import { activeRingingEntries } from "../../lib/live/ringing.js";import { speakQueue } from "../../lib/live/speak-queue.js";@@ -48,8 +50,35 @@ const SUBMENU = "live-call-submenu";const ROOM_PATH_PREFIX = "/live/r/";const MOBILE_VIDEO_TILE_BUDGET = 4;+const LAYOUT_AUTO = "auto";const LAYOUT_PRESENTATION = "presentation";const LAYOUT_TILED = "tiled";+const LAYOUT_VIDEO_WAIT_MS = 300;++/**+ * Resolves once every video under `element` has a frame, or after `timeout`+ * ms, so a layout animation never flies empty tiles around.+ */+function videoFramesReady(element, timeout) {+ const pending = [...element.querySelectorAll("video")].filter(+ (video) => video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA+ );+ if (!pending.length) {+ return Promise.resolve();+ }++ return Promise.race([+ Promise.all(+ pending.map(+ (video) =>+ new Promise((resolve) =>+ video.addEventListener("loadeddata", resolve, { once: true })+ )+ )+ ),+ new Promise((resolve) => setTimeout(resolve, timeout)),+ ]);+}export default class LiveRoomPage extends Component {@service capabilities;@@ -65,12 +94,20 @@ export default class LiveRoomPage extends Component {@tracked gridHeight = 0;@tracked gridGap = 0;@tracked gridFullscreen = false;+ @tracked fullscreenWithin = false;// Stage rooms mirror a workshop: chat panel open, presenter featured.@tracked chatOpen = !!this.args.openChat || this.#isStageRoom;@tracked chatClosing = false;- @tracked layoutMode = this.#isStageRoom ? LAYOUT_PRESENTATION : LAYOUT_TILED;+ @tracked layoutMode = LAYOUT_AUTO;@tracked spotlightParticipantId = null;+ /**+ * What the stage renders. It trails the resolved values so followLayout can+ * animate from the old arrangement; the template must read these.+ */+ @tracked shownLayout = this.resolvedLayout;+ @tracked shownFeaturedId = this.featuredParticipantId;+// Ring windows expire by wall clock, which nothing tracked observes — a// coarse ticker re-evaluates them so "Calling…" tiles disappear on time.@tracked ringingClock = Date.now();@@ -78,6 +115,7 @@ export default class LiveRoomPage extends Component {gridElement = null;trackGridSize = trackGridSize;trackFullscreen = trackFullscreen;+ trackFullscreenWithin = trackFullscreenWithin;#chatCloseFallback = null;#ringingTicker = null;@@ -144,14 +182,21 @@ export default class LiveRoomPage extends Component {: MOBILE_VIDEO_TILE_BUDGET;const videoParticipantIds = new Set();+ // Whoever can take the stage gets live video first, so a featured feed is+ // never demoted to an avatar by cameras earlier in the roster.const spotlightParticipant = this.participants.find((participant) => participant.id === this.spotlightParticipantId);- if (spotlightParticipant && this.#isPublishing(spotlightParticipant)) {- videoParticipantIds.add(spotlightParticipant.id);- }-- for (const participant of this.participants) {+ const prioritized = [+ spotlightParticipant,+ ...this.remoteScreenSharers,+ ...this.participants,+ ];++ for (const participant of prioritized) {+ if (!participant) {+ continue;+ }if (videoParticipantIds.size >= budget) {break;}@@ -175,9 +220,23 @@ export default class LiveRoomPage extends Component {return activeRingingEntries(this.room, this.ringingClock);}- get presentationTile() {+ /**+ * Excludes your own share: featuring it to yourself mirrors the screen back+ * into itself.+ */+ get remoteScreenSharers() {+ return this.participants.filter(+ (participant) =>+ participant.is_screen_sharing && participant.id !== this.currentUser?.id+ );+ }++ get featuredTile() {return (this.tiles.find((tile) => tile.spotlighted) ??+ this.tiles.find(+ (tile) => tile.participant.is_screen_sharing && !tile.isSelf+ ) ??this.tiles.find((tile) => tile.participant.is_screen_sharing) ??this.tiles.find((tile) => tile.showVideo && !tile.isSelf) ??this.tiles.find((tile) => tile.showVideo) ??@@ -188,6 +247,24 @@ export default class LiveRoomPage extends Component {);}+ get featuredParticipantId() {+ if (this.resolvedLayout !== LAYOUT_PRESENTATION) {+ return null;+ }+ return this.featuredTile?.participant.id ?? null;+ }++ /**+ * Stays on the outgoing participant while a change waits to animate, so the+ * stage is not swapped underneath the old view.+ */+ get presentationTile() {+ return (+ this.tiles.find((tile) => tile.participant.id === this.shownFeaturedId) ??+ this.featuredTile+ );+ }+get presentationRailTiles() {const featuredId = this.presentationTile?.participant.id;return this.tiles.filter((tile) => tile.participant.id !== featuredId);@@ -211,15 +288,46 @@ export default class LiveRoomPage extends Component {return this.tiles.filter((tile) => tile.participant.id !== pipId);}+ get autoLayout() {+ return this.layoutMode === LAYOUT_AUTO;+ }++ /**+ * The layout the room should show. Auto features a stage room's presenter, a+ * pinned participant, or another participant's screen share.+ */+ get resolvedLayout() {+ if (!this.autoLayout) {+ return this.layoutMode;+ }++ if (this.#isStageRoom || this.spotlightParticipantId) {+ return LAYOUT_PRESENTATION;+ }++ return this.remoteScreenSharers.length ? LAYOUT_PRESENTATION : LAYOUT_TILED;+ }+get tiledLayout() {- return this.layoutMode === LAYOUT_TILED;+ return this.shownLayout === LAYOUT_TILED;}get presentationLayout() {- return this.layoutMode === LAYOUT_PRESENTATION;+ return this.shownLayout === LAYOUT_PRESENTATION;+ }++ get #animatesLayout() {+ return (+ typeof document.startViewTransition === "function" &&+ !isTesting() &&+ !window.matchMedia("(prefers-reduced-motion: reduce)").matches+ );}get layoutIcon() {+ if (this.autoLayout) {+ return "wand-magic-sparkles";+ }return this.presentationLayout ? "person-chalkboard" : "table-cells";}@@ -302,7 +410,12 @@ export default class LiveRoomPage extends Component {toggleSpotlight(participantId) {this.spotlightParticipantId =this.spotlightParticipantId === participantId ? null : participantId;- this.layoutMode = LAYOUT_PRESENTATION;++ // Auto already features a pinned participant and falls back once they are+ // unpinned; only a fixed tiled layout needs switching.+ if (this.layoutMode === LAYOUT_TILED) {+ this.layoutMode = LAYOUT_PRESENTATION;+ }}@action@@ -311,6 +424,39 @@ export default class LiveRoomPage extends Component {this.layoutMode = layoutMode;}+ /**+ * Moves the stage to the resolved layout, gliding each tile to its new spot+ * where view transitions are supported.+ */+ @action+ followLayout(element) {+ // Re-rendering the stage would unmount whatever is fullscreen and drop the+ // viewer out of it, so changes wait until fullscreen ends.+ if (+ this.fullscreenWithin ||+ (this.shownLayout === this.resolvedLayout &&+ this.shownFeaturedId === this.featuredParticipantId)+ ) {+ return;+ }++ if (!this.#animatesLayout) {+ this.#showResolvedLayout();+ return;+ }++ document.startViewTransition(async () => {+ if (this.isDestroying) {+ return;+ }+ // Read again: the layout may have moved on before the old view was+ // captured.+ this.#showResolvedLayout();+ await new Promise((resolve) => schedule("afterRender", resolve));+ await videoFramesReady(element, LAYOUT_VIDEO_WAIT_MS);+ });+ }+@actionreconcileSpotlight() {if (@@ -367,6 +513,11 @@ export default class LiveRoomPage extends Component {});}+ @action+ setFullscreenWithin(isFullscreen) {+ this.fullscreenWithin = isFullscreen;+ }+@actionsetGridFullscreen(isFullscreen) {this.gridFullscreen = isFullscreen;@@ -501,23 +652,34 @@ export default class LiveRoomPage extends Component {event,ROOM_MENU,[+ {+ id: LAYOUT_AUTO,+ label: i18n("live.video.layout_auto"),+ icon: "wand-magic-sparkles",+ selected: this.autoLayout,+ },{id: LAYOUT_PRESENTATION,label: i18n("live.video.layout_presentation"),icon: "person-chalkboard",- selected: this.presentationLayout,+ selected: this.layoutMode === LAYOUT_PRESENTATION,},{id: LAYOUT_TILED,label: i18n("live.video.layout_tiled"),icon: "table-cells",- selected: this.tiledLayout,+ selected: this.layoutMode === LAYOUT_TILED,},],this.setLayoutMode);}+ #showResolvedLayout() {+ this.shownLayout = this.resolvedLayout;+ this.shownFeaturedId = this.featuredParticipantId;+ }+#isPublishing(participant) {if (participant.id === this.currentUser?.id) {return !!this.liveWebrtc.localVideoKind;@@ -559,6 +721,13 @@ export default class LiveRoomPage extends Component {{{didInsert this.watchRoom}}{{didInsert this.autoJoinIfRequested}}{{didUpdate this.reconcileSpotlight this.participants}}+ {{didUpdate+ this.followLayout+ this.resolvedLayout+ this.featuredParticipantId+ this.fullscreenWithin+ }}+ {{this.trackFullscreenWithin this.setFullscreenWithin}}><div class="live-room-page__main">@@ -574,6 +743,7 @@ export default class LiveRoomPage extends Component {<div class="live-room-page__presentation"><div class="live-room-page__presentation-main"><LiveVideoTile+ @animatesLayout={{true}}@isSelf={{this.presentationTile.isSelf}}@onSpotlight={{this.toggleSpotlight}}@participant={{this.presentationTile.participant}}@@ -594,6 +764,7 @@ export default class LiveRoomPage extends Component {as |tile|}}<LiveVideoTile+ @animatesLayout={{true}}@isSelf={{tile.isSelf}}@onSpotlight={{this.toggleSpotlight}}@participant={{tile.participant}}@@ -628,6 +799,7 @@ export default class LiveRoomPage extends Component {<div class="live-room-page__tiles">{{#each this.gridTiles key="participant.id" as |tile|}}<LiveVideoTile+ @animatesLayout={{true}}@isSelf={{tile.isSelf}}@onSpotlight={{this.toggleSpotlight}}@participant={{tile.participant}}@@ -643,6 +815,7 @@ export default class LiveRoomPage extends Component {{{#if this.pipTile}}<LiveVideoTileclass="live-room-page__pip"+ @animatesLayout={{true}}@isSelf={{this.pipTile.isSelf}}@onSpotlight={{this.toggleSpotlight}}@participant={{this.pipTile.participant}}diff --git a/plugins/live/assets/javascripts/discourse/components/live/video-tile.gjs b/plugins/live/assets/javascripts/discourse/components/live/video-tile.gjsindex 5b2355a42a2..d10090daad0 100644--- a/plugins/live/assets/javascripts/discourse/components/live/video-tile.gjs+++ b/plugins/live/assets/javascripts/discourse/components/live/video-tile.gjs@@ -46,7 +46,16 @@ export default class LiveVideoTile extends Component {}get tileStyle() {- return trustHTML(`aspect-ratio: ${this.aspect ?? DEFAULT_TILE_ASPECT};`);+ let style = `aspect-ratio: ${this.aspect ?? DEFAULT_TILE_ASPECT};`;++ // View transition names must be unique on the page, so only the room+ // page's tiles opt in.+ if (this.args.animatesLayout) {+ const name = CSS.escape(`live-tile-${this.participant.id}`);+ style += ` view-transition-name: ${name};`;+ }++ return trustHTML(style);}get isPortrait() {diff --git a/plugins/live/assets/javascripts/discourse/lib/live/fullscreen.js b/plugins/live/assets/javascripts/discourse/lib/live/fullscreen.jsindex a3b35c9bb51..680923019ed 100644--- a/plugins/live/assets/javascripts/discourse/lib/live/fullscreen.js+++ b/plugins/live/assets/javascripts/discourse/lib/live/fullscreen.js@@ -36,6 +36,20 @@ export function toggleTileFullscreen(tile) {toggleFullscreen(tile, { fallbackVideo: true });}+// Reports whether the fullscreen element is `element` or inside it, updating+// on every fullscreen change.+export const trackFullscreenWithin = modifier((element, [onChange]) => {+ const update = () => onChange(element.contains(fullscreenElement()));++ document.addEventListener("fullscreenchange", update);+ document.addEventListener("webkitfullscreenchange", update);++ return () => {+ document.removeEventListener("fullscreenchange", update);+ document.removeEventListener("webkitfullscreenchange", update);+ };+});+// Reports whether `element` is the current fullscreen element, updating on// every fullscreen change so a tile can flip its enter/exit affordance.export const trackFullscreen = modifier((element, [onChange]) => {diff --git a/plugins/live/assets/stylesheets/common/live-room-page.scss b/plugins/live/assets/stylesheets/common/live-room-page.scssindex 07004006ecc..d3ef904c14a 100644--- a/plugins/live/assets/stylesheets/common/live-room-page.scss+++ b/plugins/live/assets/stylesheets/common/live-room-page.scss@@ -1170,3 +1170,33 @@ body.has-sidebar-page #main-outlet-wrapper:has(.live-room-header) {opacity: 1;}}++// Tiles carry per-participant view-transition names (see followLayout in+// LiveRoomPage); these classes time every tile's move together.+.live-room-page .live-video-tile {+ view-transition-class: live-tile;+}++.live-room-page__presentation-main .live-video-tile {+ view-transition-class: live-tile live-stage-tile;+}++::view-transition-group(*.live-tile) {+ z-index: 2;+ animation-duration: 0.4s;+ animation-timing-function: cubic-bezier(0.2, 0, 0, 1);+}++// The stage tile travels the furthest; keep the other tiles moving over it+// rather than disappearing underneath.+::view-transition-group(*.live-stage-tile) {+ z-index: 1;+}++// Fill the moving box so the old and new snapshots stay aligned while they+// crossfade, instead of each keeping its own aspect and ghosting.+::view-transition-old(*.live-tile),+::view-transition-new(*.live-tile) {+ height: 100%;+ object-fit: cover;+}diff --git a/plugins/live/config/locales/client.en.yml b/plugins/live/config/locales/client.en.ymlindex d253bc9175b..34decb3e699 100644--- a/plugins/live/config/locales/client.en.yml+++ b/plugins/live/config/locales/client.en.yml@@ -103,6 +103,7 @@ en:exit_fullscreen: "Exit full screen"fullscreen_all: "Full-screen all videos"layout: "Video layout"+ layout_auto: "Auto layout"layout_presentation: "Presentation layout"layout_tiled: "Tiled layout"ptt:diff --git a/plugins/live/plugin.rb b/plugins/live/plugin.rbindex ee7c85d722f..7e5564f58a9 100644--- a/plugins/live/plugin.rb+++ b/plugins/live/plugin.rb@@ -71,6 +71,7 @@ register_svg_icon "xmark"register_svg_icon "up-right-from-square"register_svg_icon "person-chalkboard"register_svg_icon "table-cells"+register_svg_icon "wand-magic-sparkles"register_svg_icon "circle-check"register_svg_icon "circle-xmark"register_svg_icon "triangle-exclamation"diff --git a/plugins/live/test/javascripts/integration/components/live-room-page-test.gjs b/plugins/live/test/javascripts/integration/components/live-room-page-test.gjsindex dcf21cf7485..c6df74b16a6 100644--- a/plugins/live/test/javascripts/integration/components/live-room-page-test.gjs+++ b/plugins/live/test/javascripts/integration/components/live-room-page-test.gjs@@ -105,6 +105,16 @@ async function selectParticipantFromOpenMenu(owner) {await menuService.close(menu);}+async function selectLayout(owner, layout) {+ await click(".live-room-menu-trigger");+ await click(".live-room-page__layout-trigger");+ owner+ .lookup("service:menu")+ .getByIdentifier("live-call-submenu")+ .options.data.onSelect(layout);+ await settled();+}+module("Integration | Component | Live | RoomPage", function (hooks) {setupRenderingTest(hooks);@@ -155,6 +165,18 @@ module("Integration | Component | Live | RoomPage", function (hooks) {};this.liveRooms.rooms = [this.room];++ // Publishes a roster change the way the room service does, with a new+ // room object, so tracked getters see it.+ this.updateParticipant = (id, changes) => {+ this.room = {+ ...this.room,+ active_participants: this.room.active_participants.map((participant) =>+ participant.id === id ? { ...participant, ...changes } : participant+ ),+ };+ this.liveRooms.rooms = [this.room];+ };});test("defaults to the tiled layout with the layout switcher available", async function (assert) {@@ -286,14 +308,7 @@ module("Integration | Component | Live | RoomPage", function (hooks) {await click(".live-video-tile[data-user-id='3'] .live-video-tile__menu");await selectParticipantFromOpenMenu(this.owner);await settled();- await click(".live-room-menu-trigger");- await click(".live-room-page__layout-trigger");-- this.owner- .lookup("service:menu")- .getByIdentifier("live-call-submenu")- .options.data.onSelect("presentation");- await settled();+ await selectLayout(this.owner, "presentation");assert.dom(@@ -365,6 +380,149 @@ module("Integration | Component | Live | RoomPage", function (hooks) {.exists("does not restore a stale spotlight if the participant rejoins");});+ test("auto layout features another participant's screen share while it lasts", async function (assert) {+ await render(<template><LiveRoomPage @room={{this.room}} /></template>);++ assert.dom(".live-room-page").hasClass("--tiled", "starts tiled");++ this.updateParticipant(2, { is_screen_sharing: true });+ await settled();++ assert+ .dom(".live-room-page")+ .hasClass("--presentation", "switches to presentation for the share");+ assert+ .dom(+ ".live-room-page__presentation-main .live-video-tile[data-user-id='2']"+ )+ .exists("features the participant who is sharing");++ this.updateParticipant(2, { is_screen_sharing: false });+ await settled();++ assert+ .dom(".live-room-page")+ .hasClass("--tiled", "returns to tiles when the share stops");+ });++ test("auto layout ignores the viewer's own screen share", async function (assert) {+ this.liveWebrtc.localVideoKind = "screen";+ this.updateParticipant(this.currentUser.id, { is_screen_sharing: true });++ await render(<template><LiveRoomPage @room={{this.room}} /></template>);++ assert+ .dom(".live-room-page")+ .hasClass("--tiled", "keeps the presenter looking at the room");+ });++ test("auto layout features another share over the viewer's own", async function (assert) {+ this.liveWebrtc.localVideoKind = "screen";+ this.updateParticipant(this.currentUser.id, { is_screen_sharing: true });+ this.updateParticipant(3, { is_screen_sharing: true });++ await render(<template><LiveRoomPage @room={{this.room}} /></template>);++ assert+ .dom(+ ".live-room-page__presentation-main .live-video-tile[data-user-id='3']"+ )+ .exists("features the other participant's share");+ });++ test("a fixed layout choice is not overridden by a screen share", async function (assert) {+ await render(<template><LiveRoomPage @room={{this.room}} /></template>);+ await selectLayout(this.owner, "tiled");++ this.updateParticipant(2, { is_screen_sharing: true });+ await settled();++ assert+ .dom(".live-room-page")+ .hasClass("--tiled", "keeps the layout the viewer picked");++ await selectLayout(this.owner, "auto");++ assert+ .dom(".live-room-page")+ .hasClass("--presentation", "choosing auto again follows the share");+ });++ test("auto layout waits for a fullscreen tile to exit before switching", async function (assert) {+ await render(<template><LiveRoomPage @room={{this.room}} /></template>);++ const tile = document.querySelector(".live-video-tile[data-user-id='3']");+ Object.defineProperty(document, "fullscreenElement", {+ configurable: true,+ get: () => tile,+ });++ try {+ document.dispatchEvent(new Event("fullscreenchange"));+ this.updateParticipant(2, { is_screen_sharing: true });+ await settled();++ assert+ .dom(".live-room-page")+ .hasClass("--tiled", "keeps the fullscreen tile mounted");+ } finally {+ delete document.fullscreenElement;+ }++ document.dispatchEvent(new Event("fullscreenchange"));+ await settled();++ assert+ .dom(".live-room-page")+ .hasClass("--presentation", "switches once fullscreen ends");+ });++ test("unpinning in auto layout returns to tiles when nobody is sharing", async function (assert) {+ await render(<template><LiveRoomPage @room={{this.room}} /></template>);++ await click(".live-video-tile[data-user-id='3'] .live-video-tile__menu");+ await selectParticipantFromOpenMenu(this.owner);+ await settled();++ assert+ .dom(".live-room-page")+ .hasClass("--presentation", "pinning features the participant");++ await click(".live-room-page__presentation-main .live-video-tile__menu");+ await selectParticipantFromOpenMenu(this.owner);+ await settled();++ assert+ .dom(".live-room-page")+ .hasClass("--tiled", "unpinning returns to tiles");+ });++ test("a featured screen share receives the mobile video budget", async function (assert) {+ this.owner.lookup("service:capabilities").viewport.md = false;+ this.room.active_participants.push(+ ...[4, 5, 6].map((id) => ({+ id,+ username: `user${id}`,+ avatar_template: `/letter_avatar_proxy/v4/letter/u/{size}.png`,+ is_video_on: true,+ })),+ {+ id: 7,+ username: "presenter",+ avatar_template: `/letter_avatar_proxy/v4/letter/p/{size}.png`,+ is_screen_sharing: true,+ }+ );++ await render(<template><LiveRoomPage @room={{this.room}} /></template>);++ assert+ .dom(+ ".live-room-page__presentation-main .live-video-tile[data-user-id='7']"+ )+ .hasClass("--video", "the share is live video, not an avatar");+ });+test("a joined stage room with chat available opens the chat panel by default", async function (assert) {// The panel prepares its chat session on mount; an empty payload keeps it// in the pre-thread composer state without touching chat internals.