chuvsu-js 5.5.2 → 5.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +6 -9
  2. package/dist/common/http.js +2 -4
  3. package/dist/common/lesson-type.js +6 -1
  4. package/dist/common/parse.js +3 -1
  5. package/dist/core.d.ts +2 -1
  6. package/dist/core.js +2 -1
  7. package/dist/index.d.ts +1 -1
  8. package/dist/lk/client.js +10 -4
  9. package/dist/lk/parse/grade-value.js +5 -1
  10. package/dist/lk/parse/portfolio-content.js +58 -16
  11. package/dist/lk/parse/portfolio-grades.js +37 -9
  12. package/dist/lk/parse/portfolio-performance.js +26 -8
  13. package/dist/lk/parse/portfolio.js +31 -13
  14. package/dist/lk/parse/webinar-lesson.js +4 -2
  15. package/dist/lk/parse/webinars.js +7 -3
  16. package/dist/lk/webinars.js +3 -1
  17. package/dist/tt/client.js +15 -7
  18. package/dist/tt/domain/directory.js +2 -4
  19. package/dist/tt/domain/index.d.ts +2 -2
  20. package/dist/tt/domain/index.js +2 -2
  21. package/dist/tt/domain/repository.js +67 -35
  22. package/dist/tt/domain/schedule-snapshot.js +51 -24
  23. package/dist/tt/domain/schedule.js +7 -3
  24. package/dist/tt/observations.js +7 -2
  25. package/dist/tt/parse/audience.js +3 -1
  26. package/dist/tt/parse/entry-parts.js +2 -3
  27. package/dist/tt/parse/full-schedule.js +1 -1
  28. package/dist/tt/parse/groups.js +1 -3
  29. package/dist/tt/parse/index.d.ts +2 -2
  30. package/dist/tt/parse/index.js +2 -2
  31. package/dist/tt/parse/lists.js +3 -3
  32. package/dist/tt/parse/overlays.js +7 -2
  33. package/dist/tt/utils/date.js +2 -2
  34. package/dist/tt/utils/period.js +2 -1
  35. package/dist/tt/utils/semester.js +2 -2
  36. package/dist/tt/utils/time-slots.js +81 -15
  37. package/dist/webinar/chat-subscription.js +2 -1
  38. package/dist/webinar/client.d.ts +11 -0
  39. package/dist/webinar/client.js +136 -33
  40. package/dist/webinar/ddp.js +40 -13
  41. package/dist/webinar/events.js +7 -2
  42. package/dist/webinar/gateway.js +57 -16
  43. package/dist/webinar/media-protocol.d.ts +2 -1
  44. package/dist/webinar/media-protocol.js +27 -5
  45. package/dist/webinar/media.js +10 -4
  46. package/dist/webinar/parse.js +59 -14
  47. package/dist/webinar/playback.d.ts +1 -0
  48. package/dist/webinar/playback.js +49 -13
  49. package/dist/webinar/socket.js +4 -1
  50. package/dist/webinar/state.js +73 -26
  51. package/dist/webinar/types.d.ts +12 -0
  52. package/dist/webinar/typing.d.ts +8 -0
  53. package/dist/webinar/typing.js +36 -0
  54. package/docs/fixture-review.md +44 -44
  55. package/docs/student-portal.md +11 -2
  56. package/docs/v5-migration.md +85 -88
  57. package/docs/webinar.md +15 -2
  58. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  import { HttpClient } from "../common/http.js";
2
2
  import { AuthError, ParseError } from "../common/types.js";
3
- import { parseWebinarConfig, parseWebinarIceServers, parseWebinarSession } from "./parse.js";
4
- import { WebinarPermissionError } from "./types.js";
3
+ import { parseWebinarConfig, parseWebinarIceServers, parseWebinarSession, } from "./parse.js";
4
+ import { WebinarPermissionError, } from "./types.js";
5
5
  import { WebinarClient } from "./client.js";
6
6
  /** Backend-only LK/BBB bootstrap and same-server presentation HTTP access. */
