| 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. |
| 11 * @return {string} String containing the base64 representation. | 11 * @return {string} String containing the base64 representation. |
| 12 */ | 12 */ |
| 13 function bytesToBase64(bytes) { | 13 function bytesToBase64(bytes) { |
| 14 var bstr = ''; | 14 var bstr = ''; |
| 15 for (var i = 0; i < bytes.length; ++i) | 15 for (var i = 0; i < bytes.length; ++i) |
| 16 bstr += String.fromCharCode(bytes[i]); | 16 bstr += String.fromCharCode(bytes[i]); |
| 17 return btoa(bstr); | 17 return btoa(bstr).replace('=', ''); |
| 18 } | 18 } |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Function to convert a string to an array of bytes. | 21 * Function to convert a string to an array of bytes. |
| 22 * @param {string} str String to convert. | 22 * @param {string} str String to convert. |
| 23 * @return {Array} Array containing the string. | 23 * @return {Array} Array containing the string. |
| 24 */ | 24 */ |
| 25 function stringToArray(str) { | 25 function stringToArray(str) { |
| 26 var buffer = []; | 26 var buffer = []; |
| 27 for (var i = 0; i < str.length; ++i) | 27 for (var i = 0; i < str.length; ++i) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 this.whisperNacl_.send(msg); | 49 this.whisperNacl_.send(msg); |
| 50 } | 50 } |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Method to encode a token. | 53 * Method to encode a token. |
| 54 * @param {Object} params Encode token parameters object. | 54 * @param {Object} params Encode token parameters object. |
| 55 */ | 55 */ |
| 56 WhisperEncoder.prototype.encode = function(params) { | 56 WhisperEncoder.prototype.encode = function(params) { |
| 57 // Pad the token before decoding it. |
| 58 var token = params.token.token; |
| 59 while (token.length % 4 > 0) |
| 60 token += '='; |
| 61 |
| 57 var msg = { | 62 var msg = { |
| 58 type: 'encode_token', | 63 type: 'encode_token', |
| 59 // Trying to send the token in binary form to Nacl doesn't work correctly. | 64 // Trying to send the token in binary form to Nacl doesn't work correctly. |
| 60 // We end up with the correct string + a bunch of extra characters. This is | 65 // We end up with the correct string + a bunch of extra characters. This is |
| 61 // true of returning a binary string too; hence we communicate back and | 66 // true of returning a binary string too; hence we communicate back and |
| 62 // forth by converting the bytes into an array of integers. | 67 // forth by converting the bytes into an array of integers. |
| 63 token: stringToArray(atob(params.token.token)), | 68 token: stringToArray(atob(token)), |
| 64 repetitions: params.repetitions, | 69 repetitions: params.repetitions, |
| 65 use_dtmf: params.token.audible | 70 use_dtmf: params.token.audible |
| 66 }; | 71 }; |
| 72 |
| 67 this.whisperNacl_.send(msg); | 73 this.whisperNacl_.send(msg); |
| 68 }; | 74 }; |
| 69 | 75 |
| 70 /** | 76 /** |
| 71 * Method to set the callback for encoded audio data received from the encoder | 77 * Method to set the callback for encoded audio data received from the encoder |
| 72 * when we finish encoding a token. | 78 * when we finish encoding a token. |
| 73 * @param {function(string, ArrayBuffer)} callback Callback which will receive | 79 * @param {function(string, ArrayBuffer)} callback Callback which will receive |
| 74 * the audio samples. | 80 * the audio samples. |
| 75 */ | 81 */ |
| 76 WhisperEncoder.prototype.setAudioDataCallback = function(callback) { | 82 WhisperEncoder.prototype.setAudioDataCallback = function(callback) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 return; | 198 return; |
| 193 | 199 |
| 194 var returnCandidates = []; | 200 var returnCandidates = []; |
| 195 for (var i = 0; i < candidates.length; ++i) { | 201 for (var i = 0; i < candidates.length; ++i) { |
| 196 returnCandidates[i] = { token: bytesToBase64(candidates[i]), | 202 returnCandidates[i] = { token: bytesToBase64(candidates[i]), |
| 197 audible: audible }; | 203 audible: audible }; |
| 198 } | 204 } |
| 199 this.tokenCallback_(returnCandidates); | 205 this.tokenCallback_(returnCandidates); |
| 200 }; | 206 }; |
| 201 | 207 |
| OLD | NEW |