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