import { BinaryWriter } from "../binary/writer"; export enum WriteKind { Offer, Answer, IceCandidate, TargetChanged, } export type WriteEvent = { kind: WriteKind.IceCandidate, to: string, candidate: RTCIceCandidate } | { kind: WriteKind.Offer, to: string, offerSdp: string } | { kind: WriteKind.Answer, to: string, answerSdp: string } | { kind: WriteKind.TargetChanged, name: string } export function writeEvent(writer: BinaryWriter, event: WriteEvent) { writer.writeUInt8(event.kind); if (event.kind === WriteKind.TargetChanged) { writer.writeFixedString(0x20, event.name); return; } writer.writeFixedString(36, event.to); switch (event.kind) { case WriteKind.Offer: writer.writePrefixedString(event.offerSdp); return; case WriteKind.Answer: writer.writePrefixedString(event.answerSdp); return; case WriteKind.IceCandidate: writer.writePrefixedString(JSON.stringify(event.candidate.toJSON())); return; default: throw new Error("kind not good"); } }