1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <set>
#include "base/logging.h"
#include "base/time/time.h"
#include "components/copresence/copresence_state_impl.h"
#include "components/copresence/proto/data.pb.h"
#include "components/copresence/public/copresence_constants.h"
#include "components/copresence/public/copresence_observer.h"
namespace copresence {
namespace {
template<typename TokenType>
void HandleCommonFields(const TokenType& new_token, TokenType* current_token) {
if (current_token->id.empty()) {
current_token->id = new_token.id;
current_token->medium = new_token.medium;
current_token->start_time = new_token.start_time;
} else {
DCHECK_EQ(new_token.id, current_token->id);
DCHECK_EQ(new_token.medium, current_token->medium);
DCHECK(new_token.start_time.is_null() ||
new_token.start_time == current_token->start_time);
}
}
void UpdateToken(const TransmittedToken& new_token,
TransmittedToken* current_token) {
HandleCommonFields(new_token, current_token);
current_token->stop_time = new_token.stop_time;
current_token->broadcast_confirmed = new_token.broadcast_confirmed;
}
void UpdateToken(const ReceivedToken& new_token,
ReceivedToken* current_token) {
HandleCommonFields(new_token, current_token);
current_token->last_time = new_token.last_time;
if (new_token.valid != ReceivedToken::UNKNOWN)
current_token->valid = new_token.valid;
}
} // namespace
// Public functions.
CopresenceStateImpl::CopresenceStateImpl() {}
CopresenceStateImpl::~CopresenceStateImpl() {}
void CopresenceStateImpl::AddObserver(CopresenceObserver* observer) {
DCHECK(observer);
observers_.AddObserver(observer);
}
void CopresenceStateImpl::RemoveObserver(CopresenceObserver* observer) {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
const std::vector<Directive>& CopresenceStateImpl::active_directives() const {
return active_directives_;
}
const std::map<std::string, TransmittedToken>&
CopresenceStateImpl::transmitted_tokens() const {
return transmitted_tokens_;
}
const std::map<std::string, ReceivedToken>&
CopresenceStateImpl::received_tokens() const {
return received_tokens_;
}
// TODO(ckehoe): Only send updates if the directives have really changed.
void CopresenceStateImpl::UpdateDirectives(
const std::vector<Directive>& directives) {
active_directives_ = directives;
UpdateTransmittingTokens();
FOR_EACH_OBSERVER(CopresenceObserver, observers_, DirectivesUpdated());
}
void CopresenceStateImpl::UpdateTransmittedToken(
const TransmittedToken& token) {
UpdateToken(token, &transmitted_tokens_[token.id]);
FOR_EACH_OBSERVER(CopresenceObserver,
observers_,
TokenTransmitted(transmitted_tokens_[token.id]));
}
// TODO(ckehoe): Check which tokens are no longer heard and report them lost.
void CopresenceStateImpl::UpdateReceivedToken(const ReceivedToken& token) {
DCHECK(!token.id.empty());
// TODO(ckehoe): Have CopresenceManagerImpl::AudioCheck() use this to check
// if we can hear our token, and delete the logic from the AudioManager.
if (transmitted_tokens_.count(token.id) > 0) {
transmitted_tokens_[token.id].broadcast_confirmed = true;
FOR_EACH_OBSERVER(CopresenceObserver,
observers_,
TokenTransmitted(transmitted_tokens_[token.id]));
} else {
ReceivedToken& stored_token = received_tokens_[token.id];
UpdateToken(token, &stored_token);
// The decoder doesn't track when this token was heard before,
// so it should just fill in the last_time.
// If we've never seen this token, we populate the start time too.
if (stored_token.start_time.is_null())
stored_token.start_time = token.last_time;
FOR_EACH_OBSERVER(CopresenceObserver,
observers_,
TokenReceived(stored_token));
}
}
void CopresenceStateImpl::UpdateTokenStatus(const std::string& token_id,
TokenStatus status) {
if (transmitted_tokens_.count(token_id) > 0) {
LOG_IF(ERROR, status != VALID)
<< "Broadcast token " << token_id << " is invalid";
} else if (received_tokens_.count(token_id) > 0) {
received_tokens_[token_id].valid = status == VALID ?
ReceivedToken::VALID : ReceivedToken::INVALID;
FOR_EACH_OBSERVER(CopresenceObserver,
observers_,
TokenReceived(received_tokens_[token_id]));
} else {
LOG(ERROR) << "Got status update for unrecognized token " << token_id;
}
}
// Private functions.
void CopresenceStateImpl::UpdateTransmittingTokens() {
std::set<std::string> tokens_to_update;
for (const auto& token_entry : transmitted_tokens_)
tokens_to_update.insert(token_entry.first);
for (const Directive& directive : active_directives_) {
const TokenInstruction& instruction = directive.token_instruction();
if (instruction.token_instruction_type() == TRANSMIT) {
tokens_to_update.erase(instruction.token_id());
TransmittedToken& token = transmitted_tokens_[instruction.token_id()];
token.id = instruction.token_id();
token.medium = instruction.medium();
token.start_time = base::Time::Now();
token.stop_time = base::Time::Now() +
base::TimeDelta::FromMilliseconds(directive.ttl_millis());
FOR_EACH_OBSERVER(CopresenceObserver,
observers_,
TokenTransmitted(token));
}
}
// Tokens not updated above are no longer transmitting.
base::Time now = base::Time::Now();
for (const std::string& token : tokens_to_update) {
if (transmitted_tokens_[token].stop_time > now)
transmitted_tokens_[token].stop_time = now;
FOR_EACH_OBSERVER(CopresenceObserver,
observers_,
TokenTransmitted(transmitted_tokens_[token]));
}
}
} // namespace copresence
|