summaryrefslogtreecommitdiffstats
path: root/media/cdm/player_tracker_impl.cc
blob: 7d5cda3bd053c31ceef9b308c8e7166ad2c16dc2 (plain)
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
71
72
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/cdm/player_tracker_impl.h"

#include <utility>

#include "base/stl_util.h"

namespace media {

PlayerTrackerImpl::PlayerCallbacks::PlayerCallbacks(
    const base::Closure& new_key_cb,
    const base::Closure& cdm_unset_cb)
    : new_key_cb(new_key_cb), cdm_unset_cb(cdm_unset_cb) {}

PlayerTrackerImpl::PlayerCallbacks::PlayerCallbacks(
    const PlayerCallbacks& other) = default;

PlayerTrackerImpl::PlayerCallbacks::~PlayerCallbacks() {
}

PlayerTrackerImpl::PlayerTrackerImpl() : next_registration_id_(1) {
}

PlayerTrackerImpl::~PlayerTrackerImpl() {}

int PlayerTrackerImpl::RegisterPlayer(const base::Closure& new_key_cb,
                                      const base::Closure& cdm_unset_cb) {
  base::AutoLock lock(lock_);
  int registration_id = next_registration_id_++;
  DCHECK(!ContainsKey(player_callbacks_map_, registration_id));
  player_callbacks_map_.insert(std::make_pair(
      registration_id, PlayerCallbacks(new_key_cb, cdm_unset_cb)));
  return registration_id;
}

void PlayerTrackerImpl::UnregisterPlayer(int registration_id) {
  base::AutoLock lock(lock_);
  DCHECK(ContainsKey(player_callbacks_map_, registration_id))
      << registration_id;
  player_callbacks_map_.erase(registration_id);
}

void PlayerTrackerImpl::NotifyNewKey() {
  std::vector<base::Closure> new_key_callbacks;

  {
    base::AutoLock lock(lock_);
    for (const auto& entry : player_callbacks_map_)
      new_key_callbacks.push_back(entry.second.new_key_cb);
  }

  for (const auto& cb : new_key_callbacks)
    cb.Run();
}

void PlayerTrackerImpl::NotifyCdmUnset() {
  std::vector<base::Closure> cdm_unset_callbacks;

  {
    base::AutoLock lock(lock_);
    for (const auto& entry : player_callbacks_map_)
      cdm_unset_callbacks.push_back(entry.second.cdm_unset_cb);
  }

  for (const auto& cb : cdm_unset_callbacks)
    cb.Run();
}

}  // namespace media