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 "components/copresence/handlers/audio/audio_directive_list.h" | 5 #include "components/copresence/handlers/audio/audio_directive_list.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/time/default_tick_clock.h" |
| 11 #include "base/time/tick_clock.h" |
10 #include "base/time/time.h" | 12 #include "base/time/time.h" |
11 | 13 |
12 namespace copresence { | 14 namespace copresence { |
13 | 15 |
14 // Public methods. | 16 // Public methods. |
15 | 17 |
16 AudioDirective::AudioDirective() { | 18 AudioDirective::AudioDirective() { |
17 } | 19 } |
18 | 20 |
19 AudioDirective::AudioDirective(const std::string& op_id, base::Time end_time) | 21 AudioDirective::AudioDirective(const std::string& op_id, |
| 22 base::TimeTicks end_time) |
20 : op_id(op_id), end_time(end_time) { | 23 : op_id(op_id), end_time(end_time) { |
21 } | 24 } |
22 | 25 |
23 AudioDirectiveList::AudioDirectiveList() { | 26 AudioDirectiveList::AudioDirectiveList() : clock_(new base::DefaultTickClock) { |
24 } | 27 } |
25 | 28 |
26 AudioDirectiveList::~AudioDirectiveList() { | 29 AudioDirectiveList::~AudioDirectiveList() { |
27 } | 30 } |
28 | 31 |
29 void AudioDirectiveList::AddDirective(const std::string& op_id, | 32 void AudioDirectiveList::AddDirective(const std::string& op_id, |
30 base::TimeDelta ttl) { | 33 base::TimeDelta ttl) { |
31 base::Time end_time = base::Time::Now() + ttl; | 34 base::TimeTicks end_time = clock_->NowTicks() + ttl; |
32 | 35 |
33 // In case this op is already in the list, update it instead of adding | 36 // In case this op is already in the list, update it instead of adding |
34 // it again. | 37 // it again. |
35 std::vector<AudioDirective>::iterator it = FindDirectiveByOpId(op_id); | 38 std::vector<AudioDirective>::iterator it = FindDirectiveByOpId(op_id); |
36 if (it != active_directives_.end()) { | 39 if (it != active_directives_.end()) { |
37 it->end_time = end_time; | 40 it->end_time = end_time; |
38 std::make_heap(active_directives_.begin(), | 41 std::make_heap(active_directives_.begin(), |
39 active_directives_.end(), | 42 active_directives_.end(), |
40 LatestFirstComparator()); | 43 LatestFirstComparator()); |
41 return; | 44 return; |
(...skipping 13 matching lines...) Expand all Loading... |
55 std::make_heap(active_directives_.begin(), | 58 std::make_heap(active_directives_.begin(), |
56 active_directives_.end(), | 59 active_directives_.end(), |
57 LatestFirstComparator()); | 60 LatestFirstComparator()); |
58 } | 61 } |
59 | 62 |
60 scoped_ptr<AudioDirective> AudioDirectiveList::GetActiveDirective() { | 63 scoped_ptr<AudioDirective> AudioDirectiveList::GetActiveDirective() { |
61 // The top is always the instruction that is ending the latest. If that time | 64 // The top is always the instruction that is ending the latest. If that time |
62 // has passed, means all our previous instructions have expired too, hence | 65 // has passed, means all our previous instructions have expired too, hence |
63 // clear the list. | 66 // clear the list. |
64 if (!active_directives_.empty() && | 67 if (!active_directives_.empty() && |
65 active_directives_.front().end_time < base::Time::Now()) { | 68 active_directives_.front().end_time < clock_->NowTicks()) { |
66 active_directives_.clear(); | 69 active_directives_.clear(); |
67 } | 70 } |
68 | 71 |
69 if (active_directives_.empty()) | 72 if (active_directives_.empty()) |
70 return make_scoped_ptr<AudioDirective>(NULL); | 73 return make_scoped_ptr<AudioDirective>(NULL); |
71 | 74 |
72 return make_scoped_ptr(new AudioDirective(active_directives_.front())); | 75 return make_scoped_ptr(new AudioDirective(active_directives_.front())); |
73 } | 76 } |
74 | 77 |
75 std::vector<AudioDirective>::iterator AudioDirectiveList::FindDirectiveByOpId( | 78 std::vector<AudioDirective>::iterator AudioDirectiveList::FindDirectiveByOpId( |
76 const std::string& op_id) { | 79 const std::string& op_id) { |
77 for (std::vector<AudioDirective>::iterator it = active_directives_.begin(); | 80 for (std::vector<AudioDirective>::iterator it = active_directives_.begin(); |
78 it != active_directives_.end(); | 81 it != active_directives_.end(); |
79 ++it) { | 82 ++it) { |
80 if (it->op_id == op_id) | 83 if (it->op_id == op_id) |
81 return it; | 84 return it; |
82 } | 85 } |
83 return active_directives_.end(); | 86 return active_directives_.end(); |
84 } | 87 } |
85 | 88 |
86 } // namespace copresence | 89 } // namespace copresence |
OLD | NEW |