Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

452 linhas
14KB

  1. const isLandscape = screen.width > screen.height
  2. const State = {
  3. username: null,
  4. websocket: null,
  5. online: [],
  6. posts: [],
  7. rpcs: {},
  8. streams: {},
  9. options: {},
  10. }
  11. const markedOptions = {
  12. breaks: true,
  13. }
  14. marked.setOptions(markedOptions)
  15. /*
  16. *
  17. * SIGNALING
  18. *
  19. */
  20. const wire = (message) => State.websocket.send(JSON.stringify(message))
  21. const signal = (message) => dispatchEvent(new CustomEvent(message.kind, {detail: message}))
  22. const signalPeerStop = (username) => signal({kind: 'peerInfo', value: {type: 'stop'}, source: username})
  23. const listen = (kind, handler) => addEventListener(kind, handler)
  24. listen('login', ({detail}) => State.username = detail.value)
  25. listen('state', ({detail}) => Object.assign(State, detail))
  26. listen('post', ({detail}) => State.posts.push(detail))
  27. listen('peerInfo', (e) => onPeerInfo(e))
  28. const doNotLog = new Set(['login', 'state', 'post', 'peerInfo'])
  29. /*
  30. *
  31. * ALERTS
  32. *
  33. */
  34. State.unseen = 0
  35. listen('post', () => {State.unseen += !document.hasFocus(); updateTitle()})
  36. listen('focus', () => {State.unseen = 0; updateTitle()})
  37. const updateTitle = () => {
  38. document.title = `pico.chat` + (State.unseen ? ` (${State.unseen})` : ``)
  39. }
  40. /*
  41. *
  42. * WEBRTC
  43. *
  44. */
  45. const getOrCreateRpc = (username) => {
  46. if(State.username === username) {
  47. return
  48. }
  49. if(!State.rpcs[username]) {
  50. const rpc = new RTCPeerConnection({iceServers: [{urls: 'stun:stun.sipgate.net:3478'}]})
  51. rpc.onicecandidate = ({candidate}) => {
  52. if(candidate) {
  53. wire({kind: 'peerInfo', value: {type: 'candidate', candidate}})
  54. }
  55. }
  56. rpc.ontrack = (e) => {
  57. State.streams[username] = e.streams[0]
  58. m.redraw()
  59. }
  60. rpc.onclose = (e) => {
  61. console.log(username, e)
  62. }
  63. rpc.oniceconnectionstatechange = (e) => {
  64. m.redraw()
  65. }
  66. State.rpcs[username] = rpc
  67. }
  68. return State.rpcs[username]
  69. }
  70. const setSelectedMedia = async () => {
  71. const localStream = State.streams[State.username]
  72. if(!localStream) {
  73. return
  74. }
  75. const oldTracks = localStream.getTracks()
  76. const addTrack = localStream.addTrack.bind(localStream)
  77. const muted = document.querySelector('#mute-check').checked
  78. if(!muted) {
  79. const audio = Media.audioDefaults
  80. await navigator.mediaDevices.getUserMedia({audio})
  81. .then(s => s.getAudioTracks().forEach(addTrack))
  82. .catch(e => console.error(e))
  83. }
  84. const source = document.querySelector('#media-source').value
  85. if(source === 'camera') {
  86. const video = {width: {ideal: 320}, facingMode: 'user', frameRate: 26}
  87. await navigator.mediaDevices.getUserMedia({video})
  88. .then(s => s.getVideoTracks().forEach(addTrack))
  89. .catch(e => console.error(e))
  90. }
  91. if(source === 'screen' && navigator.mediaDevices.getDisplayMedia) {
  92. await navigator.mediaDevices.getDisplayMedia()
  93. .then(s => s.getVideoTracks().forEach(addTrack))
  94. .catch(e => console.error(e))
  95. }
  96. oldTracks.forEach(track => {track.stop(); localStream.removeTrack(track)})
  97. document.querySelectorAll('video').forEach(video => video.srcObject = video.srcObject)
  98. wire({kind: 'peerInfo', value: {type: 'request'}})
  99. }
  100. const onPeerInfo = async ({detail: message}) => {
  101. const localStream = State.streams[State.username]
  102. const rpc = localStream && getOrCreateRpc(message.source)
  103. const resetStreams = () => {
  104. rpc.getSenders().forEach(sender => rpc.removeTrack(sender))
  105. localStream.getTracks().forEach(track => rpc.addTrack(track, localStream))
  106. }
  107. if(rpc && message.value.type === 'request') {
  108. resetStreams()
  109. const localOffer = await rpc.createOffer()
  110. await rpc.setLocalDescription(localOffer)
  111. wire({kind: 'peerInfo', value: localOffer, target: message.source})
  112. }
  113. else if(rpc && message.value.type === 'offer') {
  114. resetStreams()
  115. const remoteOffer = new RTCSessionDescription(message.value)
  116. await rpc.setRemoteDescription(remoteOffer)
  117. const localAnswer = await rpc.createAnswer()
  118. await rpc.setLocalDescription(localAnswer)
  119. wire({kind: 'peerInfo', value: localAnswer, target: message.source})
  120. }
  121. else if(rpc && message.value.type === 'answer') {
  122. const remoteAnswer = new RTCSessionDescription(message.value)
  123. await rpc.setRemoteDescription(remoteAnswer)
  124. }
  125. else if(rpc && message.value.type === 'candidate') {
  126. const candidate = new RTCIceCandidate(message.value.candidate)
  127. rpc.addIceCandidate(candidate)
  128. }
  129. else if(message.value.type === 'stop') {
  130. if(State.streams[message.source]) {
  131. State.streams[message.source].getTracks().map(track => track.stop())
  132. delete State.streams[message.source]
  133. }
  134. if(State.rpcs[message.source]) {
  135. State.rpcs[message.source].close()
  136. delete State.rpcs[message.source]
  137. }
  138. }
  139. else if(rpc) {
  140. console.log('uncaught', message)
  141. }
  142. }
  143. /*
  144. *
  145. * GUI
  146. *
  147. */
  148. const autoFocus = (vnode) => {
  149. vnode.dom.focus()
  150. }
  151. const scrollIntoView = (vnode) => {
  152. vnode.dom.scrollIntoView()
  153. }
  154. const TextBox = {
  155. autoSize: () => {
  156. textbox.rows = textbox.value.split('\n').length
  157. },
  158. sendPost: () => {
  159. if(textbox.value) {
  160. wire({kind: 'post', value: textbox.value})
  161. textbox.value = ''
  162. textbox.focus()
  163. }
  164. },
  165. blockIndent: (text, iStart, iEnd, nLevels) => {
  166. const startLine = text.slice(0, iStart).split('\n').length - 1
  167. const endLine = text.slice(0, iEnd).split('\n').length - 1
  168. const newText = text
  169. .split('\n')
  170. .map((line, i) => {
  171. if(i < startLine || i > endLine || nLevels === 0) {
  172. newLine = line
  173. }
  174. else if(nLevels > 0) {
  175. newLine = line.replace(/^/, ' ')
  176. }
  177. else if(nLevels < 0) {
  178. newLine = line.replace(/^ /, '')
  179. }
  180. if(i === startLine) {
  181. iStart = iStart + newLine.length - line.length
  182. }
  183. iEnd = iEnd + newLine.length - line.length
  184. return newLine
  185. })
  186. .join('\n')
  187. return [newText, Math.max(0, iStart), Math.max(0, iEnd)]
  188. },
  189. hotKey: (e) => {
  190. // if isDesktop, Enter posts, unless Shift+Enter
  191. // use isLandscape as proxy for isDesktop
  192. if(e.key === 'Enter' && isLandscape && !e.shiftKey) {
  193. e.preventDefault()
  194. TextBox.sendPost()
  195. }
  196. // indent and dedent
  197. const modKey = e.ctrlKey || e.metaKey
  198. const {value: text, selectionStart: A, selectionEnd: B} = textbox
  199. if(e.key === 'Tab') {
  200. e.preventDefault()
  201. const regex = new RegExp(`([\\s\\S]{${A}})([\\s\\S]{${B - A}})`)
  202. textbox.value = text.replace(regex, (m, a, b) => a + ' '.repeat(4))
  203. textbox.setSelectionRange(A + 4, A + 4)
  204. }
  205. if(']['.includes(e.key) && modKey) {
  206. e.preventDefault()
  207. const nLevels = {']': 1, '[': -1}[e.key]
  208. const [newText, newA, newB] = TextBox.blockIndent(text, A, B, nLevels)
  209. textbox.value = newText
  210. textbox.setSelectionRange(newA, newB)
  211. }
  212. },
  213. view() {
  214. return m('.actions',
  215. m('textarea#textbox', {
  216. oncreate: (vnode) => {
  217. TextBox.autoSize()
  218. autoFocus(vnode)
  219. },
  220. onkeydown: TextBox.hotKey,
  221. onkeyup: TextBox.autoSize,
  222. }),
  223. m('button', {
  224. onclick: ({target}) => {
  225. TextBox.sendPost()
  226. TextBox.autoSize()
  227. },
  228. },
  229. 'Send'),
  230. )
  231. },
  232. }
  233. const VideoOptions = {
  234. available: ['mirror', 'square', 'full-screen'],
  235. anyFullScreen: () => {
  236. for(const username of State.online) {
  237. if(State.options[username].has('full-screen')) {
  238. return 'full-screen'
  239. }
  240. }
  241. return ''
  242. },
  243. getFor: (username) => {
  244. if(!State.options[username]) {
  245. State.options[username] = new Set(['mirror', 'square'])
  246. }
  247. return State.options[username]
  248. },
  249. getClassListFor: (username) => {
  250. return [...VideoOptions.getFor(username)].join(' ')
  251. },
  252. toggle: (options, string) => () => options.has(string)
  253. ? options.delete(string)
  254. : options.add(string),
  255. view({attrs: {username}}) {
  256. const options = VideoOptions.getFor(username)
  257. return VideoOptions.available.map((string) =>
  258. m('label.video-option',
  259. m('input', {
  260. type: 'checkbox',
  261. checked: options.has(string),
  262. onchange: VideoOptions.toggle(options, string),
  263. }),
  264. string,
  265. )
  266. )
  267. }
  268. }
  269. const Video = {
  270. keepRatio: {observe: () => {}},
  271. appendStream: ({username}) => ({dom}) => {
  272. dom.autoplay = true
  273. dom.muted = (username === State.username)
  274. dom.srcObject = State.streams[username]
  275. },
  276. view({attrs}) {
  277. const classList = VideoOptions.getClassListFor(attrs.username)
  278. const rpc = State.rpcs[attrs.username] || {iceConnectionState: null}
  279. return m('.video-container', {class: classList, oncreate: ({dom}) => Video.keepRatio.observe(dom)},
  280. m('.video-meta',
  281. m('span.video-source', attrs.username),
  282. m('.video-state', rpc.iceConnectionState),
  283. ),
  284. m('video', {playsinline: true, oncreate: Video.appendStream(attrs)}),
  285. )
  286. },
  287. }
  288. if(window.ResizeObserver) {
  289. const doOne = ({target}) => target.style.setProperty('--height', `${target.clientHeight}px`)
  290. const doAll = (entries) => entries.forEach(doOne)
  291. Video.keepRatio = new ResizeObserver(doAll)
  292. }
  293. const Media = {
  294. videoSources: ['camera', 'screen', 'none'],
  295. audioDefaults: {
  296. noiseSuppresion: true,
  297. echoCancellation: true,
  298. },
  299. turnOn: async () => {
  300. State.streams[State.username] = new MediaStream()
  301. await setSelectedMedia()
  302. m.redraw()
  303. },
  304. turnOff: () => {
  305. wire({kind: 'peerInfo', value: {type: 'stop'}})
  306. State.online.forEach(signalPeerStop)
  307. },
  308. view() {
  309. return m('.media',
  310. m('.media-settings',
  311. State.streams[State.username]
  312. ? m('button', {onclick: Media.turnOff}, 'turn off')
  313. : m('button', {onclick: Media.turnOn}, 'turn on')
  314. ,
  315. m('select#media-source', {onchange: setSelectedMedia},
  316. Media.videoSources.map(option => m('option', option))
  317. ),
  318. m('label',
  319. m('input#mute-check', {onchange: setSelectedMedia, type: 'checkbox'}),
  320. 'mute'
  321. ),
  322. ),
  323. m('.videos', {className: VideoOptions.anyFullScreen()},
  324. Object.keys(State.streams).map((username) =>
  325. m(Video, {key: username, username})
  326. ),
  327. ),
  328. )
  329. },
  330. }
  331. const Login = {
  332. sendLogin: (e) => {
  333. e.preventDefault()
  334. const username = e.target.username.value
  335. localStorage.username = username
  336. connect(username)
  337. },
  338. sendLogout: (e) => {
  339. Media.turnOff()
  340. wire({kind: 'logout'})
  341. State.posts = []
  342. },
  343. view() {
  344. const attrs = {
  345. oncreate: autoFocus,
  346. name: 'username',
  347. autocomplete: 'off',
  348. value: localStorage.username,
  349. }
  350. return m('.login',
  351. m('form', {onsubmit: Login.sendLogin},
  352. m('input', attrs),
  353. m('button', 'Login'),
  354. ),
  355. m('.error', State.info),
  356. )
  357. },
  358. }
  359. const Chat = {
  360. prettifyTime: (ts) => {
  361. const dt = new Date(ts)
  362. const H = `0${dt.getHours()}`.slice(-2)
  363. const M = `0${dt.getMinutes()}`.slice(-2)
  364. const S = `0${dt.getSeconds()}`.slice(-2)
  365. return `${H}:${M}:${S}`
  366. },
  367. view() {
  368. return m('.chat',
  369. m('.posts',
  370. State.posts.map(post => m('.post', {oncreate: scrollIntoView},
  371. m('.ts', Chat.prettifyTime(post.ts)),
  372. m('.source', post.source || '~'),
  373. m('.text', m.trust(DOMPurify.sanitize(marked(post.value)))),
  374. )),
  375. ),
  376. m(TextBox),
  377. m('.online',
  378. m('button', {onclick: Login.sendLogout}, 'Logout'),
  379. m('.user-list', State.online.map(username =>
  380. m('details',
  381. m('summary', username),
  382. m(VideoOptions, {username}),
  383. ),
  384. )),
  385. ),
  386. m(Media),
  387. )
  388. },
  389. }
  390. const Main = {
  391. view() {
  392. const connected = State.websocket && State.websocket.readyState === 1
  393. return connected ? m(Chat) : m(Login)
  394. },
  395. }
  396. m.mount(document.body, Main)
  397. /*
  398. *
  399. * WEBSOCKETS
  400. *
  401. */
  402. const connect = (username) => {
  403. const wsUrl = location.href.replace('http', 'ws')
  404. State.websocket = new WebSocket(wsUrl)
  405. State.websocket.onopen = (e) => {
  406. wire({kind: 'login', value: username})
  407. }
  408. State.websocket.onmessage = (e) => {
  409. const message = JSON.parse(e.data)
  410. if(message.online) {
  411. const difference = (l1, l2) => l1.filter(u => !l2.includes(u))
  412. difference(message.online, State.online).forEach(username =>
  413. State.posts.push({ts: message.ts, value: `${username} joined`}))
  414. difference(State.online, message.online).forEach(username =>
  415. State.posts.push({ts: message.ts, value: `${username} left`}))
  416. }
  417. if(!doNotLog.has(message.kind)) {
  418. console.log(message)
  419. }
  420. signal(message)
  421. m.redraw()
  422. }
  423. State.websocket.onclose = (e) => {
  424. State.online.forEach(signalPeerStop)
  425. if(!e.wasClean) {
  426. setTimeout(connect, 1000, username)
  427. }
  428. m.redraw()
  429. }
  430. }
  431. if(localStorage.username) {
  432. connect(localStorage.username)
  433. }
  434. addEventListener('pagehide', Media.turnOff)