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