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 cr.define('hotword', function() { | 5 cr.define('hotword', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * Class used to manage the state of the NaCl recognizer plugin. Handles all | 9 * Class used to manage the state of the NaCl recognizer plugin. Handles all |
10 * control of the NaCl plugin, including creation, start, stop, trigger, and | 10 * control of the NaCl plugin, including creation, start, stop, trigger, and |
(...skipping 22 matching lines...) Expand all Loading... |
33 this.expectingMessage_ = null; | 33 this.expectingMessage_ = null; |
34 | 34 |
35 /** | 35 /** |
36 * Whether the plugin will be started as soon as it stops. | 36 * Whether the plugin will be started as soon as it stops. |
37 * @private {boolean} | 37 * @private {boolean} |
38 */ | 38 */ |
39 this.restartOnStop_ = false; | 39 this.restartOnStop_ = false; |
40 | 40 |
41 /** | 41 /** |
42 * NaCl plugin element on extension background page. | 42 * NaCl plugin element on extension background page. |
43 * @private {?Nacl} | 43 * @private {?HTMLEmbedElement} |
44 */ | 44 */ |
45 this.plugin_ = null; | 45 this.plugin_ = null; |
46 | 46 |
47 /** | 47 /** |
48 * URL containing hotword-model data file. | 48 * URL containing hotword-model data file. |
49 * @private {string} | 49 * @private {string} |
50 */ | 50 */ |
51 this.modelUrl_ = ''; | 51 this.modelUrl_ = ''; |
52 | 52 |
53 /** | 53 /** |
(...skipping 25 matching lines...) Expand all Loading... |
79 var Error_ = hotword.constants.Error; | 79 var Error_ = hotword.constants.Error; |
80 | 80 |
81 NaClManager.prototype.__proto__ = cr.EventTarget.prototype; | 81 NaClManager.prototype.__proto__ = cr.EventTarget.prototype; |
82 | 82 |
83 /** | 83 /** |
84 * Called when an error occurs. Dispatches an event. | 84 * Called when an error occurs. Dispatches an event. |
85 * @param {!hotword.constants.Error} error | 85 * @param {!hotword.constants.Error} error |
86 * @private | 86 * @private |
87 */ | 87 */ |
88 NaClManager.prototype.handleError_ = function(error) { | 88 NaClManager.prototype.handleError_ = function(error) { |
89 event = new Event(hotword.constants.Event.ERROR); | 89 var event = new Event(hotword.constants.Event.ERROR); |
90 event.data = error; | 90 event.data = error; |
91 this.dispatchEvent(event); | 91 this.dispatchEvent(event); |
92 }; | 92 }; |
93 | 93 |
94 /** | 94 /** |
95 * @return {boolean} True if the recognizer is in a running state. | 95 * @return {boolean} True if the recognizer is in a running state. |
96 */ | 96 */ |
97 NaClManager.prototype.isRunning = function() { | 97 NaClManager.prototype.isRunning = function() { |
98 return this.recognizerState_ == ManagerState_.RUNNING; | 98 return this.recognizerState_ == ManagerState_.RUNNING; |
99 }; | 99 }; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 langs.push(language.substr(0, hyphen)); // Example: 'en'. | 191 langs.push(language.substr(0, hyphen)); // Example: 'en'. |
192 } | 192 } |
193 } | 193 } |
194 langs.push(''); | 194 langs.push(''); |
195 return langs; | 195 return langs; |
196 }; | 196 }; |
197 | 197 |
198 /** | 198 /** |
199 * Creates a NaCl plugin object and attaches it to the page. | 199 * Creates a NaCl plugin object and attaches it to the page. |
200 * @param {!string} src Location of the plugin. | 200 * @param {!string} src Location of the plugin. |
201 * @return {!Nacl} NaCl plugin DOM object. | 201 * @return {!HTMLEmbedElement} NaCl plugin DOM object. |
202 * @private | 202 * @private |
203 */ | 203 */ |
204 NaClManager.prototype.createPlugin_ = function(src) { | 204 NaClManager.prototype.createPlugin_ = function(src) { |
205 var plugin = document.createElement('embed'); | 205 var plugin = /** @type {HTMLEmbedElement} */(document.createElement('embed')); |
206 plugin.src = src; | 206 plugin.src = src; |
207 plugin.type = 'application/x-nacl'; | 207 plugin.type = 'application/x-nacl'; |
208 document.body.appendChild(plugin); | 208 document.body.appendChild(plugin); |
209 return plugin; | 209 return plugin; |
210 }; | 210 }; |
211 | 211 |
212 /** | 212 /** |
213 * Initializes the NaCl manager. | 213 * Initializes the NaCl manager. |
214 * @param {!string} naclArch Either 'arm', 'x86-32' or 'x86-64'. | 214 * @param {!string} naclArch Either 'arm', 'x86-32' or 'x86-64'. |
215 * @param {!MediaStream} stream A stream containing an audio source track. | 215 * @param {!MediaStream} stream A stream containing an audio source track. |
(...skipping 12 matching lines...) Expand all Loading... |
228 naclArch + '_' + langs[i] + '/'; | 228 naclArch + '_' + langs[i] + '/'; |
229 var dataSrc = folder + hotword.constants.File.RECOGNIZER_CONFIG; | 229 var dataSrc = folder + hotword.constants.File.RECOGNIZER_CONFIG; |
230 var pluginSrc = hotword.constants.SHARED_MODULE_ROOT + '/hotword_' + | 230 var pluginSrc = hotword.constants.SHARED_MODULE_ROOT + '/hotword_' + |
231 langs[i] + '.nmf'; | 231 langs[i] + '.nmf'; |
232 var dataExists = this.fileExists_(dataSrc) && this.fileExists_(pluginSrc); | 232 var dataExists = this.fileExists_(dataSrc) && this.fileExists_(pluginSrc); |
233 if (!dataExists) { | 233 if (!dataExists) { |
234 continue; | 234 continue; |
235 } | 235 } |
236 | 236 |
237 var plugin = this.createPlugin_(pluginSrc); | 237 var plugin = this.createPlugin_(pluginSrc); |
238 this.plugin_ = /** @type {Nacl} */ (plugin); | 238 this.plugin_ = plugin; |
239 if (!this.plugin_ || !this.plugin_.postMessage) { | 239 if (!this.plugin_ || !this.plugin_.postMessage) { |
240 document.body.removeChild(this.plugin_); | 240 document.body.removeChild(this.plugin_); |
241 this.recognizerState_ = ManagerState_.ERROR; | 241 this.recognizerState_ = ManagerState_.ERROR; |
242 return false; | 242 return false; |
243 } | 243 } |
244 this.modelUrl_ = chrome.extension.getURL(dataSrc); | 244 this.modelUrl_ = chrome.extension.getURL(dataSrc); |
245 this.stream_ = stream; | 245 this.stream_ = stream; |
246 this.recognizerState_ = ManagerState_.LOADING; | 246 this.recognizerState_ = ManagerState_.LOADING; |
247 | 247 |
248 plugin.addEventListener('message', | 248 plugin.addEventListener('message', |
(...skipping 17 matching lines...) Expand all Loading... |
266 document.body.removeChild(this.plugin_); | 266 document.body.removeChild(this.plugin_); |
267 this.plugin_ = null; | 267 this.plugin_ = null; |
268 } | 268 } |
269 this.clearTimeout_(); | 269 this.clearTimeout_(); |
270 this.recognizerState_ = ManagerState_.SHUTDOWN; | 270 this.recognizerState_ = ManagerState_.SHUTDOWN; |
271 this.stream_ = null; | 271 this.stream_ = null; |
272 }; | 272 }; |
273 | 273 |
274 /** | 274 /** |
275 * Sends data to the NaCl plugin. | 275 * Sends data to the NaCl plugin. |
276 * @param {!string} data Command to be sent to NaCl plugin. | 276 * @param {!string|!MediaStreamTrack} data Command to be sent to NaCl plugin. |
277 * @private | 277 * @private |
278 */ | 278 */ |
279 NaClManager.prototype.sendDataToPlugin_ = function(data) { | 279 NaClManager.prototype.sendDataToPlugin_ = function(data) { |
280 assert(this.recognizerState_ != ManagerState_.UNINITIALIZED, | 280 assert(this.recognizerState_ != ManagerState_.UNINITIALIZED, |
281 'Recognizer in uninitialized state'); | 281 'Recognizer in uninitialized state'); |
282 this.plugin_.postMessage(data); | 282 this.plugin_.postMessage(data); |
283 }; | 283 }; |
284 | 284 |
285 /** | 285 /** |
286 * Waits, with a timeout, for a message to be received from the plugin. If the | 286 * Waits, with a timeout, for a message to be received from the plugin. If the |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 break; | 429 break; |
430 } | 430 } |
431 } | 431 } |
432 }; | 432 }; |
433 | 433 |
434 return { | 434 return { |
435 NaClManager: NaClManager | 435 NaClManager: NaClManager |
436 }; | 436 }; |
437 | 437 |
438 }); | 438 }); |
OLD | NEW |