OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * Function to convert an array of bytes to a base64 string | 8 * Function to convert an array of bytes to a base64 string |
9 * TODO(rkc): Change this to use a Uint8array instead of a string. | 9 * TODO(rkc): Change this to use a Uint8array instead of a string. |
10 * @param {string} bytes String containing the bytes we want to convert. | 10 * @param {string} bytes String containing the bytes we want to convert. |
(...skipping 26 matching lines...) Expand all Loading... |
37 * the whispernet wrapper. | 37 * the whispernet wrapper. |
38 * @param {string} clientId A string identifying the requester. | 38 * @param {string} clientId A string identifying the requester. |
39 */ | 39 */ |
40 function WhisperEncoder(params, whisperNacl, clientId) { | 40 function WhisperEncoder(params, whisperNacl, clientId) { |
41 this.whisperNacl_ = whisperNacl; | 41 this.whisperNacl_ = whisperNacl; |
42 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); | 42 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); |
43 this.clientId_ = clientId; | 43 this.clientId_ = clientId; |
44 | 44 |
45 var msg = { | 45 var msg = { |
46 type: 'initialize_encoder', | 46 type: 'initialize_encoder', |
| 47 client_id: clientId, |
47 params: params | 48 params: params |
48 }; | 49 }; |
49 | 50 |
50 this.whisperNacl_.send(msg); | 51 this.whisperNacl_.send(msg); |
51 } | 52 } |
52 | 53 |
53 /** | 54 /** |
54 * Method to encode a token. | 55 * Method to encode a token. |
55 * @param {Object} params Encode token parameters object. | 56 * @param {Object} params Encode token parameters object. |
56 */ | 57 */ |
(...skipping 20 matching lines...) Expand all Loading... |
77 this.whisperNacl_.send(msg); | 78 this.whisperNacl_.send(msg); |
78 }; | 79 }; |
79 | 80 |
80 /** | 81 /** |
81 * Method to handle messages from the whispernet NaCl wrapper. | 82 * Method to handle messages from the whispernet NaCl wrapper. |
82 * @param {Event} e Event from the whispernet wrapper. | 83 * @param {Event} e Event from the whispernet wrapper. |
83 * @private | 84 * @private |
84 */ | 85 */ |
85 WhisperEncoder.prototype.onNaclMessage_ = function(e) { | 86 WhisperEncoder.prototype.onNaclMessage_ = function(e) { |
86 var msg = e.data; | 87 var msg = e.data; |
87 if (msg.type == 'encode_token_response') { | 88 if (msg.type == 'encode_token_response' && msg.client_id == this.clientId_) { |
88 chrome.copresencePrivate.sendSamples(this.clientId_, | 89 chrome.copresencePrivate.sendSamples(this.clientId_, |
89 { token: bytesToBase64(msg.token), audible: msg.audible }, msg.samples); | 90 { token: bytesToBase64(msg.token), audible: msg.audible }, msg.samples); |
90 } | 91 } |
91 }; | 92 }; |
92 | 93 |
93 /** | 94 /** |
94 * Creates a whispernet decoder. | 95 * Creates a whispernet decoder. |
95 * @constructor | 96 * @constructor |
96 * @param {Object} params Audio parameters for the whispernet decoder. | 97 * @param {Object} params Audio parameters for the whispernet decoder. |
97 * @param {Object} whisperNacl The NaclBridge object, used to communicate with | 98 * @param {Object} whisperNacl The NaclBridge object, used to communicate with |
98 * the whispernet wrapper. | 99 * the whispernet wrapper. |
99 * @param {string} clientId A string identifying the requester. | 100 * @param {string} clientId A string identifying the requester. |
100 */ | 101 */ |
101 function WhisperDecoder(params, whisperNacl, clientId) { | 102 function WhisperDecoder(params, whisperNacl, clientId) { |
102 this.whisperNacl_ = whisperNacl; | 103 this.whisperNacl_ = whisperNacl; |
103 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); | 104 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); |
104 this.clientId_ = clientId; | 105 this.clientId_ = clientId; |
105 | 106 |
106 var msg = { | 107 var msg = { |
107 type: 'initialize_decoder', | 108 type: 'initialize_decoder', |
| 109 client_id: clientId, |
108 params: params | 110 params: params |
109 }; | 111 }; |
110 this.whisperNacl_.send(msg); | 112 this.whisperNacl_.send(msg); |
111 } | 113 } |
112 | 114 |
113 /** | 115 /** |
114 * Method to request the decoder to wipe its internal buffer. | |
115 */ | |
116 WhisperDecoder.prototype.wipeDecoder = function() { | |
117 var msg = { | |
118 type: 'wipe_decode_buffer' | |
119 }; | |
120 this.whisperNacl_.send(msg); | |
121 }; | |
122 | |
123 /** | |
124 * Method to request the decoder to process samples. | 116 * Method to request the decoder to process samples. |
125 * @param {Object} params Process samples parameters object. | 117 * @param {Object} params Process samples parameters object. |
126 */ | 118 */ |
127 WhisperDecoder.prototype.processSamples = function(params) { | 119 WhisperDecoder.prototype.processSamples = function(params) { |
128 var msg = { | 120 var msg = { |
129 type: 'decode_tokens', | 121 type: 'decode_tokens', |
130 client_id: this.clientId_, | 122 client_id: this.clientId_, |
131 data: params.samples, | 123 data: params.samples, |
132 | 124 |
133 decode_audible: params.decodeAudible, | 125 decode_audible: params.decodeAudible, |
(...skipping 10 matching lines...) Expand all Loading... |
144 this.whisperNacl_.send(msg); | 136 this.whisperNacl_.send(msg); |
145 }; | 137 }; |
146 | 138 |
147 /** | 139 /** |
148 * Method to handle messages from the whispernet NaCl wrapper. | 140 * Method to handle messages from the whispernet NaCl wrapper. |
149 * @param {Event} e Event from the whispernet wrapper. | 141 * @param {Event} e Event from the whispernet wrapper. |
150 * @private | 142 * @private |
151 */ | 143 */ |
152 WhisperDecoder.prototype.onNaclMessage_ = function(e) { | 144 WhisperDecoder.prototype.onNaclMessage_ = function(e) { |
153 var msg = e.data; | 145 var msg = e.data; |
154 if (msg.type == 'decode_tokens_response') { | 146 if (msg.type == 'decode_tokens_response' && msg.client_id == this.clientId_) { |
155 this.handleCandidates_(msg.tokens, msg.audible); | 147 this.handleCandidates_(msg.tokens, msg.audible); |
156 } | 148 } |
157 }; | 149 }; |
158 | 150 |
159 /** | 151 /** |
160 * Method to receive tokens from the decoder and process and forward them to the | 152 * Method to receive tokens from the decoder and process and forward them to the |
161 * token callback registered with us. | 153 * token callback registered with us. |
162 * @param {!Array.string} candidates Array of token candidates. | 154 * @param {!Array.string} candidates Array of token candidates. |
163 * @param {boolean} audible Whether the received candidates are from the audible | 155 * @param {boolean} audible Whether the received candidates are from the audible |
164 * decoder or not. | 156 * decoder or not. |
165 * @private | 157 * @private |
166 */ | 158 */ |
167 WhisperDecoder.prototype.handleCandidates_ = function(candidates, audible) { | 159 WhisperDecoder.prototype.handleCandidates_ = function(candidates, audible) { |
168 if (!candidates || candidates.length == 0) | 160 if (!candidates || candidates.length == 0) |
169 return; | 161 return; |
170 | 162 |
171 var returnCandidates = []; | 163 var returnCandidates = []; |
172 for (var i = 0; i < candidates.length; ++i) { | 164 for (var i = 0; i < candidates.length; ++i) { |
173 returnCandidates[i] = { token: bytesToBase64(candidates[i]), | 165 returnCandidates[i] = { token: bytesToBase64(candidates[i]), |
174 audible: audible }; | 166 audible: audible }; |
175 } | 167 } |
176 chrome.copresencePrivate.sendFound(this.clientId_, returnCandidates); | 168 chrome.copresencePrivate.sendFound(this.clientId_, returnCandidates); |
177 }; | 169 }; |
OLD | NEW |