7
7
  export class WebinarGateway {
@@ -23,14 +23,19 @@ export class WebinarGateway {
23
23
  if (this.session)
24
24
  throw new Error("Create a new gateway for each webinar session");
25
25
  let url = new URL(opts.joinUrl);
26
- if (url.protocol !== "https:" || url.username || url.password || url.pathname !== "/bigbluebutton/api/join") {
26
+ if (url.protocol !== "https:" ||
27
+ url.username ||
28
+ url.password ||
29
+ url.pathname !== "/bigbluebutton/api/join") {
27
30
  throw new RangeError("Expected a signed HTTPS BBB join URL");
28
31
  }
29
32
  const origin = url.origin;
30
33
  let html = null;
31
34
  for (let redirects = 0; redirects < 6; redirects++) {
32
35
  const response = await this.http.get(url.href, false);
33
- if (response.status >= 300 && response.status < 400 && response.location) {
36
+ if (response.status >= 300 &&
37
+ response.status < 400 &&
38
+ response.location) {
34
39
  const next = new URL(response.location, url);
35
40
  if (next.origin !== origin || next.username || next.password)
36
41
  throw new AuthError("Webinar redirected outside its server");
@@ -64,7 +69,12 @@ export class WebinarGateway {
64
69
  const ice = await this.http.get(endpoint("/bigbluebutton/api/stuns"), false);
65
70
  if (ice.status !== 200)
66
71
  throw new AuthError("Webinar ICE configuration failed");
67
- this.session = parseWebinarSession({ clientUrl: url.href, config, entry: data, iceServers: parseWebinarIceServers(ice.body) });
72
+ this.session = parseWebinarSession({
73
+ clientUrl: url.href,
74
+ config,
75
+ entry: data,
76
+ iceServers: parseWebinarIceServers(ice.body),
77
+ });
68
78
  return structuredClone(this.session);
69
79
  }
70
80
  /** Opens state only. Browser media is connected separately by WebinarMediaClient. */
@@ -73,14 +83,18 @@ export class WebinarGateway {
73
83
  throw new AuthError("Open a webinar session first");
74
84
  if (this.client)
75
85
  throw new Error("Webinar gateway state client already opened");
76
- const client = new WebinarClient(this.session, { socketFactory: (url) => this.http.createWebSocket(url) });
86
+ const client = new WebinarClient(this.session, {
87
+ socketFactory: (url) => this.http.createWebSocket(url),
88
+ });
77
89
  await client.connect();
78
90
  this.client = client;
79
91
  return client;
80
92
  }
81
93
  /** Downloads only when the live participant state exposes this permission. */
82
94
  async downloadPresentation(opts) {
83
- const presentation = this.client?.getPresentations().find((item) => item.id === opts.presentationId);
95
+ const presentation = this.client
96
+ ?.getPresentations()
97
+ .find((item) => item.id === opts.presentationId);
84
98
  if (!this.session || !presentation?.downloadable)
85
99
  throw new WebinarPermissionError("Original presentation download is disabled or unavailable");
86
100
  const extension = presentation.name.split(".").pop() ?? "";
@@ -89,32 +103,59 @@ export class WebinarGateway {
89
103
  const url = new URL(`/bigbluebutton/presentation/download/${encodeURIComponent(this.session.meeting.id)}/${encodeURIComponent(presentation.id)}`, this.session.server.origin);
90
104
  url.searchParams.set("presFilename", `${presentation.id}.${extension}`);
91
105
  const response = await this.http.getBufferResponse(url.href, false);
92
- if (response.status !== 200 || !response.contentType || response.contentType.startsWith("text/html")) {
106
+ if (response.status !== 200 ||
107
+ !response.contentType ||
108
+ response.contentType.startsWith("text/html")) {
93
109
  throw new ParseError("Original presentation download failed");
94
110
  }
95
- return { body: response.body, contentType: response.contentType,
96
- filename: presentation.name.replace(/[\\/\x00-\x1f]/gu, "_") };
111
+ return {
112
+ body: response.body,
113
+ contentType: response.contentType,
114
+ filename: presentation.name.replace(/[\\/\x00-\x1f]/gu, "_"),
115
+ };
97
116
  }
98
117
  async downloadSlide(opts) {
99
118
  if (!this.session)
100
119
  throw new AuthError("Open a webinar session first");
101
120
  const format = opts.format ?? "svg";
102
- const value = format === "svg" ? opts.slide.svgUrl : format === "text" ? opts.slide.textUrl : opts.slide.thumbnailUrl;
121
+ const value = format === "svg"
122
+ ? opts.slide.svgUrl
123
+ : format === "text"
124
+ ? opts.slide.textUrl
125
+ : opts.slide.thumbnailUrl;
103
126
  if (!value)
104
127
  throw new ParseError("Slide asset is unavailable");
105
128
  const url = new URL(value);
106
129
  const prefix = `/bigbluebutton/presentation/${encodeURIComponent(this.session.meeting.id)}/`;
107
- if (url.origin !== this.session.server.origin || !url.pathname.startsWith(prefix)
108
- || !/\/(?:svg|thumbnail|textfiles)\/\d+$/u.test(url.pathname) || url.search || url.username || url.password) {
130
+ if (url.origin !== this.session.server.origin ||
131
+ !url.pathname.startsWith(prefix) ||
132
+ !/\/(?:svg|thumbnail|textfiles)\/\d+$/u.test(url.pathname) ||
133
+ url.search ||
134
+ url.username ||
135
+ url.password) {
109
136
  throw new RangeError("Expected a slide asset from the current webinar");
110
137
  }
111
138
  const response = await this.http.getBufferResponse(url.href, false);
112
139
  if (response.status !== 200)
113
140
  throw new Error(`Webinar slide request failed with HTTP ${response.status}`);
114
- const expected = format === "svg" ? "image/svg+xml" : format === "text" ? "text/" : "image/";
141
+ const expected = format === "svg"
142
+ ? "image/svg+xml"
143
+ : format === "text"
144
+ ? "text/"
145
+ : "image/";
115
146
  if (!response.contentType?.startsWith(expected))
116
147
  throw new ParseError("Webinar returned an invalid slide asset");
117
- const extension = format === "svg" ? "svg" : format === "text" ? "txt" : response.contentType.includes("png") ? "png" : "jpg";
118
- return { body: response.body, contentType: response.contentType, filename: `slide-${opts.slide.number}.${extension}` };
148
+ const extension = format === "svg"
149
+ ? "svg"
150
+ : format === "text"
151
+ ? "txt"
152
+ : response.contentType.includes("png")
153
+ ? "png"
154
+ : "jpg";
155
+ return {
156
+ body: response.body,
157
+ contentType: response.contentType,
158
+ filename: `slide-${opts.slide.number}.${extension}`,
159
+ };
119
160
  }
120
161
  }
@@ -3,7 +3,8 @@ import type { WebinarPlaybackKind } from "./media-types.js";
3
3
  import type { WebinarSession } from "./types.js";
4
4
  /** BBB 2.3/2.4 SFU messages. One playback per socket; no publish commands. */
5
5
  export declare function webinarMediaMessages(session: WebinarSession, kind: WebinarPlaybackKind, streamId?: string, hasAudio?: boolean): {
6
- start(sdpOffer: string): WebinarDocument;
6
+ start(sdpOffer?: string): WebinarDocument;
7
+ answer(sdpOffer: string): WebinarDocument;
7
8
  candidate(candidate: RTCIceCandidateInit): WebinarDocument;
8
9
  stop(): WebinarDocument;
9
10
  };
@@ -3,22 +3,44 @@ export function webinarMediaMessages(session, kind, streamId, hasAudio = false)
3
3
  const type = kind === "camera" ? "video" : kind === "screen" ? "screenshare" : "audio";
4
4
  const role = kind === "camera" ? "viewer" : "recv";
5
5
  const identity = { type, role, voiceBridge: session.meeting.voiceBridge };
6
- const specific = kind === "camera" ? { cameraId: streamId } : kind === "screen" ? { callerName: session.user.id } : {};
6
+ const specific = kind === "camera"
7
+ ? { cameraId: streamId }
8
+ : kind === "screen"
9
+ ? { callerName: session.user.id }
10
+ : {};
7
11
  return {
8
12
  start(sdpOffer) {
9
13
  return {
10
- ...identity, ...specific, id: "start", sdpOffer,
14
+ ...identity,
15
+ ...specific,
16
+ id: "start",
17
+ sdpOffer,
11
18
  userName: session.user.name,
12
19
  ...(kind === "camera"
13
20
  ? { meetingId: session.meeting.id, userId: session.user.id }
14
21
  : { internalMeetingId: session.meeting.id }),
15
- ...(kind === "audio" ? { userId: session.user.id, caleeName: `GLOBAL_AUDIO_${session.meeting.voiceBridge}` } : {}),
22
+ ...(kind === "audio"
23
+ ? {
24
+ userId: session.user.id,
25
+ caleeName: `GLOBAL_AUDIO_${session.meeting.voiceBridge}`,
26
+ }
27
+ : {}),
16
28
  ...(kind === "screen" ? { hasAudio } : {}),
17
29
  };
18
30
  },
31
+ answer(sdpOffer) {
32
+ return { ...identity, id: "subscriberAnswer", sdpOffer };
33
+ },
19
34
  candidate(candidate) {
20
- return { ...identity, ...specific, id: kind === "camera" ? "onIceCandidate" : "iceCandidate", candidate };
35
+ return {
36
+ ...identity,
37
+ ...specific,
38
+ id: kind === "camera" ? "onIceCandidate" : "iceCandidate",
39
+ candidate,
40
+ };
41
+ },
42
+ stop() {
43
+ return { ...identity, ...specific, id: "stop" };
21
44
  },
22
- stop() { return { ...identity, ...specific, id: "stop" }; },
23
45
  };
24
46
  }
@@ -1,6 +1,6 @@
1
1
  import { WebinarPlayback } from "./playback.js";
2
2
  import { browserWebinarSocket } from "./socket.js";
3
- import { WebinarPermissionError, WebinarProtocolError } from "./types.js";
3
+ import { WebinarPermissionError, WebinarProtocolError, } from "./types.js";
4
4
  /** Browser media stays direct; the state client supplies permitted stream IDs. */
5
5
  export class WebinarMediaClient {
6
6
  room;
@@ -28,7 +28,9 @@ export class WebinarMediaClient {
28
28
  async receiveCamera(opts) {
29
29
  if (!this.session.features.video)
30
30
  throw new WebinarPermissionError("Camera playback is disabled");
31
- const stream = this.room.getMediaStreams().find((stream) => stream.kind === "camera" && stream.id === opts.streamId);
31
+ const stream = this.room
32
+ .getMediaStreams()
33
+ .find((stream) => stream.kind === "camera" && stream.id === opts.streamId);
32
34
  if (!stream)
33
35
  throw new WebinarPermissionError("Camera stream is not published to this participant");
34
36
  return this.receive("camera", opts, stream.id);
@@ -36,7 +38,9 @@ export class WebinarMediaClient {
36
38
  async receiveScreen(opts = {}) {
37
39
  if (!this.session.features.screen)
38
40
  throw new WebinarPermissionError("Screen playback is disabled");
39
- const stream = this.room.getMediaStreams().find((stream) => stream.kind === "screen");
41
+ const stream = this.room
42
+ .getMediaStreams()
43
+ .find((stream) => stream.kind === "screen");
40
44
  if (!stream)
41
45
  throw new WebinarPermissionError("No screen is being shared");
42
46
  return this.receive("screen", opts, stream.id, stream.hasAudio);
@@ -56,7 +60,9 @@ export class WebinarMediaClient {
56
60
  throw error;
57
61
  }
58
62
  this.playbacks.add(playback);
59
- playback.onClose(() => { this.playbacks.delete(playback); });
63
+ playback.onClose(() => {
64
+ this.playbacks.delete(playback);
65
+ });
60
66
  try {
61
67
  await playback.connect(this.timeout, opts);
62
68
  return playback;
@@ -2,7 +2,8 @@ import { ParseError } from "../common/types.js";
2
2
  import { WebinarProtocolError } from "./types.js";
3
3
  export function record(value) {
4
4
  return value !== null && typeof value === "object" && !Array.isArray(value)
5
- ? value : {};
5
+ ? value
6
+ : {};
6
7
  }
7
8
  export function string(value) {
8
9
  return typeof value === "string" ? value : "";
@@ -12,8 +13,12 @@ export function safeWebinarUrl(value, origin) {
12
13
  return null;
13
14
  try {
14
15
  const url = new URL(value, origin);
15
- return url.protocol === "https:" && url.origin === origin && !url.username && !url.password
16
- ? url.href : null;
16
+ return url.protocol === "https:" &&
17
+ url.origin === origin &&
18
+ !url.username &&
19
+ !url.password
20
+ ? url.href
21
+ : null;
17
22
  }
18
23
  catch {
19
24
  return null;
@@ -44,8 +49,14 @@ export function parseWebinarSession(opts) {
44
49
  const chat = record(opts.config.chat);
45
50
  const version = String(app.bbbServerVersion ?? "");
46
51
  const token = url.searchParams.get("sessionToken");
47
- const required = [token, response.authToken, response.internalUserID, response.meetingID,
48
- response.fullname, response.voicebridge];
52
+ const required = [
53
+ token,
54
+ response.authToken,
55
+ response.internalUserID,
56
+ response.meetingID,
57
+ response.fullname,
58
+ response.voicebridge,
59
+ ];
49
60
  if (required.some((value) => typeof value !== "string" || !value)) {
50
61
  throw new ParseError("Incomplete webinar participant credentials");
51
62
  }
@@ -60,18 +71,36 @@ export function parseWebinarSession(opts) {
60
71
  media.searchParams.set("sessionToken", token);
61
72
  const enabled = (value) => value === true;
62
73
  return {
63
- server: { origin: url.origin, version, build: typeof app.html5ClientBuild === "number" ? app.html5ClientBuild : null },
74
+ server: {
75
+ origin: url.origin,
76
+ version,
77
+ build: typeof app.html5ClientBuild === "number" ? app.html5ClientBuild : null,
78
+ },
64
79
  protocol: "ddp",
65
80
  mediaBackend: "kurento",
66
81
  sessionToken: token,
67
82
  authToken: string(response.authToken),
68
83
  externalUserId: string(response.externUserID),
69
- user: { id: string(response.internalUserID), name: string(response.fullname), role: string(response.role) },
70
- meeting: { id: string(response.meetingID), name: string(response.confname), voiceBridge: string(response.voicebridge) },
84
+ user: {
85
+ id: string(response.internalUserID),
86
+ name: string(response.fullname),
87
+ role: string(response.role),
88
+ },
89
+ meeting: {
90
+ id: string(response.meetingID),
91
+ name: string(response.confname),
92
+ voiceBridge: string(response.voicebridge),
93
+ },
71
94
  connections: { state: state.href, media: media.href },
72
95
  iceServers: opts.iceServers,
96
+ mediaSettings: {
97
+ listenOnlyOffering: record(opts.config.media).listenOnlyOffering !== false,
98
+ forceRelay: record(opts.config.media).forceRelay === true,
99
+ forceRelayOnFirefox: kurento.forceRelayOnFirefox === true,
100
+ },
73
101
  features: {
74
102
  chat: enabled(chat.enabled),
103
+ typing: enabled(record(chat.typingIndicator).enabled),
75
104
  notes: enabled(record(opts.config.note).enabled),
76
105
  audio: enabled(kurento.enableListenOnly),
77
106
  video: enabled(kurento.enableVideo),
@@ -81,26 +110,42 @@ export function parseWebinarSession(opts) {
81
110
  },
82
111
  notesUrl: safeWebinarUrl(record(opts.config.note).url, url.origin),
83
112
  publicChatId: string(chat.public_group_id) || "MAIN-PUBLIC-GROUP-CHAT",
84
- chatPageSize: typeof chat.itemsPerPage === "number" && chat.itemsPerPage > 0 ? chat.itemsPerPage : 100,
85
- maxMessageLength: typeof chat.max_message_length === "number" && chat.max_message_length > 0 ? chat.max_message_length : 5_000,
113
+ publicTypingId: string(chat.public_id) ||
114
+ string(chat.public_group_id) ||
115
+ "MAIN-PUBLIC-GROUP-CHAT",
116
+ chatPageSize: typeof chat.itemsPerPage === "number" && chat.itemsPerPage > 0
117
+ ? chat.itemsPerPage
118
+ : 100,
119
+ maxMessageLength: typeof chat.max_message_length === "number" && chat.max_message_length > 0
120
+ ? chat.max_message_length
121
+ : 5_000,
86
122
  };
87
123
  }
88
124
  export function parseWebinarIceServers(body) {
89
125
  try {
90
126
  const response = record(JSON.parse(body));
91
- if (!Array.isArray(response.stunServers) || !Array.isArray(response.turnServers))
127
+ if (!Array.isArray(response.stunServers) ||
128
+ !Array.isArray(response.turnServers))
92
129
  throw new Error();
93
130
  const servers = [];
94
- for (const stun of Array.isArray(response.stunServers) ? response.stunServers : []) {
131
+ for (const stun of Array.isArray(response.stunServers)
132
+ ? response.stunServers
133
+ : []) {
95
134
  const url = string(record(stun).url) || string(stun);
96
135
  if (/^stuns?:/u.test(url))
97
136
  servers.push({ urls: url });
98
137
  }
99
- for (const turn of Array.isArray(response.turnServers) ? response.turnServers : []) {
138
+ for (const turn of Array.isArray(response.turnServers)
139
+ ? response.turnServers
140
+ : []) {
100
141
  const data = record(turn);
101
142
  const url = string(data.url);
102
143
  if (/^turns?:/u.test(url))
103
- servers.push({ urls: url, username: string(data.username), credential: string(data.password) });
144
+ servers.push({
145
+ urls: url,
146
+ username: string(data.username),
147
+ credential: string(data.password),
148
+ });
104
149
  }
105
150
  return servers;
106
151
  }
@@ -10,6 +10,7 @@ export declare class WebinarPlayback {
10
10
  private closed;
11
11
  private started;
12
12
  private answered;
13
+ private offering;
13
14
  private localCandidates;
14
15
  private remoteCandidates;
15
16
  private pendingReceive;
@@ -1,7 +1,7 @@
1
1
  import { record, string } from "./parse.js";
2
2
  import { openWebinarSocket } from "./socket.js";
3
3
  import { webinarMediaMessages } from "./media-protocol.js";
4
- import { WebinarProtocolError } from "./types.js";
4
+ import { WebinarProtocolError, } from "./types.js";
5
5
  /** Direct browser-to-BBB receiver. Attach stream to an audio/video element. */
6
6
  export class WebinarPlayback {
7
7
  kind;
@@ -12,6 +12,7 @@ export class WebinarPlayback {
12
12
  closed = false;
13
13
  started = false;
14
14
  answered = false;
15
+ offering;
15
16
  localCandidates = [];
16
17
  remoteCandidates = [];
17
18
  pendingReceive = Promise.resolve();
@@ -25,9 +26,19 @@ export class WebinarPlayback {
25
26
  constructor(session, kind, socket, opts, streamId, hasAudio = false) {
26
27
  this.kind = kind;
27
28
  this.socket = socket;
29
+ this.offering =
30
+ kind !== "audio" || session.mediaSettings?.listenOnlyOffering !== false;
28
31
  this.messages = webinarMediaMessages(session, kind, streamId, hasAudio);
29
32
  this.stream = new MediaStream();
30
- this.connection = (opts.peerFactory ?? ((config) => new RTCPeerConnection(config)))({ iceServers: session.iceServers });
33
+ this.connection = (opts.peerFactory ?? ((config) => new RTCPeerConnection(config)))({
34
+ iceServers: session.iceServers,
35
+ iceTransportPolicy: session.mediaSettings?.forceRelay ||
36
+ (session.mediaSettings?.forceRelayOnFirefox &&
37
+ typeof navigator !== "undefined" &&
38
+ /Firefox\//u.test(navigator.userAgent))
39
+ ? "relay"
40
+ : "all",
41
+ });
31
42
  if (kind !== "camera" && (kind === "audio" || hasAudio))
32
43
  this.connection.addTransceiver("audio", { direction: "recvonly" });
33
44
  if (kind !== "audio")
@@ -52,13 +63,15 @@ export class WebinarPlayback {
52
63
  this.fail(new WebinarProtocolError("Webinar media connection failed"));
53
64
  });
54
65
  socket.addEventListener("message", (event) => {
55
- this.pendingReceive = this.pendingReceive.then(async () => {
66
+ this.pendingReceive = this.pendingReceive
67
+ .then(async () => {
56
68
  if (this.closed)
57
69
  return;
58
70
  if (typeof event.data !== "string")
59
71
  throw new WebinarProtocolError("Invalid media frame");
60
72
  await this.receive(record(JSON.parse(event.data)));
61
- }).catch(() => this.fail(new WebinarProtocolError("Webinar media negotiation failed")));
73
+ })
74
+ .catch(() => this.fail(new WebinarProtocolError("Webinar media negotiation failed")));
62
75
  });
63
76
  socket.addEventListener("close", () => {
64
77
  if (!this.closed)
@@ -90,12 +103,15 @@ export class WebinarPlayback {
90
103
  await openWebinarSocket(this.socket, timeout);
91
104
  if (this.closed)
92
105
  throw new WebinarProtocolError("Webinar playback closed");
93
- const offer = await this.connection.createOffer();
94
- await this.connection.setLocalDescription(offer);
106
+ let offer;
107
+ if (this.offering) {
108
+ offer = await this.connection.createOffer();
109
+ await this.connection.setLocalDescription(offer);
110
+ }
95
111
  if (this.closed)
96
112
  throw new WebinarProtocolError("Webinar playback closed");
97
113
  this.started = true;
98
- this.send(this.messages.start(offer.sdp ?? ""));
114
+ this.send(this.messages.start(offer?.sdp));
99
115
  this.pingTimer = setInterval(() => this.send({ id: "ping" }), 10_000);
100
116
  await connected;
101
117
  }
@@ -117,7 +133,17 @@ export class WebinarPlayback {
117
133
  if (message.id === "startResponse") {
118
134
  if (message.response !== "accepted" || !string(message.sdpAnswer))
119
135
  throw new WebinarProtocolError("Webinar media offer rejected");
120
- await this.connection.setRemoteDescription({ type: "answer", sdp: string(message.sdpAnswer) });
136
+ await this.connection.setRemoteDescription({
137
+ type: this.offering ? "answer" : "offer",
138
+ sdp: string(message.sdpAnswer),
139
+ });
140
+ if (!this.offering) {
141
+ const answer = await this.connection.createAnswer();
142
+ await this.connection.setLocalDescription(answer);
143
+ if (this.closed)
144
+ return;
145
+ this.send(this.messages.answer(answer.sdp ?? ""));
146
+ }
121
147
  this.answered = true;
122
148
  for (const candidate of this.remoteCandidates.splice(0))
123
149
  await this.connection.addIceCandidate(candidate);
@@ -140,11 +166,15 @@ export class WebinarPlayback {
140
166
  }
141
167
  onError(listener) {
142
168
  this.errorListeners.add(listener);
143
- return () => { this.errorListeners.delete(listener); };
169
+ return () => {
170
+ this.errorListeners.delete(listener);
171
+ };
144
172
  }
145
173
  onClose(listener) {
146
174
  this.closeListeners.add(listener);
147
- return () => { this.closeListeners.delete(listener); };
175
+ return () => {
176
+ this.closeListeners.delete(listener);
177
+ };
148
178
  }
149
179
  fail(error) {
150
180
  if (this.closed)
@@ -154,11 +184,15 @@ export class WebinarPlayback {
154
184
  try {
155
185
  listener(error);
156
186
  }
157
- catch { /* Caller owns listener errors. */ }
187
+ catch {
188
+ /* Caller owns listener errors. */
189
+ }
158
190
  }
159
191
  this.close();
160
192
  }
161
- getStats() { return this.connection.getStats(); }
193
+ getStats() {
194
+ return this.connection.getStats();
195
+ }
162
196
  close() {
163
197
  if (this.closed)
164
198
  return;
@@ -181,7 +215,9 @@ export class WebinarPlayback {
181
215
  try {
182
216
  listener();
183
217
  }
184
- catch { /* Caller owns listener errors. */ }
218
+ catch {
219
+ /* Caller owns listener errors. */
220
+ }
185
221
  }
186
222
  this.closeListeners.clear();
187
223
  }
@@ -11,7 +11,10 @@ export async function openWebinarSocket(socket, timeout) {
11
11
  clearTimeout(timer);
12
12
  reject(new Error("Webinar WebSocket connection failed"));
13
13
  };
14
- socket.addEventListener("open", () => { clearTimeout(timer); resolve(); });
14
+ socket.addEventListener("open", () => {
15
+ clearTimeout(timer);
16
+ resolve();
17
+ });
15
18
  socket.addEventListener("error", fail);
16
19
  socket.addEventListener("close", fail);
17
20
  });