OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 * A global object that gets used by the C++ interface. | 6 * A global object that gets used by the C++ interface. |
7 */ | 7 */ |
8 var media = (function() { | 8 var media = (function() { |
9 'use strict'; | 9 'use strict'; |
10 | 10 |
11 var manager_ = null; | 11 var manager = null; |
| 12 |
| 13 // A number->string mapping that is populated through the backend that |
| 14 // describes the phase that the network entity is in. |
| 15 var eventPhases = {}; |
| 16 |
| 17 // A number->string mapping that is populated through the backend that |
| 18 // describes the type of event sent from the network. |
| 19 var eventTypes = {}; |
| 20 |
| 21 // A mapping of number->CacheEntry where the number is a unique id for that |
| 22 // network request. |
| 23 var cacheEntries = {}; |
| 24 |
| 25 // A mapping of url->CacheEntity where the url is the url of the resource. |
| 26 var cacheEntriesByKey = {}; |
| 27 |
| 28 var requrestURLs = {}; |
| 29 |
| 30 var media = { |
| 31 BAR_WIDTH: 200, |
| 32 BAR_HEIGHT: 25 |
| 33 }; |
12 | 34 |
13 /** | 35 /** |
14 * Users of |media| must call initialize prior to calling other methods. | 36 * Users of |media| must call initialize prior to calling other methods. |
15 */ | 37 */ |
16 media.initialize = function(manager) { | 38 media.initialize = function(theManager) { |
17 manager_ = manager; | 39 manager = theManager; |
18 }; | 40 }; |
19 | 41 |
20 media.onReceiveEverything = function(everything) { | 42 media.onReceiveEverything = function(everything) { |
21 for (var key in everything.audio_streams) { | 43 for (var key in everything.audio_streams) { |
22 media.updateAudioStream(everything.audio_streams[key]); | 44 media.updateAudioStream(everything.audio_streams[key]); |
23 } | 45 } |
24 }; | 46 }; |
25 | 47 |
26 media.onNetUpdate = function(update) { | 48 media.onReceiveConstants = function(constants) { |
27 // TODO(tyoverby): Implement | 49 for (var key in constants.eventTypes) { |
| 50 var value = constants.eventTypes[key]; |
| 51 eventTypes[value] = key; |
| 52 } |
| 53 |
| 54 for (var key in constants.eventPhases) { |
| 55 var value = constants.eventPhases[key]; |
| 56 eventPhases[value] = key; |
| 57 } |
| 58 }; |
| 59 |
| 60 media.cacheForUrl = function(url) { |
| 61 return cacheEntriesByKey[url]; |
| 62 }; |
| 63 |
| 64 media.onNetUpdate = function(updates) { |
| 65 updates.forEach(function(update) { |
| 66 var id = update.source.id; |
| 67 if (!cacheEntries[id]) |
| 68 cacheEntries[id] = new media.CacheEntry; |
| 69 |
| 70 switch (eventPhases[update.phase] + '.' + eventTypes[update.type]) { |
| 71 case 'PHASE_BEGIN.DISK_CACHE_ENTRY_IMPL': |
| 72 var key = update.params.key; |
| 73 |
| 74 // Merge this source with anything we already know about this key. |
| 75 if (cacheEntriesByKey[key]) { |
| 76 cacheEntriesByKey[key].merge(cacheEntries[id]); |
| 77 cacheEntries[id] = cacheEntriesByKey[key]; |
| 78 } else { |
| 79 cacheEntriesByKey[key] = cacheEntries[id]; |
| 80 } |
| 81 cacheEntriesByKey[key].key = key; |
| 82 break; |
| 83 |
| 84 case 'PHASE_BEGIN.SPARSE_READ': |
| 85 cacheEntries[id].readBytes(update.params.offset, |
| 86 update.params.buff_len); |
| 87 cacheEntries[id].sparse = true; |
| 88 break; |
| 89 |
| 90 case 'PHASE_BEGIN.SPARSE_WRITE': |
| 91 cacheEntries[id].writeBytes(update.params.offset, |
| 92 update.params.buff_len); |
| 93 cacheEntries[id].sparse = true; |
| 94 break; |
| 95 |
| 96 case 'PHASE_BEGIN.URL_REQUEST_START_JOB': |
| 97 requrestURLs[update.source.id] = update.params.url; |
| 98 break; |
| 99 |
| 100 case 'PHASE_NONE.HTTP_TRANSACTION_READ_RESPONSE_HEADERS': |
| 101 // Record the total size of the file if this was a range request. |
| 102 var range = /content-range:\s*bytes\s*\d+-\d+\/(\d+)/i.exec( |
| 103 update.params.headers); |
| 104 var key = requrestURLs[update.source.id]; |
| 105 delete requrestURLs[update.source.id]; |
| 106 if (range && key) { |
| 107 if (!cacheEntriesByKey[key]) { |
| 108 cacheEntriesByKey[key] = new media.CacheEntry; |
| 109 cacheEntriesByKey[key].key = key; |
| 110 } |
| 111 cacheEntriesByKey[key].size = range[1]; |
| 112 } |
| 113 break; |
| 114 } |
| 115 }); |
28 }; | 116 }; |
29 | 117 |
30 media.onRendererTerminated = function(renderId) { | 118 media.onRendererTerminated = function(renderId) { |
31 util.object.forEach(manager_.players_, function(playerInfo, id) { | 119 util.object.forEach(manager.players_, function(playerInfo, id) { |
32 if (playerInfo.properties['render_id'] == renderId) { | 120 if (playerInfo.properties['render_id'] == renderId) { |
33 manager_.removePlayer(id); | 121 manager.removePlayer(id); |
34 } | 122 } |
35 }); | 123 }); |
36 }; | 124 }; |
37 | 125 |
38 // For whatever reason, addAudioStream is also called on | 126 // For whatever reason, addAudioStream is also called on |
39 // the removal of audio streams. | 127 // the removal of audio streams. |
40 media.addAudioStream = function(event) { | 128 media.addAudioStream = function(event) { |
41 switch (event.status) { | 129 switch (event.status) { |
42 case 'created': | 130 case 'created': |
43 manager_.addAudioStream(event.id); | 131 manager.addAudioStream(event.id); |
44 manager_.updateAudioStream(event.id, { 'playing': event.playing }); | 132 manager.updateAudioStream(event.id, { 'playing': event.playing }); |
45 break; | 133 break; |
46 case 'closed': | 134 case 'closed': |
47 manager_.removeAudioStream(event.id); | 135 manager.removeAudioStream(event.id); |
48 break; | 136 break; |
49 } | 137 } |
50 }; | 138 }; |
51 | 139 |
52 media.updateAudioStream = function(stream) { | 140 media.updateAudioStream = function(stream) { |
53 manager_.addAudioStream(stream.id); | 141 manager.addAudioStream(stream.id); |
54 manager_.updateAudioStream(stream.id, stream); | 142 manager.updateAudioStream(stream.id, stream); |
55 }; | 143 }; |
56 | 144 |
57 media.onItemDeleted = function() { | 145 media.onItemDeleted = function() { |
58 // This only gets called when an audio stream is removed, which | 146 // This only gets called when an audio stream is removed, which |
59 // for whatever reason is also handled by addAudioStream... | 147 // for whatever reason is also handled by addAudioStream... |
60 // Because it is already handled, we can safely ignore it. | 148 // Because it is already handled, we can safely ignore it. |
61 }; | 149 }; |
62 | 150 |
63 media.onPlayerOpen = function(id, timestamp) { | 151 media.onPlayerOpen = function(id, timestamp) { |
64 manager_.addPlayer(id, timestamp); | 152 manager.addPlayer(id, timestamp); |
65 }; | 153 }; |
66 | 154 |
67 media.onMediaEvent = function(event) { | 155 media.onMediaEvent = function(event) { |
68 var source = event.renderer + ':' + event.player; | 156 var source = event.renderer + ':' + event.player; |
69 | 157 |
70 // Although this gets called on every event, there is nothing we can do | 158 // Although this gets called on every event, there is nothing we can do |
71 // because there is no onOpen event. | 159 // because there is no onOpen event. |
72 media.onPlayerOpen(source); | 160 media.onPlayerOpen(source); |
73 manager_.updatePlayerInfoNoRecord( | 161 manager.updatePlayerInfoNoRecord( |
74 source, event.ticksMillis, 'render_id', event.renderer); | 162 source, event.ticksMillis, 'render_id', event.renderer); |
75 manager_.updatePlayerInfoNoRecord( | 163 manager.updatePlayerInfoNoRecord( |
76 source, event.ticksMillis, 'player_id', event.player); | 164 source, event.ticksMillis, 'player_id', event.player); |
77 | 165 |
78 var propertyCount = 0; | 166 var propertyCount = 0; |
79 util.object.forEach(event.params, function(value, key) { | 167 util.object.forEach(event.params, function(value, key) { |
80 key = key.trim(); | 168 key = key.trim(); |
81 | 169 |
82 // These keys get spammed *a lot*, so put them on the display | 170 // These keys get spammed *a lot*, so put them on the display |
83 // but don't log list. | 171 // but don't log list. |
84 if (key === 'buffer_start' || | 172 if (key === 'buffer_start' || |
85 key === 'buffer_end' || | 173 key === 'buffer_end' || |
86 key === 'buffer_current' || | 174 key === 'buffer_current' || |
87 key === 'is_downloading_data') { | 175 key === 'is_downloading_data') { |
88 manager_.updatePlayerInfoNoRecord( | 176 manager.updatePlayerInfoNoRecord( |
89 source, event.ticksMillis, key, value); | 177 source, event.ticksMillis, key, value); |
90 } else { | 178 } else { |
91 manager_.updatePlayerInfo(source, event.ticksMillis, key, value); | 179 manager.updatePlayerInfo(source, event.ticksMillis, key, value); |
92 } | 180 } |
93 propertyCount += 1; | 181 propertyCount += 1; |
94 }); | 182 }); |
95 | 183 |
96 if (propertyCount === 0) { | 184 if (propertyCount === 0) { |
97 manager_.updatePlayerInfo( | 185 manager.updatePlayerInfo( |
98 source, event.ticksMillis, 'EVENT', event.type); | 186 source, event.ticksMillis, 'EVENT', event.type); |
99 } | 187 } |
100 }; | 188 }; |
101 | 189 |
| 190 // |chrome| is not defined during tests. |
| 191 if (window.chrome && window.chrome.send) { |
| 192 chrome.send('getEverything'); |
| 193 } |
102 return media; | 194 return media; |
103 }()); | 195 }()); |
OLD | NEW |