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