SPB Git

Repository Public

calls.ts 13.5 KB 381 lines typescript

1/**2 * WebRTC calls. Media flows peer to peer (DTLS-SRTP), through the TURN relay only when a direct3 * path is impossible. All signalling (session descriptions with their DTLS fingerprints, ICE4 * candidates) travels inside MLS-encrypted messages, so neither the server nor the TURN relay can5 * substitute keys without breaking the handshake. Group calls are a full mesh: every participant6 * holds one encrypted connection to every other participant; no media server sees the streams.7 */8import { uuid } from "./bytes"9import type { CallSignal, Engine } from "./engine"1011export type Peer = { deviceId: string; userId: string; pc: RTCPeerConnection; stream: MediaStream; offerer: boolean; state: string; queued: RTCIceCandidateInit[]; out: RTCIceCandidateInit[]; flush?: ReturnType<typeof setTimeout> }12export type CallState = {13  id: string14  conv: string15  video: boolean16  status: "ringing-in" | "ringing-out" | "active"17  from: string18  peers: Map<string, Peer>19  local: MediaStream | null20  muted: boolean21  cameraOff: boolean22  sharing: boolean23  startedAt: number24  facing: "user" | "environment"25}2627export const MAX_MESH = 628const RING_MS = 45_0002930export class CallManager {31  current: CallState | null = null32  waiting: { id: string; conv: string; video: boolean; from: string } | null = null33  error: string | null = null34  private listeners = new Set<() => void>()35  private ice: RTCIceServer[] = []36  private ringTimer: ReturnType<typeof setTimeout> | null = null37  private ended = new Set<string>()38  private screenTrack: MediaStreamTrack | null = null39  private cameraTrack: MediaStreamTrack | null = null4041  constructor(private engine: Engine) {42    engine.onCall = (s) => void this.onSignal(s).catch((e) => console.warn("call signal:", e?.message))43  }44  subscribe = (fn: () => void) => {45    this.listeners.add(fn)46    return () => this.listeners.delete(fn)47  }48  clearError() {49    this.error = null50    this.emit()51  }52  version = 053  private emit() {54    this.version++55    for (const l of this.listeners) l()56  }57  private send(conv: string, call: string, op: "ring" | "join" | "offer" | "answer" | "ice" | "leave" | "decline", extra: Record<string, unknown> = {}) {58    return this.engine.send(conv, { t: "call", call, op, ...extra } as never)59  }6061  private async media(video: boolean, facing: "user" | "environment" = "user") {62    return navigator.mediaDevices.getUserMedia({63      audio: { echoCancellation: true, noiseSuppression: true, autoGainControl: true },64      video: video ? { facingMode: facing, width: { ideal: 1280 }, height: { ideal: 720 } } : false,65    })66  }67  private async loadIce() {68    try {69      this.ice = (await this.engine.t.request<{ ice_servers: RTCIceServer[] }>("GET", "/api/turn")).ice_servers70    } catch {71      this.ice = []72    }73  }7475  async start(conv: string, video: boolean) {76    if (this.current) return77    this.error = null78    const c = this.engine.convs.get(conv)79    if (!c?.state) return80    if (c.serverMembers.length > MAX_MESH) {81      this.error = `Group calls are limited to ${MAX_MESH} participants.`82      return this.emit()83    }84    let local: MediaStream85    try {86      local = await this.media(video)87    } catch {88      this.error = "Microphone or camera access was refused."89      return this.emit()90    }91    await this.loadIce()92    const id = uuid()93    this.current = { id, conv, video, status: "ringing-out", from: this.engine.me.id, peers: new Map(), local, muted: false, cameraOff: false, sharing: false, startedAt: 0, facing: "user" }94    this.cameraTrack = local.getVideoTracks()[0] ?? null95    await this.send(conv, id, "ring", { video })96    this.ringTimer = setTimeout(() => {97      if (this.current?.id === id && this.current.status === "ringing-out") void this.hangup("unanswered")98    }, RING_MS)99    this.emit()100  }101102  async accept(fromWaiting = false) {103    if (fromWaiting && this.waiting) {104      const w = this.waiting105      await this.hangup()106      this.waiting = null107      this.current = { id: w.id, conv: w.conv, video: w.video, status: "ringing-in", from: w.from, peers: new Map(), local: null, muted: false, cameraOff: false, sharing: false, startedAt: 0, facing: "user" }108    }109    const cur = this.current110    if (!cur || cur.status !== "ringing-in") return111    try {112      cur.local = await this.media(cur.video)113    } catch {114      try {115        cur.local = await this.media(false)116        cur.video = false117      } catch {118        this.error = "Microphone access was refused."119        await this.decline()120        return121      }122    }123    this.cameraTrack = cur.local.getVideoTracks()[0] ?? null124    await this.loadIce()125    cur.status = "active"126    cur.startedAt = Date.now()127    if (this.ringTimer) clearTimeout(this.ringTimer)128    await this.send(cur.conv, cur.id, "join")129    this.engine.annotateCall(cur.conv, cur.id, "answered")130    this.emit()131  }132133  async decline(waiting = false) {134    if (waiting && this.waiting) {135      await this.send(this.waiting.conv, this.waiting.id, "decline")136      this.ended.add(this.waiting.id)137      this.waiting = null138      return this.emit()139    }140    const cur = this.current141    if (!cur) return142    await this.send(cur.conv, cur.id, "decline")143    this.engine.annotateCall(cur.conv, cur.id, "declined")144    this.cleanup()145  }146147  async hangup(reason?: string) {148    const cur = this.current149    if (!cur) return150    await this.send(cur.conv, cur.id, "leave").catch(() => {})151    if (cur.startedAt) this.engine.annotateCall(cur.conv, cur.id, `duration:${Math.round((Date.now() - cur.startedAt) / 1000)}`)152    else if (reason) this.engine.annotateCall(cur.conv, cur.id, reason)153    this.cleanup()154  }155156  private cleanup() {157    const cur = this.current158    if (cur) {159      this.ended.add(cur.id)160      for (const p of cur.peers.values()) p.pc.close()161      cur.local?.getTracks().forEach((t) => t.stop())162    }163    this.screenTrack?.stop()164    this.screenTrack = null165    this.cameraTrack = null166    if (this.ringTimer) clearTimeout(this.ringTimer)167    this.current = null168    this.emit()169  }170171  private peer(cur: CallState, deviceId: string, userId: string, offerer: boolean): Peer {172    const existing = cur.peers.get(deviceId)173    if (existing) return existing174    const pc = new RTCPeerConnection({ iceServers: this.ice })175    const p: Peer = { deviceId, userId, pc, stream: new MediaStream(), offerer, state: "connecting", queued: [], out: [] }176    cur.peers.set(deviceId, p)177    cur.local?.getTracks().forEach((t) => pc.addTrack(t, cur.local!))178    pc.ontrack = (e) => {179      p.stream.addTrack(e.track)180      this.emit()181    }182    pc.onicecandidate = (e) => {183      if (!e.candidate) return184      p.out.push(e.candidate.toJSON())185      // Candidates are batched: each batch is one encrypted message.186      p.flush ??= setTimeout(() => {187        p.flush = undefined188        const cands = p.out.splice(0)189        if (cands.length && this.current?.id === cur.id) void this.send(cur.conv, cur.id, "ice", { to: deviceId, cands })190      }, 350)191    }192    pc.onconnectionstatechange = () => {193      p.state = pc.connectionState194      if (pc.connectionState === "failed" && p.offerer && this.current?.id === cur.id) void this.negotiate(cur, p, true)195      if (pc.connectionState === "closed") cur.peers.delete(deviceId)196      this.emit()197    }198    return p199  }200201  private async negotiate(cur: CallState, p: Peer, iceRestart = false) {202    const offer = await p.pc.createOffer({ iceRestart })203    await p.pc.setLocalDescription(offer)204    await this.send(cur.conv, cur.id, "offer", { to: p.deviceId, sdp: offer.sdp })205  }206207  private async onSignal(s: CallSignal) {208    const { content: m, from } = s209    const me = this.engine.me.id, myDev = this.engine.deviceId210    const cur = this.current211    const mineElsewhere = from.userId === me && from.deviceId !== myDev212213    if (m.op === "ring") {214      if (from.userId === me || this.ended.has(m.call)) return215      if (this.engine.now() - s.at > RING_MS) return void this.engine.annotateCall(s.conv, m.call, "missed")216      if (cur) {217        if (cur.id !== m.call) {218          this.waiting = { id: m.call, conv: s.conv, video: !!m.video, from: from.userId }219          this.emit()220        }221        return222      }223      this.current = { id: m.call, conv: s.conv, video: !!m.video, status: "ringing-in", from: from.userId, peers: new Map(), local: null, muted: false, cameraOff: false, sharing: false, startedAt: 0, facing: "user" }224      this.ringTimer = setTimeout(() => {225        if (this.current?.id === m.call && this.current.status === "ringing-in") {226          this.engine.annotateCall(s.conv, m.call, "missed")227          this.cleanup()228        }229      }, Math.max(1000, RING_MS - (this.engine.now() - s.at)))230      return this.emit()231    }232233    if (this.waiting?.id === m.call && (m.op === "leave" || (mineElsewhere && (m.op === "join" || m.op === "decline")))) {234      this.waiting = null235      this.emit()236    }237    if (!cur || cur.id !== m.call) return238239    if (cur.status === "ringing-in") {240      // answered or declined on another of my devices, or the caller gave up241      if (mineElsewhere && (m.op === "join" || m.op === "decline")) return this.cleanup()242      if (m.op === "leave" && from.userId === cur.from) {243        this.engine.annotateCall(cur.conv, cur.id, "missed")244        return this.cleanup()245      }246      return247    }248249    switch (m.op) {250      case "join": {251        if (mineElsewhere || cur.peers.size >= MAX_MESH - 1) return252        if (cur.status === "ringing-out") {253          cur.status = "active"254          cur.startedAt = Date.now()255          if (this.ringTimer) clearTimeout(this.ringTimer)256          this.engine.annotateCall(cur.conv, cur.id, "answered")257        }258        const p = this.peer(cur, from.deviceId, from.userId, true)259        await this.negotiate(cur, p)260        break261      }262      case "offer": {263        if (m.to !== myDev || !m.sdp) return264        const p = this.peer(cur, from.deviceId, from.userId, false)265        await p.pc.setRemoteDescription({ type: "offer", sdp: m.sdp })266        for (const c of p.queued.splice(0)) await p.pc.addIceCandidate(c).catch(() => {})267        const answer = await p.pc.createAnswer()268        await p.pc.setLocalDescription(answer)269        await this.send(cur.conv, cur.id, "answer", { to: from.deviceId, sdp: answer.sdp })270        break271      }272      case "answer": {273        if (m.to !== myDev || !m.sdp) return274        const p = cur.peers.get(from.deviceId)275        if (!p) return276        await p.pc.setRemoteDescription({ type: "answer", sdp: m.sdp })277        for (const c of p.queued.splice(0)) await p.pc.addIceCandidate(c).catch(() => {})278        break279      }280      case "ice": {281        if (m.to !== myDev) return282        const p = cur.peers.get(from.deviceId)283        for (const c of (m.cands ?? []) as RTCIceCandidateInit[]) {284          if (p?.pc.remoteDescription) await p.pc.addIceCandidate(c).catch(() => {})285          else p?.queued.push(c)286        }287        break288      }289      case "decline": {290        const conv = this.engine.convs.get(cur.conv)291        if (cur.status === "ringing-out" && conv?.kind === "dm" && from.userId !== me) await this.hangup("declined")292        break293      }294      case "leave": {295        const p = cur.peers.get(from.deviceId)296        if (p) {297          p.pc.close()298          cur.peers.delete(from.deviceId)299        }300        if (cur.status === "active" && cur.peers.size === 0) await this.hangup()301        break302      }303    }304    this.emit()305  }306307  // ---- in-call controls308  toggleMute() {309    const cur = this.current310    if (!cur?.local) return311    cur.muted = !cur.muted312    cur.local.getAudioTracks().forEach((t) => (t.enabled = !cur.muted))313    this.emit()314  }315  toggleCamera() {316    const cur = this.current317    if (!cur?.local) return318    cur.cameraOff = !cur.cameraOff319    cur.local.getVideoTracks().forEach((t) => (t.enabled = !cur.cameraOff))320    this.emit()321  }322  private async replaceVideo(track: MediaStreamTrack) {323    const cur = this.current324    if (!cur?.local) return325    for (const p of cur.peers.values()) {326      const sender = p.pc.getSenders().find((s) => s.track?.kind === "video")327      if (sender) await sender.replaceTrack(track)328      else {329        p.pc.addTrack(track, cur.local)330        await this.negotiate(cur, p)331      }332    }333    for (const t of cur.local.getVideoTracks()) cur.local.removeTrack(t)334    cur.local.addTrack(track)335  }336  /** Front/back camera on phones. */337  async switchCamera() {338    const cur = this.current339    if (!cur?.local || cur.sharing) return340    const facing = cur.facing === "user" ? "environment" : "user"341    try {342      const s = await navigator.mediaDevices.getUserMedia({ video: { facingMode: { exact: facing } } })343      const track = s.getVideoTracks()[0]344      this.cameraTrack?.stop()345      this.cameraTrack = track346      await this.replaceVideo(track)347      cur.facing = facing348      this.emit()349    } catch {350      /* single camera */351    }352  }353  /** Screen sharing where the browser offers it (not available in iOS Safari). */354  async toggleScreen() {355    const cur = this.current356    if (!cur?.local) return357    if (cur.sharing) {358      this.screenTrack?.stop()359      this.screenTrack = null360      cur.sharing = false361      if (this.cameraTrack) await this.replaceVideo(this.cameraTrack)362      return this.emit()363    }364    if (!navigator.mediaDevices.getDisplayMedia) {365      this.error = "Screen sharing is not available in this browser."366      return this.emit()367    }368    try {369      const s = await navigator.mediaDevices.getDisplayMedia({ video: true })370      this.screenTrack = s.getVideoTracks()[0]371      this.screenTrack.onended = () => cur.sharing && void this.toggleScreen()372      await this.replaceVideo(this.screenTrack)373      cur.sharing = true374      cur.video = true375      this.emit()376    } catch {377      /* cancelled */378    }379  }380}381
Files main
Menu

Theme