Create Runner class

Several classes have common active_ and runner_ variables and
stop/start/active routines (such as reader and dispatch).

Create one common class for these to make the interface cleaner
This commit is contained in:
Aaro Altonen 2019-10-24 08:36:54 +03:00
parent bde19e3f7f
commit 0f0a052e54
10 changed files with 366 additions and 347 deletions

View File

@ -50,7 +50,7 @@ kvz_rtp::connection::connection(rtp_format_t fmt, bool reader):
kvz_rtp::connection::~connection() kvz_rtp::connection::~connection()
{ {
if (rtcp_) { if (rtcp_) {
rtcp_->terminate(); rtcp_->stop();
delete rtcp_; delete rtcp_;
} }

View File

@ -14,6 +14,7 @@
#include "frame.hh" #include "frame.hh"
#include "rtcp.hh" #include "rtcp.hh"
#include "runner.hh"
#include "socket.hh" #include "socket.hh"
#include "util.hh" #include "util.hh"
@ -22,13 +23,11 @@ namespace kvz_rtp {
class dispatcher; class dispatcher;
class frame_queue; class frame_queue;
class connection { class connection : public runner {
public: public:
connection(rtp_format_t fmt, bool reader); connection(rtp_format_t fmt, bool reader);
virtual ~connection(); virtual ~connection();
virtual rtp_error_t start() = 0;
uint16_t get_sequence() const; uint16_t get_sequence() const;
uint32_t get_ssrc() const; uint32_t get_ssrc() const;
uint8_t get_payload() const; uint8_t get_payload() const;

View File

@ -12,6 +12,7 @@ kvz_rtp::dispatcher::dispatcher(kvz_rtp::socket *socket):
kvz_rtp::dispatcher::~dispatcher() kvz_rtp::dispatcher::~dispatcher()
{ {
delete runner_; delete runner_;
runner_ = nullptr;
} }
rtp_error_t kvz_rtp::dispatcher::start() rtp_error_t kvz_rtp::dispatcher::start()
@ -30,11 +31,6 @@ rtp_error_t kvz_rtp::dispatcher::stop()
return RTP_OK; return RTP_OK;
} }
bool kvz_rtp::dispatcher::active()
{
return active_;
}
std::condition_variable& kvz_rtp::dispatcher::get_cvar() std::condition_variable& kvz_rtp::dispatcher::get_cvar()
{ {
return cv_; return cv_;

View File

@ -6,6 +6,7 @@
#include "queue.hh" #include "queue.hh"
#include "socket.hh" #include "socket.hh"
#include "runner.hh"
#include "util.hh" #include "util.hh"
namespace kvz_rtp { namespace kvz_rtp {
@ -30,15 +31,11 @@ namespace kvz_rtp {
* experiences to very small (<50 us even for large frames [>170 kB]) */ * experiences to very small (<50 us even for large frames [>170 kB]) */
typedef struct transaction transaction_t; typedef struct transaction transaction_t;
class dispatcher { class dispatcher : public runner {
public: public:
dispatcher(kvz_rtp::socket *socket); dispatcher(kvz_rtp::socket *socket);
~dispatcher(); ~dispatcher();
rtp_error_t start();
rtp_error_t stop();
bool active();
/* Add new transaction to dispatcher's task queue /* Add new transaction to dispatcher's task queue
* The task queue is emptied in FIFO style */ * The task queue is emptied in FIFO style */
rtp_error_t trigger_send(kvz_rtp::transaction_t *transaction); rtp_error_t trigger_send(kvz_rtp::transaction_t *transaction);
@ -62,9 +59,6 @@ namespace kvz_rtp {
std::mutex q_mtx_; std::mutex q_mtx_;
std::queue<kvz_rtp::transaction_t *> tasks_; std::queue<kvz_rtp::transaction_t *> tasks_;
bool active_;
std::thread *runner_;
kvz_rtp::socket *socket_; kvz_rtp::socket *socket_;
}; };
}; };

View File

@ -12,7 +12,6 @@
kvz_rtp::reader::reader(rtp_format_t fmt, std::string src_addr, int src_port): kvz_rtp::reader::reader(rtp_format_t fmt, std::string src_addr, int src_port):
connection(fmt, true), connection(fmt, true),
active_(false),
src_addr_(src_addr), src_addr_(src_addr),
src_port_(src_port), src_port_(src_port),
recv_hook_arg_(nullptr), recv_hook_arg_(nullptr),
@ -106,11 +105,6 @@ kvz_rtp::frame::rtp_frame *kvz_rtp::reader::pull_frame()
return nextFrame; return nextFrame;
} }
bool kvz_rtp::reader::active()
{
return active_;
}
uint8_t *kvz_rtp::reader::get_recv_buffer() const uint8_t *kvz_rtp::reader::get_recv_buffer() const
{ {
return recv_buffer_; return recv_buffer_;

View File

@ -16,11 +16,13 @@ namespace kvz_rtp {
/* NOTE: this operation is blocking */ /* NOTE: this operation is blocking */
kvz_rtp::frame::rtp_frame *pull_frame(); kvz_rtp::frame::rtp_frame *pull_frame();
// open socket and start runner_ /* Open socket, start frame receiver and RTCP
*
* Return RTP_OK on success
* Return RTP_MEMORY_ERROR if memory deallocation failed
* Return RTP_GENERIC_ERROR for any other error */
rtp_error_t start(); rtp_error_t start();
bool active();
bool recv_hook_installed(); bool recv_hook_installed();
void recv_hook(kvz_rtp::frame::rtp_frame *frame); void recv_hook(kvz_rtp::frame::rtp_frame *frame);
void install_recv_hook(void *arg, void (*hook)(void *arg, kvz_rtp::frame::rtp_frame *)); void install_recv_hook(void *arg, void (*hook)(void *arg, kvz_rtp::frame::rtp_frame *));
@ -44,15 +46,11 @@ namespace kvz_rtp {
void return_frame(kvz_rtp::frame::rtp_frame *frame); void return_frame(kvz_rtp::frame::rtp_frame *frame);
private: private:
// TODO implement ring buffer
bool active_;
// connection-related stuff // connection-related stuff
std::string src_addr_; std::string src_addr_;
int src_port_; int src_port_;
// receiver thread related stuff /* receiver thread related stuff */
std::thread *runner_;
uint8_t *recv_buffer_; /* buffer for incoming packet (MAX_PACKET) */ uint8_t *recv_buffer_; /* buffer for incoming packet (MAX_PACKET) */
uint32_t recv_buffer_len_; /* buffer length */ uint32_t recv_buffer_len_; /* buffer length */

View File

@ -23,7 +23,7 @@ kvz_rtp::rtcp::rtcp(uint32_t ssrc, bool receiver):
tp_(0), tc_(0), tn_(0), pmembers_(0), tp_(0), tc_(0), tn_(0), pmembers_(0),
members_(0), senders_(0), rtcp_bandwidth_(0), members_(0), senders_(0), rtcp_bandwidth_(0),
we_sent_(0), avg_rtcp_pkt_pize_(0), rtcp_pkt_count_(0), we_sent_(0), avg_rtcp_pkt_pize_(0), rtcp_pkt_count_(0),
initial_(true), active_(false), num_receivers_(0) initial_(true), num_receivers_(0)
{ {
ssrc_ = ssrc; ssrc_ = ssrc;
@ -136,7 +136,7 @@ rtp_error_t kvz_rtp::rtcp::start()
return RTP_OK; return RTP_OK;
} }
rtp_error_t kvz_rtp::rtcp::terminate() rtp_error_t kvz_rtp::rtcp::stop()
{ {
if (runner_ == nullptr) if (runner_ == nullptr)
goto free_mem; goto free_mem;
@ -170,11 +170,6 @@ free_mem:
return RTP_OK; return RTP_OK;
} }
bool kvz_rtp::rtcp::active() const
{
return active_;
}
bool kvz_rtp::rtcp::receiver() const bool kvz_rtp::rtcp::receiver() const
{ {
return receiver_; return receiver_;

View File

@ -7,6 +7,7 @@
#include "clock.hh" #include "clock.hh"
#include "frame.hh" #include "frame.hh"
#include "runner.hh"
#include "socket.hh" #include "socket.hh"
#include "util.hh" #include "util.hh"
@ -21,7 +22,7 @@ namespace kvz_rtp {
const int MAX_MISORDER = 100; const int MAX_MISORDER = 100;
const int MIN_TIMEOUT = 5000; const int MIN_TIMEOUT = 5000;
class rtcp { class rtcp : public runner {
public: public:
rtcp(uint32_t ssrc, bool receiver); rtcp(uint32_t ssrc, bool receiver);
~rtcp(); ~rtcp();
@ -34,11 +35,7 @@ namespace kvz_rtp {
/* End the RTCP session and send RTCP BYE to all participants /* End the RTCP session and send RTCP BYE to all participants
* *
* return RTP_OK on success */ * return RTP_OK on success */
rtp_error_t terminate(); rtp_error_t stop();
/* return true if the connection is still considered active
* and RTCP transmissions should continue */
bool active() const;
/* return true if this RTCP instance belongs to an RTP receiver /* return true if this RTCP instance belongs to an RTP receiver
* and a receiver report should be generated, otherwise sender report is generated */ * and a receiver report should be generated, otherwise sender report is generated */
@ -193,7 +190,6 @@ namespace kvz_rtp {
rtp_error_t generate_sender_report(); rtp_error_t generate_sender_report();
rtp_error_t generate_receiver_report(); rtp_error_t generate_receiver_report();
std::thread *runner_;
bool receiver_; bool receiver_;
/* TODO: time_t?? */ /* TODO: time_t?? */
@ -227,11 +223,6 @@ namespace kvz_rtp {
/* Flag that is true if the application has not yet sent an RTCP packet. */ /* Flag that is true if the application has not yet sent an RTCP packet. */
bool initial_; bool initial_;
/* Flag that is true if the connection is still considered open.
* When clients decided to leave, he calls rtcp->terminate()
* which stops the rtcp runner and RTCP BYE message to all participants */
bool active_;
/* Copy of our own current SSRC */ /* Copy of our own current SSRC */
uint32_t ssrc_; uint32_t ssrc_;

30
src/runner.cc Normal file
View File

@ -0,0 +1,30 @@
#include "runner.hh"
kvz_rtp::runner::runner():
active_(false), runner_(nullptr)
{
}
kvz_rtp::runner::~runner()
{
active_ = false;
if (runner_)
delete runner_;
}
rtp_error_t kvz_rtp::runner::start()
{
active_ = true;
}
rtp_error_t kvz_rtp::runner::stop()
{
active_ = false;
active_ = false;
}
bool kvz_rtp::runner::active()
{
return active_;
}

22
src/runner.hh Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <thread>
#include "util.hh"
namespace kvz_rtp {
class runner {
public:
runner();
virtual ~runner();
virtual rtp_error_t start();
virtual rtp_error_t stop();
virtual bool active();
protected:
bool active_;
std::thread *runner_;
};
};