| 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_impl.h" | 5 #include "components/copresence/handlers/audio/audio_directive_handler_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/time/default_tick_clock.h" | 13 #include "base/time/default_tick_clock.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 15 #include "components/copresence/handlers/audio/audio_directive_list.h" | 16 #include "components/copresence/handlers/audio/audio_directive_list.h" |
| 16 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" | 17 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h" |
| 17 #include "components/copresence/mediums/audio/audio_manager_impl.h" | 18 #include "components/copresence/mediums/audio/audio_manager_impl.h" |
| 18 #include "components/copresence/proto/data.pb.h" | 19 #include "components/copresence/proto/data.pb.h" |
| 19 #include "components/copresence/public/copresence_constants.h" | 20 #include "components/copresence/public/copresence_constants.h" |
| 20 #include "media/base/audio_bus.h" | 21 #include "media/base/audio_bus.h" |
| 21 | 22 |
| 22 namespace copresence { | 23 namespace copresence { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 base::TimeTicks GetEarliestEventTime(AudioDirectiveList* list, | 27 base::TimeTicks GetEarliestEventTime(AudioDirectiveList* list, |
| 27 base::TimeTicks event_time) { | 28 base::TimeTicks event_time) { |
| 28 if (!list->GetActiveDirective()) | 29 if (!list->GetActiveDirective()) |
| 29 return event_time; | 30 return event_time; |
| 30 | 31 |
| 31 return event_time.is_null() ? | 32 return event_time.is_null() ? |
| 32 list->GetActiveDirective()->end_time : | 33 list->GetActiveDirective()->end_time : |
| 33 std::min(list->GetActiveDirective()->end_time, event_time); | 34 std::min(list->GetActiveDirective()->end_time, event_time); |
| 34 } | 35 } |
| 35 | 36 |
| 37 void ConvertDirectives(const std::vector<AudioDirective>& in_directives, |
| 38 std::vector<Directive>* out_directives) { |
| 39 for (const AudioDirective& in_directive : in_directives) |
| 40 out_directives->push_back(in_directive.server_directive); |
| 41 } |
| 42 |
| 36 } // namespace | 43 } // namespace |
| 37 | 44 |
| 38 | 45 |
| 39 // Public functions. | 46 // Public functions. |
| 40 | 47 |
| 41 AudioDirectiveHandlerImpl::AudioDirectiveHandlerImpl() | 48 AudioDirectiveHandlerImpl::AudioDirectiveHandlerImpl( |
| 42 : audio_manager_(new AudioManagerImpl), | 49 const DirectivesCallback& update_directives_callback) |
| 50 : update_directives_callback_(update_directives_callback), |
| 51 audio_manager_(new AudioManagerImpl), |
| 43 audio_event_timer_(new base::OneShotTimer<AudioDirectiveHandler>), | 52 audio_event_timer_(new base::OneShotTimer<AudioDirectiveHandler>), |
| 44 clock_(new TickClockRefCounted(new base::DefaultTickClock)) {} | 53 clock_(new TickClockRefCounted(new base::DefaultTickClock)) {} |
| 45 | 54 |
| 46 // TODO(ckehoe): Merge these two constructors when | |
| 47 // delegating constructors are allowed. | |
| 48 AudioDirectiveHandlerImpl::AudioDirectiveHandlerImpl( | 55 AudioDirectiveHandlerImpl::AudioDirectiveHandlerImpl( |
| 56 const DirectivesCallback& update_directives_callback, |
| 49 scoped_ptr<AudioManager> audio_manager, | 57 scoped_ptr<AudioManager> audio_manager, |
| 50 scoped_ptr<base::Timer> timer, | 58 scoped_ptr<base::Timer> timer, |
| 51 const scoped_refptr<TickClockRefCounted>& clock) | 59 const scoped_refptr<TickClockRefCounted>& clock) |
| 52 : audio_manager_(audio_manager.Pass()), | 60 : update_directives_callback_(update_directives_callback), |
| 61 audio_manager_(audio_manager.Pass()), |
| 53 audio_event_timer_(timer.Pass()), | 62 audio_event_timer_(timer.Pass()), |
| 54 clock_(clock) {} | 63 clock_(clock) {} |
| 55 | 64 |
| 56 AudioDirectiveHandlerImpl::~AudioDirectiveHandlerImpl() {} | 65 AudioDirectiveHandlerImpl::~AudioDirectiveHandlerImpl() {} |
| 57 | 66 |
| 58 void AudioDirectiveHandlerImpl::Initialize(WhispernetClient* whispernet_client, | 67 void AudioDirectiveHandlerImpl::Initialize(WhispernetClient* whispernet_client, |
| 59 const TokensCallback& tokens_cb) { | 68 const TokensCallback& tokens_cb) { |
| 60 if (!audio_manager_) | 69 if (!audio_manager_) |
| 61 audio_manager_.reset(new AudioManagerImpl()); | 70 audio_manager_.reset(new AudioManagerImpl()); |
| 62 audio_manager_->Initialize(whispernet_client, tokens_cb); | 71 audio_manager_->Initialize(whispernet_client, tokens_cb); |
| 63 | 72 |
| 64 DCHECK(transmits_lists_.empty()); | 73 DCHECK(transmits_lists_.empty()); |
| 65 transmits_lists_.push_back(new AudioDirectiveList(clock_)); | 74 transmits_lists_.push_back(new AudioDirectiveList(clock_)); |
| 66 transmits_lists_.push_back(new AudioDirectiveList(clock_)); | 75 transmits_lists_.push_back(new AudioDirectiveList(clock_)); |
| 67 | 76 |
| 68 DCHECK(receives_lists_.empty()); | 77 DCHECK(receives_lists_.empty()); |
| 69 receives_lists_.push_back(new AudioDirectiveList(clock_)); | 78 receives_lists_.push_back(new AudioDirectiveList(clock_)); |
| 70 receives_lists_.push_back(new AudioDirectiveList(clock_)); | 79 receives_lists_.push_back(new AudioDirectiveList(clock_)); |
| 71 } | 80 } |
| 72 | 81 |
| 73 void AudioDirectiveHandlerImpl::AddInstruction( | 82 void AudioDirectiveHandlerImpl::AddInstruction( |
| 74 const TokenInstruction& instruction, | 83 const Directive& directive, |
| 75 const std::string& op_id, | 84 const std::string& op_id) { |
| 76 base::TimeDelta ttl) { | |
| 77 DCHECK(transmits_lists_.size() == 2u && receives_lists_.size() == 2u) | 85 DCHECK(transmits_lists_.size() == 2u && receives_lists_.size() == 2u) |
| 78 << "Call Initialize() before other AudioDirectiveHandler methods"; | 86 << "Call Initialize() before other AudioDirectiveHandler methods"; |
| 79 | 87 |
| 88 const TokenInstruction& instruction = directive.token_instruction(); |
| 89 base::TimeDelta ttl = |
| 90 base::TimeDelta::FromMilliseconds(directive.ttl_millis()); |
| 91 |
| 80 switch (instruction.token_instruction_type()) { | 92 switch (instruction.token_instruction_type()) { |
| 81 case TRANSMIT: | 93 case TRANSMIT: |
| 82 DVLOG(2) << "Audio Transmit Directive received. Token: " | 94 DVLOG(2) << "Audio Transmit Directive received. Token: " |
| 83 << instruction.token_id() | 95 << instruction.token_id() |
| 84 << " with medium=" << instruction.medium() | 96 << " with medium=" << instruction.medium() |
| 85 << " with TTL=" << ttl.InMilliseconds(); | 97 << " with TTL=" << ttl.InMilliseconds(); |
| 86 switch (instruction.medium()) { | 98 switch (instruction.medium()) { |
| 87 case AUDIO_ULTRASOUND_PASSBAND: | 99 case AUDIO_ULTRASOUND_PASSBAND: |
| 88 transmits_lists_[INAUDIBLE]->AddDirective(op_id, ttl); | 100 transmits_lists_[INAUDIBLE]->AddDirective(op_id, directive); |
| 89 audio_manager_->SetToken(INAUDIBLE, instruction.token_id()); | 101 audio_manager_->SetToken(INAUDIBLE, instruction.token_id()); |
| 90 break; | 102 break; |
| 91 case AUDIO_AUDIBLE_DTMF: | 103 case AUDIO_AUDIBLE_DTMF: |
| 92 transmits_lists_[AUDIBLE]->AddDirective(op_id, ttl); | 104 transmits_lists_[AUDIBLE]->AddDirective(op_id, directive); |
| 93 audio_manager_->SetToken(AUDIBLE, instruction.token_id()); | 105 audio_manager_->SetToken(AUDIBLE, instruction.token_id()); |
| 94 break; | 106 break; |
| 95 default: | 107 default: |
| 96 NOTREACHED(); | 108 NOTREACHED(); |
| 97 } | 109 } |
| 98 break; | 110 break; |
| 99 case RECEIVE: | 111 case RECEIVE: |
| 100 DVLOG(2) << "Audio Receive Directive received." | 112 DVLOG(2) << "Audio Receive Directive received." |
| 101 << " with medium=" << instruction.medium() | 113 << " with medium=" << instruction.medium() |
| 102 << " with TTL=" << ttl.InMilliseconds(); | 114 << " with TTL=" << ttl.InMilliseconds(); |
| 103 switch (instruction.medium()) { | 115 switch (instruction.medium()) { |
| 104 case AUDIO_ULTRASOUND_PASSBAND: | 116 case AUDIO_ULTRASOUND_PASSBAND: |
| 105 receives_lists_[INAUDIBLE]->AddDirective(op_id, ttl); | 117 receives_lists_[INAUDIBLE]->AddDirective(op_id, directive); |
| 106 break; | 118 break; |
| 107 case AUDIO_AUDIBLE_DTMF: | 119 case AUDIO_AUDIBLE_DTMF: |
| 108 receives_lists_[AUDIBLE]->AddDirective(op_id, ttl); | 120 receives_lists_[AUDIBLE]->AddDirective(op_id, directive); |
| 109 break; | 121 break; |
| 110 default: | 122 default: |
| 111 NOTREACHED(); | 123 NOTREACHED(); |
| 112 } | 124 } |
| 113 break; | 125 break; |
| 114 case UNKNOWN_TOKEN_INSTRUCTION_TYPE: | 126 case UNKNOWN_TOKEN_INSTRUCTION_TYPE: |
| 115 default: | 127 default: |
| 116 LOG(WARNING) << "Unknown Audio Transmit Directive received. type = " | 128 LOG(WARNING) << "Unknown Audio Transmit Directive received. type = " |
| 117 << instruction.token_instruction_type(); | 129 << instruction.token_instruction_type(); |
| 118 } | 130 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 133 | 145 |
| 134 const std::string AudioDirectiveHandlerImpl::PlayingToken( | 146 const std::string AudioDirectiveHandlerImpl::PlayingToken( |
| 135 AudioType type) const { | 147 AudioType type) const { |
| 136 return audio_manager_->GetToken(type); | 148 return audio_manager_->GetToken(type); |
| 137 } | 149 } |
| 138 | 150 |
| 139 bool AudioDirectiveHandlerImpl::IsPlayingTokenHeard(AudioType type) const { | 151 bool AudioDirectiveHandlerImpl::IsPlayingTokenHeard(AudioType type) const { |
| 140 return audio_manager_->IsPlayingTokenHeard(type); | 152 return audio_manager_->IsPlayingTokenHeard(type); |
| 141 } | 153 } |
| 142 | 154 |
| 155 |
| 143 // Private functions. | 156 // Private functions. |
| 144 | 157 |
| 145 void AudioDirectiveHandlerImpl::ProcessNextInstruction() { | 158 void AudioDirectiveHandlerImpl::ProcessNextInstruction() { |
| 146 DCHECK(audio_event_timer_); | 159 DCHECK(audio_event_timer_); |
| 147 audio_event_timer_->Stop(); | 160 audio_event_timer_->Stop(); |
| 148 | 161 |
| 149 // Change |audio_manager_| state for audible transmits. | 162 // Change |audio_manager_| state for audible transmits. |
| 150 if (transmits_lists_[AUDIBLE]->GetActiveDirective()) | 163 if (transmits_lists_[AUDIBLE]->GetActiveDirective()) |
| 151 audio_manager_->StartPlaying(AUDIBLE); | 164 audio_manager_->StartPlaying(AUDIBLE); |
| 152 else | 165 else |
| (...skipping 18 matching lines...) Expand all Loading... |
| 171 audio_manager_->StopRecording(INAUDIBLE); | 184 audio_manager_->StopRecording(INAUDIBLE); |
| 172 | 185 |
| 173 base::TimeTicks next_event_time; | 186 base::TimeTicks next_event_time; |
| 174 if (GetNextInstructionExpiry(&next_event_time)) { | 187 if (GetNextInstructionExpiry(&next_event_time)) { |
| 175 audio_event_timer_->Start( | 188 audio_event_timer_->Start( |
| 176 FROM_HERE, | 189 FROM_HERE, |
| 177 next_event_time - clock_->NowTicks(), | 190 next_event_time - clock_->NowTicks(), |
| 178 base::Bind(&AudioDirectiveHandlerImpl::ProcessNextInstruction, | 191 base::Bind(&AudioDirectiveHandlerImpl::ProcessNextInstruction, |
| 179 base::Unretained(this))); | 192 base::Unretained(this))); |
| 180 } | 193 } |
| 194 |
| 195 // TODO(crbug.com/436584): Instead of this, store the directives |
| 196 // in a single list, and prune them when expired. |
| 197 std::vector<Directive> directives; |
| 198 ConvertDirectives(transmits_lists_[AUDIBLE]->directives(), &directives); |
| 199 ConvertDirectives(transmits_lists_[INAUDIBLE]->directives(), &directives); |
| 200 ConvertDirectives(receives_lists_[AUDIBLE]->directives(), &directives); |
| 201 ConvertDirectives(receives_lists_[INAUDIBLE]->directives(), &directives); |
| 202 update_directives_callback_.Run(directives); |
| 181 } | 203 } |
| 182 | 204 |
| 183 bool AudioDirectiveHandlerImpl::GetNextInstructionExpiry( | 205 bool AudioDirectiveHandlerImpl::GetNextInstructionExpiry( |
| 184 base::TimeTicks* expiry) { | 206 base::TimeTicks* expiry) { |
| 185 DCHECK(expiry); | 207 DCHECK(expiry); |
| 186 | 208 |
| 187 *expiry = GetEarliestEventTime(transmits_lists_[AUDIBLE], base::TimeTicks()); | 209 *expiry = GetEarliestEventTime(transmits_lists_[AUDIBLE], base::TimeTicks()); |
| 188 *expiry = GetEarliestEventTime(transmits_lists_[INAUDIBLE], *expiry); | 210 *expiry = GetEarliestEventTime(transmits_lists_[INAUDIBLE], *expiry); |
| 189 *expiry = GetEarliestEventTime(receives_lists_[AUDIBLE], *expiry); | 211 *expiry = GetEarliestEventTime(receives_lists_[AUDIBLE], *expiry); |
| 190 *expiry = GetEarliestEventTime(receives_lists_[INAUDIBLE], *expiry); | 212 *expiry = GetEarliestEventTime(receives_lists_[INAUDIBLE], *expiry); |
| 191 | 213 |
| 192 return !expiry->is_null(); | 214 return !expiry->is_null(); |
| 193 } | 215 } |
| 194 | 216 |
| 195 } // namespace copresence | 217 } // namespace copresence |
| OLD | NEW |