index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import url from 'url'
  2. import mumbleConnect from 'mumble-client-websocket'
  3. import CodecsBrowser from 'mumble-client-codecs-browser'
  4. import BufferQueueNode from 'web-audio-buffer-queue'
  5. import MicrophoneStream from 'microphone-stream'
  6. import audioContext from 'audio-context'
  7. import chunker from 'stream-chunker'
  8. import Resampler from 'libsamplerate.js'
  9. import getUserMedia from 'getusermedia'
  10. import ko from 'knockout'
  11. import _dompurify from 'dompurify'
  12. const dompurify = _dompurify(window)
  13. function sanitize (html) {
  14. return dompurify.sanitize(html, {
  15. ALLOWED_TAGS: ['br', 'b', 'i', 'u', 'a', 'span', 'p']
  16. })
  17. }
  18. // GUI
  19. function ConnectDialog () {
  20. var self = this
  21. self.address = ko.observable('')
  22. self.port = ko.observable('443')
  23. self.token = ko.observable('')
  24. self.username = ko.observable('')
  25. self.visible = ko.observable(true)
  26. self.show = self.visible.bind(self.visible, true)
  27. self.hide = self.visible.bind(self.visible, false)
  28. self.connect = function () {
  29. self.hide()
  30. ui.connect(self.username(), self.address(), self.port(), self.token())
  31. }
  32. }
  33. function ConnectionInfo () {
  34. var self = this
  35. self.visible = ko.observable(false)
  36. self.show = function () {
  37. self.visible(true)
  38. }
  39. }
  40. function CommentDialog () {
  41. var self = this
  42. self.visible = ko.observable(false)
  43. self.show = function () {
  44. self.visible(true)
  45. }
  46. }
  47. function SettingsDialog () {
  48. var self = this
  49. self.visible = ko.observable(false)
  50. self.show = function () {
  51. self.visible(true)
  52. }
  53. }
  54. class GlobalBindings {
  55. constructor () {
  56. this.client = null
  57. this.connectDialog = new ConnectDialog()
  58. this.connectionInfo = new ConnectionInfo()
  59. this.commentDialog = new CommentDialog()
  60. this.settingsDialog = new SettingsDialog()
  61. this.log = ko.observableArray()
  62. this.thisUser = ko.observable()
  63. this.root = ko.observable()
  64. this.messageBox = ko.observable('')
  65. this.selected = ko.observable()
  66. this.select = element => {
  67. this.selected(element)
  68. }
  69. this.getTimeString = () => {
  70. return '[' + new Date().toLocaleTimeString('en-US') + ']'
  71. }
  72. this.connect = (username, host, port, token) => {
  73. this.resetClient()
  74. log('Connecting to server ', host)
  75. // TODO: token
  76. mumbleConnect(`wss://${host}:${port}`, {
  77. username: username,
  78. codecs: CodecsBrowser
  79. }).done(client => {
  80. log('Connected!')
  81. this.client = client
  82. // Prepare for connection errors
  83. client.on('error', function (err) {
  84. log('Connection error:', err)
  85. this.resetClient()
  86. })
  87. // Register all channels, recursively
  88. const registerChannel = channel => {
  89. this._newChannel(channel)
  90. channel.children.forEach(registerChannel)
  91. }
  92. registerChannel(client.root)
  93. // Register all users
  94. client.users.forEach(user => this._newUser(user))
  95. // Register future channels
  96. client.on('newChannel', channel => this._newChannel(channel))
  97. // Register future users
  98. client.on('newUser', user => this._newUser(user))
  99. // Handle messages
  100. client.on('message', (sender, message, users, channels, trees) => {
  101. ui.log.push({
  102. type: 'chat-message',
  103. user: sender.__ui,
  104. channel: channels.length > 0,
  105. message: sanitize(message)
  106. })
  107. })
  108. // Set own user and root channel
  109. this.thisUser(client.self.__ui)
  110. this.root(client.root.__ui)
  111. // Upate linked channels
  112. this._updateLinks()
  113. // Log welcome message
  114. if (client.welcomeMessage) {
  115. this.log.push({
  116. type: 'welcome-message',
  117. message: sanitize(client.welcomeMessage)
  118. })
  119. }
  120. }, err => {
  121. log('Connection error:', err)
  122. })
  123. }
  124. this._newUser = user => {
  125. const simpleProperties = {
  126. uniqueId: 'uid',
  127. username: 'name',
  128. mute: 'mute',
  129. deaf: 'deaf',
  130. suppress: 'suppress',
  131. selfMute: 'selfMute',
  132. selfDeaf: 'selfDeaf',
  133. comment: 'comment'
  134. }
  135. var ui = user.__ui = {
  136. model: user,
  137. talking: ko.observable('off'),
  138. channel: ko.observable()
  139. }
  140. Object.entries(simpleProperties).forEach(key => {
  141. ui[key[1]] = ko.observable(user[key[0]])
  142. })
  143. ui.state = ko.pureComputed(userToState, ui)
  144. if (user.channel) {
  145. ui.channel(user.channel.__ui)
  146. ui.channel().users.push(ui)
  147. ui.channel().users.sort(compareUsers)
  148. }
  149. user.on('update', (actor, properties) => {
  150. Object.entries(simpleProperties).forEach(key => {
  151. if (properties[key[0]] !== undefined) {
  152. ui[key[1]](properties[key[0]])
  153. }
  154. })
  155. if (properties.channel !== undefined) {
  156. if (ui.channel()) {
  157. ui.channel().users.remove(ui)
  158. }
  159. ui.channel(properties.channel.__ui)
  160. ui.channel().users.push(ui)
  161. ui.channel().users.sort(compareUsers)
  162. this._updateLinks()
  163. }
  164. }).on('remove', () => {
  165. if (ui.channel()) {
  166. ui.channel().users.remove(ui)
  167. }
  168. }).on('voice', stream => {
  169. console.log(`User ${user.username} started takling`)
  170. var userNode = new BufferQueueNode({
  171. audioContext: audioContext
  172. })
  173. userNode.connect(audioContext.destination)
  174. var resampler = new Resampler({
  175. unsafe: true,
  176. type: Resampler.Type.ZERO_ORDER_HOLD,
  177. ratio: audioContext.sampleRate / 48000
  178. })
  179. resampler.pipe(userNode)
  180. stream.on('data', data => {
  181. if (data.target === 'normal') {
  182. ui.talking('on')
  183. } else if (data.target === 'shout') {
  184. ui.talking('shout')
  185. } else if (data.target === 'whisper') {
  186. ui.talking('whisper')
  187. }
  188. resampler.write(Buffer.from(data.pcm.buffer))
  189. }).on('end', () => {
  190. console.log(`User ${user.username} stopped takling`)
  191. ui.talking('off')
  192. resampler.end()
  193. })
  194. })
  195. }
  196. this._newChannel = channel => {
  197. const simpleProperties = {
  198. position: 'position',
  199. name: 'name',
  200. description: 'description'
  201. }
  202. var ui = channel.__ui = {
  203. model: channel,
  204. expanded: ko.observable(true),
  205. parent: ko.observable(),
  206. channels: ko.observableArray(),
  207. users: ko.observableArray(),
  208. linked: ko.observable(false)
  209. }
  210. Object.entries(simpleProperties).forEach(key => {
  211. ui[key[1]] = ko.observable(channel[key[0]])
  212. })
  213. if (channel.parent) {
  214. ui.parent(channel.parent.__ui)
  215. ui.parent().channels.push(ui)
  216. ui.parent().channels.sort(compareChannels)
  217. }
  218. this._updateLinks()
  219. channel.on('update', properties => {
  220. Object.entries(simpleProperties).forEach(key => {
  221. if (properties[key[0]] !== undefined) {
  222. ui[key[1]](properties[key[0]])
  223. }
  224. })
  225. if (properties.parent !== undefined) {
  226. if (ui.parent()) {
  227. ui.parent().channel.remove(ui)
  228. }
  229. ui.parent(properties.parent.__ui)
  230. ui.parent().channels.push(ui)
  231. ui.parent().channels.sort(compareChannels)
  232. }
  233. if (properties.links !== undefined) {
  234. this._updateLinks()
  235. }
  236. }).on('remove', () => {
  237. if (ui.parent()) {
  238. ui.parent().channels.remove(ui)
  239. }
  240. this._updateLinks()
  241. })
  242. }
  243. this.resetClient = () => {
  244. if (this.client) {
  245. this.client.disconnect()
  246. }
  247. this.client = null
  248. this.thisUser(null).root(null).selected(null)
  249. }
  250. this.connected = () => this.thisUser() != null
  251. this.messageBoxHint = ko.pureComputed(() => {
  252. if (!this.thisUser()) {
  253. return '' // Not yet connected
  254. }
  255. var target = this.selected()
  256. if (!target) {
  257. target = this.thisUser()
  258. }
  259. if (target === this.thisUser()) {
  260. target = target.channel()
  261. }
  262. if (target.users) { // Channel
  263. return "Type message to channel '" + target.name() + "' here"
  264. } else { // User
  265. return "Type message to user '" + target.name() + "' here"
  266. }
  267. })
  268. this.submitMessageBox = () => {
  269. this.sendMessage(this.selected(), this.messageBox())
  270. this.messageBox('')
  271. }
  272. this.sendMessage = (target, message) => {
  273. if (this.connected()) {
  274. // If no target is selected, choose our own user
  275. if (!target) {
  276. target = this.thisUser()
  277. }
  278. // If target is our own user, send to our channel
  279. if (target === this.thisUser()) {
  280. target = target.channel()
  281. }
  282. // Send message
  283. target.model.sendMessage(message)
  284. if (target.users) { // Channel
  285. this.log.push({
  286. type: 'chat-message-self',
  287. message: sanitize(message),
  288. channel: target
  289. })
  290. } else { // User
  291. this.log.push({
  292. type: 'chat-message-self',
  293. message: sanitize(message),
  294. user: target
  295. })
  296. }
  297. }
  298. }
  299. this.requestMove = (user, channel) => {
  300. if (this.connected()) {
  301. user.model.setChannel(channel.model)
  302. }
  303. }
  304. this.requestMute = user => {
  305. if (this.connected()) {
  306. if (user === this.thisUser) {
  307. this.client.setSelfMute(true)
  308. } else {
  309. user.model.setMute(true)
  310. }
  311. }
  312. }
  313. this.requestDeaf = user => {
  314. if (this.connected()) {
  315. if (user === this.thisUser) {
  316. this.client.setSelfDeaf(true)
  317. } else {
  318. user.model.setDeaf(true)
  319. }
  320. }
  321. }
  322. this.requestUnmute = user => {
  323. if (this.connected()) {
  324. if (user === this.thisUser) {
  325. this.client.setSelfMute(false)
  326. } else {
  327. user.model.setMute(false)
  328. }
  329. }
  330. }
  331. this.requestUndeaf = user => {
  332. if (this.connected()) {
  333. if (user === this.thisUser) {
  334. this.client.setSelfDeaf(false)
  335. } else {
  336. user.model.setDeaf(false)
  337. }
  338. }
  339. }
  340. this._updateLinks = () => {
  341. if (!this.thisUser()) {
  342. return
  343. }
  344. var allChannels = getAllChannels(this.root(), [])
  345. var ownChannel = this.thisUser().channel().model
  346. var allLinked = findLinks(ownChannel, [])
  347. allChannels.forEach(channel => {
  348. channel.linked(allLinked.indexOf(channel.model) !== -1)
  349. })
  350. function findLinks (channel, knownLinks) {
  351. knownLinks.push(channel)
  352. channel.links.forEach(next => {
  353. if (next && knownLinks.indexOf(next) === -1) {
  354. findLinks(next, knownLinks)
  355. }
  356. })
  357. allChannels.map(c => c.model).forEach(next => {
  358. if (next && knownLinks.indexOf(next) === -1 && next.links.indexOf(channel) !== -1) {
  359. findLinks(next, knownLinks)
  360. }
  361. })
  362. return knownLinks
  363. }
  364. function getAllChannels (channel, channels) {
  365. channels.push(channel)
  366. channel.channels().forEach(next => getAllChannels(next, channels))
  367. return channels
  368. }
  369. }
  370. this.openSourceCode = () => {
  371. var homepage = require('../package.json').homepage
  372. window.open(homepage, '_blank').focus()
  373. }
  374. }
  375. }
  376. var ui = new GlobalBindings()
  377. // Used only for debugging
  378. window.mumbleUi = ui
  379. window.onload = function () {
  380. var queryParams = url.parse(document.location.href, true).query
  381. if (queryParams.address) {
  382. ui.connectDialog.address(queryParams.address)
  383. }
  384. if (queryParams.port) {
  385. ui.connectDialog.port(queryParams.port)
  386. }
  387. if (queryParams.token) {
  388. ui.connectDialog.token(queryParams.token)
  389. }
  390. if (queryParams.username) {
  391. ui.connectDialog.username(queryParams.username)
  392. }
  393. ko.applyBindings(ui)
  394. }
  395. function log () {
  396. console.log.apply(console, arguments)
  397. var args = []
  398. for (var i = 0; i < arguments.length; i++) {
  399. args.push(arguments[i])
  400. }
  401. ui.log.push({
  402. type: 'generic',
  403. value: args.join(' ')
  404. })
  405. }
  406. function compareChannels (c1, c2) {
  407. if (c1.position() === c2.position()) {
  408. return c1.name() === c2.name() ? 0 : c1.name() < c2.name() ? -1 : 1
  409. }
  410. return c1.position() - c2.position()
  411. }
  412. function compareUsers (u1, u2) {
  413. return u1.name() === u2.name() ? 0 : u1.name() < u2.name() ? -1 : 1
  414. }
  415. function userToState () {
  416. var flags = []
  417. // TODO: Friend
  418. if (this.uid()) {
  419. flags.push('Authenticated')
  420. }
  421. // TODO: Priority Speaker, Recording
  422. if (this.mute()) {
  423. flags.push('Muted (server)')
  424. }
  425. if (this.deaf()) {
  426. flags.push('Deafened (server)')
  427. }
  428. // TODO: Local Ignore (Text messages), Local Mute
  429. if (this.selfMute()) {
  430. flags.push('Muted (self)')
  431. }
  432. if (this.selfDeaf()) {
  433. flags.push('Deafened (self)')
  434. }
  435. return flags.join(', ')
  436. }
  437. // Audio input
  438. var resampler = new Resampler({
  439. unsafe: true,
  440. type: Resampler.Type.SINC_FASTEST,
  441. ratio: 48000 / audioContext.sampleRate
  442. })
  443. var voiceStream
  444. resampler.pipe(chunker(4 * 480)).on('data', function (data) {
  445. if (!voiceStream && ui.client) {
  446. voiceStream = ui.client.createVoiceStream()
  447. }
  448. if (voiceStream) {
  449. voiceStream.write(new Float32Array(data.buffer, data.byteOffset, data.byteLength / 4))
  450. }
  451. })
  452. getUserMedia({ audio: true }, function (err, userMedia) {
  453. if (err) {
  454. log('Cannot initialize user media. Microphone will not work:', err)
  455. } else {
  456. var micStream = new MicrophoneStream(userMedia, { objectMode: true })
  457. micStream.on('data', function (data) {
  458. resampler.write(Buffer.from(data.getChannelData(0).buffer))
  459. })
  460. }
  461. })