1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_recv_thread.hpp

215 lines
6 KiB
C++
Raw Normal View History

//
// Copyright (c) 2013-2021 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//
#ifndef SRS_APP_RECV_THREAD_HPP
#define SRS_APP_RECV_THREAD_HPP
#include <srs_core.hpp>
#include <vector>
2020-06-18 03:45:43 +00:00
#include <string>
#include <srs_app_st.hpp>
2015-09-22 01:01:47 +00:00
#include <srs_protocol_stream.hpp>
#include <srs_core_performance.hpp>
#include <srs_app_reload.hpp>
class SrsRtmpServer;
class SrsCommonMessage;
class SrsRtmpConn;
2021-05-16 08:14:00 +00:00
class SrsLiveSource;
class SrsRequest;
2021-05-16 08:14:00 +00:00
class SrsLiveConsumer;
class SrsHttpConn;
class SrsResponseOnlyHttpConn;
2019-04-30 00:24:52 +00:00
// The message consumer which consume a message.
2017-01-23 08:38:23 +00:00
class ISrsMessageConsumer
{
public:
2017-01-23 08:38:23 +00:00
ISrsMessageConsumer();
virtual ~ISrsMessageConsumer();
public:
2019-04-30 00:24:52 +00:00
// Consume the received message.
// @remark user must free this message.
2018-01-01 11:39:57 +00:00
virtual srs_error_t consume(SrsCommonMessage* msg) = 0;
2017-01-23 08:38:23 +00:00
};
2019-04-30 00:24:52 +00:00
// The message pumper to pump messages to processer.
2017-01-23 08:38:23 +00:00
class ISrsMessagePumper : public ISrsMessageConsumer
{
public:
ISrsMessagePumper();
virtual ~ISrsMessagePumper();
public:
2019-04-30 00:24:52 +00:00
// Whether the pumper is interrupted.
// For example, when pumpter is busy, it's interrupted,
// please wait for a while then try to feed the pumper.
2017-01-23 08:38:23 +00:00
virtual bool interrupted() = 0;
2019-04-30 00:24:52 +00:00
// Interrupt the pumper for a error.
2018-01-01 11:39:57 +00:00
virtual void interrupt(srs_error_t error) = 0;
2019-04-30 00:24:52 +00:00
// When start the pumper.
2017-01-23 08:38:23 +00:00
virtual void on_start() = 0;
2019-04-30 00:24:52 +00:00
// When stop the pumper.
2017-01-23 08:38:23 +00:00
virtual void on_stop() = 0;
};
2019-04-30 00:24:52 +00:00
// The recv thread, use message handler to handle each received message.
class SrsRecvThread : public ISrsCoroutineHandler
{
protected:
SrsCoroutine* trd;
2017-01-23 08:38:23 +00:00
ISrsMessagePumper* pumper;
SrsRtmpServer* rtmp;
SrsContextId _parent_cid;
// The recv timeout in srs_utime_t.
srs_utime_t timeout;
public:
// Constructor.
// @param tm The receive timeout in srs_utime_t.
SrsRecvThread(ISrsMessagePumper* p, SrsRtmpServer* r, srs_utime_t tm, SrsContextId parent_cid);
virtual ~SrsRecvThread();
public:
virtual SrsContextId cid();
public:
virtual srs_error_t start();
virtual void stop();
virtual void stop_loop();
2019-04-30 00:30:13 +00:00
// Interface ISrsReusableThread2Handler
public:
virtual srs_error_t cycle();
private:
virtual srs_error_t do_cycle();
};
2019-04-30 00:24:52 +00:00
// The recv thread used to replace the timeout recv,
// which hurt performance for the epoll_ctrl is frequently used.
// @see: SrsRtmpConn::playing
// @see: https://github.com/ossrs/srs/issues/217
2017-01-23 08:38:23 +00:00
class SrsQueueRecvThread : public ISrsMessagePumper
{
private:
std::vector<SrsCommonMessage*> queue;
SrsRecvThread trd;
SrsRtmpServer* rtmp;
2019-04-30 00:24:52 +00:00
// The recv thread error code.
2018-01-01 11:39:57 +00:00
srs_error_t recv_error;
2021-05-16 08:14:00 +00:00
SrsLiveConsumer* _consumer;
public:
2019-04-09 01:39:16 +00:00
// TODO: FIXME: Refine timeout in time unit.
2021-05-16 08:14:00 +00:00
SrsQueueRecvThread(SrsLiveConsumer* consumer, SrsRtmpServer* rtmp_sdk, srs_utime_t tm, SrsContextId parent_cid);
virtual ~SrsQueueRecvThread();
public:
virtual srs_error_t start();
virtual void stop();
public:
virtual bool empty();
virtual int size();
virtual SrsCommonMessage* pump();
2018-01-01 11:39:57 +00:00
virtual srs_error_t error_code();
2019-04-30 00:30:13 +00:00
// Interface ISrsMessagePumper
public:
2018-01-01 11:39:57 +00:00
virtual srs_error_t consume(SrsCommonMessage* msg);
2017-01-23 08:38:23 +00:00
virtual bool interrupted();
2018-01-01 11:39:57 +00:00
virtual void interrupt(srs_error_t err);
2017-01-23 08:38:23 +00:00
virtual void on_start();
virtual void on_stop();
};
2019-04-30 00:24:52 +00:00
// The publish recv thread got message and callback the source method to process message.
// @see: https://github.com/ossrs/srs/issues/237
class SrsPublishRecvThread : public ISrsMessagePumper, public ISrsReloadHandler
#ifdef SRS_PERF_MERGED_READ
, public IMergeReadHandler
#endif
{
private:
uint32_t nn_msgs_for_yield_;
SrsRecvThread trd;
SrsRtmpServer* rtmp;
SrsRequest* req;
2019-04-30 00:24:52 +00:00
// The msgs already got.
int64_t _nb_msgs;
// The video frames we got.
uint64_t video_frames;
2019-04-30 00:24:52 +00:00
// For mr(merged read),
2015-11-11 02:37:50 +00:00
// @see https://github.com/ossrs/srs/issues/241
bool mr;
int mr_fd;
2019-04-09 01:20:44 +00:00
srs_utime_t mr_sleep;
2019-04-30 00:24:52 +00:00
// For realtime
2015-11-11 02:37:50 +00:00
// @see https://github.com/ossrs/srs/issues/257
bool realtime;
2019-04-30 00:24:52 +00:00
// The recv thread error code.
2018-01-01 11:39:57 +00:00
srs_error_t recv_error;
SrsRtmpConn* _conn;
2019-04-30 00:24:52 +00:00
// The params for conn callback.
2021-05-16 08:14:00 +00:00
SrsLiveSource* _source;
2019-04-30 00:24:52 +00:00
// The error timeout cond
2015-11-11 02:37:50 +00:00
// @see https://github.com/ossrs/srs/issues/244
srs_cond_t error;
2019-04-30 00:24:52 +00:00
// The merged context id.
SrsContextId cid;
SrsContextId ncid;
public:
2019-04-17 01:21:38 +00:00
SrsPublishRecvThread(SrsRtmpServer* rtmp_sdk, SrsRequest* _req,
2021-05-16 08:14:00 +00:00
int mr_sock_fd, srs_utime_t tm, SrsRtmpConn* conn, SrsLiveSource* source, SrsContextId parent_cid);
virtual ~SrsPublishRecvThread();
public:
2019-04-30 00:24:52 +00:00
// Wait for error for some timeout.
virtual srs_error_t wait(srs_utime_t tm);
virtual int64_t nb_msgs();
virtual uint64_t nb_video_frames();
2018-01-01 11:39:57 +00:00
virtual srs_error_t error_code();
2020-08-30 01:53:10 +00:00
virtual void set_cid(SrsContextId v);
virtual SrsContextId get_cid();
public:
virtual srs_error_t start();
virtual void stop();
2019-04-30 00:30:13 +00:00
// Interface ISrsMessagePumper
public:
2018-01-01 11:39:57 +00:00
virtual srs_error_t consume(SrsCommonMessage* msg);
2017-01-23 08:38:23 +00:00
virtual bool interrupted();
2018-01-01 11:39:57 +00:00
virtual void interrupt(srs_error_t err);
2017-01-23 08:38:23 +00:00
virtual void on_start();
virtual void on_stop();
2019-04-30 00:30:13 +00:00
// Interface IMergeReadHandler
public:
#ifdef SRS_PERF_MERGED_READ
virtual void on_read(ssize_t nread);
#endif
2019-04-30 00:30:13 +00:00
// Interface ISrsReloadHandler
public:
2017-09-22 08:14:30 +00:00
virtual srs_error_t on_reload_vhost_publish(std::string vhost);
virtual srs_error_t on_reload_vhost_realtime(std::string vhost);
private:
2019-04-09 01:20:44 +00:00
virtual void set_socket_buffer(srs_utime_t sleep_v);
};
2019-04-30 00:24:52 +00:00
// The HTTP receive thread, try to read messages util EOF.
// For example, the HTTP FLV serving thread will use the receive thread to break
// when client closed the request, to avoid FD leak.
// @see https://github.com/ossrs/srs/issues/636#issuecomment-298208427
class SrsHttpRecvThread : public ISrsCoroutineHandler
{
private:
SrsResponseOnlyHttpConn* conn;
SrsCoroutine* trd;
public:
SrsHttpRecvThread(SrsResponseOnlyHttpConn* c);
virtual ~SrsHttpRecvThread();
public:
virtual srs_error_t start();
public:
2017-07-29 13:39:57 +00:00
virtual srs_error_t pull();
2019-04-30 00:30:13 +00:00
// Interface ISrsOneCycleThreadHandler
public:
virtual srs_error_t cycle();
};
#endif