OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cdm/player_tracker_impl.h" | 5 #include "media/cdm/player_tracker_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 | 10 |
11 namespace media { | 11 namespace media { |
12 | 12 |
13 PlayerTrackerImpl::PlayerCallbacks::PlayerCallbacks( | 13 PlayerTrackerImpl::PlayerCallbacks::PlayerCallbacks( |
14 base::Closure new_key_cb, | 14 const base::Closure& new_key_cb, |
15 base::Closure cdm_unset_cb) | 15 const base::Closure& cdm_unset_cb) |
16 : new_key_cb(new_key_cb), cdm_unset_cb(cdm_unset_cb) { | 16 : new_key_cb(new_key_cb), cdm_unset_cb(cdm_unset_cb) {} |
17 } | |
18 | 17 |
19 PlayerTrackerImpl::PlayerCallbacks::~PlayerCallbacks() { | 18 PlayerTrackerImpl::PlayerCallbacks::~PlayerCallbacks() { |
20 } | 19 } |
21 | 20 |
22 PlayerTrackerImpl::PlayerTrackerImpl() : next_registration_id_(1) { | 21 PlayerTrackerImpl::PlayerTrackerImpl() : next_registration_id_(1) { |
23 // Enable PlayerTrackerImpl to be created on another thread than it will be | |
24 // later exclusively used. | |
25 thread_checker_.DetachFromThread(); | |
26 } | 22 } |
27 | 23 |
28 PlayerTrackerImpl::~PlayerTrackerImpl() {} | 24 PlayerTrackerImpl::~PlayerTrackerImpl() {} |
29 | 25 |
30 int PlayerTrackerImpl::RegisterPlayer(const base::Closure& new_key_cb, | 26 int PlayerTrackerImpl::RegisterPlayer(const base::Closure& new_key_cb, |
31 const base::Closure& cdm_unset_cb) { | 27 const base::Closure& cdm_unset_cb) { |
32 DCHECK(thread_checker_.CalledOnValidThread()); | 28 base::AutoLock lock(lock_); |
33 int registration_id = next_registration_id_++; | 29 int registration_id = next_registration_id_++; |
34 DCHECK(!ContainsKey(player_callbacks_map_, registration_id)); | 30 DCHECK(!ContainsKey(player_callbacks_map_, registration_id)); |
35 player_callbacks_map_.insert(std::make_pair( | 31 player_callbacks_map_.insert(std::make_pair( |
36 registration_id, PlayerCallbacks(new_key_cb, cdm_unset_cb))); | 32 registration_id, PlayerCallbacks(new_key_cb, cdm_unset_cb))); |
37 return registration_id; | 33 return registration_id; |
38 } | 34 } |
39 | 35 |
40 void PlayerTrackerImpl::UnregisterPlayer(int registration_id) { | 36 void PlayerTrackerImpl::UnregisterPlayer(int registration_id) { |
41 DCHECK(thread_checker_.CalledOnValidThread()); | 37 base::AutoLock lock(lock_); |
42 DCHECK(ContainsKey(player_callbacks_map_, registration_id)) | 38 DCHECK(ContainsKey(player_callbacks_map_, registration_id)) |
43 << registration_id; | 39 << registration_id; |
44 player_callbacks_map_.erase(registration_id); | 40 player_callbacks_map_.erase(registration_id); |
45 } | 41 } |
46 | 42 |
47 void PlayerTrackerImpl::NotifyNewKey() { | 43 void PlayerTrackerImpl::NotifyNewKey() { |
48 DCHECK(thread_checker_.CalledOnValidThread()); | 44 std::vector<base::Closure> new_key_callbacks; |
49 std::map<int, PlayerCallbacks>::iterator iter = player_callbacks_map_.begin(); | 45 |
50 for (; iter != player_callbacks_map_.end(); ++iter) | 46 { |
51 iter->second.new_key_cb.Run(); | 47 base::AutoLock lock(lock_); |
| 48 for (const auto& entry : player_callbacks_map_) |
| 49 new_key_callbacks.push_back(entry.second.new_key_cb); |
| 50 } |
| 51 |
| 52 for (const auto& cb : new_key_callbacks) |
| 53 cb.Run(); |
52 } | 54 } |
53 | 55 |
54 void PlayerTrackerImpl::NotifyCdmUnset() { | 56 void PlayerTrackerImpl::NotifyCdmUnset() { |
55 DCHECK(thread_checker_.CalledOnValidThread()); | 57 std::vector<base::Closure> cdm_unset_callbacks; |
56 std::map<int, PlayerCallbacks>::iterator iter = player_callbacks_map_.begin(); | 58 |
57 for (; iter != player_callbacks_map_.end(); ++iter) | 59 { |
58 iter->second.cdm_unset_cb.Run(); | 60 base::AutoLock lock(lock_); |
| 61 for (const auto& entry : player_callbacks_map_) |
| 62 cdm_unset_callbacks.push_back(entry.second.cdm_unset_cb); |
| 63 } |
| 64 |
| 65 for (const auto& cb : cdm_unset_callbacks) |
| 66 cb.Run(); |
59 } | 67 } |
60 | 68 |
61 } // namespace media | 69 } // namespace media |
OLD | NEW |