diff options
author | MetaMuffin <metamuffin@yandex.com> | 2021-08-04 21:00:34 +0200 |
---|---|---|
committer | MetaMuffin <metamuffin@yandex.com> | 2021-08-04 21:00:34 +0200 |
commit | a8d6fa97be777ab08f09252dc4864ba241b17687 (patch) | |
tree | af9955f59049c87100b85669eb90368b607e21d6 /source/client/user.ts | |
parent | 6d4a9c797edf2bbb75cea6afef109cb457c52641 (diff) | |
download | keks-meet-a8d6fa97be777ab08f09252dc4864ba241b17687.tar keks-meet-a8d6fa97be777ab08f09252dc4864ba241b17687.tar.bz2 keks-meet-a8d6fa97be777ab08f09252dc4864ba241b17687.tar.zst |
a
Diffstat (limited to 'source/client/user.ts')
-rw-r--r-- | source/client/user.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/source/client/user.ts b/source/client/user.ts new file mode 100644 index 0000000..fa60012 --- /dev/null +++ b/source/client/user.ts @@ -0,0 +1,60 @@ +import { Room } from "./room" + + + +export class User { + el: HTMLElement + el_video: HTMLVideoElement + + name: string + local: boolean + peer: RTCPeerConnection + room: Room + stream: MediaStream + + constructor(room: Room, name: string, local?: 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.peer = new RTCPeerConnection() + 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); + 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) + } + + async get_local_media(): Promise<MediaStream> { + return await navigator.mediaDevices.getUserMedia({ audio: true, video: true }) + } + + add_ice_candidate(candidate: RTCIceCandidateInit) { + this.peer.addIceCandidate(new RTCIceCandidate(candidate)) + } + + + leave() { + this.room.el.removeChild(this.el) + } +}
\ No newline at end of file |