Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
|
xiyuan
2014/07/31 19:22:22
nit: 'use strict';
rkc
2014/07/31 23:12:53
Done.
| |
| 5 // Globals holding our encoder and decoder. We will never have more than one | |
| 6 // Global variable that will be used to access this Nacl bridge. | |
| 7 var whispernetNacl = null; | |
| 8 | |
| 9 // copy of an encoder or a decoder at a time. | |
| 10 var whisperEncoder = null; | |
| 11 var whisperDecoder = null; | |
| 12 | |
| 13 /** | |
| 14 * Initialize the whispernet encoder and decoder. | |
| 15 * @param {Object} audioParams Audio parameters used to initialize the encoder | |
| 16 * and decoder. | |
| 17 */ | |
| 18 function initialize(audioParams) { | |
| 19 if (!whispernetNacl) { | |
| 20 chrome.copresencePrivate.sendInitialized(false); | |
| 21 return; | |
| 22 } | |
| 23 | |
| 24 console.log('init: creating encoder!'); | |
| 25 whisperEncoder = new WhisperEncoder(audioParams.play, whispernetNacl); | |
| 26 whisperEncoder.onAudioData(chrome.copresencePrivate.sendSamples); | |
| 27 | |
| 28 console.log('init: creating decoder!'); | |
| 29 whisperDecoder = new WhisperDecoder(audioParams.record, whispernetNacl); | |
| 30 whisperDecoder.onReceive(chrome.copresencePrivate.sendFound); | |
| 31 whisperDecoder.onDetectBroadcast(chrome.copresencePrivate.sendDetect); | |
| 32 | |
| 33 chrome.copresencePrivate.sendInitialized(true); | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * Sends a request to whispernet to encode a token. | |
| 38 * @param {string} token Token to encode. This needs to be a base64 string. | |
| 39 */ | |
| 40 function encodeTokenRequest(token) { | |
| 41 if (whisperEncoder) { | |
| 42 whisperEncoder.encode(atob(token), true); | |
| 43 } else { | |
| 44 console.error('encodeTokenRequest: Whisper not initialized!'); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * Sends a request to whispernet to decode samples. | |
| 50 * @param {ArrayBuffer} samples Array of samples to decode. | |
| 51 */ | |
| 52 function decodeSamplesRequest(samples) { | |
| 53 if (whisperDecoder) { | |
| 54 whisperDecoder.processSamples(samples); | |
| 55 } else { | |
| 56 console.error('decodeSamplesRequest: Whisper not initialized!'); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * Sends a request to whispernet to detect broadcast. | |
| 62 */ | |
| 63 function detectBroadcastRequest() { | |
| 64 if (whisperDecoder) { | |
| 65 whisperDecoder.detectBroadcast(); | |
| 66 } else { | |
| 67 console.error('detectBroadcastRequest: Whisper not initialized!'); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 /** | |
| 72 * Initialize our listerners and signal that the extension is loaded. | |
| 73 */ | |
| 74 function onWhispernetLoaded() { | |
| 75 console.log('init: Nacl ready!'); | |
| 76 | |
| 77 // Setup all the listeners for the private API. | |
| 78 chrome.copresencePrivate.onInitialize.addListener(initialize); | |
| 79 chrome.copresencePrivate.onEncodeTokenRequest.addListener(encodeTokenRequest); | |
| 80 chrome.copresencePrivate.onDecodeSamplesRequest.addListener( | |
| 81 decodeSamplesRequest); | |
| 82 chrome.copresencePrivate.onDetectBroadcastRequest.addListener( | |
| 83 detectBroadcastRequest); | |
| 84 | |
| 85 // This first initialized is sent to indicate that the library is loaded. | |
| 86 // Every other time, it will be sent only when Chrome wants to reinitialize | |
| 87 // the encoder and decoder. | |
| 88 chrome.copresencePrivate.sendInitialized(true); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Initialize the whispernet Nacl bridge. | |
| 93 */ | |
| 94 function initWhispernet() { | |
| 95 console.log('init: Starting Nacl bridge.'); | |
| 96 // TODO(rkc): Figure out how to embed the .nmf and the .pexe into component | |
| 97 // resources without having to rename them to .js. | |
| 98 whispernetNacl = new NaclBridge('whispernet_proxy.nmf.html', | |
| 99 onWhispernetLoaded); | |
| 100 } | |
| 101 | |
| 102 window.addEventListener('DOMContentLoaded', initWhispernet); | |
| OLD | NEW |