Ocean
Loading...
Searching...
No Matches
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
13#include "ocean/network/Port.h"
17
18#include "ocean/base/Callback.h"
19#include "ocean/base/Lock.h"
20
21#include <queue>
22
23namespace Ocean
24{
25
26namespace 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 */
34class 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 data type and extra data changes.
45 * Parameter 0 provides the new data type
46 * Parameter 1 provides the new extra data (may be empty)
47 * Return True, to accept the change
48 */
50
51 /**
52 * Definition of a callback function for streaming data.
53 * Parameter 0 provides the buffer that has been received
54 * Parameter 1 provides the size of the buffer, in bytes
55 */
57
58 /**
59 * Definition of a vector holding channels.
60 */
62
63 public:
64
65 /**
66 * Creates a new streaming client.
67 */
69
70 /**
71 * Destructs a streaming client.
72 */
74
75 /**
76 * Connects the streaming client with a remote streaming server.
77 * @param address Address4 of the streaming server
78 * @param port Port of the streaming server
79 * @return True, if succeeded
80 */
81 bool connect(const Address4& address, const Port& port);
82
83 /**
84 * Disconnects the streaming client.
85 * @return True, if the client was connected before
86 */
87 bool disconnect();
88
89 /**
90 * Returns whether this client is currently connected with a streaming server.
91 * @return True, if so
92 */
93 bool isConnected() const;
94
95 /**
96 * Returns a list of selectable channels provides by the streaming server.
97 * @return Selectable channels
98 */
100
101 /**
102 * Returns a data type of a specified channel provides by the streaming server.
103 * @param channel Channel to return the data type for
104 * @return Data type
105 */
106 std::string channelDataType(const std::string& channel);
107
108 /**
109 * Returns extra data of a specified channel provides by the streaming server.
110 * @param channel Channel to return the extra data for
111 * @return Extra data (may be empty)
112 */
113 Buffer channelExtraData(const std::string& channel);
114
115 /**
116 * Returns the selected channel of the streaming server.
117 * @return Selected channel
118 */
119 inline std::string channel() const;
120
121 /**
122 * Returns the type of the streaming data.
123 * @return Data type
124 */
125 inline std::string dataType() const;
126
127 /**
128 * Returns the extra data of the streaming channel.
129 * @return Extra data (may be empty)
130 */
131 inline Buffer extraData() const;
132
133 /**
134 * Returns the address of the connected streaming server.
135 * @return Streaming server address
136 */
137 inline Address4 serverAddress() const;
138
139 /**
140 * Returns the port of the connected streaming server.
141 * @return Streaming server port
142 */
143 inline Port serverPort() const;
144
145 /**
146 * (Re-)Starts the streaming.
147 * @param channel Streaming channel, if any
148 * @return True, if succeeded
149 */
150 bool start(const std::string& channel = std::string());
151
152 /**
153 * Pauses the streaming.
154 * @return True, if succeeded
155 */
156 bool pause();
157
158 /**
159 * Stops the streaming.
160 * @return True, if succeeded
161 */
162 bool stop();
163
164 /**
165 * Returns whether this client is currently receiving streaming data.<br>
166 * Beware: If the client is paused the client still is handled as in receiving mode.
167 * @return True, if so
168 */
169 inline bool isReceiving();
170
171 /**
172 * Returns whether this client is currently paused.
173 * @return True, if so
174 */
175 inline bool isPaused();
176
177 /**
178 * Sets the callback function for streaming server requests (like e.g. start or stop requests).
179 * @param callback Callback function to set
180 */
181 inline void setRequestCallback(const RequestCallback& callback);
182
183 /**
184 * Sets the callback function for data type and extra data changes from the streaming server.
185 * @param callback Callback function to set
186 */
187 inline void setDataTypeChangedCallback(const DataTypeChangedCallback& callback);
188
189 /**
190 * Sets the callback function for streaming data received from the streaming server.
191 * @param callback Callback function to set
192 */
193 inline void setReceiveCallback(const ReceiveCallback& callback);
194
195 protected:
196
197 /**
198 * New command function.
199 * @param command New command to handle
200 * @param value Command value
201 * @param sessionId Unique id of the server which is not unique on the client side
202 */
203 void onCommand(const std::string& command, const std::string& value, const SessionId sessionId);
204
205 /**
206 * Function handling start commands.
207 * @param value Command value
208 * @param sessionId Unique id of the server which is not unique on the client side
209 */
210 void onStart(const std::string& value, const SessionId sessionId);
211
212 /**
213 * Function handling pause commands.
214 * @param value Command value
215 * @param sessionId Unique id of the server which is not unique on the client side
216 */
217 void onPause(const std::string& value, const SessionId sessionId);
218
219 /**
220 * Function handling stop commands.
221 * @param value Command value
222 * @param sessionId Unique id of the server which is not unique on the client side
223 */
224 void onStop(const std::string& value, const SessionId sessionId);
225
226 /**
227 * Function handling changed data type commands.
228 * @param value Command value (format: "dataType|base64EncodedExtraData")
229 * @param sessionId Unique id of the server which is not unique on the client side
230 */
231 void onChangedDataType(const std::string& value, const SessionId sessionId);
232
233 /**
234 * Callback function for TCP receive message.
235 * @param data Received data
236 * @param size Size of the received data, in bytes
237 */
238 void onTCPReceiveData(const void* data, const size_t size);
239
240 /**
241 * Callback function for UDP receive message.
242 * @param address Address4 of the sender
243 * @param port Port of the sender
244 * @param data Received data
245 * @param size Size of the received data
246 * @param messageId Unique message id for the received message
247 */
248 void onUDPReceiveData(const Address4& address, const Port& port, const void* data, const size_t size, const PackagedSocket::MessageId messageId);
249
250 protected:
251
252 /// Stream channel.
253 std::string channel_;
254
255 /// Stream data type.
256 std::string dataType_;
257
258 /// Stream extra data (e.g., codec configuration).
260
261 /// Determines whether the client is currently receiving.
262 bool isReceiving_ = false;
263
264 /// Determines whether the client is pause.
265 bool isPaused_ = false;
266
267 /// UDP client.
269
270 /// TCP client.
272
273 /// Address4 of the streaming server.
275
276 /// Port of the streaming server.
278
279 /// Streaming server request callback function.
281
282 /// Data type and extra data changed callback function.
284
285 /// Streaming data receive callback function.
287
288 /// Client lock.
289 mutable Lock lock_;
290};
291
292inline std::string StreamingClient::channel() const
293{
294 const ScopedLock scopedLock(lock_);
295 return channel_;
296}
297
298inline std::string StreamingClient::dataType() const
299{
300 const ScopedLock scopedLock(lock_);
301 return dataType_;
302}
303
305{
306 const ScopedLock scopedLock(lock_);
307 return extraData_;
308}
309
311{
312 const ScopedLock scopedLock(lock_);
313 return isReceiving_;
314}
315
317{
318 const ScopedLock scopedLock(lock_);
319 return isPaused_;
320}
321
323{
324 const ScopedLock scopedLock(lock_);
325 return serverAddress_;
326}
327
329{
330 const ScopedLock scopedLock(lock_);
331 return serverPort_;
332}
333
335{
336 const ScopedLock scopedLock(lock_);
337 requestCallback_ = callback;
338}
339
341{
342 const ScopedLock scopedLock(lock_);
343 dataTypeChangedCallback_ = callback;
344}
345
347{
348 const ScopedLock scopedLock(lock_);
349 receiveCallback_ = callback;
350}
351
352}
353
354}
355
356#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:192
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:262
void onStart(const std::string &value, const SessionId sessionId)
Function handling start commands.
DataTypeChangedCallback dataTypeChangedCallback_
Data type and extra data changed callback function.
Definition StreamingClient.h:283
bool isReceiving()
Returns whether this client is currently receiving streaming data.
Definition StreamingClient.h:310
bool stop()
Stops the streaming.
void onPause(const std::string &value, const SessionId sessionId)
Function handling pause commands.
Buffer channelExtraData(const std::string &channel)
Returns extra data of a specified channel provides by the streaming server.
Address4 serverAddress() const
Returns the address of the connected streaming server.
Definition StreamingClient.h:322
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:346
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.
Buffer extraData_
Stream extra data (e.g., codec configuration).
Definition StreamingClient.h:259
RequestCallback requestCallback_
Streaming server request callback function.
Definition StreamingClient.h:280
Address4 serverAddress_
Address4 of the streaming server.
Definition StreamingClient.h:274
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.
std::string channel() const
Returns the selected channel of the streaming server.
Definition StreamingClient.h:292
bool start(const std::string &channel=std::string())
(Re-)Starts the streaming.
PackagedUDPServer udpClient_
UDP client.
Definition StreamingClient.h:268
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:286
Buffer extraData() const
Returns the extra data of the streaming channel.
Definition StreamingClient.h:304
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.
std::string dataType() const
Returns the type of the streaming data.
Definition StreamingClient.h:298
bool disconnect()
Disconnects the streaming client.
void setDataTypeChangedCallback(const DataTypeChangedCallback &callback)
Sets the callback function for data type and extra data changes from the streaming server.
Definition StreamingClient.h:340
TCPClient tcpClient_
TCP client.
Definition StreamingClient.h:271
Lock lock_
Client lock.
Definition StreamingClient.h:289
Port serverPort() const
Returns the port of the connected streaming server.
Definition StreamingClient.h:328
Strings Channels
Definition of a vector holding channels.
Definition StreamingClient.h:61
std::string channel_
Stream channel.
Definition StreamingClient.h:253
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:334
Port serverPort_
Port of the streaming server.
Definition StreamingClient.h:277
bool isPaused()
Returns whether this client is currently paused.
Definition StreamingClient.h:316
bool isPaused_
Determines whether the client is pause.
Definition StreamingClient.h:265
std::string dataType_
Stream data type.
Definition StreamingClient.h:256
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:51
std::vector< uint8_t > Buffer
Definition of a vector holding bytes.
Definition Streaming.h:31
This class implements a TCP client.
Definition TCPClient.h:27
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
std::vector< std::string > Strings
Definition of a vector holding strings.
Definition Base.h:162
The namespace covering the entire Ocean framework.
Definition Accessor.h:15