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 // Global holding our NaclBridge. | |
8 var whispernetNacl = null; | |
9 | |
10 // Encoders and decoders for each client. | |
11 var whisperEncoders = {}; | |
12 var whisperDecoders = {}; | |
13 | |
14 /** | |
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. | |
19 */ | |
20 function audioConfig(clientId, audioParams) { | |
21 if (!whispernetNacl) { | |
22 chrome.copresencePrivate.sendInitialized(false); | |
23 return; | |
24 } | |
25 | |
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); | |
31 } | |
32 | |
33 /** | |
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. | |
37 */ | |
38 function encodeTokenRequest(clientId, params) { | |
39 if (whisperEncoders[clientId]) { | |
40 whisperEncoders[clientId].encode(params); | |
41 } else { | |
42 console.error('encodeTokenRequest: Whisper not initialized for client ' + | |
43 clientId); | |
44 } | |
45 } | |
46 | |
47 /** | |
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. | |
51 */ | |
52 function decodeSamplesRequest(clientId, params) { | |
53 if (whisperDecoders[clientId]) { | |
54 whisperDecoders[clientId].processSamples(params); | |
55 } else { | |
56 console.error('decodeSamplesRequest: Whisper not initialized for client ' + | |
57 clientId); | |
58 } | |
59 } | |
60 | |
61 /** | |
62 * Initialize our listeners and signal that the extension is loaded. | |
63 */ | |
64 function onWhispernetLoaded() { | |
65 console.log('init: Nacl ready!'); | |
66 | |
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); | |
72 | |
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); | |
77 } | |
78 | |
79 /** | |
80 * Initialize the whispernet Nacl bridge. | |
81 */ | |
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', | |
87 onWhispernetLoaded); | |
88 } | |
89 | |
90 window.addEventListener('DOMContentLoaded', initWhispernet); | |
OLD | NEW |