libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
rtmpclient.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_NETWORK
13#include <cstdint>
14
15#include <promeki/atomic.h>
16#include <promeki/duration.h>
17#include <promeki/error.h>
18#include <promeki/flvtag.h>
19#include <promeki/list.h>
20#include <promeki/metadata.h>
21#include <promeki/mutex.h>
22#include <promeki/namespace.h>
23#include <promeki/objectbase.h>
24#include <promeki/queue.h>
25#include <promeki/result.h>
26#include <promeki/rtmpmessage.h>
27#include <promeki/rtmpsession.h>
28#include <promeki/string.h>
29#include <promeki/uniqueptr.h>
30#include <promeki/url.h>
31
32#if PROMEKI_ENABLE_TLS
33#include <promeki/sslcontext.h>
34#endif
35
36PROMEKI_NAMESPACE_BEGIN
37
38class TcpSocket;
39class Thread;
40
88class RtmpClient : public ObjectBase {
89 PROMEKI_OBJECT(RtmpClient, ObjectBase)
90 public:
92 static constexpr int DefaultSendQueueDepth = 64;
93
95 static constexpr int DefaultReadQueueDepth = 64;
96
98 static constexpr unsigned int DefaultConnectTimeoutMs = 10000;
99
106 explicit RtmpClient(ObjectBase *parent = nullptr);
107
109 ~RtmpClient() override;
110
111#if PROMEKI_ENABLE_TLS
126 void setSslContext(SslContext ctx) { _sslContext = std::move(ctx); }
127#endif
128
139 Error open(const Url &url,
140 const RtmpConnectOptions &opts = {},
141 unsigned int timeoutMs = DefaultConnectTimeoutMs);
142
153 Error publish(const String &streamKey = String(),
154 const String &mode = "live",
155 unsigned int timeoutMs = 5000);
156
163 Error play(const String &streamKey = String(),
164 unsigned int timeoutMs = 5000,
165 bool useFcSubscribe = false);
166
175 Error sendVideo(const FlvVideoTag &tag, uint32_t timestampMs);
176
182 Error sendAudio(const FlvAudioTag &tag, uint32_t timestampMs);
183
195 Error sendMetadata(const Metadata &meta, uint32_t timestampMs = 0);
196
205 Result<FlvVideoTag> takeVideo(unsigned int timeoutMs);
206
211 Result<FlvAudioTag> takeAudio(unsigned int timeoutMs);
212
217 Result<Metadata> takeMetadata(unsigned int timeoutMs);
218
224 Error close();
225
227 const Url &url() const { return _url; }
228
230 const String &app() const { return _app; }
231
233 const String &streamKey() const { return _streamKey; }
234
236 uint32_t streamId() const { return _streamId.value(); }
237
239 bool isOpen() const { return _open.value(); }
240
242 RtmpSession *session() { return _session.get(); }
243
244 // ---- Stats ----
245
247 int64_t bytesSent() const { return _bytesSent.value(); }
248
250 int64_t bytesReceived() const { return _bytesReceived.value(); }
251
253 int64_t videoMessagesSent() const { return _videoMessagesSent.value(); }
254
256 int64_t audioMessagesSent() const { return _audioMessagesSent.value(); }
257
259 int64_t videoMessagesReceived() const { return _videoMessagesReceived.value(); }
260
262 int64_t audioMessagesReceived() const { return _audioMessagesReceived.value(); }
263
274 Duration rttEstimate() const { return Duration(); }
275
276 // ---- Signals ----
277
279 PROMEKI_SIGNAL(connected);
280
282 PROMEKI_SIGNAL(disconnected, Error);
283
285 PROMEKI_SIGNAL(metadataReceived, Metadata);
286
305 static void splitPath(const Url &url, String &app, String &streamKey);
306
307 private:
308 class WriterThread;
309 class ReaderThread;
310
311 void teardown(Error reason);
312 void startMediaThreads();
313
314 Url _url;
315 String _app;
316 String _streamKey;
317 Atomic<uint32_t> _streamId{0};
318 Atomic<bool> _open{false};
319 Atomic<bool> _stopping{false};
320
321 UniquePtr<TcpSocket> _socket;
322 UniquePtr<RtmpSession> _session;
323 UniquePtr<WriterThread> _writer;
324 UniquePtr<ReaderThread> _reader;
325
326#if PROMEKI_ENABLE_TLS
327 SslContext _sslContext;
328 bool _isTls = false;
329#endif
330
331 Queue<RtmpMessage> _writeQueue;
332 Queue<FlvVideoTag> _videoRxQueue;
333 Queue<FlvAudioTag> _audioRxQueue;
334 Queue<Metadata> _metadataRxQueue;
335
336 Atomic<int64_t> _bytesSent{0};
337 Atomic<int64_t> _bytesReceived{0};
338 Atomic<int64_t> _videoMessagesSent{0};
339 Atomic<int64_t> _audioMessagesSent{0};
340 Atomic<int64_t> _videoMessagesReceived{0};
341 Atomic<int64_t> _audioMessagesReceived{0};
342};
343
344PROMEKI_NAMESPACE_END
345
346#endif // PROMEKI_ENABLE_NETWORK