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 namespace copresence { | 7 namespace copresence { |
8 | 8 |
9 // Public functions. | 9 // Public functions. |
10 | 10 |
11 AudioDirective::AudioDirective() {} | 11 AudioDirective::AudioDirective() {} |
12 | 12 |
13 AudioDirective::AudioDirective(const std::string& op_id, | 13 AudioDirective::AudioDirective(const std::string& op_id, |
14 base::TimeTicks end_time, | 14 base::TimeTicks end_time) |
15 const Directive& server_directive) | 15 : op_id(op_id), end_time(end_time) {} |
16 : op_id(op_id), | |
17 end_time(end_time), | |
18 server_directive(server_directive) {} | |
19 | 16 |
20 AudioDirectiveList::AudioDirectiveList( | 17 AudioDirectiveList::AudioDirectiveList( |
21 const scoped_refptr<TickClockRefCounted>& clock) | 18 const scoped_refptr<TickClockRefCounted>& clock) |
22 : clock_(clock) {} | 19 : clock_(clock) {} |
23 | 20 |
24 AudioDirectiveList::~AudioDirectiveList() {} | 21 AudioDirectiveList::~AudioDirectiveList() {} |
25 | 22 |
26 void AudioDirectiveList::AddDirective(const std::string& op_id, | 23 void AudioDirectiveList::AddDirective(const std::string& op_id, |
27 const Directive& server_directive) { | 24 base::TimeDelta ttl) { |
28 base::TimeTicks end_time = clock_->NowTicks() + | 25 base::TimeTicks end_time = clock_->NowTicks() + ttl; |
29 base::TimeDelta::FromMilliseconds(server_directive.ttl_millis()); | |
30 | 26 |
31 // If this op is already in the list, update it instead of adding it again. | 27 // If this op is already in the list, update it instead of adding it again. |
32 auto it = FindDirectiveByOpId(op_id); | 28 auto it = FindDirectiveByOpId(op_id); |
33 if (it != active_directives_.end()) { | 29 if (it != active_directives_.end()) { |
34 it->end_time = end_time; | 30 it->end_time = end_time; |
35 std::make_heap(active_directives_.begin(), | 31 std::make_heap(active_directives_.begin(), |
36 active_directives_.end(), | 32 active_directives_.end(), |
37 LatestFirstComparator()); | 33 LatestFirstComparator()); |
38 return; | 34 return; |
39 } | 35 } |
40 | 36 |
41 active_directives_.push_back( | 37 active_directives_.push_back(AudioDirective(op_id, end_time)); |
42 AudioDirective(op_id, end_time, server_directive)); | |
43 std::push_heap(active_directives_.begin(), | 38 std::push_heap(active_directives_.begin(), |
44 active_directives_.end(), | 39 active_directives_.end(), |
45 LatestFirstComparator()); | 40 LatestFirstComparator()); |
46 } | 41 } |
47 | 42 |
48 void AudioDirectiveList::RemoveDirective(const std::string& op_id) { | 43 void AudioDirectiveList::RemoveDirective(const std::string& op_id) { |
49 auto it = FindDirectiveByOpId(op_id); | 44 auto it = FindDirectiveByOpId(op_id); |
50 if (it != active_directives_.end()) | 45 if (it != active_directives_.end()) |
51 active_directives_.erase(it); | 46 active_directives_.erase(it); |
52 | 47 |
53 std::make_heap(active_directives_.begin(), | 48 std::make_heap(active_directives_.begin(), |
54 active_directives_.end(), | 49 active_directives_.end(), |
55 LatestFirstComparator()); | 50 LatestFirstComparator()); |
56 } | 51 } |
57 | 52 |
58 scoped_ptr<AudioDirective> AudioDirectiveList::GetActiveDirective() { | 53 scoped_ptr<AudioDirective> AudioDirectiveList::GetActiveDirective() { |
59 // The top is always the instruction that is ending the latest. | 54 // The top is always the instruction that is ending the latest. |
60 // If that time has passed, all our previous instructions have expired too. | 55 // If that time has passed, all our previous instructions have expired too. |
61 // So we clear the list. | 56 // So we clear the list. |
62 if (active_directives_.empty() || | 57 if (active_directives_.empty() || |
63 active_directives_.front().end_time < clock_->NowTicks()) { | 58 active_directives_.front().end_time < clock_->NowTicks()) { |
64 active_directives_.clear(); | 59 active_directives_.clear(); |
65 return scoped_ptr<AudioDirective>().Pass(); | 60 return scoped_ptr<AudioDirective>().Pass(); |
66 } | 61 } |
67 | 62 |
68 return make_scoped_ptr(new AudioDirective(active_directives_.front())); | 63 return make_scoped_ptr(new AudioDirective(active_directives_.front())); |
69 } | 64 } |
70 | 65 |
71 const std::vector<AudioDirective>& AudioDirectiveList::directives() const { | |
72 return active_directives_; | |
73 } | |
74 | |
75 | 66 |
76 // Private functions. | 67 // Private functions. |
77 | 68 |
78 std::vector<AudioDirective>::iterator AudioDirectiveList::FindDirectiveByOpId( | 69 std::vector<AudioDirective>::iterator AudioDirectiveList::FindDirectiveByOpId( |
79 const std::string& op_id) { | 70 const std::string& op_id) { |
80 for (auto it = active_directives_.begin(); | 71 for (auto it = active_directives_.begin(); |
81 it != active_directives_.end(); | 72 it != active_directives_.end(); |
82 ++it) { | 73 ++it) { |
83 if (it->op_id == op_id) | 74 if (it->op_id == op_id) |
84 return it; | 75 return it; |
85 } | 76 } |
86 return active_directives_.end(); | 77 return active_directives_.end(); |
87 } | 78 } |
88 | 79 |
89 } // namespace copresence | 80 } // namespace copresence |
OLD | NEW |