Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / resources / media / manager.js
blobb335349fc34a77c239be7acd724d20dc06622bc1
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.
5 /**
6  * @fileoverview Keeps track of all the existing PlayerInfo and
7  * audio stream objects and is the entry-point for messages from the backend.
8  *
9  * The events captured by Manager (add, remove, update) are relayed
10  * to the clientRenderer which it can choose to use to modify the UI.
11  */
12 var Manager = (function() {
13   'use strict';
15   function Manager(clientRenderer) {
16     this.players_ = {};
17     this.audioComponents_ = [];
18     this.clientRenderer_ = clientRenderer;
19   }
21   Manager.prototype = {
22     /**
23      * Updates an audio-component.
24      * @param componentType Integer AudioComponent enum value; must match values
25      * from the AudioLogFactory::AudioComponent enum.
26      * @param componentId The unique-id of the audio-component.
27      * @param componentData The actual component data dictionary.
28      */
29     updateAudioComponent: function(componentType, componentId, componentData) {
30       if (!(componentType in this.audioComponents_))
31         this.audioComponents_[componentType] = {};
32       if (!(componentId in this.audioComponents_[componentType])) {
33         this.audioComponents_[componentType][componentId] = componentData;
34       } else {
35         for (var key in componentData) {
36           this.audioComponents_[componentType][componentId][key] =
37               componentData[key];
38         }
39       }
40       this.clientRenderer_.audioComponentAdded(
41           componentType, this.audioComponents_[componentType]);
42     },
44     /**
45      * Removes an audio-stream from the manager.
46      * @param id The unique-id of the audio-stream.
47      */
48     removeAudioComponent: function(componentType, componentId) {
49       if (!(componentType in this.audioComponents_) ||
50           !(componentId in this.audioComponents_[componentType])) {
51         return;
52       }
54       delete this.audioComponents_[componentType][componentId];
55       this.clientRenderer_.audioComponentRemoved(
56           componentType, this.audioComponents_[componentType]);
57     },
59     /**
60      * Adds a player to the list of players to manage.
61      */
62     addPlayer: function(id) {
63       if (this.players_[id]) {
64         return;
65       }
66       // Make the PlayerProperty and add it to the mapping
67       this.players_[id] = new PlayerInfo(id);
68       this.clientRenderer_.playerAdded(this.players_, this.players_[id]);
69     },
71     /**
72      * Attempts to remove a player from the UI.
73      * @param id The ID of the player to remove.
74      */
75     removePlayer: function(id) {
76       var playerRemoved = this.players_[id];
77       delete this.players_[id];
78       this.clientRenderer_.playerRemoved(this.players_, playerRemoved);
79     },
81     updatePlayerInfoNoRecord: function(id, timestamp, key, value) {
82       if (!this.players_[id]) {
83         console.error('[updatePlayerInfo] Id ' + id + ' does not exist');
84         return;
85       }
87       this.players_[id].addPropertyNoRecord(timestamp, key, value);
88       this.clientRenderer_.playerUpdated(this.players_,
89                                          this.players_[id],
90                                          key,
91                                          value);
92     },
94     /**
95      *
96      * @param id The unique ID that identifies the player to be updated.
97      * @param timestamp The timestamp of when the change occured.  This
98      * timestamp is *not* normalized.
99      * @param key The name of the property to be added/changed.
100      * @param value The value of the property.
101      */
102     updatePlayerInfo: function(id, timestamp, key, value) {
103       if (!this.players_[id]) {
104         console.error('[updatePlayerInfo] Id ' + id + ' does not exist');
105         return;
106       }
108       this.players_[id].addProperty(timestamp, key, value);
109       this.clientRenderer_.playerUpdated(this.players_,
110                                          this.players_[id],
111                                          key,
112                                          value);
113     },
115     parseVideoCaptureFormat_: function(format) {
116       /**
117        * Example:
118        *
119        * format:
120        *   "resolution: 1280x720, fps: 30.000000, pixel format: I420"
121        *
122        * formatDict:
123        *   {'resolution':'1280x720', 'fps': '30.00'}
124        */
125       var parts = format.split(', ');
126       var formatDict = {};
127       for (var i in parts) {
128         var kv = parts[i].split(': ');
129         formatDict[kv[0]] = kv[1];
130       }
132       // Round down the FPS to 2 decimals.
133       formatDict['fps'] = parseFloat(formatDict['fps']).toFixed(2);
135       // The camera does not actually output I420 so this info is misleading.
136       delete formatDict['pixel format'];
138       return formatDict;
139     },
141     updateVideoCaptureCapabilities: function(videoCaptureCapabilities) {
142       // Parse the video formats to be structured for the table.
143       for (var i in videoCaptureCapabilities) {
144         for (var j in videoCaptureCapabilities[i]['formats']) {
145           videoCaptureCapabilities[i]['formats'][j] =
146               this.parseVideoCaptureFormat_(
147                     videoCaptureCapabilities[i]['formats'][j]);
148         }
149       }
151       // The keys of each device to be shown in order of appearance.
152       var videoCaptureDeviceKeys = ['name','formats','captureApi','id'];
154       this.clientRenderer_.redrawVideoCaptureCapabilities(
155           videoCaptureCapabilities, videoCaptureDeviceKeys);
156     }
157   };
159   return Manager;
160 }());