OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ |
6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ | 6 #define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 14 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service_imp
l.h" |
14 #include "chrome/browser/media/router/discovery/mdns/dns_sd_delegate.h" | 15 #include "chrome/browser/media/router/discovery/mdns/dns_sd_delegate.h" |
15 #include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h" | 16 #include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h" |
16 #include "chrome/browser/media/router/discovery/media_sink_service_base.h" | 17 #include "chrome/browser/media/router/discovery/media_sink_service_base.h" |
17 #include "components/cast_channel/cast_channel_enum.h" | 18 #include "components/cast_channel/cast_channel_enum.h" |
18 #include "components/cast_channel/cast_socket.h" | 19 #include "components/cast_channel/cast_socket.h" |
19 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
20 | 21 |
21 namespace cast_channel { | 22 namespace cast_channel { |
22 class CastSocketService; | 23 class CastSocketService; |
23 } // namespace cast_channel | 24 } // namespace cast_channel |
24 | 25 |
25 namespace content { | 26 namespace content { |
26 class BrowserContext; | 27 class BrowserContext; |
27 } // namespace content | 28 } // namespace content |
28 | 29 |
29 namespace media_router { | 30 namespace media_router { |
30 | 31 |
31 // A service which can be used to start background discovery and resolution of | 32 // A service which can be used to start background discovery and resolution of |
32 // Cast devices. | 33 // Cast devices. |
33 // Public APIs should be invoked on the UI thread. | 34 // Public APIs should be invoked on the UI thread. |
34 class CastMediaSinkService | 35 class CastMediaSinkService : public MediaSinkServiceBase, |
35 : public MediaSinkServiceBase, | 36 public DialMediaSinkServiceDelegate, |
36 public DnsSdRegistry::DnsSdObserver, | 37 public DnsSdRegistry::DnsSdObserver { |
37 public base::RefCountedThreadSafe<CastMediaSinkService> { | |
38 public: | 38 public: |
39 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, | 39 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, |
40 content::BrowserContext* browser_context); | 40 content::BrowserContext* browser_context); |
41 | 41 |
42 // Used by unit tests. | 42 // Used by unit tests. |
43 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, | 43 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, |
44 cast_channel::CastSocketService* cast_socket_service); | 44 cast_channel::CastSocketService* cast_socket_service); |
45 | 45 |
46 // mDNS service types. | 46 // mDNS service types. |
47 static const char kCastServiceType[]; | 47 static const char kCastServiceType[]; |
48 | 48 |
| 49 // Default Cast control port to open Cast Socket from DIAL sink. |
| 50 static const int kCastControlPort; |
| 51 |
49 // MediaSinkService implementation | 52 // MediaSinkService implementation |
50 void Start() override; | 53 void Start() override; |
51 void Stop() override; | 54 void Stop() override; |
52 | 55 |
| 56 // DialMediaSinkServiceDelegate implementation. |
| 57 void OnDialSinkAdded(const MediaSinkInternal& sink) override; |
| 58 void OnDialSinksRemoved() override; |
| 59 |
53 protected: | 60 protected: |
54 // Used to mock out the DnsSdRegistry for testing. | 61 // Used to mock out the DnsSdRegistry for testing. |
55 void SetDnsSdRegistryForTest(DnsSdRegistry* registry); | 62 void SetDnsSdRegistryForTest(DnsSdRegistry* registry); |
56 | 63 |
57 ~CastMediaSinkService() override; | 64 ~CastMediaSinkService() override; |
58 | 65 |
59 private: | 66 private: |
60 // Receives incoming messages and errors and provides additional API context. | 67 // Receives incoming messages and errors and provides additional API context. |
61 class CastSocketObserver : public cast_channel::CastSocket::Observer { | 68 class CastSocketObserver : public cast_channel::CastSocket::Observer { |
62 public: | 69 public: |
(...skipping 15 matching lines...) Expand all Loading... |
78 | 85 |
79 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestReStartAfterStop); | 86 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestReStartAfterStop); |
80 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleStartAndStop); | 87 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleStartAndStop); |
81 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, | 88 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, |
82 TestOnChannelOpenedOnIOThread); | 89 TestOnChannelOpenedOnIOThread); |
83 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, | 90 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, |
84 TestMultipleOnChannelOpenedOnIOThread); | 91 TestMultipleOnChannelOpenedOnIOThread); |
85 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDnsSdEvent); | 92 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDnsSdEvent); |
86 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleOnDnsSdEvent); | 93 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleOnDnsSdEvent); |
87 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestTimer); | 94 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestTimer); |
| 95 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDialSinkAdded); |
| 96 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnFetchCompleted); |
| 97 |
| 98 // MediaSinkServiceBase implementation |
| 99 void OnFetchCompleted() override; |
| 100 |
| 101 // Returns cast sinks from |current_sinks_by_mdns_map_| and |
| 102 // |current_sinks_by_dial_map_|. |
| 103 std::set<MediaSinkInternal> GetCastSinksOnIOThread(); |
| 104 |
| 105 // Stores |cast_sinks| in |current_sinks_| and invokes |
| 106 // MediaSinkServiceBase::OnFetchCompleted on the UI thread. |
| 107 void OnFetchCompletedOnUIThread(std::set<MediaSinkInternal> cast_sinks); |
88 | 108 |
89 // DnsSdRegistry::DnsSdObserver implementation | 109 // DnsSdRegistry::DnsSdObserver implementation |
90 void OnDnsSdEvent(const std::string& service_type, | 110 void OnDnsSdEvent(const std::string& service_type, |
91 const DnsSdRegistry::DnsSdServiceList& services) override; | 111 const DnsSdRegistry::DnsSdServiceList& services) override; |
92 | 112 |
| 113 // Opens cast channels on the IO thread. |
| 114 void OpenChannelsOnIOThread(std::vector<MediaSinkInternal> cast_sinks); |
| 115 |
93 // Opens cast channel on IO thread. | 116 // Opens cast channel on IO thread. |
94 // |service|: mDNS service description. | |
95 // |ip_endpoint|: cast channel's target IP endpoint. | 117 // |ip_endpoint|: cast channel's target IP endpoint. |
96 void OpenChannelOnIOThread(const DnsSdService& service, | 118 // |cast_sink|: Cast sink created from mDNS service description or DIAL sink. |
97 const net::IPEndPoint& ip_endpoint); | 119 void OpenChannelOnIOThread(const net::IPEndPoint& ip_endpoint, |
| 120 MediaSinkInternal cast_sink); |
98 | 121 |
99 // Invoked when opening cast channel on IO thread completes. | 122 // Invoked when opening cast channel on IO thread completes. |
100 // |service|: mDNS service description. | 123 // |cast_sink|: Cast sink created from mDNS service description or DIAL sink. |
101 // |channel_id|: channel id of newly created cast channel. | 124 // |channel_id|: channel id of newly created cast channel. |
102 // |channel_error|: error encounted when opending cast channel. | 125 // |channel_error|: error encounted when opending cast channel. |
103 void OnChannelOpenedOnIOThread(const DnsSdService& service, | 126 void OnChannelOpenedOnIOThread(MediaSinkInternal cast_sink, |
104 int channel_id, | 127 int channel_id, |
105 cast_channel::ChannelError channel_error); | 128 cast_channel::ChannelError channel_error); |
106 | 129 |
107 // Invoked by |OnChannelOpenedOnIOThread| to post task on UI thread. | |
108 // |service|: mDNS service description. | |
109 // |channel_id|: channel id of newly created cast channel. | |
110 // |audio_only|: if cast channel is audio only or not. | |
111 void OnChannelOpenedOnUIThread(const DnsSdService& service, | |
112 int channel_id, | |
113 bool audio_only); | |
114 | |
115 // Raw pointer to DnsSdRegistry instance, which is a global leaky singleton | 130 // Raw pointer to DnsSdRegistry instance, which is a global leaky singleton |
116 // and lives as long as the browser process. | 131 // and lives as long as the browser process. |
117 DnsSdRegistry* dns_sd_registry_ = nullptr; | 132 DnsSdRegistry* dns_sd_registry_ = nullptr; |
118 | 133 |
119 // Service list from current round of discovery. | 134 // Set of mDNS service ip addresses from current round of discovery. |
120 DnsSdRegistry::DnsSdServiceList current_services_; | 135 std::set<net::IPEndPoint> current_service_ip_endpoints_; |
| 136 |
| 137 using MediaSinkInternalMap = std::map<net::IPEndPoint, MediaSinkInternal>; |
| 138 |
| 139 // Map of sinks from current round of mDNS discovery keyed by IP address. |
| 140 MediaSinkInternalMap current_sinks_by_mdns_; |
| 141 |
| 142 // Map of sinks from current round of DIAL discovery keyed by IP address. |
| 143 MediaSinkInternalMap current_sinks_by_dial_; |
121 | 144 |
122 // Service managing creating and removing cast channels. | 145 // Service managing creating and removing cast channels. |
123 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; | 146 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; |
124 | 147 |
125 THREAD_CHECKER(thread_checker_); | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(CastMediaSinkService); | 148 DISALLOW_COPY_AND_ASSIGN(CastMediaSinkService); |
128 }; | 149 }; |
129 | 150 |
130 } // namespace media_router | 151 } // namespace media_router |
131 | 152 |
132 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ | 153 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ |
OLD | NEW |