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.
7 // Global holding our NaclBridge.
8 var whispernetNacl = null;
10 // Encoders and decoders for each client.
11 var whisperEncoders = {};
12 var whisperDecoders = {};
15 * Initialize the whispernet encoder and decoder.
16 * Call this before any other functions.
17 * @param {string} clientId A string identifying the requester.
18 * @param {Object} audioParams Audio parameters for token encoding and decoding.
20 function audioConfig(clientId, audioParams) {
21 if (!whispernetNacl) {
22 chrome.copresencePrivate.sendInitialized(false);
26 console.log('Configuring encoder and decoder for client ' + clientId);
27 whisperEncoders[clientId] =
28 new WhisperEncoder(audioParams.paramData, whispernetNacl, clientId);
29 whisperDecoders[clientId] =
30 new WhisperDecoder(audioParams.paramData, whispernetNacl, clientId);
34 * Sends a request to whispernet to encode a token.
35 * @param {string} clientId A string identifying the requester.
36 * @param {Object} params Encode token parameters object.
38 function encodeTokenRequest(clientId, params) {
39 if (whisperEncoders[clientId]) {
40 whisperEncoders[clientId].encode(params);
42 console.error('encodeTokenRequest: Whisper not initialized for client ' +
48 * Sends a request to whispernet to decode samples.
49 * @param {string} clientId A string identifying the requester.
50 * @param {Object} params Process samples parameters object.
52 function decodeSamplesRequest(clientId, params) {
53 if (whisperDecoders[clientId]) {
54 whisperDecoders[clientId].processSamples(params);
56 console.error('decodeSamplesRequest: Whisper not initialized for client ' +
62 * Initialize our listeners and signal that the extension is loaded.
64 function onWhispernetLoaded() {
65 console.log('init: Nacl ready!');
67 // Setup all the listeners for the private API.
68 chrome.copresencePrivate.onConfigAudio.addListener(audioConfig);
69 chrome.copresencePrivate.onEncodeTokenRequest.addListener(encodeTokenRequest);
70 chrome.copresencePrivate.onDecodeSamplesRequest.addListener(
71 decodeSamplesRequest);
73 // This first initialized is sent to indicate that the library is loaded.
74 // Every other time, it will be sent only when Chrome wants to reinitialize
75 // the encoder and decoder.
76 chrome.copresencePrivate.sendInitialized(true);
80 * Initialize the whispernet Nacl bridge.
82 function initWhispernet() {
83 console.log('init: Starting Nacl bridge.');
84 // TODO(rkc): Figure out how to embed the .nmf and the .pexe into component
85 // resources without having to rename them to .js.
86 whispernetNacl = new NaclBridge('whispernet_proxy.nmf.png',
90 window.addEventListener('DOMContentLoaded', initWhispernet);