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 #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_ | |
7 | |
8 #include <memory> | |
9 #include <set> | |
10 | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/threading/thread_checker.h" | |
14 #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/media_sink_service_base.h" | |
17 #include "components/cast_channel/cast_channel_enum.h" | |
18 #include "components/cast_channel/cast_socket.h" | |
19 #include "net/base/ip_endpoint.h" | |
20 | |
21 namespace cast_channel { | |
22 class CastSocketService; | |
23 } // namespace cast_channel | |
24 | |
25 namespace content { | |
26 class BrowserContext; | |
27 } // namespace content | |
28 | |
29 namespace media_router { | |
30 | |
31 // A service which can be used to start background discovery and resolution of | |
32 // Cast devices. | |
33 // Public APIs should be invoked on the UI thread. | |
34 class CastMediaSinkService | |
35 : public MediaSinkServiceBase, | |
36 public DnsSdRegistry::DnsSdObserver, | |
37 public base::RefCountedThreadSafe<CastMediaSinkService> { | |
38 public: | |
39 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, | |
40 content::BrowserContext* browser_context); | |
41 | |
42 // Used by unit tests. | |
43 CastMediaSinkService(const OnSinksDiscoveredCallback& callback, | |
44 cast_channel::CastSocketService* cast_socket_service); | |
45 | |
46 // mDNS service types. | |
47 static const char kCastServiceType[]; | |
48 | |
49 // MediaSinkService implementation | |
50 void Start() override; | |
51 void Stop() override; | |
52 | |
53 protected: | |
54 // Used to mock out the DnsSdRegistry for testing. | |
55 void SetDnsSdRegistryForTest(DnsSdRegistry* registry); | |
56 | |
57 ~CastMediaSinkService() override; | |
58 | |
59 private: | |
60 // Receives incoming messages and errors and provides additional API context. | |
61 class CastSocketObserver : public cast_channel::CastSocket::Observer { | |
62 public: | |
63 CastSocketObserver(); | |
64 ~CastSocketObserver() override; | |
65 | |
66 // CastSocket::Observer implementation. | |
67 void OnError(const cast_channel::CastSocket& socket, | |
68 cast_channel::ChannelError error_state) override; | |
69 void OnMessage(const cast_channel::CastSocket& socket, | |
70 const cast_channel::CastMessage& message) override; | |
71 | |
72 private: | |
73 DISALLOW_COPY_AND_ASSIGN(CastSocketObserver); | |
74 }; | |
75 | |
76 friend class base::RefCountedThreadSafe<CastMediaSinkService>; | |
77 friend class CastMediaSinkServiceTest; | |
78 | |
79 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestReStartAfterStop); | |
80 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleStartAndStop); | |
81 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, | |
82 TestOnChannelOpenedOnIOThread); | |
83 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, | |
84 TestMultipleOnChannelOpenedOnIOThread); | |
85 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDnsSdEvent); | |
86 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleOnDnsSdEvent); | |
87 FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestTimer); | |
88 | |
89 // DnsSdRegistry::DnsSdObserver implementation | |
90 void OnDnsSdEvent(const std::string& service_type, | |
91 const DnsSdRegistry::DnsSdServiceList& services) override; | |
92 | |
93 // Opens cast channel on IO thread. | |
94 // |service|: mDNS service description. | |
95 // |ip_endpoint|: cast channel's target IP endpoint. | |
96 void OpenChannelOnIOThread(const DnsSdService& service, | |
97 const net::IPEndPoint& ip_endpoint); | |
98 | |
99 // Invoked when opening cast channel on IO thread completes. | |
100 // |service|: mDNS service description. | |
101 // |channel_id|: channel id of newly created cast channel. | |
102 // |channel_error|: error encounted when opending cast channel. | |
103 void OnChannelOpenedOnIOThread(const DnsSdService& service, | |
104 int channel_id, | |
105 cast_channel::ChannelError channel_error); | |
106 | |
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 | |
116 // and lives as long as the browser process. | |
117 DnsSdRegistry* dns_sd_registry_ = nullptr; | |
118 | |
119 // Service list from current round of discovery. | |
120 DnsSdRegistry::DnsSdServiceList current_services_; | |
121 | |
122 // Service managing creating and removing cast channels. | |
123 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; | |
124 | |
125 THREAD_CHECKER(thread_checker_); | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(CastMediaSinkService); | |
128 }; | |
129 | |
130 } // namespace media_router | |
131 | |
132 #endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ | |
OLD | NEW |