Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Side by Side Diff: components/copresence/handlers/audio/audio_directive_handler_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698