OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service.h" | |
6 | |
7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | |
8 #include "chrome/browser/profiles/profile.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "net/url_request/url_request_context_getter.h" | |
11 | |
12 using content::BrowserThread; | |
13 | |
14 namespace { | |
15 // Time interval when media sink service sends sinks to MRP. | |
16 const int kFetchCompleteTimeoutSecs = 3; | |
17 } | |
18 | |
19 namespace media_router { | |
20 | |
21 DialMediaSinkService::DialMediaSinkService( | |
22 const OnSinksDiscoveredCallback& callback, | |
23 content::BrowserContext* browser_context, | |
24 net::URLRequestContextGetter* request_context) | |
25 : MediaSinkService(callback), browser_context_(browser_context) { | |
imcheng
2017/04/26 22:52:16
browser_context_ not used?
zhaobin
2017/04/27 01:41:20
Done.
| |
26 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
imcheng
2017/04/26 22:52:16
The constructor will be invoked by MediaRouter in
zhaobin
2017/04/27 01:41:20
Plan to create this in IO thread. Will update http
imcheng
2017/04/27 19:21:32
Hmm, I don't think that will work. MediaRouterMojo
| |
27 request_context_ = request_context; | |
imcheng
2017/04/26 22:52:16
Can this go in the initializer?
zhaobin
2017/04/27 01:41:20
Done.
| |
28 | |
29 DCHECK(request_context_); | |
30 } | |
31 | |
32 DialMediaSinkService::~DialMediaSinkService() {} | |
33 | |
34 void DialMediaSinkService::Start() { | |
35 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
36 dial_registry()->RegisterObserver(this); | |
37 dial_registry()->OnListenerAdded(); | |
38 } | |
39 | |
40 void DialMediaSinkService::Stop() { | |
41 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
42 dial_registry()->UnregisterObserver(this); | |
43 } | |
44 | |
45 void DialMediaSinkService::AddSinkQuery(MediaSinksObserver* observer) { | |
46 // TODO(crbug.com/687375): Implement DIAL sink query. | |
47 NOTIMPLEMENTED(); | |
48 } | |
49 | |
50 void DialMediaSinkService::RemoveSinkQuery(MediaSinksObserver* observer) { | |
51 // TODO(crbug.com/687375): Implement DIAL sink query. | |
52 NOTIMPLEMENTED(); | |
53 } | |
54 | |
55 DialRegistry* DialMediaSinkService::dial_registry() { | |
56 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
57 return DialRegistry::GetInstance(); | |
58 } | |
59 | |
60 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() { | |
61 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
62 if (!description_service_.get()) { | |
63 description_service_.reset(new DeviceDescriptionService( | |
64 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable, | |
65 base::Unretained(this)), | |
66 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, | |
67 base::Unretained(this)))); | |
68 } | |
69 return description_service_.get(); | |
70 } | |
71 | |
72 void DialMediaSinkService::OnDialDeviceEvent( | |
73 const DialRegistry::DeviceList& devices) { | |
74 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
75 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() | |
76 << " devices"; | |
77 | |
78 // Add a finish timer. | |
79 finish_timer_.reset(new base::OneShotTimer()); | |
80 base::TimeDelta finish_delay = | |
81 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); | |
82 finish_timer_->Start(FROM_HERE, finish_delay, this, | |
83 &DialMediaSinkService::OnFetchCompleted); | |
84 | |
85 current_sinks_.clear(); | |
86 current_devices_ = devices; | |
87 | |
88 GetDescriptionService()->GetDeviceDescriptions(devices, | |
89 request_context_.get()); | |
90 } | |
91 | |
92 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { | |
93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
94 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); | |
95 } | |
96 | |
97 void DialMediaSinkService::OnDeviceDescriptionAvailable( | |
98 const DialDeviceData& device_data, | |
99 const ParsedDialDeviceDescription& description_data) { | |
100 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
101 | |
102 if (!base::ContainsValue(current_devices_, device_data)) { | |
103 DVLOG(2) << "Device data not found in current device data list..."; | |
104 return; | |
105 } | |
106 | |
107 MediaSink sink(description_data.unique_id, description_data.friendly_name, | |
imcheng
2017/04/26 22:52:16
Note this "sink" will have a different ID when it
zhaobin
2017/04/27 01:41:20
Added a comment.
| |
108 MediaSink::IconType::GENERIC); | |
109 DialSinkExtraData extra_data; | |
110 extra_data.app_url = description_data.app_url; | |
111 extra_data.model_name = description_data.model_name; | |
112 std::string ip_address = device_data.device_description_url().host(); | |
113 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { | |
114 DVLOG(1) << "Invalid ip_address: " << ip_address; | |
115 return; | |
116 } | |
117 | |
118 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); | |
119 | |
120 if (finish_timer_) | |
121 return; | |
122 | |
123 // Start fetch timer again if device description comes back after | |
124 // |finish_timer_| fires. | |
125 base::TimeDelta finish_delay = | |
126 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); | |
127 finish_timer_.reset(new base::OneShotTimer()); | |
128 finish_timer_->Start(FROM_HERE, finish_delay, this, | |
129 &DialMediaSinkService::OnFetchCompleted); | |
130 } | |
131 | |
132 void DialMediaSinkService::OnDeviceDescriptionError( | |
133 const DialDeviceData& device, | |
134 const std::string& error_message) { | |
135 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
136 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; | |
137 } | |
138 | |
139 void DialMediaSinkService::OnFetchCompleted() { | |
140 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
141 DCHECK(!sink_discovery_callback_.is_null()); | |
142 | |
143 finish_timer_.reset(); | |
144 | |
145 auto sinks = current_sinks_; | |
146 if (sinks == mrp_sinks_) { | |
147 DVLOG(2) << "No update to sink list."; | |
148 return; | |
149 } | |
150 | |
151 DVLOG(2) << "Send sinks to media router, [size]: " << sinks.size(); | |
152 sink_discovery_callback_.Run( | |
153 std::vector<MediaSinkInternal>(sinks.begin(), sinks.end())); | |
154 mrp_sinks_ = std::move(sinks); | |
155 } | |
156 | |
157 } // namespace media_router | |
OLD | NEW |