2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-01-01 13:37:28 +00:00
|
|
|
* Copyright (c) 2013-2019 Winlin
|
2017-03-25 09:21:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2014-11-22 09:58:02 +00:00
|
|
|
|
|
|
|
#ifndef SRS_APP_RECV_THREAD_HPP
|
|
|
|
#define SRS_APP_RECV_THREAD_HPP
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <srs_app_thread.hpp>
|
2015-09-22 01:01:47 +00:00
|
|
|
#include <srs_protocol_stream.hpp>
|
2014-12-04 05:43:55 +00:00
|
|
|
#include <srs_core_performance.hpp>
|
2014-12-04 10:21:04 +00:00
|
|
|
#include <srs_app_reload.hpp>
|
2014-11-22 09:58:02 +00:00
|
|
|
|
|
|
|
class SrsRtmpServer;
|
2014-12-05 15:03:52 +00:00
|
|
|
class SrsCommonMessage;
|
2014-12-01 15:38:51 +00:00
|
|
|
class SrsRtmpConn;
|
|
|
|
class SrsSource;
|
2014-12-04 10:21:04 +00:00
|
|
|
class SrsRequest;
|
2014-10-02 11:45:04 +00:00
|
|
|
class SrsConsumer;
|
2017-04-30 04:03:31 +00:00
|
|
|
class SrsHttpConn;
|
|
|
|
class SrsResponseOnlyHttpConn;
|
2014-11-22 09:58:02 +00:00
|
|
|
|
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
|
2014-12-01 14:39:22 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-01-23 08:38:23 +00:00
|
|
|
ISrsMessageConsumer();
|
|
|
|
virtual ~ISrsMessageConsumer();
|
2014-12-01 14:39:22 +00:00
|
|
|
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;
|
2014-12-01 14:39:22 +00:00
|
|
|
};
|
|
|
|
|
2019-04-30 00:24:52 +00:00
|
|
|
// The recv thread, use message handler to handle each received message.
|
2017-05-29 12:49:29 +00:00
|
|
|
class SrsRecvThread : public ISrsCoroutineHandler
|
2014-12-01 14:39:22 +00:00
|
|
|
{
|
|
|
|
protected:
|
2017-05-29 12:49:29 +00:00
|
|
|
SrsCoroutine* trd;
|
2017-01-23 08:38:23 +00:00
|
|
|
ISrsMessagePumper* pumper;
|
2014-12-01 14:39:22 +00:00
|
|
|
SrsRtmpServer* rtmp;
|
2019-04-17 01:21:38 +00:00
|
|
|
int _parent_cid;
|
2019-04-18 00:42:43 +00:00
|
|
|
// The recv timeout in srs_utime_t.
|
|
|
|
srs_utime_t timeout;
|
2014-12-01 14:39:22 +00:00
|
|
|
public:
|
2017-01-17 04:25:30 +00:00
|
|
|
// Constructor.
|
2019-04-18 00:46:42 +00:00
|
|
|
// @param tm The receive timeout in srs_utime_t.
|
2019-04-18 00:42:43 +00:00
|
|
|
SrsRecvThread(ISrsMessagePumper* p, SrsRtmpServer* r, srs_utime_t tm, int parent_cid);
|
2014-12-01 14:39:22 +00:00
|
|
|
virtual ~SrsRecvThread();
|
2015-08-21 07:22:40 +00:00
|
|
|
public:
|
|
|
|
virtual int cid();
|
2014-12-01 14:39:22 +00:00
|
|
|
public:
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t start();
|
2014-12-01 14:39:22 +00:00
|
|
|
virtual void stop();
|
2014-12-01 15:38:51 +00:00
|
|
|
virtual void stop_loop();
|
2015-05-23 01:49:15 +00:00
|
|
|
// interface ISrsReusableThread2Handler
|
2014-12-01 14:39:22 +00:00
|
|
|
public:
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t cycle();
|
2017-05-29 10:42:36 +00:00
|
|
|
private:
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t do_cycle();
|
2014-12-01 14:39:22 +00:00
|
|
|
};
|
|
|
|
|
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
|
2014-11-22 09:58:02 +00:00
|
|
|
{
|
|
|
|
private:
|
2014-12-05 15:03:52 +00:00
|
|
|
std::vector<SrsCommonMessage*> queue;
|
2014-12-01 14:53:03 +00:00
|
|
|
SrsRecvThread trd;
|
2014-12-02 09:16:20 +00:00
|
|
|
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;
|
2015-01-07 06:31:42 +00:00
|
|
|
SrsConsumer* _consumer;
|
2014-11-22 09:58:02 +00:00
|
|
|
public:
|
2019-04-09 01:39:16 +00:00
|
|
|
// TODO: FIXME: Refine timeout in time unit.
|
2019-04-18 00:42:43 +00:00
|
|
|
SrsQueueRecvThread(SrsConsumer* consumer, SrsRtmpServer* rtmp_sdk, srs_utime_t tm, int parent_cid);
|
2014-12-01 14:23:05 +00:00
|
|
|
virtual ~SrsQueueRecvThread();
|
2014-12-01 14:53:03 +00:00
|
|
|
public:
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t start();
|
2014-12-01 14:53:03 +00:00
|
|
|
virtual void stop();
|
2014-11-22 09:58:02 +00:00
|
|
|
public:
|
|
|
|
virtual bool empty();
|
2014-11-22 11:15:40 +00:00
|
|
|
virtual int size();
|
2014-12-05 15:03:52 +00:00
|
|
|
virtual SrsCommonMessage* pump();
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t error_code();
|
2017-01-23 08:38:23 +00:00
|
|
|
// interface ISrsMessagePumper
|
2014-11-22 09:58:02 +00:00
|
|
|
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();
|
2014-11-22 09:58:02 +00:00
|
|
|
};
|
|
|
|
|
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
|
2017-01-23 08:38:23 +00:00
|
|
|
class SrsPublishRecvThread : virtual public ISrsMessagePumper, virtual public ISrsReloadHandler
|
2014-12-04 05:43:55 +00:00
|
|
|
#ifdef SRS_PERF_MERGED_READ
|
2017-05-29 12:49:29 +00:00
|
|
|
, virtual public IMergeReadHandler
|
2014-12-04 05:43:55 +00:00
|
|
|
#endif
|
2014-12-01 15:38:51 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsRecvThread trd;
|
2014-12-02 09:16:20 +00:00
|
|
|
SrsRtmpServer* rtmp;
|
2014-12-04 10:21:04 +00:00
|
|
|
SrsRequest* req;
|
2019-04-30 00:24:52 +00:00
|
|
|
// The msgs already got.
|
2014-12-01 15:38:51 +00:00
|
|
|
int64_t _nb_msgs;
|
2017-04-23 12:55:51 +00:00
|
|
|
// 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
|
2014-12-04 10:21:04 +00:00
|
|
|
bool mr;
|
2014-12-03 14:39:25 +00:00
|
|
|
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
|
2014-12-12 13:51:06 +00:00
|
|
|
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;
|
2014-12-01 15:38:51 +00:00
|
|
|
SrsRtmpConn* _conn;
|
2019-04-30 00:24:52 +00:00
|
|
|
// The params for conn callback.
|
2014-12-01 15:38:51 +00:00
|
|
|
SrsSource* _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
|
2017-05-30 01:05:02 +00:00
|
|
|
srs_cond_t error;
|
2019-04-30 00:24:52 +00:00
|
|
|
// The merged context id.
|
2015-08-21 07:22:40 +00:00
|
|
|
int cid;
|
|
|
|
int ncid;
|
2014-12-01 15:38:51 +00:00
|
|
|
public:
|
2019-04-17 01:21:38 +00:00
|
|
|
SrsPublishRecvThread(SrsRtmpServer* rtmp_sdk, SrsRequest* _req,
|
2019-04-18 00:42:43 +00:00
|
|
|
int mr_sock_fd, srs_utime_t tm, SrsRtmpConn* conn, SrsSource* source, int parent_cid);
|
2014-12-01 15:38:51 +00:00
|
|
|
virtual ~SrsPublishRecvThread();
|
|
|
|
public:
|
2019-04-30 00:24:52 +00:00
|
|
|
// Wait for error for some timeout.
|
2019-04-18 00:42:43 +00:00
|
|
|
virtual srs_error_t wait(srs_utime_t tm);
|
2014-12-01 15:38:51 +00:00
|
|
|
virtual int64_t nb_msgs();
|
2017-04-23 12:55:51 +00:00
|
|
|
virtual uint64_t nb_video_frames();
|
2018-01-01 11:39:57 +00:00
|
|
|
virtual srs_error_t error_code();
|
2015-08-21 07:22:40 +00:00
|
|
|
virtual void set_cid(int v);
|
|
|
|
virtual int get_cid();
|
2014-12-01 15:38:51 +00:00
|
|
|
public:
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t start();
|
2014-12-01 15:38:51 +00:00
|
|
|
virtual void stop();
|
2017-01-23 08:38:23 +00:00
|
|
|
// interface ISrsMessagePumper
|
2014-12-01 15:38:51 +00:00
|
|
|
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();
|
2014-12-03 11:27:27 +00:00
|
|
|
// interface IMergeReadHandler
|
2014-12-02 09:16:20 +00:00
|
|
|
public:
|
2014-12-04 05:43:55 +00:00
|
|
|
#ifdef SRS_PERF_MERGED_READ
|
2014-12-03 14:39:25 +00:00
|
|
|
virtual void on_read(ssize_t nread);
|
2014-12-04 05:43:55 +00:00
|
|
|
#endif
|
2014-12-04 10:21:04 +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);
|
2014-12-04 10:21:04 +00:00
|
|
|
private:
|
2019-04-09 01:20:44 +00:00
|
|
|
virtual void set_socket_buffer(srs_utime_t sleep_v);
|
2014-12-01 15:38:51 +00:00
|
|
|
};
|
|
|
|
|
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
|
2017-05-29 11:45:19 +00:00
|
|
|
class SrsHttpRecvThread : public ISrsCoroutineHandler
|
2017-04-30 04:03:31 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsResponseOnlyHttpConn* conn;
|
2017-05-29 11:45:19 +00:00
|
|
|
SrsCoroutine* trd;
|
2017-04-30 04:03:31 +00:00
|
|
|
public:
|
|
|
|
SrsHttpRecvThread(SrsResponseOnlyHttpConn* c);
|
|
|
|
virtual ~SrsHttpRecvThread();
|
|
|
|
public:
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t start();
|
2017-04-30 04:03:31 +00:00
|
|
|
public:
|
2017-07-29 13:39:57 +00:00
|
|
|
virtual srs_error_t pull();
|
2017-04-30 04:03:31 +00:00
|
|
|
// interface ISrsOneCycleThreadHandler
|
|
|
|
public:
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t cycle();
|
2017-04-30 04:03:31 +00:00
|
|
|
};
|
|
|
|
|
2014-11-22 09:58:02 +00:00
|
|
|
#endif
|
|
|
|
|