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. 9var whispernetNacl = null; 10 11// copy of an encoder or a decoder at a time. 12var whisperEncoder = null; 13var 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 */ 20function 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 * @param {boolean} audible Whether we should use encode audible samples. 42 */ 43function encodeTokenRequest(token, audible) { 44 if (whisperEncoder) { 45 whisperEncoder.encode(atob(token), audible, true); 46 } else { 47 console.error('encodeTokenRequest: Whisper not initialized!'); 48 } 49} 50 51/** 52 * Sends a request to whispernet to decode samples. 53 * @param {ArrayBuffer} samples Array of samples to decode. 54 */ 55function decodeSamplesRequest(samples) { 56 if (whisperDecoder) { 57 whisperDecoder.processSamples(samples); 58 } else { 59 console.error('decodeSamplesRequest: Whisper not initialized!'); 60 } 61} 62 63/** 64 * Sends a request to whispernet to detect broadcast. 65 */ 66function detectBroadcastRequest() { 67 if (whisperDecoder) { 68 whisperDecoder.detectBroadcast(); 69 } else { 70 console.error('detectBroadcastRequest: Whisper not initialized!'); 71 } 72} 73 74/** 75 * Initialize our listerners and signal that the extension is loaded. 76 */ 77function onWhispernetLoaded() { 78 console.log('init: Nacl ready!'); 79 80 // Setup all the listeners for the private API. 81 chrome.copresencePrivate.onInitialize.addListener(initialize); 82 chrome.copresencePrivate.onEncodeTokenRequest.addListener(encodeTokenRequest); 83 chrome.copresencePrivate.onDecodeSamplesRequest.addListener( 84 decodeSamplesRequest); 85 chrome.copresencePrivate.onDetectBroadcastRequest.addListener( 86 detectBroadcastRequest); 87 88 // This first initialized is sent to indicate that the library is loaded. 89 // Every other time, it will be sent only when Chrome wants to reinitialize 90 // the encoder and decoder. 91 chrome.copresencePrivate.sendInitialized(true); 92} 93 94/** 95 * Initialize the whispernet Nacl bridge. 96 */ 97function initWhispernet() { 98 console.log('init: Starting Nacl bridge.'); 99 // TODO(rkc): Figure out how to embed the .nmf and the .pexe into component 100 // resources without having to rename them to .js. 101 whispernetNacl = new NaclBridge('whispernet_proxy.nmf.png', 102 onWhispernetLoaded); 103} 104 105window.addEventListener('DOMContentLoaded', initWhispernet); 106