diff options
author | MetaMuffin <metamuffin@yandex.com> | 2021-08-05 11:00:46 +0200 |
---|---|---|
committer | MetaMuffin <metamuffin@yandex.com> | 2021-08-05 11:00:46 +0200 |
commit | 4475fb02925c04ba19cb9ec8b99110e4e74c3acb (patch) | |
tree | 85df7f1e46ccf7abdc80303b88d57779c0f40008 /source/client/user.ts | |
parent | a8d6fa97be777ab08f09252dc4864ba241b17687 (diff) | |
download | keks-meet-4475fb02925c04ba19cb9ec8b99110e4e74c3acb.tar keks-meet-4475fb02925c04ba19cb9ec8b99110e4e74c3acb.tar.bz2 keks-meet-4475fb02925c04ba19cb9ec8b99110e4e74c3acb.tar.zst |
updated licence, added all the code
Diffstat (limited to 'source/client/user.ts')
-rw-r--r-- | source/client/user.ts | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/source/client/user.ts b/source/client/user.ts index fa60012..7848c8c 100644 --- a/source/client/user.ts +++ b/source/client/user.ts @@ -1,3 +1,5 @@ +import { local_media } from "." +import { log } from "./logger" import { Room } from "./room" @@ -7,54 +9,72 @@ export class User { el_video: HTMLVideoElement name: string - local: boolean + peer: RTCPeerConnection + room: Room stream: MediaStream - constructor(room: Room, name: string, local?: boolean) { + + constructor(room: Room, name: string, offer: boolean) { this.name = name this.room = room - this.local = !!local this.stream = new MediaStream() this.el = document.createElement("div") this.el_video = document.createElement("video") this.el.append(this.el_video) this.el_video.autoplay = true - this.el_video.muted = this.local this.el_video.setAttribute("playsinline", "1") + this.room.el.appendChild(this.el) this.peer = new RTCPeerConnection() + local_media.getTracks().forEach(t => this.peer.addTrack(t, local_media)) this.peer.onicecandidate = ev => { if (!ev.candidate) return room.websocket_send({ ice_candiate: ev.candidate.toJSON(), receiver: this.name }) - console.log("sent rtc candidate", ev.candidate); } this.peer.ontrack = ev => { - console.log("got remote track", ev.streams); + log("media", "remote track", ev.streams) + if (!ev.streams.length) return console.warn("no remote tracks") ev.streams[0].getTracks().forEach(t => { this.stream.addTrack(t) }) } - - if (this.local) this.get_local_media().then(stream => { - this.stream = stream - this.el_video.srcObject = stream - }) - - this.room.el.appendChild(this.el) + if (offer) this.offer() } - async get_local_media(): Promise<MediaStream> { - return await navigator.mediaDevices.getUserMedia({ audio: true, video: true }) + async offer() { + const offer_description = await this.peer.createOffer() + await this.peer.setLocalDescription(offer_description) + const offer = { type: offer_description.type, sdp: offer_description.sdp } + log("webrtc", "sent offer", offer) + this.room.websocket_send({ receiver: this.name, offer }) + } + async on_offer(offer: RTCSessionDescriptionInit) { + log("webrtc", "got offer", offer) + const offer_description = new RTCSessionDescription(offer) + await this.peer.setRemoteDescription(offer_description) + this.answer(offer) + } + async answer(offer: RTCSessionDescriptionInit) { + const answer_description = await this.peer.createAnswer() + await this.peer.setLocalDescription(answer_description) + const answer = { type: answer_description.type, sdp: answer_description.sdp } + log("webrtc", "sent answer", answer) + this.room.websocket_send({ receiver: this.name, answer }) + } + async on_answer(answer: RTCSessionDescriptionInit) { + log("webrtc", "got answer", answer) + const answer_description = new RTCSessionDescription(answer) + await this.peer.setRemoteDescription(answer_description) } add_ice_candidate(candidate: RTCIceCandidateInit) { this.peer.addIceCandidate(new RTCIceCandidate(candidate)) } - leave() { + this.peer.close() this.room.el.removeChild(this.el) } }
\ No newline at end of file |