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.onReceiveAudioStreamData = function(audioStreamData) {
43 for (var component in audioStreamData) {
44 media.updateAudioComponent(audioStreamData[component]);
48 media.onReceiveVideoCaptureCapabilities = function(videoCaptureCapabilities) {
49 manager.updateVideoCaptureCapabilities(videoCaptureCapabilities)
52 media.onReceiveConstants = function(constants) {
53 for (var key in constants.eventTypes) {
54 var value = constants.eventTypes[key];
55 eventTypes[value] = key;
58 for (var key in constants.eventPhases) {
59 var value = constants.eventPhases[key];
60 eventPhases[value] = key;
64 media.cacheForUrl = function(url) {
65 return cacheEntriesByKey[url];
68 media.onNetUpdate = function(updates) {
69 updates.forEach(function(update) {
70 var id = update.source.id;
71 if (!cacheEntries[id])
72 cacheEntries[id] = new media.CacheEntry;
74 switch (eventPhases[update.phase] + '.' + eventTypes[update.type]) {
75 case 'PHASE_BEGIN.DISK_CACHE_ENTRY_IMPL':
76 var key = update.params.key;
78 // Merge this source with anything we already know about this key.
79 if (cacheEntriesByKey[key]) {
80 cacheEntriesByKey[key].merge(cacheEntries[id]);
81 cacheEntries[id] = cacheEntriesByKey[key];
83 cacheEntriesByKey[key] = cacheEntries[id];
85 cacheEntriesByKey[key].key = key;
88 case 'PHASE_BEGIN.SPARSE_READ':
89 cacheEntries[id].readBytes(update.params.offset,
90 update.params.buff_len);
91 cacheEntries[id].sparse = true;
94 case 'PHASE_BEGIN.SPARSE_WRITE':
95 cacheEntries[id].writeBytes(update.params.offset,
96 update.params.buff_len);
97 cacheEntries[id].sparse = true;
100 case 'PHASE_BEGIN.URL_REQUEST_START_JOB':
101 requrestURLs[update.source.id] = update.params.url;
104 case 'PHASE_NONE.HTTP_TRANSACTION_READ_RESPONSE_HEADERS':
105 // Record the total size of the file if this was a range request.
106 var range = /content-range:\s*bytes\s*\d+-\d+\/(\d+)/i.exec(
107 update.params.headers);
108 var key = requrestURLs[update.source.id];
109 delete requrestURLs[update.source.id];
111 if (!cacheEntriesByKey[key]) {
112 cacheEntriesByKey[key] = new media.CacheEntry;
113 cacheEntriesByKey[key].key = key;
115 cacheEntriesByKey[key].size = range[1];
122 media.onRendererTerminated = function(renderId) {
123 util.object.forEach(manager.players_, function(playerInfo, id) {
124 if (playerInfo.properties['render_id'] == renderId) {
125 manager.removePlayer(id);
130 media.updateAudioComponent = function(component) {
131 var uniqueComponentId = component.owner_id + ':' + component.component_id;
132 switch (component.status) {
134 manager.removeAudioComponent(
135 component.component_type, uniqueComponentId);
138 manager.updateAudioComponent(
139 component.component_type, uniqueComponentId, component);
144 media.onPlayerOpen = function(id, timestamp) {
145 manager.addPlayer(id, timestamp);
148 media.onMediaEvent = function(event) {
149 var source = event.renderer + ':' + event.player;
151 // Although this gets called on every event, there is nothing we can do
152 // because there is no onOpen event.
153 media.onPlayerOpen(source);
154 manager.updatePlayerInfoNoRecord(
155 source, event.ticksMillis, 'render_id', event.renderer);
156 manager.updatePlayerInfoNoRecord(
157 source, event.ticksMillis, 'player_id', event.player);
159 var propertyCount = 0;
160 util.object.forEach(event.params, function(value, key) {
163 // These keys get spammed *a lot*, so put them on the display
164 // but don't log list.
165 if (key === 'buffer_start' ||
166 key === 'buffer_end' ||
167 key === 'buffer_current' ||
168 key === 'is_downloading_data') {
169 manager.updatePlayerInfoNoRecord(
170 source, event.ticksMillis, key, value);
172 manager.updatePlayerInfo(source, event.ticksMillis, key, value);
177 if (propertyCount === 0) {
178 manager.updatePlayerInfo(
179 source, event.ticksMillis, 'EVENT', event.type);
183 // |chrome| is not defined during tests.
184 if (window.chrome && window.chrome.send) {
185 chrome.send('getEverything');