OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <include src="cache_entry.js"/> | 5 <include src="cache_entry.js"/> |
6 <include src="disjoint_range_set.js"/> | |
7 <include src="event_list.js"/> | |
6 <include src="item_store.js"/> | 8 <include src="item_store.js"/> |
7 <include src="disjoint_range_set.js"/> | 9 <include src="media_player.js"/> |
10 <include src="metrics.js"/> | |
11 <include src="util.js"/> | |
8 | 12 |
9 cr.define('media', function() { | 13 cr.define('media', function() { |
10 | 14 |
11 // Stores information on open audio streams, referenced by id. | 15 // Stores information on open audio streams, referenced by id. |
12 var audioStreams = new media.ItemStore; | 16 var audioStreams = new media.ItemStore; |
13 | 17 |
18 // Active media players, indexed by 'render_id:player_id'. | |
19 var mediaPlayers = {}; | |
20 | |
14 // Cached files indexed by key and source id. | 21 // Cached files indexed by key and source id. |
15 var cacheEntriesByKey = {}; | 22 var cacheEntriesByKey = {}; |
16 var cacheEntries = {}; | 23 var cacheEntries = {}; |
17 | 24 |
18 // Map of event source -> url. | 25 // Map of event source -> url. |
19 var requestURLs = {}; | 26 var requestURLs = {}; |
20 | 27 |
21 // Constants passed to us from Chrome. | 28 // Constants passed to us from Chrome. |
22 var eventTypes = {}; | 29 var eventTypes = {}; |
23 var eventPhases = {}; | 30 var eventPhases = {}; |
24 | 31 |
25 // The <div>s on the page in which to display information. | 32 // The <div>s on the page in which to display information. |
26 var audioStreamDiv; | 33 var audioStreamDiv; |
27 var cacheDiv; | 34 var cacheDiv; |
35 var mediaPlayerDiv; | |
36 | |
37 // A timer used to limit the rate of redrawing the Media Players section. | |
38 var redrawTimer = null; | |
28 | 39 |
29 /** | 40 /** |
30 * Initialize variables and ask MediaInternals for all its data. | 41 * Initialize variables and ask MediaInternals for all its data. |
31 */ | 42 */ |
32 initialize = function() { | 43 initialize = function() { |
33 audioStreamDiv = document.getElementById('audio-streams'); | 44 audioStreamDiv = document.getElementById('audio-streams'); |
34 cacheDiv = document.getElementById('cache-entries'); | 45 cacheDiv = document.getElementById('cache-entries'); |
46 mediaPlayerDiv = document.getElementById('media-players'); | |
47 | |
35 // Get information about all currently active media. | 48 // Get information about all currently active media. |
36 chrome.send('getEverything'); | 49 chrome.send('getEverything'); |
37 }; | 50 }; |
38 | 51 |
39 /** | 52 /** |
40 * Write the set of audio streams to the DOM. | 53 * Write the set of audio streams to the DOM. |
41 */ | 54 */ |
42 printAudioStreams = function() { | 55 printAudioStreams = function() { |
43 | 56 |
44 /** | 57 /** |
(...skipping 16 matching lines...) Expand all Loading... | |
61 var out = document.createElement('ul'); | 74 var out = document.createElement('ul'); |
62 audioStreams.map(printStream).forEach(function(s) { | 75 audioStreams.map(printStream).forEach(function(s) { |
63 out.appendChild(s) | 76 out.appendChild(s) |
64 }); | 77 }); |
65 | 78 |
66 audioStreamDiv.textContent = ''; | 79 audioStreamDiv.textContent = ''; |
67 audioStreamDiv.appendChild(out); | 80 audioStreamDiv.appendChild(out); |
68 }; | 81 }; |
69 | 82 |
70 /** | 83 /** |
84 * Write the set of media players to the DOM. | |
85 */ | |
86 printMediaPlayers = function() { | |
arv (Not doing code reviews)
2011/08/16 19:29:03
missing var here and in other places
Scott Franklin
2011/08/16 23:33:52
Done.
| |
87 var out = document.createElement('ul'); | |
88 for (id in mediaPlayers) | |
arv (Not doing code reviews)
2011/08/16 19:29:03
missing var
Maybe you should swith to strict mode
Scott Franklin
2011/08/16 23:33:52
Done.
| |
89 out.appendChild(mediaPlayers[id].toListItem()); | |
90 | |
91 mediaPlayerDiv.textContent = ''; | |
92 mediaPlayerDiv.appendChild(out); | |
93 | |
94 redrawTimer = null; | |
95 }; | |
96 | |
97 /** | |
71 * Write the set of sparse CacheEntries to the DOM. | 98 * Write the set of sparse CacheEntries to the DOM. |
72 */ | 99 */ |
73 printSparseCacheEntries = function() { | 100 printSparseCacheEntries = function() { |
74 var out = document.createElement('ul'); | 101 var out = document.createElement('ul'); |
75 for (var key in cacheEntriesByKey) { | 102 for (var key in cacheEntriesByKey) { |
76 if (cacheEntriesByKey[key].sparse) | 103 if (cacheEntriesByKey[key].sparse) |
77 out.appendChild(cacheEntriesByKey[key].toListItem()); | 104 out.appendChild(cacheEntriesByKey[key].toListItem()); |
78 } | 105 } |
79 | 106 |
80 cacheDiv.textContent = ''; | 107 cacheDiv.textContent = ''; |
(...skipping 22 matching lines...) Expand all Loading... | |
103 }; | 130 }; |
104 | 131 |
105 /** | 132 /** |
106 * Removing an item from the appropriate store. | 133 * Removing an item from the appropriate store. |
107 * @param {string} id The id of the item to be removed, in the format | 134 * @param {string} id The id of the item to be removed, in the format |
108 * "item_type.identifying_info". | 135 * "item_type.identifying_info". |
109 */ | 136 */ |
110 onItemDeleted = function(id) { | 137 onItemDeleted = function(id) { |
111 var type = id.split('.')[0]; | 138 var type = id.split('.')[0]; |
112 switch (type) { | 139 switch (type) { |
113 case 'audio_streams': | 140 case 'audio_streams': |
arv (Not doing code reviews)
2011/08/16 19:29:03
indent
Scott Franklin
2011/08/16 23:33:52
Done.
| |
114 audioStreams.removeItem(id); | 141 audioStreams.removeItem(id); |
115 printAudioStreams(); | 142 printAudioStreams(); |
116 break; | 143 break; |
117 } | 144 } |
118 }; | 145 }; |
119 | 146 |
120 /** | 147 /** |
148 * A render process has ended, delete any media players associated with it. | |
149 * @param {int} renderer The id of the render process. | |
150 */ | |
151 onRendererTerminated = function(renderer) { | |
152 for (var key in mediaPlayers) { | |
153 if (mediaPlayers[key].renderer == renderer) | |
154 delete mediaPlayers[key]; | |
155 } | |
156 printMediaPlayers(); | |
157 }; | |
158 | |
159 /** | |
121 * Receiving net events. | 160 * Receiving net events. |
122 * Update cache information and update that section of the page. | 161 * Update cache information and update that section of the page. |
123 * @param {Array} updates A list of net events that have occurred. | 162 * @param {Array} updates A list of net events that have occurred. |
124 */ | 163 */ |
125 onNetUpdate = function(updates) { | 164 onNetUpdate = function(updates) { |
126 updates.forEach(function(update) { | 165 updates.forEach(function(update) { |
127 var id = update.source.id; | 166 var id = update.source.id; |
128 if (!cacheEntries[id]) | 167 if (!cacheEntries[id]) |
129 cacheEntries[id] = new media.CacheEntry; | 168 cacheEntries[id] = new media.CacheEntry; |
130 | 169 |
(...skipping 28 matching lines...) Expand all Loading... | |
159 break; | 198 break; |
160 | 199 |
161 case 'PHASE_NONE.HTTP_TRANSACTION_READ_RESPONSE_HEADERS': | 200 case 'PHASE_NONE.HTTP_TRANSACTION_READ_RESPONSE_HEADERS': |
162 // Record the total size of the file if this was a range request. | 201 // Record the total size of the file if this was a range request. |
163 var range = /content-range:\s*bytes\s*\d+-\d+\/(\d+)/i.exec( | 202 var range = /content-range:\s*bytes\s*\d+-\d+\/(\d+)/i.exec( |
164 update.params.headers); | 203 update.params.headers); |
165 var key = requestURLs[update.source.id]; | 204 var key = requestURLs[update.source.id]; |
166 delete requestURLs[update.source.id]; | 205 delete requestURLs[update.source.id]; |
167 if (range && key) { | 206 if (range && key) { |
168 if (!cacheEntriesByKey[key]) { | 207 if (!cacheEntriesByKey[key]) { |
169 cacheEntriesByKey[key] = new CacheEntry; | 208 cacheEntriesByKey[key] = new media.CacheEntry; |
170 cacheEntriesByKey[key].key = key; | 209 cacheEntriesByKey[key].key = key; |
171 } | 210 } |
172 cacheEntriesByKey[key].size = range[1]; | 211 cacheEntriesByKey[key].size = range[1]; |
173 } | 212 } |
174 break; | 213 break; |
175 } | 214 } |
176 }); | 215 }); |
177 | 216 |
178 printSparseCacheEntries(); | 217 printSparseCacheEntries(); |
179 }; | 218 }; |
180 | 219 |
181 /** | 220 /** |
182 * Receiving values for constants. Store them for later use. | 221 * Receiving values for constants. Store them for later use. |
183 * @param {Object} constants A dictionary of constants. | 222 * @param {Object} constants A dictionary of constants. |
184 * @param {Object} constants.eventTypes A dictionary of event name -> int. | 223 * @param {Object} constants.eventTypes A dictionary of event name -> int. |
185 * @param {Object} constants.eventPhases A dictionary of event phase -> int. | 224 * @param {Object} constants.eventPhases A dictionary of event phase -> int. |
186 */ | 225 */ |
187 onReceiveConstants = function(constants) { | 226 onReceiveConstants = function(constants) { |
188 var events = constants.eventTypes; | 227 var events = constants.eventTypes; |
189 for (var e in events) | 228 for (var e in events) |
190 eventTypes[events[e]] = e; | 229 eventTypes[events[e]] = e; |
191 | 230 |
192 var phases = constants.eventPhases; | 231 var phases = constants.eventPhases; |
193 for (var p in phases) | 232 for (var p in phases) |
194 eventPhases[phases[p]] = p; | 233 eventPhases[phases[p]] = p; |
195 }; | 234 }; |
196 | 235 |
236 /** | |
237 * Receiving notification of a media event. | |
238 * @param {Object} event The json representation of a MediaLog::Event. | |
239 */ | |
240 onMediaEvent = function(event) { | |
241 // List everything in miliseconds. | |
242 event.time *= 1000; | |
243 | |
244 var source = event.renderer + ':' + event.player; | |
245 var item = mediaPlayers[source] || | |
246 new media.MediaPlayer(source, event.renderer); | |
247 mediaPlayers[source] = item; | |
248 item.addEvent(event); | |
249 | |
250 // Both media and net events could provide the size of the file. | |
251 // Media takes priority, but keep the size in both places synchronized. | |
252 if (cacheEntriesByKey[item.properties.url]) { | |
253 item.properties.total_bytes = item.properties.total_bytes || | |
254 cacheEntriesByKey[item.properties.url].size; | |
255 cacheEntriesByKey[item.properties.url].size = item.properties.total_bytes; | |
256 } | |
257 | |
258 // Events tend to arrive in groups; don't redraw the page too often. | |
259 if (!redrawTimer) | |
260 redrawTimer = setTimeout("printMediaPlayers()", 50); | |
arv (Not doing code reviews)
2011/08/16 19:29:03
Don't use string based setTimout since it is a glo
Scott Franklin
2011/08/16 23:33:52
Done.
| |
261 }; | |
262 | |
197 return { | 263 return { |
198 initialize: initialize, | 264 initialize: initialize, |
199 addAudioStream: addAudioStream, | 265 addAudioStream: addAudioStream, |
266 cacheEntriesByKey: cacheEntriesByKey, | |
200 onReceiveEverything: onReceiveEverything, | 267 onReceiveEverything: onReceiveEverything, |
201 onItemDeleted: onItemDeleted, | 268 onItemDeleted: onItemDeleted, |
269 onRendererTerminated: onRendererTerminated, | |
202 onNetUpdate: onNetUpdate, | 270 onNetUpdate: onNetUpdate, |
203 onReceiveConstants: onReceiveConstants, | 271 onReceiveConstants: onReceiveConstants, |
272 onMediaEvent: onMediaEvent, | |
204 }; | 273 }; |
205 }); | 274 }); |
206 | 275 |
207 /** | 276 /** |
208 * Initialize everything once we have access to the DOM. | 277 * Initialize everything once we have access to the DOM. |
209 */ | 278 */ |
210 window.onload = function() { | 279 window.onload = function() { |
211 media.initialize(); | 280 media.initialize(); |
212 }; | 281 }; |
OLD | NEW |