1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <functional>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "host/frontend/webrtc/lib/connection_observer.h"
27 #include "host/frontend/webrtc/lib/video_sink.h"
28 #include "host/frontend/webrtc/lib/ws_connection.h"
29 
30 namespace cuttlefish {
31 namespace webrtc_streaming {
32 
33 class ClientHandler;
34 
35 struct StreamerConfig {
36   // The id with which to register with the operator server.
37   std::string device_id;
38   struct {
39     // The ip address or domain name of the operator server.
40     std::string addr;
41     int port;
42     // The path component of the operator server's register url.
43     std::string path;
44     // The security level to use when connecting to the operator server.
45     WsConnection::Security security;
46   } operator_server;
47   // The port ranges webrtc is allowed to use.
48   // [0,0] means all ports
49   std::pair<uint16_t, uint16_t> udp_port_range = {15550, 15558};
50   std::pair<uint16_t, uint16_t> tcp_port_range = {15550, 15558};
51 };
52 
53 class OperatorObserver {
54  public:
55   // Called when the websocket connection with the operator is established.
56   virtual void OnRegistered() = 0;
57   // Called when the websocket connection with the operator is closed.
58   virtual void OnClose() = 0;
59   // Called when an error is encountered in the connection to the operator.
60   virtual void OnError() = 0;
61 };
62 
63 class Streamer {
64  public:
65   // The observer_factory will be used to create an observer for every new
66   // client connection. Unregister() needs to be called to stop accepting
67   // connections.
68   static std::shared_ptr<Streamer> Create(
69       const StreamerConfig& cfg,
70       std::shared_ptr<ConnectionObserverFactory> factory);
71   virtual ~Streamer() = default;
72 
73   virtual std::shared_ptr<VideoSink> AddDisplay(const std::string& label,
74                                                 int width, int height, int dpi,
75                                                 bool touch_enabled) = 0;
76 
77   // TODO (b/128328845): Implement audio, return a shared_ptr to a class
78   // equivalent to webrtc::AudioSinkInterface.
79   virtual void AddAudio(const std::string& label) = 0;
80 
81   // Register with the operator.
82   virtual void Register(std::weak_ptr<OperatorObserver> operator_observer) = 0;
83   virtual void Unregister() = 0;
84 
85  protected:
86   Streamer() = default;
87 };
88 
89 }  // namespace webrtc_streaming
90 }  // namespace cuttlefish
91