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.
6 * A global object that gets used by the C++ interface.
8 var media = (function() {
13 // A number->string mapping that is populated through the backend that
14 // describes the phase that the network entity is in.
17 // A number->string mapping that is populated through the backend that
18 // describes the type of event sent from the network.
21 // A mapping of number->CacheEntry where the number is a unique id for that
23 var cacheEntries = {};
25 // A mapping of url->CacheEntity where the url is the url of the resource.
26 var cacheEntriesByKey = {};
28 var requrestURLs = {};
36 * Users of |media| must call initialize prior to calling other methods.
38 media.initialize = function(theManager) {
42 media.onReceiveEverything = function(everything) {
43 for (var key in everything.audio_streams) {
44 media.updateAudioStream(everything.audio_streams[key]);
48 media.onReceiveConstants = function(constants) {
49 for (var key in constants.eventTypes) {
50 var value = constants.eventTypes[key];
51 eventTypes[value] = key;
54 for (var key in constants.eventPhases) {
55 var value = constants.eventPhases[key];
56 eventPhases[value] = key;
60 media.cacheForUrl = function(url) {
61 return cacheEntriesByKey[url];
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;
70 switch (eventPhases[update.phase] + '.' + eventTypes[update.type]) {
71 case 'PHASE_BEGIN.DISK_CACHE_ENTRY_IMPL':
72 var key = update.params.key;
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];
79 cacheEntriesByKey[key] = cacheEntries[id];
81 cacheEntriesByKey[key].key = key;
84 case 'PHASE_BEGIN.SPARSE_READ':
85 cacheEntries[id].readBytes(update.params.offset,
86 update.params.buff_len);
87 cacheEntries[id].sparse = true;
90 case 'PHASE_BEGIN.SPARSE_WRITE':
91 cacheEntries[id].writeBytes(update.params.offset,
92 update.params.buff_len);
93 cacheEntries[id].sparse = true;
96 case 'PHASE_BEGIN.URL_REQUEST_START_JOB':
97 requrestURLs[update.source.id] = update.params.url;
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];
107 if (!cacheEntriesByKey[key]) {
108 cacheEntriesByKey[key] = new media.CacheEntry;
109 cacheEntriesByKey[key].key = key;
111 cacheEntriesByKey[key].size = range[1];
118 media.onRendererTerminated = function(renderId) {
119 util.object.forEach(manager.players_, function(playerInfo, id) {
120 if (playerInfo.properties['render_id'] == renderId) {
121 manager.removePlayer(id);
126 // For whatever reason, addAudioStream is also called on
127 // the removal of audio streams.
128 media.addAudioStream = function(event) {
129 switch (event.status) {
131 manager.addAudioStream(event.id);
132 manager.updateAudioStream(event.id, { 'playing': event.playing });
135 manager.removeAudioStream(event.id);
140 media.updateAudioStream = function(stream) {
141 manager.addAudioStream(stream.id);
142 manager.updateAudioStream(stream.id, stream);
145 media.onItemDeleted = function() {
146 // This only gets called when an audio stream is removed, which
147 // for whatever reason is also handled by addAudioStream...
148 // Because it is already handled, we can safely ignore it.
151 media.onPlayerOpen = function(id, timestamp) {
152 manager.addPlayer(id, timestamp);
155 media.onMediaEvent = function(event) {
156 var source = event.renderer + ':' + event.player;
158 // Although this gets called on every event, there is nothing we can do
159 // because there is no onOpen event.
160 media.onPlayerOpen(source);
161 manager.updatePlayerInfoNoRecord(
162 source, event.ticksMillis, 'render_id', event.renderer);
163 manager.updatePlayerInfoNoRecord(
164 source, event.ticksMillis, 'player_id', event.player);
166 var propertyCount = 0;
167 util.object.forEach(event.params, function(value, key) {
170 // These keys get spammed *a lot*, so put them on the display
171 // but don't log list.
172 if (key === 'buffer_start' ||
173 key === 'buffer_end' ||
174 key === 'buffer_current' ||
175 key === 'is_downloading_data') {
176 manager.updatePlayerInfoNoRecord(
177 source, event.ticksMillis, key, value);
179 manager.updatePlayerInfo(source, event.ticksMillis, key, value);
184 if (propertyCount === 0) {
185 manager.updatePlayerInfo(
186 source, event.ticksMillis, 'EVENT', event.type);
190 // |chrome| is not defined during tests.
191 if (window.chrome && window.chrome.send) {
192 chrome.send('getEverything');