diff options
Diffstat (limited to 'cast/sender/public')
-rw-r--r-- | cast/sender/public/DEPS | 2 | ||||
-rw-r--r-- | cast/sender/public/cast_app_discovery_service.cc | 48 | ||||
-rw-r--r-- | cast/sender/public/cast_app_discovery_service.h | 75 |
3 files changed, 125 insertions, 0 deletions
diff --git a/cast/sender/public/DEPS b/cast/sender/public/DEPS index 3cfae918..8a61a258 100644 --- a/cast/sender/public/DEPS +++ b/cast/sender/public/DEPS @@ -4,4 +4,6 @@ include_rules = [ # Dependencies on the implementation are not allowed in public/. '-cast/sender', '+cast/sender/public', + '-cast/common', + '+cast/common/public', ] diff --git a/cast/sender/public/cast_app_discovery_service.cc b/cast/sender/public/cast_app_discovery_service.cc new file mode 100644 index 00000000..c561b386 --- /dev/null +++ b/cast/sender/public/cast_app_discovery_service.cc @@ -0,0 +1,48 @@ +// Copyright 2020 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. + +#include "cast/sender/public/cast_app_discovery_service.h" + +namespace openscreen { +namespace cast { + +CastAppDiscoveryService::Subscription::Subscription( + CastAppDiscoveryService* discovery_service, + uint32_t id) + : discovery_service_(discovery_service), id_(id) {} + +CastAppDiscoveryService::Subscription::Subscription(Subscription&& other) + : discovery_service_(other.discovery_service_), id_(other.id_) { + other.discovery_service_ = nullptr; +} + +CastAppDiscoveryService::Subscription::~Subscription() { + Reset(); +} + +CastAppDiscoveryService::Subscription& CastAppDiscoveryService::Subscription:: +operator=(Subscription other) { + Swap(other); + return *this; +} + +void CastAppDiscoveryService::Subscription::Reset() { + if (discovery_service_) { + discovery_service_->RemoveAvailabilityCallback(id_); + } + discovery_service_ = nullptr; +} + +void CastAppDiscoveryService::Subscription::Swap(Subscription& other) { + CastAppDiscoveryService* service = other.discovery_service_; + other.discovery_service_ = discovery_service_; + discovery_service_ = service; + + uint32_t id = other.id_; + other.id_ = id_; + id_ = id; +} + +} // namespace cast +} // namespace openscreen diff --git a/cast/sender/public/cast_app_discovery_service.h b/cast/sender/public/cast_app_discovery_service.h new file mode 100644 index 00000000..ccb586ff --- /dev/null +++ b/cast/sender/public/cast_app_discovery_service.h @@ -0,0 +1,75 @@ +// Copyright 2020 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 CAST_SENDER_PUBLIC_CAST_APP_DISCOVERY_SERVICE_H_ +#define CAST_SENDER_PUBLIC_CAST_APP_DISCOVERY_SERVICE_H_ + +#include <vector> + +#include "cast/common/public/service_info.h" + +namespace openscreen { +namespace cast { + +class CastMediaSource; + +// Interface for app discovery for Cast devices. +class CastAppDiscoveryService { + public: + using AvailabilityCallback = + std::function<void(const CastMediaSource& source, + const std::vector<ServiceInfo>& devices)>; + + class Subscription { + public: + Subscription(Subscription&&); + ~Subscription(); + Subscription& operator=(Subscription); + + void Reset(); + + private: + friend class CastAppDiscoveryService; + + Subscription(CastAppDiscoveryService* discovery_service, uint32_t id); + + void Swap(Subscription& other); + + CastAppDiscoveryService* discovery_service_; + uint32_t id_; + }; + + virtual ~CastAppDiscoveryService() = default; + + // Adds an availability query for |source|. Results will be continuously + // returned via |callback| until the returned Subscription is destroyed by the + // caller. If there are cached results available, |callback| will be invoked + // before this method returns. |callback| may be invoked with an empty list + // if all devices respond to the respective queries with "unavailable" or + // don't respond before a timeout. |callback| may be invoked successively + // with the same list. + virtual Subscription StartObservingAvailability( + const CastMediaSource& source, + AvailabilityCallback callback) = 0; + + // Refreshes the state of app discovery in the service. It is suitable to call + // this method when the user initiates a user gesture. + virtual void Refresh() = 0; + + protected: + Subscription MakeSubscription(CastAppDiscoveryService* discovery_service, + uint32_t id) { + return Subscription(discovery_service, id); + } + + private: + friend class Subscription; + + virtual void RemoveAvailabilityCallback(uint32_t id) = 0; +}; + +} // namespace cast +} // namespace openscreen + +#endif // CAST_SENDER_PUBLIC_CAST_APP_DISCOVERY_SERVICE_H_ |