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 |
(...skipping 15 matching lines...) Expand all Loading... | |
26 class BrowserContext; | 26 class BrowserContext; |
27 } // namespace content | 27 } // namespace content |
28 | 28 |
29 namespace media_router { | 29 namespace media_router { |
30 | 30 |
31 // A service which can be used to start background discovery and resolution of | 31 // A service which can be used to start background discovery and resolution of |
32 // Cast devices. | 32 // Cast devices. |
33 // Public APIs should be invoked on the UI thread. | 33 // Public APIs should be invoked on the UI thread. |
34 class CastMediaSinkService | 34 class CastMediaSinkService |
35 : public MediaSinkServiceBase, | 35 : public MediaSinkServiceBase, |
36 public MediaSinkService::Observer, | |
36 public DnsSdRegistry::DnsSdObserver, | 37 public DnsSdRegistry::DnsSdObserver, |
37 public base::RefCountedThreadSafe<CastMediaSinkService> { | 38 public base::RefCountedThreadSafe<CastMediaSinkService> { |
38 public: | 39 public: |
39 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, | 40 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, |
40 content::BrowserContext* browser_context); | 41 content::BrowserContext* browser_context); |
41 | 42 |
42 // Used by unit tests. | 43 // Used by unit tests. |
43 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, | 44 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, |
44 cast_channel::CastSocketService* cast_socket_service); | 45 cast_channel::CastSocketService* cast_socket_service); |
45 | 46 |
46 // mDNS service types. | 47 // mDNS service types. |
47 static const char kCastServiceType[]; | 48 static const char kCastServiceType[]; |
imcheng
2017/07/06 22:53:30
Can this and kCastControlPort be constexpr and be
zhaobin
2017/07/10 20:21:15
Yes, but still need to define it again in .cc file
| |
48 | 49 |
50 // Default Cast control port to open Cast Socket from DIAL sink. | |
51 static int kCastControlPort; | |
52 | |
49 // MediaSinkService implementation | 53 // MediaSinkService implementation |
50 void Start() override; | 54 void Start() override; |
51 void Stop() override; | 55 void Stop() override; |
52 | 56 |
57 // MediaSinkService::Observer implementation | |
58 void OnMediaSinkAdded(const MediaSinkInternal& sink) override; | |
59 void OnMediaSinksRemoved() override; | |
60 | |
53 protected: | 61 protected: |
54 // Used to mock out the DnsSdRegistry for testing. | 62 // Used to mock out the DnsSdRegistry for testing. |
55 void SetDnsSdRegistryForTest(DnsSdRegistry* registry); | 63 void SetDnsSdRegistryForTest(DnsSdRegistry* registry); |
56 | 64 |
57 ~CastMediaSinkService() override; | 65 ~CastMediaSinkService() override; |
58 | 66 |
59 private: | 67 private: |
60 // Receives incoming messages and errors and provides additional API context. | 68 // Receives incoming messages and errors and provides additional API context. |
61 class CastSocketObserver : public cast_channel::CastSocket::Observer { | 69 class CastSocketObserver : public cast_channel::CastSocket::Observer { |
62 public: | 70 public: |
(...skipping 15 matching lines...) Expand all Loading... | |
78 | 86 |
79 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestReStartAfterStop); | 87 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestReStartAfterStop); |
80 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleStartAndStop); | 88 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleStartAndStop); |
81 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, | 89 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, |
82 TestOnChannelOpenedOnIOThread); | 90 TestOnChannelOpenedOnIOThread); |
83 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, | 91 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, |
84 TestMultipleOnChannelOpenedOnIOThread); | 92 TestMultipleOnChannelOpenedOnIOThread); |
85 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDnsSdEvent); | 93 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDnsSdEvent); |
86 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleOnDnsSdEvent); | 94 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleOnDnsSdEvent); |
87 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestTimer); | 95 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestTimer); |
96 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDialSinkAdded); | |
97 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnFetchCompleted); | |
98 | |
99 // MediaSinkServiceBase implementation | |
100 void OnFetchCompleted() override; | |
88 | 101 |
89 // DnsSdRegistry::DnsSdObserver implementation | 102 // DnsSdRegistry::DnsSdObserver implementation |
90 void OnDnsSdEvent(const std::string& service_type, | 103 void OnDnsSdEvent(const std::string& service_type, |
91 const DnsSdRegistry::DnsSdServiceList& services) override; | 104 const DnsSdRegistry::DnsSdServiceList& services) override; |
92 | 105 |
93 // Opens cast channel on IO thread. | 106 // Open cast channel on IO thread. |
94 // |service|: mDNS service description. | |
95 // |ip_endpoint|: cast channel's target IP endpoint. | 107 // |ip_endpoint|: cast channel's target IP endpoint. |
96 void OpenChannelOnIOThread(const DnsSdService& service, | 108 // |on_open_callback|: callback to be invoked on the IO thread when Cast |
97 const net::IPEndPoint& ip_endpoint); | 109 // socket opens. |
110 void OpenChannelOnIOThread( | |
111 const net::IPEndPoint& ip_endpoint, | |
112 const cast_channel::CastSocket::OnOpenCallback& on_open_callback); | |
98 | 113 |
99 // Invoked when opening cast channel on IO thread completes. | 114 // Invoked when opening cast channel on IO thread completes. |
100 // |service|: mDNS service description. | 115 // |cast_sink|: Cast sink created from mDNS service description or DIAL sink. |
101 // |channel_id|: channel id of newly created cast channel. | 116 // |channel_id|: channel id of newly created cast channel. |
102 // |channel_error|: error encounted when opending cast channel. | 117 // |channel_error|: error encounted when opending cast channel. |
103 void OnChannelOpenedOnIOThread(const DnsSdService& service, | 118 void OnChannelOpenedOnIOThread(const MediaSinkInternal& cast_sink, |
104 int channel_id, | 119 int channel_id, |
105 cast_channel::ChannelError channel_error); | 120 cast_channel::ChannelError channel_error); |
106 | 121 |
107 // Invoked by |OnChannelOpenedOnIOThread| to post task on UI thread. | 122 // Invoked by |OnChannelOpenedOnIOThread| to post task on UI thread. |
108 // |service|: mDNS service description. | 123 // |cast_sink|: Cast sink created from mDNS service description or DIAL sink. |
109 // |channel_id|: channel id of newly created cast channel. | 124 void OnChannelOpenedOnUIThread(const MediaSinkInternal& cast_sink); |
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 | 125 |
115 // Raw pointer to DnsSdRegistry instance, which is a global leaky singleton | 126 // Raw pointer to DnsSdRegistry instance, which is a global leaky singleton |
116 // and lives as long as the browser process. | 127 // and lives as long as the browser process. |
117 DnsSdRegistry* dns_sd_registry_ = nullptr; | 128 DnsSdRegistry* dns_sd_registry_ = nullptr; |
118 | 129 |
119 // Service list from current round of discovery. | 130 // Set of mDNS service ip addresses from current round of discovery. |
120 DnsSdRegistry::DnsSdServiceList current_services_; | 131 std::set<net::IPEndPoint> current_service_ip_endpoints_; |
132 | |
133 // Map of sinks from current round of mDNS discovery keyed by IP address. | |
134 std::map<net::IPEndPoint, MediaSinkInternal> current_sinks_by_mdns_map_; | |
135 | |
136 // Map of sinks from current round of DIAL discovery keyed by IP address. | |
137 std::map<net::IPEndPoint, MediaSinkInternal> current_sinks_by_dial_map_; | |
121 | 138 |
122 // Service managing creating and removing cast channels. | 139 // Service managing creating and removing cast channels. |
123 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; | 140 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; |
124 | 141 |
125 THREAD_CHECKER(thread_checker_); | 142 THREAD_CHECKER(thread_checker_); |
126 | 143 |
127 DISALLOW_COPY_AND_ASSIGN(CastMediaSinkService); | 144 DISALLOW_COPY_AND_ASSIGN(CastMediaSinkService); |
128 }; | 145 }; |
129 | 146 |
130 } // namespace media_router | 147 } // namespace media_router |
131 | 148 |
132 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ | 149 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ |
OLD | NEW |