Chromium Code Reviews| Index: chrome/browser/media/router/discovery/mdns/cast_media_sink_service.h |
| diff --git a/chrome/browser/media/router/discovery/mdns/cast_media_sink_service.h b/chrome/browser/media/router/discovery/mdns/cast_media_sink_service.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..27eb3b05826b5257daebd811c223d7d4dd9f0bc3 |
| --- /dev/null |
| +++ b/chrome/browser/media/router/discovery/mdns/cast_media_sink_service.h |
| @@ -0,0 +1,130 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ |
| +#define CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ |
| + |
| +#include <memory> |
| +#include <set> |
| + |
| +#include "base/gtest_prod_util.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "chrome/browser/media/router/discovery/mdns/dns_sd_delegate.h" |
| +#include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h" |
| +#include "chrome/browser/media/router/discovery/media_sink_service_base.h" |
| +#include "components/cast_channel/cast_channel_enum.h" |
| +#include "components/cast_channel/cast_socket.h" |
| +#include "net/base/ip_endpoint.h" |
| + |
| +namespace cast_channel { |
| +class CastSocketService; |
| +} // namespace cast_channel |
| + |
| +namespace content { |
| +class BrowserContext; |
| +} // namespace content |
| + |
| +namespace media_router { |
| + |
| +// A service which can be used to start background discovery and resolution of |
| +// Cast devices. |
| +// Public APIs should be invoked on the UI thread. |
| +class CastMediaSinkService |
| + : public MediaSinkServiceBase, |
| + public DnsSdRegistry::DnsSdObserver, |
| + public base::RefCountedThreadSafe<CastMediaSinkService> { |
| + public: |
| + CastMediaSinkService(const OnSinksDiscoveredCallback& callback, |
| + content::BrowserContext* browser_context); |
| + |
| + // Used by unit tests. |
| + CastMediaSinkService(const OnSinksDiscoveredCallback& callback, |
| + cast_channel::CastSocketService* cast_socket_service); |
| + |
| + // MediaSinkService implementation |
| + void Start() override; |
| + void Stop() override; |
| + |
| + protected: |
| + // Used to mock out the DnsSdRegistry for testing. |
| + void SetDnsSdRegistryForTest(DnsSdRegistry* registry); |
|
imcheng
2017/06/29 23:15:44
Can you set DnsSdRegistry in the test constructor?
zhaobin
2017/07/05 18:01:37
We are going to keep this function and remove |tes
|
| + |
| + ~CastMediaSinkService() override; |
| + |
| + private: |
| + // Receives incoming messages and errors and provides additional API context. |
| + class CastSocketObserver : public cast_channel::CastSocket::Observer { |
| + public: |
| + CastSocketObserver(); |
| + ~CastSocketObserver() override; |
| + |
| + // CastSocket::Observer implementation. |
| + void OnError(const cast_channel::CastSocket& socket, |
| + cast_channel::ChannelError error_state) override; |
| + void OnMessage(const cast_channel::CastSocket& socket, |
| + const cast_channel::CastMessage& message) override; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(CastSocketObserver); |
| + }; |
| + |
| + friend class base::RefCountedThreadSafe<CastMediaSinkService>; |
| + friend class CastMediaSinkServiceTest; |
| + |
| + FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestReStartAfterStop); |
| + FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, |
| + TestOnChannelOpenedOnIOThread); |
| + FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, |
| + TestMultipleOnChannelOpenedOnIOThread); |
| + FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestOnDnsSdEvent); |
| + FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestMultipleOnDnsSdEvent); |
| + FRIEND_TEST_ALL_PREFIXES(CastMediaSinkServiceTest, TestTimer); |
| + |
| + // DnsSdRegistry::DnsSdObserver implementation |
| + void OnDnsSdEvent(const std::string& service_type, |
| + const DnsSdRegistry::DnsSdServiceList& services) override; |
| + |
| + // Open cast channel on IO thread. |
|
mark a. foltz
2017/06/28 22:43:13
nit: Opens
zhaobin
2017/07/05 18:01:36
Done.
|
| + // |service|: mDNS service description. |
| + // |ip_endpoint|: cast channel's target IP endpoint. |
| + void OpenChannelOnIOThread(const DnsSdService& service, |
| + const net::IPEndPoint& ip_endpoint); |
| + |
| + // Invoked when opening cast channel on IO thread completes. |
| + // |service|: mDNS service description. |
| + // |channel_id|: channel id of newly created cast channel. |
| + // |channel_error|: error encounted when opending cast channel. |
| + void OnChannelOpenedOnIOThread(const DnsSdService& service, |
| + int channel_id, |
| + cast_channel::ChannelError channel_error); |
| + |
| + // Invoked by |OnChannelOpenedOnIOThread| to post task on UI thread. |
| + // |service|: mDNS service description. |
| + // |channel_id|: channel id of newly created cast channel. |
| + // |audio_only|: if cast channel is audio only or not. |
| + void OnChannelOpenedOnUIThread(const DnsSdService& service, |
| + int channel_id, |
| + bool audio_only); |
| + |
| + // Raw pointer to DnsSdRegistry singleton. |
|
mark a. foltz
2017/06/28 22:43:13
Can you comment about object lifetime here? I ass
zhaobin
2017/07/05 18:01:37
Done.
|
| + DnsSdRegistry* dns_sd_registry_ = nullptr; |
| + |
| + // DnsSdRegistry for test. |
| + DnsSdRegistry* test_dns_sd_registry_ = nullptr; |
|
mark a. foltz
2017/06/28 22:43:13
Does this need to be held separately or can tests
zhaobin
2017/07/05 18:01:37
Yes, we can remove this and let SetDnsSdRegistryFo
|
| + |
| + // Service list from current round of discovery. |
| + DnsSdRegistry::DnsSdServiceList current_services_; |
| + |
| + // Service managing creating and removing cast channels. |
| + scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; |
| + |
| + THREAD_CHECKER(thread_checker_); |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CastMediaSinkService); |
| +}; |
| + |
| +} // namespace media_router |
| + |
| +#endif // CHROME_BROWSER_MEDIA_ROUTER_DISCOVERY_MDNS_CAST_MEDIA_SINK_SERVICE_H_ |