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

Side by Side Diff: chrome/browser/resources/chromeos/chromevox/cvox2/background/media_automation_handler.js

Issue 2563013003: Support audio ducking and suspension in ChromeVox (Closed)
Patch Set: Fix compile. Created 3 years, 11 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 /** 5 /**
6 * @fileoverview Handles media events automation. 6 * @fileoverview Handles media automation events. Note that to perform any of
7 * the actions below such as ducking, and suspension of media sessions, the
8 * --enable-default-media-session flag must be passed at the command line.
7 */ 9 */
8 10
9 goog.provide('MediaAutomationHandler'); 11 goog.provide('MediaAutomationHandler');
10 12
11 goog.require('BaseAutomationHandler'); 13 goog.require('BaseAutomationHandler');
14 goog.require('cvox.TtsCapturingEventListener');
12 15
13 goog.scope(function() { 16 goog.scope(function() {
14 var AutomationEvent = chrome.automation.AutomationEvent; 17 var AutomationEvent = chrome.automation.AutomationEvent;
15 var AutomationNode = chrome.automation.AutomationNode; 18 var AutomationNode = chrome.automation.AutomationNode;
16 var EventType = chrome.automation.EventType; 19 var EventType = chrome.automation.EventType;
17 var RoleType = chrome.automation.RoleType; 20 var RoleType = chrome.automation.RoleType;
18 21
19 /** 22 /**
20 * @param {!AutomationNode} node The root to observe media changes.
21 * @constructor 23 * @constructor
22 * @extends {BaseAutomationHandler} 24 * @extends {BaseAutomationHandler}
25 * @implements {cvox.TtsCapturingEventListener}
23 */ 26 */
24 MediaAutomationHandler = function(node) { 27 MediaAutomationHandler = function() {
25 BaseAutomationHandler.call(this, node); 28 /** @type {!Set<AutomationNode>} @private */
29 this.mediaRoots_ = new Set();
26 30
27 var e = EventType; 31 /** @type {Date} @private */
28 this.addListener_(e.mediaStartedPlaying, this.onMediaStartedPlaying); 32 this.lastTtsEvent_ = new Date();
29 this.addListener_(e.mediaStoppedPlaying, this.onMediaStoppedPlaying); 33
34 cvox.ChromeVox.tts.addCapturingEventListener(this);
35
36 chrome.automation.getDesktop(function(node) {
37 BaseAutomationHandler.call(this, node);
38
39 var e = EventType;
40 this.addListener_(e.mediaStartedPlaying, this.onMediaStartedPlaying);
41 this.addListener_(e.mediaStoppedPlaying, this.onMediaStoppedPlaying);
42 }.bind(this));
30 }; 43 };
31 44
45 /** @type {number} */
46 MediaAutomationHandler.MIN_WAITTIME_MS = 1000;
47
32 MediaAutomationHandler.prototype = { 48 MediaAutomationHandler.prototype = {
33 __proto__: BaseAutomationHandler.prototype, 49 __proto__: BaseAutomationHandler.prototype,
34 50
51 /** @override */
52 onTtsStart: function() {
53 this.lastTtsEvent_ = new Date();
54 this.update_({start: true});
55 },
56
57 /** @override */
58 onTtsEnd: function() {
59 var now = new Date();
60 setTimeout(function() {
61 var then = this.lastTtsEvent_;
62 if (now < then)
63 return;
64 this.lastTtsEvent_ = now;
65 this.update_({end: true});
66 }.bind(this), MediaAutomationHandler.MIN_WAITTIME_MS);
67 },
68
69 /** @override */
70 onTtsInterrupted: function() {
71 this.onTtsEnd();
72 },
73
35 /** 74 /**
36 * @param {!AutomationEvent} evt 75 * @param {!AutomationEvent} evt
37 */ 76 */
38 onMediaStartedPlaying: function(evt) { 77 onMediaStartedPlaying: function(evt) {
78 this.mediaRoots_.add(evt.target);
79 if (cvox.ChromeVox.tts.isSpeaking())
80 this.update_({start: true});
39 }, 81 },
40 82
41 /** 83 /**
42 * @param {!AutomationEvent} evt 84 * @param {!AutomationEvent} evt
43 */ 85 */
44 onMediaStoppedPlaying: function(evt) { 86 onMediaStoppedPlaying: function(evt) {
87 // Intentionally does nothing (to cover resume).
88 },
89
90 /**
91 * Updates the media state for all observed automation roots.
92 * @param {{start: (boolean|undefined),
93 * end: (boolean|undefined)}} options
94 * @private
95 */
96 update_: function(options) {
97 var it = this.mediaRoots_.values();
98 var item = it.next();
99 var audioStrategy = localStorage['audioStrategy'];
100 while (!item.done) {
101 var root = item.value;
102 if (options.start) {
103 if (audioStrategy == 'audioDuck')
104 root.startDuckingMedia();
105 else if (audioStrategy == 'audioSuspend')
106 root.suspendMedia();
107 } else if (options.end) {
108 if (audioStrategy == 'audioDuck')
109 root.stopDuckingMedia();
110 else if (audioStrategy == 'audioSuspend')
111 root.resumeMedia();
112 }
113 item = it.next();
114 }
45 } 115 }
46 }; 116 };
47 117
48 }); // goog.scope 118 }); // goog.scope
119
120 new MediaAutomationHandler();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698