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/**
6 * @fileoverview A class for keeping track of the details of a player.
7 */
8
9var PlayerInfo = (function() {
10  'use strict';
11
12  /**
13   * A class that keeps track of properties on a media player.
14   * @param id A unique id that can be used to identify this player.
15   */
16  function PlayerInfo(id) {
17    this.id = id;
18    // The current value of the properties for this player.
19    this.properties = {};
20    // All of the past (and present) values of the properties.
21    this.pastValues = {};
22
23    // Every single event in the order in which they were received.
24    this.allEvents = [];
25    this.lastRendered = 0;
26
27    this.firstTimestamp_ = -1;
28  }
29
30  PlayerInfo.prototype = {
31    /**
32     * Adds or set a property on this player.
33     * This is the default logging method as it keeps track of old values.
34     * @param timestamp  The time in milliseconds since the Epoch.
35     * @param key A String key that describes the property.
36     * @param value The value of the property.
37     */
38    addProperty: function(timestamp, key, value) {
39      // The first timestamp that we get will be recorded.
40      // Then, all future timestamps are deltas of that.
41      if (this.firstTimestamp_ === -1) {
42        this.firstTimestamp_ = timestamp;
43      }
44
45      if (typeof key !== 'string') {
46        throw new Error(typeof key + ' is not a valid key type');
47      }
48
49      this.properties[key] = value;
50
51      if (!this.pastValues[key]) {
52        this.pastValues[key] = [];
53      }
54
55      var recordValue = {
56        time: timestamp - this.firstTimestamp_,
57        key: key,
58        value: value
59      };
60
61      this.pastValues[key].push(recordValue);
62      this.allEvents.push(recordValue);
63    },
64
65    /**
66     * Adds or set a property on this player.
67     * Does not keep track of old values.  This is better for
68     * values that get spammed repeatedly.
69     * @param timestamp The time in milliseconds since the Epoch.
70     * @param key A String key that describes the property.
71     * @param value The value of the property.
72     */
73    addPropertyNoRecord: function(timestamp, key, value) {
74      this.addProperty(timestamp, key, value);
75      this.allEvents.pop();
76    }
77  };
78
79  return PlayerInfo;
80}());
81