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 net::URLRequestContextGetter* request_context) |
| 24 : MediaSinkService(callback), request_context_(request_context) { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 26 DCHECK(request_context_); |
| 27 } |
| 28 |
| 29 DialMediaSinkService::~DialMediaSinkService() {} |
| 30 |
| 31 void DialMediaSinkService::Start() { |
| 32 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 33 dial_registry()->RegisterObserver(this); |
| 34 dial_registry()->StartPeriodicDiscovery(); |
| 35 } |
| 36 |
| 37 void DialMediaSinkService::Stop() { |
| 38 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 39 dial_registry()->UnregisterObserver(this); |
| 40 } |
| 41 |
| 42 DialRegistry* DialMediaSinkService::dial_registry() { |
| 43 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 44 return DialRegistry::GetInstance(); |
| 45 } |
| 46 |
| 47 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() { |
| 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 49 if (!description_service_.get()) { |
| 50 description_service_.reset(new DeviceDescriptionService( |
| 51 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable, |
| 52 base::Unretained(this)), |
| 53 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, |
| 54 base::Unretained(this)))); |
| 55 } |
| 56 return description_service_.get(); |
| 57 } |
| 58 |
| 59 void DialMediaSinkService::OnDialDeviceEvent( |
| 60 const DialRegistry::DeviceList& devices) { |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 62 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() |
| 63 << " devices"; |
| 64 |
| 65 // Add a finish timer. |
| 66 finish_timer_.reset(new base::OneShotTimer()); |
| 67 base::TimeDelta finish_delay = |
| 68 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); |
| 69 finish_timer_->Start(FROM_HERE, finish_delay, this, |
| 70 &DialMediaSinkService::OnFetchCompleted); |
| 71 |
| 72 current_sinks_.clear(); |
| 73 current_devices_ = devices; |
| 74 |
| 75 GetDescriptionService()->GetDeviceDescriptions(devices, |
| 76 request_context_.get()); |
| 77 } |
| 78 |
| 79 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 81 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); |
| 82 } |
| 83 |
| 84 void DialMediaSinkService::OnDeviceDescriptionAvailable( |
| 85 const DialDeviceData& device_data, |
| 86 const ParsedDialDeviceDescription& description_data) { |
| 87 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 88 |
| 89 if (!base::ContainsValue(current_devices_, device_data)) { |
| 90 DVLOG(2) << "Device data not found in current device data list..."; |
| 91 return; |
| 92 } |
| 93 |
| 94 // When use this "sink" within browser, please note it will have a different |
| 95 // ID when it is sent to the extension, because it derives a different sink ID |
| 96 // using the given sink ID. |
| 97 MediaSink sink(description_data.unique_id, description_data.friendly_name, |
| 98 MediaSink::IconType::GENERIC); |
| 99 DialSinkExtraData extra_data; |
| 100 extra_data.app_url = description_data.app_url; |
| 101 extra_data.model_name = description_data.model_name; |
| 102 std::string ip_address = device_data.device_description_url().host(); |
| 103 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { |
| 104 DVLOG(1) << "Invalid ip_address: " << ip_address; |
| 105 return; |
| 106 } |
| 107 |
| 108 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); |
| 109 |
| 110 if (finish_timer_) |
| 111 return; |
| 112 |
| 113 // Start fetch timer again if device description comes back after |
| 114 // |finish_timer_| fires. |
| 115 base::TimeDelta finish_delay = |
| 116 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); |
| 117 finish_timer_.reset(new base::OneShotTimer()); |
| 118 finish_timer_->Start(FROM_HERE, finish_delay, this, |
| 119 &DialMediaSinkService::OnFetchCompleted); |
| 120 } |
| 121 |
| 122 void DialMediaSinkService::OnDeviceDescriptionError( |
| 123 const DialDeviceData& device, |
| 124 const std::string& error_message) { |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 126 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; |
| 127 } |
| 128 |
| 129 void DialMediaSinkService::OnFetchCompleted() { |
| 130 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 131 DCHECK(!sink_discovery_callback_.is_null()); |
| 132 |
| 133 finish_timer_.reset(); |
| 134 |
| 135 auto sinks = current_sinks_; |
| 136 if (sinks == mrp_sinks_) { |
| 137 DVLOG(2) << "No update to sink list."; |
| 138 return; |
| 139 } |
| 140 |
| 141 DVLOG(2) << "Send sinks to media router, [size]: " << sinks.size(); |
| 142 sink_discovery_callback_.Run( |
| 143 std::vector<MediaSinkInternal>(sinks.begin(), sinks.end())); |
| 144 mrp_sinks_ = std::move(sinks); |
| 145 } |
| 146 |
| 147 } // namespace media_router |
OLD | NEW |