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