Ocean
StreamingClient.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef FACEBOOK_NETWORK_STREAMING_CLIENT_H
9 #define FACEBOOK_NETWORK_STREAMING_CLIENT_H
10 
11 #include "ocean/network/Network.h"
12 #include "ocean/network/Address4.h"
13 #include "ocean/network/Port.h"
17 
18 #include "ocean/base/Callback.h"
19 #include "ocean/base/Lock.h"
20 
21 #include <queue>
22 
23 namespace Ocean
24 {
25 
26 namespace Network
27 {
28 
29 /**
30  * This class implements a streaming client.<br>
31  * The client uses a TCP connection for configuration tasks and a UDP connection for the data transfer.
32  * @ingroup network
33  */
34 class OCEAN_NETWORK_EXPORT StreamingClient : public Streaming
35 {
36  public:
37 
38  /**
39  * Definition of a callback function for streaming server requests.
40  */
42 
43  /**
44  * Definition of a callback function for streaming data.
45  * Parameter 0 provides the buffer that has been received
46  * Parameter 1 provides the size of the buffer, in bytes
47  */
49 
50  /**
51  * Definition of a vector holding channels.
52  */
53  typedef std::vector<std::string> Channels;
54 
55  public:
56 
57  /**
58  * Creates a new streaming client.
59  */
61 
62  /**
63  * Destructs a streaming client.
64  */
66 
67  /**
68  * Connects the streaming client with a remote streaming server.
69  * @param address Address4 of the streaming server
70  * @param port Port of the streaming server
71  * @return True, if succeeded
72  */
73  bool connect(const Address4& address, const Port& port);
74 
75  /**
76  * Disconnects the streaming client.
77  * @return True, if the client was connected before
78  */
79  bool disconnect();
80 
81  /**
82  * Returns whether this client is currently connected with a streaming server.
83  * @return True, if so
84  */
85  bool isConnected() const;
86 
87  /**
88  * Returns a list of selectable channels provides by the streaming server.
89  * @return Selectable channels
90  */
92 
93  /**
94  * Returns a data type of a specified channel provides by the streaming server.
95  * @param channel Channel to return the data type for
96  * @return Data type
97  */
98  std::string channelDataType(const std::string& channel);
99 
100  /**
101  * Returns the selected channel of the streaming server.
102  * @return Selected channel
103  */
104  inline const std::string& channel() const;
105 
106  /**
107  * Returns the type of the streaming data.
108  * @return Data type
109  */
110  inline const std::string& dataType() const;
111 
112  /**
113  * Returns the address of the connected streaming server.
114  * @return Streaming server address
115  */
116  inline const Address4& serverAddress() const;
117 
118  /**
119  * Returns the port of the connected streaming server.
120  * @return Streaming server port
121  */
122  inline const Port& serverPort() const;
123 
124  /**
125  * (Re-)Starts the streaming.
126  * @param channel Streaming channel, if any
127  * @return True, if succeeded
128  */
129  bool start(const std::string& channel = std::string());
130 
131  /**
132  * Pauses the streaming.
133  * @return True, if succeeded
134  */
135  bool pause();
136 
137  /**
138  * Stops the streaming.
139  * @return True, if succeeded
140  */
141  bool stop();
142 
143  /**
144  * Returns whether this client is currently receiving streaming data.<br>
145  * Beware: If the client is paused the client still is handled as in receiving mode.
146  * @return True, if so
147  */
148  inline bool isReceiving();
149 
150  /**
151  * Returns whether this client is currently paused.
152  * @return True, if so
153  */
154  inline bool isPaused();
155 
156  /**
157  * Sets the callback function for streaming server requests (like e.g. start or stop requests).
158  * @param callback Callback function to set
159  */
160  inline void setRequestCallback(const RequestCallback& callback);
161 
162  /**
163  * Sets the callback function for streaming data received from the streaming server.
164  * @param callback Callback function to set
165  */
166  inline void setReceiveCallback(const ReceiveCallback& callback);
167 
168  protected:
169 
170  /**
171  * New command function.
172  * @param command New command to handle
173  * @param value Command value
174  * @param sessionId Unique id of the server which is not unique on the client side
175  */
176  void onCommand(const std::string& command, const std::string& value, const SessionId sessionId);
177 
178  /**
179  * Function handling start commands.
180  * @param value Command value
181  * @param sessionId Unique id of the server which is not unique on the client side
182  */
183  void onStart(const std::string& value, const SessionId sessionId);
184 
185  /**
186  * Function handling pause commands.
187  * @param value Command value
188  * @param sessionId Unique id of the server which is not unique on the client side
189  */
190  void onPause(const std::string& value, const SessionId sessionId);
191 
192  /**
193  * Function handling stop commands.
194  * @param value Command value
195  * @param sessionId Unique id of the server which is not unique on the client side
196  */
197  void onStop(const std::string& value, const SessionId sessionId);
198 
199  /**
200  * Function handling changed data type commands.
201  * @param value Command value
202  * @param sessionId Unique id of the server which is not unique on the client side
203  */
204  void onChangedDataType(const std::string& value, const SessionId sessionId);
205 
206  /**
207  * Callback function for TCP receive message.
208  * @param data Received data
209  * @param size Size of the received data, in bytes
210  */
211  void onTCPReceiveData(const void* data, const size_t size);
212 
213  /**
214  * Callback function for UDP receive message.
215  * @param address Address4 of the sender
216  * @param port Port of the sender
217  * @param data Received data
218  * @param size Size of the received data
219  * @param messageId Unique message id for the received message
220  */
221  void onUDPReceiveData(const Address4& address, const Port& port, const void* data, const size_t size, const PackagedSocket::MessageId messageId);
222 
223  protected:
224 
225  /// Stream channel.
226  std::string channel_;
227 
228  /// Stream data type.
229  std::string dataType_;
230 
231  /// Determines whether the client is currently receiving.
232  bool isReceiving_ = false;
233 
234  /// Determines whether the client is pause.
235  bool isPaused_ = false;
236 
237  /// UDP client.
239 
240  /// TCP client.
242 
243  /// Address4 of the streaming server.
245 
246  /// Port of the streaming server.
248 
249  /// Streaming server request callback function.
251 
252  /// Streaming data receive callback function.
254 
255  /// Client lock.
256  mutable Lock lock_;
257 };
258 
259 inline const std::string& StreamingClient::channel() const
260 {
261  return channel_;
262 }
263 
264 inline const std::string& StreamingClient::dataType() const
265 {
266  return dataType_;
267 }
268 
270 {
271  return isReceiving_;
272 }
273 
275 {
276  return isPaused_;
277 }
278 
280 {
281  return serverAddress_;
282 }
283 
284 inline const Port& StreamingClient::serverPort() const
285 {
286  return serverPort_;
287 }
288 
290 {
291  requestCallback_ = callback;
292 }
293 
295 {
296  receiveCallback_ = callback;
297 }
298 
299 }
300 
301 }
302 
303 #endif // FACEBOOK_NETWORK_STREAMING_CLIENT_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class wraps an address number with 32 bits.
Definition: Address4.h:26
uint32_t MessageId
Definition of a message id.
Definition: PackagedSocket.h:185
This class implements a packaged UDP server.
Definition: PackagedUDPServer.h:25
This class wraps a port number with 16 bits.
Definition: Port.h:26
This class implements a streaming client.
Definition: StreamingClient.h:35
bool isReceiving_
Determines whether the client is currently receiving.
Definition: StreamingClient.h:232
void onStart(const std::string &value, const SessionId sessionId)
Function handling start commands.
Callback< void, const void *, const size_t > ReceiveCallback
Definition of a callback function for streaming data.
Definition: StreamingClient.h:48
bool isReceiving()
Returns whether this client is currently receiving streaming data.
Definition: StreamingClient.h:269
bool stop()
Stops the streaming.
void onPause(const std::string &value, const SessionId sessionId)
Function handling pause commands.
const Port & serverPort() const
Returns the port of the connected streaming server.
Definition: StreamingClient.h:284
bool connect(const Address4 &address, const Port &port)
Connects the streaming client with a remote streaming server.
~StreamingClient()
Destructs a streaming client.
bool pause()
Pauses the streaming.
void setReceiveCallback(const ReceiveCallback &callback)
Sets the callback function for streaming data received from the streaming server.
Definition: StreamingClient.h:294
bool isConnected() const
Returns whether this client is currently connected with a streaming server.
StreamingClient()
Creates a new streaming client.
std::string channelDataType(const std::string &channel)
Returns a data type of a specified channel provides by the streaming server.
RequestCallback requestCallback_
Streaming server request callback function.
Definition: StreamingClient.h:250
const Address4 & serverAddress() const
Returns the address of the connected streaming server.
Definition: StreamingClient.h:279
const std::string & dataType() const
Returns the type of the streaming data.
Definition: StreamingClient.h:264
Address4 serverAddress_
Address4 of the streaming server.
Definition: StreamingClient.h:244
void onChangedDataType(const std::string &value, const SessionId sessionId)
Function handling changed data type commands.
void onUDPReceiveData(const Address4 &address, const Port &port, const void *data, const size_t size, const PackagedSocket::MessageId messageId)
Callback function for UDP receive message.
bool start(const std::string &channel=std::string())
(Re-)Starts the streaming.
Callback< bool, const State, const std::string & > RequestCallback
Definition of a callback function for streaming server requests.
Definition: StreamingClient.h:41
std::vector< std::string > Channels
Definition of a vector holding channels.
Definition: StreamingClient.h:53
PackagedUDPServer udpClient_
UDP client.
Definition: StreamingClient.h:238
void onTCPReceiveData(const void *data, const size_t size)
Callback function for TCP receive message.
ReceiveCallback receiveCallback_
Streaming data receive callback function.
Definition: StreamingClient.h:253
void onStop(const std::string &value, const SessionId sessionId)
Function handling stop commands.
Channels selectableChannels()
Returns a list of selectable channels provides by the streaming server.
const std::string & channel() const
Returns the selected channel of the streaming server.
Definition: StreamingClient.h:259
bool disconnect()
Disconnects the streaming client.
TCPClient tcpClient_
TCP client.
Definition: StreamingClient.h:241
Lock lock_
Client lock.
Definition: StreamingClient.h:256
std::string channel_
Stream channel.
Definition: StreamingClient.h:226
void onCommand(const std::string &command, const std::string &value, const SessionId sessionId)
New command function.
void setRequestCallback(const RequestCallback &callback)
Sets the callback function for streaming server requests (like e.g.
Definition: StreamingClient.h:289
Port serverPort_
Port of the streaming server.
Definition: StreamingClient.h:247
bool isPaused()
Returns whether this client is currently paused.
Definition: StreamingClient.h:274
bool isPaused_
Determines whether the client is pause.
Definition: StreamingClient.h:235
std::string dataType_
Stream data type.
Definition: StreamingClient.h:229
This class is the base class for all streaming objects.
Definition: Streaming.h:25
MessageQueue::Id SessionId
Definition of a session id.
Definition: Streaming.h:46
This class implements a TCP client.
Definition: TCPClient.h:27
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15