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
|
// Copyright 2015 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 "chrome/browser/ui/webui/media_router/query_result_manager.h"
#include "base/containers/hash_tables.h"
#include "base/stl_util.h"
#include "chrome/browser/media/router/media_router.h"
#include "chrome/browser/media/router/media_sinks_observer.h"
namespace media_router {
// MediaSinkObserver that propagates results back to |result_manager|.
// An instance of this class is associated with each registered MediaCastMode.
class QueryResultManager::CastModeMediaSinksObserver
: public MediaSinksObserver {
public:
CastModeMediaSinksObserver(MediaCastMode cast_mode,
const MediaSource& source,
MediaRouter* router,
QueryResultManager* result_manager)
: MediaSinksObserver(router, source),
cast_mode_(cast_mode),
result_manager_(result_manager) {
DCHECK(result_manager);
}
~CastModeMediaSinksObserver() override {}
// MediaSinksObserver
void OnSinksReceived(const std::vector<MediaSink>& result) override {
latest_sink_ids_.clear();
for (const MediaSink& sink : result) {
latest_sink_ids_.push_back(sink.id());
}
result_manager_->UpdateWithSinksQueryResult(cast_mode_, result);
result_manager_->NotifyOnResultsUpdated();
}
// Returns the most recent sink IDs that were passed to |OnSinksReceived|.
void GetLatestSinkIds(std::vector<MediaSink::Id>* sink_ids) const {
DCHECK(sink_ids);
*sink_ids = latest_sink_ids_;
}
MediaCastMode cast_mode() const { return cast_mode_; }
private:
MediaCastMode cast_mode_;
std::vector<MediaSink::Id> latest_sink_ids_;
QueryResultManager* result_manager_;
};
QueryResultManager::QueryResultManager(MediaRouter* router) : router_(router) {
DCHECK(router_);
}
QueryResultManager::~QueryResultManager() {
DCHECK(thread_checker_.CalledOnValidThread());
}
void QueryResultManager::AddObserver(Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(observer);
observers_.AddObserver(observer);
}
void QueryResultManager::RemoveObserver(Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(observer);
observers_.RemoveObserver(observer);
}
void QueryResultManager::StartSinksQuery(MediaCastMode cast_mode,
const MediaSource& source) {
DCHECK(thread_checker_.CalledOnValidThread());
if (source.Empty()) {
LOG(WARNING) << "StartSinksQuery called with empty source for "
<< cast_mode;
return;
}
SetSourceForCastMode(cast_mode, source);
RemoveObserverForCastMode(cast_mode);
UpdateWithSinksQueryResult(cast_mode, std::vector<MediaSink>());
scoped_ptr<CastModeMediaSinksObserver> observer(
new CastModeMediaSinksObserver(cast_mode, source, router_, this));
observer->Init();
auto result =
sinks_observers_.insert(std::make_pair(cast_mode, std::move(observer)));
DCHECK(result.second);
NotifyOnResultsUpdated();
}
void QueryResultManager::StopSinksQuery(MediaCastMode cast_mode) {
DCHECK(thread_checker_.CalledOnValidThread());
RemoveObserverForCastMode(cast_mode);
SetSourceForCastMode(cast_mode, MediaSource());
UpdateWithSinksQueryResult(cast_mode, std::vector<MediaSink>());
NotifyOnResultsUpdated();
}
void QueryResultManager::SetSourceForCastMode(
MediaCastMode cast_mode, const MediaSource& source) {
DCHECK(thread_checker_.CalledOnValidThread());
cast_mode_sources_[cast_mode] = source;
}
void QueryResultManager::RemoveObserverForCastMode(MediaCastMode cast_mode) {
auto observers_it = sinks_observers_.find(cast_mode);
if (observers_it != sinks_observers_.end())
sinks_observers_.erase(observers_it);
}
bool QueryResultManager::IsValid(const MediaSinkWithCastModes& entry) const {
return !entry.cast_modes.empty();
}
void QueryResultManager::UpdateWithSinksQueryResult(
MediaCastMode cast_mode,
const std::vector<MediaSink>& result) {
base::hash_set<MediaSink::Id> result_sink_ids;
for (const MediaSink& sink : result)
result_sink_ids.insert(sink.id());
// (1) Iterate through current sink set, remove cast mode from those that
// do not appear in latest result.
for (auto it = all_sinks_.begin(); it != all_sinks_.end(); /*no-op*/) {
if (!ContainsKey(result_sink_ids, it->first)) {
it->second.cast_modes.erase(cast_mode);
}
if (!IsValid(it->second)) {
all_sinks_.erase(it++);
} else {
++it;
}
}
// (2) Add / update sinks with latest result.
for (const MediaSink& sink : result) {
auto result =
all_sinks_.insert(std::make_pair(sink.id(),
MediaSinkWithCastModes(sink)));
if (!result.second)
result.first->second.sink = sink;
result.first->second.cast_modes.insert(cast_mode);
}
}
void QueryResultManager::GetSupportedCastModes(CastModeSet* cast_modes) const {
DCHECK(cast_modes);
cast_modes->clear();
for (const auto& observer_pair : sinks_observers_) {
cast_modes->insert(observer_pair.first);
}
}
MediaSource QueryResultManager::GetSourceForCastMode(
MediaCastMode cast_mode) const {
DCHECK(thread_checker_.CalledOnValidThread());
auto source_it = cast_mode_sources_.find(cast_mode);
return source_it == cast_mode_sources_.end() ?
MediaSource() : source_it->second;
}
void QueryResultManager::NotifyOnResultsUpdated() {
std::vector<MediaSinkWithCastModes> sinks;
for (const auto& sink_pair : all_sinks_) {
sinks.push_back(sink_pair.second);
}
FOR_EACH_OBSERVER(QueryResultManager::Observer, observers_,
OnResultsUpdated(sinks));
}
} // namespace media_router
|