1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70

content / browser / resources / media / player_info.js [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview A class for keeping track of the details of a player.
 */

export class PlayerInfo {
  /**
   * A class that keeps track of properties on a media player.
   * @param id A unique id that can be used to identify this player.
   */
  constructor(id) {
    this.id = id;
    // The current value of the properties for this player.
    this.properties = {};

    // Every single event in the order in which they were received.
    this.allEvents = [];
    this.lastRendered = 0;

    this.firstTimestamp_ = -1;
  }

  /**
   * Adds or set a property on this player.
   * This is the default logging method as it keeps track of old values.
   * @param timestamp  The time in milliseconds since the Epoch.
   * @param key A String key that describes the property.
   * @param value The value of the property.
   */
  addProperty(timestamp, key, value) {
    // The first timestamp that we get will be recorded.
    // Then, all future timestamps are deltas of that.
    if (this.firstTimestamp_ === -1) {
      this.firstTimestamp_ = timestamp;
    }

    if (typeof key !== 'string') {
      throw new Error(typeof key + ' is not a valid key type');
    }

    this.properties[key] = value;

    const recordValue = {
      time: timestamp - this.firstTimestamp_,
      key: key,
      value: value,
    };

    this.allEvents.push(recordValue);
  }

  /**
   * Adds or set a property on this player.
   * Does not keep track of old values.  This is better for
   * values that get spammed repeatedly.
   * @param timestamp The time in milliseconds since the Epoch.
   * @param key A String key that describes the property.
   * @param value The value of the property.
   */
  addPropertyNoRecord(timestamp, key, value) {
    this.addProperty(timestamp, key, value);
    this.allEvents.pop();
  }
}

// Exporting the class on |window| for tests.
window.PlayerInfo = PlayerInfo;