OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 var ClientRenderer = (function() { | |
6 var ClientRenderer = function() { | |
7 this.playerListElement = document.getElementById('player-list'); | |
8 this.audioStreamListElement = document.getElementById('audio-stream-list'); | |
9 this.propertiesTable = document.getElementById('property-table'); | |
10 this.logTable = document.getElementById('log'); | |
11 this.graphElement = document.getElementById('graphs'); | |
12 | |
13 this.selectedPlayer = null; | |
14 this.selectedStream = null; | |
15 | |
16 this.selectedPlayerLogIndex = 0; | |
17 | |
18 this.bufferCanvas = document.createElement('canvas'); | |
19 this.bufferCanvas.width = media.BAR_WIDTH; | |
20 this.bufferCanvas.height = media.BAR_HEIGHT; | |
21 }; | |
22 | |
23 function removeChildren(element) { | |
24 while (element.hasChildNodes()) { | |
25 element.removeChild(element.lastChild); | |
26 } | |
27 }; | |
28 | |
29 function createButton(text, select_cb) { | |
30 var button = document.createElement('button'); | |
31 | |
32 button.appendChild(document.createTextNode(text)); | |
33 button.onclick = function() { | |
34 select_cb(); | |
35 }; | |
36 | |
37 return button; | |
38 }; | |
39 | |
40 ClientRenderer.prototype = { | |
41 audioStreamAdded: function(audioStreams, audioStreamAdded) { | |
42 this.redrawAudioStreamList_(audioStreams); | |
43 }, | |
44 | |
45 audioStreamUpdated: function(audioStreams, stream, key, value) { | |
46 if (stream === this.selectedStream) { | |
47 this.drawProperties_(stream); | |
48 } | |
49 }, | |
50 | |
51 audioStreamRemoved: function(audioStreams, audioStreamRemoved) { | |
52 this.redrawAudioStreamList_(audioStreams); | |
53 }, | |
54 | |
55 /** | |
56 * Called when a player is added to the collection. | |
57 * @param players The entire map of id -> player. | |
58 * @param player_added The player that is added. | |
59 */ | |
60 playerAdded: function(players, playerAdded) { | |
61 this.redrawPlayerList_(players); | |
62 }, | |
63 | |
64 /** | |
65 * Called when a playre is removed from the collection. | |
66 * @param players The entire map of id -> player. | |
67 * @param player_added The player that was removed. | |
68 */ | |
69 playerRemoved: function(players, playerRemoved) { | |
70 this.redrawPlayerList_(players); | |
71 }, | |
72 | |
73 /** | |
74 * Called when a property on a player is changed. | |
75 * @param players The entire map of id -> player. | |
76 * @param player The player that had its property changed. | |
77 * @param key The name of the property that was changed. | |
78 * @param value The new value of the property. | |
79 */ | |
80 playerUpdated: function(players, player, key, value) { | |
81 if (player === this.selectedPlayer) { | |
82 this.drawProperties_(player.properties); | |
83 this.drawLog_(); | |
84 this.drawGraphs_(); | |
85 } | |
86 if (key === 'name' || key === 'url') { | |
87 this.redrawPlayerList_(players); | |
88 } | |
89 }, | |
90 | |
91 redrawAudioStreamList_: function(streams) { | |
92 removeChildren(this.audioStreamListElement); | |
93 | |
94 for (id in streams) { | |
95 var li = document.createElement('li'); | |
96 li.appendChild(createButton( | |
97 id, this.selectAudioStream_.bind(this, streams[id]))); | |
98 this.audioStreamListElement.appendChild(li); | |
99 } | |
100 }, | |
101 | |
102 selectAudioStream_: function(audioStream) { | |
103 this.selectedStream = audioStream; | |
104 this.selectedPlayer = null; | |
105 this.drawProperties_(audioStream); | |
106 removeChildren(this.logTable.querySelector('tbody')); | |
107 removeChildren(this.graphElement); | |
108 }, | |
109 | |
110 redrawPlayerList_: function(players) { | |
111 removeChildren(this.playerListElement); | |
112 | |
113 for (id in players) { | |
114 var li = document.createElement('li'); | |
115 var player = players[id]; | |
116 var usableName = player.properties.name || | |
117 player.properties.url || | |
118 'player ' + player.id; | |
119 | |
120 li.appendChild(createButton( | |
121 usableName, this.selectPlayer_.bind(this, player))); | |
122 this.playerListElement.appendChild(li); | |
123 } | |
124 }, | |
125 | |
126 selectPlayer_: function(player) { | |
127 this.selectedPlayer = player; | |
128 this.selectedPlayerLogIndex = 0; | |
129 this.selectedStream = null; | |
130 this.drawProperties_(player.properties); | |
131 | |
132 removeChildren(this.logTable.querySelector('tbody')); | |
133 removeChildren(this.graphElement); | |
134 this.drawLog_(); | |
135 this.drawGraphs_(); | |
136 }, | |
137 | |
138 drawProperties_: function(propertyMap) { | |
139 removeChildren(this.propertiesTable); | |
140 | |
141 for (key in propertyMap) { | |
142 var value = propertyMap[key]; | |
143 | |
144 var row = this.propertiesTable.insertRow(-1); | |
145 var keyCell = row.insertCell(-1); | |
146 var valueCell = row.insertCell(-1); | |
147 | |
148 keyCell.appendChild(document.createTextNode(key)); | |
149 valueCell.appendChild(document.createTextNode(value)); | |
150 } | |
151 }, | |
152 | |
153 appendEventToLog_: function(event) { | |
154 var row = this.logTable.querySelector('tbody').insertRow(-1); | |
155 | |
156 row.insertCell(-1).appendChild(document.createTextNode( | |
157 util.millisecondsToString(event.time))); | |
158 row.insertCell(-1).appendChild(document.createTextNode(event.key)); | |
159 row.insertCell(-1).appendChild(document.createTextNode(event.value)); | |
160 }, | |
161 | |
162 drawLog_: function() { | |
163 var toDraw = this.selectedPlayer.allEvents.slice( | |
164 this.selectedPlayerLogIndex); | |
165 toDraw.forEach(this.appendEventToLog_.bind(this)); | |
166 this.selectedPlayerLogIndex = this.selectedPlayer.allEvents.length; | |
167 }, | |
168 | |
169 drawGraphs_: function() { | |
170 function addToGraphs (name, graph, graphElement) { | |
171 var li = document.createElement('li'); | |
172 li.appendChild(graph); | |
173 li.appendChild(document.createTextNode(name)); | |
174 graphElement.appendChild(li); | |
175 } | |
176 | |
177 var url = this.selectedPlayer.properties.url; | |
178 if (!url) { | |
179 return; | |
180 } | |
181 | |
182 var cache = media.cacheForUrl(url); | |
183 | |
184 var player = this.selectedPlayer; | |
185 var props = player.properties; | |
186 | |
187 var cacheExists = false; | |
188 var bufferExists = false; | |
189 | |
190 if (props['buffer_start'] !== undefined && | |
191 props['buffer_current'] !== undefined && | |
192 props['buffer_end'] !== undefined && | |
193 props['total_bytes'] !== undefined) { | |
194 this.drawBufferGraph_(props['buffer_start'], | |
195 props['buffer_current'], | |
196 props['buffer_end'], | |
197 props['total_bytes']); | |
198 bufferExists = true; | |
199 } | |
200 | |
201 if (cache) { | |
202 if(player.properties['total_bytes']) { | |
203 cache.size = Number(player.properties['total_bytes']); | |
204 } | |
205 cache.generateDetails(); | |
206 cacheExists = true; | |
207 | |
208 } | |
209 | |
210 if (!this.graphElement.hasChildNodes()) { | |
211 if (bufferExists) { | |
212 addToGraphs('buffer', this.bufferCanvas, this.graphElement); | |
213 } | |
214 if (cacheExists) { | |
215 addToGraphs('cache read', cache.readCanvas, this.graphElement); | |
216 addToGraphs('cache write', cache.writeCanvas, this.graphElement); | |
217 } | |
218 } | |
219 }, | |
220 | |
221 drawBufferGraph_: function(start, current, end, size) { | |
222 var ctx = this.bufferCanvas.getContext('2d'); | |
223 var width = this.bufferCanvas.width; | |
224 var height = this.bufferCanvas.height; | |
225 ctx.fillStyle = '#aaa'; | |
226 ctx.fillRect(0, 0, width, height); | |
227 | |
228 var scale_factor = width / size; | |
229 var left = start * scale_factor; | |
230 var middle = current * scale_factor; | |
231 var right = end * scale_factor; | |
232 | |
233 ctx.fillStyle = '#a0a'; | |
234 ctx.fillRect(left, 0, middle - left, height); | |
235 ctx.fillStyle = '#aa0'; | |
236 ctx.fillRect(middle, 0, right - middle, height); | |
237 } | |
238 }; | |
239 | |
240 return ClientRenderer; | |
241 })(); | |
OLD | NEW |