Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(489)

Side by Side Diff: chrome/browser/resources/whispernet_proxy/js/wrapper.js

Issue 637223011: Redesign the copresence audio handlers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 this.repetitions_ = params.repetitions || 3; 42 this.repetitions_ = params.repetitions || 3;
43 43
44 this.whisperNacl_ = whisperNacl; 44 this.whisperNacl_ = whisperNacl;
45 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); 45 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this));
46 46
47 var msg = { 47 var msg = {
48 type: 'initialize_encoder', 48 type: 'initialize_encoder',
49 sample_rate: params.sampleRate || 48000.0, 49 sample_rate: params.sampleRate || 48000.0,
50 upsampling_factor: params.bitsPerSample || 16, 50 upsampling_factor: params.bitsPerSample || 16,
51 }; 51 };
52 this.whisperNacl_.send(JSON.stringify(msg)); 52 this.whisperNacl_.send(msg);
53 } 53 }
54 54
55 /** 55 /**
56 * Method to encode a token. 56 * Method to encode a token.
57 * @param {string} token Token to encode. 57 * @param {string} token Token to encode.
58 * @param {boolean} audible Whether we should use encode audible samples. 58 * @param {boolean} audible Whether we should use encode audible samples.
59 * @param {boolean} raw Whether we should return the encoded samples in raw 59 * @param {boolean} raw Whether we should return the encoded samples in raw
60 * format or as a Wave file. 60 * format or as a Wave file.
61 */ 61 */
62 WhisperEncoder.prototype.encode = function(token, audible, raw) { 62 WhisperEncoder.prototype.encode = function(token, audible, raw) {
63 var msg = { 63 var msg = {
64 type: 'encode_token', 64 type: 'encode_token',
65 // Trying to send the token in binary form to Nacl doesn't work correctly. 65 // Trying to send the token in binary form to Nacl doesn't work correctly.
66 // We end up with the correct string + a bunch of extra characters. This is 66 // We end up with the correct string + a bunch of extra characters. This is
67 // true of returning a binary string too; hence we communicate back and 67 // true of returning a binary string too; hence we communicate back and
68 // forth by converting the bytes into an array of integers. 68 // forth by converting the bytes into an array of integers.
69 token: stringToArray(token), 69 token: stringToArray(token),
70 repetitions: this.repetitions_, 70 repetitions: this.repetitions_,
71 use_dtmf: audible, 71 use_dtmf: audible,
72 return_raw_samples: raw 72 return_raw_samples: raw
73 }; 73 };
74 this.whisperNacl_.send(JSON.stringify(msg)); 74 this.whisperNacl_.send(msg);
75 }; 75 };
76 76
77 /** 77 /**
78 * Method to set the callback for encoded audio data received from the encoder 78 * Method to set the callback for encoded audio data received from the encoder
79 * when we finish encoding a token. 79 * when we finish encoding a token.
80 * @param {function(string, ArrayBuffer)} callback Callback which will receive 80 * @param {function(string, ArrayBuffer)} callback Callback which will receive
81 * the audio samples. 81 * the audio samples.
82 */ 82 */
83 WhisperEncoder.prototype.setAudioDataCallback = function(callback) { 83 WhisperEncoder.prototype.setAudioDataCallback = function(callback) {
84 this.audioDataCallback_ = callback; 84 this.audioDataCallback_ = callback;
(...skipping 27 matching lines...) Expand all
112 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this)); 112 this.whisperNacl_.addListener(this.onNaclMessage_.bind(this));
113 113
114 var msg = { 114 var msg = {
115 type: 'initialize_decoder', 115 type: 'initialize_decoder',
116 channels: params.channels || 1, 116 channels: params.channels || 1,
117 sample_rate: params.sampleRate || 48000.0, 117 sample_rate: params.sampleRate || 48000.0,
118 upsampling_factor: params.bitsPerSample || 16, 118 upsampling_factor: params.bitsPerSample || 16,
119 max_candidates: 1, 119 max_candidates: 1,
120 max_buffer_duration_in_seconds: 3 120 max_buffer_duration_in_seconds: 3
121 }; 121 };
122 this.whisperNacl_.send(JSON.stringify(msg)); 122 this.whisperNacl_.send(msg);
123 } 123 }
124 124
125 /** 125 /**
126 * Method to request the decoder to wipe its internal buffer. 126 * Method to request the decoder to wipe its internal buffer.
127 */ 127 */
128 WhisperDecoder.prototype.wipeDecoder = function() { 128 WhisperDecoder.prototype.wipeDecoder = function() {
129 var msg = { 129 var msg = {
130 type: 'wipe_decode_buffer' 130 type: 'wipe_decode_buffer'
131 }; 131 };
132 this.whisperNacl_.send(JSON.stringify(msg)); 132 this.whisperNacl_.send(msg);
133 }; 133 };
134 134
135 /** 135 /**
136 * Method to request the decoder to detect a broadcast. 136 * Method to request the decoder to detect a broadcast.
137 */ 137 */
138 WhisperDecoder.prototype.detectBroadcast = function() { 138 WhisperDecoder.prototype.detectBroadcast = function() {
139 var msg = { 139 var msg = {
140 type: 'detect_broadcast' 140 type: 'detect_broadcast'
141 }; 141 };
142 this.whisperNacl_.send(JSON.stringify(msg)); 142 this.whisperNacl_.send(msg);
143 }; 143 };
144 144
145 /** 145 /**
146 * Method to request the decoder to process samples. 146 * Method to request the decoder to process samples.
147 * @param {ArrayBuffer} samples Array of samples to process. 147 * @param {ArrayBuffer} samples Array of samples to process.
148 * @param {Object} type Type of decoding to perform.
148 */ 149 */
149 WhisperDecoder.prototype.processSamples = function(samples) { 150 WhisperDecoder.prototype.processSamples = function(samples, type) {
150 // For sample processing, the Nacl module doesn't expect any frills in the 151 var msg = {
151 // message, just send the samples directly. 152 type: 'decode_tokens',
152 this.whisperNacl_.send(samples); 153 decode_audible: type.decodeAudible,
154 decode_inaudible: type.decodeInaudible,
155 data: samples,
156 };
157
158 this.whisperNacl_.send(msg);
153 }; 159 };
154 160
155 /** 161 /**
156 * Method to set the callback for decoded tokens received from the decoder. 162 * Method to set the callback for decoded tokens received from the decoder.
157 * @param {function(!Array.string)} callback Callback to receive the list of 163 * @param {function(!Array.string)} callback Callback to receive the list of
158 * decoded tokens. 164 * decoded tokens.
159 */ 165 */
160 WhisperDecoder.prototype.setReceiveCallback = function(callback) { 166 WhisperDecoder.prototype.setReceiveCallback = function(callback) {
161 this.tokenCallback_ = callback; 167 this.tokenCallback_ = callback;
162 }; 168 };
163 169
164 /** 170 /**
165 * Method to set the callback for receiving the detect callback status received 171 * Method to set the callback for receiving the detect callback status received
166 * from the decoder. 172 * from the decoder.
167 * @param {function()} callback Callback to set to receive the detect broadcast 173 * @param {function()} callback Callback to set to receive the detect broadcast
168 * status. 174 * status.
169 */ 175 */
170 WhisperDecoder.prototype.onDetectBroadcast = function(callback) { 176 WhisperDecoder.prototype.onDetectBroadcast = function(callback) {
171 this.detectBroadcastCallback_ = callback; 177 this.detectBroadcastCallback_ = callback;
172 }; 178 };
173 179
174 /** 180 /**
175 * Method to handle messages from the whispernet NaCl wrapper. 181 * Method to handle messages from the whispernet NaCl wrapper.
176 * @param {Event} e Event from the whispernet wrapper. 182 * @param {Event} e Event from the whispernet wrapper.
177 * @private 183 * @private
178 */ 184 */
179 WhisperDecoder.prototype.onNaclMessage_ = function(e) { 185 WhisperDecoder.prototype.onNaclMessage_ = function(e) {
180 var msg = e.data; 186 var msg = e.data;
181 if (msg.type == 'decode_tokens_response') { 187 if (msg.type == 'decode_tokens_response') {
182 this.handleCandidates_(JSON.parse(msg.tokens), msg.audible); 188 this.handleCandidates_(msg.tokens, msg.audible);
183 } else if (msg.type == 'detect_broadcast_response') { 189 } else if (msg.type == 'detect_broadcast_response') {
184 this.detectBroadcastCallback_(msg.detected); 190 this.detectBroadcastCallback_(msg.detected);
185 } 191 }
186 }; 192 };
187 193
188 /** 194 /**
189 * Method to receive tokens from the decoder and process and forward them to the 195 * Method to receive tokens from the decoder and process and forward them to the
190 * token callback registered with us. 196 * token callback registered with us.
191 * @param {!Array.string} candidates Array of token candidates. 197 * @param {!Array.string} candidates Array of token candidates.
192 * @param {boolean} audible Whether the received candidates are from the audible 198 * @param {boolean} audible Whether the received candidates are from the audible
193 * decoder or not. 199 * decoder or not.
194 * @private 200 * @private
195 */ 201 */
196 WhisperDecoder.prototype.handleCandidates_ = function(candidates, audible) { 202 WhisperDecoder.prototype.handleCandidates_ = function(candidates, audible) {
197 if (!this.tokenCallback_ || !candidates || candidates.length == 0) 203 if (!this.tokenCallback_ || !candidates || candidates.length == 0)
198 return; 204 return;
199 205
200 var returnCandidates = []; 206 var returnCandidates = [];
201 for (var i = 0; i < candidates.length; ++i) { 207 for (var i = 0; i < candidates.length; ++i) {
202 returnCandidates[i] = { token: bytesToBase64(candidates[i]), 208 returnCandidates[i] = { token: bytesToBase64(candidates[i]),
203 audible: audible }; 209 audible: audible };
204 } 210 }
205 this.tokenCallback_(returnCandidates); 211 this.tokenCallback_(returnCandidates);
206 }; 212 };
207 213
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698