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_LOOPING_FILE_CAST_AGENT_H_
6 #define CAST_STANDALONE_SENDER_LOOPING_FILE_CAST_AGENT_H_
7 
8 #include <openssl/x509.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "absl/types/optional.h"
15 #include "cast/common/channel/cast_socket_message_port.h"
16 #include "cast/common/channel/virtual_connection_manager.h"
17 #include "cast/common/channel/virtual_connection_router.h"
18 #include "cast/common/public/cast_socket.h"
19 #include "cast/sender/public/sender_socket_factory.h"
20 #include "cast/standalone_sender/looping_file_sender.h"
21 #include "cast/streaming/environment.h"
22 #include "cast/streaming/sender_session.h"
23 #include "platform/api/scoped_wake_lock.h"
24 #include "platform/api/serial_delete_ptr.h"
25 #include "platform/base/error.h"
26 #include "platform/base/interface_info.h"
27 #include "platform/impl/task_runner.h"
28 
29 namespace openscreen {
30 namespace cast {
31 
32 // This class manages sender connections, starting with listening over TLS for
33 // connection attempts, constructing SenderSessions when OFFER messages are
34 // received, and linking Senders to the output decoder and SDL visualizer.
35 class LoopingFileCastAgent final
36     : public SenderSocketFactory::Client,
37       public VirtualConnectionRouter::SocketErrorHandler,
38       public SenderSession::Client {
39  public:
40   explicit LoopingFileCastAgent(TaskRunner* task_runner);
41   ~LoopingFileCastAgent();
42 
43   struct ConnectionSettings {
44     // The endpoint of the receiver we wish to connect to. Eventually this
45     // will come from discovery, instead of an endpoint here.
46     IPEndpoint receiver_endpoint;
47 
48     // The path to the file that we want to play.
49     std::string path_to_file;
50 
51     // The maximum bitrate. Default value means a reasonable default will be
52     // selected.
53     int max_bitrate = 0;
54 
55     // Whether the stream should include video, or just be audio only.
56     bool should_include_video = true;
57 
58     // Whether we should use the hacky RTP stream IDs for legacy android
59     // receivers, or if we should use the proper values.
60     bool use_android_rtp_hack = true;
61   };
62 
63   void Connect(ConnectionSettings settings);
64   void Stop();
65 
66   // SenderSocketFactory::Client overrides.
67   void OnConnected(SenderSocketFactory* factory,
68                    const IPEndpoint& endpoint,
69                    std::unique_ptr<CastSocket> socket) override;
70   void OnError(SenderSocketFactory* factory,
71                const IPEndpoint& endpoint,
72                Error error) override;
73 
74   // VirtualConnectionRouter::SocketErrorHandler overrides.
75   void OnClose(CastSocket* cast_socket) override;
76   void OnError(CastSocket* socket, Error error) override;
77 
78   // SenderSession::Client overrides.
79   void OnNegotiated(const SenderSession* session,
80                     SenderSession::ConfiguredSenders senders,
81                     capture_recommendations::Recommendations
82                         capture_recommendations) override;
83   void OnError(const SenderSession* session, Error error) override;
84 
85  private:
86   // Once we have a connection to the receiver we need to create and start
87   // a sender session. This method results in the OFFER/ANSWER exchange
88   // being completed and a session should be started.
89   void CreateAndStartSession();
90 
91   // Helper for stopping the current session. This is useful for when we don't
92   // want to completely stop (e.g. an issue with a specific Sender) but need
93   // to terminate the current connection.
94   void StopCurrentSession();
95 
96   // Member variables set as part of construction.
97   VirtualConnectionManager connection_manager_;
98   TaskRunner* const task_runner_;
99   SerialDeletePtr<VirtualConnectionRouter> router_;
100   SerialDeletePtr<CastSocketMessagePort> message_port_;
101   SerialDeletePtr<SenderSocketFactory> socket_factory_;
102   SerialDeletePtr<TlsConnectionFactory> connection_factory_;
103 
104   // Member variables set as part of starting up.
105   std::unique_ptr<Environment> environment_;
106   absl::optional<ConnectionSettings> connection_settings_;
107   SerialDeletePtr<ScopedWakeLock> wake_lock_;
108 
109   // Member variables set as part of a sender connection.
110   // NOTE: currently we only support a single sender connection and a
111   // single streaming session.
112   std::unique_ptr<SenderSession> current_session_;
113   std::unique_ptr<LoopingFileSender> file_sender_;
114 };
115 
116 }  // namespace cast
117 }  // namespace openscreen
118 
119 #endif  // CAST_STANDALONE_SENDER_LOOPING_FILE_CAST_AGENT_H_
120