aboutsummaryrefslogtreecommitdiff
path: root/cast/standalone_receiver/cast_agent.h
blob: e7b92200f02fe3890e662a8185de896db58b20c3 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
// 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_STANDALONE_RECEIVER_CAST_AGENT_H_
#define CAST_STANDALONE_RECEIVER_CAST_AGENT_H_

#include <openssl/x509.h>

#include <memory>
#include <vector>

#include "cast/common/channel/cast_socket_message_port.h"
#include "cast/common/channel/virtual_connection_manager.h"
#include "cast/common/channel/virtual_connection_router.h"
#include "cast/common/public/cast_socket.h"
#include "cast/receiver/channel/device_auth_namespace_handler.h"
#include "cast/receiver/channel/static_credentials.h"
#include "cast/receiver/public/receiver_socket_factory.h"
#include "cast/standalone_receiver/streaming_playback_controller.h"
#include "cast/streaming/environment.h"
#include "cast/streaming/receiver_session.h"
#include "platform/api/scoped_wake_lock.h"
#include "platform/api/serial_delete_ptr.h"
#include "platform/base/error.h"
#include "platform/base/interface_info.h"
#include "platform/base/tls_credentials.h"
#include "platform/impl/task_runner.h"

namespace openscreen {
namespace cast {

// This class manages sender connections, starting with listening over TLS for
// connection attempts, constructing ReceiverSessions when OFFER messages are
// received, and linking Receivers to the output decoder and SDL visualizer.
//
// Consumers of this class are expected to provide a single threaded task runner
// implementation, a network interface information struct that will be used
// both for TLS listening and UDP messaging, and a credentials provider used
// for TLS listening.
class CastAgent final : public ReceiverSocketFactory::Client,
                        public VirtualConnectionRouter::SocketErrorHandler,
                        public ReceiverSession::Client,
                        public StreamingPlaybackController::Client {
 public:
  CastAgent(
      TaskRunner* task_runner,
      const InterfaceInfo& interface,
      DeviceAuthNamespaceHandler::CredentialsProvider* credentials_provider,
      TlsCredentials tls_credentials);
  ~CastAgent();

  // Initialization occurs as part of construction, however to actually bind
  // for discovery and listening over TLS, the CastAgent must be started.
  Error Start();
  Error Stop();

  // ReceiverSocketFactory::Client overrides.
  void OnConnected(ReceiverSocketFactory* factory,
                   const IPEndpoint& endpoint,
                   std::unique_ptr<CastSocket> socket) override;
  void OnError(ReceiverSocketFactory* factory, Error error) override;

  // VirtualConnectionRouter::SocketErrorHandler overrides.
  void OnClose(CastSocket* cast_socket) override;
  void OnError(CastSocket* socket, Error error) override;

  // ReceiverSession::Client overrides.
  void OnNegotiated(const ReceiverSession* session,
                    ReceiverSession::ConfiguredReceivers receivers) override;
  void OnReceiversDestroying(const ReceiverSession* session,
                             ReceiversDestroyingReason reason) override;
  void OnError(const ReceiverSession* session, Error error) override;

  // StreamingPlaybackController::Client overrides
  void OnPlaybackError(StreamingPlaybackController* controller,
                       Error error) override;

 private:
  // Helper for stopping the current session. This is useful for when we don't
  // want to completely stop (e.g. an issue with a specific Sender) but need
  // to terminate the current connection.
  void StopCurrentSession();

  // Member variables set as part of construction.
  std::unique_ptr<Environment> environment_;
  TaskRunner* const task_runner_;
  IPEndpoint receive_endpoint_;
  DeviceAuthNamespaceHandler::CredentialsProvider* credentials_provider_;
  CastSocketMessagePort message_port_;
  TlsCredentials tls_credentials_;

  // Member variables set as part of starting up.
  SerialDeletePtr<DeviceAuthNamespaceHandler> auth_handler_;
  SerialDeletePtr<TlsConnectionFactory> connection_factory_;
  VirtualConnectionManager connection_manager_;
  SerialDeletePtr<VirtualConnectionRouter> router_;
  SerialDeletePtr<ReceiverSocketFactory> socket_factory_;
  SerialDeletePtr<ScopedWakeLock> wake_lock_;

  // Member variables set as part of a sender connection.
  // NOTE: currently we only support a single sender connection and a
  // single streaming session.
  std::unique_ptr<ReceiverSession> current_session_;
  std::unique_ptr<StreamingPlaybackController> controller_;
};

}  // namespace cast
}  // namespace openscreen

#endif  // CAST_STANDALONE_RECEIVER_CAST_AGENT_H_