| 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_handler.h" | 5 #include "components/copresence/handlers/audio/audio_directive_handler.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" | 10 #include "base/time/default_tick_clock.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
| 13 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" |
| 13 #include "components/copresence/mediums/audio/audio_manager_impl.h" | 14 #include "components/copresence/mediums/audio/audio_manager_impl.h" |
| 14 #include "components/copresence/proto/data.pb.h" | 15 #include "components/copresence/proto/data.pb.h" |
| 15 #include "components/copresence/public/copresence_constants.h" | 16 #include "components/copresence/public/copresence_constants.h" |
| 16 #include "media/base/audio_bus.h" | 17 #include "media/base/audio_bus.h" |
| 17 | 18 |
| 18 namespace copresence { | 19 namespace copresence { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 base::TimeTicks GetEarliestEventTime(AudioDirectiveList* list, | 23 base::TimeTicks GetEarliestEventTime(AudioDirectiveList* list, |
| 23 base::TimeTicks event_time) { | 24 base::TimeTicks event_time) { |
| 24 if (!list->GetActiveDirective()) | 25 if (!list->GetActiveDirective()) |
| 25 return event_time; | 26 return event_time; |
| 26 | 27 |
| 27 if (event_time.is_null()) | 28 if (event_time.is_null()) |
| 28 return list->GetActiveDirective()->end_time; | 29 return list->GetActiveDirective()->end_time; |
| 29 else | 30 else |
| 30 return std::min(list->GetActiveDirective()->end_time, event_time); | 31 return std::min(list->GetActiveDirective()->end_time, event_time); |
| 31 } | 32 } |
| 32 | 33 |
| 33 } // namespace | 34 } // namespace |
| 34 | 35 |
| 35 // Public methods. | 36 // Public methods. |
| 36 | 37 |
| 37 AudioDirectiveHandler::AudioDirectiveHandler() | 38 AudioDirectiveHandler::AudioDirectiveHandler() |
| 38 : audio_event_timer_(new base::OneShotTimer<AudioDirectiveHandler>), | 39 : audio_event_timer_(new base::OneShotTimer<AudioDirectiveHandler>), |
| 39 clock_(new base::DefaultTickClock) { | 40 clock_(new TickClockRefCounted( |
| 41 make_scoped_ptr(new base::DefaultTickClock))) { |
| 40 } | 42 } |
| 41 | 43 |
| 42 AudioDirectiveHandler::~AudioDirectiveHandler() { | 44 AudioDirectiveHandler::~AudioDirectiveHandler() { |
| 43 } | 45 } |
| 44 | 46 |
| 45 void AudioDirectiveHandler::Initialize( | 47 void AudioDirectiveHandler::Initialize( |
| 46 const AudioManager::DecodeSamplesCallback& decode_cb, | 48 const AudioManager::DecodeSamplesCallback& decode_cb, |
| 47 const AudioManager::EncodeTokenCallback& encode_cb) { | 49 const AudioManager::EncodeTokenCallback& encode_cb) { |
| 48 if (!audio_manager_) | 50 if (!audio_manager_) |
| 49 audio_manager_.reset(new AudioManagerImpl()); | 51 audio_manager_.reset(new AudioManagerImpl()); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 transmits_list_[INAUDIBLE].RemoveDirective(op_id); | 102 transmits_list_[INAUDIBLE].RemoveDirective(op_id); |
| 101 receives_list_[AUDIBLE].RemoveDirective(op_id); | 103 receives_list_[AUDIBLE].RemoveDirective(op_id); |
| 102 receives_list_[INAUDIBLE].RemoveDirective(op_id); | 104 receives_list_[INAUDIBLE].RemoveDirective(op_id); |
| 103 | 105 |
| 104 ProcessNextInstruction(); | 106 ProcessNextInstruction(); |
| 105 } | 107 } |
| 106 | 108 |
| 107 const std::string AudioDirectiveHandler::PlayingToken(AudioType type) const { | 109 const std::string AudioDirectiveHandler::PlayingToken(AudioType type) const { |
| 108 return audio_manager_->GetToken(type); | 110 return audio_manager_->GetToken(type); |
| 109 } | 111 } |
| 112 |
| 113 void AudioDirectiveHandler::set_clock_for_testing( |
| 114 const scoped_refptr<TickClockRefCounted>& clock) { |
| 115 clock_ = clock; |
| 116 |
| 117 transmits_list_[AUDIBLE].set_clock_for_testing(clock); |
| 118 transmits_list_[INAUDIBLE].set_clock_for_testing(clock); |
| 119 receives_list_[AUDIBLE].set_clock_for_testing(clock); |
| 120 receives_list_[INAUDIBLE].set_clock_for_testing(clock); |
| 121 } |
| 122 |
| 123 void AudioDirectiveHandler::set_timer_for_testing( |
| 124 scoped_ptr<base::Timer> timer) { |
| 125 audio_event_timer_.swap(timer); |
| 126 } |
| 127 |
| 110 // Private methods. | 128 // Private methods. |
| 111 | 129 |
| 112 void AudioDirectiveHandler::ProcessNextInstruction() { | 130 void AudioDirectiveHandler::ProcessNextInstruction() { |
| 113 DCHECK(audio_event_timer_); | 131 DCHECK(audio_event_timer_); |
| 114 audio_event_timer_->Stop(); | 132 audio_event_timer_->Stop(); |
| 115 | 133 |
| 116 // Change |audio_manager_| state for audible transmits. | 134 // Change |audio_manager_| state for audible transmits. |
| 117 if (transmits_list_[AUDIBLE].GetActiveDirective()) | 135 if (transmits_list_[AUDIBLE].GetActiveDirective()) |
| 118 audio_manager_->StartPlaying(AUDIBLE); | 136 audio_manager_->StartPlaying(AUDIBLE); |
| 119 else | 137 else |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 170 |
| 153 *expiry = GetEarliestEventTime(&transmits_list_[AUDIBLE], base::TimeTicks()); | 171 *expiry = GetEarliestEventTime(&transmits_list_[AUDIBLE], base::TimeTicks()); |
| 154 *expiry = GetEarliestEventTime(&transmits_list_[INAUDIBLE], *expiry); | 172 *expiry = GetEarliestEventTime(&transmits_list_[INAUDIBLE], *expiry); |
| 155 *expiry = GetEarliestEventTime(&receives_list_[AUDIBLE], *expiry); | 173 *expiry = GetEarliestEventTime(&receives_list_[AUDIBLE], *expiry); |
| 156 *expiry = GetEarliestEventTime(&receives_list_[INAUDIBLE], *expiry); | 174 *expiry = GetEarliestEventTime(&receives_list_[INAUDIBLE], *expiry); |
| 157 | 175 |
| 158 return !expiry->is_null(); | 176 return !expiry->is_null(); |
| 159 } | 177 } |
| 160 | 178 |
| 161 } // namespace copresence | 179 } // namespace copresence |
| OLD | NEW |