1 // Copyright 2020 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 CAST_STANDALONE_SENDER_RECEIVER_CHOOSER_H_ 6 #define CAST_STANDALONE_SENDER_RECEIVER_CHOOSER_H_ 7 8 #include <functional> 9 #include <memory> 10 #include <vector> 11 12 #include "cast/common/public/service_info.h" 13 #include "discovery/common/reporting_client.h" 14 #include "discovery/public/dns_sd_service_factory.h" 15 #include "discovery/public/dns_sd_service_watcher.h" 16 #include "platform/api/network_interface.h" 17 #include "platform/api/serial_delete_ptr.h" 18 #include "platform/api/task_runner.h" 19 #include "platform/base/ip_address.h" 20 #include "util/alarm.h" 21 #include "util/chrono_helpers.h" 22 23 namespace openscreen { 24 namespace cast { 25 26 // Discovers Cast Receivers on the LAN for a given network interface, and 27 // provides a console menu interface for the user to choose one. 28 class ReceiverChooser final : public discovery::ReportingClient { 29 public: 30 using ResultCallback = std::function<void(IPEndpoint)>; 31 32 ReceiverChooser(const InterfaceInfo& interface, 33 TaskRunner* task_runner, 34 ResultCallback result_callback); 35 36 ~ReceiverChooser() final; 37 38 private: 39 // discovery::ReportingClient implementation. 40 void OnFatalError(Error error) final; 41 void OnRecoverableError(Error error) final; 42 43 // Called from the DnsWatcher with |all| ServiceInfos any time there is a 44 // change in the set of discovered devices. 45 void OnDnsWatcherUpdate( 46 std::vector<std::reference_wrapper<const ServiceInfo>> all); 47 48 // Called from |menu_alarm_| when it is a good time for the user to choose 49 // from the discovered-so-far set of Cast Receivers. 50 void PrintMenuAndHandleChoice(); 51 52 ResultCallback result_callback_; 53 SerialDeletePtr<discovery::DnsSdService> service_; 54 std::unique_ptr<discovery::DnsSdServiceWatcher<ServiceInfo>> watcher_; 55 std::vector<ServiceInfo> discovered_receivers_; 56 Alarm menu_alarm_; 57 58 // After there is another Cast Receiver discovered, ready to show to the user 59 // via the console menu, how long should the ReceiverChooser wait for 60 // additional receivers to be discovered and be included in the menu too? 61 static constexpr auto kWaitForStragglersDelay = seconds(5); 62 }; 63 64 } // namespace cast 65 } // namespace openscreen 66 67 #endif // CAST_STANDALONE_SENDER_RECEIVER_CHOOSER_H_ 68