aboutsummaryrefslogtreecommitdiff
path: root/cast/standalone_receiver/cast_service.cc
blob: 009190b003a088c83a3656b2f6ce81763e809fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// 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/standalone_receiver/cast_service.h"

#include <utility>

#include "discovery/common/config.h"
#include "platform/api/tls_connection_factory.h"
#include "platform/base/interface_info.h"
#include "platform/base/tls_listen_options.h"
#include "util/osp_logging.h"
#include "util/stringprintf.h"

namespace openscreen {
namespace cast {

namespace {

constexpr uint16_t kDefaultCastServicePort = 8010;

constexpr int kDefaultMaxBacklogSize = 64;
const TlsListenOptions kDefaultListenOptions{kDefaultMaxBacklogSize};

IPEndpoint DetermineEndpoint(const InterfaceInfo& interface) {
  const IPAddress address = interface.GetIpAddressV4()
                                ? interface.GetIpAddressV4()
                                : interface.GetIpAddressV6();
  OSP_CHECK(address);
  return IPEndpoint{address, kDefaultCastServicePort};
}

discovery::Config MakeDiscoveryConfig(const InterfaceInfo& interface) {
  return discovery::Config{.network_info = {interface}};
}

}  // namespace

CastService::CastService(CastService::Configuration config)
    : local_endpoint_(DetermineEndpoint(config.interface)),
      credentials_(std::move(config.credentials)),
      agent_(config.task_runner, credentials_.provider.get()),
      mirroring_application_(config.task_runner,
                             local_endpoint_.address,
                             &agent_),
      socket_factory_(&agent_, agent_.cast_socket_client()),
      connection_factory_(
          TlsConnectionFactory::CreateFactory(&socket_factory_,
                                              config.task_runner)),
      discovery_service_(config.enable_discovery
                             ? discovery::CreateDnsSdService(
                                   config.task_runner,
                                   this,
                                   MakeDiscoveryConfig(config.interface))
                             : LazyDeletedDiscoveryService()),
      discovery_publisher_(
          discovery_service_
              ? MakeSerialDelete<
                    discovery::DnsSdServicePublisher<ReceiverInfo>>(
                    config.task_runner,
                    discovery_service_.get(),
                    kCastV2ServiceId,
                    ReceiverInfoToDnsSdInstance)
              : LazyDeletedDiscoveryPublisher()) {
  connection_factory_->SetListenCredentials(credentials_.tls_credentials);
  connection_factory_->Listen(local_endpoint_, kDefaultListenOptions);

  if (discovery_publisher_) {
    ReceiverInfo info;
    info.port = local_endpoint_.port;
    info.unique_id = HexEncode(config.interface.hardware_address);
    info.friendly_name = config.friendly_name;
    info.model_name = config.model_name;
    info.capabilities = kHasVideoOutput | kHasAudioOutput;
    const Error error = discovery_publisher_->Register(info);
    if (!error.ok()) {
      OnFatalError(std::move(error));
    }
  }
}

CastService::~CastService() {
  if (discovery_publisher_) {
    discovery_publisher_->DeregisterAll();
  }
}

void CastService::OnFatalError(Error error) {
  OSP_LOG_FATAL << "Encountered fatal discovery error: " << error;
}

void CastService::OnRecoverableError(Error error) {
  OSP_LOG_ERROR << "Encountered recoverable discovery error: " << error;
}

}  // namespace cast
}  // namespace openscreen